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

View File

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

View File

@ -72,7 +72,7 @@ func fi(x float64) fixed.Int26_6 {
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)
if err != nil {
panic(err)
@ -82,7 +82,7 @@ func loadFontFace(path string, size float64) font.Face {
panic(err)
}
return truetype.NewFace(f, &truetype.Options{
Size: size,
Size: points,
Hinting: font.HintingFull,
})
}