the first loaded font is now used as the default font

This commit is contained in:
Thomas Friedel 2018-04-04 17:22:03 +02:00
parent 3854f40898
commit db1797c19a
2 changed files with 33 additions and 12 deletions

View file

@ -442,8 +442,15 @@ func (cv *Canvas) SetLineWidth(width float64) {
cv.state.lineWidth = width
}
// SetFont sets the font and font size
// SetFont sets the font and font size. The font parameter can be:
// - a font loaded with the LoadFont function
// - a string matching the name parameter on LoadFont
// - a filename for a font to load which will be cached
// - nil, in which case the first loaded font will be used
func (cv *Canvas) SetFont(font interface{}, size float64) {
if font == nil {
cv.state.font = defaultFont
} else {
switch v := font.(type) {
case *Font:
cv.state.font = v
@ -459,6 +466,7 @@ func (cv *Canvas) SetFont(font interface{}, size float64) {
}
}
}
}
cv.state.fontSize = size
}

13
text.go
View file

@ -20,6 +20,8 @@ type Font struct {
var fonts = make(map[string]*Font)
var zeroes [alphaTexSize]byte
var defaultFont *Font
func LoadFont(src interface{}, name string) (*Font, error) {
var f *Font
switch v := src.(type) {
@ -47,12 +49,19 @@ func LoadFont(src interface{}, name string) (*Font, error) {
if name != "" {
fonts[name] = f
}
if defaultFont == nil {
defaultFont = f
}
return f, nil
}
func (cv *Canvas) FillText(str string, x, y float64) {
cv.activate()
if cv.state.font == nil {
return
}
frc := fontRenderingContext
frc.setFont(cv.state.font.font)
frc.setFontSize(float64(cv.state.fontSize))
@ -129,6 +138,10 @@ type TextMetrics struct {
}
func (cv *Canvas) MeasureText(str string) TextMetrics {
if cv.state.font == nil {
return TextMetrics{}
}
frc := fontRenderingContext
frc.setFont(cv.state.font.font)
frc.setFontSize(float64(cv.state.fontSize))