Skip to content

Commit

Permalink
Make FontFace.mmPerEm public, fixes #256
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Sep 29, 2023
1 parent c651fde commit db72658
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 39 deletions.
68 changes: 34 additions & 34 deletions font.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ func (f *Font) Face(size float64, ifill interface{}, deco ...FontDecorator) *Fon
}
face.Deco = deco
face.Hinting = font.VerticalHinting
face.mmPerEm = face.Size / float64(face.Font.Head.UnitsPerEm)
face.MmPerEm = face.Size / float64(face.Font.Head.UnitsPerEm)
return face
}

Expand Down Expand Up @@ -599,7 +599,7 @@ func (family *FontFamily) Face(size float64, args ...interface{}) *FontFace {
face.XOffset = int32(float64(xOffset) / scale)
face.YOffset = int32(float64(yOffset) / scale)
}
face.mmPerEm = face.Size / float64(face.Font.Head.UnitsPerEm)
face.MmPerEm = face.Size / float64(face.Font.Head.UnitsPerEm)
return face
}

Expand Down Expand Up @@ -630,7 +630,7 @@ type FontFace struct {
// line height
// shadow

mmPerEm float64 // millimeters per EM unit!
MmPerEm float64 // millimeters per EM unit!
}

// Equals returns true when two font face are equal.
Expand Down Expand Up @@ -670,23 +670,23 @@ func (face *FontFace) Metrics() FontMetrics {
sfnt := face.Font.SFNT
ascender, descender, lineGap := sfnt.VerticalMetrics()
return FontMetrics{
LineHeight: face.mmPerEm * float64(ascender+descender+lineGap),
Ascent: face.mmPerEm * float64(ascender),
Descent: face.mmPerEm * float64(descender),
LineGap: face.mmPerEm * float64(lineGap),
XHeight: face.mmPerEm * float64(sfnt.OS2.SxHeight),
CapHeight: face.mmPerEm * float64(sfnt.OS2.SCapHeight),
XMin: face.mmPerEm * float64(sfnt.Head.XMin),
YMin: face.mmPerEm * float64(sfnt.Head.YMin),
XMax: face.mmPerEm * float64(sfnt.Head.XMax),
YMax: face.mmPerEm * float64(sfnt.Head.YMax),
LineHeight: face.MmPerEm * float64(ascender+descender+lineGap),
Ascent: face.MmPerEm * float64(ascender),
Descent: face.MmPerEm * float64(descender),
LineGap: face.MmPerEm * float64(lineGap),
XHeight: face.MmPerEm * float64(sfnt.OS2.SxHeight),
CapHeight: face.MmPerEm * float64(sfnt.OS2.SCapHeight),
XMin: face.MmPerEm * float64(sfnt.Head.XMin),
YMin: face.MmPerEm * float64(sfnt.Head.YMin),
XMax: face.MmPerEm * float64(sfnt.Head.XMax),
YMax: face.MmPerEm * float64(sfnt.Head.YMax),
}
}

// PPEM returns the pixels-per-EM for a given resolution of the font face.
func (face *FontFace) PPEM(resolution Resolution) uint16 {
// ppem is for hinting purposes only, this does not influence glyph advances
return uint16(resolution.DPMM() * face.mmPerEm * float64(face.Font.Head.UnitsPerEm))
return uint16(resolution.DPMM() * face.MmPerEm * float64(face.Font.Head.UnitsPerEm))
}

// LineHeight returns the height (ascent+descent) of a line.
Expand All @@ -711,7 +711,7 @@ func (face *FontFace) textWidth(glyphs []text.Glyph) float64 {
w -= glyph.YAdvance
}
}
return face.mmPerEm * float64(w)
return face.MmPerEm * float64(w)
}

func (face *FontFace) heights(mode WritingMode) (float64, float64, float64, float64) {
Expand Down Expand Up @@ -748,7 +748,7 @@ func (face *FontFace) ToPath(s string) (*Path, float64, error) {

func (face *FontFace) toPath(glyphs []text.Glyph, ppem uint16) (*Path, float64, error) {
p := &Path{}
f := face.mmPerEm
f := face.MmPerEm
x, y := face.XOffset, face.YOffset
for _, glyph := range glyphs {
err := face.Font.GlyphPath(p, glyph.ID, ppem, f*float64(x+glyph.XOffset), f*float64(y+glyph.YOffset), f, font.NoHinting)
Expand All @@ -765,7 +765,7 @@ func (face *FontFace) toPath(glyphs []text.Glyph, ppem uint16) (*Path, float64,
if face.FauxItalic != 0.0 {
p = p.Transform(Identity.Shear(face.FauxItalic, 0.0))
}
return p, face.mmPerEm * float64(x), nil
return p, face.MmPerEm * float64(x), nil
}

////////////////////////////////////////////////////////////////
Expand All @@ -787,10 +787,10 @@ func (underline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand All @@ -813,7 +813,7 @@ func (overline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := face.Metrics().Ascent
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
y -= 0.5 * r

Expand All @@ -839,10 +839,10 @@ func (strikethrough) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := face.Metrics().XHeight / 2.0
if face.Font.OS2.YStrikeoutSize != 0 {
r = face.mmPerEm * float64(face.Font.OS2.YStrikeoutSize)
r = face.MmPerEm * float64(face.Font.OS2.YStrikeoutSize)
}
if face.Font.OS2.YStrikeoutPosition != 0 {
y = face.mmPerEm * float64(face.Font.OS2.YStrikeoutPosition)
y = face.MmPerEm * float64(face.Font.OS2.YStrikeoutPosition)
}
y += 0.5 * r

Expand All @@ -868,10 +868,10 @@ func (doubleUnderline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand All @@ -896,10 +896,10 @@ func (dottedUnderline) Decorate(face *FontFace, w float64) *Path {
r := 0.5 * face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = 0.5 * face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = 0.5 * face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= 2.0 * r
w -= 2.0 * r
Expand Down Expand Up @@ -934,10 +934,10 @@ func (dashedUnderline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand Down Expand Up @@ -967,10 +967,10 @@ func (wavyUnderline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand Down Expand Up @@ -1012,10 +1012,10 @@ func (sineUnderline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand Down Expand Up @@ -1056,10 +1056,10 @@ func (sawtoothUnderline) Decorate(face *FontFace, w float64) *Path {
r := face.Size * underlineThickness
y := -face.Size * underlineDistance
if face.Font.Post.UnderlineThickness != 0 {
r = face.mmPerEm * float64(face.Font.Post.UnderlineThickness)
r = face.MmPerEm * float64(face.Font.Post.UnderlineThickness)
}
if face.Font.Post.UnderlinePosition != 0 {
y = face.mmPerEm * float64(face.Font.Post.UnderlinePosition)
y = face.MmPerEm * float64(face.Font.Post.UnderlinePosition)
}
y -= r

Expand Down
10 changes: 5 additions & 5 deletions text.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (l line) Heights(mode WritingMode) (float64, float64, float64, float64) {
if span.IsText() {
for _, glyph := range span.Glyphs {
if glyph.Vertical {
width = math.Max(width, 1.2*span.Face.mmPerEm*float64(glyph.SFNT.GlyphAdvance(glyph.ID))) // TODO: what left/right padding should upright characters in a vertical layout have?
width = math.Max(width, 1.2*span.Face.MmPerEm*float64(glyph.SFNT.GlyphAdvance(glyph.ID))) // TODO: what left/right padding should upright characters in a vertical layout have?
} else {
spanTop, spanAscent, spanDescent, spanBottom := span.Face.heights(mode)
top = math.Max(top, spanTop)
Expand Down Expand Up @@ -1090,8 +1090,8 @@ func (t *Text) WalkDecorations(callback func(fill Paint, deco *Path)) {
if !found {
// remove active decoration and draw it
decoSpan := active[i-di]
xOffset := span.Face.mmPerEm * float64(span.Face.XOffset)
yOffset := span.Face.mmPerEm * float64(span.Face.YOffset)
xOffset := span.Face.MmPerEm * float64(span.Face.XOffset)
yOffset := span.Face.MmPerEm * float64(span.Face.YOffset)
p := decoSpan.deco.Decorate(decoSpan.face, decoSpan.x1-decoSpan.x0)
p = p.Translate(decoSpan.x0+xOffset, -line.y+yOffset)

Expand Down Expand Up @@ -1130,8 +1130,8 @@ func (t *Text) WalkLines(callback func(float64, []TextSpan)) {
func (t *Text) WalkSpans(callback func(float64, float64, TextSpan)) {
for _, line := range t.lines {
for _, span := range line.spans {
xOffset := span.Face.mmPerEm * float64(span.Face.XOffset)
yOffset := span.Face.mmPerEm * float64(span.Face.YOffset)
xOffset := span.Face.MmPerEm * float64(span.Face.XOffset)
yOffset := span.Face.MmPerEm * float64(span.Face.YOffset)
if t.WritingMode == HorizontalTB {
callback(span.X+xOffset, -line.y+yOffset, span)
} else {
Expand Down

0 comments on commit db72658

Please sign in to comment.