package font import ( "image" "strings" "golang.org/x/image/font" "golang.org/x/image/font/opentype" "golang.org/x/image/math/fixed" "github.com/BeatGlow/display/draw" ) // openTypeFont contains a TrueType or OpenType font face. type openTypeFont struct { font *opentype.Font face font.Face attr FontAttr fg, bg image.Image scaleX, scaleY float64 changed bool } func (f openTypeFont) Attr() FontAttr { return f.attr } // SetPointSize sets the point size of the font. func (f *openTypeFont) 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 *openTypeFont) WithPointSize(size float64) Font { v := new(openTypeFont) *v = *f v.SetPointSize(size) return v } // SetDPI sets the resolution of the font in dots per inches (DPI). func (f *openTypeFont) 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 *openTypeFont) WithDPI(dpi float64) Font { v := new(openTypeFont) *v = *f v.SetDPI(dpi) return v } // SetLineSpacing sets the amount of spacing between lines (in terms of font height). func (f *openTypeFont) SetLineSpacing(spacing float64) { if spacing == f.attr.LineSpacing { return } f.attr.LineSpacing = spacing f.changed = true } // SetHinting sets the hinting type. func (f *openTypeFont) 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 *openTypeFont) 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 *openTypeFont) updateFace() { if f.changed { f.face, _ = opentype.NewFace(f.font, f.attr.newOTOptions(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 *openTypeFont) 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 *openTypeFont) Metrics() font.Metrics { f.updateFace() return f.face.Metrics() } // Draw text onto an image using this font. func (f *openTypeFont) 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 } } }