Files
beatglow/app/font/font_ttf.go
2026-10-29 17:29:54 +01:00

151 lines
3.4 KiB
Go

package font
import (
"image"
"strings"
"github.com/golang/freetype/truetype"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"github.com/BeatGlow/display/draw"
)
// trueTypeFont contains a TrueType font face.
type trueTypeFont struct {
font *truetype.Font
face font.Face
attr FontAttr
fg, bg image.Image
scaleX, scaleY float64
changed bool
}
func (f trueTypeFont) Attr() FontAttr {
return f.attr
}
// SetPointSize sets the point size of the font.
func (f *trueTypeFont) SetPointSize(size float64) Font {
if size == f.attr.PointSize {
return f
}
f.attr.PointSize = size
f.changed = true
return f
}
// WithPointSize returns a new font instance with new point size settings.
func (f *trueTypeFont) WithPointSize(size float64) Font {
v := new(trueTypeFont)
*v = *f
v.SetPointSize(size)
return v
}
// SetDPI sets the resolution of the font in dots per inches (DPI).
func (f *trueTypeFont) SetDPI(dpi float64) Font {
if dpi == f.attr.DPI {
return f
}
f.attr.DPI = dpi
f.changed = true
return f
}
// WithDPI returns a new font instance with new DPI settings.
func (f *trueTypeFont) WithDPI(dpi float64) Font {
v := new(trueTypeFont)
*v = *f
v.SetDPI(dpi)
return v
}
// SetLineSpacing sets the amount of spacing between lines (in terms of font height).
func (f *trueTypeFont) SetLineSpacing(spacing float64) {
if spacing == f.attr.LineSpacing {
return
}
f.attr.LineSpacing = spacing
f.changed = true
}
// SetHinting sets the hinting type.
func (f *trueTypeFont) SetHinting(hinting Hinting) Font {
if hinting == f.attr.Hinting {
return f
}
f.attr.Hinting = hinting
f.changed = true
return f
}
// SetScale sets the ratio of actual pixel/GL point.
func (f *trueTypeFont) SetScaleXY(x, y float64) {
if x == f.scaleX && y == f.scaleY {
return
}
f.scaleX = x
f.scaleY = y
f.changed = true
}
// updateFace updates the font face if parameters have changed.
func (f *trueTypeFont) updateFace() {
if f.changed {
f.face = truetype.NewFace(f.font, f.attr.newTTOptions(f.scaleX, f.scaleY))
f.changed = false
}
}
// MeasureText returns the minimum width and height in pixels necessary for an image to contain
// the specified text. The supplied text string can contain line break escape sequences (\n).
func (f *trueTypeFont) MeasureString(text string) (width, height int) {
f.updateFace()
bounds, _ := font.BoundString(f.face, text)
width = (bounds.Max.X - bounds.Min.X).Ceil()
metrics := f.face.Metrics()
height = (metrics.Ascent + metrics.Descent).Ceil()
return
}
// Metrics returns the font metrics.
func (f *trueTypeFont) Metrics() font.Metrics {
f.updateFace()
return f.face.Metrics()
}
// Draw text onto an image using this font.
func (f *trueTypeFont) Draw(dst draw.Image, p image.Point, fg image.Image, text string) {
f.updateFace()
var (
metrics = f.face.Metrics()
py = p.Y + metrics.Ascent.Round()
lineHeight = (metrics.Ascent + metrics.Descent).Ceil()
lineGap = int((f.attr.LineSpacing - float64(1)) * float64(lineHeight))
lines = strings.Split(text, "\n")
drawer = &font.Drawer{
Dst: dst,
Src: fg,
Face: f.face,
}
)
bounds, _ := drawer.BoundString(text)
drawer.Dot = fixed.P(p.X, 1+p.Y)
drawer.Dot = drawer.Dot.Sub(bounds.Min)
for i, line := range lines {
drawer.DrawString(line)
drawer.Dot.Y += fixed.I(py)
py += lineHeight
if i > 1 {
py += lineGap
}
}
}