From 06369b10f7729c1f383a6a5561eccb401ae49530 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Mon, 13 Sep 2010 18:20:47 +1000 Subject: [PATCH] freetype: add a clip rectangle. R=r CC=golang-dev http://codereview.appspot.com/2171045 --- example/freetype/main.go | 1 + freetype/freetype.go | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/example/freetype/main.go b/example/freetype/main.go index 018aefb..f73d86c 100644 --- a/example/freetype/main.go +++ b/example/freetype/main.go @@ -91,6 +91,7 @@ func main() { c.SetDPI(*dpi) c.SetFont(font) c.SetFontSize(*size) + c.SetClip(rgba.Bounds()) c.SetDst(rgba) c.SetSrc(fg) diff --git a/freetype/freetype.go b/freetype/freetype.go index 9b92229..4699f4b 100644 --- a/freetype/freetype.go +++ b/freetype/freetype.go @@ -56,6 +56,8 @@ type Context struct { 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. dst draw.Image src image.Image @@ -228,7 +230,12 @@ func (c *Context) DrawText(s string) os.Error { return err } c.pt.X += c.FUnitToFix32(int(c.font.HMetric(index).AdvanceWidth)) - draw.DrawMask(c.dst, mask.Bounds().Add(offset), c.src, image.ZP, mask, image.ZP, draw.Over) + glyphRect := mask.Bounds().Add(offset) + dr := c.clip.Intersect(glyphRect) + if !dr.Empty() { + mp := image.Point{0, dr.Min.Y - glyphRect.Min.Y} + draw.DrawMask(c.dst, dr, c.src, image.ZP, mask, mp, draw.Over) + } prev, hasPrev = index, true } return nil @@ -302,6 +309,11 @@ 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 +} + // TODO(nigeltao): implement Context.SetGamma. // NewContext creates a new Context.