Skip to content

Commit

Permalink
Merge pull request #209 from oakmound/fix/emptyfontoptions
Browse files Browse the repository at this point in the history
render/font: Enforce default truetype font height for Height()
  • Loading branch information
200sc committed Jun 11, 2022
2 parents 3325adc + 80d3cfd commit df66d92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
7 changes: 6 additions & 1 deletion render/font.go
Expand Up @@ -29,6 +29,8 @@ var (
// DefaultFontGenerator is a default font generator, using an internally
// compiled font colored white by default.
DefaultFontGenerator = DefFontGenerator

defFontSize = 12.0
)

// A Font can create text renderables. It should be constructed from
Expand Down Expand Up @@ -100,7 +102,7 @@ func (fg *FontGenerator) Generate() (*Font, error) {
}

// This logic is copied from truetype for their face scaling
size := 12.0
size := defFontSize
if fg.FontOptions.Size != 0 {
size = fg.FontOptions.Size
}
Expand Down Expand Up @@ -217,6 +219,9 @@ func (f *Font) drawString(s string) {

// Height returns the height or size of the font
func (f *Font) Height() float64 {
if f.gen.Size == 0 {
return defFontSize
}
return f.gen.Size
}

Expand Down
13 changes: 13 additions & 0 deletions render/font_test.go
Expand Up @@ -120,6 +120,19 @@ func TestFont_Height(t *testing.T) {
if f.Height() != ht {
t.Fatalf("size did not match height: got %v expected %v", f.Height(), ht)
}

fgEmpty := FontGenerator{
File: "testdata/assets/fonts/luxisr.ttf",
Color: image.NewUniform(color.RGBA{255, 0, 0, 255}),
}
f, err = fgEmpty.Generate()
if err != nil {
t.Fatalf("generate failed: %v", err)
}
if f.Height() != defFontSize {
t.Fatalf("size did not match height: got %v expected %v", f.Height(), ht)
}

}

func TestFont_RegenerateWith(t *testing.T) {
Expand Down

0 comments on commit df66d92

Please sign in to comment.