Add support code for glyph name export -- not enabled for now

This commit is contained in:
be5invis 2021-05-30 18:56:31 -07:00
parent b7e7752109
commit 35fa24274d
2 changed files with 39 additions and 7 deletions

View file

@ -3,7 +3,7 @@
const { Ot } = require("ot-builder"); const { Ot } = require("ot-builder");
module.exports = function () { module.exports = function () {
var font = { let font = {
head: new Ot.Head.Table(), head: new Ot.Head.Table(),
hhea: new Ot.MetricHead.Hhea(), hhea: new Ot.MetricHead.Hhea(),
os2: new Ot.Os2.Table(4), os2: new Ot.Os2.Table(4),
@ -12,7 +12,7 @@ module.exports = function () {
name: new Ot.Name.Table() name: new Ot.Name.Table()
}; };
if (process.env.SOURCE_DATE_EPOCH) { if (process.env.SOURCE_DATE_EPOCH) {
font.head.created = new Date(process.env.SOURCE_DATE_EPOCH * 1000); font.head.created = new Date(process.env.SOURCE_DATE_EPOCH * 1000);
font.head.modified = new Date(process.env.SOURCE_DATE_EPOCH * 1000); font.head.modified = new Date(process.env.SOURCE_DATE_EPOCH * 1000);
} }
return font; return font;

View file

@ -5,13 +5,16 @@ class MappedGlyphStore {
constructor() { constructor() {
this.m_nameMapping = new Map(); this.m_nameMapping = new Map();
this.m_mapping = new Map(); this.m_mapping = new Map();
this.m_primaryUnicodeMapping = new Map();
} }
declare(name, source) { declare(name, source) {
const g = new Ot.Glyph(); const g = new Ot.Glyph();
g.name = name;
this.m_nameMapping.set(name, g); this.m_nameMapping.set(name, g);
this.m_mapping.set(source, g); this.m_mapping.set(source, g);
} }
setPrimaryUnicode(source, u) {
this.m_primaryUnicodeMapping.set(source, u);
}
queryBySourceGlyph(source) { queryBySourceGlyph(source) {
return this.m_mapping.get(source); return this.m_mapping.get(source);
} }
@ -39,6 +42,31 @@ class MappedGlyphStore {
this.fillContours(g, source.geometry.asContours()); this.fillContours(g, source.geometry.asContours());
} }
} }
fillOtGlyphNames() {
let gid = 0;
let conflictSet = new Set();
for (const [gSrc, gOt] of this.m_mapping) {
gOt.name = this.nameSingleGlyph(gid, gSrc, conflictSet);
gid++;
}
}
nameSingleGlyph(gid, gSrc, conflictSet) {
if (gid === 0) return ".notdef";
if (gid === 1) return ".null";
let preferredName = null;
let primaryUnicode = this.m_primaryUnicodeMapping.get(gSrc);
if (primaryUnicode) {
preferredName = `u${formatCodePointHex(primaryUnicode)}`;
}
if (preferredName && !conflictSet.has(preferredName)) {
conflictSet.add(preferredName);
return preferredName;
}
return `.gid${gid}`;
}
fillReferences(g, rs) { fillReferences(g, rs) {
const gl = new Ot.Glyph.GeometryList(); const gl = new Ot.Glyph.GeometryList();
@ -85,14 +113,14 @@ function convertGlyphs(gsOrig) {
const us = gsOrig.queryUnicodeOf(gSrc); const us = gsOrig.queryUnicodeOf(gSrc);
if (us) { if (us) {
for (const u of us) { for (const u of us) {
if (isFinite(u - 0) && u) { if (!(isFinite(u - 0) && u)) continue;
cmap.unicode.set(u, gs.queryBySourceGlyph(gSrc)); cmap.unicode.set(u, gs.queryBySourceGlyph(gSrc));
} gs.setPrimaryUnicode(gSrc, u);
} }
} }
} }
for (const [origIndex, name, uOrd, gSrc] of sortedEntries) gs.fill(name, gSrc); for (const [origIndex, name, uOrd, gSrc] of sortedEntries) gs.fill(name, gSrc);
gs.fillOtGlyphNames();
return { glyphs: gs, cmap }; return { glyphs: gs, cmap };
} }
function queryOrderingUnicode(gs, g) { function queryOrderingUnicode(gs, g) {
@ -105,3 +133,7 @@ function byRank([ja, gna, ua, a], [jb, gnb, ub, b]) {
(b.glyphRank || 0) - (a.glyphRank || 0) || (ua || 0) - (ub || 0) || (ja || 0) - (jb || 0) (b.glyphRank || 0) - (a.glyphRank || 0) || (ua || 0) - (ub || 0) || (ja || 0) - (jb || 0)
); );
} }
function formatCodePointHex(u) {
return u.toString(16).padStart(4, "0").toUpperCase();
}