From 0c4b93bdc323e67fb04ee7bd6e65e13cda53d475 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Wed, 26 May 2010 13:23:24 -0700 Subject: [PATCH] Freetype-Go: move the pixel offset from the painter to the rasterizer. R=rsc, r CC=rog, golang-dev http://codereview.appspot.com/1332041 --- freetype/freetype.go | 4 ++-- freetype/raster/paint.go | 42 ++++++++++++++++----------------------- freetype/raster/raster.go | 6 ++++-- 3 files changed, 23 insertions(+), 29 deletions(-) diff --git a/freetype/freetype.go b/freetype/freetype.go index 192e39b..7554de0 100644 --- a/freetype/freetype.go +++ b/freetype/freetype.go @@ -145,7 +145,7 @@ func (c *RGBAContext) DrawText(pt raster.Point, s string) (err os.Error) { advance, x0 := 0, pt.X dx := raster.Fixed(-c.xmin << 8) dy := raster.Fixed(-c.ymin << 8) - c.rp.Dy, y = c.ymin+int(pt.Y>>8), pt.Y&0xff + c.r.Dy, y = c.ymin+int(pt.Y>>8), pt.Y&0xff y += dy prev, hasPrev := truetype.Index(0), false for _, ch := range s { @@ -170,7 +170,7 @@ func (c *RGBAContext) DrawText(pt raster.Point, s string) (err os.Error) { x = x0 + c.FUnitToFixed(advance) // Break the co-ordinate down into an integer pixel part and a // sub-pixel part, making sure that the latter is non-negative. - c.rp.Dx, x = c.xmin+int(x>>8), x&0xff + c.r.Dx, x = c.xmin+int(x>>8), x&0xff x += dx // Draw the contours. c.r.Clear() diff --git a/freetype/raster/paint.go b/freetype/raster/paint.go index ba04eb1..ed06da8 100644 --- a/freetype/raster/paint.go +++ b/freetype/raster/paint.go @@ -39,38 +39,34 @@ type AlphaPainter struct { Image *image.Alpha // The Porter-Duff composition operator. Op draw.Op - // An offset (in pixels) to the painted spans. - Dx, Dy int } // Paint satisfies the Painter interface by painting ss onto an image.Alpha. func (r *AlphaPainter) Paint(ss []Span, done bool) { for _, s := range ss { - y := r.Dy + s.Y - if y < 0 { + if s.Y < 0 { continue } - if y >= len(r.Image.Pixel) { + if s.Y >= len(r.Image.Pixel) { return } - p := r.Image.Pixel[y] - x0, x1 := r.Dx+s.X0, r.Dx+s.X1 - if x0 < 0 { - x0 = 0 + p := r.Image.Pixel[s.Y] + if s.X0 < 0 { + s.X0 = 0 } - if x1 > len(p) { - x1 = len(p) + if s.X1 > len(p) { + s.X1 = len(p) } if r.Op == draw.Over { a := int(s.A >> 24) - for x := x0; x < x1; x++ { + for x := s.X0; x < s.X1; x++ { ax := int(p[x].A) ax = (ax*255 + (255-ax)*a) / 255 p[x] = image.AlphaColor{uint8(ax)} } } else { color := image.AlphaColor{uint8(s.A >> 24)} - for x := x0; x < x1; x++ { + for x := s.X0; x < s.X1; x++ { p[x] = color } } @@ -87,8 +83,6 @@ type RGBAPainter struct { Image *image.RGBA // The Porter-Duff composition operator. Op draw.Op - // An offset (in pixels) to the painted spans. - Dx, Dy int // The 16-bit color to paint the spans. cr, cg, cb, ca uint32 } @@ -96,22 +90,20 @@ type RGBAPainter struct { // Paint satisfies the Painter interface by painting ss onto an image.RGBA. func (r *RGBAPainter) Paint(ss []Span, done bool) { for _, s := range ss { - y := r.Dy + s.Y - if y < 0 { + if s.Y < 0 { continue } - if y >= len(r.Image.Pixel) { + if s.Y >= len(r.Image.Pixel) { return } - p := r.Image.Pixel[y] - x0, x1 := r.Dx+s.X0, r.Dx+s.X1 - if x0 < 0 { - x0 = 0 + p := r.Image.Pixel[s.Y] + if s.X0 < 0 { + s.X0 = 0 } - if x1 > len(p) { - x1 = len(p) + if s.X1 > len(p) { + s.X1 = len(p) } - for x := x0; x < x1; x++ { + for x := s.X0; x < s.X1; x++ { // This code is duplicated from drawGlyphOver in $GOROOT/src/pkg/exp/draw/draw.go. // TODO(nigeltao): Factor out common code into a utility function, once the compiler // can inline such function calls. diff --git a/freetype/raster/raster.go b/freetype/raster/raster.go index 8d0b837..e3038d2 100644 --- a/freetype/raster/raster.go +++ b/freetype/raster/raster.go @@ -64,6 +64,8 @@ type Rasterizer struct { // If false, the default behavior is to use the even-odd winding fill // rule during Rasterize. UseNonZeroWinding bool + // An offset (in pixels) to the painted spans. + Dx, Dy int // The width of the Rasterizer. The height is implicit in len(cellIndex). width int @@ -501,7 +503,7 @@ func (r *Rasterizer) Rasterize(p Painter) { xi1 = r.width } if xi0 < xi1 { - r.spanBuf[s] = Span{yi, xi0, xi1, alpha} + r.spanBuf[s] = Span{yi + r.Dy, xi0 + r.Dx, xi1 + r.Dx, alpha} s++ } } @@ -518,7 +520,7 @@ func (r *Rasterizer) Rasterize(p Painter) { xi1 = r.width } if xi0 < xi1 { - r.spanBuf[s] = Span{yi, xi0, xi1, alpha} + r.spanBuf[s] = Span{yi + r.Dy, xi0 + r.Dx, xi1 + r.Dx, alpha} s++ } }