Skip to content

Instantly share code, notes, and snippets.

@jtanx
jtanx / multibytecmap4-1.md
Created December 17, 2013 06:59
Looking up non-Unicode format 4 TrueType character maps (cmap; ShiftJIS, PRC, Big5, Wansung, Johab)

Reference material: http://www.microsoft.com/typography/otspec/cmap.htm

Convert Unicode string using the appropriate encoding (e.g ShiftJIS). For each unicode code point, store the corresponding multibyte character sequence in little endian format.

E.g: Suppose you converted the character 'コ' (U+30B3) using ShiftJIS encoding. This would give the bytes [0x83, 0x52]. Store the result in a WORD (or larger sized variable) to give 0x8352 as the value. Use this value to lookup the glyph id from the cmap.

Conversion between charsets may be achieved through a number of ways, such as by using ICU. On Windows, the Unicode and Character set functions, such as WideChartoMultiByte may be used instead. Depending on the language, there may be in-built character set conversion utilities.

@jtanx
jtanx / Calculating xAvgCharwidth.md
Created November 30, 2013 05:55
Calculating xAvgCharWidth for the OS/2 font table.

For OS/2 table versions >= 3, take the sum of the advancement widths (from the hmtx table) of each glyph, iff the advancement width is greater than zero. Do integer division of this sum by the number of glyphs that had an advancement width > 0.

For versions < 3, follow a similar procedure, but weight according to: http://www.microsoft.com/typography/otspec/os2ver2.htm#acw, and only consider lowercase latin characters for the average.