File: ttload.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (1509 lines) | stat: -rw-r--r-- 39,658 bytes parent folder | download | duplicates (8)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
(*******************************************************************
 *
 *  TTLoad.Pas                                                 1.0
 *
 *    TrueType Tables loaders
 *
 *  Copyright 1996 David Turner, Robert Wilhelm and Werner Lemberg
 *
 *  This file is part of the FreeType project, and may only be used
 *  modified and distributed under the terms of the FreeType project
 *  license, LICENSE.TXT. By continuing to use, modify or distribute
 *  this file you indicate that you have read the license and
 *  understand and accept it fully.
 *
 *
 *  Difference between 1.0 and 1.1 : HUGE !!
 *
 *  - Changed the load model to get in touch with TTFile 1.1
 *  - Now loads one whole resident table in one call
 *  - defined resident and instance records/data
 *
 ******************************************************************)

Unit TTLoad;

interface

{$R-}
uses TTTypes, TTTables, TTCMap, TTObjs, TTFile;

 function LookUp_TrueType_Table( face : PFace;
                                 aTag : string ) : int;

 function Load_TrueType_Directory( AStream: TFreeTypeStream;  face      : PFace;
                                   faceIndex : Int ) : TError;

 function Load_TrueType_MaxProfile( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_Header    ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_Locations ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_CVT       ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_CMap      ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_Gasp      ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_Names     ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_Programs  ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_trueType_Postscript( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_OS2       ( AStream: TFreeTypeStream;  face : PFace ) : TError;
 function Load_TrueType_HDMX      ( AStream: TFreeTypeStream;  face : PFace ) : TError;

 function Load_TrueType_Metrics_Header( AStream: TFreeTypeStream;  face     : PFace;
                                        vertical : Boolean ) : TError;

 function Load_TrueType_Any( face        : PFace;
                             tag         : longint;
                             offset      : longint;
                             var buffer;
                             var length  : longint ) : TError;

implementation

uses TTError, TTMemory;

  (* Composite glyph decoding flags *)

(*******************************************************************
 *
 *  Function    :  LookUp_TrueType_Table
 *
 *  Description :  Looks for a TrueType table by name
 *
 *  Input  :  face   resident table to look for
 *            aTag        searched tag
 *
 *  Output :  index of table if found, -1 otherwise.
 *
 ******************************************************************)

 function LookUp_TrueType_Table( face : PFace;
                                 aTag : string ) : int;
 var
   ltag : Long;
   i   : int;
 begin
   ltag := (Long(ord(aTag[1])) shl 24) +  (Long(ord(aTag[2])) shl 16) +
           (Long(ord(aTag[3])) shl 8 ) +   Long(ord(aTag[4]));

   for i := 0 to face^.numTables-1 do
     begin

       if face^.dirTables^[i].Tag = lTag then
         begin
           LookUp_TrueType_Table := i;
           exit;
         end
     end;

   (* couldn't find the table *)
   LookUp_TrueType_Table := -1;
 end;


 function LookUp_Mandatory_Table( face : PFace;
                                  aTag : string ) : int;
 var
   table : int;
 begin
   table := LookUp_TrueType_Table( face, aTag );
   if table < 0 then
     error := TT_Err_Table_Missing;

   LookUp_Mandatory_Table := table;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Collection
 *
 *  Description :
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes : A table directory doesn't own subttables. There is no
 *          constructor or destructor for it.
 *
 ******************************************************************)

 function Load_TrueType_Collection( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   n : Int;
 const
   TTC_Tag = ( ord('t') shl 24 ) +
             ( ord('t') shl 16 ) +
             ( ord('c') shl 8  ) +
             ( ord(' ')        );
 begin
   Load_TrueType_Collection := Failure;

   with face^.ttcHeader do
   begin

     if AStream.SeekFile( 0 )     or
        AStream.AccessFrame(12 ) then exit;

     Tag      := AStream.Get_ULong;
     version  := AStream.Get_Long;
     dirCount := AStream.Get_Long;

     AStream.ForgetFrame;

     if Tag <> TTC_Tag then
     begin
       Tag            := 0;
       version        := 0;
       dirCount       := 0;
       tableDirectory := nil;

       error := TT_Err_File_Is_Not_Collection;
       exit;
     end;

     if Alloc( tableDirectory, dirCount * sizeof(ULong) ) or
        AStream.AccessFrame( dirCount*4 ) then exit;

     for n := 0 to dirCount-1 do
       tableDirectory^[n] := AStream.Get_ULong;

     AStream.ForgetFrame;
   end;

   Load_TrueType_Collection := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Directory
 *
 *  Description :
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes : A table directory doesn't own subttables. There is no
 *          constructor or destructor for it.
 *
 ******************************************************************)

 function Load_TrueType_Directory( AStream: TFreeTypeStream;  face      : PFace;
                                   faceIndex : Int ) : TError;
 var
   n        : Int;
   tableDir : TTableDir;
 begin
    Load_TrueType_Directory := Failure;

    {$IFDEF FREETYPE_DEBUG} Write('Directory '); {$ENDIF}

    if Load_TrueType_Collection(AStream, face) then
      begin
        if error <> TT_Err_File_Is_Not_Collection then
          exit;

        (* The file isn't a collection, exit if index isn't 0 *)
        if faceIndex <> 0 then
          exit;

        error := TT_Err_Ok;

        (* Now skip to the beginning of the file *)
        if AStream.SeekFile(0) then
          exit;
      end
    else
      begin
        (* file is a collection. Check the index *)
        if ( faceIndex < 0 ) or
           ( ulong(faceIndex) >= face^.ttcHeader.dirCount ) then
          begin
            error := TT_Err_Bad_Argument;
            exit;
          end;

        (* select a TT Font within the ttc file *)
        if AStream.SeekFile( face^.ttcHeader.tableDirectory^[faceIndex] ) then
          exit;
      end;

    if AStream.AccessFrame( 12 ) then
      exit;

    tableDir.version   := AStream.GET_Long;
    tableDir.numTables := AStream.GET_UShort;

    tableDir.searchRange   := AStream.GET_UShort;
    tableDir.entrySelector := AStream.GET_UShort;
    tableDir.rangeShift    := AStream.GET_UShort;

    {$IFDEF FREETYPE_DEBUG} Writeln('Tables number : ', tableDir.numTables ); {$ENDIF}

    AStream.ForgetFrame;

    (* Check that we have a 'sfnt' format there *)
    if (tableDir.version <> $10000   ) and     (* MS fonts  *)
       (tableDir.version <> $74727565) then    (* Mac fonts *)
    begin
      {$IFDEF FREETYPE_DEBUG} Writeln('Invalid font format'); {$ENDIF}
      error := TT_Err_Invalid_File_Format;
      exit;
    end;

    with face^ do
    begin

      numTables := tableDir.numTables;

      if Alloc( dirTables, numTables * sizeof( TTableDirEntry ) ) or
         AStream.AccessFrame( 16 * numTables ) then exit;

      for n := 0 to numTables-1 do with dirTables^[n] do
      begin
        Tag        := AStream.GET_ULong;
        Checksum   := AStream.GET_ULong;
        Offset     := AStream.GET_Long;
        Length     := AStream.Get_Long;
      end;

      AStream.ForgetFrame;

   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_Directory := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_MaxProfile
 *
 *  Description :
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes : A maximum profile is a static table that owns no
 *          subttable. It has then no constructor nor destructor
 *
 ******************************************************************)

 function Load_TrueType_MaxProfile( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   table : int;
 begin

   Load_TrueType_MaxProfile := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('MaxProfile '); {$ENDIF}

   table := LookUp_Mandatory_Table( face, 'maxp');
   if table < 0 then exit;

   with face^ do
   begin

     if astream.SeekFile( dirTables^[table].Offset ) or
        AStream.AccessFrame( 32 ) then exit;

     with AStream, MaxProfile do
      begin

        ULong(Version) := GET_ULong;

        numGlyphs   := GET_UShort;
        maxPoints   := GET_UShort;
        maxContours := GET_UShort;

        maxCompositePoints   := GET_UShort;
        maxCompositeContours := GET_UShort;
        maxZones             := GET_UShort;
        maxTwilightPoints    := GET_UShort;
        maxStorage           := GET_UShort;
        maxFunctionDefs      := GET_UShort;
        maxINstructionDefs   := GET_UShort;
        maxStackElements     := GET_UShort;

        maxSizeOfInstructions := GET_UShort;
        maxComponentElements  := GET_UShort;
        maxComponentDepth     := GET_UShort;
      end;

     AStream.ForgetFrame;

    (* XXX : an adjustement that is necessary to load certain */
    /*       broken fonts like "Keystrokes MT" :-(            */
    /*                                                        */
    /*   We allocate 64 function entries by default when      */
    /*   the maxFunctionDefs field is null.                   *)

    (*   otherwise, we increment this field by one, in order  *)
    (*   to load some old Apple fonts..                       *)

     if maxProfile.maxFunctionDefs = 0 then
       maxProfile.maxFunctionDefs := 64;

     numGlyphs := MaxProfile.numGlyphs;
     (* compute number of glyphs *)

     maxPoints := MaxProfile.maxCompositePoints;
     if (maxPoints < MaxProfile.maxPoints) then
       maxPoints := MaxProfile.maxPoints;
     (* compute max number of points *)

     maxContours := MaxProfile.maxCompositeContours;
     if maxContours < MaxProfile.maxContours then
       maxContours := MaxProfile.maxContours;
     (* compute max number of contours *)

     maxComponents := MaxProfile.maxComponentElements +
                      MaxProfile.maxComponentDepth;
     (* compute max number of components for glyph loading *)

     (* XXX: some fonts have maxComponents set to 0; we will *)
     (*      then use 16 of them by default                  *)
     if maxComponents = 0 then maxComponents := 16;

     (* We also increase maxPoints and maxContours in order to support *)
     (* some broken fonts                                              *)
     inc( maxPoints,   8 );
     inc( maxContours, 4 );
   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_MaxProfile := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Gasp
 *
 *  Description :
 *
 *  Input  :  face
 *
 ******************************************************************)

 function Load_TrueType_Gasp( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   gRanges  : PGaspRanges;
   table, i : Int;
 label
   Fail;
 begin
   Load_TrueType_Gasp := Failure;

   with face^.gasp do
   begin
     version    := 0;
     numRanges  := 0;
     gaspRanges := nil;
   end;

   table := Lookup_TrueType_Table( face, 'gasp' );
   if ( table < 0 ) then
   begin
     Load_TrueType_Gasp := Success;
     exit;
   end;

   if astream.SeekFile( face^.dirTables^[table].Offset ) or
      AStream.AccessFrame( 4 ) then exit;

   with AStream, face^.gasp do
   begin
     version    := Get_UShort;
     numRanges  := Get_UShort;
     gaspRanges := nil;
   end;

   AStream.ForgetFrame;

   gRanges:=nil;
   if Alloc( gRanges, face^.gasp.numRanges * sizeof(TGaspRange) ) or
      AStream.AccessFrame( face^.gasp.numRanges * 4 ) then
     goto Fail;

   face^.gasp.gaspRanges := gRanges;

   for i := 0 to face^.gasp.numRanges-1 do
     with AStream, gRanges^[i] do
     begin
       maxPPEM  := Get_UShort;
       gaspFlag := Get_UShort;
     end;

   AStream.ForgetFrame;

   Load_TrueType_Gasp := Success;
   exit;

 Fail:
   Free( gRanges );
   face^.gasp.numRanges := 0;
 end;


(*******************************************************************
 *
 *  Function    :  Load_TrueType_Header
 *
 *  Description :  Load the TrueType header table in the resident
 *                 table
 *
 *  Input  :  face   current leading segment.
 *
 *  Output :  True on success. False on failure
 *
 *  Notes : A font header is a static table that owns no
 *          subttable. It has then no constructor nor destructor
 *
 ******************************************************************)

 function  Load_TrueType_Header( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   i : int;
 begin
   Load_TrueType_Header := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('Header '); {$ENDIF}

   i := LookUp_Mandatory_Table(face, 'head');
   if i <= 0 then exit;

   with face^ do
   begin

     if AStream.SeekFile( dirTables^[i].offset ) or
        AStream.AccessFrame(54 ) then exit;

     with AStream, FontHeader do
     begin

       ULong(Table_Version) := GET_ULong;
       ULong(Font_Revision) := GET_ULong;

       Checksum_Adjust := GET_Long;
       Magic_Number    := GET_Long;

       Flags        := GET_UShort;
       Units_Per_EM := GET_UShort;

       Created [0] := GET_Long; Created [1] := GET_Long;
       Modified[0] := GET_Long; Modified[1] := GET_Long;

       xMin := GET_Short;
       yMin := GET_SHort;
       xMax := GET_SHort;
       yMax := GET_Short;

       Mac_Style       := GET_UShort;
       Lowest_Rec_PPEM := GET_UShort;

       Font_Direction      := GET_Short;
       Index_To_Loc_Format := GET_Short;
       Glyph_Data_Format   := GET_Short;

       {$IFDEF FREETYPE_DEBUG} Writeln('Units per EM : ',Units_Per_EM ); {$ENDIF}

     end;

     AStream.ForgetFrame;

   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_Header := Success;
 end;

(*******************************************************************
 *
 *  Function    : Load_TrueType_Metrics
 *
 *  Description : Load TrueType metrics either from the "hmtx" or
 *                "vmtx" table.
 *
 *  Input  :  face      current resident leading segment
 *            vertical  boolean. When set, try to load the vertical
 *                      header.
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Metrics( AStream: TFreeTypeStream; face     : PFace;
                                 vertical : Boolean ) : TError;
 var
   table, n           : int;
   num_longs          : int;
   num_shorts         : int;
   num_shorts_checked : int;
   temp               : Short;

   header     : ^TT_Horizontal_Header;

   shorts     : ^PTableShortMetrics;
   longs      : ^PTableLongMetrics;

 begin
   Load_TrueType_Metrics := Failure;

   {$IFDEF FREETYPE_DEBUG}
   if vertical then
     Write('vmtx ')
   else
     Write('hmtx ');
   {$ENDIF}

   if vertical then
     begin

       table := LookUp_TrueType_Table( face, 'vmtx' );
       if table < 0 then
         begin
           (* This is an optional table. Return silently if it *)
           (* wasn't found. Note : some fonts have a vertical  *)
           (* header, but no 'vmtx'. E.g. : mingliu.ttf        *)

           face^.verticalHeader.number_Of_VMetrics := 0;
           Load_TrueType_Metrics := Success;
           exit;
         end;

       header := @TT_Horizontal_Header(face^.verticalHeader);
     end
   else
     begin
       table := LookUp_Mandatory_Table( face, 'hmtx' );
       if table < 0 then
         exit;

       header := @face^.horizontalHeader;
     end;


   shorts     := @PTableShortMetrics(header^.short_metrics);
   longs      := @PTableLongMetrics (header^.long_metrics );

   num_longs  := header^.number_Of_HMetrics;
   num_shorts := face^.numGlyphs - num_longs;

   num_shorts_checked := (face^.dirTables^[table].Length - num_longs*4) div 2;

   if num_shorts < 0 then
   begin
     {$IFDEF FREETYPE_DEBUG} Writeln('!! More metrics than glyphs !\n'); {$ENDIF}
     if vertical then  error := TT_Err_Invalid_Vert_Metrics
                 else  error := TT_Err_Invalid_Horiz_Metrics;
     exit;
   end;

   if Alloc( longs^,  sizeof(TLongMetrics) * num_longs )   or
      Alloc( shorts^, sizeof(TShortMetrics)* num_shorts )  or

      AStream.SeekFile( face^.dirTables^[table].Offset )       or
      AStream.AccessFrame( face^.dirTables^[table].Length )    then exit;

   for n := 0 to num_longs-1 do with longs^^[n] do
   begin
     advance := AStream.GET_UShort;
     bearing := AStream.GET_Short;
   end;

   (* do we have an inconsistent number of metric values ? *)
   if num_shorts > num_shorts_checked then
     begin
       for n := 0 to num_shorts_checked-1 do
         shorts^^[n] := AStream.GET_Short;

        (* we fill up the missing left side bearings with the    *)
        (* last valid value. Since this will occur for buggy CJK *)
        (* fonts usually, nothing serious will happen.           *)

        temp := shorts^^[num_shorts_checked-1];

        for n := num_shorts_checked to num_shorts-1 do
          shorts^^[n] := temp;
     end
   else
     for n := 0 to num_shorts-1 do
       shorts^^[n] := AStream.GET_Short;

   AStream.ForgetFrame;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_Metrics := Success;
 end;


(*******************************************************************
 *
 *  Function    : Load_TrueType_Metrics_Header
 *
 *  Description :
 *
 *  Input  :  face      current resident leading segment
 *            vertical  boolean. When set, try to load the vertical
 *                      header.
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Metrics_Header( AStream: TFreeTypeStream; face     : PFace;
                                        vertical : Boolean ) : TError;
 var
   table  : int;
   header : ^TT_Horizontal_Header;
 begin
   Load_TrueType_Metrics_Header := Failure;

    {$IFDEF FREETYPE_DEBUG}
    if vertical then
      Write('Vertical Header ')
    else
      Write('Horizontal Header ');
    {$ENDIF}

   if vertical then
     begin
       face^.verticalInfo := False;

       (* the vertical header is an optional table.. so return *)
       (* silently if we don't find it                         *)
       table := LookUp_TrueType_Table( face, 'vhea' );
       if (table < 0) then
         begin
           Load_TrueType_Metrics_Header := Success;
           exit;
         end;

       face^.verticalInfo := True;
       header := @TT_Horizontal_Header(face^.verticalHeader);
     end
   else
     begin
       table := LookUp_Mandatory_Table( face, 'hhea');
       if ( table < 0 ) then
         exit;
       header := @face^.horizontalHeader;
     end;

   with face^ do
   begin

     if AStream.SeekFile( dirTables^[table].Offset ) or
        AStream.AccessFrame( 36 ) then
        exit;

     with AStream, header^ do
     begin

       Long(Version) := GET_ULong;
       Ascender      := GET_Short;
       Descender     := GET_Short;
       Line_Gap      := GET_Short;

       advance_Width_Max := GET_UShort;

       min_Left_Side_Bearing  := GET_Short;
       min_Right_Side_Bearing := GET_Short;
       xMax_Extent            := GET_Short;
       caret_Slope_Rise       := GET_Short;
       caret_Slope_Run        := GET_Short;

       Reserved[0] := GET_Short;  (* this is cared offset for vertical *)

       Reserved[1] := GET_Short;
       Reserved[2] := GET_Short;
       Reserved[3] := GET_Short;
       Reserved[4] := GET_Short;

       metric_Data_Format := GET_Short;
       number_Of_HMetrics := GET_UShort;

       short_metrics := nil;
       long_metrics  := nil;

     end;

     AStream.ForgetFrame;

   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_Metrics_Header := Load_TrueType_Metrics( AStream, face, vertical );
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Locations
 *
 *  Description :  Loads the location table in resident table
 *
 *  Input  :  face     Current Resident Leading Segment
 *
 *  Output :  True on success. False on failure
 *
 *  NOTES :
 *
 *    The Font Header *must* be loaded in the leading segment
 *    before calling this function.
 *
 *    This table is destroyed directly by the resident destructor.
 *
 ******************************************************************)

 function Load_TrueType_Locations( AStream: TFreeTypeStream; face : PFace ): TError;
 var
   t, n        : int;
   LongOffsets : int;
 begin

   Load_TrueType_Locations := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('Locations '); {$ENDIF}

   with face^ do
   begin

     LongOffsets :=  fontHeader.Index_To_Loc_Format;

     t := LookUp_Mandatory_Table( face, 'loca' );
     if t < 0 then exit;

     if AStream.SeekFile( dirTables^[T].Offset ) then exit;

     if LongOffsets <> 0 then
       begin

         numLocations := dirTables^[T].Length shr 2;

         {$IFDEF FREETYPE_DEBUG}
         Writeln('Glyph locations # ( 32 bits offsets ) : ', numLocations );
         {$ENDIF}

         if Alloc( glyphLocations, sizeof(Long)*numLocations ) or
            AStream.AccessFrame( numLocations*4 ) then exit;

         for n := 0 to numLocations-1 do
           glyphLocations^[n] := AStream.GET_Long;

         AStream.ForgetFrame;

       end
     else
       begin
         numLocations := dirTables^[T].Length shr 1;

         {$IFDEF FREETYPE_DEBUG}
         Writeln('Glyph locations # ( 16 bits offsets ) : ', numLocations );
         {$ENDIF}

         if Alloc( glyphLocations, sizeof(Long)*numLocations ) or
            AStream.AccessFrame( numLocations*2 ) then exit;

         for n := 0 to numLocations-1 do
           glyphLocations^[n] := Long(AStream.GET_UShort) * 2;

         AStream.ForgetFrame;
       end;

   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_Locations := Success;
 end;


(*******************************************************************
 *
 *  Function    :  Load_TrueType_Names
 *
 *  Description :  Loads the name table into the face table
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes  :  This attribute table is destroyed by the resident
 *            destructor.
 *
 ******************************************************************)

  function Load_TrueType_Names( AStream: TFreeTypeStream; face : PFace ) : TError;
  var
    table, i : Int;
    bytes    : Long;
  begin
    Load_TrueType_Names := Failure;

    table := Lookup_Mandatory_Table( face, 'name' );
    if table < 0 then exit;

    with face^.nameTable do
    begin
      (* Seek to the beginning of the table and check the frame access. *)
      if AStream.SeekFile( face^.dirTables^[table].Offset ) or
         AStream.AccessFrame(6 ) then exit;

      format         := AStream.GET_UShort;
      numNameRecords := AStream.GET_UShort;
      storageOffset  := AStream.GET_UShort;

      AStream.ForgetFrame;

      if Alloc( names, numNameRecords*sizeof(TName_Record) ) or
         AStream.AccessFrame( numNameRecords*12 ) then
      begin
        numNameRecords := 0;
        exit;
      end;

      (* Load the name records and determine how much storage is needed *)
      (* to hold the strings themselves                                 *)

      bytes := 0;
      for i := 0 to numNameRecords-1 do with AStream, names^[i] do
      begin
        platformID := GET_UShort;
        encodingID := GET_UShort;
        languageID := GET_UShort;
        nameID     := GET_UShort;
        length     := GET_UShort;
        offset     := GET_UShort;

        (* this test takes care of 'holes' in the names tabls, as *)
        (* reported by Erwin                                      *)
        if long(Offset + Length) > bytes then
          bytes := Offset + Length;
      end;

      AStream.ForgetFrame;

      storage := nil;
      if bytes > 0 then
      begin
        if Alloc( storage, bytes ) then exit;

        if AStream.ReadAtFile( face^.dirTables^[table].Offset + storageOffset,
                            storage^, bytes ) then
        begin
          Free(storage);
          exit;
        end;
      end;

    end;

    Load_TrueType_Names := Success;
    exit;
  end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_CVT
 *
 *  Description :
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes  :  This attribute table is destroyed by the resident
 *            destructor.
 *
 ******************************************************************)

 function Load_TrueType_CVT( AStream: TFreeTypeStream; face : PFace ): TError;
 var
   t, n : Int;
 begin
   Load_TrueType_CVT := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('CVT '); {$ENDIF}

   (* the CVT table is optional *)

   t := LookUp_TrueType_Table( face, 'cvt ');
   if t < 0 then
   begin
     face^.cvt     := nil;
     face^.cvtSize := 0;
     Load_TrueType_CVT := Success;
     {$IFDEF FREETYPE_DEBUG}  writeln('none'); {$ENDIF}
     exit;
   end;

   with face^ do
   begin

     cvtSize := dirTables^[t].Length div 2;

     if Alloc( cvt, sizeof(Short)*cvtSize )  or

        AStream.SeekFile( dirTables^[t].Offset ) or

        AStream.AccessFrame(2*cvtSize )         then exit;

     for n := 0 to cvtSize-1 do
       cvt^[n] := AStream.GET_Short;

     AStream.ForgetFrame;
   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}
   Load_TrueType_CVT := Success;
 end;


(*******************************************************************
 *
 *  Function    :  Load_TrueType_CMap
 *
 *  Description :
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 *  Notes  :  The Cmap table directory is destroyed by the resident
 *            destructor. The Cmap subtables must be destroyed by
 *            Free_CMap_Table.
 *
 ******************************************************************)

 function Load_TrueType_CMap( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   off, table_start : Longint;
   n, t      : Int;

   cmap_dir : TCMapDir;
   entry    : TCMapDirEntry;
   cmap     : PCMapTable;
 label
   Fail;
 begin

   Load_TrueType_CMap := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('CMaps '); {$ENDIF}

   t := LookUp_Mandatory_Table( face,'cmap' );
   if t < 0 then exit;

   with face^ do
   begin

     table_start := dirTables^[t].offset;

     if AStream.SeekFile( dirTables^[t].Offset ) or
        AStream.AccessFrame( 4 )  then exit;

     cmap_dir.tableVersionNumber := AStream.GET_UShort;
     cmap_dir.numCMaps           := AStream.GET_UShort;

     AStream.ForgetFrame;

     off := AStream.Position;

     (* save space in face data for cmap tables *)
     numCMaps := cmap_dir.numCMaps;
     if Alloc( cMaps, numCMaps * sizeof(TCMapTable) ) then exit;

     for n := 0 to numCMaps-1 do
     begin

       if AStream.SeekFile   ( off ) or
          AStream.AccessFrame( 8 )   then exit;

       cmap := @cMaps^[n];

       entry.platformID         := AStream.GET_UShort;
       entry.platformEncodingID := AStream.GET_UShort;
       entry.offset             := AStream.GET_Long;

       cmap^.loaded             := False;
       cmap^.platformID         := entry.platformID;
       cmap^.platformEncodingID := entry.platformEncodingID;

       AStream.ForgetFrame;

       off := AStream.Position;

       if AStream.SeekFile   ( table_start + entry.offset ) or
          AStream.AccessFrame( 6 ) then exit;

       cmap^.format  := AStream.Get_UShort;
       cmap^.length  := AStream.Get_UShort;
       cmap^.version := AStream.Get_UShort;

       AStream.ForgetFrame;

       cmap^.StreamPtr := @face^.stream;
       cmap^.offset := AStream.Position;

     end;  (* for n *)

   end;  (* with face^ *)

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_CMap := Success;
   exit;

 Fail:
   Free( face^.cMaps );
   Load_TrueType_CMap := Failure;
 end;


(*
 procedure Free_CMap_Table( var cmap : TCMapTable );
 begin
   if cmap.cmap0 <> nil then
     with cmap do
       case format of

         0 : begin
               Free( cmap0^.glyphIdArray );
               Free( cmap0 );
             end;

         2 : begin
               Free( cmap2^.glyphIdArray );
               Free( cmap2^.subHeaders );
               Free( cmap2 );
             end;

         4 : begin
               Free( cmap4^.glyphIdArray );
               Free( cmap4^.segments );
               Free( cmap4 );
             end;

         6 : begin
               Free( cmap6^.glyphIdArray );
               Free( cmap6 );
             end;
       end;

   cmap.format  := 0;
   cmap.length  := 0;
   cmap.version := 0;
 end;
*)

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Programs
 *
 *  Description :  Load the Font and CVT programs in the resident
 *                 table
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Programs( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   t : Int;
 begin

   Load_TrueType_Programs := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('Font program '); {$ENDIF}

   (* The font program is optional *)

   t := Lookup_TrueType_Table( face, 'fpgm' );

   if t < 0 then

     with face^ do
     begin
       fontProgram := nil;
       fontPgmSize := 0;

       {$IFDEF FREETYPE_DEBUG} Writeln('none in file'); {$ENDIF}
     end

   else

     with face^ do
     begin

       fontPgmSize := dirTables^[t].Length;

       if Alloc( fontProgram, fontPgmSize ) or
          AStream.ReadAtFile( dirTables^[t].offset,
                           fontProgram^,
                           fontPgmSize ) then exit;

       {$IFDEF FREETYPE_DEBUG} Writeln('loaded, ',fontPgmSize,' bytes'); {$ENDIF}
     end;

   {$IFDEF FREETYPE_DEBUG} Write('CVT program '); {$ENDIF}

   t := LookUp_trueType_Table( face, 'prep' );

   (* The CVT table is optional *)

   if t < 0 then

     with face^ do
     begin
       cvtProgram := nil;
       cvtPgmSize := 0;

       {$IFDEF FREETYPE_DEBUG} Writeln('none in file'); {$ENDIF}
     end

   else

     with face^ do
     begin

       cvtPgmSize := dirTables^[t].Length;

       if Alloc( cvtProgram, cvtPgmSize ) or
          AStream.ReadAtFile( dirTables^[t].offset,
                           cvtProgram^,
                           cvtPgmSize ) then exit;

       {$IFDEF FREETYPE_DEBUG} Writeln('loaded, ',cvtPgmSize,' bytes'); {$ENDIF}
     end;

   Load_TrueType_Programs := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_OS2
 *
 *  Description :  Load the OS2 Table
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_OS2( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   table : Int;
   i     : Int;
 begin
   Load_TrueType_OS2 := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('OS/2 table '); {$ENDIF}

   (* We now support Apple fonts who do not have an OS/2 table *)
   table := LookUp_Mandatory_Table( face, 'OS/2' );
   if table < 0 then begin
     face^.os2.version := $FFFF;
     Load_TrueType_OS2 := Success;
     error             := TT_Err_Ok;  (* clear error *)
     exit;
   end;

   if AStream.SeekFile( face^.dirTables^[table].offset ) or
      AStream.AccessFrame( 78 ) then exit;

   with AStream, face^.os2 do
   begin
     version             := Get_UShort;
     xAvgCharWidth       := Get_Short;
     usWeightClass       := Get_UShort;
     usWidthClass        := Get_UShort;
     fsType              := Get_Short;
     ySubscriptXSize     := Get_Short;
     ySubscriptYSize     := Get_Short;
     ySubscriptXOffset   := Get_Short;
     ySubscriptYOffset   := Get_Short;
     ySuperscriptXSize   := Get_Short;
     ySuperscriptYSize   := Get_Short;
     ySuperscriptXOffset := Get_Short;
     ySuperscriptYOffset := Get_Short;
     yStrikeoutSize      := Get_Short;
     yStrikeoutPosition  := Get_Short;
     sFamilyClass        := Get_Short;

     for i := 0 to 9 do panose[i] := Get_Byte;

     ulUnicodeRange1 := Get_ULong;
     ulUnicodeRange2 := Get_ULong;
     ulUnicodeRange3 := Get_ULong;
     ulUnicodeRange4 := Get_ULong;

     for i := 0 to 3 do achVendID[i] := Get_Byte;

     fsSelection      := Get_UShort;
     usFirstCharIndex := Get_UShort;
     usLastCharIndex  := Get_UShort;
     sTypoAscender    := Get_Short;
     sTypoDescender   := Get_Short;
     sTypoLineGap     := Get_Short;
     usWinAscent      := Get_UShort;
     usWinDescent     := Get_UShort;

     AStream.ForgetFrame;

     if version >= $0001 then
       begin
         if AStream.AccessFrame(8) then exit;

         ulCodePageRange1 := AStream.Get_ULong;
         ulCodePageRange2 := AStream.Get_ULong;

         AStream.ForgetFrame;
       end
     else
       begin
         ulCodePageRange1 := 0;
         ulCodePageRange2 := 0;
       end;

     if version >= $0002 then
       begin
            if AStream.AccessFrame(10) then exit;

            sxHeight      := AStream.Get_Short;
            sCapHeight    := AStream.Get_Short;
            usDefaultChar := AStream.Get_UShort;
            usBreakChar   := AStream.Get_UShort;
            usMaxContext  := AStream.Get_UShort;

            AStream.ForgetFrame;
       end;
   end;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_TrueType_OS2 := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_Postscript
 *
 *  Description :  Load the 'post' table
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Postscript( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   table : Int;
 begin
   Load_TrueType_Postscript := Failure;

   {$IFDEF FREETYPE_DEBUG} Write('post table '); {$ENDIF}

   table := LookUp_TrueType_Table( face, 'post' );
   if table < 0 then exit;

   if AStream.SeekFile( face^.dirTables^[table].offset ) or
      AStream.AccessFrame(32) then exit;

   with AStream, face^.postscript do
   begin
     formatType         := Get_ULong;
     italicAngle        := Get_ULong;
     underlinePosition  := Get_Short;
     underlineThickness := Get_Short;
     isFixedPitch       := Get_ULong;
     minMemType42       := Get_ULong;
     maxMemType42       := Get_ULong;
     minMemType1        := Get_ULong;
     maxMemType1        := Get_ULong;
   end;

   AStream.ForgetFrame;

   {$IFDEF FREETYPE_DEBUG} Writeln('loaded'); {$ENDIF}

   Load_trueType_Postscript := Success;
 end;

(*******************************************************************
 *
 *  Function    :  Load_TrueType_HDMX
 *
 *  Description :  Load the 'hdmx' tables
 *
 *  Input  :  face
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Hdmx( AStream: TFreeTypeStream; face : PFace ) : TError;
 var
   table, n   : Int;
   num_glyphs : Int;

   version  : UShort;
   num_rec  : Short;
   rec_size : Long;
   rec      : PHdmx_Record;
 label
   Fail;
 begin
   Load_TrueType_Hdmx := Failure;

   with face^.hdmx do
   begin
     version     := 0;
     num_records := 0;
     records     := nil;
   end;

   (* This table is optional *)

   table := LookUp_TrueType_Table( face, 'hdmx' );
   if table < 0 then
   begin
     Load_TrueType_Hdmx := Success;
     exit;
   end;

   if AStream.SeekFile( face^.dirTables^[table].offset ) or
      AStream.AccessFrame(8 ) then exit;

   version  := AStream.Get_UShort;
   num_rec  := AStream.Get_Short;
   rec_size := AStream.Get_Long;

   AStream.ForgetFrame;

   (* right now, we only recognize format 0 *)

   if version <> 0 then
     exit;

   if Alloc( face^.hdmx.records, sizeof(THdmx_Record)*num_rec ) then
     exit;

   face^.hdmx.num_records := num_rec;
   num_glyphs := face^.NumGlyphs;

   rec_size := rec_size - num_glyphs - 2;

   for n := 0 to num_rec-1 do
   begin
     rec := @face^.hdmx.records^[n];

     (* read record *)

     if AStream.AccessFrame(2) then
       goto Fail;

     rec^.ppem      := AStream.Get_Byte;
     rec^.max_width := AStream.Get_Byte;

     AStream.ForgetFrame;

     if Alloc( rec^.widths, num_glyphs ) or
        AStream.ReadFile( rec^.widths^, num_glyphs ) then
       goto Fail;

     (* skip padding bytes *)

     if rec_size > 0 then
       if AStream.SkipFile( rec_size ) then
         goto Fail;
   end;

   Load_TrueType_HDMX := Success;
   exit;

 Fail:
   for n := 0 to num_rec-1 do
    Free( face^.hdmx.records^[n].widths );

   Free( face^.hdmx.records );
   face^.hdmx.num_records := 0;
 end;


(*******************************************************************
 *
 *  Function    :  Load_TrueType_Any
 *
 *  Description :  Load any TrueType table in user memory
 *
 *  Input  :  face    the font file's face object
 *            tag     the table
 *
 *  Output :  True on success. False on failure
 *
 ******************************************************************)

 function Load_TrueType_Any( face        : PFace;
                             tag         : longint;
                             offset      : longint;
                             var buffer;
                             var length  : longint ) : TError;
 var
   ftstream   : TFreeTypeStream;
   found, i : integer;
 begin
   if tag <> 0 then
     begin
       found := -1;
       i     := 0;
       while i < face^.numTables do
         if Longint(face^.dirTables^[i].tag) = tag then
           begin
             found := i;
             i := face^.numTables;
           end
         else
           inc(i);

       if found < 0 then
         begin
           error := TT_Err_Table_Missing;
           Load_TrueType_Any := Failure;
           exit;
         end;

       inc( offset, face^.dirTables^[found].offset );

       (* if length = 0, the user requested the table's size *)
       if length = 0 then
         begin
           length := face^.dirTables^[found].length;
           Load_TrueType_Any := Success;
           exit;
         end;
     end
   else
     (* if length = 0 and tag = 0, the user requested the font file's size *)
     if length = 0 then
       begin
         (* return length of font file *)
         length := TT_Stream_Size( face^.stream );
         Load_TrueType_Any := Success;
         exit;
       end;

   TT_Use_Stream( face^.stream, ftstream {%H-});
   Load_TrueType_Any := ftstream.ReadAtFile( offset, buffer, length );
   TT_Done_Stream( face^.stream );
 end;

end.