Repository navigation
Expand file tree
/
Copy pathtext.go
More file actions
327 lines (312 loc) · 8.8 KB
/
Copy pathtext.go
File metadata and controls
327 lines (312 loc) · 8.8 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package render
import (
"github.com/go-gfx/gfx/geometry"
"github.com/go-gfx/gfx/vector"
"github.com/go-opentype/opentype"
"github.com/go-pdfkit/pdffont"
"github.com/go-pdfkit/reader"
"math"
)
// A textState is everything a show operator reads besides the string itself.
type textState struct {
font *pdfFont
size float64
charSpace float64
wordSpace float64
// scale is the horizontal stretch, as a fraction rather than the
// percentage the operator takes.
scale float64
leading float64
rise float64
mode int
}
// The text rendering modes that matter to what appears on the page.
const (
modeFill = iota
modeStroke
modeFillStroke
modeInvisible
)
// runText handles every operator between BT and ET, and the two that set the
// text state from outside it.
func (r *renderer) runText(g *gstate, op string, operands []reader.Object, resources reader.Dict) {
n := numbers(operands)
switch op {
case "BT":
r.tm, r.tlm = geometry.Identity(), geometry.Identity()
case "ET":
case "Tf":
if len(operands) >= 2 {
g.text.font = r.font(operands[0], resources)
g.text.size, _ = reader.ToFloat(operands[1])
}
case "Tc":
if len(n) >= 1 {
g.text.charSpace = n[0]
}
case "Tw":
if len(n) >= 1 {
g.text.wordSpace = n[0]
}
case "Tz":
if len(n) >= 1 {
g.text.scale = n[0] / 100
}
case "TL":
if len(n) >= 1 {
g.text.leading = n[0]
}
case "Ts":
if len(n) >= 1 {
g.text.rise = n[0]
}
case "Tr":
if len(n) >= 1 {
g.text.mode = int(n[0])
}
case "Td":
if len(n) >= 2 {
r.nextLine(n[0], n[1])
}
case "TD":
if len(n) >= 2 {
g.text.leading = -n[1]
r.nextLine(n[0], n[1])
}
case "Tm":
if len(n) >= 6 {
r.tm = matrix(n)
r.tlm = r.tm
}
case "T*":
r.nextLine(0, -g.text.leading)
case "Tj":
if len(operands) >= 1 {
r.showOperand(g, operands[0], resources)
}
case "'":
r.nextLine(0, -g.text.leading)
if len(operands) >= 1 {
r.showOperand(g, operands[0], resources)
}
case "\"":
if len(operands) >= 3 {
g.text.wordSpace, _ = reader.ToFloat(operands[0])
g.text.charSpace, _ = reader.ToFloat(operands[1])
r.nextLine(0, -g.text.leading)
r.showOperand(g, operands[2], resources)
}
case "TJ":
if len(operands) >= 1 {
r.showArray(g, operands[0], resources)
}
}
}
// nextLine moves the text position, which is what every positioning operator
// comes down to.
func (r *renderer) nextLine(tx, ty float64) {
r.tlm = r.tlm.Mul(geometry.Translate(tx, ty))
r.tm = r.tlm
}
// showOperand draws one string.
func (r *renderer) showOperand(g *gstate, o reader.Object, resources reader.Dict) {
s, ok := reader.ToString(o)
if !ok {
return
}
r.show(g, s, resources)
}
// showArray draws the strings of a TJ array, moving the pen back by the
// numbers between them.
func (r *renderer) showArray(g *gstate, o reader.Object, resources reader.Dict) {
arr, ok := reader.ToArray(o)
if !ok {
return
}
for _, e := range arr {
if s, ok := reader.ToString(e); ok {
r.show(g, s, resources)
continue
}
if v, ok := reader.ToFloat(e); ok {
// A number moves the pen by that many thousandths of the size,
// backwards, which is how a file tightens or loosens its spacing.
r.tm = r.tm.Mul(geometry.Translate(-v/1000*g.text.size*g.text.scale, 0))
}
}
}
// show draws one string and leaves the pen where the next one starts.
func (r *renderer) show(g *gstate, s []byte, resources reader.Dict) {
f := g.text.font
if f == nil || g.text.size == 0 {
return
}
if g.text.scale == 0 {
g.text.scale = 1
}
single := f.Kind() != pdffont.Composite
for _, code := range f.Codes(s) {
if g.text.mode != modeInvisible && !r.suppressed() {
r.drawGlyph(g, f, code, resources)
}
advance := (f.advance(code)*g.text.size + g.text.charSpace) * g.text.scale
if single && code == ' ' {
advance += g.text.wordSpace * g.text.scale
}
r.tm = r.tm.Mul(geometry.Translate(advance, 0))
}
}
// glyphMatrix maps a glyph's own coordinates onto the page: the size, the
// stretch and the rise, then where the text is, then where the page is.
func (r *renderer) glyphMatrix(g *gstate) geometry.Matrix {
scale := geometry.Matrix{
Xx: g.text.size * g.text.scale, Yy: g.text.size,
Y0: g.text.rise,
}
return g.ctm.Mul(r.tm).Mul(scale)
}
// drawGlyph puts one glyph on the page.
func (r *renderer) drawGlyph(g *gstate, f *pdfFont, code int, resources reader.Dict) {
if f.Kind() == pdffont.Type3 {
r.drawType3Glyph(g, f, code, resources)
return
}
segs, ok := f.glyph(code)
if !ok || len(segs) == 0 {
return
}
m := r.glyphMatrix(g).Mul(geometry.Scale(1/f.perEm, 1/f.perEm))
if f.slant {
// Leaning the glyph rather than the line, so that the pen goes on
// where it was going.
m = m.Mul(geometry.Matrix{Xx: 1, Yy: 1, Yx: fauxSlant})
}
path := vector.NewPath()
at := func(p opentype.Point) geometry.Point {
return m.TransformPoint(geometry.Point{X: p.X, Y: p.Y})
}
for _, seg := range segs {
switch seg.Op {
case opentype.SegMoveTo:
p := at(seg.P[0])
path.MoveTo(p.X, p.Y)
case opentype.SegLineTo:
p := at(seg.P[0])
path.LineTo(p.X, p.Y)
case opentype.SegQuadTo:
c, p := at(seg.P[0]), at(seg.P[1])
path.QuadTo(c.X, c.Y, p.X, p.Y)
case opentype.SegClose:
path.Close()
}
}
r.paintGlyph(g, path, resources)
if f.embolden {
// A face carried in one weight is made bold by drawing its outline as
// well as filling it.
r.thicken(g, path, resources)
}
}
// thicken strokes a glyph's outline in the colour it was filled with, which is
// what makes a face carried in one weight read as bold.
func (r *renderer) thicken(g *gstate, path *vector.Path, resources reader.Dict) {
width := fauxBoldWidth * g.text.size * emScale(g)
if width <= 0 {
return
}
style := vector.StrokeStyle{Width: width, Cap: vector.RoundCap, Join: vector.RoundJoin}
if cov, ox, oy, w, h, ok := r.rz.StrokeWith(path, style, r.img.W, r.img.H); ok {
r.paintCoverage(g, g.fillPattern, cov, ox, oy, w, h, g.fill, g.fillAlpha, resources)
}
}
// emScale is how many device pixels an em of text space comes to, which is
// what a width given as a fraction of the em has to be multiplied by.
func emScale(g *gstate) float64 {
x := g.ctm.Xx*g.ctm.Xx + g.ctm.Xy*g.ctm.Xy
y := g.ctm.Yx*g.ctm.Yx + g.ctm.Yy*g.ctm.Yy
return math.Sqrt(math.Sqrt(x * y))
}
// paintGlyph fills or strokes a glyph, in whichever way the text state says.
func (r *renderer) paintGlyph(g *gstate, path *vector.Path, resources reader.Dict) {
if g.text.mode == modeFill || g.text.mode == modeFillStroke {
if cov, ox, oy, w, h, ok := r.rz.Fill(path, vector.NonZero, r.img.W, r.img.H); ok {
r.paintCoverage(g, g.fillPattern, cov, ox, oy, w, h, g.fill, g.fillAlpha, resources)
}
}
if g.text.mode == modeStroke || g.text.mode == modeFillStroke {
if cov, ox, oy, w, h, ok := r.rz.StrokeWith(path, r.strokeStyle(g), r.img.W, r.img.H); ok {
r.paintCoverage(g, g.strokePattern, cov, ox, oy, w, h, g.stroke, g.strokeAlpha, resources)
}
}
}
// drawType3Glyph runs the little content stream that is a Type 3 glyph.
func (r *renderer) drawType3Glyph(g *gstate, f *pdfFont, code int, resources reader.Dict) {
name, ok := f.GlyphName(code)
if !ok || f.CharProcs() == nil || r.depth >= maxFormDepth {
return
}
entry := f.CharProcs().Get(reader.Name(name))
stream, ok := reader.ToStream(resolve(r.doc, entry))
if !ok {
return
}
// A glyph procedure that is already being drawn is a cycle, and a cycle has
// no meaning: refuse it rather than let the depth bound turn it into 3^12
// executions. Only a procedure named by a reference can be recognised, and
// the number leaves the list on the way out, so showing the same glyph twice
// in sequence is still allowed.
if ref, isRef := entry.(reader.Ref); isRef {
for _, num := range r.drawingProcs {
if num == ref.Num {
return
}
}
r.drawingProcs = append(r.drawingProcs, ref.Num)
defer func() { r.drawingProcs = r.drawingProcs[:len(r.drawingProcs)-1] }()
}
content, img, err := r.salvaged(stream)
if err != nil || img != "" {
return
}
inner := g.clone()
inner.ctm = r.glyphMatrix(g).Mul(f.fontMatrix)
res := f.Type3Resources()
if res == nil {
res = resources
}
// A glyph draws in the state that showed it, and its own text position is
// its own business.
tm, tlm := r.tm, r.tlm
r.depth++
r.run(content, res, inner)
r.depth--
r.tm, r.tlm = tm, tlm
}
// font finds and caches the font a name stands for.
func (r *renderer) font(o reader.Object, resources reader.Dict) *pdfFont {
name, ok := reader.ToName(o)
if !ok {
return nil
}
entry := r.named(resources, "Font", name)
if entry == nil {
return nil
}
if ref, ok := entry.(reader.Ref); ok {
if f, ok := r.fonts[ref.Num]; ok {
return f
}
dict, ok := reader.ToDict(resolve(r.doc, entry))
if !ok {
return nil
}
f := r.loadFont(dict)
r.fonts[ref.Num] = f
return f
}
dict, ok := reader.ToDict(resolve(r.doc, entry))
if !ok {
return nil
}
return r.loadFont(dict)
}