text/v2: replace Metrics.Height/Width with HLineGap and VLineGap

Updates #2454
This commit is contained in:
Hajime Hoshi 2023-12-02 14:26:17 +09:00
parent d91455c0a7
commit a444f2dd40
7 changed files with 24 additions and 24 deletions

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.
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)
}

View File

@ -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)
}
{

View File

@ -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 {

View File

@ -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
}

View File

@ -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

View File

@ -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),
}

View File

@ -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.