-
Notifications
You must be signed in to change notification settings - Fork 1
/
text.go
1357 lines (1197 loc) · 37 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package ui
import (
"encoding"
"fmt"
"math"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/axe/axe-go/pkg/color"
"github.com/axe/axe-go/pkg/ease"
"github.com/axe/axe-go/pkg/gfx"
"github.com/axe/axe-go/pkg/id"
"github.com/axe/axe-go/pkg/util"
)
type FontRune struct {
// The extends around the baseline point at the start of the font rune
ExtentTile
// The advance width of the rune
Width float32
}
type Font struct {
// The name of the font
Name string
// The texture of the font
Texture string
// The lineheight relative to the font size. 1.0=matches font-size
LineHeight float32
// Within a line how far down is the baseline from the top relative to the font size.
Baseline float32
// Data about each rune in the font
Runes map[rune]FontRune
// Kerning data when run B follows rune A
Kerning map[rune]map[rune]float32
}
func (f Font) GetKerning(prev rune, next rune) float32 {
kerningMap := f.Kerning[prev]
if kerningMap != nil {
if kerning, ok := kerningMap[next]; ok {
return kerning
}
}
return 0
}
type TextWrap string
const (
TextWrapNone TextWrap = "none"
TextWrapWord TextWrap = "word"
TextWrapChar TextWrap = "char"
)
func (w TextWrap) MarshalText() ([]byte, error) {
return []byte(w), nil
}
func (w *TextWrap) UnmarshalText(text []byte) error {
s := strings.ToLower(string(text))
switch s {
case "none", "n", "":
*w = TextWrapNone
case "word", "w":
*w = TextWrapWord
case "char", "c", "letter":
*w = TextWrapChar
default:
return fmt.Errorf("invalid text wrap: " + s)
}
return nil
}
type Alignment float32
const (
AlignmentTop Alignment = 0
AlignmentLeft Alignment = 0
AlignmentMiddle Alignment = 0.5
AlignmentCenter Alignment = 0.5
AlignmentRight Alignment = 1
AlignmentBottom Alignment = 1
)
var _ encoding.TextMarshaler = Alignment(0)
var _ encoding.TextUnmarshaler = new(Alignment)
func (a Alignment) Compute(span float32) float32 {
return float32(a) * span
}
func (a Alignment) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%3f", float32(a))), nil
}
func (a *Alignment) UnmarshalText(text []byte) error {
s := strings.ToLower(string(text))
switch s {
case "top", "t", "left", "l":
*a = 0
case "bottom", "b", "right", "r":
*a = 1
case "middle", "m", "center", "c":
*a = 0.5
default:
multiplier := float64(1)
if strings.HasSuffix(s, "%") {
s = strings.TrimSuffix(s, "%")
multiplier = 0.01
}
asFloat, err := strconv.ParseFloat(s, 32)
if err != nil {
return err
}
*a = Alignment(asFloat * multiplier)
}
return nil
}
type ClipShow int
const (
ClipShowNone ClipShow = iota
ClipShowLeft
ClipShowRight
ClipShowTop
ClipShowBottom
)
func (a ClipShow) MarshalText() ([]byte, error) {
switch a {
case ClipShowNone:
return []byte{}, nil
case ClipShowLeft:
return []byte("left"), nil
case ClipShowTop:
return []byte("top"), nil
case ClipShowRight:
return []byte("right"), nil
case ClipShowBottom:
return []byte("bottom"), nil
}
return nil, fmt.Errorf("invalid clip show %d", a)
}
func (c *ClipShow) UnmarshalText(text []byte) error {
s := strings.ToLower(string(text))
switch s {
case "none", "":
*c = ClipShowNone
case "top", "t":
*c = ClipShowTop
case "left", "l":
*c = ClipShowLeft
case "bottom", "b":
*c = ClipShowBottom
case "right", "r":
*c = ClipShowRight
}
return fmt.Errorf("invalid clip show %s", string(text))
}
type ParagraphStyles struct {
Spacing Amount // extra space between glyphs TODO
LineSpacing Amount // extra space between lines
LineHeight Amount // 0=calculated per line
LineVerticalAlignment Alignment // 0=top, 0.5=middle, 1=bottom
Indent Amount // shift first line in a wrappable line
HorizontalAlignment Alignment // 0=left, 0.5=middle, 1=right
Wrap TextWrap // when we are going beyond the width, how do we decide to wrap
ParagraphPadding AmountBounds // padding around the paragraph
}
func (s *ParagraphStyles) Override(o *ParagraphStylesOverride) *ParagraphStyles {
if !o.HasOverride() {
return s
}
return &ParagraphStyles{
Spacing: util.Coalesce(o.Spacing, s.Spacing),
LineSpacing: util.Coalesce(o.LineSpacing, s.LineSpacing),
LineHeight: util.Coalesce(o.LineHeight, s.LineHeight),
LineVerticalAlignment: util.Coalesce(o.LineVerticalAlignment, s.LineVerticalAlignment),
Indent: util.Coalesce(o.Indent, s.Indent),
HorizontalAlignment: util.Coalesce(o.HorizontalAlignment, s.HorizontalAlignment),
Wrap: util.Coalesce(o.Wrap, s.Wrap),
ParagraphPadding: util.Coalesce(o.ParagraphPadding, s.ParagraphPadding),
}
}
type ParagraphStylesOverride struct {
Spacing *Amount // extra space between glyphs
LineSpacing *Amount // extra space between lines
LineHeight *Amount // 0=calculated per line
LineVerticalAlignment *Alignment // 0=top, 0.5=middle, 1=bottom
Indent *Amount // shift first line in a wrappable line
HorizontalAlignment *Alignment // 0=left, 0.5=middle, 1=right
Wrap *TextWrap // when we are going beyond the width, how do we decide to wrap
ParagraphPadding *AmountBounds // padding around the paragraph
}
func (o *ParagraphStylesOverride) HasOverride() bool {
if o == nil {
return false
}
return o.Spacing != nil || o.LineSpacing != nil || o.LineHeight != nil || o.LineVerticalAlignment != nil ||
o.Indent != nil || o.HorizontalAlignment != nil || o.Wrap != nil || o.ParagraphPadding != nil
}
func (o *ParagraphStylesOverride) Clone() *ParagraphStylesOverride {
if o == nil {
return nil
}
return &ParagraphStylesOverride{
Spacing: util.Clone(o.Spacing),
LineSpacing: util.Clone(o.LineSpacing),
LineHeight: util.Clone(o.LineHeight),
LineVerticalAlignment: util.Clone(o.LineVerticalAlignment),
Indent: util.Clone(o.Indent),
HorizontalAlignment: util.Clone(o.HorizontalAlignment),
Wrap: util.Clone(o.Wrap),
ParagraphPadding: util.Clone(o.ParagraphPadding),
}
}
type ParagraphsStyles struct {
ParagraphSpacing Amount // how much space between paragraphs
VerticalAlignment Alignment // how to align all paragraphs in an area
ClipShowX ClipShow // when text cannot fit in the area width, which side should we prefer to show (left, right, none)
ClipShowY ClipShow // when text cannot fit in the area height, which side should we prefer to show (top, bottom, none)
}
func (s *ParagraphsStyles) Override(o *ParagraphsStylesOverride) *ParagraphsStyles {
if !o.HasOverride() {
return s
}
return &ParagraphsStyles{
ParagraphSpacing: util.Coalesce(o.ParagraphSpacing, s.ParagraphSpacing),
VerticalAlignment: util.Coalesce(o.VerticalAlignment, s.VerticalAlignment),
ClipShowX: util.Coalesce(o.ClipShowX, s.ClipShowX),
ClipShowY: util.Coalesce(o.ClipShowY, s.ClipShowY),
}
}
type ParagraphsStylesOverride struct {
ParagraphSpacing *Amount // how much space between paragraphs
VerticalAlignment *Alignment // how to align all paragraphs in an area
ClipShowX *ClipShow // when text cannot fit in the area width, which side should we prefer to show (left, right, none)
ClipShowY *ClipShow // when text cannot fit in the area height, which side should we prefer to show (top, bottom, none)
}
func (o *ParagraphsStylesOverride) HasOverride() bool {
if o == nil {
return false
}
return o.ParagraphSpacing != nil || o.VerticalAlignment != nil || o.ClipShowX != nil || o.ClipShowY != nil
}
type TextStyles struct {
ParagraphStyles
ParagraphsStyles
Color color.Able
Font id.Identifier
FontSize Amount
}
func (s *TextStyles) Override(o *TextStylesOverride) *TextStyles {
if !o.HasOverride() {
return s
}
return &TextStyles{
ParagraphStyles: *s.ParagraphStyles.Override(o.ParagraphStylesOverride),
ParagraphsStyles: *s.ParagraphsStyles.Override(o.ParagraphsStylesOverride),
Color: util.Coalesce(o.Color, s.Color),
Font: util.Coalesce(o.Font, s.Font),
FontSize: util.Coalesce(o.FontSize, s.FontSize),
}
}
type TextStylesOverride struct {
ParagraphStylesOverride *ParagraphStylesOverride
ParagraphsStylesOverride *ParagraphsStylesOverride
Color *color.Able
Font *id.Identifier
FontSize *Amount
}
func (o *TextStylesOverride) HasOverride() bool {
if o == nil {
return false
}
return o.ParagraphStylesOverride.HasOverride() || o.ParagraphStylesOverride.HasOverride() ||
o.Color != nil || o.Font != nil || o.FontSize != nil
}
type Paragraph struct {
Styles *ParagraphStylesOverride
Glyphs []Glyph
}
type Paragraphs struct {
Styles *ParagraphsStylesOverride
Paragraphs []Paragraph
MaxWidth float32
MaxHeight float32
KeepEmpty bool
}
type RenderedText struct {
Bounds Bounds
Glyphs []RenderedGlyph
Paragraphs Paragraphs
}
func (text RenderedText) UpdateVisibility(visibleBounds Bounds) {
for i := range text.Glyphs {
g := &text.Glyphs[i]
if visibleBounds.Contains(g.Bounds) {
g.Visibility = GlyphVisibilityVisible
} else if visibleBounds.Intersects(g.Bounds) {
g.Visibility = GlyphVisibilityPartial
} else {
g.Visibility = GlyphVisibilityInvisible
}
}
}
func (text RenderedText) CountVisible(threshold *GlyphVisibility) int {
visible := 0
for i := range text.Glyphs {
g := &text.Glyphs[i]
if g.Empty || (threshold != nil && g.Visibility > *threshold) {
continue
}
visible++
}
return visible
}
func (text RenderedText) Closest(x, y float32) int {
closest := -1
closestDistanceSq := float32(0)
for index, glyph := range text.Glyphs {
cx, cy := glyph.Bounds.Center()
distanceSq := LengthSq(cx-x, cy-y)
if closest == -1 || distanceSq < closestDistanceSq {
closest = index
closestDistanceSq = distanceSq
}
}
return closest
}
func (text RenderedText) ClosestByLine(x, y float32) int {
closest := -1
closestY := float32(math.MaxFloat32)
closestX := float32(math.MaxFloat32)
closestLine := -1
for index, glyph := range text.Glyphs {
cx, cy := glyph.Bounds.Closest(x, y)
dy := util.Abs(cy - y)
if dy <= closestY || closestLine == glyph.Line {
dx := util.Abs(cx - x)
if dx < closestX || closestLine != glyph.Line {
closest = index
closestLine = glyph.Line
closestY = dy
closestX = dx
}
}
}
return closest
}
type RenderedGlyph struct {
gfx.Tile
Bounds Bounds
Color color.Color
Visibility GlyphVisibility
Line int
Column int
Word int
Paragraph int
ParagraphIndex int
Index int
Empty bool
}
func (g RenderedGlyph) Write(quad []Vertex) {
quad[0] = Vertex{X: g.Bounds.Left, Y: g.Bounds.Top, Tex: g.Coord(0, 0), HasCoord: true, Color: g.Color, HasColor: true}
quad[1] = Vertex{X: g.Bounds.Right, Y: g.Bounds.Top, Tex: g.Coord(1, 0), HasCoord: true, Color: g.Color, HasColor: true}
quad[2] = Vertex{X: g.Bounds.Right, Y: g.Bounds.Bottom, Tex: g.Coord(1, 1), HasCoord: true, Color: g.Color, HasColor: true}
quad[3] = Vertex{X: g.Bounds.Left, Y: g.Bounds.Bottom, Tex: g.Coord(0, 1), HasCoord: true, Color: g.Color, HasColor: true}
}
type GlyphVisibility int
const (
GlyphVisibilityVisible GlyphVisibility = iota
GlyphVisibilityPartial
GlyphVisibilityInvisible
)
type Glyph interface {
GetState(ctx *RenderContext, wrap TextWrap, prev Glyph) GlyphState
Render(ctx *RenderContext, start gfx.Coord, base *Base) RenderedGlyph
String() string
}
type GlyphState struct {
Size gfx.Coord
CanBreak bool
ShouldBreak bool
Empty bool
}
func (paragraph Paragraph) GetLineHeight(ctx *RenderContext, style *ParagraphStyles, actualLineHeight float32) float32 {
if style.LineHeight.IsZero() {
return actualLineHeight
} else {
return style.LineHeight.Get(ctx.AmountContext, false)
}
}
func (paragraph Paragraph) GetStates(ctx *RenderContext, style *ParagraphStyles) []GlyphState {
states := make([]GlyphState, len(paragraph.Glyphs))
var prev Glyph
for i, g := range paragraph.Glyphs {
states[i] = g.GetState(ctx, style.Wrap, prev)
prev = paragraph.Glyphs[i]
}
return states
}
const (
MeasureWidthRoundingError = 0.001
MeasureHeightRoundingError = 0.001
)
func (paragraph Paragraph) MinWidth(ctx *RenderContext, keepEmpty bool) float32 {
style := ctx.TextStyles.ParagraphStyles.Override(paragraph.Styles)
states := paragraph.GetStates(ctx, style)
lineIndent := style.Indent.Get(ctx.AmountContext, true)
kerning := style.Spacing.Get(ctx.AmountContext, true)
lineWidth := lineIndent
maxWidth := float32(0)
for _, state := range states {
if state.ShouldBreak || state.CanBreak {
maxWidth = util.Max(maxWidth, lineWidth-kerning)
lineWidth = 0
if state.Empty && !keepEmpty {
continue
}
}
lineWidth += state.Size.X + kerning
}
maxWidth = util.Max(maxWidth, lineWidth-kerning)
minWidth := maxWidth
minWidth += style.ParagraphPadding.Left.Get(ctx.AmountContext, true)
minWidth += style.ParagraphPadding.Right.Get(ctx.AmountContext, true)
minWidth += MeasureWidthRoundingError
return minWidth
}
func (paragraph Paragraph) String() string {
out := strings.Builder{}
out.Grow(len(paragraph.Glyphs))
for _, glyph := range paragraph.Glyphs {
out.WriteString(glyph.String())
}
return out.String()
}
type paragraphLine struct {
start, endExclusive int
width, height float32
indent float32
}
type paragraphLines struct {
lines []paragraphLine
style *ParagraphStyles
states []GlyphState
padding Bounds
lineSpacing float32
totalHeight float32
maxWidth float32
kerning float32
}
func (paragraph Paragraph) getLines(ctx *RenderContext, paragraphs Paragraphs) paragraphLines {
lines := make([]paragraphLine, 0, 8)
style := ctx.TextStyles.ParagraphStyles.Override(paragraph.Styles)
states := paragraph.GetStates(ctx, style)
lineSpacing := style.LineSpacing.Get(ctx.AmountContext, false)
lineIndent := style.Indent.Get(ctx.AmountContext, true)
kerning := style.Spacing.Get(ctx.AmountContext, true)
padding := style.ParagraphPadding.GetBounds(ctx.AmountContext)
paddingWidth := padding.Left + padding.Right
lastBreak := -1
glyphLast := len(paragraph.Glyphs) - 1
currentLine := paragraphLine{width: paddingWidth + lineIndent, indent: lineIndent}
endLine := func(nextLineWidth float32, nextLineStart int) {
currentLine.endExclusive = nextLineStart
currentLine.width -= nextLineWidth
if !paragraphs.KeepEmpty {
for i := currentLine.endExclusive - 1; i >= currentLine.start; i-- {
if states[i].Empty {
currentLine.width -= states[i].Size.X + kerning
currentLine.endExclusive--
} else {
break
}
}
}
lines = append(lines, currentLine)
currentLine = paragraphLine{
start: nextLineStart,
endExclusive: nextLineStart,
width: paddingWidth + nextLineWidth,
}
lastBreak = -1
}
for glyphIndex := 0; glyphIndex <= glyphLast; glyphIndex++ {
state := states[glyphIndex]
if state.ShouldBreak {
endLine(0, glyphIndex)
if state.Empty && !paragraphs.KeepEmpty {
continue
}
} else if paragraphs.Wrap(currentLine.width+state.Size.X) && lastBreak != -1 {
// Wrap everything after lastBreak
endLineAt := lastBreak + 1
// Glyphs moving to next line
nextLineWidth := float32(0)
for k := endLineAt; k < glyphIndex; k++ {
nextLineWidth += states[k].Size.X + kerning
}
// End line and start next
endLine(nextLineWidth, endLineAt)
}
currentLine.width += state.Size.X + kerning
currentLine.endExclusive++
if state.CanBreak {
lastBreak = glyphIndex
}
}
if currentLine.start != currentLine.endExclusive {
endLine(0, glyphLast+1)
}
totalHeight := float32(0)
maxWidth := float32(0)
for lineIndex := range lines {
line := &lines[lineIndex]
actualLineHeight := float32(0)
for k := line.start; k < line.endExclusive; k++ {
actualLineHeight = util.Max(actualLineHeight, states[k].Size.Y)
}
line.height = paragraph.GetLineHeight(ctx, style, actualLineHeight)
totalHeight += line.height
maxWidth = util.Max(maxWidth, line.width)
}
totalHeight += lineSpacing * float32(len(lines)-1)
totalHeight += padding.Top + padding.Bottom
return paragraphLines{
lines: lines,
style: style,
states: states,
padding: padding,
kerning: kerning,
lineSpacing: lineSpacing,
maxWidth: maxWidth,
totalHeight: totalHeight,
}
}
func (paragraph Paragraph) Measure(ctx *RenderContext, paragraphs Paragraphs) gfx.Coord {
size := gfx.Coord{}
lines := paragraph.getLines(ctx, paragraphs)
size.X = lines.maxWidth + MeasureWidthRoundingError
size.Y = lines.totalHeight + MeasureHeightRoundingError
return size
}
func (paragraph Paragraph) Render(ctx *RenderContext, paragraphs Paragraphs, b *Base) RenderedText {
lines := paragraph.getLines(ctx, paragraphs)
bounds := Bounds{
Left: 0,
Top: 0,
Right: 0,
Bottom: lines.totalHeight,
}
rendered := make([]RenderedGlyph, 0, len(paragraph.Glyphs))
offsetY := lines.padding.Top
maxWidth := util.Max(0, paragraphs.MaxWidth)
wordCount := 0
for lineIndex, line := range lines.lines {
start := gfx.Coord{
X: lines.style.HorizontalAlignment.Compute(maxWidth - line.width),
Y: offsetY,
}
bounds.Left = util.Min(bounds.Left, start.X)
bounds.Right = util.Max(bounds.Right, start.X+line.width)
start.X += line.indent + lines.padding.Left
for k := line.start; k < line.endExclusive; k++ {
g := paragraph.Glyphs[k]
s := lines.states[k]
start.Y = offsetY + lines.style.LineVerticalAlignment.Compute(line.height-s.Size.Y)
render := g.Render(ctx, start, b)
render.Line = lineIndex
render.Column = k - line.start
render.Word = wordCount
render.Index = k
render.ParagraphIndex = k
render.Empty = s.Empty || render.Bounds.Width() <= 0
if !render.Empty || paragraphs.KeepEmpty {
rendered = append(rendered, render)
} else if s.CanBreak || s.ShouldBreak {
wordCount++
}
start.X += s.Size.X + lines.kerning
}
offsetY += line.height + lines.lineSpacing
}
return RenderedText{Bounds: bounds, Glyphs: rendered, Paragraphs: paragraphs}
}
func (paragraph *RenderedText) Translate(x, y float32) {
paragraph.Bounds.Translate(x, y)
for i := range paragraph.Glyphs {
paragraph.Glyphs[i].Bounds.Translate(x, y)
}
}
func (paragraphs Paragraphs) String() string {
return paragraphs.ToString("\n")
}
func (paragraphs Paragraphs) ToString(paragraphSeparator string) string {
paras := make([]string, len(paragraphs.Paragraphs))
for i, para := range paragraphs.Paragraphs {
paras[i] = para.String()
}
return strings.Join(paras, paragraphSeparator)
}
func (paragraphs Paragraphs) Wrap(lineWidth float32) bool {
return paragraphs.MaxWidth > 0 && lineWidth > paragraphs.MaxWidth-MeasureWidthRoundingError
}
func (paragraphs Paragraphs) MinWidth(ctx *RenderContext) float32 {
paragraphCtx := ctx.WithParent(util.Max(0, paragraphs.MaxWidth), paragraphs.MaxHeight)
minWidth := float32(0)
for _, para := range paragraphs.Paragraphs {
minWidth = util.Max(minWidth, para.MinWidth(paragraphCtx, paragraphs.KeepEmpty))
}
return minWidth
}
func (paragraphs Paragraphs) Measure(ctx *RenderContext) gfx.Coord {
style := ctx.TextStyles.ParagraphsStyles.Override(paragraphs.Styles)
size := gfx.Coord{}
paragraphCtx := ctx.WithParent(util.Max(0, paragraphs.MaxWidth), paragraphs.MaxHeight)
paragraphSpacing := style.ParagraphSpacing.Get(paragraphCtx.AmountContext, false)
for _, paragraph := range paragraphs.Paragraphs {
paragraphSize := paragraph.Measure(paragraphCtx, paragraphs)
size.Y += paragraphSize.Y
size.X = util.Max(size.X, paragraphSize.X)
}
size.Y += paragraphSpacing * float32(len(paragraphs.Paragraphs)-1)
return size
}
func (paragraphs Paragraphs) Render(ctx *RenderContext, b *Base) RenderedText {
style := ctx.TextStyles.ParagraphsStyles.Override(paragraphs.Styles)
rendered := make([]RenderedText, len(paragraphs.Paragraphs))
totalHeight := float32(0)
totalGlyphs := 0
paragraphCtx := ctx.WithParent(util.Max(0, paragraphs.MaxWidth), paragraphs.MaxHeight)
paragraphSpacing := style.ParagraphSpacing.Get(paragraphCtx.AmountContext, false)
for i, paragraph := range paragraphs.Paragraphs {
rendered[i] = paragraph.Render(paragraphCtx, paragraphs, b)
totalHeight += rendered[i].Bounds.Height()
totalGlyphs += len(rendered[i].Glyphs)
}
totalHeight += paragraphSpacing * float32(len(paragraphs.Paragraphs)-1)
offsetY := style.VerticalAlignment.Compute(paragraphs.MaxHeight - totalHeight)
switch style.ClipShowY {
case ClipShowTop:
if offsetY < 0 {
offsetY = 0
}
case ClipShowBottom:
if totalHeight > paragraphs.MaxHeight {
offsetY = paragraphs.MaxHeight - totalHeight
}
}
joinedBounds := Bounds{}
for i := range rendered {
paragraph := &rendered[i]
offsetX := float32(0)
switch style.ClipShowX {
case ClipShowLeft:
if paragraph.Bounds.Left < 0 {
offsetX = -paragraph.Bounds.Left
}
case ClipShowRight:
if paragraphs.MaxWidth > 0 && paragraph.Bounds.Right > paragraphs.MaxWidth {
offsetX = paragraph.Bounds.Right - paragraphs.MaxWidth
}
}
if offsetY != 0 || offsetX != 0 {
paragraph.Translate(offsetX, offsetY)
}
offsetY += paragraph.Bounds.Height() + paragraphSpacing
joinedBounds = joinedBounds.Union(paragraph.Bounds)
}
joined := RenderedText{
Bounds: joinedBounds,
Glyphs: make([]RenderedGlyph, 0, totalGlyphs),
Paragraphs: paragraphs,
}
lineOffset := 0
wordOffset := 0
indexOffset := 0
for paragraphIndex, paragraph := range rendered {
n := len(paragraph.Glyphs) - 1
if n == -1 {
continue
}
for i := 0; i <= n; i++ {
g := ¶graph.Glyphs[i]
g.Line += lineOffset
g.Word += wordOffset
g.Index += indexOffset
g.Paragraph = paragraphIndex
}
joined.Glyphs = append(joined.Glyphs, paragraph.Glyphs...)
last := paragraph.Glyphs[n]
lineOffset = last.Line + 1
wordOffset = last.Word + 1
indexOffset = last.Index + 1
}
return joined
}
type BaseGlyph struct {
Text rune
Font id.Identifier
Size Amount
Color color.Color
initialized bool
font *Font
fontRune *FontRune
}
var _ Glyph = &BaseGlyph{}
func (g *BaseGlyph) init(ctx *RenderContext) {
if !g.initialized {
font := ctx.Theme.Fonts.Get(g.Font)
if font == nil {
font = ctx.Theme.Fonts.Get(ctx.TextStyles.Font)
}
if font != nil {
fontRune := font.Runes[g.Text]
if fontRune.Width != 0 {
g.font = font
g.fontRune = &fontRune
}
}
g.initialized = true
}
}
func (g BaseGlyph) getColor(ctx *RenderContext, b *Base) color.Color {
if g.Color.IsZero() {
color, _ := color.Get(ctx.TextStyles.Color, b)
return color
} else {
return g.Color
}
}
func (g BaseGlyph) getSize(ctx *RenderContext) float32 {
if g.Size.Value == 0 {
return ctx.TextStyles.FontSize.Get(ctx.AmountContext, true)
} else {
return g.Size.Get(ctx.AmountContext, true)
}
}
func (g *BaseGlyph) GetState(ctx *RenderContext, wrap TextWrap, prev Glyph) GlyphState {
space := unicode.IsSpace(g.Text)
state := GlyphState{
CanBreak: wrap == TextWrapChar || (wrap == TextWrapWord && space),
ShouldBreak: g.Text == '\r' || g.Text == '\n',
Empty: space,
}
g.init(ctx)
if g.font == nil {
state.Empty = true
return state
}
offset := float32(0)
if prev != nil {
if prevGlyph, ok := prev.(*BaseGlyph); ok {
offset = g.font.GetKerning(prevGlyph.Text, g.Text)
}
}
size := g.getSize(ctx)
state.Size.X = (size * g.fontRune.Width) + offset*size
state.Size.Y = size * g.font.LineHeight
return state
}
func (g *BaseGlyph) Render(ctx *RenderContext, topLeft gfx.Coord, b *Base) RenderedGlyph {
g.init(ctx)
if g.font == nil {
return RenderedGlyph{}
}
color := g.getColor(ctx, b)
size := g.getSize(ctx)
extents := g.fontRune.Extent
baselineOffset := g.font.Baseline * size
return RenderedGlyph{
Tile: g.fontRune.Tile,
Color: color,
Bounds: Bounds{
Left: topLeft.X - extents.Left*size,
Right: topLeft.X + extents.Right*size,
Top: topLeft.Y - extents.Top*size + baselineOffset,
Bottom: topLeft.Y + extents.Bottom*size + baselineOffset,
},
}
}
func (g BaseGlyph) String() string {
return string([]rune{g.Text})
}
var TextFormatRegex = regexp.MustCompile(`\\{|\{([^:}]+):?([^}]*)\}|.|\s`)
// Parses text and converts it to glyphs using a special format.
// In the text you can set:
// - font = {f:name} or {f:} to reset to default
// - color = {c:#RRGGBB} or {c:#RRGGBBAA} or {c:red} or {c:} to reset to default
// - size = {s:12} or {s:50%} or {s:} to reset to default
// - new paragraph = {p} or {p:reset} to reset paragraph style
// - set kerning to paragraph = {k:5}
// - set horizontal alignment in paragraph = {h:0.5}
// - set vertical alignment within a line in paragraph = {v:0}
// - set wrap in paragraph = {w:none} or {w:word} or {w:char}
// - set line spacing in paragraph = {ls:10}
// - set line height in paragraph = {lh:10} or {lh:120%}
// - set indent = {i:5em}
// - set padding in paragraph = {pa:10} or {pl:10%} or {pr:0} or {pb} or {pt:}
// - set spacing between paragraphs = {ps:10}
// - set vertical alignment of paragraphs in area = {pv:0.5}
// - set max height in area = {mh:100}
// - set max width in area = {mw:100}
// - set clip show x in area = {cx} aka {cx:} aka {cx:none} or {cx:left} or {cx:right}
// - set clip show y in area = {cy} aka {cy:} aka {cy:none} or {cy:top} or {cy:bottom}
//
// Ex: "Hello {f:roboto}World{f:} {s:20}HOW{s:} {c:#00F}blue {c:red}red {c:} \{"
func TextToParagraphs(text string) (paragraphs Paragraphs, err error) {
paragraph := Paragraph{}
glyph := BaseGlyph{}
readAmount := func(s string, required bool) *Amount {
if s == "" {
if required {
return &Amount{}
}
return nil
}
amt := Amount{}
err = amt.UnmarshalText([]byte(s))
return &amt
}
readFloat := func(s string) float32 {
f := float64(0)
if s != "" {
f, err = strconv.ParseFloat(s, 32)
}
return float32(f)
}
readAlignment := func(s string) *Alignment {
if s == "" {
return nil
}
align := Alignment(0)
err = align.UnmarshalText([]byte(s))
return &align
}
readClipShow := func(s string) *ClipShow {
if s == "" {
return nil
}
show := ClipShow(0)
err = show.UnmarshalText([]byte(s))
return &show
}
readWrap := func(s string) *TextWrap {
if s == "" {
return nil
}
wrap := TextWrap("")
err = wrap.UnmarshalText([]byte(s))
return &wrap
}
readColor := func(s string) color.Color {
color := color.Color{}
if s != "" {
err = color.UnmarshalText([]byte(s))
}
return color
}
getParagraphStyles := func() *ParagraphStylesOverride {
if paragraph.Styles == nil {
paragraph.Styles = &ParagraphStylesOverride{}
}
return paragraph.Styles
}
getParagraphPadding := func() *AmountBounds {
style := getParagraphStyles()
if style.ParagraphPadding == nil {
style.ParagraphPadding = &AmountBounds{}
}
return style.ParagraphPadding
}
getParagraphsStyles := func() *ParagraphsStylesOverride {
if paragraphs.Styles == nil {
paragraphs.Styles = &ParagraphsStylesOverride{}
}
return paragraphs.Styles
}
pieces := TextFormatRegex.FindAllStringSubmatch(text, -1)
for _, piece := range pieces {
runes := piece[0]
command := piece[1]
value := piece[2]
switch command {
case "": // Add glyph to paragraph
copy := glyph
copy.Text, _ = utf8.DecodeRuneInString(runes)
paragraph.Glyphs = append(paragraph.Glyphs, ©)
case "f": // Glyph commands
glyph.Font = id.Get(value)
case "s":
glyph.Size = *readAmount(value, true)