text/v2: refactoring: rename receivers of GoXFace

This commit is contained in:
Hajime Hoshi 2024-10-27 22:58:57 +09:00
parent d19a774316
commit 2748f31fc4

View File

@ -52,32 +52,32 @@ type GoXFace struct {
// NewGoXFace creates a new GoXFace from a semi-standard font.Face. // NewGoXFace creates a new GoXFace from a semi-standard font.Face.
func NewGoXFace(face font.Face) *GoXFace { func NewGoXFace(face font.Face) *GoXFace {
s := &GoXFace{ g := &GoXFace{
f: &faceWithCache{ f: &faceWithCache{
f: face, f: face,
}, },
} }
// Set addr as early as possible. This is necessary for glyphVariationCount. // Set addr as early as possible. This is necessary for glyphVariationCount.
s.addr = s g.addr = g
s.glyphImageCache = newCache[goXFaceGlyphImageCacheKey, *ebiten.Image](128 * glyphVariationCount(s)) g.glyphImageCache = newCache[goXFaceGlyphImageCacheKey, *ebiten.Image](128 * glyphVariationCount(g))
return s return g
} }
func (s *GoXFace) copyCheck() { func (g *GoXFace) copyCheck() {
if s.addr != s { if g.addr != g {
panic("text: illegal use of non-zero GoXFace copied by value") panic("text: illegal use of non-zero GoXFace copied by value")
} }
} }
// Metrics implements Face. // Metrics implements Face.
func (s *GoXFace) Metrics() Metrics { func (g *GoXFace) Metrics() Metrics {
s.copyCheck() g.copyCheck()
if s.cachedMetrics != (Metrics{}) { if g.cachedMetrics != (Metrics{}) {
return s.cachedMetrics return g.cachedMetrics
} }
fm := s.f.Metrics() fm := g.f.Metrics()
m := Metrics{ m := Metrics{
HLineGap: fixed26_6ToFloat64(fm.Height - fm.Ascent - fm.Descent), HLineGap: fixed26_6ToFloat64(fm.Height - fm.Ascent - fm.Descent),
HAscent: fixed26_6ToFloat64(fm.Ascent), HAscent: fixed26_6ToFloat64(fm.Ascent),
@ -93,7 +93,7 @@ func (s *GoXFace) Metrics() Metrics {
if fm.CapHeight < 0 { if fm.CapHeight < 0 {
m.CapHeight *= -1 m.CapHeight *= -1
} }
s.cachedMetrics = m g.cachedMetrics = m
return m return m
} }
@ -102,25 +102,25 @@ func (s *GoXFace) Metrics() Metrics {
// UnsafeInternal is unsafe since this might make internal cache states out of sync. // UnsafeInternal is unsafe since this might make internal cache states out of sync.
// //
// UnsafeInternal might have breaking changes even in the same major version. // UnsafeInternal might have breaking changes even in the same major version.
func (s *GoXFace) UnsafeInternal() font.Face { func (g *GoXFace) UnsafeInternal() font.Face {
s.copyCheck() g.copyCheck()
return s.f.f return g.f.f
} }
// advance implements Face. // advance implements Face.
func (s *GoXFace) advance(text string) float64 { func (g *GoXFace) advance(text string) float64 {
return fixed26_6ToFloat64(font.MeasureString(s.f, text)) return fixed26_6ToFloat64(font.MeasureString(g.f, text))
} }
// hasGlyph implements Face. // hasGlyph implements Face.
func (s *GoXFace) hasGlyph(r rune) bool { func (g *GoXFace) hasGlyph(r rune) bool {
_, ok := s.f.GlyphAdvance(r) _, ok := g.f.GlyphAdvance(r)
return ok return ok
} }
// appendGlyphsForLine implements Face. // appendGlyphsForLine implements Face.
func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset int, originX, originY float64) []Glyph { func (g *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset int, originX, originY float64) []Glyph {
s.copyCheck() g.copyCheck()
origin := fixed.Point26_6{ origin := fixed.Point26_6{
X: float64ToFixed26_6(originX), X: float64ToFixed26_6(originX),
@ -130,9 +130,9 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
for i, r := range line { for i, r := range line {
if prevR >= 0 { if prevR >= 0 {
origin.X += s.f.Kern(prevR, r) origin.X += g.f.Kern(prevR, r)
} }
img, imgX, imgY, a := s.glyphImage(r, origin) img, imgX, imgY, a := g.glyphImage(r, origin)
// Adjust the position to the integers. // Adjust the position to the integers.
// The current glyph images assume that they are rendered on integer positions so far. // The current glyph images assume that they are rendered on integer positions so far.
@ -158,12 +158,12 @@ func (s *GoXFace) appendGlyphsForLine(glyphs []Glyph, line string, indexOffset i
return glyphs return glyphs
} }
func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int, int, fixed.Int26_6) { func (g *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int, int, fixed.Int26_6) {
// Assume that GoXFace's direction is always horizontal. // Assume that GoXFace's direction is always horizontal.
origin.X = adjustGranularity(origin.X, s) origin.X = adjustGranularity(origin.X, g)
origin.Y &^= ((1 << 6) - 1) origin.Y &^= ((1 << 6) - 1)
b, a, _ := s.f.GlyphBounds(r) b, a, _ := g.f.GlyphBounds(r)
subpixelOffset := fixed.Point26_6{ subpixelOffset := fixed.Point26_6{
X: (origin.X + b.Min.X) & ((1 << 6) - 1), X: (origin.X + b.Min.X) & ((1 << 6) - 1),
Y: (origin.Y + b.Min.Y) & ((1 << 6) - 1), Y: (origin.Y + b.Min.Y) & ((1 << 6) - 1),
@ -172,8 +172,8 @@ func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int
rune: r, rune: r,
xoffset: subpixelOffset.X, xoffset: subpixelOffset.X,
} }
img := s.glyphImageCache.getOrCreate(key, func() (*ebiten.Image, bool) { img := g.glyphImageCache.getOrCreate(key, func() (*ebiten.Image, bool) {
img := s.glyphImageImpl(r, subpixelOffset, b) img := g.glyphImageImpl(r, subpixelOffset, b)
return img, img != nil return img, img != nil
}) })
imgX := (origin.X + b.Min.X).Floor() imgX := (origin.X + b.Min.X).Floor()
@ -181,7 +181,7 @@ func (s *GoXFace) glyphImage(r rune, origin fixed.Point26_6) (*ebiten.Image, int
return img, imgX, imgY, a return img, imgX, imgY, a
} }
func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image { func (g *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBounds fixed.Rectangle26_6) *ebiten.Image {
w, h := (glyphBounds.Max.X - glyphBounds.Min.X).Ceil(), (glyphBounds.Max.Y - glyphBounds.Min.Y).Ceil() w, h := (glyphBounds.Max.X - glyphBounds.Min.X).Ceil(), (glyphBounds.Max.Y - glyphBounds.Min.Y).Ceil()
if w == 0 || h == 0 { if w == 0 || h == 0 {
return nil return nil
@ -197,7 +197,7 @@ func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBo
d := font.Drawer{ d := font.Drawer{
Dst: rgba, Dst: rgba,
Src: image.White, Src: image.White,
Face: s.f, Face: g.f,
Dot: fixed.Point26_6{ Dot: fixed.Point26_6{
X: -glyphBounds.Min.X + subpixelOffset.X, X: -glyphBounds.Min.X + subpixelOffset.X,
Y: -glyphBounds.Min.Y + subpixelOffset.Y, Y: -glyphBounds.Min.Y + subpixelOffset.Y,
@ -209,14 +209,14 @@ func (s *GoXFace) glyphImageImpl(r rune, subpixelOffset fixed.Point26_6, glyphBo
} }
// direction implements Face. // direction implements Face.
func (s *GoXFace) direction() Direction { func (g *GoXFace) direction() Direction {
return DirectionLeftToRight return DirectionLeftToRight
} }
// appendVectorPathForLine implements Face. // appendVectorPathForLine implements Face.
func (s *GoXFace) appendVectorPathForLine(path *vector.Path, line string, originX, originY float64) { func (g *GoXFace) appendVectorPathForLine(path *vector.Path, line string, originX, originY float64) {
} }
// Metrics implements Face. // Metrics implements Face.
func (s *GoXFace) private() { func (g *GoXFace) private() {
} }