fontHeight

This commit is contained in:
Michael Fogleman 2016-02-19 14:03:52 -05:00
parent 6a8c25552f
commit 0fd2b0f764
3 changed files with 50 additions and 40 deletions

View File

@ -46,6 +46,7 @@ type Context struct {
lineJoin LineJoin lineJoin LineJoin
fillRule FillRule fillRule FillRule
fontFace font.Face fontFace font.Face
fontHeight float64
matrix Matrix matrix Matrix
stack []*Context stack []*Context
} }
@ -67,6 +68,7 @@ func NewContextForRGBA(im *image.RGBA) *Context {
lineWidth: 1, lineWidth: 1,
fillRule: FillRuleWinding, fillRule: FillRuleWinding,
fontFace: basicfont.Face7x13, fontFace: basicfont.Face7x13,
fontHeight: 13,
matrix: Identity(), matrix: Identity(),
} }
} }
@ -315,8 +317,9 @@ func (dc *Context) SetFontFace(fontFace font.Face) {
dc.fontFace = fontFace dc.fontFace = fontFace
} }
func (dc *Context) LoadFontFace(path string, size float64) { func (dc *Context) LoadFontFace(path string, points float64) {
dc.fontFace = loadFontFace(path, size) dc.fontFace = loadFontFace(path, points)
dc.fontHeight = points * 72 / 96
} }
func (dc *Context) DrawString(x, y float64, s string) { func (dc *Context) DrawString(x, y float64, s string) {
@ -330,14 +333,14 @@ func (dc *Context) DrawString(x, y float64, s string) {
d.DrawString(s) d.DrawString(s)
} }
func (dc *Context) MeasureString(s string) float64 { func (dc *Context) MeasureString(s string) (w, h float64) {
d := &font.Drawer{ d := &font.Drawer{
Dst: nil, Dst: nil,
Src: nil, Src: nil,
Face: dc.fontFace, Face: dc.fontFace,
} }
a := d.MeasureString(s) a := d.MeasureString(s)
return float64(a >> 6) return float64(a >> 6), dc.fontHeight
} }
// Transformation Matrix Operations // Transformation Matrix Operations
@ -384,6 +387,7 @@ func (dc *Context) Push() {
lineJoin: dc.lineJoin, lineJoin: dc.lineJoin,
fillRule: dc.fillRule, fillRule: dc.fillRule,
fontFace: dc.fontFace, fontFace: dc.fontFace,
fontHeight: dc.fontHeight,
matrix: dc.matrix, matrix: dc.matrix,
}) })
} }
@ -399,5 +403,6 @@ func (dc *Context) Pop() {
dc.lineJoin = x.lineJoin dc.lineJoin = x.lineJoin
dc.fillRule = x.fillRule dc.fillRule = x.fillRule
dc.fontFace = x.fontFace dc.fontFace = x.fontFace
dc.fontHeight = x.fontHeight
dc.matrix = x.matrix dc.matrix = x.matrix
} }

View File

@ -1,15 +1,20 @@
package main package main
import "github.com/fogleman/gg" import (
"fmt"
"github.com/fogleman/gg"
)
func main() { func main() {
dc := gg.NewContext(1000, 1000) dc := gg.NewContext(1000, 1000)
dc.SetRGB(1, 1, 1) dc.SetRGB(1, 1, 1)
dc.Clear() dc.Clear()
dc.SetRGB(0, 0, 0) dc.SetRGB(0, 0, 0)
dc.LoadFontFace("/Library/Fonts/Impact.ttf", 96) dc.LoadFontFace("/Library/Fonts/Arial.ttf", 96)
s := "Hello, world!" s := "Hello, world!"
w := dc.MeasureString(s) w, h := dc.MeasureString(s)
dc.DrawString(500-w/2, 500, s) fmt.Println(w, h)
dc.DrawString(500-w/2, 500+h/2, s)
dc.SavePNG("out.png") dc.SavePNG("out.png")
} }

View File

@ -72,7 +72,7 @@ func fi(x float64) fixed.Int26_6 {
return fixed.Int26_6(x * 64) return fixed.Int26_6(x * 64)
} }
func loadFontFace(path string, size float64) font.Face { func loadFontFace(path string, points float64) font.Face {
fontBytes, err := ioutil.ReadFile(path) fontBytes, err := ioutil.ReadFile(path)
if err != nil { if err != nil {
panic(err) panic(err)
@ -82,7 +82,7 @@ func loadFontFace(path string, size float64) font.Face {
panic(err) panic(err)
} }
return truetype.NewFace(f, &truetype.Options{ return truetype.NewFace(f, &truetype.Options{
Size: size, Size: points,
Hinting: font.HintingFull, Hinting: font.HintingFull,
}) })
} }