/* * The Font Converter's WOFF2 codec: Google's woff2 library (with brotli) * behind four plain C functions. The page copies a font in, asks for the * output size, and copies the result out. WOFF 1.0, the outline conversion * and everything else are done in TypeScript. */ #include #include #include #include #include #include #include extern "C" { EMSCRIPTEN_KEEPALIVE void *w2_alloc(size_t n) { return malloc(n ? n : 1); } EMSCRIPTEN_KEEPALIVE void w2_free(void *p) { free(p); } /* * Decodes WOFF2 into a buffer that grows as needed: the size a file declares * is not always the size it rebuilds to (Font Awesome's, with untransformed * glyf, come out larger), so a fixed buffer of that size can fail. * Returns the decoded length, or 0 on error; w2_decoded() points at it until * the next call. */ static std::string decoded; EMSCRIPTEN_KEEPALIVE size_t w2_decode(const uint8_t *data, size_t len) { decoded.clear(); woff2::WOFF2StringOut out(&decoded); out.SetMaxSize(256 * 1024 * 1024); if (!woff2::ConvertWOFF2ToTTF(data, len, &out)) { decoded.clear(); return 0; } return out.Size(); } EMSCRIPTEN_KEEPALIVE const uint8_t *w2_decoded(void) { return reinterpret_cast(decoded.data()); } /* The most a font can grow to as WOFF2, to size the output buffer. */ EMSCRIPTEN_KEEPALIVE size_t w2_max_encoded_size(const uint8_t *data, size_t len) { return woff2::MaxWOFF2CompressedSize(data, len); } /* Encodes a TrueType/OpenType font as WOFF2. *out_len is the buffer size in, the result size out. Returns 1 on success. */ EMSCRIPTEN_KEEPALIVE int w2_encode(const uint8_t *data, size_t len, uint8_t *out, size_t *out_len) { return woff2::ConvertTTFToWOFF2(data, len, out, out_len) ? 1 : 0; } }