From b2c45a369af146fbd10ab3f6f58953ec83a42cc8 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 15 Nov 2023 21:52:25 +0900 Subject: [PATCH] text/v2: remove the suffix 'InPixels' Now all the units are pixels, the suffix is redundant. Updates #2454 --- examples/2048/2048/tile.go | 10 +++++----- examples/keyboard/main.go | 2 +- examples/text/main.go | 10 +++++----- examples/texti18n/main.go | 30 +++++++++++++++--------------- examples/textinput/main.go | 2 +- text/v2/glyph.go | 4 ++-- text/v2/gotext.go | 29 +++++++++++++++++------------ text/v2/gotextfacesource.go | 20 ++++++++++---------- text/v2/layout.go | 16 ++++++++-------- text/v2/text.go | 6 +++--- 10 files changed, 67 insertions(+), 62 deletions(-) diff --git a/examples/2048/2048/tile.go b/examples/2048/2048/tile.go index 24b4e5a75..fe44e8f94 100644 --- a/examples/2048/2048/tile.go +++ b/examples/2048/2048/tile.go @@ -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) } diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index aaccd0f65..187b7f231 100644 --- a/examples/keyboard/main.go +++ b/examples/keyboard/main.go @@ -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) } diff --git a/examples/text/main.go b/examples/text/main.go index e52f47cd6..7053912c9 100644 --- a/examples/text/main.go +++ b/examples/text/main.go @@ -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) diff --git a/examples/texti18n/main.go b/examples/texti18n/main.go index 31119790f..b08c5ca0a 100644 --- a/examples/texti18n/main.go +++ b/examples/texti18n/main.go @@ -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) } diff --git a/examples/textinput/main.go b/examples/textinput/main.go index 05ad0e701..5d174216d 100644 --- a/examples/textinput/main.go +++ b/examples/textinput/main.go @@ -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) } diff --git a/text/v2/glyph.go b/text/v2/glyph.go index 2e77a74c4..611da7592 100644 --- a/text/v2/glyph.go +++ b/text/v2/glyph.go @@ -40,8 +40,8 @@ func init() { } type faceCacheKey struct { - id uint64 - goTextFaceSizeInPixels float64 + id uint64 + goTextFaceSize float64 } type glyphImageCacheKey struct { diff --git a/text/v2/gotext.go b/text/v2/gotext.go index 8d3a415c9..594aedc5c 100644 --- a/text/v2/gotext.go +++ b/text/v2/gotext.go @@ -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(), } } diff --git a/text/v2/gotextfacesource.go b/text/v2/gotextfacesource.go index 1a0e78e01..58051eb9f 100644 --- a/text/v2/gotextfacesource.go +++ b/text/v2/gotextfacesource.go @@ -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()) } diff --git a/text/v2/layout.go b/text/v2/layout.go index 9e7dcb8b2..eda6cfac8 100644 --- a/text/v2/layout.go +++ b/text/v2/layout.go @@ -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 } } diff --git a/text/v2/text.go b/text/v2/text.go index d53b900b0..f840022e0 100644 --- a/text/v2/text.go +++ b/text/v2/text.go @@ -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 }