text/v2: rename LineHeight -> LineSpacingInPixels

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-11-17 01:17:29 +09:00
parent e7fa9d50de
commit 147175d400
9 changed files with 34 additions and 34 deletions

View File

@ -48,7 +48,7 @@ func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(float64(x)+1, float64(y)+1) op.GeoM.Translate(float64(x)+1, float64(y)+1)
op.ColorScale.ScaleWithColor(shadowColor) op.ColorScale.ScaleWithColor(shadowColor)
op.LineHeight = arcadeFontBaseSize * float64(scale) op.LineSpacingInPixels = arcadeFontBaseSize * float64(scale)
op.PrimaryAlign = primaryAlign op.PrimaryAlign = primaryAlign
op.SecondaryAlign = secondaryAlign op.SecondaryAlign = secondaryAlign
text.Draw(rt, str, &text.GoTextFace{ text.Draw(rt, str, &text.GoTextFace{

View File

@ -277,7 +277,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, 3*titleFontSize) op.GeoM.Translate(screenWidth/2, 3*titleFontSize)
op.ColorScale.ScaleWithColor(color.White) op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = titleFontSize op.LineSpacingInPixels = titleFontSize
op.PrimaryAlign = text.AlignCenter op.PrimaryAlign = text.AlignCenter
text.Draw(screen, titleTexts, &text.GoTextFace{ text.Draw(screen, titleTexts, &text.GoTextFace{
Source: arcadeFaceSource, Source: arcadeFaceSource,
@ -287,7 +287,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op = &text.DrawOptions{} op = &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, 3*titleFontSize) op.GeoM.Translate(screenWidth/2, 3*titleFontSize)
op.ColorScale.ScaleWithColor(color.White) op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = fontSize op.LineSpacingInPixels = fontSize
op.PrimaryAlign = text.AlignCenter op.PrimaryAlign = text.AlignCenter
text.Draw(screen, texts, &text.GoTextFace{ text.Draw(screen, texts, &text.GoTextFace{
Source: arcadeFaceSource, Source: arcadeFaceSource,
@ -300,7 +300,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(screenWidth/2, screenHeight-smallFontSize/2) op.GeoM.Translate(screenWidth/2, screenHeight-smallFontSize/2)
op.ColorScale.ScaleWithColor(color.White) op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = smallFontSize op.LineSpacingInPixels = smallFontSize
op.PrimaryAlign = text.AlignCenter op.PrimaryAlign = text.AlignCenter
op.SecondaryAlign = text.AlignEnd op.SecondaryAlign = text.AlignEnd
text.Draw(screen, msg, &text.GoTextFace{ text.Draw(screen, msg, &text.GoTextFace{
@ -312,7 +312,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op = &text.DrawOptions{} op = &text.DrawOptions{}
op.GeoM.Translate(screenWidth, 0) op.GeoM.Translate(screenWidth, 0)
op.ColorScale.ScaleWithColor(color.White) op.ColorScale.ScaleWithColor(color.White)
op.LineHeight = fontSize op.LineSpacingInPixels = fontSize
op.PrimaryAlign = text.AlignEnd op.PrimaryAlign = text.AlignEnd
text.Draw(screen, fmt.Sprintf("%04d", g.score()), &text.GoTextFace{ text.Draw(screen, fmt.Sprintf("%04d", g.score()), &text.GoTextFace{
Source: arcadeFaceSource, Source: arcadeFaceSource,

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.LineHeight = fontFace.Metrics().Height textOp.LineSpacingInPixels = 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

@ -68,7 +68,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.LineHeight = mplusNormalFace.Metrics().Height op.LineSpacingInPixels = 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
@ -83,7 +83,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.LineHeight = mplusNormalFace.Metrics().Height op.LineSpacingInPixels = mplusNormalFace.Metrics().Height
text.Draw(screen, sampleText, mplusNormalFace, op) text.Draw(screen, sampleText, mplusNormalFace, op)
} }
{ {
@ -92,7 +92,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.LineHeight = mplusBigFace.Metrics().Height op.LineSpacingInPixels = mplusBigFace.Metrics().Height
text.Draw(screen, sampleText, mplusBigFace, op) text.Draw(screen, sampleText, mplusBigFace, op)
} }
{ {
@ -101,19 +101,19 @@ 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.LineHeight = mplusNormalFace.Metrics().Height op.LineSpacingInPixels = mplusNormalFace.Metrics().Height
text.Draw(screen, sampleText, mplusNormalFace, op) text.Draw(screen, sampleText, mplusNormalFace, op)
} }
{ {
const x, y = 160, 220 const x, y = 160, 220
const lineHeight = 80 const lineSpacingInPixels = 80
w, h := text.Measure(sampleText, mplusBigFace, lineHeight) w, h := text.Measure(sampleText, mplusBigFace, lineSpacingInPixels)
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{}
// 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.LineHeight = lineHeight op.LineSpacingInPixels = lineSpacingInPixels
// 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

@ -126,7 +126,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
op.GeoM.Reset() op.GeoM.Reset()
op.GeoM.Translate(screenWidth-20, 210) op.GeoM.Translate(screenWidth-20, 210)
op.LineHeight = 48 op.LineSpacingInPixels = 48
text.Draw(screen, japaneseText, &text.GoTextFace{ text.Draw(screen, japaneseText, &text.GoTextFace{
Source: japaneseFaceSource, Source: japaneseFaceSource,
Direction: text.DirectionTopToBottomAndRightToLeft, Direction: text.DirectionTopToBottomAndRightToLeft,

View File

@ -91,7 +91,7 @@ func (t *TextField) textIndexByCursorPosition(x, y int) (int, bool) {
y = 0 y = 0
} }
lineHeight := int(fontFace.Metrics().Height) lineSpacingInPixels := int(fontFace.Metrics().Height)
var nlCount int var nlCount int
var lineStart int var lineStart int
var prevAdvance float64 var prevAdvance float64
@ -113,7 +113,7 @@ func (t *TextField) textIndexByCursorPosition(x, y int) (int, bool) {
} else { } else {
x1 = int(currentAdvance) x1 = int(currentAdvance)
} }
if x0 <= x && x < x1 && nlCount*lineHeight <= y && y < (nlCount+1)*lineHeight { if x0 <= x && x < x1 && nlCount*lineSpacingInPixels <= y && y < (nlCount+1)*lineSpacingInPixels {
return i, true return i, true
} }
prevAdvance = currentAdvance prevAdvance = currentAdvance
@ -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.LineHeight = fontFace.Metrics().Height op.LineSpacingInPixels = fontFace.Metrics().Height
text.Draw(screen, shownText, fontFace, op) text.Draw(screen, shownText, fontFace, op)
} }

View File

@ -31,8 +31,8 @@ import (
) )
const ( const (
uiFontSize = 12 uiFontSize = 12
lineHeight = 16 lineSpacingInPixels = 16
) )
var ( var (
@ -180,7 +180,7 @@ func (b *Button) Draw(dst *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(float64(b.Rect.Min.X+b.Rect.Max.X)/2, float64(b.Rect.Min.Y+b.Rect.Max.Y)/2) op.GeoM.Translate(float64(b.Rect.Min.X+b.Rect.Max.X)/2, float64(b.Rect.Min.Y+b.Rect.Max.Y)/2)
op.ColorScale.ScaleWithColor(color.Black) op.ColorScale.ScaleWithColor(color.Black)
op.LineHeight = lineHeight op.LineSpacingInPixels = lineSpacingInPixels
op.PrimaryAlign = text.AlignCenter op.PrimaryAlign = text.AlignCenter
op.SecondaryAlign = text.AlignCenter op.SecondaryAlign = text.AlignCenter
text.Draw(dst, b.Text, &text.GoTextFace{ text.Draw(dst, b.Text, &text.GoTextFace{
@ -319,7 +319,7 @@ func (t *TextBox) Update() {
} }
func (t *TextBox) contentSize() (int, int) { func (t *TextBox) contentSize() (int, int) {
h := len(strings.Split(t.Text, "\n"))*lineHeight + textBoxPaddingTop h := len(strings.Split(t.Text, "\n"))*lineSpacingInPixels + textBoxPaddingTop
return t.Rect.Dx(), h return t.Rect.Dx(), h
} }
@ -340,7 +340,7 @@ func (t *TextBox) Draw(dst *ebiten.Image) {
textOp.GeoM.Translate(x, y) textOp.GeoM.Translate(x, y)
textOp.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y)) textOp.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y))
textOp.ColorScale.ScaleWithColor(color.Black) textOp.ColorScale.ScaleWithColor(color.Black)
textOp.LineHeight = lineHeight textOp.LineSpacingInPixels = lineSpacingInPixels
text.Draw(dst.SubImage(t.Rect).(*ebiten.Image), t.Text, &text.GoTextFace{ text.Draw(dst.SubImage(t.Rect).(*ebiten.Image), t.Text, &text.GoTextFace{
Source: uiFaceSource, Source: uiFaceSource,
Size: uiFontSize, Size: uiFontSize,
@ -409,7 +409,7 @@ func (c *CheckBox) Draw(dst *ebiten.Image) {
op := &text.DrawOptions{} op := &text.DrawOptions{}
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.ColorScale.ScaleWithColor(color.Black) op.ColorScale.ScaleWithColor(color.Black)
op.LineHeight = lineHeight op.LineSpacingInPixels = lineSpacingInPixels
op.PrimaryAlign = text.AlignStart op.PrimaryAlign = text.AlignStart
op.SecondaryAlign = text.AlignCenter op.SecondaryAlign = text.AlignCenter
text.Draw(dst, c.Text, &text.GoTextFace{ text.Draw(dst, c.Text, &text.GoTextFace{

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 {
// LineHeight is a line height in pixels. // LineSpacingInPixels is a distance between two adjacent lines's baselines.
LineHeight float64 LineSpacingInPixels 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.LineHeight + m.HAscent + m.HDescent boundaryHeight = float64(lineCount-1)*options.LineSpacingInPixels + m.HAscent + m.HDescent
} else { } else {
boundaryWidth = float64(lineCount-1)*options.LineHeight + m.VAscent + m.VDescent boundaryWidth = float64(lineCount-1)*options.LineSpacingInPixels + 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.LineHeight originY += options.LineSpacingInPixels
case DirectionRightToLeft: case DirectionRightToLeft:
originY += options.LineHeight originY += options.LineSpacingInPixels
case DirectionTopToBottomAndLeftToRight: case DirectionTopToBottomAndLeftToRight:
originX += options.LineHeight originX += options.LineSpacingInPixels
case DirectionTopToBottomAndRightToLeft: case DirectionTopToBottomAndRightToLeft:
originX -= options.LineHeight originX -= options.LineSpacingInPixels
} }
} }

View File

@ -195,7 +195,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, lineHeight float64) (width, height float64) { func Measure(text string, face Face, lineSpacingInPixels float64) (width, height float64) {
if text == "" { if text == "" {
return 0, 0 return 0, 0
} }
@ -218,10 +218,10 @@ func Measure(text string, face Face, lineHeight float64) (width, height float64)
m := face.Metrics() m := face.Metrics()
if face.direction().isHorizontal() { if face.direction().isHorizontal() {
secondary := float64(lineCount-1)*lineHeight + m.HAscent + m.HDescent secondary := float64(lineCount-1)*lineSpacingInPixels + m.HAscent + m.HDescent
return primary, secondary return primary, secondary
} }
secondary := float64(lineCount-1)*lineHeight + m.VAscent + m.VDescent secondary := float64(lineCount-1)*lineSpacingInPixels + m.VAscent + m.VDescent
return secondary, primary return secondary, primary
} }