From b8ed0a9c12b6f7f06a5fa5e4cb3740cd2b28dbb9 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Fri, 27 Aug 2010 15:49:35 +1000 Subject: [PATCH] freetype-go: change freetype.DrawText's point to be at the baseline instead of at 1em above the baseline. R=r CC=golang-dev http://codereview.appspot.com/2042041 --- example/freetype/main.go | 2 +- freetype/freetype.go | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/example/freetype/main.go b/example/freetype/main.go index 1675639..0fb4e8c 100644 --- a/example/freetype/main.go +++ b/example/freetype/main.go @@ -104,7 +104,7 @@ func main() { } // Draw the text. - pt := freetype.Pt(10, 10) + pt := freetype.Pt(10, 10+c.FUnitToPixelRU(font.UnitsPerEm())) for _, s := range text { err = c.DrawText(gcp, pt, s) if err != nil { diff --git a/freetype/freetype.go b/freetype/freetype.go index 4cda0b3..dee94ac 100644 --- a/freetype/freetype.go +++ b/freetype/freetype.go @@ -87,14 +87,14 @@ func (c *Context) drawContour(ps []truetype.Point, dx, dy raster.Fix32) { // going downwards, and offset by (dx, dy) start := raster.Point{ dx + c.FUnitToFix32(int(ps[0].X)), - dy + c.FUnitToFix32(c.upe-int(ps[0].Y)), + dy + c.FUnitToFix32(-int(ps[0].Y)), } c.r.Start(start) q0, on0 := start, true for _, p := range ps[1:] { q := raster.Point{ dx + c.FUnitToFix32(int(p.X)), - dy + c.FUnitToFix32(c.upe-int(p.Y)), + dy + c.FUnitToFix32(-int(p.Y)), } on := p.Flags&0x01 != 0 if on { @@ -124,11 +124,11 @@ func (c *Context) drawContour(ps []truetype.Point, dx, dy raster.Fix32) { } } -// DrawText draws s at pt using p. The text is placed so that the top left of -// the em square of the first character of s is equal to pt. The majority of -// the affected pixels will be below and to the right of pt, but some may be -// above or to the left. For example, drawing a string that starts with a 'J' -// in an italic font may affect pixels to the left of pt. +// DrawText draws s at pt using p. The text is placed so that the left edge of +// the em square of the first character of s and the baseline intersect at pt. +// The majority of the affected pixels will be above and to the right of pt, +// but some may be below or to the left. For example, drawing a string that +// starts with a 'J' in an italic font may affect pixels below and left of pt. // pt is a raster.Point and can therefore represent sub-pixel positions. func (c *Context) DrawText(p raster.Painter, pt raster.Point, s string) (err os.Error) { if c.font == nil { @@ -192,9 +192,9 @@ func (c *Context) recalc() { } else { b := c.font.Bounds() c.xmin = c.FUnitToPixelRD(int(b.XMin)) - c.ymin = c.FUnitToPixelRD(c.upe - int(b.YMax)) + c.ymin = c.FUnitToPixelRD(-int(b.YMax)) xmax := c.FUnitToPixelRU(int(b.XMax)) - ymax := c.FUnitToPixelRU(c.upe - int(b.YMin)) + ymax := c.FUnitToPixelRU(-int(b.YMin)) c.r.SetBounds(xmax-c.xmin, ymax-c.ymin) } }