Skip to content

Commit

Permalink
text/v2: rename LineSpaceInPixels -> LineSpace for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Dec 24, 2023
1 parent f0396ba commit f577031
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 33 deletions.
2 changes: 1 addition & 1 deletion examples/blocks/blocks/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions examples/flappy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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{
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/font/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/fontfeature/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions examples/fontvariation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/fontvector/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion examples/fullscreen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion examples/keyboard/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
2 changes: 1 addition & 1 deletion examples/mixedfont/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
10 changes: 5 additions & 5 deletions examples/text/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
{
Expand All @@ -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)
}
{
Expand All @@ -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)
}
{
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions examples/texti18n/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
{
Expand All @@ -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)
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/textinput/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
6 changes: 3 additions & 3 deletions examples/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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{
Expand Down
17 changes: 9 additions & 8 deletions text/v2/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
}
}
Expand Down

0 comments on commit f577031

Please sign in to comment.