diff --git a/CHANGELOG.md b/CHANGELOG.md index 08b970e..dc8aa19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Removed Visual Studio solution and Makefile - now has to be generated by CMake - CMake configuration overhaul, added installation configuration - Switched to libpng as the primary PNG file encoder -- Fixed a bug that prevented glyph 0 to be specified in a glyphset +- Added `-varfont` option to configure variables of variable fonts ### Version 1.2.2 (2021-09-06) diff --git a/README.md b/README.md index 22b242b..23f0e7c 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,7 @@ Use the following command line arguments for the standalone version of the atlas ### Input - `-font ` (required) – sets the input font file. + - Alternatively, use `-varfont ` to configure a variable font. - `-charset ` – sets the character set. The ASCII charset will be used if not specified. See [the syntax specification](#character-set-specification-syntax) of `charset.txt`. - `-glyphset ` – sets the set of input glyphs using their indices within the font file. See [the syntax specification](#glyph-set-specification). - `-fontscale ` – applies a scaling transformation to the font's glyphs. Mainly to be used to generate multiple sizes in a single atlas, otherwise use [`-size`](#glyph-configuration). diff --git a/msdf-atlas-gen/main.cpp b/msdf-atlas-gen/main.cpp index 12dabdb..cb818dc 100644 --- a/msdf-atlas-gen/main.cpp +++ b/msdf-atlas-gen/main.cpp @@ -52,7 +52,9 @@ MSDF Atlas Generator by Viktor Chlumsky v)" MSDF_ATLAS_VERSION_STRING R"( (with INPUT SPECIFICATION -font - Specifies the input TrueType / OpenType font file. This is required. + Specifies the input TrueType / OpenType font file. A font specification is required. + -varfont + Specifies an input variable font file and configures its variables. -charset Specifies the input character set. Refer to the documentation for format of charset specification. Defaults to ASCII. -glyphset @@ -196,8 +198,32 @@ static bool cmpExtension(const char *path, const char *ext) { return true; } +static msdfgen::FontHandle * loadVarFont(msdfgen::FreetypeHandle *library, const char *filename) { + std::string buffer; + while (*filename && *filename != '?') + buffer.push_back(*filename++); + msdfgen::FontHandle *font = msdfgen::loadFont(library, buffer.c_str()); + if (font && *filename++ == '?') { + do { + buffer.clear(); + while (*filename && *filename != '=') + buffer.push_back(*filename++); + if (*filename == '=') { + double value = 0; + int skip = 0; + if (sscanf(++filename, "%lf%n", &value, &skip) == 1) { + msdfgen::setFontVariationAxis(library, font, buffer.c_str(), value); + filename += skip; + } + } + } while (*filename++ == '&'); + } + return font; +} + struct FontInput { const char *fontFilename; + bool variableFont; GlyphIdentifierType glyphIdentifierType; const char *charsetFilename; double fontScale; @@ -356,6 +382,13 @@ int main(int argc, const char * const *argv) { } ARG_CASE("-font", 1) { fontInput.fontFilename = argv[++argPos]; + fontInput.variableFont = false; + ++argPos; + continue; + } + ARG_CASE("-varfont", 1) { + fontInput.fontFilename = argv[++argPos]; + fontInput.variableFont = true; ++argPos; continue; } @@ -783,13 +816,13 @@ int main(int argc, const char * const *argv) { msdfgen::deinitializeFreetype(ft); } } - bool load(const char *fontFilename) { + bool load(const char *fontFilename, bool isVarFont) { if (ft && fontFilename) { if (this->fontFilename && !strcmp(this->fontFilename, fontFilename)) return true; if (font) msdfgen::destroyFont(font); - if ((font = msdfgen::loadFont(ft, fontFilename))) { + if ((font = isVarFont ? loadVarFont(ft, fontFilename) : msdfgen::loadFont(ft, fontFilename))) { this->fontFilename = fontFilename; return true; } @@ -803,7 +836,7 @@ int main(int argc, const char * const *argv) { } font; for (FontInput &fontInput : fontInputs) { - if (!font.load(fontInput.fontFilename)) + if (!font.load(fontInput.fontFilename, fontInput.variableFont)) ABORT("Failed to load specified font file."); if (fontInput.fontScale <= 0) fontInput.fontScale = 1;