Skip to content

Commit

Permalink
Noto universal fallback (#25)
Browse files Browse the repository at this point in the history
* 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]
  • Loading branch information
bdon authored Aug 22, 2023
1 parent 46fac6c commit b936d6f
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 108 deletions.
8 changes: 4 additions & 4 deletions app/public/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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]);
};
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
notofonts.github.io*
55 changes: 28 additions & 27 deletions examples/NotoSansBold.sh
Original file line number Diff line number Diff line change
@@ -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
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
131 changes: 66 additions & 65 deletions examples/NotoSansRegular.sh
Original file line number Diff line number Diff line change
@@ -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
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
36 changes: 24 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ struct fontstack {
std::vector<char *> *data;
std::set<std::string> *seen_face_names;
std::string *name;
bool auto_name;
};

struct glyph_buffer {
Expand All @@ -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<FT_Face>;
f->data = new std::vector<char *>;
f->name = new std::string;
f->seen_face_names = new std::set<std::string>;

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);

Expand All @@ -130,17 +138,19 @@ extern "C" {
FT_Set_Char_Size(face, 0, static_cast<FT_F26Dot6>(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);
}
}

Expand Down Expand Up @@ -196,6 +206,7 @@ int main(int argc, char *argv[])
cmd_options.add_options()
("output", "Output directory", cxxopts::value<string>())
("fonts", "Input fonts TTF or OTF", cxxopts::value<vector<string>>())
("name", "Override output fontstack name", cxxopts::value<string>())
;
cmd_options.parse_positional({"output","fonts"});
auto result = cmd_options.parse(argc, argv);
Expand All @@ -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<string>().c_str());

for (auto const &font : fonts) {
std::ifstream file(font, std::ios::binary | std::ios::ate);
Expand All @@ -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);
}

Expand Down

0 comments on commit b936d6f

Please sign in to comment.