model: add a reference to width table format and simplify the code

This commit is contained in:
Denys Smirnov 2018-11-29 05:43:08 +02:00
parent 7e2a987f8a
commit e07fa3b2c0

View File

@ -439,25 +439,37 @@ func NewCompositePdfFontFromTTFFile(filePath string) (*PdfFont, error) {
// Construct W array. Stores character code to width mappings.
wArr := &core.PdfObjectArray{}
for i := uint16(0); int(i) < len(runes); {
// 9.7.4.3 Glyph metrics in CIDFonts
j := i + 1
for int(j) < len(runes) {
if runeToWidthMap[runes[i]] != runeToWidthMap[runes[j]] {
// In the first format, c shall be an integer specifying a starting CID value; it shall be followed by an array of
// n numbers that shall specify the widths for n consecutive CIDs, starting with c.
// The second format shall define the same width, w, as a number, for all CIDs in the range c_first to c_last.
// We always use the second format.
// TODO(dennwc): this can be done on GIDs instead of runes
for i := 0; i < len(runes); {
w := runeToWidthMap[runes[i]]
li := i
for j := i + 1; j < len(runes); j++ {
w2 := runeToWidthMap[runes[j]]
if w == w2 {
li = j
} else {
break
}
j++
}
// The W maps from CID to width, here CID = GID.
gid1 := ttf.Chars[runes[i]]
gid2 := ttf.Chars[runes[j-1]]
gid2 := ttf.Chars[runes[li]]
wArr.Append(core.MakeInteger(int64(gid1)))
wArr.Append(core.MakeInteger(int64(gid2)))
wArr.Append(core.MakeInteger(int64(runeToWidthMap[runes[i]])))
wArr.Append(core.MakeInteger(int64(w)))
i = j
i = li + 1
}
cidfont.W = core.MakeIndirectObject(wArr)