From a444f2dd40eec5847ec653d83535cbf38b241836 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 2 Dec 2023 14:26:17 +0900 Subject: [PATCH] text/v2: replace Metrics.Height/Width with HLineGap and VLineGap Updates #2454 --- examples/keyboard/main.go | 2 +- examples/text/main.go | 12 ++++++------ examples/textinput/main.go | 10 +++++----- text/v2/gotext.go | 4 ++-- text/v2/multi.go | 8 ++++---- text/v2/std.go | 2 +- text/v2/text.go | 10 +++++----- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index abd04d34f..41f1e059e 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().Height + textOp.LineSpacingInPixels = 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/text/main.go b/examples/text/main.go index e98534302..2bbcb92d6 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.Metrics().Height + op.LineSpacingInPixels = mplusNormalFace.Size * 1.5 g.glyphs = text.AppendGlyphs(g.glyphs, sampleText, mplusNormalFace, op) } return nil @@ -79,20 +79,20 @@ func (g *Game) Draw(screen *ebiten.Image) { { const x, y = 20, 20 - w, h := text.Measure(sampleText, mplusNormalFace, mplusNormalFace.Metrics().Height) + w, h := text.Measure(sampleText, mplusNormalFace, mplusNormalFace.Size*1.5) vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false) op := &text.DrawOptions{} op.GeoM.Translate(x, y) - op.LineSpacingInPixels = mplusNormalFace.Metrics().Height + op.LineSpacingInPixels = mplusNormalFace.Size * 1.5 text.Draw(screen, sampleText, mplusNormalFace, op) } { const x, y = 20, 120 - w, h := text.Measure(sampleText, mplusBigFace, mplusBigFace.Metrics().Height) + w, h := text.Measure(sampleText, mplusBigFace, mplusBigFace.Size*1.5) vector.DrawFilledRect(screen, x, y, float32(w), float32(h), gray, false) op := &text.DrawOptions{} op.GeoM.Translate(x, y) - op.LineSpacingInPixels = mplusBigFace.Metrics().Height + op.LineSpacingInPixels = 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.Metrics().Height + op.LineSpacingInPixels = mplusNormalFace.Size * 1.5 text.Draw(screen, sampleText, mplusNormalFace, op) } { diff --git a/examples/textinput/main.go b/examples/textinput/main.go index dc029de09..0dbfb0532 100644 --- a/examples/textinput/main.go +++ b/examples/textinput/main.go @@ -91,7 +91,7 @@ func (t *TextField) textIndexByCursorPosition(x, y int) (int, bool) { y = 0 } - lineSpacingInPixels := int(fontFace.Metrics().Height) + lineSpacingInPixels := int(fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent) var nlCount int var lineStart int var prevAdvance float64 @@ -256,7 +256,7 @@ func (t *TextField) cursorPos() (int, int) { txt += t.state.Text[:t.state.CompositionSelectionStartInBytes] } x := int(text.Advance(txt, fontFace)) - y := nlCount * int(fontFace.Metrics().Height) + y := nlCount * int(fontFace.Metrics().HLineGap+fontFace.Metrics().HAscent+fontFace.Metrics().HDescent) return x, y } @@ -274,7 +274,7 @@ func (t *TextField) Draw(screen *ebiten.Image) { cx, cy := t.cursorPos() x += px + cx y += py + cy - h := int(fontFace.Metrics().Height) + h := int(fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent) vector.StrokeLine(screen, float32(x), float32(y), float32(x), float32(y+h), 1, color.Black, false) } @@ -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().Height + op.LineSpacingInPixels = fontFace.Metrics().HLineGap + fontFace.Metrics().HAscent + fontFace.Metrics().HDescent text.Draw(screen, shownText, fontFace, op) } @@ -296,7 +296,7 @@ const textFieldHeight = 24 func textFieldPadding() (int, int) { m := fontFace.Metrics() - return 4, (textFieldHeight - int(m.Height)) / 2 + return 4, (textFieldHeight - int(m.HLineGap+m.HAscent+m.HDescent)) / 2 } type Game struct { diff --git a/text/v2/gotext.go b/text/v2/gotext.go index 3cfa8d083..9e709d069 100644 --- a/text/v2/gotext.go +++ b/text/v2/gotext.go @@ -189,12 +189,12 @@ func (g *GoTextFace) Metrics() Metrics { var m Metrics if h, ok := g.Source.f.FontHExtents(); ok { - m.Height = float64(h.Ascender-h.Descender+h.LineGap) * scale + m.HLineGap = float64(h.LineGap) * scale m.HAscent = float64(h.Ascender) * scale m.HDescent = float64(-h.Descender) * scale } if v, ok := g.Source.f.FontVExtents(); ok { - m.Width = float64(v.Ascender-v.Descender+v.LineGap) * scale + m.VLineGap = float64(v.LineGap) * scale m.VAscent = float64(v.Ascender) * scale m.VDescent = float64(-v.Descender) * scale } diff --git a/text/v2/multi.go b/text/v2/multi.go index 944f00272..84cab3415 100644 --- a/text/v2/multi.go +++ b/text/v2/multi.go @@ -33,8 +33,8 @@ func (m MultiFace) Metrics() Metrics { var mt Metrics for _, f := range m { mt1 := f.Metrics() - if mt1.Height > mt.Height { - mt.Height = mt1.Height + if mt1.HLineGap > mt.HLineGap { + mt.HLineGap = mt1.HLineGap } if mt1.HAscent > mt.HAscent { mt.HAscent = mt1.HAscent @@ -42,8 +42,8 @@ func (m MultiFace) Metrics() Metrics { if mt1.HDescent > mt.HDescent { mt.HDescent = mt1.HDescent } - if mt1.Width > mt.Width { - mt.Width = mt1.Width + if mt1.VLineGap > mt.VLineGap { + mt.VLineGap = mt1.VLineGap } if mt1.VAscent > mt.VAscent { mt.VAscent = mt1.VAscent diff --git a/text/v2/std.go b/text/v2/std.go index 9c6b195d8..46cfaeded 100644 --- a/text/v2/std.go +++ b/text/v2/std.go @@ -68,7 +68,7 @@ func (s *StdFace) Metrics() Metrics { m := s.f.Metrics() return Metrics{ - Height: fixed26_6ToFloat64(m.Height), + HLineGap: fixed26_6ToFloat64(m.Height - m.Ascent - m.Descent), HAscent: fixed26_6ToFloat64(m.Ascent), HDescent: fixed26_6ToFloat64(m.Descent), } diff --git a/text/v2/text.go b/text/v2/text.go index 3e2589809..9d133435b 100644 --- a/text/v2/text.go +++ b/text/v2/text.go @@ -48,8 +48,8 @@ type Face interface { // Metrics holds the metrics for a Face. // A visual depiction is at https://developer.apple.com/library/mac/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyph_metrics_2x.png type Metrics struct { - // Height is the recommended amount of vertical space between two lines of text in pixels. - Height float64 + // HLineGap is the recommended amount of vertical space between two lines of text in pixels. + HLineGap float64 // HAscent is the distance in pixels from the top of a line to its baseline for horizontal lines. HAscent float64 @@ -58,9 +58,9 @@ type Metrics struct { // The value is typically positive, even though a descender goes below the baseline. HDescent float64 - // Width is the recommended amount of horizontal space between two lines of text in pixels. - // If the face is StdFace or the font dosen't support a vertical direction, Width is 0. - Width float64 + // VLineGap is the recommended amount of horizontal space between two lines of text in pixels. + // If the face is StdFace or the font dosen't support a vertical direction, VLineGap is 0. + VLineGap float64 // VAscent is the distance in pixels from the top of a line to its baseline for vertical lines. // If the face is StdFace or the font dosen't support a vertical direction, VAscent is 0.