2023-11-11 11:04:13 +01:00
|
|
|
// Copyright 2023 The Ebitengine Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package text
|
|
|
|
|
|
|
|
import (
|
|
|
|
"image"
|
2023-11-13 17:16:26 +01:00
|
|
|
"unicode/utf8"
|
2023-11-11 11:04:13 +01:00
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-11-19 18:50:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/vector"
|
2023-11-11 11:04:13 +01:00
|
|
|
)
|
|
|
|
|
2024-03-19 18:38:22 +01:00
|
|
|
var _ Face = (*GoXFace)(nil)
|
2023-11-11 11:04:13 +01:00
|
|
|
|
2024-03-19 18:38:22 +01:00
|
|
|
type goXFaceGlyphImageCacheKey struct {
|
2023-11-19 15:40:47 +01:00
|
|
|
rune rune
|
|
|
|
xoffset fixed.Int26_6
|
|
|
|
|
|
|
|
// yoffset is always the same if the rune is the same, so this doesn't have to be a key.
|
|
|
|
}
|
|
|
|
|
2024-03-19 18:38:22 +01:00
|
|
|
// GoXFace is a Face implementation for a semi-standard font.Face (golang.org/x/image/font).
|
|
|
|
// GoXFace is useful to transit from existing codebase with text v1, or to use some bitmap fonts defined as font.Face.
|
|
|
|
// GoXFace must not be copied by value.
|
2024-04-07 12:22:37 +02:00
|
|
|
//
|
2024-10-26 22:00:01 +02:00
|
|
|
// Unlike GoTextFace, one GoXFace instance has its own glyph image cache.
|
2024-04-07 12:22:37 +02:00
|
|
|
// You should reuse the same GoXFace instance as much as possible.
|
2024-03-19 18:38:22 +01:00
|
|
|
type GoXFace struct {
|
2023-11-11 11:04:13 +01:00
|
|
|
f *faceWithCache
|
|
|
|
|
2024-10-27 13:44:17 +01:00
|
|
|
glyphImageCache *cache[goXFaceGlyphImageCacheKey, *ebiten.Image]
|
2023-11-12 10:46:22 +01:00
|
|
|
|
2024-09-09 16:51:13 +02:00
|
|
|
cachedMetrics Metrics
|
|
|
|
|
2024-10-28 16:47:47 +01:00
|
|
|
originXCache *cache[string, []fixed.Int26_6]
|
|
|
|
|
2024-03-19 18:38:22 +01:00
|
|
|
addr *GoXFace
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2024-03-19 18:38:22 +01:00
|
|
|
// NewGoXFace creates a new GoXFace from a semi-standard font.Face.
|
|
|
|
func NewGoXFace(face font.Face) *GoXFace {
|
2024-10-27 14:58:57 +01:00
|
|
|
g := &GoXFace{
|
2023-11-11 11:04:13 +01:00
|
|
|
f: &faceWithCache{
|
|
|
|
f: face,
|
|
|
|
},
|
|
|
|
}
|
2024-10-27 13:44:17 +01:00
|
|
|
// Set addr as early as possible. This is necessary for glyphVariationCount.
|
2024-10-27 14:58:57 +01:00
|
|
|
g.addr = g
|
|
|
|
g.glyphImageCache = newCache[goXFaceGlyphImageCacheKey, *ebiten.Image](128 * glyphVariationCount(g))
|
2024-10-28 16:47:47 +01:00
|
|
|
g.originXCache = newCache[string, []fixed.Int26_6](512)
|
2024-10-27 14:58:57 +01:00
|
|
|
return g
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) copyCheck() {
|
|
|
|
if g.addr != g {
|
2024-03-19 18:38:22 +01:00
|
|
|
panic("text: illegal use of non-zero GoXFace copied by value")
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// Metrics implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) Metrics() Metrics {
|
|
|
|
g.copyCheck()
|
2023-11-11 11:04:13 +01:00
|
|
|
|
2024-10-27 14:58:57 +01:00
|
|
|
if g.cachedMetrics != (Metrics{}) {
|
|
|
|
return g.cachedMetrics
|
2024-09-09 16:51:13 +02:00
|
|
|
}
|
|
|
|
|
2024-10-27 14:58:57 +01:00
|
|
|
fm := g.f.Metrics()
|
2024-09-09 16:51:13 +02:00
|
|
|
m := Metrics{
|
|
|
|
HLineGap: fixed26_6ToFloat64(fm.Height - fm.Ascent - fm.Descent),
|
|
|
|
HAscent: fixed26_6ToFloat64(fm.Ascent),
|
|
|
|
HDescent: fixed26_6ToFloat64(fm.Descent),
|
|
|
|
XHeight: fixed26_6ToFloat64(fm.XHeight),
|
|
|
|
CapHeight: fixed26_6ToFloat64(fm.CapHeight),
|
|
|
|
}
|
|
|
|
|
2024-09-10 17:22:17 +02:00
|
|
|
// There is an issue that XHeight and CapHeight are negative for some old fonts (golang/go#69378).
|
2024-09-09 16:51:13 +02:00
|
|
|
if fm.XHeight < 0 {
|
|
|
|
m.XHeight *= -1
|
|
|
|
}
|
|
|
|
if fm.CapHeight < 0 {
|
|
|
|
m.CapHeight *= -1
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
2024-10-27 14:58:57 +01:00
|
|
|
g.cachedMetrics = m
|
2024-09-09 16:51:13 +02:00
|
|
|
return m
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 14:55:53 +01:00
|
|
|
// UnsafeInternal returns its internal font.Face.
|
|
|
|
//
|
2024-09-09 16:07:30 +02:00
|
|
|
// UnsafeInternal is unsafe since this might make internal cache states out of sync.
|
|
|
|
//
|
|
|
|
// UnsafeInternal might have breaking changes even in the same major version.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) UnsafeInternal() font.Face {
|
|
|
|
g.copyCheck()
|
|
|
|
return g.f.f
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// advance implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) advance(text string) float64 {
|
2024-10-28 16:47:47 +01:00
|
|
|
xs := g.originXs(text)
|
|
|
|
if len(xs) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return fixed26_6ToFloat64(xs[len(xs)-1])
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *GoXFace) originXs(text string) []fixed.Int26_6 {
|
|
|
|
return g.originXCache.getOrCreate(text, func() ([]fixed.Int26_6, bool) {
|
|
|
|
if len(text) == 0 {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
var originXs []fixed.Int26_6
|
|
|
|
prevR := rune(-1)
|
|
|
|
var originX fixed.Int26_6
|
|
|
|
for _, r := range text {
|
|
|
|
if prevR >= 0 {
|
|
|
|
originX += g.f.Kern(prevR, r)
|
|
|
|
originXs = append(originXs, originX)
|
|
|
|
}
|
|
|
|
a, _ := g.f.GlyphAdvance(r)
|
|
|
|
originX += a
|
|
|
|
prevR = r
|
|
|
|
}
|
|
|
|
originXs = append(originXs, originX)
|
|
|
|
return originXs, true
|
|
|
|
})
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2023-11-25 18:36:08 +01:00
|
|
|
// hasGlyph implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) hasGlyph(r rune) bool {
|
|
|
|
_, ok := g.f.GlyphAdvance(r)
|
2023-11-25 18:36:08 +01:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2023-12-05 11:44:32 +01:00
|
|
|
// appendGlyphsForLine implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset int, originX, originY float64) []Glyph {
|
|
|
|
g.copyCheck()
|
2023-11-11 11:04:13 +01:00
|
|
|
|
2023-11-12 17:31:25 +01:00
|
|
|
origin := fixed.Point26_6{
|
|
|
|
X: float64ToFixed26_6(originX),
|
|
|
|
Y: float64ToFixed26_6(originY),
|
|
|
|
}
|
2024-10-28 16:47:47 +01:00
|
|
|
ox := origin.X
|
2023-11-11 11:04:13 +01:00
|
|
|
|
2024-10-28 16:47:47 +01:00
|
|
|
originXs := g.originXs(line)
|
|
|
|
var advanceIndex int
|
2023-11-19 18:50:27 +01:00
|
|
|
for i, r := range line {
|
2024-10-28 16:47:47 +01:00
|
|
|
if i > 0 {
|
|
|
|
origin.X = ox + originXs[advanceIndex]
|
|
|
|
advanceIndex++
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
2024-10-28 16:15:41 +01:00
|
|
|
|
|
|
|
// imgX and imgY are integers so that the nearest filter can be used.
|
2024-10-28 16:47:47 +01:00
|
|
|
img, imgX, imgY := g.glyphImage(r, origin)
|
2023-12-05 12:01:25 +01:00
|
|
|
|
|
|
|
// Adjust the position to the integers.
|
|
|
|
// The current glyph images assume that they are rendered on integer positions so far.
|
|
|
|
_, size := utf8.DecodeRuneInString(line[i:])
|
|
|
|
|
|
|
|
// Append a glyph even if img is nil.
|
|
|
|
// This is necessary to return index information for control characters.
|
|
|
|
glyphs = append(glyphs, Glyph{
|
|
|
|
StartIndexInBytes: indexOffset + i,
|
|
|
|
EndIndexInBytes: indexOffset + i + size,
|
|
|
|
Image: img,
|
|
|
|
X: float64(imgX),
|
|
|
|
Y: float64(imgY),
|
2024-08-22 16:36:20 +02:00
|
|
|
OriginX: fixed26_6ToFloat64(origin.X),
|
|
|
|
OriginY: fixed26_6ToFloat64(origin.Y),
|
2024-08-22 20:54:11 +02:00
|
|
|
OriginOffsetX: 0,
|
|
|
|
OriginOffsetY: 0,
|
2023-12-05 12:01:25 +01:00
|
|
|
})
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-05 11:44:32 +01:00
|
|
|
return glyphs
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 16:47:47 +01:00
|
|
|
func (g *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int, int) {
|
2024-03-19 18:38:22 +01:00
|
|
|
// Assume that GoXFace's direction is always horizontal.
|
2024-10-27 14:58:57 +01:00
|
|
|
origin.X = adjustGranularity(origin.X, g)
|
2023-11-13 05:04:55 +01:00
|
|
|
origin.Y &^= ((1 << 6) - 1)
|
|
|
|
|
2024-10-28 16:47:47 +01:00
|
|
|
b, _, _ := g.f.GlyphBounds(r)
|
2023-11-12 18:10:57 +01:00
|
|
|
subpixelOffset := fixed.Point26_6{
|
2023-11-13 05:04:55 +01:00
|
|
|
X: (origin.X + b.Min.X) & ((1 << 6) - 1),
|
|
|
|
Y: (origin.Y + b.Min.Y) & ((1 << 6) - 1),
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
2024-03-19 18:38:22 +01:00
|
|
|
key := goXFaceGlyphImageCacheKey{
|
2023-11-19 15:40:47 +01:00
|
|
|
rune: r,
|
2023-11-12 18:10:57 +01:00
|
|
|
xoffset: subpixelOffset.X,
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
2024-10-27 14:58:57 +01:00
|
|
|
img := g.glyphImageCache.getOrCreate(key, func() (*ebiten.Image, bool) {
|
|
|
|
img := g.glyphImageImpl(r, subpixelOffset, b)
|
2024-10-27 13:44:17 +01:00
|
|
|
return img, img != nil
|
2023-11-11 11:04:13 +01:00
|
|
|
})
|
2023-11-13 05:04:55 +01:00
|
|
|
imgX := (origin.X + b.Min.X).Floor()
|
|
|
|
imgY := (origin.Y + b.Min.Y).Floor()
|
2024-10-28 16:47:47 +01:00
|
|
|
return img, imgX, imgY
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
|
2023-11-12 18:10:57 +01:00
|
|
|
w, h := (glyphBounds.Max.X - glyphBounds.Min.X).Ceil(), (glyphBounds.Max.Y - glyphBounds.Min.Y).Ceil()
|
2023-11-11 11:04:13 +01:00
|
|
|
if w == 0 || h == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-24 17:24:52 +01:00
|
|
|
// Add always 1 to the size.
|
|
|
|
// In theory, it is possible to determine whether +1 is necessary or not, but the calculation is pretty complicated.
|
|
|
|
w++
|
|
|
|
h++
|
|
|
|
|
2023-11-11 11:04:13 +01:00
|
|
|
rgba := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
|
|
|
|
|
|
d := font.Drawer{
|
|
|
|
Dst: rgba,
|
|
|
|
Src: image.White,
|
2024-10-27 14:58:57 +01:00
|
|
|
Face: g.f,
|
2023-11-12 18:10:57 +01:00
|
|
|
Dot: fixed.Point26_6{
|
|
|
|
X: -glyphBounds.Min.X + subpixelOffset.X,
|
|
|
|
Y: -glyphBounds.Min.Y + subpixelOffset.Y,
|
|
|
|
},
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|
|
|
|
d.DrawString(string(r))
|
|
|
|
|
|
|
|
return ebiten.NewImageFromImage(rgba)
|
|
|
|
}
|
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// direction implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) direction() Direction {
|
2023-11-11 11:04:13 +01:00
|
|
|
return DirectionLeftToRight
|
|
|
|
}
|
|
|
|
|
2023-11-19 18:50:27 +01:00
|
|
|
// appendVectorPathForLine implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) appendVectorPathForLine(path *vector.Path, line string, originX, originY float64) {
|
2023-11-19 18:50:27 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 14:42:34 +01:00
|
|
|
// Metrics implements Face.
|
2024-10-27 14:58:57 +01:00
|
|
|
func (g *GoXFace) private() {
|
2023-11-11 11:04:13 +01:00
|
|
|
}
|