-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtruetype.h
1173 lines (986 loc) · 33.1 KB
/
truetype.h
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
//
// Truetype
//
union glyph_edge
{
struct
{
f32 X1;
f32 Y1;
f32 X2;
f32 Y2;
};
struct
{
v2 ThisPoint;
v2 PrevPoint;
};
};
struct glyph_active_edge
{
f32 FX;
f32 FDeltaX;
f32 FDeltaY;
f32 Direction;
f32 SY;
f32 EY;
};
#define Read8() (u8) (*(u8 *)(READ_Data + READ_Offset)); READ_Offset += sizeof(u8); Assert(READ_Offset <= READ_Size);
#define Read16() (u16)BE16(*(u16 *)(READ_Data + READ_Offset)); READ_Offset += sizeof(u16); Assert(READ_Offset <= READ_Size);
#define Read32() (u32)BE32(*(u32 *)(READ_Data + READ_Offset)); READ_Offset += sizeof(u32); Assert(READ_Offset <= READ_Size);
#define Read64() (u64)BE64(*(u64 *)(READ_Data + READ_Offset)); READ_Offset += sizeof(u64); Assert(READ_Offset <= READ_Size);
#define SIMPLE_GLYPH_FLAG_ON_CURVE_POINT 0x01
#define SIMPLE_GLYPH_FLAG_X_SHORT_VECTOR 0x02
#define SIMPLE_GLYPH_FLAG_Y_SHORT_VECTOR 0x04
#define SIMPLE_GLYPH_FLAG_REPEAT_FLAG 0x08
#define SIMPLE_GLYPH_FLAG_X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR 0x10
#define SIMPLE_GLYPH_FLAG_Y_IS_SAME_OR_POSITIVE_Y_SHORT_VECTOR 0x20
#define SIMPLE_GLYPH_FLAG_OVERLAP_SIMPLE 0x40
#define SIMPLE_GLYPH_FLAG_RESERVED 0x80 // Must be set to 0
internal inline void TTFGlyfParseCoords(u8 *READ_Data, smm *OffsetIn, smm READ_Size, u8 *Flags, smm CoordCount, s16 *Coords, smm CoordIndex)
{
// Get the current file read offset
// Can't be a pointer for the Read8,16,32,64 macros to work
smm READ_Offset = *OffsetIn;
// CoordIndex is 0 for X, and 1 for Y
// The first coordinate is relative to (0, 0)
// and the others are relative to the previous one
s16 LastCoord = 0;
for (smm Index = 0; Index < CoordCount; ++Index)
{
smm IsSameOrPositive = ((Flags[Index] & (SIMPLE_GLYPH_FLAG_X_IS_SAME_OR_POSITIVE_X_SHORT_VECTOR << CoordIndex)) != 0);
smm IsShortVector = ((Flags[Index] & (SIMPLE_GLYPH_FLAG_X_SHORT_VECTOR << CoordIndex)) != 0);
if (IsShortVector)
{
smm IsPositive = IsSameOrPositive;
s16 RawCoord = (s16)Read8();
Coords[Index] = LastCoord + RawCoord * (IsPositive ? +1 : -1);
}
else
{
smm IsTheSameAsLast = IsSameOrPositive;
if (IsTheSameAsLast)
{
Coords[Index] = LastCoord;
}
else
{
Coords[Index] = LastCoord + (s16)Read16();
}
}
LastCoord = Coords[Index];
}
// Write the new file read offset to the main function
*OffsetIn = READ_Offset;
}
internal void HandleClippedEdge(
f32 *ScanLine, smm X, glyph_active_edge *Edge, f32 X0, f32 Y0, f32 X1, f32 Y1)
{
if (Y0 == Y1) return;
Assert(Y0 < Y1);
Assert(Edge->SY <= Edge->EY);
if (Y0 > Edge->EY) return;
if (Y1 < Edge->SY) return;
f32 FX = (f32)X;
if (Y0 < Edge->SY)
{
X0 += (X1 - X0) * (Edge->SY - Y0) / (Y1 - Y0);
Y0 = Edge->SY;
}
if (Y1 > Edge->EY)
{
X1 += (X1 - X0) * (Edge->EY - Y1) / (Y1 - Y0);
Y1 = Edge->EY;
}
if (X0 == FX)
{
Assert(X1 <= (FX + 1));
}
else if (X0 == (FX + 1))
{
Assert(X1 >= FX);
}
else if (X0 <= FX)
{
Assert(X1 <= FX);
}
else if (X0 >= (FX + 1.0f))
{
Assert(X1 >= (FX + 1.0f));
}
else
{
Assert((X1 >= FX) && (X1 <= (FX + 1.0f)));
}
if ((X0 <= FX) && (X1 <= FX))
{
ScanLine[X] += Edge->Direction * (Y1 - Y0);
}
else if ((X0 >= (FX + 1.0f)) && (X1 >= (FX + 1.0f)))
{
// Nothing
}
else
{
Assert((X0 >= FX) && (X0 <= (FX + 1)) && (X1 >= FX) && (X1 <= (FX + 1)));
// Coverage = 1 - Average X Position
ScanLine[X] += Edge->Direction * (Y1 - Y0) * ((1.0f - ((X0 - FX) + (X1 - FX)) * 0.5f));
}
}
internal inline void MakeEdge(v2 ThisPoint, v2 PrevPoint,
smm *EdgeCount, smm MaxEdgeCount, glyph_edge *Edges, u8 *EdgesYSwap)
{
// Cull horizontal edges, because they are not needed
// scanlines are horizontal and it will not have any effect
if (PrevPoint.Y != ThisPoint.Y)
{
// Swap P0 and P1 so that P0 ALWAYS has higher Y than P1
// This is useful for sorting and rasterization below
// because we can guarantee which Y is bigger
bmm DoSwap = (ThisPoint.Y > PrevPoint.Y);
if (DoSwap)
{
v2 SwapTemp = ThisPoint;
ThisPoint = PrevPoint;
PrevPoint = SwapTemp;
}
Assert(*EdgeCount < MaxEdgeCount);
EdgesYSwap[*EdgeCount] = DoSwap;
Edges[*EdgeCount] = glyph_edge{
ThisPoint.X, ThisPoint.Y,
PrevPoint.X, PrevPoint.Y
};
*EdgeCount += 1;
}
}
internal smm RasterizeGlyph(smm GlyphIndex)
{
font *Font = &GLOBAL_MainState->Font;
if (GlyphIndex < 0) { GlyphIndex = 0; }
Assert(GlyphIndex < Font->GlyphCount);
//
// Check if the glyph is already rasterized
//
for (smm LoadedGlyph = 0;
LoadedGlyph < Font->LoadedGlyphCount;
++LoadedGlyph)
{
if (GlyphIndex == Font->LoadedGlyphsCodepoint[LoadedGlyph])
{
// The glyph is already rasterized
Font->LoadedGlyphsUsedFrame[LoadedGlyph] = GLOBAL_MainState->Frame;
return LoadedGlyph;
}
}
// @Note We are not modifying the actual arena
// This is a local copy that SHOULD NOT affect the TransientArena
memory_arena ArenaSnapshot = GLOBAL_MainState->TransientArena;
memory_arena *TempArena = &ArenaSnapshot;
//
// GLYF
//
u8 *READ_Data = Font->FileData;
smm READ_Size = Font->FileSize;
smm READ_Offset;
smm OffsetToGlyph;
if (Font->IndexToLocFormat == 1)
{
READ_Offset = Font->LocaOffset + GlyphIndex * sizeof(u32);
OffsetToGlyph = Read32();
}
else if (Font->IndexToLocFormat == 0)
{
READ_Offset = Font->LocaOffset + GlyphIndex * sizeof(u16);
OffsetToGlyph = 2 * (smm)Read16();
}
else
{
OffsetToGlyph = 0;
Assert(false);
}
READ_Offset = Font->GlyfOffset + OffsetToGlyph;
smm GLYF_NumberOfContours = (s16)Read16();
smm GLYF_XMin = (s16)Read16();
smm GLYF_YMin = (s16)Read16();
smm GLYF_XMax = (s16)Read16();
smm GLYF_YMax = (s16)Read16();
Print("FontSize %f", Font->FontSize);
Print("ScaleFactor %f", Font->ScaleFactor);
Print("GLYF_NumberOfContours %d", GLYF_NumberOfContours);
Print("GLYF_XMin %d", GLYF_XMin);
Print("GLYF_XMax %d", GLYF_XMax);
Print("GLYF_YMin %d", GLYF_YMin);
Print("GLYF_YMax %d", GLYF_YMax);
if (GLYF_NumberOfContours == -1) // COMPOSITE glyph
{
return 0;
}
u16 *EndPtsOfContours = ArenaAlloc(TempArena, u16, GLYF_NumberOfContours);
for (smm ContourIndex = 0; ContourIndex < GLYF_NumberOfContours; ++ContourIndex)
{
EndPtsOfContours[ContourIndex] = Read16();
}
smm InstructionLength = Read16();
if (InstructionLength > 0)
{
Print("InstructionLength %d", InstructionLength);
u8 *Instructions = ArenaAlloc(TempArena, u8, InstructionLength);
for (smm Index = 0; Index < InstructionLength; ++Index)
{
Instructions[Index] = Read8();
}
}
smm LastPointIndex = EndPtsOfContours[GLYF_NumberOfContours - 1];
smm PointCount = LastPointIndex + 1;
Print("PointCount %d", PointCount);
u8 *Flags = ArenaAlloc(TempArena, u8, PointCount);
for (smm Index = 0; Index < PointCount; ++Index)
{
Flags[Index] = Read8();
smm IsRepeated = ((Flags[Index] & SIMPLE_GLYPH_FLAG_REPEAT_FLAG) != 0);
if (IsRepeated)
{
smm RepeatCount = Read8();
Print("RepeatCount %d", RepeatCount);
u8 FlagToRepeat = Flags[Index];
for (smm RepeatIndex = 0; RepeatIndex < RepeatCount; ++RepeatIndex)
{
Assert((Index + 1) <= PointCount);
Flags[++Index] = FlagToRepeat;
}
}
}
s16 *PointsX = ArenaAlloc(TempArena, s16, PointCount);
s16 *PointsY = ArenaAlloc(TempArena, s16, PointCount);
TTFGlyfParseCoords(READ_Data, &READ_Offset, READ_Size, Flags, PointCount, PointsX, 0); // X
TTFGlyfParseCoords(READ_Data, &READ_Offset, READ_Size, Flags, PointCount, PointsY, 1); // Y
//
// Scale points
//
f32 Scale = Font->FontSize * Font->ScaleFactor;
v2 *Points = ArenaAlloc(TempArena, v2, PointCount);
for (smm Index = 0; Index < PointCount; ++Index)
{
Points[Index] = v2{ (f32)PointsX[Index], (f32)PointsY[Index] } * v2{ Scale, -Scale };
}
//
// Gather all edges
//
smm MaxEdgeCount = PointCount * 2;
smm EdgeCount = 0;
glyph_edge *Edges = ArenaAlloc(TempArena, glyph_edge, MaxEdgeCount);
u8 *EdgesYSwap = ArenaAlloc(TempArena, u8, MaxEdgeCount);
{
v2 PrevPoint;
smm PointIndex = 0;
for (smm ContourIndex = 0;
ContourIndex < GLYF_NumberOfContours;
++ContourIndex)
{
smm FirstInContour = PointIndex;
PrevPoint = Points[PointIndex];
++PointIndex;
for (; PointIndex <= EndPtsOfContours[ContourIndex]; )
{
v2 ThisPoint = Points[PointIndex];
smm IsOnCurve = ((Flags[PointIndex] & SIMPLE_GLYPH_FLAG_ON_CURVE_POINT) != 0);
if (IsOnCurve)
{
MakeEdge(ThisPoint, PrevPoint,
&EdgeCount, MaxEdgeCount, Edges, EdgesYSwap);
}
else
{
//
// @Todo TO DO DEBUG ------------ THERE IS A MISTAKE IN THE BEZIER INTERPOLATION MAKING EDGES
// @Debug
//
#if 0
v2 NextPoint = Points[PointIndex + 1];
smm IsNextOnCurve = ((Flags[PointIndex + 1] & SIMPLE_GLYPH_FLAG_ON_CURVE_POINT) != 0);
if (!IsNextOnCurve)
{
// Split the CUBIC curve into TWO QUADRATIC curves
// This is the midpoint we use to do the split
NextPoint = ThisPoint + 0.5f * (NextPoint - ThisPoint);
}
//
// Tesselate bezier
//
smm LastIndex = EdgeCount - 1;
v2 LastEdgePoint = (EdgesYSwap[LastIndex] ?
Edges[LastIndex].PrevPoint : Edges[LastIndex].ThisPoint);
smm BezierIterations = 1;
for (smm Index = 0; Index <= BezierIterations; ++Index)
{
f32 T = (f32)Index * (1.0f / (f32)BezierIterations);
v2 Point =
Square(1.0f - T) * PrevPoint +
2.0f * T * (1.0f - T) * ThisPoint +
Square(T) * NextPoint;
MakeEdge(Point, LastEdgePoint,
&EdgeCount, MaxEdgeCount, Edges, EdgesYSwap);
LastEdgePoint = Point;
}
ThisPoint = PrevPoint;
#else
MakeEdge(ThisPoint, PrevPoint,
&EdgeCount, MaxEdgeCount, Edges, EdgesYSwap);
#endif
}
// Next point
PrevPoint = ThisPoint;
++PointIndex;
}
// Close the contours
MakeEdge(Points[FirstInContour], PrevPoint,
&EdgeCount, MaxEdgeCount, Edges, EdgesYSwap);
}
}
//
// Debug print of all edge points
//
Print("EdgeCount %d", EdgeCount);
for (smm Index = 0; Index < EdgeCount; ++Index)
{
Print("%f, %f\n%f, %f\n",
Edges[Index].X1, Edges[Index].Y1,
Edges[Index].X2, Edges[Index].Y2);
}
//
// Insertion Sort for Glyph Edges
// @Todo Time it and choose a better algorithm
//
for (smm SortIndex = 1; SortIndex < EdgeCount; ++SortIndex)
{
glyph_edge ThisEdge = Edges[SortIndex];
b8 ThisYSwap = EdgesYSwap[SortIndex];
// Y1 is always higher than Y2
// because we swap them inside MakeEdge
smm J = SortIndex - 1;
for (; (J >= 0) && (Edges[J].Y1 > ThisEdge.Y1); --J)
{
Edges[J + 1] = Edges[J];
EdgesYSwap[J + 1] = EdgesYSwap[J];
}
Edges[J + 1] = ThisEdge;
EdgesYSwap[J + 1] = ThisYSwap;
}
//
// Glyph Bounding Box
//
smm BBoxX1 = FloorF32(GLYF_XMin * +Scale);
smm BBoxY1 = FloorF32(GLYF_YMax * -Scale);
smm BBoxX2 = CeilF32(GLYF_XMax * +Scale);
smm BBoxY2 = CeilF32(GLYF_YMin * -Scale);
smm BBoxWidth = BBoxX2 - BBoxX1;
smm BBoxHeight = BBoxY2 - BBoxY1;
Print("BBoxX1 %d", BBoxX1);
Print("BBoxX2 %d", BBoxX2);
Print("BBoxY1 %d", BBoxY1);
Print("BBoxY2 %d", BBoxY2);
Print("BBoxWidth %d", BBoxWidth);
Print("BBoxHeight %d", BBoxHeight);
//
// Glyph Rasterization
//
glyph_active_edge *ActiveEdges = ArenaAlloc(TempArena, glyph_active_edge, EdgeCount);
smm ActiveEdgeCount = 0;
#define FONT_CELL_WIDTH 64
#define FONT_CELL_HEIGHT 64
// Clear the outputing image to black
u8 *OutputImageU8 = ArenaAlloc(TempArena, u8, FONT_CELL_WIDTH * FONT_CELL_HEIGHT);
smm EdgeIndex = 0; // Continous edge index
f32 ScanlineY = (f32)BBoxY1;
for (smm ScanlineIndex = 0; ScanlineIndex < BBoxHeight; ++ScanlineIndex)
{
f32 ScanlineTop = ScanlineY + 0.0f;
f32 ScanlineBottom = ScanlineY + 1.0f;
// Remove active edges that terminate before the top of the scanline
for (smm ActiveEdgeIndex = ActiveEdgeCount - 1; ActiveEdgeIndex >= 0; --ActiveEdgeIndex)
{
if (ActiveEdges[ActiveEdgeIndex].EY <= ScanlineTop)
{
// Delete from array
for (smm ShiftIndex = ActiveEdgeIndex + 1; ShiftIndex < ActiveEdgeCount; ++ShiftIndex)
{
ActiveEdges[ShiftIndex - 1] = ActiveEdges[ShiftIndex];
}
--ActiveEdgeCount;
}
}
// Add active edges that start before the bottom of the scanline
for (; EdgeIndex < EdgeCount; ++EdgeIndex)
{
glyph_edge Edge = Edges[EdgeIndex];
// The edge is active if it is below the scanline
if (Edge.Y1 <= ScanlineBottom)
{
f32 DXDY = (Edge.X2 - Edge.X1) / (Edge.Y2 - Edge.Y1);
glyph_active_edge ActiveEdge;
ActiveEdge.FX = Edge.X1 + DXDY * (ScanlineTop - Edge.Y1) - /* Offset */(f32)BBoxX1;
ActiveEdge.FDeltaX = DXDY;
ActiveEdge.FDeltaY = ((DXDY != 0.0f) ? (1.0f / DXDY) : 0.0f); // Safe divide, default 0
ActiveEdge.Direction = (EdgesYSwap[EdgeIndex] ? 1.0f : -1.0f);
ActiveEdge.SY = Edge.Y1;
ActiveEdge.EY = Edge.Y2;
if ((ScanlineIndex == 0) && (BBoxY1 != 0))
{
if (ActiveEdge.EY < ScanlineTop)
{
ActiveEdge.EY = ScanlineTop;
}
}
Assert((ActiveEdgeCount + 1) <= EdgeCount);
ActiveEdges[ActiveEdgeCount++] = ActiveEdge;
// @Note Removing this Assert because it fires, fp error?
// Assert(ActiveEdge.EY >= ScanlineTop);
}
else
{
// Other edges cannot be active
break;
}
}
//
// stb_truetype rasterizer
// https://nothings.org/gamedev/rasterize/
// https://github.com/nothings/stb/blob/master/stb_truetype.h
//
f32 ScanLine1[FONT_CELL_WIDTH + (FONT_CELL_WIDTH + 1)] = {};
f32 *ScanLine2 = ScanLine1 + FONT_CELL_WIDTH;
for (smm ActiveEdgeIndex = ActiveEdgeCount - 1; ActiveEdgeIndex >= 0; --ActiveEdgeIndex)
{
glyph_active_edge *Edge = &ActiveEdges[ActiveEdgeIndex];
//
// Compute intersection points with top & bottom
//
Assert(Edge->EY >= ScanlineTop);
if (Edge->FDeltaX == 0)
{
f32 X1 = Edge->FX;
if (X1 < (f32)BBoxWidth)
{
if (X1 >= 0)
{
HandleClippedEdge(ScanLine1, (smm)X1 + 0, Edge, X1, ScanlineTop, X1, ScanlineBottom);
HandleClippedEdge(ScanLine2, (smm)X1 + 1, Edge, X1, ScanlineTop, X1, ScanlineBottom);
}
else
{
HandleClippedEdge(ScanLine2, /* PADD */ 0, Edge, X1, ScanlineTop, X1, ScanlineBottom);
}
}
}
else
{
f32 X0 = Edge->FX;
f32 DX = Edge->FDeltaX;
f32 XB = X0 + DX;
f32 DY = Edge->FDeltaY;
Assert((Edge->SY <= ScanlineBottom) &&
(Edge->EY >= ScanlineTop));
f32 XTop = ((Edge->SY > ScanlineTop) ?
(X0 + DX * (Edge->SY - ScanlineTop)) : X0);
f32 XBottom = ((Edge->EY < ScanlineBottom) ?
(X0 + DX * (Edge->EY - ScanlineTop)) : XB);
f32 SY0 = ((Edge->SY > ScanlineTop) ? Edge->SY : ScanlineTop);
f32 SY1 = ((Edge->EY < ScanlineBottom) ? Edge->EY : ScanlineBottom);
#define GetTrapezoidArea(Height, TX0, TX1, BX0, BX1)\
( ( ( (TX1) - (TX0) ) + ( (BX1) - (BX0) ) ) * 0.5f * (Height) )
if ((XTop >= 0) && (XBottom >= 0) && (XTop < (f32)BBoxWidth) && (XBottom < (f32)BBoxWidth))
{
if ((smm)XTop == (smm)XBottom)
{
// Simple case, only spans one pixel
smm X = (smm)XTop;
f32 Height = (SY1 - SY0) * Edge->Direction;
Assert((X >= 0) && (X < BBoxWidth));
ScanLine1[X + 0] += GetTrapezoidArea(Height, XTop, ((f32)X + 1.0f), XBottom, ((f32)X + 1.0f));
ScanLine2[X + 1] += Height; // Everything right of this pixel is filled
}
else
{
// Covers 2+ pixels
if (XTop > XBottom)
{
// Flip scanline vertically. Signed area stays the same
SY0 = ScanlineBottom - (SY0 - ScanlineTop);
SY1 = ScanlineBottom - (SY1 - ScanlineTop);
f32 Temp;
Temp = SY0;
SY0 = SY1;
SY1 = Temp;
Temp = XBottom;
XBottom = XTop;
XTop = Temp;
DX = -DX;
DY = -DY;
Temp = X0;
X0 = XB;
XB = Temp;
}
Assert(DY >= 0.0f);
Assert(DX >= 0.0f);
smm X1 = (smm)XTop;
smm X2 = (smm)XBottom;
// Compute intersection with Y axis at (X1 + 1)
f32 YCrossing = ScanlineTop + DY * ((f32)(X1 + 1) - X0);
// Compute intersection with Y axis at X2
f32 YFinal = ScanlineTop + DY * ((f32)X2 - X0);
YCrossing = Min(YCrossing, ScanlineBottom);
f32 Sign = Edge->Direction;
// Area of the rectangle covered from SY0..YCrossing
f32 Area = Sign * (YCrossing - SY0);
// Area of the triangle (XTop,SY0), (X1+1,SY0), (X1+1,YCrossing)
ScanLine1[X1] += (Area * ((f32)(X1 + 1) - XTop)) * 0.5f;
// Check if final YCrossing is blown up
if (YFinal > ScanlineBottom)
{
YFinal = ScanlineBottom;
DY = (YFinal - YCrossing) / (f32)(X2 - (X1 + 1));
}
f32 Step = Sign * DY * 1.0f;
for (smm X = X1 + 1; X < X2; ++X)
{
// Area of trapezoid is (1 * (Step / 2))
ScanLine1[X] += Area + (Step * 0.5f);
Area += Step;
}
Assert(Abs(Area) <= 1.01f); // Accumulated error from Area += Step unless we round Step down
Assert(SY1 > (YFinal - 0.01f));
// Area covered in the last pixel is the rectangle from all the pixels to the left,
// plus the trapezoid filled by the line segment in this pixel all the way to the right edge
f32 TrapezoidArea = GetTrapezoidArea((SY1 - YFinal), (f32)X2, (f32)(X2 + 1), XBottom, (f32)(X2 + 1));
ScanLine1[X2] += Area + Sign * TrapezoidArea;
// The rest of the line is filled based on the total height of the line segment in this pixel
ScanLine2[X2 + 1] += Sign * (SY1 - SY0);
}
}
else
{
// Uncommon scenario
for (smm X = 0; X < BBoxWidth; ++X) {
// Rename variables to clearly-defined pairs
f32 Y0 = ScanlineTop;
f32 X1 = (f32)(X + 0);
f32 X2 = (f32)(X + 1);
f32 X3 = XB;
f32 Y3 = ScanlineBottom;
f32 Y1 = ((f32)(X + 0) - X0) / DX + ScanlineTop;
f32 Y2 = ((f32)(X + 1) - X0) / DX + ScanlineTop;
if (X0 < X1 && X3 > X2)
{
// Three segments descending down-right
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X1, Y1);
HandleClippedEdge(ScanLine1, X, Edge, X1, Y1, X2, Y2);
HandleClippedEdge(ScanLine1, X, Edge, X2, Y2, X3, Y3);
}
else if (X3 < X1 && X0 > X2)
{
// Three segments descending down-left
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X2, Y2);
HandleClippedEdge(ScanLine1, X, Edge, X2, Y2, X1, Y1);
HandleClippedEdge(ScanLine1, X, Edge, X1, Y1, X3, Y3);
}
else if (X0 < X1 && X3 > X1)
{
// Two segments across X, down-right
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X1, Y1);
HandleClippedEdge(ScanLine1, X, Edge, X1, Y1, X3, Y3);
}
else if (X3 < X1 && X0 > X1)
{
// Two segments across X, down-left
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X1, Y1);
HandleClippedEdge(ScanLine1, X, Edge, X1, Y1, X3, Y3);
}
else if (X0 < X2 && X3 > X2)
{
// Two segments across X+1, down-right
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X2, Y2);
HandleClippedEdge(ScanLine1, X, Edge, X2, Y2, X3, Y3);
}
else if (X3 < X2 && X0 > X2)
{
// Two segments across X+1, down-left
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X2, Y2);
HandleClippedEdge(ScanLine1, X, Edge, X2, Y2, X3, Y3);
}
else
{
// One segment
HandleClippedEdge(ScanLine1, X, Edge, X0, Y0, X3, Y3);
}
}
}
}
}
//
// Write pixels to the texture buffer
//
f32 Sum = 0;
for (smm X = 0; X < BBoxWidth; ++X)
{
Sum += ScanLine2[X];
f32 K = ScanLine1[X] + Sum;
K = Abs(K) * 255.0f + 0.5f;
u32 M = (u32)K;
M = Min(M, 255);
u8 *ImageLine = OutputImageU8 + ScanlineIndex * FONT_CELL_WIDTH;
ImageLine[X] = (u8)M;
}
// Advance all active edges
for (smm Index = 0; Index < ActiveEdgeCount; ++Index)
{
ActiveEdges[Index].FX += ActiveEdges[Index].FDeltaX;
}
// Advance the scanline y
ScanlineY += 1.0f;
}
// Normalize the FontImage from single channel monochromic R to RGBA
u32 *OutputImageU32 = ArenaAlloc(TempArena, u32, FONT_CELL_WIDTH * FONT_CELL_HEIGHT);
for (smm Index = 0; Index < (FONT_CELL_WIDTH * FONT_CELL_HEIGHT); ++Index)
{
u8 *OutputImageChannels = (u8 *)(OutputImageU32 + Index);
u8 ColorValue = OutputImageU8[Index];
OutputImageChannels[0] = ColorValue;
OutputImageChannels[1] = ColorValue;
OutputImageChannels[2] = ColorValue;
OutputImageChannels[3] = ColorValue;
}
//
// Find the advance width of the glyph
//
smm HMetricIndex = Min(GlyphIndex, Font->HMetricCount);
// u16 AdvanceWidth; u16 LeftSideBearing;
READ_Offset = Font->HmtxOffset + HMetricIndex * sizeof(u16) * 2;
smm AdvanceWidth = Read16();
//
// Find a slot for the glyph in the atlas
//
smm LoadedGlyphIndex;
if (Font->LoadedGlyphCount == Font->MaxLoadedGlyphCount)
{
// Find the most irrelevant glyph to replace
// starting with an unreserved glyph
LoadedGlyphIndex = Font->ReservedGlyphCount;
smm ReplaceUsedFrame = Font->LoadedGlyphsUsedFrame[LoadedGlyphIndex];
for (smm LoadedIndex = LoadedGlyphIndex;
LoadedIndex < Font->LoadedGlyphCount;
++LoadedIndex)
{
smm UsedFrame = Font->LoadedGlyphsUsedFrame[LoadedIndex];
if (UsedFrame < ReplaceUsedFrame)
{
ReplaceUsedFrame = UsedFrame;
LoadedGlyphIndex = LoadedIndex;
}
}
}
else
{
LoadedGlyphIndex = Font->LoadedGlyphCount++;
}
Font->LoadedGlyphsUsedFrame[LoadedGlyphIndex] = GLOBAL_MainState->Frame;
Font->LoadedGlyphsCodepoint[LoadedGlyphIndex] = GlyphIndex;
loaded_glyph *Glyph = &Font->LoadedGlyphsData[LoadedGlyphIndex];
Glyph->Width = (u8)BBoxWidth;
Glyph->Height = (u8)BBoxHeight;
Glyph->OffsetX = (s8)BBoxX1;
Glyph->OffsetY = (s8)-BBoxY2;
Glyph->Advance = (u8)FloorF32((f32)AdvanceWidth * Scale);
Print("Width %d", Glyph->Width);
Print("Height %d", Glyph->Height);
Print("OffsetX %d", Glyph->OffsetX);
Print("OffsetY %d", Glyph->OffsetY);
Print("Advance %d", Glyph->Advance);
//
// Send the pixels to the GPU texture atlas
//
GLint TextureX = (GLint)((LoadedGlyphIndex & Font->LoadedGlyphMask) * FONT_CELL_WIDTH);
GLint TextureY = (GLint)((LoadedGlyphIndex >> Font->LoadedGlyphShift) * FONT_CELL_HEIGHT);
glTexSubImage2D(
GL_TEXTURE_2D, 0,
TextureX, TextureY, FONT_CELL_WIDTH, FONT_CELL_HEIGHT, // (GLint)BBoxWidth, (GLint)BBoxHeight
PLATFORM_OPENGL_UPLOAD_FORMAT, GL_UNSIGNED_BYTE, OutputImageU32
);
return LoadedGlyphIndex;
}
internal inline void ParseTTF()
{
font *Font = &GLOBAL_MainState->Font;
memory_arena *FontArena = &Font->Arena;
// @Todo Dynamically choosing font size
Font->FontSize = 64.0f;
//
// Atlas sizing
//
Font->MaxLoadedGlyphCount = 128;
smm GlyphSectionWidth = TEXTURE_SIZE;
smm GlyphCountX = GlyphSectionWidth / FONT_CELL_WIDTH;
// smm GlyphCountY = Font->MaxLoadedGlyphCount / GlyphCountX;
// smm GlyphSectionHeight = GlyphCountY * FONT_CELL_HEIGHT;
// @Todo Use GlyphSectionHeight to position other texture atlas for images
Font->LoadedGlyphMask = (u8)(GlyphCountX - 1);
Font->LoadedGlyphShift = (u8)BitScanForwardU32(GlyphCountX);
Font->ReservedGlyphCount = 11; // null, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Font->LoadedGlyphCount = 0;
Font->LoadedGlyphsCodepoint = ArenaAlloc(FontArena, u32, Font->MaxLoadedGlyphCount);
Font->LoadedGlyphsUsedFrame = ArenaAlloc(FontArena, u16, Font->MaxLoadedGlyphCount);
Font->LoadedGlyphsData = ArenaAlloc(FontArena, loaded_glyph, Font->MaxLoadedGlyphCount);
Print("Mask %d, Shift %d", Font->LoadedGlyphMask, Font->LoadedGlyphShift);
//
// Parse the TTF metadata
//
u8 *READ_Data = Font->FileData;
smm READ_Size = Font->FileSize;
smm READ_Offset = 0; // Offset in the TTF file, Read8,16,32,64 functions use this offset
Print("READ_Data %lld, READ_Size %d, READ_Offset %d", READ_Data, READ_Size, READ_Offset);
// Disable unused variable warnings for this part of the code
// Because we parse unused fields as well
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
u32 DirVersion = Read32();
u16 DirNumTables = Read16();
u16 DirSearchRange = Read16();
u16 DirEntrySelector = Read16();
u16 DirRangeShift = Read16();
Assert((DirVersion == 0x00010000) || (DirVersion == 0x4F54544F));
for (smm DirIndex = 0; DirIndex < DirNumTables; ++DirIndex)
{
//
// Table metadata from table directory
//
char TableTag[4];
TableTag[0] = Read8();
TableTag[1] = Read8();
TableTag[2] = Read8();
TableTag[3] = Read8();
u32 TableCheckSum = Read32();
u32 TableOffset = Read32();
u32 TableLength = Read32();
smm TableEndOffset = READ_Offset;
//
// Parse the specific type of table
//
READ_Offset = TableOffset;
if (*(u32 *)TableTag == *(u32 *)"head")
{
u16 HEAD_MajorVersion = Read16();
u16 HEAD_MinorVersion = Read16();
u32 HEAD_FontRevision = Read32();
u32 HEAD_ChecksumAdjustment = Read32();
u32 HEAD_MagicNumber = Read32();
u16 HEAD_Flags = Read16();
u16 HEAD_UnitsPerEm = Read16();
u64 HEAD_Created = Read64();
u64 HEAD_Modified = Read64();
u16 HEAD_XMin = Read16();
u16 HEAD_YMin = Read16();
u16 HEAD_XMax = Read16();
u16 HEAD_YMax = Read16();
u16 HEAD_MacStyle = Read16();
u16 HEAD_LowestRecPPEM = Read16();
u16 HEAD_FontDirectionHint = Read16();
u16 HEAD_IndexToLocFormat = Read16();
u16 HEAD_GlyphDataFormat = Read16();
// Store the format of the 'loca' table, index format can be 16-bit or 32-bit
Font->IndexToLocFormat = HEAD_IndexToLocFormat;
}
else if (*(u32 *)TableTag == *(u32 *)"maxp")
{
u32 MAXP_Version = Read32();
u16 MAXP_NumGlyphs = Read16();
Font->GlyphCount = MAXP_NumGlyphs;
}
else if (*(u32 *)TableTag == *(u32 *)"cmap")
{
u16 CMAP_Version = Read16();
u16 CMAP_NumTables = Read16();
for (smm TableIndex = 0; TableIndex < CMAP_NumTables; ++TableIndex)
{
u16 CMAP_RecordPlatformID = Read16();
u16 CMAP_RecordEncodingID = Read16();
u32 CMAP_RecordSubtableOffset = Read32();
smm RecordTablesOffset = READ_Offset;
if (CMAP_RecordPlatformID == 3) // Platform ID Windows
{
READ_Offset = TableOffset + CMAP_RecordSubtableOffset;
smm CMAP_Format = Read16();
if (CMAP_RecordEncodingID == 1) // BMP Unicode Plane
{
Assert(CMAP_Format == 4);
u16 CMAP_Length = Read16();
u16 CMAP_Language = Read16(); // Macintosh platform only
u16 CMAP_SegCountX2 = Read16();
u16 CMAP_SearchRange = Read16();
u16 CMAP_EntrySelector = Read16();
u16 CMAP_RangeShift = Read16();
// @Todo Is Big Endian swapping here even worth it?
// We can just do it in UTF8ToCodepoint without wasting memory
smm SegCount = CMAP_SegCountX2 / 2;
Font->CMAP_SegmentCount = SegCount;
Font->CMAP_EndCodes = ArenaAlloc(FontArena, u16, SegCount);
Font->CMAP_StartCodes = ArenaAlloc(FontArena, u16, SegCount);
Font->CMAP_IDDeltas = ArenaAlloc(FontArena, s16, SegCount);
Font->CMAP_IDRangeOffsets = ArenaAlloc(FontArena, u16, SegCount);
for (smm Index = 0; Index < SegCount; ++Index)
{
Font->CMAP_EndCodes[Index] = Read16();
}
u16 CMAP_ReservedPadding = Read16();
for (smm Index = 0; Index < SegCount; ++Index)
{
Font->CMAP_StartCodes[Index] = Read16();