diff --git a/README.md b/README.md index 9f7f8ac..db2b680 100644 --- a/README.md +++ b/README.md @@ -42,10 +42,11 @@ Use the following command line arguments for the standalone version of the atlas ### Input -- `-font ` – sets the input font file. +- `-font ` (required) – sets the input font file. - `-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). +- `-fontname ` – sets a name for the font that will be stored in certain output files as metadata. - `-and` – separates multiple inputs to be combined into a single atlas. ### Bitmap atlas type @@ -89,7 +90,7 @@ Alternativelly, the minimum possible dimensions may be selected automatically if ### Outputs -Any subset of the following may be specified: +Any non-empty subset of the following may be specified: - `-imageout ` – saves the atlas bitmap as a plain image file. Format matches `-format` - `-json ` – writes the atlas's layout data as well as other metrics into a structured JSON file diff --git a/msdf-atlas-gen/FontGeometry.cpp b/msdf-atlas-gen/FontGeometry.cpp index 4a75a22..ad89d45 100644 --- a/msdf-atlas-gen/FontGeometry.cpp +++ b/msdf-atlas-gen/FontGeometry.cpp @@ -113,6 +113,13 @@ int FontGeometry::loadKerning(msdfgen::FontHandle *font) { return loaded; } +void FontGeometry::setName(const char *name) { + if (name) + this->name = name; + else + this->name.clear(); +} + double FontGeometry::getGeometryScale() const { return geometryScale; } @@ -169,4 +176,10 @@ const std::map, double> & FontGeometry::getKerning() const { return kerning; } +const char * FontGeometry::getName() const { + if (name.empty()) + return nullptr; + return name.c_str(); +} + } diff --git a/msdf-atlas-gen/FontGeometry.h b/msdf-atlas-gen/FontGeometry.h index c95185c..4ac230e 100644 --- a/msdf-atlas-gen/FontGeometry.h +++ b/msdf-atlas-gen/FontGeometry.h @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -46,6 +47,8 @@ public: bool addGlyph(GlyphGeometry &&glyph); /// Loads kerning pairs for all glyphs that are currently present, returns the number of loaded kerning pairs int loadKerning(msdfgen::FontHandle *font); + /// Sets a name to be associated with the font + void setName(const char *name); /// Returns the geometry scale to be used when loading glyphs double getGeometryScale() const; @@ -63,6 +66,8 @@ public: bool getAdvance(double &advance, unicode_t codepoint1, unicode_t codepoint2) const; /// Returns the complete mapping of kerning pairs (by glyph indices) and their respective advance values const std::map, double> & getKerning() const; + /// Returns the name associated with the font or null if not set + const char * getName() const; private: double geometryScale; @@ -74,6 +79,7 @@ private: std::map glyphsByCodepoint; std::map, double> kerning; std::vector ownGlyphs; + std::string name; }; diff --git a/msdf-atlas-gen/artery-font-export.cpp b/msdf-atlas-gen/artery-font-export.cpp index 31db21d..fc8a61a 100644 --- a/msdf-atlas-gen/artery-font-export.cpp +++ b/msdf-atlas-gen/artery-font-export.cpp @@ -74,6 +74,9 @@ bool exportArteryFont(const FontGeometry *fonts, int fontCount, const msdfgen::B fontVariant.metrics.lineHeight = REAL(fontMetrics.lineHeight); fontVariant.metrics.underlineY = REAL(fontMetrics.underlineY); fontVariant.metrics.underlineThickness = REAL(fontMetrics.underlineThickness); + const char *name = font.getName(); + if (name) + fontVariant.name.string = name; fontVariant.glyphs = artery_font::StdList >(font.getGlyphs().size()); int j = 0; for (const GlyphGeometry &glyphGeom : font.getGlyphs()) { diff --git a/msdf-atlas-gen/json-export.cpp b/msdf-atlas-gen/json-export.cpp index 745f21c..2483bcf 100644 --- a/msdf-atlas-gen/json-export.cpp +++ b/msdf-atlas-gen/json-export.cpp @@ -1,10 +1,45 @@ #include "json-export.h" +#include #include "GlyphGeometry.h" namespace msdf_atlas { +static std::string escapeJsonString(const char *str) { + char uval[7] = "\\u0000"; + std::string outStr; + while (*str) { + switch (*str) { + case '\\': + outStr += "\\\\"; + break; + case '"': + outStr += "\\\""; + break; + case '\n': + outStr += "\\n"; + break; + case '\r': + outStr += "\\r"; + break; + case '\t': + outStr += "\\t"; + break; + case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08: /* \\t */ /* \\n */ case 0x0b: case 0x0c: /* \\r */ case 0x0e: case 0x0f: + case 0x10: case 0x11: case 0x12: case 0x13: case 0x14: case 0x15: case 0x16: case 0x17: case 0x18: case 0x19: case 0x1a: case 0x1b: case 0x1c: case 0x1d: case 0x1e: case 0x1f: + uval[4] = '0'+(*str >= 0x10); + uval[5] = "0123456789abcdef"[*str&0x0f]; + outStr += uval; + break; + default: + outStr.push_back(*str); + } + ++str; + } + return outStr; +} + static const char * imageTypeString(ImageType type) { switch (type) { case ImageType::HARD_MASK: @@ -47,6 +82,11 @@ bool exportJSON(const FontGeometry *fonts, int fontCount, double fontSize, doubl if (fontCount > 1) fputs(i == 0 ? "{" : ",{", f); + // Font name + const char *name = font.getName(); + if (name) + fprintf(f, "\"name\":\"%s\",", escapeJsonString(name).c_str()); + // Font metrics fputs("\"metrics\":{", f); { const msdfgen::FontMetrics &metrics = font.getMetrics(); diff --git a/msdf-atlas-gen/main.cpp b/msdf-atlas-gen/main.cpp index 29cc9d8..6967117 100644 --- a/msdf-atlas-gen/main.cpp +++ b/msdf-atlas-gen/main.cpp @@ -49,6 +49,8 @@ INPUT SPECIFICATION Specifies the set of input glyphs as glyph indices within the font file. -fontscale Specifies the scale to be applied to the glyph geometry of the font. + -fontname + Specifies a name for the font that will be propagated into the output files as metadata. -and Separates multiple inputs to be combined into a single atlas. @@ -159,6 +161,7 @@ struct FontInput { GlyphIdentifierType glyphIdentifierType; const char *charsetFilename; double fontScale; + const char *fontName; }; struct Configuration { @@ -330,12 +333,18 @@ int main(int argc, const char * const *argv) { ++argPos; continue; } + ARG_CASE("-fontname", 1) { + fontInput.fontName = argv[++argPos]; + ++argPos; + continue; + } ARG_CASE("-and", 0) { if (!fontInput.fontFilename && !fontInput.charsetFilename && fontInput.fontScale < 0) ABORT("No font, character set, or font scale specified before -and separator."); if (!fontInputs.empty() && !memcmp(&fontInputs.back(), &fontInput, sizeof(FontInput))) ABORT("No changes between subsequent inputs. A different font, character set, or font scale must be set inbetween -and separators."); fontInputs.push_back(fontInput); + fontInput.fontName = nullptr; ++argPos; continue; } @@ -724,6 +733,9 @@ int main(int argc, const char * const *argv) { printf("\n"); } + if (fontInput.fontName) + fontGeometry.setName(fontInput.fontName); + fonts.push_back((FontGeometry &&) fontGeometry); } }