Debian Codegraph @

drawing/text.go

raw symbols SCIP
1 package drawing
2
3 import (
4 "github.com/golang/freetype/truetype"
5 "golang.org/x/image/math/fixed"
6 )
7
8 // DrawContour draws the given closed contour at the given sub-pixel offset.
9 func DrawContour(path PathBuilder, ps []truetype.Point, dx, dy float64) {
10 if len(ps) == 0 {
11 return
12 }
13 startX, startY := pointToF64Point(ps[0])
14 path.MoveTo(startX+dx, startY+dy)
15 q0X, q0Y, on0 := startX, startY, true
16 for _, p := range ps[1:] {
17 qX, qY := pointToF64Point(p)
18 on := p.Flags&0x01 != 0
19 if on {
20 if on0 {
21 path.LineTo(qX+dx, qY+dy)
22 } else {
23 path.QuadCurveTo(q0X+dx, q0Y+dy, qX+dx, qY+dy)
24 }
25 } else if !on0 {
26 midX := (q0X + qX) / 2
27 midY := (q0Y + qY) / 2
28 path.QuadCurveTo(q0X+dx, q0Y+dy, midX+dx, midY+dy)
29 }
30 q0X, q0Y, on0 = qX, qY, on
31 }
32 // Close the curve.
33 if on0 {
34 path.LineTo(startX+dx, startY+dy)
35 } else {
36 path.QuadCurveTo(q0X+dx, q0Y+dy, startX+dx, startY+dy)
37 }
38 }
39
40 // FontExtents contains font metric information.
41 type FontExtents struct {
42 // Ascent is the distance that the text
43 // extends above the baseline.
44 Ascent float64
45
46 // Descent is the distance that the text
47 // extends below the baseline. The descent
48 // is given as a negative value.
49 Descent float64
50
51 // Height is the distance from the lowest
52 // descending point to the highest ascending
53 // point.
54 Height float64
55 }
56
57 // Extents returns the FontExtents for a font.
58 // TODO needs to read this https://developer.apple.com/fonts/TrueType-Reference-Manual/RM02/Chap2.html#intro
59 func Extents(font *truetype.Font, size float64) FontExtents {
60 bounds := font.Bounds(fixed.Int26_6(font.FUnitsPerEm()))
61 scale := size / float64(font.FUnitsPerEm())
62 return FontExtents{
63 Ascent: float64(bounds.Max.Y) * scale,
64 Descent: float64(bounds.Min.Y) * scale,
65 Height: float64(bounds.Max.Y-bounds.Min.Y) * scale,
66 }
67 }