-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.go
415 lines (375 loc) · 11.7 KB
/
text.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
// Copyright (c) 2018, The Goki Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package svg
import (
"image"
"goki.dev/colors"
"goki.dev/girl/paint"
"goki.dev/girl/styles"
"goki.dev/girl/units"
"goki.dev/mat32/v2"
)
// Text renders SVG text, handling both text and tspan elements.
// tspan is nested under a parent text -- text has empty Text string.
type Text struct {
NodeBase
// position of the left, baseline of the text
Pos mat32.Vec2 `xml:"{x,y}" set:"-"`
// width of text to render if using word-wrapping
Width float32 `xml:"width"`
// text string to render
Text string `xml:"text"`
// render version of text
TextRender paint.Text `xml:"-" json:"-"`
// character positions along X axis, if specified
CharPosX []float32
// character positions along Y axis, if specified
CharPosY []float32
// character delta-positions along X axis, if specified
CharPosDX []float32
// character delta-positions along Y axis, if specified
CharPosDY []float32
// character rotations, if specified
CharRots []float32
// author's computed text length, if specified -- we attempt to match
TextLength float32
// in attempting to match TextLength, should we adjust glyphs in addition to spacing?
AdjustGlyphs bool
// last text render position -- lower-left baseline of start
LastPos mat32.Vec2 `xml:"-" json:"-"`
// last actual bounding box in display units (dots)
LastBBox mat32.Box2 `xml:"-" json:"-"`
}
func (g *Text) SVGName() string {
if len(g.Text) == 0 {
return "text"
}
return "tspan"
}
func (g *Text) CopyFieldsFrom(frm any) {
fr := frm.(*Text)
g.NodeBase.CopyFieldsFrom(&fr.NodeBase)
g.Pos = fr.Pos
g.Width = fr.Width
g.Text = fr.Text
mat32.CopyFloat32s(&g.CharPosX, fr.CharPosX)
mat32.CopyFloat32s(&g.CharPosY, fr.CharPosY)
mat32.CopyFloat32s(&g.CharPosDX, fr.CharPosDX)
mat32.CopyFloat32s(&g.CharPosDY, fr.CharPosDY)
mat32.CopyFloat32s(&g.CharRots, fr.CharRots)
g.TextLength = fr.TextLength
g.AdjustGlyphs = fr.AdjustGlyphs
}
// IsParText returns true if this element serves as a parent text element
// to tspan elements within it. This is true if NumChildren() > 0 and
// Text == ""
func (g *Text) IsParText() bool {
return g.NumChildren() > 0 && g.Text == ""
}
func (g *Text) SetPos(pos mat32.Vec2) *Text {
g.Pos = pos
for _, kii := range g.Kids {
kt := kii.(*Text)
kt.Pos = g.Paint.Transform.MulVec2AsPt(pos)
}
return g
}
func (g *Text) SetSize(sz mat32.Vec2) *Text {
g.Width = sz.X
scx, _ := g.Paint.Transform.ExtractScale()
for _, kii := range g.Kids {
kt := kii.(*Text)
kt.Width = g.Width * scx
}
return g
}
func (g *Text) NodeBBox(sv *SVG) image.Rectangle {
if g.IsParText() {
return BBoxFromChildren(g)
} else {
return image.Rectangle{Min: g.LastBBox.Min.ToPointFloor(), Max: g.LastBBox.Max.ToPointCeil()}
}
}
// TextBBox returns the bounding box in local coordinates
func (g *Text) TextBBox() mat32.Box2 {
if g.Text == "" {
return mat32.Box2{}
}
pc := &g.Paint
pc.FontStyle.Font = paint.OpenFont(&pc.FontStyle, &pc.UnContext) // use original size font
g.TextRender.SetString(g.Text, &pc.FontStyle, &pc.UnContext, &pc.TextStyle, true, 0, 1)
sr := &(g.TextRender.Spans[0])
sr.Render[0].Face = pc.FontStyle.Face.Face // upscale
pos := g.Pos
if pc.TextStyle.Align == styles.Center || pc.TextStyle.Anchor == styles.AnchorMiddle {
pos.X -= g.TextRender.Size.X * .5
} else if pc.TextStyle.Align == styles.End || pc.TextStyle.Anchor == styles.AnchorEnd {
pos.X -= g.TextRender.Size.X
}
if len(g.CharPosX) > 0 {
mx := min(len(g.CharPosX), len(sr.Render))
for i := 0; i < mx; i++ {
sr.Render[i].RelPos.X = g.CharPosX[i]
}
}
if len(g.CharPosY) > 0 {
mx := min(len(g.CharPosY), len(sr.Render))
for i := 0; i < mx; i++ {
sr.Render[i].RelPos.Y = g.CharPosY[i]
}
}
if len(g.CharPosDX) > 0 {
mx := min(len(g.CharPosDX), len(sr.Render))
for i := 0; i < mx; i++ {
if i > 0 {
sr.Render[i].RelPos.X = sr.Render[i-1].RelPos.X + g.CharPosDX[i]
} else {
sr.Render[i].RelPos.X = g.CharPosDX[i] // todo: not sure this is right
}
}
}
if len(g.CharPosDY) > 0 {
mx := min(len(g.CharPosDY), len(sr.Render))
for i := 0; i < mx; i++ {
if i > 0 {
sr.Render[i].RelPos.Y = sr.Render[i-1].RelPos.Y + g.CharPosDY[i]
} else {
sr.Render[i].RelPos.Y = g.CharPosDY[i] // todo: not sure this is right
}
}
}
// todo: TextLength, AdjustGlyphs -- also svg2 at least supports word wrapping!
// accumulate final bbox
sz := mat32.Vec2{}
maxh := float32(0)
for i := range sr.Render {
mxp := sr.Render[i].RelPos.Add(sr.Render[i].Size)
sz.SetMax(mxp)
maxh = mat32.Max(maxh, sr.Render[i].Size.Y)
}
bb := mat32.Box2{}
bb.Min = pos
bb.Min.Y -= maxh * .8 // baseline adjust
bb.Max = bb.Min.Add(g.TextRender.Size)
return bb
}
// RenderText renders the text in full coords
func (g *Text) RenderText(sv *SVG) {
pc := &paint.Context{&sv.RenderState, &g.Paint}
orgsz := pc.FontStyle.Size
pos := pc.CurTransform.MulVec2AsPt(mat32.V2(g.Pos.X, g.Pos.Y))
rot := pc.CurTransform.ExtractRot()
scx, scy := pc.CurTransform.ExtractScale()
scalex := scx / scy
if scalex == 1 {
scalex = 0
}
pc.FontStyle.Font = paint.OpenFont(&pc.FontStyle, &pc.UnContext) // use original size font
if pc.FillStyle.Color != nil {
pc.FontStyle.Color = colors.ToUniform(pc.FillStyle.Color)
}
g.TextRender.SetString(g.Text, &pc.FontStyle, &pc.UnContext, &pc.TextStyle, true, rot, scalex)
pc.FontStyle.Size = units.Value{Val: orgsz.Val * scy, Un: orgsz.Un, Dots: orgsz.Dots * scy} // rescale by y
pc.FontStyle.Font = paint.OpenFont(&pc.FontStyle, &pc.UnContext)
sr := &(g.TextRender.Spans[0])
sr.Render[0].Face = pc.FontStyle.Face.Face // upscale
g.TextRender.Size = g.TextRender.Size.Mul(mat32.V2(scx, scy))
// todo: align styling only affects multi-line text and is about how tspan is arranged within
// the overall text block.
if pc.TextStyle.Align == styles.Center || pc.TextStyle.Anchor == styles.AnchorMiddle {
pos.X -= g.TextRender.Size.X * .5
} else if pc.TextStyle.Align == styles.End || pc.TextStyle.Anchor == styles.AnchorEnd {
pos.X -= g.TextRender.Size.X
}
for i := range sr.Render {
sr.Render[i].RelPos = pc.CurTransform.MulVec2AsVec(sr.Render[i].RelPos)
sr.Render[i].Size.Y *= scy
sr.Render[i].Size.X *= scx
}
pc.FontStyle.Size = orgsz
if len(g.CharPosX) > 0 {
mx := min(len(g.CharPosX), len(sr.Render))
for i := 0; i < mx; i++ {
// todo: this may not be fully correct, given relativity constraints
cpx := pc.CurTransform.MulVec2AsVec(mat32.V2(g.CharPosX[i], 0))
sr.Render[i].RelPos.X = cpx.X
}
}
if len(g.CharPosY) > 0 {
mx := min(len(g.CharPosY), len(sr.Render))
for i := 0; i < mx; i++ {
cpy := pc.CurTransform.MulVec2AsPt(mat32.V2(g.CharPosY[i], 0))
sr.Render[i].RelPos.Y = cpy.Y
}
}
if len(g.CharPosDX) > 0 {
mx := min(len(g.CharPosDX), len(sr.Render))
for i := 0; i < mx; i++ {
dx := pc.CurTransform.MulVec2AsVec(mat32.V2(g.CharPosDX[i], 0))
if i > 0 {
sr.Render[i].RelPos.X = sr.Render[i-1].RelPos.X + dx.X
} else {
sr.Render[i].RelPos.X = dx.X // todo: not sure this is right
}
}
}
if len(g.CharPosDY) > 0 {
mx := min(len(g.CharPosDY), len(sr.Render))
for i := 0; i < mx; i++ {
dy := pc.CurTransform.MulVec2AsVec(mat32.V2(g.CharPosDY[i], 0))
if i > 0 {
sr.Render[i].RelPos.Y = sr.Render[i-1].RelPos.Y + dy.Y
} else {
sr.Render[i].RelPos.Y = dy.Y // todo: not sure this is right
}
}
}
// todo: TextLength, AdjustGlyphs -- also svg2 at least supports word wrapping!
// accumulate final bbox
sz := mat32.Vec2{}
maxh := float32(0)
for i := range sr.Render {
mxp := sr.Render[i].RelPos.Add(sr.Render[i].Size)
sz.SetMax(mxp)
maxh = mat32.Max(maxh, sr.Render[i].Size.Y)
}
g.TextRender.Size = sz
g.LastPos = pos
g.LastBBox.Min = pos
g.LastBBox.Min.Y -= maxh * .8 // baseline adjust
g.LastBBox.Max = g.LastBBox.Min.Add(g.TextRender.Size)
g.TextRender.Render(pc, pos)
g.BBoxes(sv)
}
func (g *Text) LocalBBox() mat32.Box2 {
return g.TextBBox()
}
func (g *Text) Render(sv *SVG) {
if g.IsParText() {
pc := &g.Paint
rs := &sv.RenderState
rs.PushTransformLock(pc.Transform)
g.RenderChildren(sv)
g.BBoxes(sv) // must come after render
rs.PopTransformLock()
} else {
vis, rs := g.PushTransform(sv)
if !vis {
return
}
if len(g.Text) > 0 {
rs.Lock()
g.RenderText(sv)
rs.Unlock()
}
g.RenderChildren(sv)
if g.IsParText() {
g.BBoxes(sv) // after kids have rendered
}
rs.PopTransformLock()
}
}
// ApplyTransform applies the given 2D transform to the geometry of this node
// each node must define this for itself
func (g *Text) ApplyTransform(sv *SVG, xf mat32.Mat2) {
rot := xf.ExtractRot()
if rot != 0 || !g.Paint.Transform.IsIdentity() {
g.Paint.Transform = g.Paint.Transform.Mul(xf)
g.SetProp("transform", g.Paint.Transform.String())
} else {
if g.IsParText() {
for _, kii := range g.Kids {
kt := kii.(*Text)
kt.ApplyTransform(sv, xf)
}
} else {
g.Pos = xf.MulVec2AsPt(g.Pos)
scx, _ := xf.ExtractScale()
g.Width *= scx
g.GradientApplyTransform(sv, xf)
}
}
}
// ApplyDeltaTransform applies the given 2D delta transforms to the geometry of this node
// relative to given point. Trans translation and point are in top-level coordinates,
// so must be transformed into local coords first.
// Point is upper left corner of selection box that anchors the translation and scaling,
// and for rotation it is the center point around which to rotate
func (g *Text) ApplyDeltaTransform(sv *SVG, trans mat32.Vec2, scale mat32.Vec2, rot float32, pt mat32.Vec2) {
crot := g.Paint.Transform.ExtractRot()
if rot != 0 || crot != 0 {
xf, lpt := g.DeltaTransform(trans, scale, rot, pt, false) // exclude self
mat := g.Paint.Transform.MulCtr(xf, lpt)
g.Paint.Transform = mat
g.SetProp("transform", g.Paint.Transform.String())
} else {
if g.IsParText() {
// translation transform
xft, lptt := g.DeltaTransform(trans, scale, rot, pt, true) // include self when not a parent
// transform transform
xf, lpt := g.DeltaTransform(trans, scale, rot, pt, false)
xf.X0 = 0 // negate translation effects
xf.Y0 = 0
mat := g.Paint.Transform.MulCtr(xf, lpt)
g.Paint.Transform = mat
g.SetProp("transform", g.Paint.Transform.String())
g.Pos = xft.MulVec2AsPtCtr(g.Pos, lptt)
scx, _ := xft.ExtractScale()
g.Width *= scx
for _, kii := range g.Kids {
kt := kii.(*Text)
kt.Pos = xft.MulVec2AsPtCtr(kt.Pos, lptt)
kt.Width *= scx
}
} else {
xf, lpt := g.DeltaTransform(trans, scale, rot, pt, true) // include self when not a parent
g.Pos = xf.MulVec2AsPtCtr(g.Pos, lpt)
scx, _ := xf.ExtractScale()
g.Width *= scx
}
}
}
// WriteGeom writes the geometry of the node to a slice of floating point numbers
// the length and ordering of which is specific to each node type.
// Slice must be passed and will be resized if not the correct length.
func (g *Text) WriteGeom(sv *SVG, dat *[]float32) {
if g.IsParText() {
npt := 9 + g.NumChildren()*3
SetFloat32SliceLen(dat, npt)
(*dat)[0] = g.Pos.X
(*dat)[1] = g.Pos.Y
(*dat)[2] = g.Width
g.WriteTransform(*dat, 3)
for i, kii := range g.Kids {
kt := kii.(*Text)
off := 9 + i*3
(*dat)[off+0] = kt.Pos.X
(*dat)[off+1] = kt.Pos.Y
(*dat)[off+2] = kt.Width
}
} else {
SetFloat32SliceLen(dat, 3+6)
(*dat)[0] = g.Pos.X
(*dat)[1] = g.Pos.Y
(*dat)[2] = g.Width
g.WriteTransform(*dat, 3)
}
}
// ReadGeom reads the geometry of the node from a slice of floating point numbers
// the length and ordering of which is specific to each node type.
func (g *Text) ReadGeom(sv *SVG, dat []float32) {
g.Pos.X = dat[0]
g.Pos.Y = dat[1]
g.Width = dat[2]
g.ReadTransform(dat, 3)
if g.IsParText() {
for i, kii := range g.Kids {
kt := kii.(*Text)
off := 9 + i*3
kt.Pos.X = dat[off+0]
kt.Pos.Y = dat[off+1]
kt.Width = dat[off+2]
}
}
}