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

@ -35,19 +35,20 @@ const (
) )
type Context struct { type Context struct {
width int width int
height int height int
im *image.RGBA im *image.RGBA
color color.Color color color.Color
path raster.Path path raster.Path
start fixed.Point26_6 start fixed.Point26_6
lineWidth float64 lineWidth float64
lineCap LineCap lineCap LineCap
lineJoin LineJoin lineJoin LineJoin
fillRule FillRule fillRule FillRule
fontFace font.Face fontFace font.Face
matrix Matrix fontHeight float64
stack []*Context matrix Matrix
stack []*Context
} }
func NewContext(width, height int) *Context { func NewContext(width, height int) *Context {
@ -60,14 +61,15 @@ func NewContextForImage(im image.Image) *Context {
func NewContextForRGBA(im *image.RGBA) *Context { func NewContextForRGBA(im *image.RGBA) *Context {
return &Context{ return &Context{
width: im.Bounds().Size().X, width: im.Bounds().Size().X,
height: im.Bounds().Size().Y, height: im.Bounds().Size().Y,
im: im, im: im,
color: color.Transparent, color: color.Transparent,
lineWidth: 1, lineWidth: 1,
fillRule: FillRuleWinding, fillRule: FillRuleWinding,
fontFace: basicfont.Face7x13, fontFace: basicfont.Face7x13,
matrix: Identity(), fontHeight: 13,
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
@ -376,15 +379,16 @@ func (dc *Context) Push() {
path := make(raster.Path, len(dc.path)) path := make(raster.Path, len(dc.path))
copy(path, dc.path) copy(path, dc.path)
dc.stack = append(dc.stack, &Context{ dc.stack = append(dc.stack, &Context{
color: dc.color, color: dc.color,
path: path, path: path,
start: dc.start, start: dc.start,
lineWidth: dc.lineWidth, lineWidth: dc.lineWidth,
lineCap: dc.lineCap, lineCap: dc.lineCap,
lineJoin: dc.lineJoin, lineJoin: dc.lineJoin,
fillRule: dc.fillRule, fillRule: dc.fillRule,
fontFace: dc.fontFace, fontFace: dc.fontFace,
matrix: dc.matrix, fontHeight: dc.fontHeight,
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,
}) })
} }