fontHeight
This commit is contained in:
parent
6a8c25552f
commit
0fd2b0f764
73
context.go
73
context.go
@ -35,19 +35,20 @@ const (
|
||||
)
|
||||
|
||||
type Context struct {
|
||||
width int
|
||||
height int
|
||||
im *image.RGBA
|
||||
color color.Color
|
||||
path raster.Path
|
||||
start fixed.Point26_6
|
||||
lineWidth float64
|
||||
lineCap LineCap
|
||||
lineJoin LineJoin
|
||||
fillRule FillRule
|
||||
fontFace font.Face
|
||||
matrix Matrix
|
||||
stack []*Context
|
||||
width int
|
||||
height int
|
||||
im *image.RGBA
|
||||
color color.Color
|
||||
path raster.Path
|
||||
start fixed.Point26_6
|
||||
lineWidth float64
|
||||
lineCap LineCap
|
||||
lineJoin LineJoin
|
||||
fillRule FillRule
|
||||
fontFace font.Face
|
||||
fontHeight float64
|
||||
matrix Matrix
|
||||
stack []*Context
|
||||
}
|
||||
|
||||
func NewContext(width, height int) *Context {
|
||||
@ -60,14 +61,15 @@ func NewContextForImage(im image.Image) *Context {
|
||||
|
||||
func NewContextForRGBA(im *image.RGBA) *Context {
|
||||
return &Context{
|
||||
width: im.Bounds().Size().X,
|
||||
height: im.Bounds().Size().Y,
|
||||
im: im,
|
||||
color: color.Transparent,
|
||||
lineWidth: 1,
|
||||
fillRule: FillRuleWinding,
|
||||
fontFace: basicfont.Face7x13,
|
||||
matrix: Identity(),
|
||||
width: im.Bounds().Size().X,
|
||||
height: im.Bounds().Size().Y,
|
||||
im: im,
|
||||
color: color.Transparent,
|
||||
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
|
||||
@ -376,15 +379,16 @@ func (dc *Context) Push() {
|
||||
path := make(raster.Path, len(dc.path))
|
||||
copy(path, dc.path)
|
||||
dc.stack = append(dc.stack, &Context{
|
||||
color: dc.color,
|
||||
path: path,
|
||||
start: dc.start,
|
||||
lineWidth: dc.lineWidth,
|
||||
lineCap: dc.lineCap,
|
||||
lineJoin: dc.lineJoin,
|
||||
fillRule: dc.fillRule,
|
||||
fontFace: dc.fontFace,
|
||||
matrix: dc.matrix,
|
||||
color: dc.color,
|
||||
path: path,
|
||||
start: dc.start,
|
||||
lineWidth: dc.lineWidth,
|
||||
lineCap: dc.lineCap,
|
||||
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
|
||||
}
|
||||
|
@ -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")
|
||||
}
|
||||
|
4
util.go
4
util.go
@ -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,
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user