From f57703175efa78d4cd19f08bdba0198628278c79 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 24 Dec 2023 15:06:16 +0900 Subject: [PATCH] text/v2: rename LineSpaceInPixels -> LineSpace for consistency Updates #2454 --- examples/blocks/blocks/font.go | 2 +- examples/flappy/main.go | 8 ++++---- examples/font/main.go | 2 +- examples/fontfeature/main.go | 4 ++-- examples/fontvariation/main.go | 4 ++-- examples/fontvector/main.go | 2 +- examples/fullscreen/main.go | 2 +- examples/keyboard/main.go | 2 +- examples/mixedfont/main.go | 2 +- examples/text/main.go | 10 +++++----- examples/texti18n/main.go | 4 ++-- examples/textinput/main.go | 2 +- examples/ui/main.go | 6 +++--- text/v2/layout.go | 17 +++++++++-------- 14 files changed, 34 insertions(+), 33 deletions(-) diff --git a/examples/blocks/blocks/font.go b/examples/blocks/blocks/font.go index d59128a3d..934e5b56f 100644 --- a/examples/blocks/blocks/font.go +++ b/examples/blocks/blocks/font.go @@ -48,7 +48,7 @@ func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color op := &text.DrawOptions{} op.GeoM.Translate(float64(x)+1, float64(y)+1) op.ColorScale.ScaleWithColor(shadowColor) - op.LineSpacingInPixels = arcadeFontBaseSize * float64(scale) + op.LineSpacing = arcadeFontBaseSize * float64(scale) op.PrimaryAlign = primaryAlign op.SecondaryAlign = secondaryAlign text.Draw(rt, str, &text.GoTextFace{ diff --git a/examples/flappy/main.go b/examples/flappy/main.go index d0d84ac24..32df62ec8 100644 --- a/examples/flappy/main.go +++ b/examples/flappy/main.go @@ -277,7 +277,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op := &text.DrawOptions{} op.GeoM.Translate(screenWidth/2, 3*titleFontSize) op.ColorScale.ScaleWithColor(color.White) - op.LineSpacingInPixels = titleFontSize + op.LineSpacing = titleFontSize op.PrimaryAlign = text.AlignCenter text.Draw(screen, titleTexts, &text.GoTextFace{ Source: arcadeFaceSource, @@ -287,7 +287,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op = &text.DrawOptions{} op.GeoM.Translate(screenWidth/2, 3*titleFontSize) op.ColorScale.ScaleWithColor(color.White) - op.LineSpacingInPixels = fontSize + op.LineSpacing = fontSize op.PrimaryAlign = text.AlignCenter text.Draw(screen, texts, &text.GoTextFace{ Source: arcadeFaceSource, @@ -300,7 +300,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op := &text.DrawOptions{} op.GeoM.Translate(screenWidth/2, screenHeight-smallFontSize/2) op.ColorScale.ScaleWithColor(color.White) - op.LineSpacingInPixels = smallFontSize + op.LineSpacing = smallFontSize op.PrimaryAlign = text.AlignCenter op.SecondaryAlign = text.AlignEnd text.Draw(screen, msg, &text.GoTextFace{ @@ -312,7 +312,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op = &text.DrawOptions{} op.GeoM.Translate(screenWidth, 0) op.ColorScale.ScaleWithColor(color.White) - op.LineSpacingInPixels = fontSize + op.LineSpacing = fontSize op.PrimaryAlign = text.AlignEnd text.Draw(screen, fmt.Sprintf("%04d", g.score()), &text.GoTextFace{ Source: arcadeFaceSource, diff --git a/examples/font/main.go b/examples/font/main.go index 253263fbd..1048c40ac 100644 --- a/examples/font/main.go +++ b/examples/font/main.go @@ -155,7 +155,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op = &text.DrawOptions{} op.GeoM.Translate(x, 110) op.ColorScale.ScaleWithColor(g.kanjiTextColor) - op.LineSpacingInPixels = bigFontSize * 1.2 + op.LineSpacing = bigFontSize * 1.2 text.Draw(screen, g.kanjiText, &text.GoTextFace{ Source: mplusFaceSource, Size: bigFontSize, diff --git a/examples/fontfeature/main.go b/examples/fontfeature/main.go index dbae22009..9770c9cb4 100644 --- a/examples/fontfeature/main.go +++ b/examples/fontfeature/main.go @@ -102,7 +102,7 @@ func (g *Game) Draw(screen *ebiten.Image) { [Z] 'zero' (Slashed Zero) (%d)`, g.liga, g.tnum, g.smcp, g.zero) op := &text.DrawOptions{} op.GeoM.Translate(20, 20) - op.LineSpacingInPixels = 30 + op.LineSpacing = 30 text.Draw(screen, inst, &text.GoTextFace{ Source: firaSansFaceSource, Size: 20, @@ -115,7 +115,7 @@ ffi 2.71` op = &text.DrawOptions{} op.GeoM.Translate(20, screenHeight/2) - op.LineSpacingInPixels = 50 + op.LineSpacing = 50 f := &text.GoTextFace{ Source: firaSansFaceSource, Size: 40, diff --git a/examples/fontvariation/main.go b/examples/fontvariation/main.go index 48e9dbb03..2f405642f 100644 --- a/examples/fontvariation/main.go +++ b/examples/fontvariation/main.go @@ -116,7 +116,7 @@ func (g *Game) Draw(screen *ebiten.Image) { [Z, X]: slnt (Slant): %0.0f [%d-%d]`, g.wght, minWght, maxWght, g.wdth, minWdth, maxWdth, g.slnt, minSlnt, maxSlnt) op := &text.DrawOptions{} op.GeoM.Translate(20, 20) - op.LineSpacingInPixels = 30 + op.LineSpacing = 30 text.Draw(screen, inst, &text.GoTextFace{ Source: robotoFlexFaceSource, Size: 20, @@ -127,7 +127,7 @@ func (g *Game) Draw(screen *ebiten.Image) { over the lazy dog.` op = &text.DrawOptions{} op.GeoM.Translate(20, screenHeight/2) - op.LineSpacingInPixels = 50 + op.LineSpacing = 50 f := &text.GoTextFace{ Source: robotoFlexFaceSource, Size: 40, diff --git a/examples/fontvector/main.go b/examples/fontvector/main.go index 218c28738..d57ad7e73 100644 --- a/examples/fontvector/main.go +++ b/examples/fontvector/main.go @@ -59,7 +59,7 @@ func (g *Game) Update() error { return err } op := &text.LayoutOptions{} - op.LineSpacingInPixels = 110 + op.LineSpacing = 110 text.AppendVectorPath(&g.path, "ABCEDFG\nabcdefg\nあいうえお\nかきくけこ", &text.GoTextFace{ Source: s, Size: 90, diff --git a/examples/fullscreen/main.go b/examples/fullscreen/main.go index dd8e77a2a..187aecd2b 100644 --- a/examples/fullscreen/main.go +++ b/examples/fullscreen/main.go @@ -99,7 +99,7 @@ func (g *Game) Draw(screen *ebiten.Image) { textOp := &text.DrawOptions{} textOp.GeoM.Translate(50*scale, 50*scale) textOp.ColorScale.ScaleWithColor(color.White) - textOp.LineSpacingInPixels = 12 * ebiten.DeviceScaleFactor() * 1.5 + textOp.LineSpacing = 12 * ebiten.DeviceScaleFactor() * 1.5 text.Draw(screen, msg, &text.GoTextFace{ Source: mplusFaceSource, Size: 12 * ebiten.DeviceScaleFactor(), diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index 41f1e059e..dd0b5d558 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.LineSpacingInPixels = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent + textOp.LineSpacing = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent text.Draw(screen, strings.Join(keyStrs, ", ")+"\n"+strings.Join(keyNames, ", "), fontFace, textOp) } diff --git a/examples/mixedfont/main.go b/examples/mixedfont/main.go index 27f2cd878..3e4593ac1 100644 --- a/examples/mixedfont/main.go +++ b/examples/mixedfont/main.go @@ -86,7 +86,7 @@ func (g *Game) Update() error { func (g *Game) Draw(screen *ebiten.Image) { op := &text.DrawOptions{} op.GeoM.Translate(20, 20) - op.LineSpacingInPixels = 48 + op.LineSpacing = 48 text.Draw(screen, "HelloこんにちはWorld世界\n日本語とEnglish…", g.face, op) } diff --git a/examples/text/main.go b/examples/text/main.go index 8a0090d21..a7e44d908 100644 --- a/examples/text/main.go +++ b/examples/text/main.go @@ -68,7 +68,7 @@ func (g *Game) Update() error { // Initialize the glyphs for special (colorful) rendering. if len(g.glyphs) == 0 { op := &text.LayoutOptions{} - op.LineSpacingInPixels = mplusNormalFace.Size * 1.5 + op.LineSpacing = mplusNormalFace.Size * 1.5 g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op) } return nil @@ -83,7 +83,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.LineSpacingInPixels = mplusNormalFace.Size * 1.5 + op.LineSpacing = mplusNormalFace.Size * 1.5 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) op := &text.DrawOptions{} op.GeoM.Translate(x, y) - op.LineSpacingInPixels = mplusBigFace.Size * 1.5 + op.LineSpacing = mplusBigFace.Size * 1.5 text.Draw(screen, sampleText, mplusBigFace, op) } { @@ -101,7 +101,7 @@ func (g *Game) Draw(screen *ebiten.Image) { op.GeoM.Rotate(math.Pi / 4) op.GeoM.Translate(x, y) op.Filter = ebiten.FilterLinear - op.LineSpacingInPixels = mplusNormalFace.Size * 1.5 + op.LineSpacing = mplusNormalFace.Size * 1.5 text.Draw(screen, sampleText, mplusNormalFace, op) } { @@ -113,7 +113,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.LineSpacingInPixels = lineSpacingInPixels + op.LineSpacing = lineSpacingInPixels // 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 0021957c2..0ff389ced 100644 --- a/examples/texti18n/main.go +++ b/examples/texti18n/main.go @@ -195,7 +195,7 @@ func (g *Game) Draw(screen *ebiten.Image) { vector.DrawFilledRect(screen, float32(x), float32(y), float32(w), float32(h), gray, false) op := &text.DrawOptions{} op.GeoM.Translate(float64(x), float64(y)) - op.LineSpacingInPixels = lineSpacing + op.LineSpacing = lineSpacing text.Draw(screen, mongolianText, f, op) } { @@ -213,7 +213,7 @@ func (g *Game) Draw(screen *ebiten.Image) { vector.DrawFilledRect(screen, float32(x)-float32(w), float32(y), float32(w), float32(h), gray, false) op := &text.DrawOptions{} op.GeoM.Translate(float64(x), float64(y)) - op.LineSpacingInPixels = lineSpacing + op.LineSpacing = lineSpacing text.Draw(screen, japaneseText, f, op) } } diff --git a/examples/textinput/main.go b/examples/textinput/main.go index 0dbfb0532..ee690127a 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.LineSpacingInPixels = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent + op.LineSpacing = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent text.Draw(screen, shownText, fontFace, op) } diff --git a/examples/ui/main.go b/examples/ui/main.go index 1bb715582..aa30d0411 100644 --- a/examples/ui/main.go +++ b/examples/ui/main.go @@ -180,7 +180,7 @@ func (b *Button) Draw(dst *ebiten.Image) { 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.ColorScale.ScaleWithColor(color.Black) - op.LineSpacingInPixels = lineSpacingInPixels + op.LineSpacing = lineSpacingInPixels op.PrimaryAlign = text.AlignCenter op.SecondaryAlign = text.AlignCenter text.Draw(dst, b.Text, &text.GoTextFace{ @@ -340,7 +340,7 @@ func (t *TextBox) Draw(dst *ebiten.Image) { textOp.GeoM.Translate(x, y) textOp.GeoM.Translate(float64(t.Rect.Min.X), float64(t.Rect.Min.Y)) textOp.ColorScale.ScaleWithColor(color.Black) - textOp.LineSpacingInPixels = lineSpacingInPixels + textOp.LineSpacing = lineSpacingInPixels text.Draw(dst.SubImage(t.Rect).(*ebiten.Image), t.Text, &text.GoTextFace{ Source: uiFaceSource, Size: uiFontSize, @@ -409,7 +409,7 @@ func (c *CheckBox) Draw(dst *ebiten.Image) { op := &text.DrawOptions{} op.GeoM.Translate(float64(x), float64(y)) op.ColorScale.ScaleWithColor(color.Black) - op.LineSpacingInPixels = lineSpacingInPixels + op.LineSpacing = lineSpacingInPixels op.PrimaryAlign = text.AlignStart op.SecondaryAlign = text.AlignCenter text.Draw(dst, c.Text, &text.GoTextFace{ diff --git a/text/v2/layout.go b/text/v2/layout.go index 3dc720216..ce022f393 100644 --- a/text/v2/layout.go +++ b/text/v2/layout.go @@ -46,8 +46,9 @@ 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 { - // LineSpacingInPixels is a distance between two adjacent lines's baselines. - LineSpacingInPixels float64 + // LineSpacing is a distance between two adjacent lines's baselines. + // The unit is in pixels. + LineSpacing 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, @@ -184,10 +185,10 @@ func forEachLine(text string, face Face, options *LayoutOptions, f func(text str var boundaryWidth, boundaryHeight float64 if d.isHorizontal() { boundaryWidth = longestAdvance - boundaryHeight = float64(lineCount-1)*options.LineSpacingInPixels + m.HAscent + m.HDescent + boundaryHeight = float64(lineCount-1)*options.LineSpacing + m.HAscent + m.HDescent } else { // TODO: Perhaps HAscent and HDescent should be used for sideways glyphs. - boundaryWidth = float64(lineCount-1)*options.LineSpacingInPixels + m.VAscent + m.VDescent + boundaryWidth = float64(lineCount-1)*options.LineSpacing + m.VAscent + m.VDescent boundaryHeight = longestAdvance } @@ -267,13 +268,13 @@ func forEachLine(text string, face Face, options *LayoutOptions, f func(text str // Advance the origin position in the secondary direction. switch face.direction() { case DirectionLeftToRight: - originY += options.LineSpacingInPixels + originY += options.LineSpacing case DirectionRightToLeft: - originY += options.LineSpacingInPixels + originY += options.LineSpacing case DirectionTopToBottomAndLeftToRight: - originX += options.LineSpacingInPixels + originX += options.LineSpacing case DirectionTopToBottomAndRightToLeft: - originX -= options.LineSpacingInPixels + originX -= options.LineSpacing } } }