text: Add FaceWithLineHeight

Closes #1760
This commit is contained in:
Hajime Hoshi 2021-09-12 22:53:41 +09:00
parent 82c41cea39
commit 4509f2aee4
2 changed files with 47 additions and 0 deletions

View File

@ -108,6 +108,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.Filter = ebiten.FilterLinear
text.DrawWithOptions(screen, sampleText, mplusNormalFont, op)
}
{
const x, y = 160, 240
const lineHeight = 80
b := text.BoundString(text.FaceWithLineHeight(mplusBigFont, lineHeight), sampleText)
ebitenutil.DrawRect(screen, float64(b.Min.X+x), float64(b.Min.Y+y), float64(b.Dx()), float64(b.Dy()), gray)
text.Draw(screen, sampleText, text.FaceWithLineHeight(mplusBigFont, lineHeight), x, y, color.White)
}
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {

View File

@ -352,3 +352,43 @@ func CacheGlyphs(face font.Face, text string) {
getGlyphImage(face, r)
}
}
// FaceWithLineHeight returns a font.Face with the given lineHeight in pixels.
// The returned face will otherwise have the same glyphs and metrics as face.
func FaceWithLineHeight(face font.Face, lineHeight float64) font.Face {
return faceWithLineHeight{
face: face,
lineHeight: fixed.Int26_6(lineHeight * (1 << 6)),
}
}
type faceWithLineHeight struct {
face font.Face
lineHeight fixed.Int26_6
}
func (f faceWithLineHeight) Close() error {
return f.face.Close()
}
func (f faceWithLineHeight) Glyph(dot fixed.Point26_6, r rune) (dr image.Rectangle, mask image.Image, maskp image.Point, advance fixed.Int26_6, ok bool) {
return f.face.Glyph(dot, r)
}
func (f faceWithLineHeight) GlyphBounds(r rune) (bounds fixed.Rectangle26_6, advance fixed.Int26_6, ok bool) {
return f.face.GlyphBounds(r)
}
func (f faceWithLineHeight) GlyphAdvance(r rune) (advance fixed.Int26_6, ok bool) {
return f.face.GlyphAdvance(r)
}
func (f faceWithLineHeight) Kern(r0, r1 rune) fixed.Int26_6 {
return f.face.Kern(r0, r1)
}
func (f faceWithLineHeight) Metrics() font.Metrics {
m := f.face.Metrics()
m.Height = f.lineHeight
return m
}