Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable font (= CFF2) writing #701

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bin/test-render
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
//
// When viewing the SVG, it will be upside-down (since glyphs are designed Y-up).

var opentype = require('../dist/opentype.js');
var opentype = require('/mnt/d/programming/_opensource/opentypejs/dist/opentype.js');

const SVG_FOOTER = `</svg>`;

Expand Down
3 changes: 2 additions & 1 deletion docs/glyph-inspector.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ <h1>Free Software</h1>
}
}).join('</li><li>') + '</li></ul>';
} else if (glyph.path) {
html += 'path:<br><pre> ' + glyph.path.commands.map(pathCommandToString).join('\n ') + '\n</pre>';
const transGlyph = window.font.variation.getTransform(glyph, window.fontOptions.variation);
html += 'path:<br><pre> ' + transGlyph.path.commands.map(pathCommandToString).join('\n ') + '\n</pre>';
}

const layers = glyph.getLayers(font);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"scripts": {
"build": "npm run b:umd && npm run b:esm",
"dist": " npm run d:umd && npm run d:esm",
"test": "npm run build && npm run dist && mocha --require reify --recursive && npm run lint",
"test": "npm run build && npm run dist && mocha -g \"cff\" --require reify --recursive && npm run lint",
"lint": "eslint src",
"lint-fix": "eslint src --fix",
"start": "esbuild --bundle src/opentype.js --outdir=dist --external:fs --external:http --external:https --target=es2018 --format=esm --out-extension:.js=.module.js --global-name=opentype --define:DEBUG=false --footer:js=\"(function (root, factory) { if (typeof define === 'function' && define.amd)define(factory); else if (typeof module === 'object' && module.exports)module.exports = factory(); else root.opentype = factory(); }(typeof self !== 'undefined' ? self : this, () => ({...opentype,'default':opentype})));\" --watch --servedir=. --footer:js=\"new EventSource('/esbuild').addEventListener('change', () => location.reload())\"",
Expand Down
7 changes: 7 additions & 0 deletions src/font.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,13 @@ Font.prototype.drawMetrics = function(ctx, text, x, y, fontSize, options) {
*/
Font.prototype.getEnglishName = function(name) {
const translations = (this.names.unicode || this.names.macintosh || this.names.windows)[name];
if(!translations) {
for(let platform of ['unicode', 'macintosh', 'windows']) {
if(this.names[platform] && this.names[platform][name]) {
return this.names[platform][name].en;
}
}
}
if (translations) {
return translations.en;
}
Expand Down
5 changes: 5 additions & 0 deletions src/glyph.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ function getPathDefinition(glyph, path) {

set: function(p) {
_path = p;
// remove the subrs/gsubrs
// @TODO: In the future we'll need an algorithm that finds
// candidates for sub routines and adds them to the index
delete glyph.subrs;
delete glyph.gsubrs;
}
};
}
Expand Down
81 changes: 81 additions & 0 deletions src/make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Writing utility functions for common formats
import table from './table.js'
import { masks } from './parse.js'
import { sizeOf } from './types.js';

export function ItemVariationStore(vstore, fvar) {
const variationRegions = vstore.variationRegions;
const subTables = vstore.itemVariationSubtables;
const subTableCount = subTables.length;
const fields = [
{ name: 'format', type: 'USHORT', value: 1 },
{ name: 'variationRegionListOffset', type: 'ULONG', value: 0 },
{ name: 'itemVariationDataCount', type: 'USHORT', value: subTableCount },
];

for(let n = 0; n < subTableCount; n++) {
fields.push(
{ name: `itemVariationDataOffsets_${n}`, type: 'ULONG', value: 0 },
)
}

const t = new table.Record('ItemVariationStore', fields);
let currentOffset = t.variationRegionListOffset = t.sizeOf();

// VariationRegions List
const axisCount = fvar.axes.length;
t.fields.push({ name: 'axisCount', type: 'USHORT', value: axisCount });
const VariationRegionList = table.recordList('variationRegions', variationRegions, (record, i) => {
const namePrefix = `VariationRegion_${i}_`;
const fields = [];
for(let n = 0; n < axisCount; n++) {
const fieldNamePrefix = namePrefix + `regionAxes_${n}_`;
for(const f of ['startCoord', 'peakCoord', 'endCoord']) {
fields.push({ name: fieldNamePrefix + f, type: 'F2DOT14', value: record.regionAxes[n][f]});
}
}
return fields;
});

for(const region of VariationRegionList) {
t.fields.push(region);
}

currentOffset = t.sizeOf();

// ItemVariationData subtables
const subTableList = table.recordList('ItemVariationData', subTables, (record, i) => {
const subTable = ItemVariationData(record, `ItemVariationData_${i}_`);
t[`itemVariationDataOffsets_${i}`] = currentOffset;
currentOffset += sizeOf.OBJECT(subTable);
return subTable;
});


for(const field of subTableList) {
// we already have the ItemVariationDataCount in the ItemVariationStore above
if(field.name === 'ItemVariationDataCount') continue;
t.fields.push(field);
}

return t;

}

export function ItemVariationData(ivd, namePrefix) {
const deltaSetCount = ivd.deltaSets.length;
const regionCount = ivd.regionIndexes.length;
const fields = [
{ name: namePrefix +'_itemCount', type: 'USHORT', value: deltaSetCount },
{ name: namePrefix +'_wordDeltaCount', type: 'USHORT', value: 0 },
{ name: namePrefix +'_regionIndexCount', type: 'USHORT', value: regionCount },
];

for(let i = 0; i < regionCount; i++) {
fields.push(
{ name: namePrefix + `_regionIndexes_${i}`, type: 'USHORT', value: ivd.regionIndexes[i] },
);
}

return fields;
}
2 changes: 1 addition & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const typeOffsets = {
tag: 4
};

const masks = {
export const masks = {
LONG_WORDS: 0x8000,
WORD_DELTA_COUNT_MASK: 0x7FFF,
SHARED_POINT_NUMBERS: 0x8000,
Expand Down
Loading
Loading