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

View File

@ -81,7 +81,7 @@ func (g *Game) Update() error {
// Initialize the glyphs for special (colorful) rendering.
if len(g.glyphs) == 0 {
op := &text.LayoutOptions{}
op.LineHeightInPixels = mplusNormalFace.Metrics().Height
op.LineHeight = mplusNormalFace.Metrics().Height
g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op)
}
return nil
@ -100,7 +100,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false)
op := &text.DrawOptions{}
op.GeoM.Translate(x, y)
op.LineHeightInPixels = mplusNormalFace.Metrics().Height
op.LineHeight = mplusNormalFace.Metrics().Height
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)
op := &text.DrawOptions{}
op.GeoM.Translate(x, y)
op.LineHeightInPixels = mplusBigFace.Metrics().Height
op.LineHeight = mplusBigFace.Metrics().Height
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.Translate(x, y)
op.Filter = ebiten.FilterLinear
op.LineHeightInPixels = mplusNormalFace.Metrics().Height
op.LineHeight = mplusNormalFace.Metrics().Height
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)
// when the horizontal alignment is right. The alignment is specified later (PrimaryAlign).
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.
op.PrimaryAlign = text.AlignEnd
text.Draw(screen, sampleText, mplusBigFace, op)

View File

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

View File

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

View File

@ -40,8 +40,8 @@ func init() {
}
type faceCacheKey struct {
id uint64
goTextFaceSizeInPixels float64
id uint64
goTextFaceSize float64
}
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.
// GoTextFace includes the source and various options.
type GoTextFace struct {
// Source is the font face source.
Source *GoTextFaceSource
Direction Direction
SizeInPixels float64
// Direction is the rendering direction.
// 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 language.Tag
@ -179,7 +184,7 @@ func MustParseTag(str string) Tag {
// Metrics implements Face.
func (g *GoTextFace) Metrics() Metrics {
scale := g.Source.scale(g.SizeInPixels)
scale := g.Source.scale(g.Size)
var m Metrics
if h, ok := g.Source.f.FontHExtents(); ok {
@ -236,20 +241,20 @@ func (g *GoTextFace) ensureFeaturesString() string {
// faceCacheKey implements Face.
func (g *GoTextFace) faceCacheKey() faceCacheKey {
return faceCacheKey{
id: g.Source.id,
goTextFaceSizeInPixels: g.SizeInPixels,
id: g.Source.id,
goTextFaceSize: g.Size,
}
}
func (g *GoTextFace) outputCacheKey(text string) goTextOutputCacheKey {
return goTextOutputCacheKey{
text: text,
direction: g.Direction,
sizeInPixels: g.SizeInPixels,
language: g.Language.String(),
script: g.Script.String(),
variations: g.ensureVariationsString(),
features: g.ensureFeaturesString(),
text: text,
direction: g.Direction,
size: g.Size,
language: g.Language.String(),
script: g.Script.String(),
variations: g.ensureVariationsString(),
features: g.ensureFeaturesString(),
}
}

View File

@ -27,13 +27,13 @@ import (
)
type goTextOutputCacheKey struct {
text string
direction Direction
sizeInPixels float64
language string
script string
variations string
features string
text string
direction Direction
size float64
language string
script string
variations string
features string
}
type glyph struct {
@ -147,7 +147,7 @@ func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output,
Direction: face.diDirection(),
Face: face.Source.f,
FontFeatures: face.features,
Size: float64ToFixed26_6(face.SizeInPixels),
Size: float64ToFixed26_6(face.Size),
Script: face.gScript(),
Language: language.Language(face.Language.String()),
}
@ -215,6 +215,6 @@ func (g *GoTextFaceSource) shape(text string, face *GoTextFace) (shaping.Output,
return out, gs
}
func (g *GoTextFaceSource) scale(sizeInPixels float64) float64 {
return sizeInPixels / float64(g.f.Upem())
func (g *GoTextFaceSource) scale(size float64) float64 {
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.
// 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 {
// LineHeightInPixels is a line height in pixels.
LineHeightInPixels float64
// LineHeight is a line height in pixels.
LineHeight float64
// 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,
@ -162,9 +162,9 @@ func appendGlyphs(glyphs []Glyph, text string, face Face, x, y float64, options
var boundaryWidth, boundaryHeight float64
if d.isHorizontal() {
boundaryWidth = longestAdvance
boundaryHeight = float64(lineCount-1)*options.LineHeightInPixels + m.HAscent + m.HDescent
boundaryHeight = float64(lineCount-1)*options.LineHeight + m.HAscent + m.HDescent
} else {
boundaryWidth = float64(lineCount-1)*options.LineHeightInPixels + m.VAscent + m.VDescent
boundaryWidth = float64(lineCount-1)*options.LineHeight + m.VAscent + m.VDescent
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.
switch face.direction() {
case DirectionLeftToRight:
originY += options.LineHeightInPixels
originY += options.LineHeight
case DirectionRightToLeft:
originY += options.LineHeightInPixels
originY += options.LineHeight
case DirectionTopToBottomAndLeftToRight:
originX += options.LineHeightInPixels
originX += options.LineHeight
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.
//
// 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 == "" {
return 0, 0
}
@ -198,10 +198,10 @@ func Measure(text string, face Face, lineHeightInPixels float64) (width, height
m := face.Metrics()
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
}
secondary := float64(lineCount-1)*lineHeightInPixels + m.VAscent + m.VDescent
secondary := float64(lineCount-1)*lineHeight + m.VAscent + m.VDescent
return secondary, primary
}