From e88fe00bdb1a4c1414ce984a1de0c9a6a683d66d Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Tue, 1 Oct 2013 18:08:52 +1000 Subject: [PATCH] freetype/truetype: fix hinting of compound glyphs. R=bsiegert CC=golang-dev https://codereview.appspot.com/14117043 --- freetype/truetype/glyph.go | 22 ++++++++++++++++++++-- freetype/truetype/truetype_test.go | 2 +- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/freetype/truetype/glyph.go b/freetype/truetype/glyph.go index 3688670..ca94fe7 100644 --- a/freetype/truetype/glyph.go +++ b/freetype/truetype/glyph.go @@ -230,7 +230,7 @@ func (g *GlyphBuf) load(f *Font, scale int32, i Index, h *Hinter, g.End = make([]int, ne, ne*2) } for i := ne0; i < ne; i++ { - g.End[i] = 1 + np0 + int(u16(glyf, offset)) + g.End[i] = 1 + int(u16(glyf, offset)) offset += 2 } @@ -241,7 +241,7 @@ func (g *GlyphBuf) load(f *Font, scale int32, i Index, h *Hinter, offset += instrLen // Decode the points. - np := int(g.End[ne-1]) + np := int(g.End[ne-1]) + np0 if np <= cap(g.Point) { g.Point = g.Point[:np] } else { @@ -275,9 +275,27 @@ func (g *GlyphBuf) load(f *Font, scale int32, i Index, h *Hinter, } if h != nil { g.Unhinted = append(g.Unhinted, g.Point[np0:np]...) + // For compound glyphs, the hinting program expects the []Point and + // []End slices to be indexed relative to the inner glyph, not the + // outer glyph. Save the outer slices, run the program, and restore + // the outer slices. + // TODO: make these four slices arguments to Hinter.run? + gp, gu, gi, ge := g.Point, g.Unhinted, g.InFontUnits, g.End + g.Point = g.Point[np0:] + g.Unhinted = g.Unhinted[np0:] + g.InFontUnits = g.InFontUnits[np0:] + g.End = g.End[ne0:] if err := h.run(program); err != nil { return err } + g.Point, g.Unhinted, g.InFontUnits, g.End = gp, gu, gi, ge + } + + // The hinting program expects the []End values to be indexed relative + // to the inner glyph, not the outer glyph, so we delay adding np0 until + // after the hinting program (if any) has run. + for i := ne0; i < ne; i++ { + g.End[i] += np0 } return nil diff --git a/freetype/truetype/truetype_test.go b/freetype/truetype/truetype_test.go index 37dbfea..11d5571 100644 --- a/freetype/truetype/truetype_test.go +++ b/freetype/truetype/truetype_test.go @@ -116,7 +116,7 @@ func testScaling(t *testing.T, filename string, hinter *Hinter) { for i, want := range wants { // TODO: completely implement hinting. For now, only the first N glyphs // of luxisr.ttf are correctly hinted. - const N = 9 + const N = 106 if hinter != nil && i == N { break }