package font import ( "bytes" "fmt" "image" "math" "os" "github.com/golang/freetype/truetype" "github.com/sirupsen/logrus" "github.com/zachomedia/go-bdf" "golang.org/x/image/font" "golang.org/x/image/font/opentype" "github.com/BeatGlow/display/draw" ) // Fonts contained var ( HackMono = Must(NewFromBytes(hackMonoTTF)) MairyBlackItalic = Must(NewFromBytes(mairyBlackItalicTTF)) MicroGrotesk = Must(NewFromBytes(microgroteskTTF)) Monaco = Must(NewFromBytes(monacoRegularTTF)) Montserrat = Must(NewFromBytes(montserratTTF)) MontserratItalic = Must(NewFromBytes(montserratItalicTTF)) SmoochSans = Must(NewFromBytes(smoochSansTTF)) ) type Font interface { Draw(dst draw.Image, p image.Point, fg image.Image, text string) MeasureString(string) (width, height int) // SetHintint changes the hinting. SetHinting(hinting Hinting) Font // SetDPI changes the dots-per-inch. SetDPI(float64) Font // SetPointSize changes the point size. SetPointSize(float64) Font // WithDPI returns a copy of the font with the requested dots-per-inch. WithDPI(float64) Font // WithPointSize returns a copy of the font with the requested point size. WithPointSize(float64) Font } func Must(font Font, err error) Font { if err != nil { panic(err) } return font } type FontAttr struct { PointSize float64 // Point size of the font DPI float64 // Resolution of the font in dots per inch LineSpacing float64 // Spacing between lines (in terms of font height) Hinting font.Hinting // Font hinting } // Hinting is a type alias. type Hinting = font.Hinting const ( HintingNone = font.HintingNone HintingVertical = font.HintingVertical HintingFull = font.HintingFull ) func (a *FontAttr) newTTOptions(scaleX, scaleY float64) *truetype.Options { dpi := a.DPI if scaleX*scaleY != 0 { dpi *= math.Sqrt(scaleX * scaleY) } return &truetype.Options{ Size: a.PointSize, DPI: dpi, Hinting: a.Hinting, } } func (a *FontAttr) newOTOptions(scaleX, scaleY float64) *opentype.FaceOptions { dpi := a.DPI if scaleX*scaleY != 0 { dpi *= math.Sqrt(scaleX * scaleY) } return &opentype.FaceOptions{ Size: a.PointSize, DPI: dpi, Hinting: a.Hinting, } } func New(name string) (Font, error) { b, err := os.ReadFile(name) if err != nil { return nil, err } return NewFromBytes(b) } func NewFromBytes(b []byte) (Font, error) { attr := FontAttr{ PointSize: 12, DPI: 72, LineSpacing: 1, Hinting: font.HintingFull, } switch { case bytes.HasPrefix(b, []byte("STARTFONT")): font, err := bdf.Parse(b) if err != nil { return nil, fmt.Errorf("invalid bitmap data font: %w", err) } logrus.Debugf("bdf: %d glyphs with encoding %s", len(font.Characters), font.Encoding) return &bdfFont{ font: font, face: font.NewFace().(*bdf.Face), }, nil /* case bytes.HasPrefix(b, []byte{0x00, 0x01, 0x00, 0x00, 0x00}): font, err := truetype.Parse(b) if err != nil { return nil, fmt.Errorf("invalid TrueType font: %w", err) } logrus.Debug("ttf") return &trueTypeFont{ font: font, face: truetype.NewFace(font, attr.newTTOptions(0, 0)), }, nil */ default: font, err := opentype.Parse(b) if err != nil { return nil, fmt.Errorf("invalid OpenType font: %w", err) } logrus.Debugf("otf: %d glyphs %s", font.NumGlyphs(), font) face, _ := opentype.NewFace(font, attr.newOTOptions(0, 0)) return &openTypeFont{ font: font, face: face, attr: attr, }, nil } }