From c378327bfa3e79590bd7289b75bdfb9d8bf12748 Mon Sep 17 00:00:00 2001 From: Andrew Brampton Date: Sun, 6 Sep 2015 18:03:03 -0700 Subject: [PATCH] Update code.google.com/p/freetype-go to github.com/golang/freetype. Fixes #86. Updates the dependency, as well as other changes needed to support the newer version of freetype. --- draw2dbase/stack_gc.go | 2 +- draw2dgl/gc.go | 4 ++-- draw2dimg/ftgc.go | 38 ++++++++++++++++++++------------------ draw2dimg/ftpath.go | 7 ++++--- draw2dimg/text.go | 14 ++++++++------ draw2dpdf/gc.go | 2 +- font.go | 2 +- 7 files changed, 37 insertions(+), 32 deletions(-) diff --git a/draw2dbase/stack_gc.go b/draw2dbase/stack_gc.go index ff8f4f8..12723c7 100644 --- a/draw2dbase/stack_gc.go +++ b/draw2dbase/stack_gc.go @@ -9,7 +9,7 @@ import ( "github.com/llgcode/draw2d" - "code.google.com/p/freetype-go/freetype/truetype" + "github.com/golang/freetype/truetype" ) var DefaultFontData = draw2d.FontData{Name: "luxi", Family: draw2d.FontFamilySans, Style: draw2d.FontStyleNormal} diff --git a/draw2dgl/gc.go b/draw2dgl/gc.go index b738a5e..b8b3450 100644 --- a/draw2dgl/gc.go +++ b/draw2dgl/gc.go @@ -6,8 +6,8 @@ import ( "image/draw" "runtime" - "code.google.com/p/freetype-go/freetype/raster" "github.com/go-gl/gl/v2.1/gl" + "github.com/golang/freetype/raster" "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dbase" "github.com/llgcode/draw2d/draw2dimg" @@ -52,7 +52,7 @@ func (p *Painter) Paint(ss []raster.Span, done bool) { vertices []int32 ) for _, s := range ss { - ma := s.A >> 16 + ma := s.Alpha >> 16 a := uint8((ma * p.ca / M16) >> 8) colors = p.colors[ci:] colors[0] = p.cr diff --git a/draw2dimg/ftgc.go b/draw2dimg/ftgc.go index 2e8c52d..f0ebf03 100644 --- a/draw2dimg/ftgc.go +++ b/draw2dimg/ftgc.go @@ -14,8 +14,11 @@ import ( "github.com/llgcode/draw2d" "github.com/llgcode/draw2d/draw2dbase" - "code.google.com/p/freetype-go/freetype/raster" - "code.google.com/p/freetype-go/freetype/truetype" + "github.com/golang/freetype/raster" + "github.com/golang/freetype/truetype" + + "golang.org/x/image/font" + "golang.org/x/image/math/fixed" ) // Painter implements the freetype raster.Painter and has a SetColor method like the RGBAPainter @@ -58,7 +61,7 @@ func NewGraphicContextWithPainter(img draw.Image, painter Painter) *GraphicConte painter, raster.NewRasterizer(width, height), raster.NewRasterizer(width, height), - truetype.NewGlyphBuf(), + &truetype.GlyphBuf{}, dpi, } return gc @@ -128,12 +131,12 @@ func (gc *GraphicContext) loadCurrentFont() (*truetype.Font, error) { // going downwards. func (gc *GraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) error { - if err := gc.glyphBuf.Load(gc.Current.Font, int32(gc.Current.Scale), glyph, truetype.NoHinting); err != nil { + if err := gc.glyphBuf.Load(gc.Current.Font, fixed.Int26_6(gc.Current.Scale), glyph, font.HintingNone); err != nil { return err } e0 := 0 - for _, e1 := range gc.glyphBuf.End { - DrawContour(gc, gc.glyphBuf.Point[e0:e1], dx, dy) + for _, e1 := range gc.glyphBuf.Ends { + DrawContour(gc, gc.glyphBuf.Points[e0:e1], dx, dy) e0 = e1 } return nil @@ -146,7 +149,7 @@ func (gc *GraphicContext) drawGlyph(glyph truetype.Index, dx, dy float64) error // For example, drawing a string that starts with a 'J' in an italic font may // affect pixels below and left of the point. func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 { - font, err := gc.loadCurrentFont() + f, err := gc.loadCurrentFont() if err != nil { log.Println(err) return 0.0 @@ -154,16 +157,16 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 { startx := x prev, hasPrev := truetype.Index(0), false for _, rune := range s { - index := font.Index(rune) + index := f.Index(rune) if hasPrev { - x += fUnitsToFloat64(font.Kerning(int32(gc.Current.Scale), prev, index)) + x += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } err := gc.drawGlyph(index, x, y) if err != nil { log.Println(err) return startx - x } - x += fUnitsToFloat64(font.HMetric(int32(gc.Current.Scale), index).AdvanceWidth) + x += fUnitsToFloat64(f.HMetric(fixed.Int26_6(gc.Current.Scale), index).AdvanceWidth) prev, hasPrev = index, true } return x - startx @@ -174,7 +177,7 @@ func (gc *GraphicContext) CreateStringPath(s string, x, y float64) float64 { // and the baseline intersect at 0, 0 in the returned coordinates. // Therefore the top and left coordinates may well be negative. func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom float64) { - font, err := gc.loadCurrentFont() + f, err := gc.loadCurrentFont() if err != nil { log.Println(err) return 0, 0, 0, 0 @@ -183,18 +186,17 @@ func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom fl cursor := 0.0 prev, hasPrev := truetype.Index(0), false for _, rune := range s { - index := font.Index(rune) + index := f.Index(rune) if hasPrev { - - cursor += fUnitsToFloat64(font.Kerning(int32(gc.Current.Scale), prev, index)) + cursor += fUnitsToFloat64(f.Kern(fixed.Int26_6(gc.Current.Scale), prev, index)) } - if err := gc.glyphBuf.Load(gc.Current.Font, int32(gc.Current.Scale), index, truetype.NoHinting); err != nil { + if err := gc.glyphBuf.Load(gc.Current.Font, fixed.Int26_6(gc.Current.Scale), index, font.HintingNone); err != nil { log.Println(err) return 0, 0, 0, 0 } e0 := 0 - for _, e1 := range gc.glyphBuf.End { - ps := gc.glyphBuf.Point[e0:e1] + for _, e1 := range gc.glyphBuf.Ends { + ps := gc.glyphBuf.Points[e0:e1] for _, p := range ps { x, y := pointToF64Point(p) top = math.Min(top, y) @@ -203,7 +205,7 @@ func (gc *GraphicContext) GetStringBounds(s string) (left, top, right, bottom fl right = math.Max(right, x+cursor) } } - cursor += fUnitsToFloat64(font.HMetric(int32(gc.Current.Scale), index).AdvanceWidth) + cursor += fUnitsToFloat64(f.HMetric(fixed.Int26_6(gc.Current.Scale), index).AdvanceWidth) prev, hasPrev = index, true } return left, top, right, bottom diff --git a/draw2dimg/ftpath.go b/draw2dimg/ftpath.go index ce6c422..785fc31 100644 --- a/draw2dimg/ftpath.go +++ b/draw2dimg/ftpath.go @@ -4,7 +4,8 @@ package draw2dimg import ( - "code.google.com/p/freetype-go/freetype/raster" + "github.com/golang/freetype/raster" + "golang.org/x/image/math/fixed" ) type FtLineBuilder struct { @@ -12,11 +13,11 @@ type FtLineBuilder struct { } func (liner FtLineBuilder) MoveTo(x, y float64) { - liner.Adder.Start(raster.Point{X: raster.Fix32(x * 256), Y: raster.Fix32(y * 256)}) + liner.Adder.Start(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}) } func (liner FtLineBuilder) LineTo(x, y float64) { - liner.Adder.Add1(raster.Point{X: raster.Fix32(x * 256), Y: raster.Fix32(y * 256)}) + liner.Adder.Add1(fixed.Point26_6{X: fixed.Int26_6(x * 64), Y: fixed.Int26_6(y * 64)}) } func (liner FtLineBuilder) LineJoin() { diff --git a/draw2dimg/text.go b/draw2dimg/text.go index 78b0547..2fe2493 100644 --- a/draw2dimg/text.go +++ b/draw2dimg/text.go @@ -1,8 +1,10 @@ package draw2dimg import ( - "code.google.com/p/freetype-go/freetype/truetype" + "github.com/golang/freetype/truetype" "github.com/llgcode/draw2d" + + "golang.org/x/image/math/fixed" ) // DrawContour draws the given closed contour at the given sub-pixel offset. @@ -45,7 +47,7 @@ func pointToF64Point(p truetype.Point) (x, y float64) { return fUnitsToFloat64(p.X), -fUnitsToFloat64(p.Y) } -func fUnitsToFloat64(x int32) float64 { +func fUnitsToFloat64(x fixed.Int26_6) float64 { scaled := x << 2 return float64(scaled/256) + float64(scaled%256)/256.0 } @@ -70,11 +72,11 @@ type FontExtents struct { // Extents returns the FontExtents for a font. // TODO needs to read this https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/Chap2.html#intro func Extents(font *truetype.Font, size float64) FontExtents { - bounds := font.Bounds(font.FUnitsPerEm()) + bounds := font.Bounds(fixed.Int26_6(font.FUnitsPerEm())) scale := size / float64(font.FUnitsPerEm()) return FontExtents{ - Ascent: float64(bounds.YMax) * scale, - Descent: float64(bounds.YMin) * scale, - Height: float64(bounds.YMax-bounds.YMin) * scale, + Ascent: float64(bounds.Max.Y) * scale, + Descent: float64(bounds.Min.Y) * scale, + Height: float64(bounds.Max.Y-bounds.Min.Y) * scale, } } diff --git a/draw2dpdf/gc.go b/draw2dpdf/gc.go index c4ddd7d..c14610a 100644 --- a/draw2dpdf/gc.go +++ b/draw2dpdf/gc.go @@ -14,7 +14,7 @@ import ( "os" "strconv" - "code.google.com/p/freetype-go/freetype/truetype" + "github.com/golang/freetype/truetype" "github.com/jung-kurt/gofpdf" "github.com/llgcode/draw2d" diff --git a/font.go b/font.go index 279706c..1a1acf4 100644 --- a/font.go +++ b/font.go @@ -9,7 +9,7 @@ import ( "path" "path/filepath" - "code.google.com/p/freetype-go/freetype/truetype" + "github.com/golang/freetype/truetype" ) var (