Skip to content

Commit 3f44ec7

Browse files
authored
Merge pull request #3 from anovik/cmap6
Added support of cmap format 6
2 parents 3efbeef + bc8913e commit 3f44ec7

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

truetype/truetype.go

+22
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,16 @@ type Font struct {
201201
cmGlyphIDArray []uint8
202202
charcodeToGID map[uint8]uint32
203203
hasShortCmap bool
204+
// support of cmap format 6
205+
cmGlyphIDArrayUint16 []uint16
206+
firstCode uint16
204207
}
205208

206209
func (f *Font) parseCmap() error {
207210
const (
208211
cmapFormat0 = 0
209212
cmapFormat4 = 4
213+
cmapFormat6 = 6
210214
cmapFormat12 = 12
211215
languageIndependent = 0
212216
)
@@ -301,6 +305,17 @@ func (f *Font) parseCmap() error {
301305
f.charcodeToGID = charcodeMap
302306
}
303307
return nil
308+
case cmapFormat6:
309+
firstCode := u16(f.cmap, offset+6)
310+
entryCount := u16(f.cmap, offset+8)
311+
glyphIDArray := make([]uint16, entryCount)
312+
for i := 0; i < int(entryCount); i++ {
313+
glyphIDArray[i] = u16(f.cmap, offset+10+2*i)
314+
}
315+
f.cmGlyphIDArrayUint16 = glyphIDArray
316+
f.hasShortCmap = true
317+
f.firstCode = firstCode
318+
return nil
304319
case cmapFormat12:
305320
if u16(f.cmap, offset+2) != 0 {
306321
return FormatError(fmt.Sprintf("cmap format: % x", f.cmap[offset:offset+4]))
@@ -455,6 +470,11 @@ func (f *Font) Index(x rune) Index {
455470
return Index(val)
456471
}
457472

473+
if len(f.cmGlyphIDArrayUint16)+int(f.firstCode) > int(c) && int(f.firstCode) <= int(c) {
474+
val := f.cmGlyphIDArrayUint16[c-uint32(f.firstCode)]
475+
return Index(val)
476+
}
477+
458478
if len(f.charcodeToGID) > 0 {
459479
val := f.charcodeToGID[uint8(c)]
460480
return Index(val)
@@ -628,6 +648,8 @@ func parse(ttf []byte, offset int) (font *Font, err error) {
628648
switch magic {
629649
case 0x00010000:
630650
// No-op.
651+
case 0x74727565:
652+
// No-op.
631653
case 0x74746366: // "ttcf" as a big-endian uint32.
632654
if originalOffset != 0 {
633655
err = FormatError("recursive TTC")

0 commit comments

Comments
 (0)