Checkpoint
This commit is contained in:
223 files changed
+14770
-18352
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,45 @@
|
||||
The work in the Hack project is Copyright 2018 Source Foundry Authors and licensed under the MIT License
|
||||
|
||||
The work in the DejaVu project was committed to the public domain.
|
||||
|
||||
Bitstream Vera Sans Mono Copyright 2003 Bitstream Inc. and licensed under the Bitstream Vera License with Reserved Font Names "Bitstream" and "Vera"
|
||||
|
||||
### MIT License
|
||||
|
||||
Copyright (c) 2018 Source Foundry Authors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
|
||||
### BITSTREAM VERA LICENSE
|
||||
|
||||
Copyright (c) 2003 by Bitstream, Inc. All Rights Reserved. Bitstream Vera is a trademark of Bitstream, Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of the fonts accompanying this license ("Fonts") and associated documentation files (the "Font Software"), to reproduce and distribute the Font Software, including without limitation the rights to use, copy, merge, publish, distribute, and/or sell copies of the Font Software, and to permit persons to whom the Font Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright and trademark notices and this permission notice shall be included in all copies of one or more of the Font Software typefaces.
|
||||
|
||||
The Font Software may be modified, altered, or added to, and in particular the designs of glyphs or characters in the Fonts may be modified and additional glyphs or characters may be added to the Fonts, only if the fonts are renamed to names not containing either the words "Bitstream" or the word "Vera".
|
||||
|
||||
This License becomes null and void to the extent applicable to Fonts or Font Software that has been modified and is distributed under the "Bitstream Vera" names.
|
||||
|
||||
The Font Software may be sold as part of a larger software package but no copy of one or more of the Font Software typefaces may be sold by itself.
|
||||
|
||||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL BITSTREAM OR THE GNOME FOUNDATION BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER DEALINGS IN THE FONT SOFTWARE.
|
||||
|
||||
Except as contained in this notice, the names of Gnome, the Gnome Foundation, and Bitstream Inc., shall not be used in advertising or otherwise to promote the sale, use or other dealings in this Font Software without prior written authorization from the Gnome Foundation or Bitstream Inc., respectively. For further information, contact: fonts at gnome dot org.
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
Binary file not shown.
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,43 @@
|
||||
package font
|
||||
|
||||
import _ "embed"
|
||||
|
||||
var (
|
||||
//go:embed asset/sm4tik.bdf
|
||||
sm4tikBDF []byte
|
||||
|
||||
//go:embed asset/siji.bdf
|
||||
sijiBDF []byte
|
||||
|
||||
//go:embed asset/spleen-5x8.bdf
|
||||
spleen5x8BDF []byte
|
||||
|
||||
//go:embed asset/spleen-6x12.bdf
|
||||
spleen6x12BDF []byte
|
||||
|
||||
//go:embed asset/spleen-8x16.bdf
|
||||
spleen8x16BDF []byte
|
||||
)
|
||||
|
||||
var (
|
||||
//go:embed asset/HackNerdFontMono-Regular.ttf
|
||||
hackMonoTTF []byte
|
||||
|
||||
//go:embed asset/Mairy-BlackItalic.ttf
|
||||
mairyBlackItalicTTF []byte
|
||||
|
||||
//go:embed asset/MicroGrotesk.ttf
|
||||
microgroteskTTF []byte
|
||||
|
||||
//go:embed asset/Monaco.ttf
|
||||
monacoRegularTTF []byte
|
||||
|
||||
//go:embed asset/Montserrat.ttf
|
||||
montserratTTF []byte
|
||||
|
||||
//go:embed asset/Montserrat-Italic.ttf
|
||||
montserratItalicTTF []byte
|
||||
|
||||
//go:embed asset/SmoochSans-Regular.ttf
|
||||
smoochSansTTF []byte
|
||||
)
|
||||
@@ -0,0 +1,151 @@
|
||||
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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package font
|
||||
|
||||
import (
|
||||
"image"
|
||||
|
||||
"github.com/zachomedia/go-bdf"
|
||||
"golang.org/x/image/math/fixed"
|
||||
|
||||
"github.com/BeatGlow/display/draw"
|
||||
)
|
||||
|
||||
// Bitmap Distribution Format fonts contained.
|
||||
var (
|
||||
SM4TIK = Must(NewFromBytes(sm4tikBDF))
|
||||
Siji = Must(NewFromBytes(sijiBDF))
|
||||
Spleen5x8 = Must(NewFromBytes(spleen5x8BDF))
|
||||
Spleen6x12 = Must(NewFromBytes(spleen6x12BDF))
|
||||
Spleen8x16 = Must(NewFromBytes(spleen8x16BDF))
|
||||
)
|
||||
|
||||
// Icons mapped in SM4TIK
|
||||
const (
|
||||
IconAC rune = 0xE000 + iota
|
||||
IconACAlt
|
||||
IconArch
|
||||
IconArchAlt
|
||||
IconBatEmpty
|
||||
IconBatEmptyAlt
|
||||
IconBatFull
|
||||
IconBatFullAlt
|
||||
IconBatLow
|
||||
IconBatLowAlt
|
||||
IconBluetooth
|
||||
IconBug
|
||||
IconBugAlt
|
||||
IconCat
|
||||
IconClock
|
||||
IconCPU
|
||||
IconDish
|
||||
IconDiskette
|
||||
IconEmpty
|
||||
IconEyeLeft
|
||||
IconEyeRight
|
||||
IconFox
|
||||
IconStar
|
||||
IconStarAlt
|
||||
IconFull
|
||||
IconForward
|
||||
IconHalf
|
||||
IconInfo
|
||||
IconInfoAlt1
|
||||
IconinfoAlt2
|
||||
IconMail
|
||||
IconMemory
|
||||
IconMouse
|
||||
IconNetDown
|
||||
IconNetDownAlt1
|
||||
IconNetDownAlt2
|
||||
IconNetUp
|
||||
IconNetUpAlt1
|
||||
IconNetUpAlt2
|
||||
IconNetWired
|
||||
IconNext
|
||||
IconNote
|
||||
IconPacman
|
||||
IconPause
|
||||
IconPhones
|
||||
IconPlay
|
||||
IconPlug
|
||||
IconPrev
|
||||
IconRewind
|
||||
IconScorpion
|
||||
IconShroom
|
||||
IconSpeaker
|
||||
IconSpeakerAlt1
|
||||
IconSpeakerAlt2
|
||||
IconStop
|
||||
IconTemp
|
||||
IconTest
|
||||
IconUSB
|
||||
IconUSBAlt
|
||||
IconWiFi
|
||||
IconWifiAlt
|
||||
)
|
||||
|
||||
type bdfFont struct {
|
||||
font *bdf.Font
|
||||
face *bdf.Face
|
||||
attr FontAttr
|
||||
}
|
||||
|
||||
func (f *bdfFont) MeasureString(s string) (width, height int) {
|
||||
metrics := f.face.Metrics()
|
||||
if c, ok := f.font.CharMap[f.font.DefaultChar]; ok {
|
||||
width = len(s) * c.Advance[0]
|
||||
} else {
|
||||
width = len(s) * f.font.Characters[0].Advance[0]
|
||||
}
|
||||
height = (metrics.Ascent + metrics.Descent).Ceil()
|
||||
return
|
||||
}
|
||||
|
||||
func (f *bdfFont) Draw(dst draw.Image, p image.Point, fg image.Image, text string) {
|
||||
var (
|
||||
metrics = f.face.Metrics()
|
||||
dot = fixed.P(p.X, metrics.Ascent.Ceil()+p.Y)
|
||||
prevC rune = -1
|
||||
)
|
||||
|
||||
for _, c := range text {
|
||||
if prevC >= 0 {
|
||||
dot.X += f.face.Kern(prevC, c)
|
||||
}
|
||||
dr, mask, maskp, advance, _ := f.face.Glyph(dot, c)
|
||||
if !dr.Empty() {
|
||||
draw.DrawMask(dst, dr, fg, image.Point{}, mask, maskp, draw.Over)
|
||||
}
|
||||
dot.X += advance
|
||||
prevC = c
|
||||
}
|
||||
}
|
||||
|
||||
func (f *bdfFont) SetDPI(_ float64) Font { return f }
|
||||
func (f *bdfFont) WithDPI(_ float64) Font { return f }
|
||||
func (f *bdfFont) SetPointSize(_ float64) Font { return f }
|
||||
func (f *bdfFont) WithPointSize(_ float64) Font { return f }
|
||||
func (f *bdfFont) SetHinting(_ Hinting) Font { return f }
|
||||
@@ -0,0 +1,150 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
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
|
||||
}
|
||||
}
|
||||
}
|
||||
+10412
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
+44522
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user