128 lines
2.3 KiB
Go
128 lines
2.3 KiB
Go
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 }
|