From eda48ef1c36b6b722f99e6ae294588edadf7bf9e Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 15 Sep 2010 14:35:22 +1000 Subject: [PATCH] freetype: make the point an argument to DrawString, not part of the context state. Also rename DrawText to DrawString. R=r CC=golang-dev http://codereview.appspot.com/2208041 --- example/freetype/main.go | 3 +-- freetype/freetype.go | 33 +++++++++++++-------------------- 2 files changed, 14 insertions(+), 22 deletions(-) diff --git a/example/freetype/main.go b/example/freetype/main.go index f73d86c..97c7271 100644 --- a/example/freetype/main.go +++ b/example/freetype/main.go @@ -104,8 +104,7 @@ func main() { // Draw the text. pt := freetype.Pt(10, 10+c.FUnitToPixelRU(font.UnitsPerEm())) for _, s := range text { - c.SetPoint(pt) - err = c.DrawText(s) + _, err = c.DrawString(s, pt) if err != nil { log.Stderr(err) return diff --git a/freetype/freetype.go b/freetype/freetype.go index 4699f4b..0daa810 100644 --- a/freetype/freetype.go +++ b/freetype/freetype.go @@ -54,8 +54,6 @@ type Context struct { r *raster.Rasterizer font *truetype.Font glyphBuf *truetype.GlyphBuf - // pt is the location where drawing starts. - pt raster.Point // clip is the clip rectangle for drawing. clip image.Rectangle // dst and src are the destination and source images for drawing. @@ -209,27 +207,28 @@ func (c *Context) glyph(glyph truetype.Index, p raster.Point) (*image.Alpha, ima return mask, offset.Add(image.Point{ix, iy}), nil } -// DrawText draws s at the current point and then advances the point. The text +// DrawString draws s at p and returns p advanced by the text extent. The text // is placed so that the left edge of the em square of the first character of s -// and the baseline intersect at the point. The majority of the affected pixels -// will be above and to the right of the point, 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 the point. -func (c *Context) DrawText(s string) os.Error { +// and the baseline intersect at p. The majority of the affected pixels will be +// above and to the right of the point, 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 the point. +// p is a raster.Point and can therefore represent sub-pixel positions. +func (c *Context) DrawString(s string, p raster.Point) (raster.Point, os.Error) { if c.font == nil { - return os.NewError("freetype: DrawText called with a nil font") + return raster.Point{}, os.NewError("freetype: DrawText called with a nil font") } prev, hasPrev := truetype.Index(0), false for _, rune := range s { index := c.font.Index(rune) if hasPrev { - c.pt.X += c.FUnitToFix32(int(c.font.Kerning(prev, index))) + p.X += c.FUnitToFix32(int(c.font.Kerning(prev, index))) } - mask, offset, err := c.glyph(index, c.pt) + mask, offset, err := c.glyph(index, p) if err != nil { - return err + return raster.Point{}, err } - c.pt.X += c.FUnitToFix32(int(c.font.HMetric(index).AdvanceWidth)) + p.X += c.FUnitToFix32(int(c.font.HMetric(index).AdvanceWidth)) glyphRect := mask.Bounds().Add(offset) dr := c.clip.Intersect(glyphRect) if !dr.Empty() { @@ -238,7 +237,7 @@ func (c *Context) DrawText(s string) os.Error { } prev, hasPrev = index, true } - return nil + return p, nil } // recalc recalculates scale and bounds values from the font size, screen @@ -303,12 +302,6 @@ func (c *Context) SetSrc(src image.Image) { c.src = src } -// SetPoint sets the point where DrawText will next draw text. -// pt is a raster.Point and can therefore represent sub-pixel positions. -func (c *Context) SetPoint(pt raster.Point) { - c.pt = pt -} - // SetClip sets the clip rectangle for drawing. func (c *Context) SetClip(clip image.Rectangle) { c.clip = clip