-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext.go
901 lines (756 loc) · 32.4 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
package glitch
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"strings"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
"unicode"
"github.com/golang/freetype/truetype"
"github.com/unitoftime/flow/glm"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/font/gofont/goregular"
)
// TODO: Look into this: https://steamcdn-a.akamaihd.net/apps/valve/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf
// TODO: And this: https://blog.mapbox.com/drawing-text-with-signed-distance-fields-in-mapbox-gl-b0933af6f817
// TODO: And this: https://www.youtube.com/watch?v=Y1kuhXtVAc4
// TODO: Ideally this wouldn't return an error
func DefaultAtlas() (*Atlas, error) {
runes := make([]rune, unicode.MaxASCII-32)
for i := range runes {
runes[i] = rune(32 + i)
}
font, err := truetype.Parse(goregular.TTF)
if err != nil {
return nil, err
}
fontFace := truetype.NewFace(font, &truetype.Options{
Size: 32,
// GlyphCacheEntries: 1,
})
cfg := AtlasConfig{
Smooth: true,
TextureSize: 1024,
Padding: 10,
}
atlas := NewAtlas(fontFace, runes, cfg)
return atlas, nil
}
func BasicFontAtlas() (*Atlas, error) {
runes := make([]rune, unicode.MaxASCII-32)
for i := range runes {
runes[i] = rune(32 + i)
}
// font, err := truetype.Parse(gofont.TTF)
// if err != nil {
// return nil, err
// }
// fontFace := truetype.NewFace(font, &truetype.Options{
// Size: size,
// // GlyphCacheEntries: 1,
// })
fontFace := basicfont.Face7x13
cfg := AtlasConfig{
Smooth: true,
TextureSize: 512,
Padding: 10,
}
atlas := NewAtlas(fontFace, runes, cfg)
return atlas, nil
}
type Glyph struct {
Advance float64
Bearing Vec2
BoundsUV Rect
}
// TODO - instead of creating a single atlas ahead of time. I should just load the font and then dynamically create the atlas as needed. This should probably change once you add automatic texture batching.
type Atlas struct {
// face font.Face
mapping map[rune]Glyph
ascent float64 // Distance from top of line to baseline
descent float64 // Distance from bottom of line to baseline
height float64 // The recommended gap between two lines
texture *Texture
border int // Specifies a border on the font.
pixelPerfect bool // if true anti-aliasing will be disabled
defaultKerning float64
defaultMaterial Material
}
func fixedToFloat(val fixed.Int26_6) float64 {
// Shift to the left by 6 then convert to an int, then to a float, then shift right by 6
// TODO - How to handle overruns?
// intVal := val.Mul(fixed.I(1_000_000)).Floor()
// return float32(intVal) / 1_000_000.0
return float64(val) / (1 << 6)
}
func floatToFixed(val float64) fixed.Int26_6 {
// Shift to the left by 6 then convert to an int, then to a float, then shift right by 6
// TODO - How to handle overruns?
// intVal := val.Mul(fixed.I(1_000_000)).Floor()
// return float32(intVal) / 1_000_000.0
return fixed.Int26_6(val * (1 << 6))
}
type AtlasConfig struct {
Border float64
Smooth bool
Padding int
Kerning float64
TextureSize int // TODO: automagically calculate
}
func NewAtlas(face font.Face, runes []rune, config AtlasConfig) *Atlas {
metrics := face.Metrics()
// fmt.Println("Metrics: ", fixedToFloat(metrics.Height), fixedToFloat(metrics.Ascent), fixedToFloat(metrics.Descent))
atlas := &Atlas{
// face: face,
mapping: make(map[rune]Glyph),
ascent: fixedToFloat(metrics.Ascent),
descent: fixedToFloat(metrics.Descent),
height: fixedToFloat(metrics.Height),
border: int(config.Border),
pixelPerfect: !config.Smooth, // TODO - not sure this is exactly right. You could presumably want a bilinear filtered texture but anti-aliasing turned off on the text.
defaultKerning: config.Kerning,
}
border := int(config.Border)
basePadding := config.Padding
size := config.TextureSize
fixedSize := fixed.I(size)
fSize := float64(size)
blackImg := image.NewRGBA(image.Rect(0, 0, size, size))
draw.Draw(blackImg, blackImg.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
img := image.NewRGBA(image.Rect(0, 0, size, size))
// draw.Draw(img, img.Bounds(), image.NewUniform(color.Alpha{0}), image.ZP, draw.Src)
// Note: In case you want to see the boundary of each rune, uncomment this
// draw.Draw(img, img.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
padding := fixed.I(basePadding + (2 * atlas.border)) // Padding for runes drawn to atlas
startDot := fixed.P(padding.Floor(), (metrics.Ascent + padding).Floor()) // Starting point of the dot
dot := startDot
for i, r := range runes {
// https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyphterms_2x.png
bounds, mask, maskp, adv, ok := face.Glyph(dot, r)
if !ok {
panic("Missing rune!")
}
bearingRect, _, _ := face.GlyphBounds(r)
// Instead of flooring we convert from fixed int to float manually (mult by 10^6 then floor, cast and divide by 10^6). I think this is slightly more accurate but it's hard to tell so I'll leave old code below
// log.Println("Rune: ", string(r), " - BearingRect: ", bearingRect)
bearingX := float64((bearingRect.Min.X * 1000000).Floor()) / (1000000 * fSize)
bearingY := float64((-bearingRect.Max.Y * 1000000).Floor()) / (1000000 * fSize)
// advance := float32((adv * 1000000).Floor())/(1000000 * fSize) // TODO - why doesn't this work?
// log.Println("Rune: ", string(r), " - BearingX: ", float32(bearingRect.Min.X.Floor())/fSize)
// log.Println("Rune: ", string(r), " - BearingX: ", bearingX)
// log.Println("Rune: ", string(r), " - BearingY: ", float32(-bearingRect.Max.Y.Floor())/fSize)
// log.Println("Rune: ", string(r), " - BearingY: ", bearingY)
// Before: Single draw which wouldn't have a border
// draw.Draw(img, bounds, mask, maskp, draw.Src)
// After: 9 offset draws in every direction, then a normal draw
// Draw nine slots around
// // x = dist * cos(pi/2)
// diagDist := int(float64(border) * 1.0 / math.Sqrt(2))
// draw.DrawMask(img, bounds.Add(image.Point{border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{0, border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{0, -border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // Draw shadow
// shadow := 1
// draw.DrawMask(img, bounds.Add(image.Point{border + shadow, border + shadow}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{0, -border-shadow}), blackImg, image.Point{}, mask, maskp, draw.Over)
if border > 0 && config.Smooth {
// Draw nine slots around
// x = dist * cos(pi/2)
diagDist := int(float64(border) * 1.0 / math.Sqrt(2))
// diagDist := int(float64(border)/2)
draw.DrawMask(img, bounds.Add(image.Point{border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{-border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{-diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{-diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{0, border}), blackImg, image.Point{}, mask, maskp, draw.Over)
draw.DrawMask(img, bounds.Add(image.Point{0, -border}), blackImg, image.Point{}, mask, maskp, draw.Over)
}
draw.Draw(img, bounds, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds, blackImg, image.Point{}, mask, maskp, draw.Src)
atlas.mapping[r] = Glyph{
// Advance: float64(adv.Floor() + (2*border))/fSize,
Advance: float64(adv.Floor()) / fSize,
// Bearing: Vec2{float32(bearingRect.Min.X.Floor())/fSize, float32((-bearingRect.Max.Y).Floor())/fSize},
//Advance: advance,
Bearing: Vec2{bearingX, bearingY},
BoundsUV: glm.R(
float64(bounds.Min.X-atlas.border)/fSize, float64(bounds.Min.Y-atlas.border)/fSize,
float64(bounds.Max.X+atlas.border)/fSize, float64(bounds.Max.Y+atlas.border)/fSize,
).Norm(),
}
// Usual next dot location
nextDotX := dot.X + adv + padding
nextDotY := dot.Y
// Exit if we are at the end
if (i + 1) >= len(runes) {
break
}
// If the rune after this one pushes us too far then loop around
nextAdv, ok := face.GlyphAdvance(runes[i+1])
if !ok {
panic("Missing rune!")
}
if nextDotX+nextAdv >= fixedSize {
// log.Println("Ascending!")
nextDotX = startDot.X
nextDotY = dot.Y + metrics.Ascent + padding
}
// log.Println(nextDotX, nextDotY)
dot = fixed.Point26_6{nextDotX, nextDotY}
}
// This just disables anti-aliasing by snapping pixels to either white or transparent
// if atlas.pixelPerfect {
// imgBounds := img.Bounds()
// for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
// for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
// rgba := img.RGBAAt(x, y)
// if rgba.A > 0 {
// rgba.A = 255
// img.Set(x, y, color.White)
// }
// }
// }
// }
// This runs a box filter based on the border side
if atlas.border != 0 && !config.Smooth {
// Only border this way for pixel fonts
// Finds white pixels and draws borders around the edges
imgBounds := img.Bounds()
for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
rgba := img.RGBAAt(x, y)
if (rgba != color.RGBA{255, 255, 255, 255}) {
continue // If the pixel is not white, then it doesnt trigger a border
}
box := image.Rect(x-atlas.border, y-atlas.border, x+atlas.border, y+atlas.border)
for xx := box.Min.X; xx <= box.Max.X; xx++ {
for yy := box.Min.Y; yy <= box.Max.Y; yy++ {
rgba := img.RGBAAt(xx, yy)
if rgba.A == 0 {
// Only add a border to transparent pixels
img.Set(xx, yy, color.Black)
}
}
}
}
}
// Finds transparent pixels and draws borders inward on non-transparent pixels
// imgBounds := img.Bounds()
// for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
// for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
// rgba := img.RGBAAt(x, y)
// if rgba.A != 0 {
// continue // Skip if pixel is not fully transparent
// }
// box := image.Rect(x-atlas.border, y-atlas.border, x+atlas.border, y+atlas.border)
// for xx := box.Min.X; xx <= box.Max.X; xx++ {
// for yy := box.Min.Y; yy <= box.Max.Y; yy++ {
// rgba := img.RGBAAt(xx, yy)
// if rgba.A != 0 {
// // Only add a border to transparent pixels
// rgba.R = 0
// rgba.G = 0
// rgba.B = 0
// img.Set(xx, yy, rgba)
// }
// }
// }
// }
// }
}
// // outputFile is a File type which satisfies Writer interface
// outputFile, err := os.Create("test.png")
// if err != nil { panic(err) }
// png.Encode(outputFile, img)
// outputFile.Close()
atlas.texture = NewTexture(img, config.Smooth)
atlas.defaultMaterial = DefaultMaterial(atlas.texture)
// fmt.Println("TextAtlas: ", atlas.texture.width, atlas.texture.height)
return atlas
}
func (a *Atlas) Material() *Material {
return &a.defaultMaterial
}
// func (a *Atlas) GappedLineHeight() float64 {
// // TODO - scale?
// return (-fixedToFloat(a.ascent) + fixedToFloat(a.descent) - fixedToFloat(a.height)) + float64(2 * a.border)
// }
func (a *Atlas) LineHeight() float64 {
return a.height
}
func (a *Atlas) UngappedLineHeight() float64 {
// TODO - scale?
return (a.ascent + a.descent)
}
func (a *Atlas) RuneVerts(mesh *Mesh, r rune, dot Vec2, scale float64, color RGBA) (Vec2, float64) {
// multiplying by texture sizes converts from UV to pixel coords
scaleX := scale * float64(a.texture.width)
scaleY := scale * float64(a.texture.height)
glyph, ok := a.mapping[r]
// if !ok { panic(fmt.Sprintf("Missing Rune: %v", r)) }
if !ok {
// fmt.Printf("Missing Rune: %v", r)
// Replace rune with '?'
oldR := r
r = '?' // TODO - Pick some other rune. TODO - require this rune to be in the atlas!
glyph, ok = a.mapping[r]
if !ok {
panic(fmt.Sprintf("Missing Rune: %v and replacement%v", oldR, r))
}
}
// log.Println(glyph.Bearing)
// UV coordinates of the quad
u1 := glyph.BoundsUV.Min.X
u2 := glyph.BoundsUV.Max.X
v1 := glyph.BoundsUV.Min.Y
v2 := glyph.BoundsUV.Max.Y
// Pixel coordinates of the quad (scaled by scale)
x1 := dot.X + (scaleX * glyph.Bearing.X)
x2 := x1 + (scaleX * (u2 - u1))
// Note: Commented out the downard shift here, and I'm doing it in the above func
y1 := dot.Y + (scaleY * glyph.Bearing.Y) + (a.descent * scale)
y2 := y1 + (scaleY * (v2 - v1))
destRect := glm.R(x1, y1, x2, y2)
if a.pixelPerfect {
destRect = glm.R(math.Round(x1), math.Round(y1), math.Round(x2), math.Round(y2))
}
if mesh != nil {
mesh.AppendQuadMesh(destRect, glm.R(u1, v1, u2, v2), color)
// mesh := NewQuadMesh(R(x1, y1, x2, y2), R(u1, v1, u2, v2))
}
dot.X += (scaleX * glyph.Advance) + (a.defaultKerning * scale) // TODO: Kerning should come from text, not atlas
return dot, y2
}
func (a *Atlas) Text(str string, scale float64) *Text {
t := &Text{
currentString: "",
atlas: a,
texture: a.texture,
// material: NewSpriteMaterial(a.texture),
material: a.defaultMaterial,
scale: scale,
// LineHeight: a.UngappedLineHeight(),
mesh: NewMesh(),
tmpMesh: NewMesh(),
color: RGBA{1, 1, 1, 1},
}
t.Set(str)
return t
}
// TODO: This could be improved by just calling specialized measurement functions
func (a *Atlas) Measure(str string, scale float64) Rect {
fakeText := Text{
currentString: str,
atlas: a,
scale: scale,
}
return fakeText.AppendStringVerts(str, true)
}
func (a *Atlas) MeasureWrapped(str string, scale float64, wrapRect Rect) Rect {
fakeText := Text{
currentString: str,
atlas: a,
scale: scale,
wordWrap: true,
wrapRect: wrapRect,
}
return fakeText.AppendStringVerts(str, true)
}
type Text struct {
currentString string
mesh *Mesh
tmpMesh *Mesh // For temporarily buffering data. TODO - would be more efficient just to append the quads directly to the mesh rather than buffering them here
atlas *Atlas
bounds Rect
texture *Texture
material Material
scale float64
shadow Vec2
wordWrap bool
wrapRect Rect
// LineHeight float64
Orig Vec2 // The baseline starting point from which to draw the text
Dot Vec2 // The location of the next rune to draw
color RGBA // The color with which to draw the next text
}
func (t *Text) Bounds() Rect {
return t.bounds
}
// func (t *Text) SetMaterial(material Material) {
// t.material = material
// }
func (t *Text) Material() *Material {
return &t.material
}
func (t *Text) MeshBounds() Rect {
return t.mesh.Bounds().Rect()
}
func (t *Text) SetScale(scale float64) {
t.scale = scale
}
func (t *Text) SetColor(col RGBA) {
t.color = col
}
func (t *Text) SetShadow(shadow Vec2) {
t.shadow = shadow
}
func (t *Text) SetWordWrap(wrap bool, wrapRect Rect) {
t.wordWrap = wrap
t.wrapRect = wrapRect
// if t.wordWrap {
// // lineHeight := t.atlas.UngappedLineHeight() * t.scale
// t.Orig = Vec2{wrapRect.Min.X, wrapRect.Max.Y} // + lineHeight}
// }
}
func (t *Text) Clear() {
t.Orig = Vec2{}
t.Dot = t.Orig
t.mesh.Clear()
}
// This resets the text and sets it to the passed in string (if the passed in string is different!)
// TODO - I need to deprecate this in favor of a better interface
func (t *Text) Set(str string) {
// TODO: If wordwrap we also need to regenerate, but technically only if the bounds of the wrapRect have changed
if t.currentString != str || t.wordWrap {
t.currentString = str
t.regenerate()
}
}
func (t *Text) regenerate() {
t.Clear()
t.bounds = t.AppendStringVerts(t.currentString, false)
}
func (t *Text) WriteString(str string) (n int, err error) {
return t.Write([]byte(str))
}
// This appends the list of bytes onto the end of the string
// Note: implements io.Writer interface
func (t *Text) Write(p []byte) (n int, err error) {
appendedStr := string(p)
if t.mesh == nil {
t.Set(appendedStr)
return len(p), nil
}
t.currentString = t.currentString + appendedStr
newBounds := t.AppendStringVerts(appendedStr, false)
t.bounds = t.bounds.Union(newBounds)
return len(p), nil
}
func (t *Text) Draw(target BatchTarget, matrix Mat4) {
t.DrawColorMask(target, matrix, White)
}
func (t *Text) DrawColorMask(target BatchTarget, matrix Mat4, color RGBA) {
// mat2 := matrix
// mat2.Translate(0, -0.5, 0)
// target.Add(t.mesh, mat2, Black, t.material, false)
target.Add(t.mesh, glm4(matrix), color, t.material, true)
}
func (t *Text) RectDraw(target BatchTarget, rect Rect) {
t.DrawRect(target, rect, White)
}
func (t *Text) DrawRect(target BatchTarget, rect Rect, color RGBA) {
mat := Mat4Ident
mat.Scale(1.0, 1.0, 1.0).Translate(rect.Min.X, rect.Min.Y, 0)
target.Add(t.mesh, glm4(mat), color, t.material, true)
}
func (t *Text) RectDrawColorMask(target BatchTarget, bounds Rect, mask RGBA) {
mat := Mat4Ident
// TODO why shouldn't I be shifting to the middle?
// mat.Scale(bounds.W() / t.bounds.W(), bounds.H() / t.bounds.H(), 1).Translate(bounds.W()/2 + bounds.Min.X, bounds.H()/2 + bounds.Min.Y, 0)
// mat.Scale(1.0, 1.0, 1.0).Translate(rect.Min.X, rect.Min.Y, 0)
// TODO!!! - There's something wrong with this
mat.Scale(bounds.W()/t.bounds.W(), bounds.H()/t.bounds.H(), 1).Translate(bounds.Min.X, bounds.Min.Y, 0)
target.Add(t.mesh, glm4(mat), mask, t.material, true)
}
// If measure is set true, dont add them to the text mesh, just measure the bounds of the string
func (t *Text) AppendStringVerts(text string, measure bool) Rect {
// maxAscent := float32(0) // Tracks the maximum y point of the text block
lineHeight := t.atlas.UngappedLineHeight() * t.scale
initialDot := t.Dot
maxDotX := t.Dot.X
numLines := 1.0
for i, r := range text {
// If the rune is a newline, then we need to reset the dot for the next line
newline := r == '\n'
// If we wordwraping and are on a space, check to see if we should go to the next line
if t.wordWrap && r == ' ' {
nextSpaceIdx := strings.Index(text[i+1:], " ")
if nextSpaceIdx < 0 {
// There is no next space so we measure the rest of the line
nextSpaceIdx = len(text) - i
}
nextWord := t.atlas.Measure(text[i:i+nextSpaceIdx], t.scale)
if (t.Dot.X-initialDot.X)+nextWord.W() > t.wrapRect.W() {
newline = true
}
}
if newline {
// t.Dot.Y -= t.atlas.LineHeight()
t.Dot.Y -= lineHeight
t.Dot.X = t.Orig.X
numLines++
continue
}
var dstMesh *Mesh
if !measure {
dstMesh = t.mesh
}
newDot, _ := t.atlas.RuneVerts(dstMesh, r, t.Dot, t.scale, t.color)
noShadow := Vec2{}
if t.shadow != noShadow {
_, _ = t.atlas.RuneVerts(dstMesh, r, t.Dot.Add(t.shadow), t.scale, Black)
}
maxDotX = max(maxDotX, newDot.X)
t.Dot = newDot
// if maxAscent < ascent {
// maxAscent = dot.Y + ascent
// }
}
// // return R(meshBounds.Min.X, initialDot.Y, meshBounds.Max.X, initialDot.Y - (numLines * lineHeight)).Norm()
// // Attempt 4 - use mesh bounds for X and line height for Y
// meshBounds := t.mesh.Bounds().Rect()
// // Note: The RuneVerts function corners the glyph into the bounds by applying the descent. So I dont need to track ascent/descent here
// // top := initialDot.Y + (fixedToFloat(t.atlas.ascent) * t.scale)
// // bot := top - (numLines * lineHeight)
// top := initialDot.Y + lineHeight
// // top := initialDot.Y + t.atlas.descent
// bot := top - (numLines * lineHeight)
// return R(meshBounds.Min.X, top, meshBounds.Max.X, bot).Norm()// .MoveMin(Vec2{})
// Attempt 5 - use dot for X and line height for Y
top := initialDot.Y + lineHeight
// top := initialDot.Y + t.atlas.descent
bot := top - (numLines * lineHeight)
return glm.R(initialDot.X, top, maxDotX, bot).Norm() // .MoveMin(Vec2{})
//--------------------------------------------------------------------------------
// // return mesh, R(initialDot[0], initialDot[1], dot[0], dot[1] + maxAscent)
// // fmt.Println("-----")
// // fmt.Println(fixedToFloat(a.ascent))
// // fmt.Println(fixedToFloat(a.descent))
// // fmt.Println(fixedToFloat(a.height))
// // fmt.Println(maxAscent)
// // fmt.Println(scale)
// // bounds := R(initialDot[0], initialDot[1], dot[0], dot[1] - a.LineHeight())
// // bounds := R(initialDot[0],
// // initialDot[1] - (2 * fixedToFloat(a.ascent)),
// // dot[0], // TODO - this is wrong if because this is the length of the last line, we need the length of the longest line
// // dot[1] - (2 * fixedToFloat(a.descent)))
// // TODO - this used the glyphs to determine bounds, below I use the mesh
// // // TODO - idk what I'm doing here, but it seems to work. Man text rendering is hard.
// // bounds := R(initialDot[0],
// // initialDot[1] - (fixedToFloat(t.atlas.ascent)),
// // t.Dot[0], // TODO - this is wrong if because this is the length of the last line, we need the length of the longest line
// // t.Dot[1] - (fixedToFloat(t.atlas.descent))).
// // Norm().
// // Moved(Vec2{0, fixedToFloat(t.atlas.ascent)})
// // return bounds
// // Attempt 2 - Use mesh bounds
// // return t.mesh.Bounds().Rect()
// // Attempt 3 - use mesh bounds for X and line height for Y
// meshBounds := t.mesh.Bounds().Rect()
// return R(meshBounds.Min[0], initialDot[1], meshBounds.Max[0], t.Dot[1] + lineHeight)
// // fmt.Println(bounds)
// // bounds := R(initialDot[0],
// // initialDot[1] - (fixedToFloat(a.descent)),
// // dot[0], // TODO - this is wrong if because this is the length of the last line, we need the length of the longest line
// // dot[1] - (fixedToFloat(a.ascent))).Norm()
// // return mesh, bounds
// // return mesh, R(initialDot[0], initialDot[1], dot[0], dot[1] + fixedToFloat(a.lineHeight))
// // return mesh, R(initialDot[0], initialDot[1], dot[0], dot[1] - (fixedToFloat(a.ascent) - fixedToFloat(a.descent)))
// // return mesh, R(initialDot[0],
// // initialDot[1] - fixedToFloat(a.descent)/1024,
// // dot[0], // TODO - this is wrong if because this is the length of the last line, we need the length of the longest line
// // dot[1] + fixedToFloat(a.ascent)/1024)
}
// //--------------------------------------------------------------------------------
// //--------------------------------------------------------------------------------
// //--------------------------------------------------------------------------------
// //--------------------------------------------------------------------------------
// func NewAtlas(face font.Face, runes []rune, config AtlasConfig) *Atlas {
// metrics := face.Metrics()
// // fmt.Println("Metrics: ", fixedToFloat(metrics.Height), fixedToFloat(metrics.Ascent), fixedToFloat(metrics.Descent))
// atlas := &Atlas{
// // face: face,
// mapping: make(map[rune]Glyph),
// ascent: fixedToFloat(metrics.Ascent),
// descent: fixedToFloat(metrics.Descent),
// height: fixedToFloat(metrics.Height),
// border: int(config.Border),
// pixelPerfect: !config.Smooth, // TODO - not sure this is exactly right. You could presumably want a bilinear filtered texture but anti-aliasing turned off on the text.
// defaultKerning: config.Kerning,
// }
// border := int(config.Border)
// basePadding := config.Padding
// size := config.TextureSize
// fixedSize := fixed.I(size)
// fSize := float64(size)
// blackImg := image.NewRGBA(image.Rect(0, 0, size, size))
// draw.Draw(blackImg, blackImg.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
// img := image.NewRGBA(image.Rect(0, 0, size, size))
// // draw.Draw(img, img.Bounds(), image.NewUniform(color.Alpha{0}), image.ZP, draw.Src)
// // Note: In case you want to see the boundary of each rune, uncomment this
// // draw.Draw(img, img.Bounds(), image.NewUniform(color.Black), image.ZP, draw.Src)
// padding := fixed.I(basePadding + (2 * atlas.border)) // Padding for runes drawn to atlas
// startDot := fixed.P(padding.Floor(), (atlas.ascent + padding).Floor()) // Starting point of the dot
// dot := startDot
// for i, r := range runes {
// // https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/Art/glyphterms_2x.png
// bounds, mask, maskp, adv, ok := face.Glyph(dot, r)
// if !ok { panic("Missing rune!") }
// bearingRect, _, _ := face.GlyphBounds(r)
// // if r == 'R' {
// // fmt.Printf("%T\n", mask)
// // // fmt.Println(mask)
// // outputFile, err := os.Create("testR.png")
// // if err != nil { panic(err) }
// // png.Encode(outputFile, mask)
// // outputFile.Close()
// // }
// // Instead of flooring we convert from fixed int to float manually (mult by 10^6 then floor, cast and divide by 10^6). I think this is slightly more accurate but it's hard to tell so I'll leave old code below
// // log.Println("Rune: ", string(r), " - BearingRect: ", bearingRect)
// bearingX := float64((bearingRect.Min.X * 1000000).Floor()) / (1000000 * fSize)
// bearingY := float64((-bearingRect.Max.Y * 1000000).Floor()) / (1000000 * fSize)
// // advance := float32((adv * 1000000).Floor())/(1000000 * fSize) // TODO - why doesn't this work?
// // log.Println("Rune: ", string(r), " - BearingX: ", float32(bearingRect.Min.X.Floor())/fSize)
// // log.Println("Rune: ", string(r), " - BearingX: ", bearingX)
// // log.Println("Rune: ", string(r), " - BearingY: ", float32(-bearingRect.Max.Y.Floor())/fSize)
// // log.Println("Rune: ", string(r), " - BearingY: ", bearingY)
// // Before: Single draw which wouldn't have a border
// // draw.Draw(img, bounds, mask, maskp, draw.Src)
// // After: 9 offset draws in every direction, then a normal draw
// // Draw nine slots around
// // // x = dist * cos(pi/2)
// // diagDist := int(float64(border) * 1.0 / math.Sqrt(2))
// // draw.DrawMask(img, bounds.Add(image.Point{border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{-border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{-diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{-diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{0, border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds.Add(image.Point{0, -border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // // Draw shadow
// // shadow := 1
// // draw.DrawMask(img, bounds.Add(image.Point{border + shadow, border + shadow}), blackImg, image.Point{}, mask, maskp, draw.Over)
// // // draw.DrawMask(img, bounds.Add(image.Point{0, -border-shadow}), blackImg, image.Point{}, mask, maskp, draw.Over)
// if border > 0 && config.Smooth {
// // Draw nine slots around
// // x = dist * cos(pi/2)
// diagDist := int(float64(border) * 1.0 / math.Sqrt(2))
// // diagDist := int(float64(border)/2)
// draw.DrawMask(img, bounds.Add(image.Point{border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-border, 0}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-diagDist, diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{-diagDist, -diagDist}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{0, border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// draw.DrawMask(img, bounds.Add(image.Point{0, -border}), blackImg, image.Point{}, mask, maskp, draw.Over)
// }
// draw.Draw(img, bounds, mask, maskp, draw.Over)
// // draw.DrawMask(img, bounds, blackImg, image.Point{}, mask, maskp, draw.Src)
// atlas.mapping[r] = Glyph{
// // Advance: float64(adv.Floor() + (2*border))/fSize,
// Advance: float64(adv.Floor())/fSize,
// // Bearing: Vec2{float32(bearingRect.Min.X.Floor())/fSize, float32((-bearingRect.Max.Y).Floor())/fSize},
// //Advance: advance,
// Bearing: Vec2{bearingX, bearingY},
// BoundsUV: R(
// float64(bounds.Min.X - atlas.border)/fSize, float64(bounds.Min.Y - atlas.border)/fSize,
// float64(bounds.Max.X + atlas.border)/fSize, float64(bounds.Max.Y + atlas.border)/fSize),
// }
// // Usual next dot location
// nextDotX := dot.X + adv + padding
// nextDotY := dot.Y
// // Exit if we are at the end
// if (i+1) >= len(runes) { break }
// // If the rune after this one pushes us too far then loop around
// nextAdv, ok := face.GlyphAdvance(runes[i+1])
// if !ok { panic("Missing rune!") }
// if nextDotX + nextAdv >= fixedSize {
// // log.Println("Ascending!")
// nextDotX = startDot.X
// nextDotY = dot.Y + atlas.ascent + padding
// }
// // log.Println(nextDotX, nextDotY)
// dot = fixed.Point26_6{nextDotX, nextDotY}
// }
// // This just disables anti-aliasing by snapping pixels to either white or transparent
// // if atlas.pixelPerfect {
// // imgBounds := img.Bounds()
// // for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
// // for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
// // rgba := img.RGBAAt(x, y)
// // if rgba.A > 0 {
// // rgba.A = 255
// // img.Set(x, y, color.White)
// // }
// // }
// // }
// // }
// // This runs a box filter based on the border side
// if atlas.border != 0 && !config.Smooth {
// // Only border this way for pixel fonts
// // Finds white pixels and draws borders around the edges
// imgBounds := img.Bounds()
// for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
// for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
// rgba := img.RGBAAt(x, y)
// if (rgba != color.RGBA{255, 255, 255, 255}) {
// continue // If the pixel is not white, then it doesnt trigger a border
// }
// box := image.Rect(x-atlas.border, y-atlas.border, x+atlas.border, y+atlas.border)
// for xx := box.Min.X; xx <= box.Max.X; xx++ {
// for yy := box.Min.Y; yy <= box.Max.Y; yy++ {
// rgba := img.RGBAAt(xx, yy)
// if rgba.A == 0 {
// // Only add a border to transparent pixels
// img.Set(xx, yy, color.Black)
// }
// }
// }
// }
// }
// // Finds transparent pixels and draws borders inward on non-transparent pixels
// // imgBounds := img.Bounds()
// // for x := imgBounds.Min.X; x < imgBounds.Max.X; x++ {
// // for y := imgBounds.Min.Y; y < imgBounds.Max.Y; y++ {
// // rgba := img.RGBAAt(x, y)
// // if rgba.A != 0 {
// // continue // Skip if pixel is not fully transparent
// // }
// // box := image.Rect(x-atlas.border, y-atlas.border, x+atlas.border, y+atlas.border)
// // for xx := box.Min.X; xx <= box.Max.X; xx++ {
// // for yy := box.Min.Y; yy <= box.Max.Y; yy++ {
// // rgba := img.RGBAAt(xx, yy)
// // if rgba.A != 0 {
// // // Only add a border to transparent pixels
// // rgba.R = 0
// // rgba.G = 0
// // rgba.B = 0
// // img.Set(xx, yy, rgba)
// // }
// // }
// // }
// // }
// // }
// }
// // // outputFile is a File type which satisfies Writer interface
// // outputFile, err := os.Create("test.png")
// // if err != nil { panic(err) }
// // png.Encode(outputFile, img)
// // outputFile.Close()
// atlas.texture = NewTexture(img, config.Smooth)
// atlas.defaultMaterial = DefaultMaterial(atlas.texture)
// // fmt.Println("TextAtlas: ", atlas.texture.width, atlas.texture.height)
// return atlas
// }