Skip to content

Commit

Permalink
cff: Revert to use different private DICTs for CFF and CFF2
Browse files Browse the repository at this point in the history
[why]
While CFF has no `vsindex` operator in the private DICT it is a
defaulted operator in CFF2. This can not be really disentangled with the
current code and the opertor ends up erroreously written to CFF fonts
(where it is usually ignored).

[how]
It took a bit time to understand the CFF version code (that I believe is
not finished), but reinstate that code with different tables to be used.

Sorry I messed with the code in the first place; I had overlooked the
`vsindex` operator and how a defaulted operator in the tables turn out
in the written files.

I just reverted the version 'detection' code as it was, not checking
anything. Note that there still is one hard-coded `2` somewhere, but
that was already there when I started.

Signed-off-by: Fini Jastrow <[email protected]>
  • Loading branch information
Finii committed Sep 9, 2024
1 parent dad7f74 commit 332c8f2
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/tables/cff.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -401,13 +401,6 @@ const TOP_DICT_META_CFF2 = [
];

const PRIVATE_DICT_META = [
{name: 'subrs', op: 19, type: 'offset', value: 0},
{name: 'defaultWidthX', op: 20, type: 'number', value: 0},
{name: 'nominalWidthX', op: 21, type: 'number', value: 0}
];

// https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-16-private-dict-operators
const PRIVATE_DICT_META_CFF2 = [
{name: 'blueValues', op: 6, type: 'delta'},
{name: 'otherBlues', op: 7, type: 'delta'},
{name: 'familyBlues', op: 8, type: 'delta'},
Expand All @@ -424,9 +417,16 @@ const PRIVATE_DICT_META_CFF2 = [
{name: 'subrs', op: 19, type: 'offset'},
{name: 'defaultWidthX', op: 20, type: 'number', value: 0},
{name: 'nominalWidthX', op: 21, type: 'number', value: 0},
{name: 'vsindex', op: 22, type: 'number', value: 0},
{name: 'subrs', op: 19, type: 'offset', value: 0},
{name: 'defaultWidthX', op: 20, type: 'number', value: 0},
{name: 'nominalWidthX', op: 21, type: 'number', value: 0}
];

// https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-16-private-dict-operators
const PRIVATE_DICT_META_CFF2 = PRIVATE_DICT_META.concat([
{name: 'vsindex', op: 22, type: 'number', value: 0},
]);

// https://learn.microsoft.com/en-us/typography/opentype/spec/cff2#table-10-font-dict-operator-entries
const FONT_DICT_META = [
{name: 'private', op: 18, type: ['number', 'offset'], value: [0, 0]}
Expand Down Expand Up @@ -499,7 +499,7 @@ function gatherCFFTopDicts(data, start, cffIndex, strings, version) {
const privateSize = version < 2 ? topDict.private[0] : 0;
const privateOffset = version < 2 ? topDict.private[1] : 0;
if (privateSize !== 0 && privateOffset !== 0) {
const privateDict = parseCFFPrivateDict(data, privateOffset + start, privateSize, strings, 2);
const privateDict = parseCFFPrivateDict(data, privateOffset + start, privateSize, strings, version);
topDict._defaultWidthX = privateDict.defaultWidthX;
topDict._nominalWidthX = privateDict.nominalWidthX;
if (privateDict.subrs !== null && privateDict.subrs !== 0) {
Expand Down Expand Up @@ -1295,7 +1295,7 @@ function parseCFFTable(data, start, font, opt) {

if (header.formatMajor < 2 && topDict.private[0] !== 0) {
const privateDictOffset = start + topDict.private[1];
const privateDict = parseCFFPrivateDict(data, privateDictOffset, topDict.private[0], stringIndex.objects, 2);
const privateDict = parseCFFPrivateDict(data, privateDictOffset, topDict.private[0], stringIndex.objects, header.formatMajor);
font.defaultWidthX = privateDict.defaultWidthX;
font.nominalWidthX = privateDict.nominalWidthX;

Expand Down Expand Up @@ -1657,7 +1657,7 @@ function makeCFFTable(glyphs, options) {
t.globalSubrIndex = makeGlobalSubrIndex();
t.charsets = makeCharsets(glyphNames, strings);
t.charStringsIndex = makeCharStringsIndex(glyphs, cffVersion);
t.privateDict = makePrivateDict(privateAttrs, strings, 2);
t.privateDict = makePrivateDict(privateAttrs, strings, cffVersion);

// Needs to come at the end, to encode all custom strings used in the font.
t.stringIndex = makeStringIndex(strings);
Expand Down

0 comments on commit 332c8f2

Please sign in to comment.