-
Notifications
You must be signed in to change notification settings - Fork 0
/
type3.go
149 lines (131 loc) · 3.62 KB
/
type3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// seehuhn.de/go/pdf - a library for reading and writing PDF files
// Copyright (C) 2023 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package makefont
import (
"seehuhn.de/go/pdf/font/type3"
"seehuhn.de/go/postscript/funit"
"seehuhn.de/go/sfnt/glyf"
"seehuhn.de/go/sfnt/glyph"
)
// Type3 returns a Type3 font.
func Type3() (*type3.Font, error) {
info := TrueType()
info = clone(info)
info.EnsureGlyphNames()
res := type3.New(info.UnitsPerEm)
res.Ascent = info.Ascent
res.Descent = info.Descent
res.BaseLineSkip = info.Ascent - info.Descent + info.LineGap
res.ItalicAngle = info.ItalicAngle
res.IsFixedPitch = info.IsFixedPitch()
res.IsSerif = info.IsSerif
res.IsScript = info.IsScript
// convert glypf outlines to type 3 outlines
origOutlines := info.Outlines.(*glyf.Outlines)
for i, origGlyph := range origOutlines.Glyphs {
gid := glyph.ID(i)
name := info.GlyphName(gid)
if name == ".notdef" {
continue
}
var bbox funit.Rect16
if origGlyph != nil {
bbox = origGlyph.Rect16
}
newGlyph, err := res.AddGlyph(name, info.GlyphWidth(gid), bbox, true)
if err != nil {
return nil, err
}
if origGlyph == nil {
goto done
}
switch g := origGlyph.Data.(type) {
case glyf.SimpleGlyph:
glyphInfo, err := g.Decode()
if err != nil {
panic(err)
}
for _, cc := range glyphInfo.Contours {
var extended glyf.Contour
var prev glyf.Point
onCurve := true
for _, cur := range cc {
if !onCurve && !cur.OnCurve {
extended = append(extended, glyf.Point{
X: (cur.X + prev.X) / 2,
Y: (cur.Y + prev.Y) / 2,
OnCurve: true,
})
}
extended = append(extended, cur)
prev = cur
onCurve = cur.OnCurve
}
n := len(extended)
var offs int
for i := 0; i < len(extended); i++ {
if extended[i].OnCurve {
offs = i
break
}
}
newGlyph.MoveTo(float64(extended[offs].X), float64(extended[offs].Y))
i := 0
for i < n {
i0 := (i + offs) % n
if !extended[i0].OnCurve {
panic("not on curve")
}
i1 := (i0 + 1) % n
if extended[i1].OnCurve {
if i == n-1 {
break
}
newGlyph.LineTo(float64(extended[i1].X), float64(extended[i1].Y))
i++
} else {
// See the following link for converting truetype outlines
// to CFF outlines:
// https://pomax.github.io/bezierinfo/#reordering
i2 := (i1 + 1) % n
newGlyph.CurveTo(
float64(extended[i0].X)/3+float64(extended[i1].X)*2/3,
float64(extended[i0].Y)/3+float64(extended[i1].Y)*2/3,
float64(extended[i1].X)*2/3+float64(extended[i2].X)/3,
float64(extended[i1].Y)*2/3+float64(extended[i2].Y)/3,
float64(extended[i2].X),
float64(extended[i2].Y))
i += 2
}
}
newGlyph.ClosePath()
}
newGlyph.Fill()
case glyf.CompositeGlyph:
panic("not implemented")
}
done:
err = newGlyph.Close()
if err != nil {
return nil, err
}
}
return res, nil
}
func clone[T any](x *T) *T {
y := *x
return &y
}