Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 82 additions & 36 deletions font/cmap_cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions font/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,9 @@ func loadGDEF(ld *ot.Loader, axisCount int, gsub, gpos []byte) (tables.GDEF, err
type Face struct {
*Font

extentsCache extentsCache
cmapCache cache21_19_8
extentsCache extentsCache
cmapCache cache21_19_8 // supported runes, mapping to GID
cmapNotSupportedCache cache21_0_13 // not supported runes

coords []tables.Coord
xPpem, yPpem uint16
Expand All @@ -631,6 +632,7 @@ type Face struct {
func NewFace(font *Font) *Face {
out := &Face{Font: font, extentsCache: make(extentsCache, font.nGlyphs)}
out.cmapCache.clear()
out.cmapNotSupportedCache.clear()
return out
}

Expand All @@ -639,12 +641,17 @@ func NewFace(font *Font) *Face {
// Note that it only looks into the cmap, without taking account substitutions
// nor variation selectors.
func (f *Face) NominalGlyph(ch rune) (GID, bool) {
if notSupported := f.cmapNotSupportedCache.get(uint32(ch)); notSupported {
return 0, false
}
if g, ok := f.cmapCache.get(uint32(ch)); ok {
return GID(g), ok
}
g, ok := f.Cmap.Lookup(ch)
if ok {
f.cmapCache.set(uint32(ch), uint32(g))
} else {
f.cmapNotSupportedCache.set(uint32(ch))
}
return g, ok
}
Expand Down
32 changes: 32 additions & 0 deletions font/font_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,35 @@ func TestBitmapExtents(t *testing.T) {
extents, ok := face.GlyphExtents(41)
tu.Assert(t, ok && extents.Width == 819.2 && extents.Height == -1433.6)
}

func BenchmarkCmap(b *testing.B) {
font := loadFont(b, "common/Roboto-BoldItalic.ttf")
face := NewFace(font)
latinText := []rune("Hi this is a test with some âccents : $£8")
chineseText := []rune("襄陽曲四首/魯中都東樓醉起作-李白 刊误")
mixedText := append(latinText, chineseText...)

b.ResetTimer()

b.Run("latin text", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, r := range latinText {
_, _ = face.NominalGlyph(r)
}
}
})
b.Run("chinese text", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, r := range chineseText {
_, _ = face.NominalGlyph(r)
}
}
})
b.Run("mixed text", func(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, r := range mixedText {
_, _ = face.NominalGlyph(r)
}
}
})
}
3 changes: 2 additions & 1 deletion font/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ func TestAppleBitmapGlyph(t *testing.T) {
ft, err := NewFont(fonts[0])
tu.AssertNoErr(t, err)

face := Face{Font: ft, xPpem: 94, yPpem: 94}
face := NewFace(ft)
face.SetPpem(94, 94)

runes := "The quick brown fox jumps over the lazy dog"
for _, r := range runes {
Expand Down
93 changes: 54 additions & 39 deletions harfbuzz/caches.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading