From 84a65118940ed74c8e841be84d3e502c29eff703 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 31 Mar 2017 11:07:23 +1100 Subject: [PATCH] font/sfnt: use charmap EncodeRune API. Change-Id: I91d850d1816b43748580f6f8a8f19151472750d0 Reviewed-on: https://go-review.googlesource.com/39070 Reviewed-by: Marcel van Lohuizen Run-TryBot: Nigel Tao TryBot-Result: Gobot Gobot --- font/sfnt/cmap.go | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/font/sfnt/cmap.go b/font/sfnt/cmap.go index 42338f1..797e9d1 100644 --- a/font/sfnt/cmap.go +++ b/font/sfnt/cmap.go @@ -5,8 +5,6 @@ package sfnt import ( - "unicode/utf8" - "golang.org/x/text/encoding/charmap" ) @@ -112,20 +110,12 @@ func (f *Font) makeCachedGlyphIndexFormat0(buf []byte, offset, length uint32) ([ var table [256]byte copy(table[:], buf[6:]) return buf, func(f *Font, b *Buffer, r rune) (GlyphIndex, error) { - // TODO: for this closure to be goroutine-safe, the - // golang.org/x/text/encoding/charmap API needs to allocate a new - // Encoder and new []byte buffers, for every call to this closure, even - // though all we want to do is to encode one rune as one byte. We could - // possibly add some fields in the Buffer struct to re-use these - // allocations, but a better solution is to improve the charmap API. - var dst, src [utf8.UTFMax]byte - n := utf8.EncodeRune(src[:], r) - _, _, err = charmap.Macintosh.NewEncoder().Transform(dst[:], src[:n], true) - if err != nil { + x, ok := charmap.Macintosh.EncodeRune(r) + if !ok { // The source rune r is not representable in the Macintosh-Roman encoding. return 0, nil } - return GlyphIndex(table[dst[0]]), nil + return GlyphIndex(table[x]), nil }, nil }