I have an error that revolves around these 3 specification properties for building an OpenType font:
On this page it mentions this is how it works:
- searchRange is the largest power of two less than or equal to the number of items in the table, i.e. the largest number of items that can be easily searched.
- rangeShift is the number of items minus searchRange; i.e. the number of items that will not get looked at if you only look at searchRange items.
- entrySelector is log2(searchRange).
Given that information, we have:
searchRange = maxPowerOf2 <= n
, where n
is number of items in table.rangeShift = n - searchRange
entrySelector = log2(searchRange)
But then we read a little further.
Note that the searchRange, the entrySelector and the rangeShift are all multiplied by 16 which represents the size of a directory entry.
So now we have:
searchRange = 16 * maxPowerOf2 <= n
, where n
is number of items in table.rangeShift = 16 * n - searchRange
entrySelector = 16 * log2(searchRange)
But wait, the following table shows this:
- searchRange:
(maximum power of 2 <= numTables)*16
- entrySelector:
log2(maximum power of 2 <= numTables)
- rangeShift:
numTables*16-searchRange
So that means our entrySelector doesn't have 16 multiplier it seems:
searchRange = 16 * maxPowerOf2 <= n
, where n
is number of items in table.rangeShift = 16 * n - searchRange
entrySelector = log2(searchRange)
But wait, there's more:
- searchRange
(Maximum power of 2 <= numTables) x 16
- entrySelector
Log2(maximum power of 2 <= numTables)
- rangeShift
NumTables x 16-searchRange
That matches our last one.
Checking some arbitrary font source code, it matches the last one too.
What I don't understand is the only thing that has worked for me is the first one:
let maxPowerOf2 = Math.pow(2, maxExponentFor2);
var searchRange = maxPowerOf2
var entrySelector = Math.log2(searchRange)
var rangeShift = numTables - searchRange
It's the only one that gets me past the error:
OTS parsing error: incorrect entrySelector for table directory
But then I end up at this error:
OTS parsing error: CFF : misaligned table
Given this output:
4f54 544f 0009 0008 0003 0001 4346 4620
0000 0000 0000 00c2 9c00 0000 0563 6d61
7000 0000 0000 0000 c2a4 0000 0024 6865
6164 0000 0000 0000 00c3 8800 0000 3668
6865 6100 0000 0000 0001 0000 0000 2468
6d74 7800 0000 0000 0001 2400 0000 006d
6178 7000 0000 0000 0001 2400 0000 066e
616d 6500 0000 0000 0001 2c00 0000 064f
532f 3200 0000 0000 0001 3400 0000 6470
6f73 7400 0000 0000 0001 c298 0000 0020
0200 0400 0000 0000 0000 0001 0000 0006
0000 0010 0004 0010 0000 0002 0002 0000
0000 c3bf c3bf 0000 c3bf c3bf 0001 0000
0001 0000 0001 0000 0000 0000 5f0f 3cc3
b500 0303 c3a8 0000 0000 c398 6526 c388
0000 0000 c398 6526 c388 0000 0000 0000
0000 0000 0003 0002 0000 0000 0000 0001
0000 00c3 8800 6400 0000 0000 0000 0000
0000 0100 0000 0000 0000 0000 0000 0000
0000 0000 0050 0000 0000 0000 0000 0000
0600 0000 0500 0000 0000 0000 0002 c28a
02c2 bb00 0000 c28c 02c2 8a02 c2bb 0000
01c3 9f00 3101 0200 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0058 5858 5800 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0300
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 00
Trying the way in which the majority are doing it (the two doc references and the source code), but which gives me the first error, is this:
4f54 544f 0009 00c2 8000 0700 1043 4646
2000 0000 0000 0000 c29c 0000 0005 636d
6170 0000 0000 0000 00c2 a400 0000 2468
6561 6400 0000 0000 0000 c388 0000 0036
6868 6561 0000 0000 0000 0100 0000 0024
686d 7478 0000 0000 0000 0124 0000 0000
6d61 7870 0000 0000 0000 0124 0000 0006
6e61 6d65 0000 0000 0000 012c 0000 0006
4f53 2f32 0000 0000 0000 0134 0000 0064
706f 7374 0000 0000 0000 01c2 9800 0000
2002 0004 0000 0000 0000 0000 0100 0000
0600 0000 1000 0400 1000 0000 0200 0200
0000 00c3 bfc3 bf00 00c3 bfc3 bf00 0100
0000 0100 0000 0100 0000 0000 005f 0f3c
c3b5 0003 03c3 a800 0000 00c3 9865 271d
0000 0000 c398 6527 1d00 0000 0000 0000
0000 0000 0300 0200 0000 0000 0000 0100
0000 c388 0064 0000 0000 0000 0000 0000
0001 0000 0000 0000 0000 0000 0000 0000
0000 0000 5000 0000 0000 0000 0000 0006
0000 0005 0000 0000 0000 0000 02c2 8a02
c2bb 0000 00c2 8c02 c28a 02c2 bb00 0001
c39f 0031 0102 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 5858 5858 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0003 0000
0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000
There are no glyphs in this yet, just the bare minimum. Wondering what I am missing to get this working.
If this is at all helpful, this was the basic data that went into generating the binary:
{
"name": "sfnt",
"fields": {
"scalar": {
"type": "TAG",
"value": "OTTO"
},
"numTables": {
"type": "USHORT",
"value": 9
},
"searchRange": {
"type": "USHORT",
"value": 128
},
"entrySelector": {
"type": "USHORT",
"value": 7
},
"rangeShift": {
"type": "USHORT",
"value": 16
},
"CFF Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "CFF "
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 156
},
"length": {
"type": "ULONG",
"value": 5
}
}
}
},
"cmap Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "cmap"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 164
},
"length": {
"type": "ULONG",
"value": 36
}
}
}
},
"head Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "head"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 200
},
"length": {
"type": "ULONG",
"value": 54
}
}
}
},
"hhea Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "hhea"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 256
},
"length": {
"type": "ULONG",
"value": 36
}
}
}
},
"hmtx Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "hmtx"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 292
},
"length": {
"type": "ULONG",
"value": 0
}
}
}
},
"maxp Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "maxp"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 292
},
"length": {
"type": "ULONG",
"value": 6
}
}
}
},
"name Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "name"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 300
},
"length": {
"type": "ULONG",
"value": 6
}
}
}
},
"OS/2 Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "OS/2"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 308
},
"length": {
"type": "ULONG",
"value": 100
}
}
}
},
"post Table Record": {
"type": "RECORD",
"value": {
"name": "Table Record",
"fields": {
"tag": {
"type": "TAG",
"value": "post"
},
"checkSum": {
"type": "ULONG",
"value": 0
},
"offset": {
"type": "ULONG",
"value": 408
},
"length": {
"type": "ULONG",
"value": 32
}
}
}
},
"CFF table": {
"type": "RECORD",
"value": {
"name": "CFF ",
"fields": {
"header": {
"type": "RECORD",
"value": {
"name": "Header",
"fields": {
"major": {
"type": "BYTE",
"value": 2
},
"minor": {
"type": "BYTE",
"value": 0
},
"hdrSize": {
"type": "BYTE",
"value": 4
},
"topDictLength": {
"type": "USHORT",
"value": 0
}
}
}
}
}
}
},
"padding_0": {
"type": "BYTE",
"value": 0
},
"padding_1": {
"type": "BYTE",
"value": 0
},
"padding_2": {
"type": "BYTE",
"value": 0
},
"cmap table": {
"type": "RECORD",
"value": {
"name": "cmap",
"fields": {
"version": {
"type": "USHORT",
"value": 0
},
"numTables": {
"type": "USHORT",
"value": 1
},
"platformID": {
"type": "USHORT",
"value": 0
},
"encodingID": {
"type": "USHORT",
"value": 6
},
"offset": {
"type": "ULONG",
"value": 16
},
"format": {
"type": "USHORT",
"value": 4
},
"cmap4Length": {
"type": "USHORT",
"value": 16
},
"language": {
"type": "USHORT",
"value": 0
},
"segCountX2": {
"type": "USHORT",
"value": 2
},
"searchRange": {
"type": "USHORT",
"value": 2
},
"entrySelector": {
"type": "USHORT",
"value": 0
},
"rangeShift": {
"type": "USHORT",
"value": 0
},
"end_0": {
"type": "USHORT",
"value": 65535
},
"reservedPad": {
"type": "USHORT",
"value": 0
},
"start_0": {
"type": "USHORT",
"value": 65535
},
"idDelta_0": {
"type": "SHORT",
"value": 1
},
"idRangeOffset_0": {
"type": "USHORT",
"value": 0
}
}
}
},
"head table": {
"type": "RECORD",
"value": {
"name": "head",
"fields": {
"version": {
"type": "FIXED",
"value": 65536
},
"fontRevision": {
"type": "FIXED",
"value": 65536
},
"checkSumAdjustment": {
"type": "ULONG",
"value": 0
},
"magicNumber": {
"type": "ULONG",
"value": 1594834165
},
"flags": {
"type": "USHORT",
"value": 3
},
"unitsPerEm": {
"type": "USHORT",
"value": 1000
},
"created": {
"type": "LONGDATETIME",
"value": 3630507805
},
"modified": {
"type": "LONGDATETIME",
"value": 3630507805
},
"xMin": {
"type": "SHORT",
"value": null
},
"yMin": {
"type": "SHORT",
"value": null
},
"xMax": {
"type": "SHORT",
"value": null
},
"yMax": {
"type": "SHORT",
"value": null
},
"macStyle": {
"type": "USHORT",
"value": 0
},
"lowestRecPPEM": {
"type": "USHORT",
"value": 3
},
"fontDirectionHint": {
"type": "SHORT",
"value": 2
},
"indexToLocFormat": {
"type": "SHORT",
"value": 0
},
"glyphDataFormat": {
"type": "SHORT",
"value": 0
}
}
}
},
"padding_3": {
"type": "BYTE",
"value": 0
},
"padding_4": {
"type": "BYTE",
"value": 0
},
"hhea table": {
"type": "RECORD",
"value": {
"name": "hhea",
"fields": {
"version": {
"type": "FIXED",
"value": 65536
},
"ascender": {
"type": "FWORD",
"value": 200
},
"descender": {
"type": "FWORD",
"value": 100
},
"lineGap": {
"type": "FWORD",
"value": 0
},
"advanceWidthMax": {
"type": "UFWORD",
"value": null
},
"minLeftSideBearing": {
"type": "FWORD",
"value": null
},
"minRightSideBearing": {
"type": "FWORD",
"value": null
},
"xMaxExtent": {
"type": "FWORD",
"value": null
},
"caretSlopeRise": {
"type": "SHORT",
"value": 1
},
"caretSlopeRun": {
"type": "SHORT",
"value": 0
},
"caretOffset": {
"type": "SHORT",
"value": 0
},
"reserved1": {
"type": "SHORT",
"value": 0
},
"reserved2": {
"type": "SHORT",
"value": 0
},
"reserved3": {
"type": "SHORT",
"value": 0
},
"reserved4": {
"type": "SHORT",
"value": 0
},
"metricDataFormat": {
"type": "SHORT",
"value": 0
},
"numberOfHMetrics": {
"type": "USHORT",
"value": 0
}
}
}
},
"hmtx table": {
"type": "RECORD",
"value": {
"name": "hmtx",
"fields": {}
}
},
"maxp table": {
"type": "RECORD",
"value": {
"name": "maxp",
"fields": {
"version": {
"type": "FIXED",
"value": 20480
},
"numGlyphs": {
"type": "USHORT",
"value": 0
}
}
}
},
"padding_5": {
"type": "BYTE",
"value": 0
},
"padding_6": {
"type": "BYTE",
"value": 0
},
"name table": {
"type": "RECORD",
"value": {
"name": "name",
"fields": {
"format": {
"type": "USHORT",
"value": 0
},
"count": {
"type": "USHORT",
"value": 0
},
"stringOffset": {
"type": "USHORT",
"value": 6
},
"strings": {
"type": "LITERAL",
"value": []
}
}
}
},
"padding_7": {
"type": "BYTE",
"value": 0
},
"padding_8": {
"type": "BYTE",
"value": 0
},
"OS/2 table": {
"type": "RECORD",
"value": {
"name": "OS/2",
"fields": {
"version": {
"type": "USHORT",
"value": 5
},
"xAvgCharWidth": {
"type": "SHORT",
"value": 0
},
"usWeightClass": {
"type": "USHORT",
"value": 0
},
"usWidthClass": {
"type": "USHORT",
"value": 0
},
"fsType": {
"type": "USHORT",
"value": 0
},
"ySubscriptXSize": {
"type": "SHORT",
"value": 650
},
"ySubscriptYSize": {
"type": "SHORT",
"value": 699
},
"ySubscriptXOffset": {
"type": "SHORT",
"value": 0
},
"ySubscriptYOffset": {
"type": "SHORT",
"value": 140
},
"ySuperscriptXSize": {
"type": "SHORT",
"value": 650
},
"ySuperscriptYSize": {
"type": "SHORT",
"value": 699
},
"ySuperscriptXOffset": {
"type": "SHORT",
"value": 0
},
"ySuperscriptYOffset": {
"type": "SHORT",
"value": 479
},
"yStrikeoutSize": {
"type": "SHORT",
"value": 49
},
"yStrikeoutPosition": {
"type": "SHORT",
"value": 258
},
"sFamilyClass": {
"type": "SHORT",
"value": 0
},
"bFamilyType": {
"type": "BYTE",
"value": 0
},
"bSerifStyle": {
"type": "BYTE",
"value": 0
},
"bWeight": {
"type": "BYTE",
"value": 0
},
"bProportion": {
"type": "BYTE",
"value": 0
},
"bContrast": {
"type": "BYTE",
"value": 0
},
"bStrokeVariation": {
"type": "BYTE",
"value": 0
},
"bArmStyle": {
"type": "BYTE",
"value": 0
},
"bLetterform": {
"type": "BYTE",
"value": 0
},
"bMidline": {
"type": "BYTE",
"value": 0
},
"bXHeight": {
"type": "BYTE",
"value": 0
},
"ulUnicodeRange1": {
"type": "ULONG",
"value": 0
},
"ulUnicodeRange2": {
"type": "ULONG",
"value": 0
},
"ulUnicodeRange3": {
"type": "ULONG",
"value": 0
},
"ulUnicodeRange4": {
"type": "ULONG",
"value": 0
},
"achVendID": {
"type": "CHARARRAY",
"value": "XXXX"
},
"fsSelection": {
"type": "USHORT",
"value": 0
},
"usFirstCharIndex": {
"type": "USHORT",
"value": 0
},
"usLastCharIndex": {
"type": "USHORT",
"value": 0
},
"sTypoAscender": {
"type": "SHORT",
"value": 0
},
"sTypoDescender": {
"type": "SHORT",
"value": 0
},
"sTypoLineGap": {
"type": "SHORT",
"value": 0
},
"usWinAscent": {
"type": "USHORT",
"value": 0
},
"usWinDescent": {
"type": "USHORT",
"value": 0
},
"ulCodePageRange1": {
"type": "ULONG",
"value": 0
},
"ulCodePageRange2": {
"type": "ULONG",
"value": 0
},
"sxHeight": {
"type": "SHORT",
"value": 0
},
"sCapHeight": {
"type": "SHORT",
"value": 0
},
"usDefaultChar": {
"type": "USHORT",
"value": 0
},
"usBreakChar": {
"type": "USHORT",
"value": 0
},
"usMaxContext": {
"type": "USHORT",
"value": 0
},
"usLowerOpticalPointSize": {
"type": "USHORT",
"value": 0
},
"usUpperOpticalPointSize": {
"type": "USHORT",
"value": 0
}
}
}
},
"post table": {
"type": "RECORD",
"value": {
"name": "post",
"fields": {
"version": {
"type": "FIXED",
"value": 196608
},
"italicAngle": {
"type": "FIXED",
"value": 0
},
"underlinePosition": {
"type": "FWORD",
"value": 0
},
"underlineThickness": {
"type": "FWORD",
"value": 0
},
"isFixedPitch": {
"type": "ULONG",
"value": 0
},
"minMemType42": {
"type": "ULONG",
"value": 0
},
"maxMemType42": {
"type": "ULONG",
"value": 0
},
"minMemType1": {
"type": "ULONG",
"value": 0
},
"maxMemType1": {
"type": "ULONG",
"value": 0
}
}
}
}
}
}
And these are the decimal values used to create the output bytes for one of the slight variations:
79 84 84 79
0 9 0 8
0 3 0 1
67 70 70 32 (1)
0 0 0 0
0 0 0 156
0 0 0 5
99 109 97 112 (2)
0 0 0 0
0 0 0 164
0 0 0 36
104 101 97 100 (3)
0 0 0 0
0 0 0 200
0 0 0 54
104 104 101 97 (4)
0 0 0 0
0 0 1 0
0 0 0 36
104 109 116 120 (5)
0 0 0 0
0 0 1 36
0 0 0 0
109 97 120 112 (6)
0 0 0 0
0 0 1 36
0 0 0 6
110 97 109 101 (7)
0 0 0 0
0 0 1 44
0 0 0 6
79 83 47 50 (8)
0 0 0 0
0 0 1 52
0 0 0 100
112 111 115 116 (9)
0 0 0 0
0 0 1 152
0 0 0 32
2 0 4 0 (CFF start)
0 0 0 0 (4-align with three 0's)
0 0 0 1 (cmap start)
0 0 0 6
0 0 0 16
0 4 0 16
0 0 0 2 0 2 0 0 0 0 255 255 0 0 255 255 0 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 95 15 60 245 0 3 3 232 0 0 0 0 216 101 49 90 0 0 0 0 216 101 49 90 0 0 0 0 0 0 0 0 0 0 0 3 0 2 0 0 0 0 0 0 0 1 0 0 0 200 0 100 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 80 0 0 0 0 0 0 0 0 0 0 6 0 0 0 5 0 0 0 0 0 0 0 0 2 138 2 187 0 0 0 140 2 138 2 187 0 0 1 223 0 49 1 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 88 88 88 88 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
It seems to show that it is aligned, so I don't understand. Hoping for some help.
When debugging the output, you might find it helpful to use a hex dump format that shows an ASCII representation of the bytes in the right column. I use xxd
for this and recommend you do the same. For OpenType fonts in particular, this dump format is really handy for spotting alignment issues, especially in the font header.
To see what I mean: here's the first section of one of your dumps:
00000000: 4f54 544f 0009 0008 0003 0001 4346 4620 OTTO........CFF
00000010: 0000 0000 0000 00c2 9c00 0000 0563 6d61 .............cma
00000020: 7000 0000 0000 0000 c2a4 0000 0024 6865 p............$he
00000030: 6164 0000 0000 0000 00c3 8800 0000 3668 ad............6h
00000040: 6865 6100 0000 0000 0001 0000 0000 2468 hea...........$h
00000050: 6d74 7800 0000 0000 0001 2400 0000 006d mtx.......$....m
00000060: 6178 7000 0000 0000 0001 2400 0000 066e axp.......$....n
00000070: 616d 6500 0000 0000 0001 2c00 0000 064f ame.......,....O
00000080: 532f 3200 0000 0000 0001 3400 0000 6470 S/2.......4...dp
00000090: 6f73 7400 0000 0000 0001 c298 0000 0020 ost............
You can see that the first tableDirectory entry, for the CFF
table, the tag ('CFF ') begins as the last 4 bytes of the first line (4346 4620
which is 'CFF ' in ASCII), and that is the correct alignment. Because the hexdump row length is exactly 16 bytes, and because each tableDirectory entry is 16 bytes, a properly-aligned tableDirectory will have the tags for each table as the last 4 bytes of the line. But as you can see, you have some misalignment...the 'c' (0x63) of 'cmap' is one byte too far to the right, and subsequent tableDirectory entries are also shifted. It should look more like this:
00000000: 4f54 544f 0009 0008 0003 0001 4346 4620 OTTO........CFF
00000010: ???? ???? ???? ???? ???? ???? 636d 6170 ............cmap
00000020: ???? ???? ???? ???? ???? ???? 6865 6164 ............head
00000030: ???? ???? ???? ???? ???? ???? 6868 6561 ............hhea
[...]
So it looks like for some of your tableDirectory entries, there's an extra byte being added at the end. So you need to back up and examine that code and get it working correctly first, then worry about the searchRange, rangeShift, and entrySelector (which, assuming your font contains 9 tables as advertised, should be: 0080 0003 0010
(hex)).