From b936d6f708eef528ca77c32c21f572d5adcbee49 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 22 Aug 2023 17:47:10 +0800 Subject: [PATCH] Noto universal fallback (#25) * fix memory leak in wasm worker [#21] * worker: move freeing font data to end [#21] * Update example scripts to build off the noto repository. [#16] * Ignore CJK since they will never get rendered in MapLibre with default settings. * Fix file paths and remove unavailable fonts. * command line has name override option [#16] * set a single output name e.g. Noto Sans Regular instead of determining one from fonts, which may be too long. * Web worker passes 0 for face name override [#16] --- app/public/worker.js | 8 +-- examples/.gitignore | 1 + examples/NotoSansBold.sh | 55 +++++++-------- examples/NotoSansRegular.sh | 131 ++++++++++++++++++------------------ main.cpp | 36 ++++++---- 5 files changed, 123 insertions(+), 108 deletions(-) create mode 100644 examples/.gitignore diff --git a/app/public/worker.js b/app/public/worker.js index c8edc3e..d38c766 100644 --- a/app/public/worker.js +++ b/app/public/worker.js @@ -2,7 +2,7 @@ importScripts("sdfglyph.js"); self.onmessage = function (message) { const { stackId, buffers } = message.data; - const fontstack_ptr = Module.ccall("create_fontstack", "number", [], []); + const fontstack_ptr = Module.ccall("create_fontstack", "number", ["number"], [0]); const font_datas = []; @@ -76,10 +76,10 @@ self.onmessage = function (message) { ); } - Module._free(s); - Module.ccall("free_fontstack", "number", ["number"], [fontstack_ptr]); - for (let data_ptr of font_datas) { Module._free(data_ptr); } + + Module._free(s); + Module.ccall("free_fontstack", "number", ["number"], [fontstack_ptr]); }; diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..cfcfc15 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +notofonts.github.io* diff --git a/examples/NotoSansBold.sh b/examples/NotoSansBold.sh index 963748b..4e59c32 100755 --- a/examples/NotoSansBold.sh +++ b/examples/NotoSansBold.sh @@ -1,27 +1,28 @@ -../font-maker pbfs/NotoSans-Bold \ -fonts/Noto-unhinted/NotoSans-Bold.ttf \ -fonts/Noto-unhinted/NotoSansArmenian-Bold.ttf \ -fonts/Noto-unhinted/NotoSansHebrew-Bold.ttf \ -fonts/Noto-unhinted/NotoSansArabic-Bold.ttf \ -fonts/Noto-unhinted/NotoSansThaana-Bold.ttf \ -fonts/Noto-unhinted/NotoSansDevanagari-Bold.ttf \ -fonts/Noto-unhinted/NotoSansBengali-Bold.ttf \ -fonts/Noto-unhinted/NotoSansGurmukhi-Bold.ttf \ -fonts/Noto-unhinted/NotoSansGujarati-Bold.ttf \ -fonts/Noto-unhinted/NotoSansOriya-Bold.ttf \ -fonts/Noto-unhinted/NotoSansTamil-Bold.ttf \ -fonts/Noto-unhinted/NotoSansTelugu-Bold.ttf \ -fonts/Noto-unhinted/NotoSansKannada-Bold.ttf \ -fonts/Noto-unhinted/NotoSansMalayalam-Bold.ttf \ -fonts/Noto-unhinted/NotoSansSinhala-Bold.ttf \ -fonts/Noto-unhinted/NotoSansThai-Bold.ttf \ -fonts/Noto-unhinted/NotoSansLao-Bold.ttf \ -fonts/Noto-unhinted/NotoSansTibetan-Bold.ttf \ -fonts/Noto-unhinted/NotoSansMyanmar-Bold.ttf \ -fonts/Noto-unhinted/NotoSansGeorgian-Bold.ttf \ -fonts/Noto-unhinted/NotoSansEthiopic-Bold.ttf \ -fonts/Noto-unhinted/NotoSansCherokee-Bold.ttf \ -fonts/Noto-unhinted/NotoSansKhmer-Bold.ttf \ -fonts/Noto-unhinted/NotoSansSymbols-Bold.ttf \ -fonts/Noto-unhinted/NotoSansCJKtc-Bold.otf \ -fonts/Noto-unhinted/NotoSansCham-Bold.ttf \ No newline at end of file +ROOT=notofonts.github.io-main/fonts/ +PATH=unhinted/ttf + +../font-maker --name "Noto Sans Bold" out2 \ +$ROOT/NotoSans/$PATH/NotoSans-Bold.ttf \ +$ROOT/NotoSansArmenian/$PATH/NotoSansArmenian-Bold.ttf \ +$ROOT/NotoSansHebrew/$PATH/NotoSansHebrew-Bold.ttf \ +$ROOT/NotoSansArabic/$PATH/NotoSansArabic-Bold.ttf \ +$ROOT/NotoSansThaana/$PATH/NotoSansThaana-Bold.ttf \ +$ROOT/NotoSansDevanagari/$PATH/NotoSansDevanagari-Bold.ttf \ +$ROOT/NotoSansBengali/$PATH/NotoSansBengali-Bold.ttf \ +$ROOT/NotoSansGurmukhi/$PATH/NotoSansGurmukhi-Bold.ttf \ +$ROOT/NotoSansGujarati/$PATH/NotoSansGujarati-Bold.ttf \ +$ROOT/NotoSansOriya/$PATH/NotoSansOriya-Bold.ttf \ +$ROOT/NotoSansTamil/$PATH/NotoSansTamil-Bold.ttf \ +$ROOT/NotoSansTelugu/$PATH/NotoSansTelugu-Bold.ttf \ +$ROOT/NotoSansKannada/$PATH/NotoSansKannada-Bold.ttf \ +$ROOT/NotoSansMalayalam/$PATH/NotoSansMalayalam-Bold.ttf \ +$ROOT/NotoSansSinhala/$PATH/NotoSansSinhala-Bold.ttf \ +$ROOT/NotoSansThai/$PATH/NotoSansThai-Bold.ttf \ +$ROOT/NotoSansLao/$PATH/NotoSansLao-Bold.ttf \ +$ROOT/NotoSansMyanmar/$PATH/NotoSansMyanmar-Bold.ttf \ +$ROOT/NotoSansGeorgian/$PATH/NotoSansGeorgian-Bold.ttf \ +$ROOT/NotoSansEthiopic/$PATH/NotoSansEthiopic-Bold.ttf \ +$ROOT/NotoSansCherokee/$PATH/NotoSansCherokee-Bold.ttf \ +$ROOT/NotoSansKhmer/$PATH/NotoSansKhmer-Bold.ttf \ +$ROOT/NotoSansSymbols/$PATH/NotoSansSymbols-Bold.ttf \ +$ROOT/NotoSansCham/$PATH/NotoSansCham-Bold.ttf \ No newline at end of file diff --git a/examples/NotoSansRegular.sh b/examples/NotoSansRegular.sh index 6267845..5655c78 100755 --- a/examples/NotoSansRegular.sh +++ b/examples/NotoSansRegular.sh @@ -1,65 +1,66 @@ -../font-maker pbfs/NotoSans-Regular \ -fonts/Noto-unhinted/NotoSans-Regular.ttf \ -fonts/Noto-unhinted/NotoSansArmenian-Regular.ttf \ -fonts/Noto-unhinted/NotoSansHebrew-Regular.ttf \ -fonts/Noto-unhinted/NotoSansArabic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSyriacEastern-Regular.ttf \ -fonts/Noto-unhinted/NotoSansThaana-Regular.ttf \ -fonts/Noto-unhinted/NotoSansNKo-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSamaritan-Regular.ttf \ -fonts/Noto-unhinted/NotoSansMandaic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansDevanagari-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBengali-Regular.ttf \ -fonts/Noto-unhinted/NotoSansGurmukhi-Regular.ttf \ -fonts/Noto-unhinted/NotoSansGujarati-Regular.ttf \ -fonts/Noto-unhinted/NotoSansOriya-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTamil-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTelugu-Regular.ttf \ -fonts/Noto-unhinted/NotoSansKannada-Regular.ttf \ -fonts/Noto-unhinted/NotoSansMalayalam-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSinhala-Regular.ttf \ -fonts/Noto-unhinted/NotoSansThai-Regular.ttf \ -fonts/Noto-unhinted/NotoSansLao-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTibetan-Regular.ttf \ -fonts/Noto-unhinted/NotoSansMyanmar-Regular.ttf \ -fonts/Noto-unhinted/NotoSansGeorgian-Regular.ttf \ -fonts/Noto-unhinted/NotoSansEthiopic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansCherokee-Regular.ttf \ -fonts/Noto-unhinted/NotoSansCanadianAboriginal-Regular.ttf \ -fonts/Noto-unhinted/NotoSansOgham-Regular.ttf \ -fonts/Noto-unhinted/NotoSansRunic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTagalog-Regular.ttf \ -fonts/Noto-unhinted/NotoSansHanunoo-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBuhid-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTagbanwa-Regular.ttf \ -fonts/Noto-unhinted/NotoSansKhmer-Regular.ttf \ -fonts/Noto-unhinted/NotoSansMongolian-Regular.ttf \ -fonts/Noto-unhinted/NotoSansLimbu-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTaiLe-Regular.ttf \ -fonts/Noto-unhinted/NotoSansNewTaiLue-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBuginese-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTaiTham-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBalinese-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSundanese-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBatak-Regular.ttf \ -fonts/Noto-unhinted/NotoSansLepcha-Regular.ttf \ -fonts/Noto-unhinted/NotoSansOlChiki-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSymbols-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSymbols2-Regular.ttf \ -fonts/Noto-unhinted/NotoSansGlagolitic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansCoptic-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTifinagh-Regular.ttf \ -fonts/Noto-unhinted/NotoSansCJKtc-Regular.otf \ -fonts/Noto-unhinted/NotoSansYi-Regular.ttf \ -fonts/Noto-unhinted/NotoSansLisu-Regular.ttf \ -fonts/Noto-unhinted/NotoSansVai-Regular.ttf \ -fonts/Noto-unhinted/NotoSansBamum-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSylotiNagri-Regular.ttf \ -fonts/Noto-unhinted/NotoSansPhagsPa-Regular.ttf \ -fonts/Noto-unhinted/NotoSansSaurashtra-Regular.ttf \ -fonts/Noto-unhinted/NotoSansKayahLi-Regular.ttf \ -fonts/Noto-unhinted/NotoSansRejang-Regular.ttf \ -fonts/Noto-unhinted/NotoSansJavanese-Regular.ttf \ -fonts/Noto-unhinted/NotoSansCham-Regular.ttf \ -fonts/Noto-unhinted/NotoSansTaiViet-Regular.ttf \ -fonts/Noto-unhinted/NotoSansMeeteiMayek-Regular.ttf \ No newline at end of file +ROOT=notofonts.github.io-main/fonts/ +PATH=unhinted/ttf + +../font-maker --name "Noto Sans Regular" out \ +$ROOT/NotoSans/$PATH/NotoSans-Regular.ttf \ +$ROOT/NotoSansArmenian/$PATH/NotoSansArmenian-Regular.ttf \ +$ROOT/NotoSansHebrew/$PATH/NotoSansHebrew-Regular.ttf \ +$ROOT/NotoSansArabic/$PATH/NotoSansArabic-Regular.ttf \ +$ROOT/NotoSansSyriac/$PATH/NotoSansSyriac-Regular.ttf \ +$ROOT/NotoSansThaana/$PATH/NotoSansThaana-Regular.ttf \ +$ROOT/NotoSansNKo/$PATH/NotoSansNKo-Regular.ttf \ +$ROOT/NotoSansSamaritan/$PATH/NotoSansSamaritan-Regular.ttf \ +$ROOT/NotoSansDevanagari/$PATH/NotoSansDevanagari-Regular.ttf \ +$ROOT/NotoSansBengali/$PATH/NotoSansBengali-Regular.ttf \ +$ROOT/NotoSansGurmukhi/$PATH/NotoSansGurmukhi-Regular.ttf \ +$ROOT/NotoSansGujarati/$PATH/NotoSansGujarati-Regular.ttf \ +$ROOT/NotoSansOriya/$PATH/NotoSansOriya-Regular.ttf \ +$ROOT/NotoSansTamil/$PATH/NotoSansTamil-Regular.ttf \ +$ROOT/NotoSansTelugu/$PATH/NotoSansTelugu-Regular.ttf \ +$ROOT/NotoSansKannada/$PATH/NotoSansKannada-Regular.ttf \ +$ROOT/NotoSansMalayalam/$PATH/NotoSansMalayalam-Regular.ttf \ +$ROOT/NotoSansSinhala/$PATH/NotoSansSinhala-Regular.ttf \ +$ROOT/NotoSansThai/$PATH/NotoSansThai-Regular.ttf \ +$ROOT/NotoSansLao/$PATH/NotoSansLao-Regular.ttf \ +$ROOT/NotoSerifTibetan/$PATH/NotoSerifTibetan-Regular.ttf \ +$ROOT/NotoSansMyanmar/$PATH/NotoSansMyanmar-Regular.ttf \ +$ROOT/NotoSansGeorgian/$PATH/NotoSansGeorgian-Regular.ttf \ +$ROOT/NotoSansEthiopic/$PATH/NotoSansEthiopic-Regular.ttf \ +$ROOT/NotoSansCherokee/$PATH/NotoSansCherokee-Regular.ttf \ +$ROOT/NotoSansCanadianAboriginal/$PATH/NotoSansCanadianAboriginal-Regular.ttf \ +$ROOT/NotoSansOgham/$PATH/NotoSansOgham-Regular.ttf \ +$ROOT/NotoSansRunic/$PATH/NotoSansRunic-Regular.ttf \ +$ROOT/NotoSansTagalog/$PATH/NotoSansTagalog-Regular.ttf \ +$ROOT/NotoSansHanunoo/$PATH/NotoSansHanunoo-Regular.ttf \ +$ROOT/NotoSansBuhid/$PATH/NotoSansBuhid-Regular.ttf \ +$ROOT/NotoSansTagbanwa/$PATH/NotoSansTagbanwa-Regular.ttf \ +$ROOT/NotoSansKhmer/$PATH/NotoSansKhmer-Regular.ttf \ +$ROOT/NotoSansMongolian/$PATH/NotoSansMongolian-Regular.ttf \ +$ROOT/NotoSansLimbu/$PATH/NotoSansLimbu-Regular.ttf \ +$ROOT/NotoSansTaiLe/$PATH/NotoSansTaiLe-Regular.ttf \ +$ROOT/NotoSansNewTaiLue/$PATH/NotoSansNewTaiLue-Regular.ttf \ +$ROOT/NotoSansBuginese/$PATH/NotoSansBuginese-Regular.ttf \ +$ROOT/NotoSansTaiTham/$PATH/NotoSansTaiTham-Regular.ttf \ +$ROOT/NotoSansBalinese/$PATH/NotoSansBalinese-Regular.ttf \ +$ROOT/NotoSansSundanese/$PATH/NotoSansSundanese-Regular.ttf \ +$ROOT/NotoSansBatak/$PATH/NotoSansBatak-Regular.ttf \ +$ROOT/NotoSansLepcha/$PATH/NotoSansLepcha-Regular.ttf \ +$ROOT/NotoSansOlChiki/$PATH/NotoSansOlChiki-Regular.ttf \ +$ROOT/NotoSansSymbols/$PATH/NotoSansSymbols-Regular.ttf \ +$ROOT/NotoSansSymbols2/$PATH/NotoSansSymbols2-Regular.ttf \ +$ROOT/NotoSansGlagolitic/$PATH/NotoSansGlagolitic-Regular.ttf \ +$ROOT/NotoSansCoptic/$PATH/NotoSansCoptic-Regular.ttf \ +$ROOT/NotoSansTifinagh/$PATH/NotoSansTifinagh-Regular.ttf \ +$ROOT/NotoSansYi/$PATH/NotoSansYi-Regular.ttf \ +$ROOT/NotoSansLisu/$PATH/NotoSansLisu-Regular.ttf \ +$ROOT/NotoSansVai/$PATH/NotoSansVai-Regular.ttf \ +$ROOT/NotoSansBamum/$PATH/NotoSansBamum-Regular.ttf \ +$ROOT/NotoSansSylotiNagri/$PATH/NotoSansSylotiNagri-Regular.ttf \ +$ROOT/NotoSansPhagsPa/$PATH/NotoSansPhags-Pa-Regular.ttf \ +$ROOT/NotoSansSaurashtra/$PATH/NotoSansSaurashtra-Regular.ttf \ +$ROOT/NotoSansKayahLi/$PATH/NotoSansKayahLi-Regular.ttf \ +$ROOT/NotoSansRejang/$PATH/NotoSansRejang-Regular.ttf \ +$ROOT/NotoSansJavanese/$PATH/NotoSansJavanese-Regular.ttf \ +$ROOT/NotoSansCham/$PATH/NotoSansCham-Regular.ttf \ +$ROOT/NotoSansTaiViet/$PATH/NotoSansTaiViet-Regular.ttf \ +$ROOT/NotoSansMeeteiMayek/$PATH/NotoSansMeeteiMayek-Regular.ttf \ No newline at end of file diff --git a/main.cpp b/main.cpp index f112129..3f0e461 100644 --- a/main.cpp +++ b/main.cpp @@ -91,6 +91,7 @@ struct fontstack { std::vector *data; std::set *seen_face_names; std::string *name; + bool auto_name; }; struct glyph_buffer { @@ -99,13 +100,20 @@ struct glyph_buffer { }; extern "C" { - fontstack *create_fontstack() { + fontstack *create_fontstack(const char *name) { fontstack *f = (fontstack *)malloc(sizeof(fontstack)); f->faces = new std::vector; f->data = new std::vector; - f->name = new std::string; f->seen_face_names = new std::set; + if (name != nullptr) { + f->name = new std::string(name); + f->auto_name = false; + } else { + f->name = new std::string; + f->auto_name = true; + } + FT_Library library = nullptr; FT_Error error = FT_Init_FreeType(&library); @@ -130,17 +138,19 @@ extern "C" { FT_Set_Char_Size(face, 0, static_cast(size * (1 << 6)), 0, 0); f->faces->push_back(face); - std::string combined_name = std::string(face->family_name); - if (face->style_name != NULL) { - combined_name += " " + std::string(face->style_name); - } + if (f->auto_name) { + std::string combined_name = std::string(face->family_name); + if (face->style_name != NULL) { + combined_name += " " + std::string(face->style_name); + } - if (f->seen_face_names->count(combined_name) == 0) { - if (f->seen_face_names->size() > 0) { - *f->name += ","; + if (f->seen_face_names->count(combined_name) == 0) { + if (f->seen_face_names->size() > 0) { + *f->name += ","; + } + *f->name += combined_name; + f->seen_face_names->insert(combined_name); } - *f->name += combined_name; - f->seen_face_names->insert(combined_name); } } @@ -196,6 +206,7 @@ int main(int argc, char *argv[]) cmd_options.add_options() ("output", "Output directory", cxxopts::value()) ("fonts", "Input fonts TTF or OTF", cxxopts::value>()) + ("name", "Override output fontstack name", cxxopts::value()) ; cmd_options.parse_positional({"output","fonts"}); auto result = cmd_options.parse(argc, argv); @@ -213,7 +224,7 @@ int main(int argc, char *argv[]) if (ghc::filesystem::exists(output_dir)) ghc::filesystem::remove_all(output_dir); ghc::filesystem::create_directory(output_dir); - fontstack *f = create_fontstack(); + fontstack *f = create_fontstack(result["name"].as().c_str()); for (auto const &font : fonts) { std::ifstream file(font, std::ios::binary | std::ios::ate); @@ -223,6 +234,7 @@ int main(int argc, char *argv[]) char * buffer = (char *)malloc(size); f->data->push_back(buffer); file.read(buffer, size); + std::cout << "Adding " << font << std::endl; fontstack_add_face(f,(FT_Byte *)buffer,size); }