Variable font support

This commit is contained in:
Chlumsky 2023-01-21 15:42:07 +01:00
parent 6932b35c00
commit 718a0ca77c
3 changed files with 39 additions and 5 deletions

View File

@ -6,7 +6,7 @@
- Removed Visual Studio solution and Makefile - now has to be generated by CMake - Removed Visual Studio solution and Makefile - now has to be generated by CMake
- CMake configuration overhaul, added installation configuration - CMake configuration overhaul, added installation configuration
- Switched to libpng as the primary PNG file encoder - 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) ### Version 1.2.2 (2021-09-06)

View File

@ -46,6 +46,7 @@ Use the following command line arguments for the standalone version of the atlas
### Input ### Input
- `-font <fontfile.ttf/otf>` (required) &ndash; sets the input font file. - `-font <fontfile.ttf/otf>` (required) &ndash; sets the input font file.
- Alternatively, use `-varfont <fontfile.ttf/otf?var0=value0&var1=value1>` to configure a variable font.
- `-charset <charset.txt>` &ndash; sets the character set. The ASCII charset will be used if not specified. See [the syntax specification](#character-set-specification-syntax) of `charset.txt`. - `-charset <charset.txt>` &ndash; 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 <glyphset.txt>` &ndash; sets the set of input glyphs using their indices within the font file. See [the syntax specification](#glyph-set-specification). - `-glyphset <glyphset.txt>` &ndash; sets the set of input glyphs using their indices within the font file. See [the syntax specification](#glyph-set-specification).
- `-fontscale <scale>` &ndash; 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). - `-fontscale <scale>` &ndash; 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).

View File

@ -52,7 +52,9 @@ MSDF Atlas Generator by Viktor Chlumsky v)" MSDF_ATLAS_VERSION_STRING R"( (with
INPUT SPECIFICATION INPUT SPECIFICATION
-font <filename.ttf/otf> -font <filename.ttf/otf>
Specifies the input TrueType / OpenType font file. This is required. Specifies the input TrueType / OpenType font file. A font specification is required.
-varfont <filename.ttf/otf?var0=value0&var1=value1>
Specifies an input variable font file and configures its variables.
-charset <filename> -charset <filename>
Specifies the input character set. Refer to the documentation for format of charset specification. Defaults to ASCII. Specifies the input character set. Refer to the documentation for format of charset specification. Defaults to ASCII.
-glyphset <filename> -glyphset <filename>
@ -196,8 +198,32 @@ static bool cmpExtension(const char *path, const char *ext) {
return true; 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 { struct FontInput {
const char *fontFilename; const char *fontFilename;
bool variableFont;
GlyphIdentifierType glyphIdentifierType; GlyphIdentifierType glyphIdentifierType;
const char *charsetFilename; const char *charsetFilename;
double fontScale; double fontScale;
@ -356,6 +382,13 @@ int main(int argc, const char * const *argv) {
} }
ARG_CASE("-font", 1) { ARG_CASE("-font", 1) {
fontInput.fontFilename = argv[++argPos]; fontInput.fontFilename = argv[++argPos];
fontInput.variableFont = false;
++argPos;
continue;
}
ARG_CASE("-varfont", 1) {
fontInput.fontFilename = argv[++argPos];
fontInput.variableFont = true;
++argPos; ++argPos;
continue; continue;
} }
@ -783,13 +816,13 @@ int main(int argc, const char * const *argv) {
msdfgen::deinitializeFreetype(ft); msdfgen::deinitializeFreetype(ft);
} }
} }
bool load(const char *fontFilename) { bool load(const char *fontFilename, bool isVarFont) {
if (ft && fontFilename) { if (ft && fontFilename) {
if (this->fontFilename && !strcmp(this->fontFilename, fontFilename)) if (this->fontFilename && !strcmp(this->fontFilename, fontFilename))
return true; return true;
if (font) if (font)
msdfgen::destroyFont(font); msdfgen::destroyFont(font);
if ((font = msdfgen::loadFont(ft, fontFilename))) { if ((font = isVarFont ? loadVarFont(ft, fontFilename) : msdfgen::loadFont(ft, fontFilename))) {
this->fontFilename = fontFilename; this->fontFilename = fontFilename;
return true; return true;
} }
@ -803,7 +836,7 @@ int main(int argc, const char * const *argv) {
} font; } font;
for (FontInput &fontInput : fontInputs) { for (FontInput &fontInput : fontInputs) {
if (!font.load(fontInput.fontFilename)) if (!font.load(fontInput.fontFilename, fontInput.variableFont))
ABORT("Failed to load specified font file."); ABORT("Failed to load specified font file.");
if (fontInput.fontScale <= 0) if (fontInput.fontScale <= 0)
fontInput.fontScale = 1; fontInput.fontScale = 1;