Skip to content

Commit aeb3319

Browse files
committed
feat: Write CFF private DICT data
[why] The hinting of CFF (1 and 2) Open Type fonts is usually entirely in their private DICT data. To preserve the hinting - which is needed to make the font rendering look good and as expected - the private DICT data needs to be read and written. [how] Parts of the needed code seem to be added already in preparation for CFF2. I guess there was a misunderstanding or a mixture of versions, but the private DICT did not change between CFF1 and CFF2, and we need to always use the PRIVATE_DICT_META_CFF2, the link given in its definition is the same as given with link [1] for CFF1. See also [2]. So firstly we always refer to 'version 2', as the previous code did also but only in one case. For writing the delta values the encoding function is missing and so that is added. encode.delta() just calls encode.number() repeatedly, figuratively speaking. Last but not least the correct private DICT length has to be set. [1] https://adobe-type-tools.github.io/font-tech-notes/pdfs/5176.CFF.pdf [2] https://learn.microsoft.com/en-us/typography/opentype/otspec180/cff2#appendixD Signed-off-by: Fini Jastrow <ulf.fini.jastrow@desy.de>
1 parent 52d167f commit aeb3319

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

src/tables/cff.mjs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ function gatherCFFTopDicts(data, start, cffIndex, strings, version) {
487487
const privateSize = version < 2 ? topDict.private[0] : 0;
488488
const privateOffset = version < 2 ? topDict.private[1] : 0;
489489
if (privateSize !== 0 && privateOffset !== 0) {
490-
const privateDict = parseCFFPrivateDict(data, privateOffset + start, privateSize, strings, version);
490+
const privateDict = parseCFFPrivateDict(data, privateOffset + start, privateSize, strings, 2);
491491
topDict._defaultWidthX = privateDict.defaultWidthX;
492492
topDict._nominalWidthX = privateDict.nominalWidthX;
493493
if (privateDict.subrs !== 0) {
@@ -1283,7 +1283,7 @@ function parseCFFTable(data, start, font, opt) {
12831283

12841284
if (header.formatMajor < 2) {
12851285
const privateDictOffset = start + topDict.private[1];
1286-
const privateDict = parseCFFPrivateDict(data, privateDictOffset, topDict.private[0], stringIndex.objects, header.formatMajor);
1286+
const privateDict = parseCFFPrivateDict(data, privateDictOffset, topDict.private[0], stringIndex.objects, 2);
12871287
font.defaultWidthX = privateDict.defaultWidthX;
12881288
font.nominalWidthX = privateDict.nominalWidthX;
12891289

@@ -1561,9 +1561,8 @@ function makeCharStringsIndex(glyphs, version) {
15611561

15621562
function makePrivateDict(attrs, strings, version) {
15631563
const t = new table.Record('Private DICT', [
1564-
{name: 'dict', type: 'DICT', value: {}}
1564+
{name: 'dict', type: 'DICT', value: makeDict(version > 1 ? PRIVATE_DICT_META_CFF2 : PRIVATE_DICT_META, attrs, strings)}
15651565
]);
1566-
t.dict = makeDict(version > 1 ? PRIVATE_DICT_META_CFF2 : PRIVATE_DICT_META, attrs, strings);
15671566
return t;
15681567
}
15691568

@@ -1607,7 +1606,7 @@ function makeCFFTable(glyphs, options) {
16071606
attrs.strokeWidth = topDictOptions.strokeWidth || 0;
16081607
}
16091608

1610-
const privateAttrs = {};
1609+
const privateAttrs = topDictOptions._privateDict || {};
16111610

16121611
const glyphNames = [];
16131612
let glyph;
@@ -1627,7 +1626,7 @@ function makeCFFTable(glyphs, options) {
16271626
t.globalSubrIndex = makeGlobalSubrIndex();
16281627
t.charsets = makeCharsets(glyphNames, strings);
16291628
t.charStringsIndex = makeCharStringsIndex(glyphs, cffVersion);
1630-
t.privateDict = makePrivateDict(privateAttrs, strings);
1629+
t.privateDict = makePrivateDict(privateAttrs, strings, 2);
16311630

16321631
// Needs to come at the end, to encode all custom strings used in the font.
16331632
t.stringIndex = makeStringIndex(strings);
@@ -1643,6 +1642,7 @@ function makeCFFTable(glyphs, options) {
16431642
attrs.encoding = 0;
16441643
attrs.charStrings = attrs.charset + t.charsets.sizeOf();
16451644
attrs.private[1] = attrs.charStrings + t.charStringsIndex.sizeOf();
1645+
attrs.private[0] = t.privateDict.sizeOf();
16461646

16471647
// Recreate the Top DICT INDEX with the correct offsets.
16481648
topDict = makeTopDict(attrs, strings);

src/types.mjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,13 @@ encode.OPERAND = function(v, type) {
855855
for (let j = 0; j < enc1.length; j++) {
856856
d.push(enc1[j]);
857857
}
858+
} else if (type === 'delta') {
859+
for (let i = 0; i < v.length; i++) {
860+
const enc1 = encode.NUMBER(v[i]);
861+
for (let j = 0; j < enc1.length; j++) {
862+
d.push(enc1[j]);
863+
}
864+
}
858865
} else {
859866
throw new Error('Unknown operand type ' + type);
860867
// FIXME Add support for booleans

0 commit comments

Comments
 (0)