Added -fontname argument

This commit is contained in:
Chlumsky 2021-03-15 22:32:13 +01:00 committed by Viktor Chlumský
parent 690ae5fb16
commit 7dea527d8d
6 changed files with 77 additions and 2 deletions

View File

@ -42,10 +42,11 @@ Use the following command line arguments for the standalone version of the atlas
### Input ### Input
- `-font <fontfile.ttf/otf>` &ndash; sets the input font file. - `-font <fontfile.ttf/otf>` (required) &ndash; sets the input font file.
- `-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).
- `-fontname <name>` &ndash; sets a name for the font that will be stored in certain output files as metadata.
- `-and` &ndash; separates multiple inputs to be combined into a single atlas. - `-and` &ndash; separates multiple inputs to be combined into a single atlas.
### Bitmap atlas type ### Bitmap atlas type
@ -89,7 +90,7 @@ Alternativelly, the minimum possible dimensions may be selected automatically if
### Outputs ### Outputs
Any subset of the following may be specified: Any non-empty subset of the following may be specified:
- `-imageout <filename.*>` &ndash; saves the atlas bitmap as a plain image file. Format matches `-format` - `-imageout <filename.*>` &ndash; saves the atlas bitmap as a plain image file. Format matches `-format`
- `-json <filename.json>` &ndash; writes the atlas's layout data as well as other metrics into a structured JSON file - `-json <filename.json>` &ndash; writes the atlas's layout data as well as other metrics into a structured JSON file

View File

@ -113,6 +113,13 @@ int FontGeometry::loadKerning(msdfgen::FontHandle *font) {
return loaded; return loaded;
} }
void FontGeometry::setName(const char *name) {
if (name)
this->name = name;
else
this->name.clear();
}
double FontGeometry::getGeometryScale() const { double FontGeometry::getGeometryScale() const {
return geometryScale; return geometryScale;
} }
@ -169,4 +176,10 @@ const std::map<std::pair<int, int>, double> & FontGeometry::getKerning() const {
return kerning; return kerning;
} }
const char * FontGeometry::getName() const {
if (name.empty())
return nullptr;
return name.c_str();
}
} }

View File

@ -3,6 +3,7 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
#include <string>
#include <map> #include <map>
#include <msdfgen.h> #include <msdfgen.h>
#include <msdfgen-ext.h> #include <msdfgen-ext.h>
@ -46,6 +47,8 @@ public:
bool addGlyph(GlyphGeometry &&glyph); bool addGlyph(GlyphGeometry &&glyph);
/// Loads kerning pairs for all glyphs that are currently present, returns the number of loaded kerning pairs /// Loads kerning pairs for all glyphs that are currently present, returns the number of loaded kerning pairs
int loadKerning(msdfgen::FontHandle *font); 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 /// Returns the geometry scale to be used when loading glyphs
double getGeometryScale() const; double getGeometryScale() const;
@ -63,6 +66,8 @@ public:
bool getAdvance(double &advance, unicode_t codepoint1, unicode_t codepoint2) const; 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 /// Returns the complete mapping of kerning pairs (by glyph indices) and their respective advance values
const std::map<std::pair<int, int>, double> & getKerning() const; const std::map<std::pair<int, int>, double> & getKerning() const;
/// Returns the name associated with the font or null if not set
const char * getName() const;
private: private:
double geometryScale; double geometryScale;
@ -74,6 +79,7 @@ private:
std::map<unicode_t, size_t> glyphsByCodepoint; std::map<unicode_t, size_t> glyphsByCodepoint;
std::map<std::pair<int, int>, double> kerning; std::map<std::pair<int, int>, double> kerning;
std::vector<GlyphGeometry> ownGlyphs; std::vector<GlyphGeometry> ownGlyphs;
std::string name;
}; };

View File

@ -74,6 +74,9 @@ bool exportArteryFont(const FontGeometry *fonts, int fontCount, const msdfgen::B
fontVariant.metrics.lineHeight = REAL(fontMetrics.lineHeight); fontVariant.metrics.lineHeight = REAL(fontMetrics.lineHeight);
fontVariant.metrics.underlineY = REAL(fontMetrics.underlineY); fontVariant.metrics.underlineY = REAL(fontMetrics.underlineY);
fontVariant.metrics.underlineThickness = REAL(fontMetrics.underlineThickness); fontVariant.metrics.underlineThickness = REAL(fontMetrics.underlineThickness);
const char *name = font.getName();
if (name)
fontVariant.name.string = name;
fontVariant.glyphs = artery_font::StdList<artery_font::Glyph<REAL> >(font.getGlyphs().size()); fontVariant.glyphs = artery_font::StdList<artery_font::Glyph<REAL> >(font.getGlyphs().size());
int j = 0; int j = 0;
for (const GlyphGeometry &glyphGeom : font.getGlyphs()) { for (const GlyphGeometry &glyphGeom : font.getGlyphs()) {

View File

@ -1,10 +1,45 @@
#include "json-export.h" #include "json-export.h"
#include <string>
#include "GlyphGeometry.h" #include "GlyphGeometry.h"
namespace msdf_atlas { 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) { static const char * imageTypeString(ImageType type) {
switch (type) { switch (type) {
case ImageType::HARD_MASK: case ImageType::HARD_MASK:
@ -47,6 +82,11 @@ bool exportJSON(const FontGeometry *fonts, int fontCount, double fontSize, doubl
if (fontCount > 1) if (fontCount > 1)
fputs(i == 0 ? "{" : ",{", f); fputs(i == 0 ? "{" : ",{", f);
// Font name
const char *name = font.getName();
if (name)
fprintf(f, "\"name\":\"%s\",", escapeJsonString(name).c_str());
// Font metrics // Font metrics
fputs("\"metrics\":{", f); { fputs("\"metrics\":{", f); {
const msdfgen::FontMetrics &metrics = font.getMetrics(); const msdfgen::FontMetrics &metrics = font.getMetrics();

View File

@ -49,6 +49,8 @@ INPUT SPECIFICATION
Specifies the set of input glyphs as glyph indices within the font file. Specifies the set of input glyphs as glyph indices within the font file.
-fontscale <scale> -fontscale <scale>
Specifies the scale to be applied to the glyph geometry of the font. Specifies the scale to be applied to the glyph geometry of the font.
-fontname <name>
Specifies a name for the font that will be propagated into the output files as metadata.
-and -and
Separates multiple inputs to be combined into a single atlas. Separates multiple inputs to be combined into a single atlas.
@ -159,6 +161,7 @@ struct FontInput {
GlyphIdentifierType glyphIdentifierType; GlyphIdentifierType glyphIdentifierType;
const char *charsetFilename; const char *charsetFilename;
double fontScale; double fontScale;
const char *fontName;
}; };
struct Configuration { struct Configuration {
@ -330,12 +333,18 @@ int main(int argc, const char * const *argv) {
++argPos; ++argPos;
continue; continue;
} }
ARG_CASE("-fontname", 1) {
fontInput.fontName = argv[++argPos];
++argPos;
continue;
}
ARG_CASE("-and", 0) { ARG_CASE("-and", 0) {
if (!fontInput.fontFilename && !fontInput.charsetFilename && fontInput.fontScale < 0) if (!fontInput.fontFilename && !fontInput.charsetFilename && fontInput.fontScale < 0)
ABORT("No font, character set, or font scale specified before -and separator."); ABORT("No font, character set, or font scale specified before -and separator.");
if (!fontInputs.empty() && !memcmp(&fontInputs.back(), &fontInput, sizeof(FontInput))) 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."); ABORT("No changes between subsequent inputs. A different font, character set, or font scale must be set inbetween -and separators.");
fontInputs.push_back(fontInput); fontInputs.push_back(fontInput);
fontInput.fontName = nullptr;
++argPos; ++argPos;
continue; continue;
} }
@ -724,6 +733,9 @@ int main(int argc, const char * const *argv) {
printf("\n"); printf("\n");
} }
if (fontInput.fontName)
fontGeometry.setName(fontInput.fontName);
fonts.push_back((FontGeometry &&) fontGeometry); fonts.push_back((FontGeometry &&) fontGeometry);
} }
} }