Subject: Big5 TTF to GB TTF conversion

From: "L.S.Ng" <[email-masked]>
Date: Thu, 23 Jun 1994 09:42:53 BST

Here's are the tips that I learn to understand True Type Font.

1. The full specification document from Microsoft is rather
comprehensive and definitive. Hence there are too much for us to
understand in one go. So I pick up the most important part to start
with: the CMAP and GLYF table.

2. 'What is it?' This is the question you will ask yourself everytime
you face with a problem: always start from definition.

3.  The True Type Font File (TTF File) is a file.  This file contains
various tables.  Each table contains one type of information.  Of all
tables, there are two tables which are most important: CMAP table and
GLYF table.

4. Since a TTF file contains several tables, hence it must have one
table directory. The sole purpose of having a table directory is to tell
where each table is located and how big it is. And as usual, the table
directory itself is located at the start of the TTF file. Other tables
are placed at various location in the file. As you know the unit of
location and size is usually byte. :-)

5. By reading the table directory, you are able to find out 'where' and
'how big' the CMAP table and GLYF table is.

6. Each table contains a certain type of information. CMAP table is
'character map' table, hence it list the glyph index for each character
code.

7. What are glyph index and character code? What is a glyph? A glyph is
a set of numbers that defines how a shape should be drawn. Therefore a
TTF file will have, of course, several hundred glyphs, up to several
thousand glyph. Each glyph ought to have a unique glyph index. A Big5
TTF file will normally have 13000 glyphs and each glyph has a unique
glyph index. For example, glyph no.96 is the Chinese character 'One'.

8. What is a character code then? Let's forget about TTF for a moment.
Big5 is the name of a 'character encoding scheme'. A character encoding
scheme seeks to identify a character with a number. A character is any
character which can be used in a human language, e.g. 'One' in Chinese.
The encoding scheme seeks to identify each character with a unique
number. So for example in Big5, 'One' (Chinese) encoded as 629. There
are 628 symbols before 'One' and about 13000 after 'One'.

8.1. The previous paragraph really describe only 'primary encoding'.
There is one further level of encoding a character and this varies
because of implementation limits. Taking 'One' as example, we can
represent its code 629 in many ways. e.g. We can use radix 157. So
629 = 4*157 + 1. Hence 629 can be represented as (4,1).

8.1.1. Notes on radix. We are familiar with radix 10, that is the
decimal number system. We are also familiar with radix 2, that is the
binary number system. However, in implementing Big5 code, radix 157 was
chosen for various reasons.

8.1.2. One further level of encoding will be to add a base value to each
number in the radix pair. e.g. (4,1) becomes (34,31) by adding 30 to
each number in the radix pair. To make matter worse, we can add
different base value to the numbers in the radix pair!! In Big5
implementation, we add 160 to the first number, 32 to the second number.
To make the matter worse again, we add 98 if the second number is
greater than 63. Taking one step further, theses number pairs
can be encoded using one byte each.

8.1.3. Here is a extract from a Big5 specification, ignore the
filenames. HBF means something else, ignore it.

CHARS 13867
HBF_START_BYTE_2_RANGES 2
HBF_BYTE_2_RANGE 0x40-0x7E
HBF_BYTE_2_RANGE 0xA1-0xFE
HBF_END_BYTE_2_RANGES
COMMENT
HBF_START_CODE_RANGES 5
HBF_CODE_RANGE 0xA140-0xA3BF spcfont.15 0
HBF_CODE_RANGE 0xA440-0xAAEE stdfont1.15 0
HBF_CODE_RANGE 0xAAEF-0xC67E stdfonts.15 0
HBF_CODE_RANGE 0xC6A1-0xC8D3 spcfsupp.15 0
HBF_CODE_RANGE 0xC940-0xF9FE stdfonts.15 129540
HBF_END_CODE_RANGES

8.1.4. So I hope you can see why Big5 code can be encoded using TWO
Bytes. The first byte or the high byte correspond the the first number
of the radix pair above, and the second byte or the low byte correspond
to the second number of the radix pair. Each number is represented in
hexadecimal notation.

9. Hence in CMAP table of a Big5 TTF file, you can find the glyph index
for each Big5 character code. In TTF specification, there are four
categories of encoding scheme.  Big5 or other two byte encoding scheme
is Format 3.

10. Using the glyph index, you can find the glyph of each character. The
GLYF table is a table containing all the glyphs.

Hence, we can see that whether or not a Big5 TTF file IS a Big5 TTF file
depends purely on the CMAP table. If we change the CMAP table, then it
becomes a TTF file with another different encoding scheme. So using this
techniques, we can make GB TTF file. Suppose Big5 'One' 0xA440 points to
glyph index 96. And 'One' in GB code is 0xCCAB. (I pick the number
arbitrarily). Then if we edit the CMAP entry such that the code points
to glyph 96 is 0xCCAB, then effectively we made a GB TTF.

In practice, however, instead of changing the character code with
respect to glyph index, it may be better to change the glyph index
instead. Either way will achieve the same effect.

This is purely a search and replace, table lookup problem.