Microsoft OpenType specification for cmap
The cmap table in OpenType files translates a character code into a glyph ID.
Could any one help me to understand the C expression:
*(idRangeOffset[i]/2 + (c - startCount[i]) + &idRangeOffset[i])
开发者_Go百科
Here is the Format 4 cmap subtable.
So, in that expression i = segment index and c = character code. idRangeOffset gets the offset of the segment into a glyphIdArray inside the cmap. The value you're actually looking for in this case is glyphIdArray[something]. Since glyphIdArray immediately follows idRangeOffset in the font file, you use idRangeOffset as the base pointer.
To get to the start of the glyphIdArray you need to add the idRangeOffset, but since that value is in bytes and the idRangeOffset table is 16bit, you need to divide by 2 to get the word count. You then get the offset of segment i inside the glyphIdArray.
Your character's offset however inside this segment is at c - startCount[i] so you need to add that as well.
The final expression is a pointer so you need to dereference it to actually get the glyph's index.
This index is then used for the LOCA table.
Not sure if you still need it, but I share my findings for someone looking around.
&idRangeOffset[i]
refers to the address of idRangeOffset[i]
, or in the document's speech, the offset from beginning of the file.
*(x)
is the content of the address of x.
So, find the address of idRangeOffset[i]
, add it to idRangeOffset[i]/2 + (c - startCount[i])
, the result will be another address. The content of that address is the glyph id you want.
RTFM!
"If the idRangeOffset value for the segment is not 0, the mapping of character codes relies on glyphIdArray. The character code offset from startCode is added to the idRangeOffset value. This sum is used as an offset from the current location within idRangeOffset itself to index out the correct glyphIdArray value. This obscure indexing trick works because glyphIdArray immediately follows idRangeOffset in the font file. The C expression that yields the glyph index is:
*(idRangeOffset[i]/2 + (c - startCount[i]) + &idRangeOffset[i])
The value c is the character code in question, and i is the segment index in which c appears. If the value obtained from the indexing operation is not 0 (which indicates missingGlyph), idDelta[i] is added to it to get the glyph index. The idDelta arithmetic is modulo 65536."
精彩评论