text/v2: remove the suffix 'InPixels'

Now all the units are pixels, the suffix is redundant.

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-15 21:52:25 +09:00
parent a612b3c861
commit b2c45a369a
10 changed files with 67 additions and 62 deletions

View File

@ -358,12 +358,12 @@ func (t *Tile) Draw(boardImage *ebiten.Image) {
boardImage.DrawImage(tileImage, op) boardImage.DrawImage(tileImage, op)
str := strconv.Itoa(v) str := strconv.Itoa(v)
sizeInPixels := 48.0 size := 48.0
switch { switch {
case 3 < len(str): case 3 < len(str):
sizeInPixels = 24 size = 24
case 2 < len(str): case 2 < len(str):
sizeInPixels = 32 size = 32
} }
textOp := &text.DrawOptions{} textOp := &text.DrawOptions{}
@ -372,7 +372,7 @@ func (t *Tile) Draw(boardImage *ebiten.Image) {
textOp.PrimaryAlign = text.AlignCenter textOp.PrimaryAlign = text.AlignCenter
textOp.SecondaryAlign = text.AlignCenter textOp.SecondaryAlign = text.AlignCenter
text.Draw(boardImage, str, &text.GoTextFace{ text.Draw(boardImage, str, &text.GoTextFace{
Source: mplusFaceSource, Source: mplusFaceSource,
SizeInPixels: sizeInPixels, Size: size,
}, textOp) }, textOp)
} }

View File

@ -93,7 +93,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Use bitmapfont.Face instead of ebitenutil.DebugPrint, since some key names might not be printed with DebugPrint. // Use bitmapfont.Face instead of ebitenutil.DebugPrint, since some key names might not be printed with DebugPrint.
textOp := &text.DrawOptions{} textOp := &text.DrawOptions{}
textOp.LineHeightInPixels = fontFace.Metrics().Height textOp.LineHeight = fontFace.Metrics().Height
text.Draw(screen, strings.Join(keyStrs, ", ")+"\n"+strings.Join(keyNames, ", "), fontFace, textOp) text.Draw(screen, strings.Join(keyStrs, ", ")+"\n"+strings.Join(keyNames, ", "), fontFace, textOp)
} }

View File

@ -81,7 +81,7 @@ func (g *Game) Update() error {
// Initialize the glyphs for special (colorful) rendering. // Initialize the glyphs for special (colorful) rendering.
if len(g.glyphs) == 0 { if len(g.glyphs) == 0 {
op := &text.LayoutOptions{} op := &text.LayoutOptions{}
op.LineHeightInPixels = mplusNormalFace.Metrics().Height op.LineHeight = mplusNormalFace.Metrics().Height
g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op) g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op)
} }
return nil return nil
@ -100,7 +100,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false) vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
op.LineHeightInPixels = mplusNormalFace.Metrics().Height op.LineHeight = mplusNormalFace.Metrics().Height
text.Draw(screen, sampleText, mplusNormalFace, op) text.Draw(screen, sampleText, mplusNormalFace, op)
} }
{ {
@ -109,7 +109,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false) vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
op.LineHeightInPixels = mplusBigFace.Metrics().Height op.LineHeight = mplusBigFace.Metrics().Height
text.Draw(screen, sampleText, mplusBigFace, op) text.Draw(screen, sampleText, mplusBigFace, op)
} }
{ {
@ -118,7 +118,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Rotate(math.Pi / 4) op.GeoM.Rotate(math.Pi / 4)
op.GeoM.Translate(x, y) op.GeoM.Translate(x, y)
op.Filter = ebiten.FilterLinear op.Filter = ebiten.FilterLinear
op.LineHeightInPixels = mplusNormalFace.Metrics().Height op.LineHeight = mplusNormalFace.Metrics().Height
text.Draw(screen, sampleText, mplusNormalFace, op) text.Draw(screen, sampleText, mplusNormalFace, op)
} }
{ {
@ -130,7 +130,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Add the width as the text rendering region's upper-right position comes to (0, 0) // Add the width as the text rendering region's upper-right position comes to (0, 0)
// when the horizontal alignment is right. The alignment is specified later (PrimaryAlign). // when the horizontal alignment is right. The alignment is specified later (PrimaryAlign).
op.GeoM.Translate(x+w, y) op.GeoM.Translate(x+w, y)
op.LineHeightInPixels = lineHeight op.LineHeight = lineHeight
// The primary alignment for the left-to-right direction is a horizontal alignment, and the end means the right. // The primary alignment for the left-to-right direction is a horizontal alignment, and the end means the right.
op.PrimaryAlign = text.AlignEnd op.PrimaryAlign = text.AlignEnd
text.Draw(screen, sampleText, mplusBigFace, op) text.Draw(screen, sampleText, mplusBigFace, op)

View File

@ -96,10 +96,10 @@ func (g *Game) Draw(screen *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(screenWidth-20, 40) op.GeoM.Translate(screenWidth-20, 40)
text.Draw(screen, arabicText, &text.GoTextFace{ text.Draw(screen, arabicText, &text.GoTextFace{
Source: arabicFaceSource, Source: arabicFaceSource,
Direction: text.DirectionRightToLeft, Direction: text.DirectionRightToLeft,
SizeInPixels: 24, Size: 24,
Language: language.Arabic, Language: language.Arabic,
}, op) }, op)
const hindiText = "चूंकि मानव परिवार के सभी सदस्यों के जन्मजात गौरव और समान" const hindiText = "चूंकि मानव परिवार के सभी सदस्यों के जन्मजात गौरव और समान"
@ -107,9 +107,9 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(20, 110) op.GeoM.Translate(20, 110)
text.Draw(screen, hindiText, &text.GoTextFace{ text.Draw(screen, hindiText, &text.GoTextFace{
Source: devanagariFaceSource, Source: devanagariFaceSource,
SizeInPixels: 24, Size: 24,
Language: language.Hindi, Language: language.Hindi,
}, op) }, op)
const thaiText = "โดยที่การไม่นำพาและการหมิ่นในคุณค่าของสิทธิมนุษยชน" const thaiText = "โดยที่การไม่นำพาและการหมิ่นในคุณค่าของสิทธิมนุษยชน"
@ -117,21 +117,21 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(20, 160) op.GeoM.Translate(20, 160)
text.Draw(screen, thaiText, &text.GoTextFace{ text.Draw(screen, thaiText, &text.GoTextFace{
Source: thaiFaceSource, Source: thaiFaceSource,
SizeInPixels: 24, Size: 24,
Language: language.Thai, Language: language.Thai,
}, op) }, op)
const japaneseText = "あのイーハトーヴォの\nすきとおった風、\n夏でも底に冷たさを\nもつ青いそら…" const japaneseText = "あのイーハトーヴォの\nすきとおった風、\n夏でも底に冷たさを\nもつ青いそら…"
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(screenWidth-20, 210) op.GeoM.Translate(screenWidth-20, 210)
op.LineHeightInPixels = 48 op.LineHeight = 48
text.Draw(screen, japaneseText, &text.GoTextFace{ text.Draw(screen, japaneseText, &text.GoTextFace{
Source: japaneseFaceSource, Source: japaneseFaceSource,
Direction: text.DirectionTopToBottomAndRightToLeft, Direction: text.DirectionTopToBottomAndRightToLeft,
SizeInPixels: 24, Size: 24,
Language: language.Japanese, Language: language.Japanese,
}, op) }, op)
} }

View File

@ -288,7 +288,7 @@ func (t *TextField) Draw(screen *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(float64(tx), float64(ty)) op.GeoM.Translate(float64(tx), float64(ty))
op.ColorScale.ScaleWithColor(color.Black) op.ColorScale.ScaleWithColor(color.Black)
op.LineHeightInPixels = fontFace.Metrics().Height op.LineHeight = fontFace.Metrics().Height
text.Draw(screen, shownText, fontFace, op) text.Draw(screen, shownText, fontFace, op)
} }

View File

@ -40,8 +40,8 @@ func init() {
} }
type faceCacheKey struct { type faceCacheKey struct {
id uint64 id uint64
goTextFaceSizeInPixels float64 goTextFaceSize float64
} }
type glyphImageCacheKey struct { type glyphImageCacheKey struct {

View File

@ -37,10 +37,15 @@ var _ Face = (*GoTextFace)(nil)
// With a GoTextFace, shaping.HarfBuzzShaper is always used as a shaper internally. // With a GoTextFace, shaping.HarfBuzzShaper is always used as a shaper internally.
// GoTextFace includes the source and various options. // GoTextFace includes the source and various options.
type GoTextFace struct { type GoTextFace struct {
// Source is the font face source.
Source *GoTextFaceSource Source *GoTextFaceSource
Direction Direction // Direction is the rendering direction.
SizeInPixels float64 // The default (zero) value is left-to-right horizontal.
Direction Direction
// Size is the font size in pixels.
Size float64
// Language is a hiint for a language (BCP 47). // Language is a hiint for a language (BCP 47).
Language language.Tag Language language.Tag
@ -179,7 +184,7 @@ func MustParseTag(str string) Tag {
// Metrics implements Face. // Metrics implements Face.
func (g *GoTextFace) Metrics() Metrics { func (g *GoTextFace) Metrics() Metrics {
scale := g.Source.scale(g.SizeInPixels) scale := g.Source.scale(g.Size)
var m Metrics var m Metrics
if h, ok := g.Source.f.FontHExtents(); ok { if h, ok := g.Source.f.FontHExtents(); ok {
@ -236,20 +241,20 @@ func (g *GoTextFace) ensureFeaturesString() string {
// faceCacheKey implements Face. // faceCacheKey implements Face.
func (g *GoTextFace) faceCacheKey() faceCacheKey { func (g *GoTextFace) faceCacheKey() faceCacheKey {
return faceCacheKey{ return faceCacheKey{
id: g.Source.id, id: g.Source.id,
goTextFaceSizeInPixels: g.SizeInPixels, goTextFaceSize: g.Size,
} }
} }
func (g *GoTextFace) outputCacheKey(text string) goTextOutputCacheKey { func (g *GoTextFace) outputCacheKey(text string) goTextOutputCacheKey {
return goTextOutputCacheKey{ return goTextOutputCacheKey{
text: text, text: text,
direction: g.Direction, direction: g.Direction,
sizeInPixels: g.SizeInPixels, size: g.Size,
language: g.Language.String(), language: g.Language.String(),
script: g.Script.String(), script: g.Script.String(),
variations: g.ensureVariationsString(), variations: g.ensureVariationsString(),
features: g.ensureFeaturesString(), features: g.ensureFeaturesString(),
} }
} }

View File

@ -27,13 +27,13 @@ import (
) )
type goTextOutputCacheKey struct { type goTextOutputCacheKey struct {
text string text string
direction Direction direction Direction
sizeInPixels float64 size float64
language string language string
script string script string
variations string variations string
features string features string
} }
type glyph struct { type glyph struct {
@ -147,7 +147,7 @@ func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output,
Direction: face.diDirection(), Direction: face.diDirection(),
Face: face.Source.f, Face: face.Source.f,
FontFeatures: face.features, FontFeatures: face.features,
Size: float64ToFixed26_6(face.SizeInPixels), Size: float64ToFixed26_6(face.Size),
Script: face.gScript(), Script: face.gScript(),
Language: language.Language(face.Language.String()), Language: language.Language(face.Language.String()),
} }
@ -215,6 +215,6 @@ func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output,
return out, gs return out, gs
} }
func (g *GoTextFaceSource) scale(sizeInPixels float64) float64 { func (g *GoTextFaceSource) scale(size float64) float64 {
return sizeInPixels / float64(g.f.Upem()) return size / float64(g.f.Upem())
} }

View File

@ -45,8 +45,8 @@ type DrawOptions struct {
// PrimaryAlign and SecondaryAlign determine where to put the text in the given region at Draw. // PrimaryAlign and SecondaryAlign determine where to put the text in the given region at Draw.
// Draw might render the text outside of the specified image bounds, so you might have to specify GeoM to make the text visible. // Draw might render the text outside of the specified image bounds, so you might have to specify GeoM to make the text visible.
type LayoutOptions struct { type LayoutOptions struct {
// LineHeightInPixels is a line height in pixels. // LineHeight is a line height in pixels.
LineHeightInPixels float64 LineHeight float64
// PrimaryAlign is an alignment of the primary direction, in which a text in one line is rendered. // PrimaryAlign is an alignment of the primary direction, in which a text in one line is rendered.
// The primary direction is the horizontal direction for a horizontal-direction face, // The primary direction is the horizontal direction for a horizontal-direction face,
@ -162,9 +162,9 @@ func appendGlyphs(glyphs []Glyph, text string, face Face, x, y float64, options
var boundaryWidth, boundaryHeight float64 var boundaryWidth, boundaryHeight float64
if d.isHorizontal() { if d.isHorizontal() {
boundaryWidth = longestAdvance boundaryWidth = longestAdvance
boundaryHeight = float64(lineCount-1)*options.LineHeightInPixels + m.HAscent + m.HDescent boundaryHeight = float64(lineCount-1)*options.LineHeight + m.HAscent + m.HDescent
} else { } else {
boundaryWidth = float64(lineCount-1)*options.LineHeightInPixels + m.VAscent + m.VDescent boundaryWidth = float64(lineCount-1)*options.LineHeight + m.VAscent + m.VDescent
boundaryHeight = longestAdvance boundaryHeight = longestAdvance
} }
@ -244,13 +244,13 @@ func appendGlyphs(glyphs []Glyph, text string, face Face, x, y float64, options
// Advance the origin position in the secondary direction. // Advance the origin position in the secondary direction.
switch face.direction() { switch face.direction() {
case DirectionLeftToRight: case DirectionLeftToRight:
originY += options.LineHeightInPixels originY += options.LineHeight
case DirectionRightToLeft: case DirectionRightToLeft:
originY += options.LineHeightInPixels originY += options.LineHeight
case DirectionTopToBottomAndLeftToRight: case DirectionTopToBottomAndLeftToRight:
originX += options.LineHeightInPixels originX += options.LineHeight
case DirectionTopToBottomAndRightToLeft: case DirectionTopToBottomAndRightToLeft:
originX -= options.LineHeightInPixels originX -= options.LineHeight
} }
} }

View File

@ -175,7 +175,7 @@ func (d Direction) isHorizontal() bool {
// With a vertical direction face, the width and the height are calculated in an opposite manner. // With a vertical direction face, the width and the height are calculated in an opposite manner.
// //
// Measure is concurrent-safe. // Measure is concurrent-safe.
func Measure(text string, face Face, lineHeightInPixels float64) (width, height float64) { func Measure(text string, face Face, lineHeight float64) (width, height float64) {
if text == "" { if text == "" {
return 0, 0 return 0, 0
} }
@ -198,10 +198,10 @@ func Measure(text string, face Face, lineHeightInPixels float64) (width, height
m := face.Metrics() m := face.Metrics()
if face.direction().isHorizontal() { if face.direction().isHorizontal() {
secondary := float64(lineCount-1)*lineHeightInPixels + m.HAscent + m.HDescent secondary := float64(lineCount-1)*lineHeight + m.HAscent + m.HDescent
return primary, secondary return primary, secondary
} }
secondary := float64(lineCount-1)*lineHeightInPixels + m.VAscent + m.VDescent secondary := float64(lineCount-1)*lineHeight + m.VAscent + m.VDescent
return secondary, primary return secondary, primary
} }