-
Notifications
You must be signed in to change notification settings - Fork 41
/
TERRA_TTF.pas
1575 lines (1333 loc) · 43.6 KB
/
TERRA_TTF.pas
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
{***********************************************************************************************************************
*
* TERRA Game Engine
* ==========================================
*
* Copyright (C) 2003, 2014 by Sérgio Flores
*
***********************************************************************************************************************
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
**********************************************************************************************************************
* TERRA_TTF
* Implements TTF font loading
***********************************************************************************************************************
}
Unit TERRA_TTF;
{$I terra.inc}
Interface
Uses TERRA_Object, TERRA_String, TERRA_Utils, TERRA_Stream, TERRA_Font, TERRA_Color, TERRA_FileUtils, TERRA_EdgeList, TERRA_Image, TERRA_FileFormat;
{$RANGECHECKS OFF}
Type
TTFFormat = Class(TERRAFileFormat)
Public
Function Identify(Source:TERRAStream):Boolean; Override;
Function LoadFromStream(Target:TERRAObject; Source:TERRAStream):Boolean; Override;
End;
TStBttVertex = record
x,y,cx,cy: Smallint;
vertexType,padding: Byte;
end;
TStBttVertexArray = Record
List:Array Of TStBttVertex;
Count:Integer;
End;
TStBttPoint = record
x,y: Single;
end;
TStBttPointArray = Record
List:Array Of TStBttPoint;
Count:Integer;
End;
TStBttActiveEdge = Class(TERRAObject)
x,dx: Integer;
ey: Single;
next: TStBttActiveEdge;
valid: Integer;
End;
TTFContourArray = Record
List:Array Of Integer;
Count:Integer;
End;
TTFFont = Class(FontGlyphFactory)
Private
_Scale:Single;
_Ready:Boolean;
_Scanline:Array Of Byte;
_GlyphCount:Integer; // number of glyphs, needed for range checking
_Data:PByteArray; // pointer to .ttf file
_DataSize:Cardinal;
loca,head,glyf,hhea,hmtx, kern: Cardinal; // table locations as offset from start of .ttf
index_map: Integer; // a cmap mapping for our chosen character encoding
indexToLocFormat: Integer; // format needed to map from glyph index to glyph
Function ttBYTE(Const offset: Cardinal): Byte;
Function ttUSHORT(Const offset: Cardinal): Word;
Function ttSHORT(Const offset: Cardinal): Smallint;
Function ttULONG(Const offset: Cardinal): Cardinal;
Function ttLONG(Const offset: Cardinal): Integer;
Function stbtt_tag(Const offset: Cardinal; Const TableTag:FileHeader): Boolean;
Function stbtt__find_table(Const TableTag:FileHeader): Cardinal;
Procedure stbtt_GetGlyphHMetrics(GlyphIndex: Integer; var advanceWidth, leftSideBearing: Integer);
Function stbtt_GetGlyphKernAdvance(glyph1: Integer; glyph2: Integer): Integer;
Function stbtt_GetGlyphBitmap(scale_x, scale_y, shift_x, shift_y: Single; glyph: Integer; var xoff, yoff: Integer):TERRAImage;
Procedure stbtt_GetGlyphShape(glyph_index: Integer; Out Result:TStBttVertexArray);
Function stbtt__GetGlyfOffset(glyph_index: Integer): Integer;
Function stbtt_GetGlyphBox(glyph_index: Integer; var x0, y0, x1, y1: Integer): Integer;
Procedure stbtt_GetGlyphBitmapBox(glyph: Integer; scale_x, scale_y, shift_x, shift_y: Single; var ix0, iy0, ix1, iy1: Integer);
Function stbtt_GetCodepointKernAdvance(ch1: Integer; ch2: Integer): Integer;
Procedure stbtt_GetCodepointHMetrics(codepoint: Integer; var advanceWidth, leftSideBearing: Integer);
Procedure stbtt_GetCodepointShape(unicode_codepoint: Integer; Out vertices: TStBttVertexArray);
Function stbtt_GetCodepointBox(codepoint: Integer; var x0, y0, x1, y1: Integer): Integer;
Procedure stbtt_GetCodepointBitmapBox(codepoint: Integer; scale_x, scale_y: Single; var ix0, iy0, ix1, iy1: Integer);
Procedure stbtt_Rasterize(resultBitmap:TERRAImage; FlatnessInPixels: Single; Var Vertices:TStBttVertexArray; ScaleX, ScaleY, ShiftX, ShiftY: Single; XOff, YOff, Invert: Integer);
Procedure stbtt_GetFontVMetrics(var ascent, descent, lineGap: Integer);
Procedure stbtt_setvertex(var v:TStBttVertex; typeByte: Byte; x, y, cx, cy: Smallint);
Procedure stbtt__rasterize(resultBitmap:TERRAImage; Var Pts:TStBttPointArray; Var Windings:TTFContourArray; ScaleX, ScaleY, ShiftX, ShiftY: Single; XOff, YOff, Invert: Integer);
Procedure stbtt_FlattenCurves(Var Vertices:TStBttVertexArray; ObjSpaceFlatness: Single; Out Contours:TTFContourArray; Out Windings:TStBttPointArray);
Procedure stbtt__add_point(Var points:TStBttPointArray; n: Integer; x, y: Single);
Function stbtt__tesselate_curve(Var points:TStBttPointArray; var num_points: Integer; x0, y0, x1, y1, x2, y2, objspace_flatness_squared: Single; n: Integer): Integer;
Procedure stbtt__rasterize_sorted_edges(resultBitmap:TERRAImage; e:EdgeList; vsubsample, off_x, off_y: Integer);
Procedure stbtt__fill_active_edges(scanline: PByteArray; len: Integer; e:TStBttActiveEdge; max_weight: Integer);
Function new_active(Const e: TStBttEdge; off_x: Integer; start_point: Single):TStBttActiveEdge;
Public
Procedure LoadFromStream(Source:TERRAStream); Override;
Procedure Release; Override;
Function stbtt_FindGlyphIndex(unicode_codepoint: Integer):Word;
Function GetCodepointBitmap(scaleX, scaleY: Single; codepoint: Integer; var xoff, yoff: Integer):TERRAImage;
Function ScaleForPixelHeight(height: Single): Single;
Function HasGlyph(ID:TERRAChar):Boolean;
Function InitGlyph(Font:TERRAFont; ID:TERRAChar; Size:Integer):FontGlyph; Override;
Function GetKerning(Current, Next:TERRAChar):Integer; Override;
Property Ready:Boolean Read _Ready;
End;
Implementation
Uses TERRA_Log, TERRA_FileManager, TERRA_FontManager, TERRA_Engine, TERRA_Math;
// platformID
const STBTT_PLATFORM_ID_UNICODE = 0;
const STBTT_PLATFORM_ID_MAC = 1;
const STBTT_PLATFORM_ID_ISO = 2;
const STBTT_PLATFORM_ID_MICROSOFT = 3;
const STBTT_MS_EID_SYMBOL = 0;
const STBTT_MS_EID_UNICODE_BMP = 1;
const STBTT_MS_EID_SHIFTJIS = 2;
const STBTT_MS_EID_UNICODE_FULL = 10;
const FIXSHIFT = 10;
const FIX = (1 shl FIXSHIFT);
const FIXMASK = (FIX-1);
const STBTT_vmove = 1;
const STBTT_vline = 2;
const STBTT_vcurve = 3;
{ TTF }
Procedure TTFFont.LoadFromStream(Source:TERRAStream);
var
cmap, t: Cardinal;
encoding_record: Cardinal;
i,numTables: Cardinal;
begin
_Data := Nil;
_DataSize := 0;
_Ready := False;
If (Source = Nil) then
Begin
Engine.Log.Write(logError, 'TTF', 'Null stream!');
Exit;
End;
// read file
_DataSize := Source.Size;
GetMem(_Data, _DataSize);
Source.Read(@_Data[0], _DataSize);
cmap := stbtt__find_table('cmap');
loca := stbtt__find_table('loca');
head := stbtt__find_table('head');
glyf := stbtt__find_table('glyf');
hhea := stbtt__find_table('hhea');
hmtx := stbtt__find_table('hmtx');
kern := stbtt__find_table('kern');
If (cmap=0) or (loca=0) or (head=0) or (glyf=0) or (hhea=0) or (hmtx=0) then
Begin
Engine.Log.Write(logError, 'TTF', 'Invalid font file: '+ Source.Name);
Exit;
End;
t := stbtt__find_table('maxp');
if t<>0 then
_GlyphCount := ttUSHORT(t+4)
else
_GlyphCount := -1;
// find a cmap encoding table we understand *now* to avoid searching
// later. (todo: could make this installable)
// the same regardless of glyph.
numTables := Integer(ttUSHORT( cmap + 2));
index_map := 0;
for i:=0 to numTables-1 do
begin
encoding_record := cmap + 4 + 8 * i;
// find an encoding we understand:
case ttUSHORT(encoding_record) of
STBTT_PLATFORM_ID_MICROSOFT:
case ttUSHORT(encoding_record+2) of
STBTT_MS_EID_UNICODE_BMP,
STBTT_MS_EID_UNICODE_FULL:
// MS/Unicode
index_map := cmap + ttULONG( encoding_record+4);
end;
end;
end;
If index_map = 0 then
Begin
Engine.Log.Write(logError, 'TTF', 'Could not find font index map: '+ Source.Name);
Exit;
End;
indexToLocFormat := ttUSHORT(head + 50);
_Ready := True;
//t := stbtt_FindGlyphIndex(35821);
//IntToString(T);
End;
Procedure TTFFont.Release;
Begin
If Assigned(_Data) Then
Begin
FreeMem(_Data);
_Data := Nil;
End;
End;
function TTFFont.ttBYTE(Const offset:Cardinal):Byte;
begin
If (Offset>=_DataSize) Then
Result := 0
Else
Begin
Result := _Data[offset];
End;
end;
function TTFFont.ttUSHORT(Const offset:Cardinal): Word;
begin
If (Offset>=_DataSize) Then
Result := 0
Else
Begin
Result := (_Data[offset] shl 8) + _Data[offset+1];
End;
end;
function TTFFont.ttSHORT(Const offset:Cardinal): Smallint;
begin
If (Offset>=_DataSize) Then
Result := 0
Else
Result := (SmallInt(_Data[offset]) shl 8) + SmallInt(_Data[offset+1]);
end;
function TTFFont.ttULONG(Const offset:Cardinal): Cardinal;
begin
If (Offset>=_DataSize) Then
Result := 0
Else
Result := (PtrUInt(_Data[offset]) shl 24) + (PtrUInt(_Data[offset+1]) shl 16) + (PtrUInt(_Data[offset+2]) shl 8) + Cardinal(_Data[offset+3]);
end;
function TTFFont.ttLONG(Const offset:Cardinal): Integer;
begin
If (Offset>=_DataSize) Then
Result := 0
Else
Result := PtrUInt(_Data[offset] shl 24) + PtrUInt(_Data[offset+1] shl 16) + PtrUInt(_Data[offset+2] shl 8) + PtrUInt(_Data[offset+3]);
end;
function TTFFont.stbtt_tag(Const offset:Cardinal; Const TableTag:FileHeader):Boolean;
begin
If (Offset>=_DataSize) Then
Result := False
Else
Result:=(_data[offset]=Byte(TableTag[1])) and (_data[offset+1]=Byte(TableTag[2])) and (_data[offset+2]=Byte(TableTag[3])) and (_data[offset+3]=Byte(TableTag[4]));
end;
// OPTIMIZE: binary search
function TTFFont.stbtt__find_table(Const TableTag:FileHeader): Cardinal;
var
num_tables: Integer;
tabledir: Cardinal;
i: Cardinal;
loc: Cardinal;
Begin
num_tables := ttUSHORT(4);
If (num_tables<=0) Then
Begin
Result := 0;
Exit;
End;
tabledir := 12;
For i:=0 to num_tables-1 do
Begin
Loc := tabledir + 16*i;
If stbtt_tag(loc, TableTag) then
Begin
Result := ttULONG(loc+8);
Break;
End;
End;
End;
procedure TTFFont.stbtt_GetGlyphHMetrics(GlyphIndex: Integer; var advanceWidth, leftSideBearing: Integer);
var
numOfLongHorMetrics: Cardinal;
begin
numOfLongHorMetrics := Integer(ttUSHORT(hhea + 34));
if GlyphIndex < numOfLongHorMetrics then
begin
advanceWidth := Integer(ttSHORT(hmtx + 4* GlyphIndex));
leftSideBearing := Integer(ttSHORT(hmtx + 4*GlyphIndex + 2));
end else begin
advanceWidth := Integer(ttSHORT(hmtx + 4*(numOfLongHorMetrics-1)));
leftSideBearing := Integer(ttSHORT( hmtx + 4*numOfLongHorMetrics + 2*(GlyphIndex - numOfLongHorMetrics)));
end;
end;
function TTFFont.stbtt_GetGlyphKernAdvance(glyph1: Integer; glyph2: Integer): Integer;
Var
needle, straw:Cardinal;
l, r, m:Integer;
Begin
Result := 0;
If (Self.kern<=0) Then
Exit;
// we only look at the first table. it must be 'horizontal' and format 0.
If (ttUSHORT( Self.kern +2) < 1) Then // number of tables
Exit;
If (ttUSHORT( Self.kern +8) <> 1) Then // horizontal flag, format
Exit;
l := 0;
r := ttUSHORT(Self.kern + 10) - 1;
needle := (glyph1 Shl 16) Or glyph2;
While (l <= r) Do
Begin
m := (l + r) Shr 1;
straw := ttULONG(Self.kern +18+(m*6)); // note: unaligned read
If (needle < straw) Then
r := m - 1
Else
If (needle > straw) Then
l := m + 1
Else
Begin
Result := ttSHORT(Self.kern + 22+(m*6));
Exit;
End;
End;
End;
function TTFFont.stbtt_GetCodepointKernAdvance(ch1: Integer; ch2: Integer): Integer;
begin
If (kern<=0) Then
Result := 0
Else
Result := stbtt_GetGlyphKernAdvance(stbtt_FindGlyphIndex(ch1), stbtt_FindGlyphIndex(ch2));
end;
procedure TTFFont.stbtt_GetCodepointHMetrics(codepoint: Integer; var advanceWidth, leftSideBearing: Integer);
begin
stbtt_GetGlyphHMetrics(stbtt_FindGlyphIndex(codepoint), advanceWidth, leftSideBearing);
end;
procedure TTFFont.stbtt_GetFontVMetrics(var ascent, descent, lineGap: Integer);
begin
ascent := Integer(ttSHORT( hhea + 4));
descent := Integer(ttSHORT( hhea + 6));
lineGap := Integer(ttSHORT( hhea + 8));
end;
Function TTFFont.ScaleForPixelHeight(height: Single): Single;
Var
fHeight: Integer;
Begin
If (_Ready) Then
Begin
fHeight := Integer(ttSHORT( hhea + 4) - ttSHORT( hhea + 6));
Result := height / fheight;
End Else
Result := 0.0;
End;
Function TTFFont.GetCodepointBitmap(scaleX, scaleY: Single; codepoint: Integer; var xoff, yoff: Integer):TERRAImage;
Begin
If (_Ready) Then
Result := stbtt_GetGlyphBitmap(scaleX, scaleY, 0, 0, stbtt_FindGlyphIndex(codepoint), xoff, yoff)
Else
Result := Nil;
End;
function TTFFont.stbtt_GetGlyphBitmap(scale_x, scale_y, shift_x, shift_y: Single; glyph: Integer; var xoff, yoff: Integer):TERRAImage;
var
ix0,iy0,ix1,iy1: Integer;
w,h:Integer;
vertices:TStBttVertexArray;
begin
Result := nil;
stbtt_GetGlyphShape(glyph, vertices);
if scale_x = 0 then
scale_x := scale_y;
if scale_y = 0 then
begin
if scale_x = 0 then
Exit;
scale_y := scale_x;
end;
stbtt_GetGlyphBitmapBox(glyph, scale_x, scale_y, shift_x, shift_y, ix0, iy0, ix1, iy1);
w := (ix1 - ix0);
h := (iy1 - iy0);
If (w<=0) Or (h<=0) Then
Exit;
// now we get the size
Result := TERRAImage.Create(w, h);
Result.ClearWithColor(ColorNull);
xoff := ix0;
yoff := iy0;
stbtt_Rasterize(Result, 0.35, vertices, scale_x, scale_y, shift_x, shift_y, ix0, iy0, 1);
End;
Function TTFFont.stbtt_FindGlyphIndex(unicode_codepoint: Integer):Word;
var
format: Smallint;
bytes: Integer;
first, count: Cardinal;
segcount, searchRange, entrySelector, rangeShift: Word;
item, offset, startValue, endValue: Word;
startValue2, endValue2: Word;
endCount: Cardinal;
search: Cardinal;
ngroups: Word;
low,high: Integer;
g: Word;
n, mid: Integer;
start_char: Cardinal;
end_char: Cardinal;
start_glyph: Cardinal;
Begin
format := ttUSHORT( index_map);
Case format Of
0: // apple byte encoding
Begin
bytes := Integer(ttUSHORT( index_map + 2));
if unicode_codepoint < bytes-6 then
begin
Result := Integer(_Data[index_map + 6 + unicode_codepoint]);
Exit;
end;
Result := 0;
End;
6:
Begin
first := Cardinal(ttUSHORT( index_map + 6));
count := Cardinal(ttUSHORT( index_map + 8));
if (Cardinal(unicode_codepoint) >= first) and (unicode_codepoint < first+count) then
begin
Result := Integer(ttUSHORT( PtrUInt(index_map) + 10 + (unicode_codepoint - first)*2));
Exit;
end;
Result := 0;
End;
2:
Begin
//STBTT_assert(0); // TODO: high-byte mapping for japanese/chinese/korean
Result := 0;
Exit;
End;
4: // standard mapping for windows fonts: binary search collection of ranges
Begin
segcount := ttUSHORT( index_map+6) shr 1;
searchRange := ttUSHORT( index_map+8) shr 1;
entrySelector := ttUSHORT( index_map+10);
rangeShift := ttUSHORT( index_map+12) shr 1;
// do a binary search of the segments
endCount := index_map + 14;
search := endCount;
if unicode_codepoint > $FFFF then
begin
Result := 0;
Exit;
end;
// they lie from endCount .. endCount + segCount
// but searchRange is the nearest power of two, so...
if unicode_codepoint >= Integer(ttUSHORT( search + rangeShift*2)) then
Inc(search, rangeShift*2);
// now decrement to bias correctly to find smallest
Dec(search, 2);
while entrySelector<>0 do
begin
//stbtt_uint16 start, end;
searchRange := searchRange shr 1;
startValue2 := ttUSHORT( search + 2 + segcount*2 + 2);
endValue2 := ttUSHORT( search + 2);
startValue2 := ttUSHORT( search + searchRange*2 + segcount*2 + 2);
endValue2 := ttUSHORT( search + searchRange*2);
if unicode_codepoint > endValue2 then
Inc(search, searchRange*2);
Dec(entrySelector);
end;
Inc(search, 2);
item := Word((search - endCount) shr 1);
//STBTT_assert(unicode_codepoint <= ttUSHORT(data + endCount + 2*item));
startValue := ttUSHORT( index_map + 14 + segcount*2 + 2 + 2*item);
endValue := ttUSHORT(index_map + 14 + 2 + 2*item);
if unicode_codepoint < startValue then
begin
//IntToString(unicode_codepoint); //BOO
Result:=0;
Exit;
end;
offset := Integer(ttUSHORT( index_map + 14 + segcount*6 + 2 + 2*item));
if offset = 0 then
begin
n := ttSHORT( index_map + 14 + segcount*4 + 2 + 2*item);
Result := unicode_codepoint + n;
Exit;
end;
Result := ttUSHORT( offset + (unicode_codepoint-startValue)*2 + index_map + 14 + segcount*6 + 2 + 2*item);
End;
12:
Begin
ngroups := ttUSHORT( index_map+6);
g := 0;
low := 0;
high := Integer(ngroups);
// Binary search the right group.
while low <= high do
begin
mid := low + ((high-low) shr 1); // rounds down, so low <= mid < high
start_char := ttULONG( index_map+16+mid*12);
end_char := ttULONG( index_map+16+mid*12+4);
if unicode_codepoint < start_char then
high := mid-1
else
if unicode_codepoint > end_char then
low := mid+1
else
Begin
start_glyph := ttULONG( index_map+16+mid*12+8);
Result := start_glyph + unicode_codepoint - start_char;
Exit;
End;
end;
Result := 0; // not found
End;
Else
Begin
// TODO
Result := 0;
End;
End;
End;
Procedure TTFFont.stbtt_GetCodepointShape(unicode_codepoint: Integer; Out vertices: TStBttVertexArray);
begin
stbtt_GetGlyphShape(stbtt_FindGlyphIndex(unicode_codepoint), vertices);
end;
procedure TTFFont.stbtt_setvertex(var v:TStBttVertex; typeByte: Byte; x, y, cx, cy: Smallint);
begin
v.vertexType := typeByte;
v.x := x;
v.y := y;
v.cx := cx;
v.cy := cy;
end;
function TTFFont.stbtt__GetGlyfOffset(glyph_index: Integer): Integer;
var
g1, g2: Integer;
begin
if glyph_index >= _GlyphCount then
begin
Result := -1; // glyph index out of range
Exit;
end;
if indexToLocFormat >= 2 then
begin
Result := -1; // unknown index->glyph map format
Exit;
end;
if indexToLocFormat = 0 then
begin
g1 := glyf + ttUSHORT( loca + glyph_index * 2) * 2;
g2 := glyf + ttUSHORT( loca + glyph_index * 2 + 2) * 2;
end else begin
g1 := glyf + ttULONG( loca + glyph_index * 4);
g2 := glyf + ttULONG( loca + glyph_index * 4 + 4);
end;
if g1=g2 then
Result := -1
else
Result := g1; // if length is 0, return -1
end;
function TTFFont.stbtt_GetGlyphBox(glyph_index: Integer; var x0, y0, x1, y1: Integer): Integer;
var
g: Integer;
begin
g := stbtt__GetGlyfOffset(glyph_index);
if g < 0 then
begin
Result := 0;
Exit;
end;
x0 := ttSHORT( g + 2);
y0 := ttSHORT( g + 4);
x1 := ttSHORT( g + 6);
y1 := ttSHORT( g + 8);
Result := 1;
end;
function TTFFont.stbtt_GetCodepointBox(codepoint: Integer; var x0, y0, x1, y1: Integer): Integer;
begin
Result := stbtt_GetGlyphBox(stbtt_FindGlyphIndex(codepoint), x0, y0, x1, y1);
end;
Procedure TTFFont.stbtt_GetGlyphShape(glyph_index: Integer; Out Result:TStBttVertexArray);
var
numberOfContours: Smallint;
endPtsOfContours:Cardinal;
g: Integer;
flags,flagcount: Byte;
ins,i,j,m,n,next_move,was_off,off: Integer;
x,y,cx,cy,sx,sy,dx,dy: Smallint;
more: Integer;
comp2:Cardinal;
points: PByteArray;
gidx: Word;
comp_verts: TStBttVertexArray;
ms,ns: Single;
mtx: array[0..5] of Single;
xx, yy: Smallint;
PointIndex: Integer;
TempSize:Integer;
begin
//stbtt_uint8 *data = info->data;
Result.List := Nil;
Result.Count := 0;
g := stbtt__GetGlyfOffset(glyph_index);
If (g < 0) Then
Exit;
numberOfContours := ttSHORT(g);
if numberOfContours > 0 then
begin
flags :=0;
j := 0;
was_off :=0;
endPtsOfContours := g + 10;
ins := ttUSHORT(g + 10 + numberOfContours * 2);
points := PByteArray(PtrUInt(_Data) + PtrUInt(g) + 10 + PtrUInt(numberOfContours) * 2 + 2 + PtrUInt(ins));
n := 1 + ttUSHORT(endPtsOfContours+ numberOfContours*2-2);
m := n + numberOfContours; // a loose bound on how many vertices we might need
Result.Count := M;
SetLength(Result.List, Result.Count);
next_move := 0;
flagcount := 0;
// in first pass, we load uninterpreted data into the allocated array
// above, shifted to the end of the array so we won't overwrite it when
// we create our final data starting from the front
off := m - n; // starting offset for uninterpreted data, regardless of how m ends up being calculated
// first load flags
PointIndex := 0;
for i:=0 to n-1 do
begin
if flagcount = 0 then
begin
flags := points[PointIndex];
Inc(PointIndex);
if (Flags and 8)<>0 then
begin
flagcount := points[PointIndex];
Inc(PointIndex);
end;
end else
Dec(flagcount);
Result.List[off+i].vertexType := flags;
end;
// now load x coordinates
x := 0;
for i:=0 to n-1 do
begin
flags := Result.List[off+i].vertexType;
if (flags and 2)<>0 then
begin
dx := points[PointIndex];
Inc(PointIndex);
if (flags and 16)<>0 then
Inc(x, dx)
else
Dec(x, dx); // ???
end else begin
if (flags and 16)=0 then
begin
x := x + (Smallint(points[PointIndex]) shl 8) + Smallint(points[PointIndex+1]);
Inc(PointIndex, 2);
end;
end;
Result.List[off+i].x := x;
end;
// now load y coordinates
y := 0;
for i:=0 to n-1 do
begin
flags := Result.List[off+i].vertexType;
if (flags and 4)<>0 then
begin
dy := points[PointIndex];
Inc(PointIndex);
if (flags and 32)<>0 then
Inc(y, dy)
else
Dec(y, dy); // ???
end else begin
if (flags and 32)=0 then
begin
y := y + (Smallint(points[PointIndex]) shl 8) + Smallint(points[PointIndex+1]);
Inc(PointIndex, 2);
end;
end;
Result.List[off+i].y := y;
end;
// now convert them to our format
Result.Count := 0;
sx := 0;
sy := 0;
cx := 0;
cy := 0;
i := 0;
while i<n do
begin
flags := Result.List[off+i].vertexType;
x := Smallint(Result.List[off+i].x);
y := Smallint(Result.List[off+i].y);
if next_move = i then
begin
// when we get to the end, we have to close the shape explicitly
if i<>0 then
begin
if was_off<>0 then
stbtt_setvertex(Result.List[Result.Count], STBTT_vcurve, sx, sy, cx, cy)
else
stbtt_setvertex(Result.List[Result.Count], STBTT_vline, sx, sy, 0, 0);
Inc(Result.Count);
end;
// now start the new one
stbtt_setvertex(Result.List[Result.Count], STBTT_vmove,x,y,0,0);
Inc(Result.Count);
next_move := 1 + ttUSHORT(endPtsOfContours+j*2);
Inc(j);
was_off := 0;
sx := x;
sy := y;
end else begin
if (flags and 1) = 0 then // if it's a curve
begin
if was_off<>0 then // two off-curve control points in a row means interpolate an on-curve midpoint
begin
stbtt_setvertex(Result.List[Result.Count], STBTT_vcurve, (cx+x) shr 1, (cy+y) shr 1, cx, cy);
Inc(Result.Count);
end;
cx := x;
cy := y;
was_off := 1;
end else begin
if was_off<>0 then
stbtt_setvertex(Result.List[Result.Count], STBTT_vcurve, x,y, cx, cy)
else
stbtt_setvertex(Result.List[Result.Count], STBTT_vline, x,y,0,0);
Inc(Result.Count);
was_off := 0;
end;
end;
Inc(i);
end;
if i<>0 then
begin
if was_off<>0 then
stbtt_setvertex(Result.List[Result.Count], STBTT_vcurve,sx,sy,cx,cy)
else
stbtt_setvertex(Result.List[Result.Count], STBTT_vline,sx,sy,0,0);
Inc(Result.Count);
end;
end else
if numberOfContours = -1 then
begin
// Compound shapes.
more := 1;
comp2 := g + 10;
Result.Count := 0;
Result.List := Nil;
While more<>0 Do
Begin
comp_verts.Count := 0;
comp_verts.List := nil;
mtx[0] := 1;
mtx[1] := 0;
mtx[2] := 0;
mtx[3] := 1;
mtx[4] := 0;
mtx[5] := 0;
flags := ttSHORT(comp2);
Inc(Comp2, 2);
gidx := ttSHORT(comp2);
Inc(Comp2, 2);
if (flags and 2)<>0 then // XY values
begin
If (flags and 1)<>0 then // shorts
Begin
mtx[4] := ttSHORT(comp2);
Inc(Comp2, 2);
mtx[5] := ttSHORT(comp2);
Inc(Comp2, 2);
End Else
Begin
mtx[4] := ttByte(comp2);
Inc(Comp2);
mtx[5] := ttByte(comp2);
Inc(Comp2);
end;
End Else
begin
// TODO handle matching point
//STBTT_assert(0);
end;
if (flags and (1 shl 3))<>0 then // WE_HAVE_A_SCALE
begin
mtx[0] := ttSHORT(comp2)/16384.0;
mtx[1] := 0;
mtx[2] := 0;
mtx[3] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
end else if (flags and (1 shl 6))<>0 then // WE_HAVE_AN_X_AND_YSCALE
begin
mtx[0] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
mtx[1] := 0;
mtx[2] := 0;
mtx[3] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
end else if (flags and (1 shl 7))<>0 then// WE_HAVE_A_TWO_BY_TWO
begin
mtx[0] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
mtx[1] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
mtx[2] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
mtx[3] := ttSHORT(comp2)/16384.0;
Inc(Comp2, 2);
end;
// Find transformation scales.
ms := Sqrt(mtx[0]*mtx[0] + mtx[1]*mtx[1]);
ns := Sqrt(mtx[2]*mtx[2] + mtx[3]*mtx[3]);
// Get indexed glyph.
stbtt_GetGlyphShape(gidx, comp_verts);
If comp_verts.Count > 0 Then
Begin
// Transform vertices.
for i := 0 to comp_verts.Count-1 do
begin
//comp_verts[i];
xx := comp_verts.List[i].x;
yy := comp_verts.List[i].y;
comp_verts.List[i].x := Smallint(Round(ms * (mtx[0]*xx + mtx[2]*yy + mtx[4])));
comp_verts.List[i].y := Smallint(Round(ns * (mtx[1]*xx + mtx[3]*yy + mtx[5])));
xx := comp_verts.List[i].cx;
yy := comp_verts.List[i].cy;
comp_verts.List[i].cx := Smallint(Round(ms * (mtx[0]*xx + mtx[2]*yy + mtx[4])));
comp_verts.List[i].cy := Smallint(Round(ns * (mtx[1]*xx + mtx[3]*yy + mtx[5])));
end;
// Append vertices.
TempSize := Result.Count + Comp_Verts.Count;
SetLength(Result.List, TempSize);
For I:=0 To Pred(Comp_Verts.Count) Do
Begin
Result.List[Result.Count + I] := Comp_Verts.List[I];
End;
Result.Count := TempSize;
end;
// More components ?
more := flags and (1 shl 5);
End;
End Else
If numberOfContours < 0 Then
Begin
// TODO other compound variations?
//STBTT_assert(0);
End Else
Begin
// numberOfCounters == 0, do nothing
End;
End;
// antialiasing software rasterizer
procedure TTFFont.stbtt_GetGlyphBitmapBox(glyph: Integer; scale_x, scale_y, shift_x, shift_y: Single; var ix0, iy0, ix1, iy1: Integer);
var
x0,y0,x1,y1: Integer;
begin
if stbtt_GetGlyphBox(glyph, x0,y0,x1,y1)=0 then
begin
x0 := 0;
y0 := 0;
x1 := 0;