From 0ec7c0ecaefd32313b11d4140568160700b08b40 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 4 Oct 2013 08:30:53 +1000 Subject: [PATCH] freetype/truetype: fix IUP rounding for negative numbers. We now match Freetype C exactly for hinting luxisr.ttf. Yay. R=bsiegert CC=golang-dev https://codereview.appspot.com/14336044 --- freetype/truetype/hint.go | 60 ++++++++++++++++++------------ freetype/truetype/truetype_test.go | 7 ---- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/freetype/truetype/hint.go b/freetype/truetype/hint.go index d4ea9b8..f1a869c 100644 --- a/freetype/truetype/hint.go +++ b/freetype/truetype/hint.go @@ -1108,36 +1108,48 @@ func (h *Hinter) iupInterp(interpY bool, p1, p2, ref1, ref2 int) { h.points[glyphZone][current][i].X = xy } } + return + } - } else { - scale, scaleOK := int64(0), false - for i := p1; i <= p2; i++ { - if interpY { - xy = h.points[glyphZone][unhinted][i].Y - ifuXY = h.points[glyphZone][inFontUnits][i].Y - } else { - xy = h.points[glyphZone][unhinted][i].X - ifuXY = h.points[glyphZone][inFontUnits][i].X - } + scale, scaleOK := int64(0), false + for i := p1; i <= p2; i++ { + if interpY { + xy = h.points[glyphZone][unhinted][i].Y + ifuXY = h.points[glyphZone][inFontUnits][i].Y + } else { + xy = h.points[glyphZone][unhinted][i].X + ifuXY = h.points[glyphZone][inFontUnits][i].X + } - if xy <= unh1 { - xy += delta1 - } else if xy >= unh2 { - xy += delta2 - } else { - if !scaleOK { - scaleOK = true - denom := int64(ifu2 - ifu1) - scale = (int64(unh2+delta2-unh1-delta1)*0x10000 + denom/2) / denom + if xy <= unh1 { + xy += delta1 + } else if xy >= unh2 { + xy += delta2 + } else { + if !scaleOK { + scaleOK = true + numer := int64(unh2+delta2-unh1-delta1) * 0x10000 + denom := int64(ifu2 - ifu1) + if numer >= 0 { + numer += denom / 2 + } else { + numer -= denom / 2 } - xy = unh1 + delta1 + int32((int64(ifuXY-ifu1)*scale+0x8000)/0x10000) + scale = numer / denom } - - if interpY { - h.points[glyphZone][current][i].Y = xy + numer := int64(ifuXY-ifu1) * scale + if numer >= 0 { + numer += 0x8000 } else { - h.points[glyphZone][current][i].X = xy + numer -= 0x8000 } + xy = unh1 + delta1 + int32(numer/0x10000) + } + + if interpY { + h.points[glyphZone][current][i].Y = xy + } else { + h.points[glyphZone][current][i].X = xy } } } diff --git a/freetype/truetype/truetype_test.go b/freetype/truetype/truetype_test.go index f784fce..18b65b5 100644 --- a/freetype/truetype/truetype_test.go +++ b/freetype/truetype/truetype_test.go @@ -114,13 +114,6 @@ func testScaling(t *testing.T, filename string, hinter *Hinter) { const fontSize = 12 glyphBuf := NewGlyphBuf() for i, want := range wants { - // TODO: completely implement hinting. For now, only the first N glyphs - // of luxisr.ttf are correctly hinted. - const N = 136 - if hinter != nil && i == N { - break - } - if err = glyphBuf.Load(font, fontSize*64, Index(i), hinter); err != nil { t.Fatalf("Load: %v", err) }