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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "config.h" 
 
#include "global.h" 
#include "module.h" 
 
#ifdef HAVE_LIBTTF 
#if defined(HAVE_FREETYPE_FREETYPE_H) && defined(HAVE_FREETYPE_FTXKERN_H) 
 
#include <freetype/freetype.h> 
#include <freetype/ftxkern.h> 
 
#else /* !HAVE_FREETYPE_FREETYPE_H || !HAVE_FREETYPE_FTXKERN_H */ 
 
#include <freetype.h> 
#include <ftxkern.h> 
 
#endif /* HAVE_FREETYPE_FREETYPE_H && HAVE_FREETYPE_FTXKERN_H */ 
#endif /* HAVE_LIBTTF */ 
 
#include "pike_macros.h" 
#include "object.h" 
#include "constants.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "threads.h" 
#include "array.h" 
#include "mapping.h" 
#include "pike_error.h" 
#include "stralloc.h" 
#include "builtin_functions.h" 
#include "operators.h" 
 
#ifdef HAVE_LIBTTF 
#include "../Image/image.h" 
 
 
#ifdef DYNAMIC_MODULE 
static struct program *image_program=NULL; 
#else 
extern struct program *image_program; 
/* Image module is probably linked static too. */ 
#endif 
 
static TT_Engine engine; 
 
#endif /* HAVE_LIBTTF */ 
 
 
#define sp Pike_sp 
 
static struct pike_string *param_baseline; 
static struct pike_string *param_quality; 
 
#ifdef HAVE_LIBTTF 
 
/* 
**! module Image 
**! submodule TTF 
**! 
**!     This module adds TTF (Truetype font) capability 
**!     to the Image module. 
**! 
**! note 
**!     This module needs the <tt>libttf</tt> "Freetype" library 
**! 
*/ 
 
void my_tt_error(char *where,char *extra,int err) 
{ 
   char *errc="Unknown"; 
 
   if (err==TT_Err_Invalid_Face_Handle) 
      errc="TT_Err_Invalid_Face_Handle"; 
   else if (err==TT_Err_Invalid_Instance_Handle) 
      errc="TT_Err_Invalid_Instance_Handle"; 
   else if (err==TT_Err_Invalid_Glyph_Handle) 
      errc="TT_Err_Invalid_Glyph_Handle"; 
   else if (err==TT_Err_Invalid_CharMap_Handle) 
      errc="TT_Err_Invalid_CharMap_Handle"; 
   else if (err==TT_Err_Invalid_Result_Address) 
      errc="TT_Err_Invalid_Result_Address"; 
   else if (err==TT_Err_Invalid_Glyph_Index) 
      errc="TT_Err_Invalid_Glyph_Index"; 
   else if (err==TT_Err_Invalid_Argument) 
      errc="TT_Err_Invalid_Argument"; 
   else if (err==TT_Err_Could_Not_Open_File) 
      errc="TT_Err_Could_Not_Open_File"; 
   else if (err==TT_Err_File_Is_Not_Collection) 
      errc="TT_Err_File_Is_Not_Collection"; 
   else if (err==TT_Err_Table_Missing) 
      errc="TT_Err_Table_Missing"; 
   else if (err==TT_Err_Invalid_Horiz_Metrics) 
      errc="TT_Err_Invalid_Horiz_Metrics"; 
   else if (err==TT_Err_Invalid_CharMap_Format) 
      errc="TT_Err_Invalid_CharMap_Format"; 
   else if (err==TT_Err_Invalid_PPem) 
      errc="TT_Err_Invalid_PPem"; 
   else if (err==TT_Err_Invalid_File_Format) 
      errc="TT_Err_Invalid_File_Format"; 
   else if (err==TT_Err_Invalid_Engine) 
      errc="TT_Err_Invalid_Engine"; 
   else if (err==TT_Err_Too_Many_Extensions) 
      errc="TT_Err_Too_Many_Extensions"; 
   else if (err==TT_Err_Extensions_Unsupported) 
      errc="TT_Err_Extensions_Unsupported"; 
   else if (err==TT_Err_Invalid_Extension_Id) 
      errc="TT_Err_Invalid_Extension_Id"; 
   else if (err==TT_Err_Max_Profile_Missing) 
      errc="TT_Err_Max_Profile_Missing"; 
   else if (err==TT_Err_Header_Table_Missing) 
      errc="TT_Err_Header_Table_Missing"; 
   else if (err==TT_Err_Horiz_Header_Missing) 
      errc="TT_Err_Horiz_Header_Missing"; 
   else if (err==TT_Err_Locations_Missing) 
      errc="TT_Err_Locations_Missing"; 
   else if (err==TT_Err_Name_Table_Missing) 
      errc="TT_Err_Name_Table_Missing"; 
   else if (err==TT_Err_CMap_Table_Missing) 
      errc="TT_Err_CMap_Table_Missing"; 
   else if (err==TT_Err_Hmtx_Table_Missing) 
      errc="TT_Err_Hmtx_Table_Missing"; 
   else if (err==TT_Err_OS2_Table_Missing) 
      errc="TT_Err_OS2_Table_Missing"; 
   else if (err==TT_Err_Post_Table_Missing) 
      errc="TT_Err_Post_Table_Missing"; 
   else if (err==TT_Err_Out_Of_Memory) 
      errc="TT_Err_Out_Of_Memory"; 
   else if (err==TT_Err_Invalid_File_Offset) 
      errc="TT_Err_Invalid_File_Offset"; 
   else if (err==TT_Err_Invalid_File_Read) 
      errc="TT_Err_Invalid_File_Read"; 
   else if (err==TT_Err_Invalid_Frame_Access) 
      errc="TT_Err_Invalid_Frame_Access"; 
   else if (err==TT_Err_Too_Many_Points) 
      errc="TT_Err_Too_Many_Points"; 
   else if (err==TT_Err_Too_Many_Contours) 
      errc="TT_Err_Too_Many_Contours"; 
   else if (err==TT_Err_Invalid_Composite) 
      errc="TT_Err_Invalid_Composite"; 
   else if (err==TT_Err_Too_Many_Ins) 
      errc="TT_Err_Too_Many_Ins"; 
   else if (err==TT_Err_Invalid_Opcode) 
      errc="TT_Err_Invalid_Opcode"; 
   else if (err==TT_Err_Too_Few_Arguments) 
      errc="TT_Err_Too_Few_Arguments"; 
   else if (err==TT_Err_Stack_Overflow) 
      errc="TT_Err_Stack_Overflow"; 
   else if (err==TT_Err_Code_Overflow) 
      errc="TT_Err_Code_Overflow"; 
   else if (err==TT_Err_Bad_Argument) 
      errc="TT_Err_Bad_Argument"; 
   else if (err==TT_Err_Divide_By_Zero) 
      errc="TT_Err_Divide_By_Zero"; 
   else if (err==TT_Err_Storage_Overflow) 
      errc="TT_Err_Storage_Overflow"; 
   else if (err==TT_Err_Cvt_Overflow) 
      errc="TT_Err_Cvt_Overflow"; 
   else if (err==TT_Err_Invalid_Reference) 
      errc="TT_Err_Invalid_Reference"; 
   else if (err==TT_Err_Invalid_Distance) 
      errc="TT_Err_Invalid_Distance"; 
   else if (err==TT_Err_Interpolate_Twilight) 
      errc="TT_Err_Interpolate_Twilight"; 
   else if (err==TT_Err_Debug_OpCode) 
      errc="TT_Err_Debug_OpCode"; 
   else if (err==TT_Err_ENDF_In_Exec_Stream) 
      errc="TT_Err_ENDF_In_Exec_Stream"; 
   else if (err==TT_Err_Out_Of_CodeRanges) 
      errc="TT_Err_Out_Of_CodeRanges"; 
   else if (err==TT_Err_Nested_DEFS) 
      errc="TT_Err_Nested_DEFS"; 
   else if (err==TT_Err_Invalid_CodeRange) 
      errc="TT_Err_Invalid_CodeRange"; 
   else if (err==TT_Err_Invalid_Displacement) 
      errc="TT_Err_Invalid_Displacement"; 
   else if (err==TT_Err_Nested_Frame_Access) 
      errc="TT_Err_Nested_Frame_Access"; 
   else if (err==TT_Err_Invalid_Cache_List) 
      errc="TT_Err_Invalid_Cache_List"; 
   else if (err==TT_Err_Could_Not_Find_Context) 
      errc="TT_Err_Could_Not_Find_Context"; 
   else if (err==TT_Err_Unlisted_Object) 
      errc="TT_Err_Unlisted_Object"; 
   else if (err==TT_Err_Raster_Pool_Overflow) 
      errc="TT_Err_Raster_Pool_Overflow"; 
   else if (err==TT_Err_Raster_Negative_Height) 
      errc="TT_Err_Raster_Negative_Height"; 
   else if (err==TT_Err_Raster_Invalid_Value) 
      errc="TT_Err_Raster_Invalid_Value"; 
   else if (err==TT_Err_Raster_Not_Initialized) 
      errc="TT_Err_Raster_Not_Initialized"; 
   Pike_error("%s: %sFreeType error 0x%03x (%s)\n", 
         where,extra,err,errc); 
} 
 
/* 
**! method object `()(string filename) 
**! method object `()(string filename,mapping options) 
**! 
**!     Makes a new TTF Face object. 
**! 
**! returns 0 if failed. 
**! 
**! arg string filename 
**!     The filename of the <tt>TTF</tt> font or the <tt>TTC</tt> 
**!     font collection. 
**! 
**! arg mapping options 
**!     <pre> 
**!     advanced options: 
**!         "face":int 
**!             If opening a font collection,  '<tt>.TTC</tt>', 
**!             this is used to get other fonts than the first. 
**!     </pre> 
**! 
*/ 
 
struct image_ttf_face_struct 
{ 
   TT_Face face; 
}; 
 
struct image_ttf_faceinstance_struct 
{ 
   TT_Instance instance; 
   struct object *faceobj; 
   int load_flags; 
   int baseline,height;  /* "baseline" position */ 
   int trans;            /* translation of position (ypos) in 26.6*/ 
}; 
 
static struct program *image_ttf_face_program=NULL; 
static struct program *image_ttf_faceinstance_program=NULL; 
 
static void image_ttf_make(INT32 args) 
{ 
   int col=0, i=0; 
   struct object *o; 
   TT_Error res; 
   TT_Face face; 
 
   if (TYPEOF(sp[-args]) != T_STRING) 
      Pike_error("Image.TTF(): illegal argument 1\n"); 
 
   res=TT_Open_Collection(engine, sp[-args].u.string->str, col, &face); 
   if (res) my_tt_error("Image.TTF()","",res); 
   while(! TT_Load_Kerning_Table( face, (TT_UShort)(i++) ) ); 
 
   pop_n_elems(args); 
 
   o=clone_object(image_ttf_face_program,0); 
   ((struct image_ttf_face_struct*)o->storage)->face=face; 
 
   push_object(o); 
} 
 
/* 
**! class Face 
**! 
**!     This represents instances of TTF Faces. 
*/ 
 
#define THISOBJ (Pike_fp->current_object) 
#define THISf ((struct image_ttf_face_struct *)get_storage(THISOBJ,image_ttf_face_program)) 
#define THISi ((struct image_ttf_faceinstance_struct *)get_storage(THISOBJ,image_ttf_faceinstance_program)) 
 
static void image_ttf_face_exit(struct object *o) 
{ 
  if(THISf) 
    TT_Close_Face(THISf->face); 
} 
 
static void image_ttf_faceinstance_exit(struct object *o) 
{ 
  if (THISi) { 
    if(THISi->faceobj->prog) 
      TT_Done_Instance(THISi->instance); 
    free_object(THISi->faceobj); 
  } 
} 
 
#ifdef TTF_DEBUG_INFO 
/* 
**! method mapping properties() 
**!     This gives back a structure of the face's properties. 
**!     Most of this stuff is information you can skip. 
**! 
**!     The most interesting item to look at may be 
**!     <tt>->num_Faces</tt>, which describes the number of faces 
**!     in a <tt>.TTC</tt> font collection. 
**! 
**! returns a mapping of a lot of properties 
*/ 
 
static void image_ttf_face_properties(INT32 args) 
{ 
   int res; 
   TT_Face_Properties prop; 
 
   pop_n_elems(args); 
 
   res=TT_Get_Face_Properties(THISf->face,&prop); 
   if (res) my_tt_error("Image.TTF.Face->properties()","",res); 
 
   push_static_text("num_Glyphs");   push_int(prop.num_Glyphs); 
   push_static_text("max_Points");   push_int(prop.max_Points); 
   push_static_text("max_Contours");   push_int(prop.max_Contours); 
   push_static_text("num_Faces");   push_int(prop.num_Faces); 
 
   push_static_text("header"); 
   if (prop.header) 
   { 
      push_static_text("Table_Version"); push_int(prop.header->Table_Version); 
      push_static_text("Font_Revision"); push_int(prop.header->Font_Revision); 
      push_static_text("CheckSum_Adjust"); push_int(prop.header->CheckSum_Adjust); 
      push_static_text("Magic_Number"); push_int(prop.header->Magic_Number); 
      push_static_text("Flags"); push_int(prop.header->Flags); 
      push_static_text("Units_Per_EM"); push_int(prop.header->Units_Per_EM); 
      push_static_text("Created"); 
      push_int(prop.header->Created[0]); 
      push_int(prop.header->Created[1]); 
      f_aggregate(2); 
      push_static_text("Modified"); 
      push_int(prop.header->Modified[0]); 
      push_int(prop.header->Modified[1]); 
      f_aggregate(2); 
      push_static_text("xMin"); push_int(prop.header->xMin); 
      push_static_text("yMin"); push_int(prop.header->yMin); 
      push_static_text("xMax"); push_int(prop.header->xMax); 
      push_static_text("yMax"); push_int(prop.header->yMax); 
      push_static_text("Mac_Style"); push_int(prop.header->Mac_Style); 
      push_static_text("Lowest_Rec_PPEM"); push_int(prop.header->Lowest_Rec_PPEM); 
      push_static_text("Font_Direction"); push_int(prop.header->Font_Direction); 
      push_static_text("Index_To_Loc_Format"); 
      push_int(prop.header->Index_To_Loc_Format); 
      push_static_text("Glyph_Data_Format"); 
      push_int(prop.header->Glyph_Data_Format); 
      f_aggregate_mapping(17*2); 
   } 
   else push_int(0); 
 
   push_static_text("horizontal"); 
   if (prop.horizontal) 
   { 
      push_static_text("Version"); push_int(prop.horizontal->Version); 
      push_static_text("Ascender"); push_int(prop.horizontal->Ascender); 
      push_static_text("Descender"); push_int(prop.horizontal->Descender); 
      push_static_text("Line_Gap"); push_int(prop.horizontal->Line_Gap); 
      push_static_text("advance_Width_Max"); push_int(prop.horizontal->advance_Width_Max); 
      push_static_text("min_Left_Side_Bearing"); push_int(prop.horizontal->min_Left_Side_Bearing); 
      push_static_text("min_Right_Side_Bearing"); push_int(prop.horizontal->min_Right_Side_Bearing); 
      push_static_text("xMax_Extent"); push_int(prop.horizontal->xMax_Extent); 
      push_static_text("caret_Slope_Rise"); push_int(prop.horizontal->caret_Slope_Rise); 
      push_static_text("caret_Slope_Run"); push_int(prop.horizontal->caret_Slope_Run); 
      push_static_text("metric_Data_Format"); push_int(prop.horizontal->metric_Data_Format); 
      push_static_text("number_Of_HMetrics"); push_int(prop.horizontal->number_Of_HMetrics); 
      f_aggregate_mapping(13*2); 
   } 
   else push_int(0); 
 
   push_static_text("os2"); 
   if (prop.os2) 
   { 
      push_static_text("version"); push_int(prop.os2->version); 
      push_static_text("xAvgCharWidth"); push_int(prop.os2->xAvgCharWidth); 
      push_static_text("usWeightClass"); push_int(prop.os2->usWeightClass); 
      push_static_text("usWidthClass"); push_int(prop.os2->usWidthClass); 
      push_static_text("fsType"); push_int(prop.os2->fsType); 
      push_static_text("ySubscriptXSize"); push_int(prop.os2->ySubscriptXSize); 
      push_static_text("ySubscriptYSize"); push_int(prop.os2->ySubscriptYSize); 
      push_static_text("ySubscriptXOffset"); push_int(prop.os2->ySubscriptXOffset); 
      push_static_text("ySubscriptYOffset"); push_int(prop.os2->ySubscriptYOffset); 
      push_static_text("ySuperscriptXSize"); push_int(prop.os2->ySuperscriptXSize); 
      push_static_text("ySuperscriptYSize"); push_int(prop.os2->ySuperscriptYSize); 
      push_static_text("ySuperscriptXOffset"); push_int(prop.os2->ySuperscriptXOffset); 
      push_static_text("ySuperscriptYOffset"); push_int(prop.os2->ySuperscriptYOffset); 
      push_static_text("yStrikeoutSize"); push_int(prop.os2->yStrikeoutSize); 
      push_static_text("yStrikeoutPosition"); push_int(prop.os2->yStrikeoutPosition); 
      push_static_text("sFamilyClass"); push_int(prop.os2->sFamilyClass); 
 
      push_static_text("panose"); 
      push_string(make_shared_binary_string(prop.os2->panose,10)); 
 
      push_static_text("ulUnicodeRange1"); push_int(prop.os2->ulUnicodeRange1); 
      push_static_text("ulUnicodeRange2"); push_int(prop.os2->ulUnicodeRange2); 
      push_static_text("ulUnicodeRange3"); push_int(prop.os2->ulUnicodeRange3); 
      push_static_text("ulUnicodeRange4"); push_int(prop.os2->ulUnicodeRange4); 
 
      push_static_text("achVendID"); 
      push_string(make_shared_binary_string(prop.os2->achVendID,4)); 
 
      push_static_text("fsSelection"); push_int(prop.os2->fsSelection); 
      push_static_text("usFirstCharIndex"); push_int(prop.os2->usFirstCharIndex); 
      push_static_text("usLastCharIndex"); push_int(prop.os2->usLastCharIndex); 
      push_static_text("sTypoAscender"); push_int(prop.os2->sTypoAscender); 
      push_static_text("sTypoDescender"); push_int(prop.os2->sTypoDescender); 
      push_static_text("sTypoLineGap"); push_int(prop.os2->sTypoLineGap); 
      push_static_text("usWinAscent"); push_int(prop.os2->usWinAscent); 
      push_static_text("usWinDescent"); push_int(prop.os2->usWinDescent); 
      push_static_text("ulCodePageRange1"); push_int(prop.os2->ulCodePageRange1); 
      push_static_text("ulCodePageRange2"); push_int(prop.os2->ulCodePageRange2); 
 
      f_aggregate_mapping(32*2); 
   } 
   else push_int(0); 
 
   push_static_text("postscript"); 
   if (prop.postscript) 
   { 
      push_static_text("FormatType"); push_int(prop.postscript->FormatType); 
      push_static_text("italicAngle"); push_int(prop.postscript->italicAngle); 
      push_static_text("underlinePosition"); push_int(prop.postscript->underlinePosition); 
      push_static_text("underlineThickness"); push_int(prop.postscript->underlineThickness); 
      push_static_text("isFixedPitch"); push_int(prop.postscript->isFixedPitch); 
      push_static_text("minMemType42"); push_int(prop.postscript->minMemType42); 
      push_static_text("maxMemType42"); push_int(prop.postscript->maxMemType42); 
      push_static_text("minMemType1"); push_int(prop.postscript->minMemType1); 
      push_static_text("maxMemType1"); push_int(prop.postscript->maxMemType1); 
      f_aggregate_mapping(9*2); 
   } 
   else push_int(0); 
 
   push_static_text("hdmx"); 
   if (prop.hdmx) 
   { 
      int i; 
 
      push_static_text("version"); push_int(prop.hdmx->version); 
      push_static_text("num_records"); push_int(prop.hdmx->num_records); 
      push_static_text("records"); 
 
      for (i=0; i<prop.hdmx->num_records; i++) 
      { 
         push_static_text("ppem"); push_int(prop.hdmx->records[i].ppem); 
         push_static_text("max_width"); push_int(prop.hdmx->records[i].max_width); 
         /*    push_text("widths"); push_int(prop.hdmx->records[i].widths);*/ 
         f_aggregate_mapping(2*2); 
      } 
      f_aggregate(prop.hdmx->num_records); 
 
      f_aggregate_mapping(3*2); 
   } 
   else push_int(0); 
 
   f_aggregate_mapping(9*2); 
} 
#endif /* TTF_DEBUG_INFO */ 
 
/* 
**! method object flush() 
**!     This flushes all cached information. 
**!     Might be used to save memory - the face 
**!     information is read back from disk upon need. 
**! 
**! returns the object being called 
*/ 
 
static void image_ttf_face_flush(INT32 args) 
{ 
   int res; 
   pop_n_elems(args); 
 
   if ((res=TT_Flush_Face(THISf->face))) 
      my_tt_error("Image.TTF.Face->flush()","",res); 
 
   ref_push_object(THISOBJ); 
} 
 
/* 
**! method mapping names() 
**! method array(array) _names() 
**!     Gives back the names or the complete name-list 
**!     of this face. 
**! 
**!     The result from <ref>names</ref>() is a mapping, 
**!     which has any or all of these indices: 
**!     <pre> 
**!     "copyright":   ("Copyright the Foo Corporation...bla bla") 
**!     "family":      ("My Little Font") 
**!     "style":       ("Bold") 
**!     "full":        ("Foo: My Little Font: 1998") 
**!     "expose":      ("My Little Font Bold") 
**!     "version":     ("June 1, 1998; 1.00, ...") 
**!     "postscript":  ("MyLittleFont-Bold") 
**!     "trademark":   ("MyLittleFont is a registered...bla bla") 
**!     </pre> 
**! 
**!     This is extracted from the information from _names(), 
**!     and fit into pike-strings using unicode or iso-8859-1, 
**!     if possible. 
**! 
**!     The result from <ref>_names</ref>() is a 
**!     matrix, on this form: <pre> 
**!          ({ ({ int platform, encoding, language, id, string name }), 
**!             ({ int platform, encoding, language, id, string name }), 
**!             ... 
**!          }) 
**!     </pre> 
**! 
**! returns the name as a mapping to string or the names as a matrix 
**! 
**! note: 
**!     To use the values from <ref>_names</ref>(), 
**!     check the TrueType standard documentation. 
*/ 
 
static void image_ttf_face__names(INT32 args) 
{ 
   int ns,res; 
   TT_UShort i; 
   TT_Face face=THISf->face; 
   pop_n_elems(args); 
 
   if ((ns=TT_Get_Name_Count(face))==-1) 
      Pike_error("Image.TTF.Face->names(): Illegal face handler\n"); 
 
   for (i=0; i<ns; i++) 
   { 
      unsigned short platformID,encodingID,languageID,nameID; 
      TT_UShort length; 
      char *stringPtr; 
 
      if ((res=TT_Get_Name_ID(face,i, 
                              &platformID,&encodingID,&languageID,&nameID))) 
         my_tt_error("Image.TTF.Face->names()","TT_Get_Name_ID: ",res); 
 
      push_int(platformID); 
      push_int(encodingID); 
      push_int(languageID); 
      push_int(nameID); 
 
      if ((res=TT_Get_Name_String(face,i,&stringPtr,&length))) 
         my_tt_error("Image.TTF.Face->names()","TT_Get_Name_String: ",res); 
 
      push_string(make_shared_binary_string(stringPtr,length)); 
 
      f_aggregate(5); 
   } 
   f_aggregate(ns); 
} 
 
static void image_ttf_face_names(INT32 args) 
{ 
   int n,i; 
   int has[8]={0,0,0,0,0,0,0,0}; /* iso8859=20, unicode=30, any=1 */ 
   char *hasname[8]={"copyright","family","style","full", 
                     "expose","version","postscript","trademark"}; 
   struct array *a,*b; 
 
   image_ttf_face__names(args); 
 
   if (TYPEOF(sp[-1]) != T_ARRAY) 
      Pike_error("Image.TTF.Face->names(): internal error, weird _names()\n"); 
 
   a=sp[-1].u.array; 
 
   n=0; 
   for (i=0; i<a->size; i++) 
   { 
      int ihas=1; 
      int what; 
      b=a->item[i].u.array; 
 
      what=b->item[3].u.integer; 
      if (what>=8 || what<0) continue; /* weird */ 
      switch (b->item[0].u.integer*100+b->item[1].u.integer) 
      { 
         case 301: /* M$:  unicode */ 
         case 300: /* M$:  unicode (?) */ 
            ihas=30; 
            break; 
         case 202: /* ISO: iso-8859-1 */ 
            ihas=20; 
            break; 
      } 
      if (ihas<has[what]) continue; /* worse */ 
 
      push_text(hasname[what]); 
 
      if (ihas==30) /* unicode, M$ but weird enough correct byteorder */ 
      { 
         ptrdiff_t n = b->item[4].u.string->len/2; 
         struct pike_string *ps=begin_wide_shared_string(n,1); 
         p_wchar1 *d=STR1(ps); 
         p_wchar0 *s=STR0(b->item[4].u.string); 
         while (n--) *(d++)=((p_wchar1)s[0]<<8)|(p_wchar1)s[1],s+=2; 
         push_string(end_shared_string(ps)); 
      } 
      else 
         push_svalue(b->item+4); 
 
      n++; 
   } 
   f_aggregate_mapping(n*2); 
   stack_swap(); 
   pop_stack(); 
} 
 
/* 
**! method object `()() 
**!     This instantiates the face for normal usage - 
**!     to convert font data to images. 
**! 
**! returns a <ref>Image.TTF.FaceInstance</ref> object. 
*/ 
 
static void image_ttf_face_make(INT32 args) 
{ 
   pop_n_elems(args); 
 
   ref_push_object(THISOBJ); 
   push_object(clone_object(image_ttf_faceinstance_program,1)); 
} 
 
/* 
**! class FaceInstance 
**!     This is the instance of a face, with geometrics, 
**!     encodings and stuff. 
**! 
**! method void create(object face) 
**!     creates a new Instance from a face. 
*/ 
 
static void ttf_instance_setc(struct image_ttf_face_struct *face_s, 
                              struct image_ttf_faceinstance_struct *face_i, 
                              int towhat, 
                              char *where) 
{ 
   TT_Face_Properties prop; 
   TT_Instance_Metrics metr; 
   int res; 
   int resol; 
 
   if ((res=TT_Get_Face_Properties(face_s->face,&prop))) 
      my_tt_error(where,"TT_Get_Face_Properties",res); 
 
   resol=58; /* should be 72, but glyphs fit using this value */ 
      /* 
         (int)((72*(prop.horizontal->Ascender+ 
                    prop.horizontal->Descender)/ 
                (float)prop.horizontal->Ascender)); 
*/ 
 
   if ((res=TT_Set_Instance_Resolutions(face_i->instance, 
                                        (TT_UShort)resol, (TT_UShort)resol))) 
      my_tt_error("Image.TTF.FaceInstance()", 
                  "TT_Set_Instance_Resolutions: ",res); 
 
   if ((res=TT_Get_Instance_Metrics(face_i->instance,&metr))) 
      my_tt_error(where,"TT_Get_Instance_Metrics",res); 
 
   if ((res=TT_Set_Instance_CharSize(face_i->instance,towhat))) 
      my_tt_error(where,"TT_Set_Instance_CharSize: ",res); 
 
   face_i->baseline= 
      DOUBLE_TO_INT(((double)(towhat/64.0+towhat/640.0)* 
                     prop.horizontal->Ascender)/ 
                    (prop.horizontal->Ascender - prop.horizontal->Descender)); 
 
   face_i->height= (towhat/64 + towhat/640); 
 
   face_i->trans = ~63 & 
      (32 + 
       DOUBLE_TO_INT(64*((towhat/64.0+towhat/640.0)* 
                         prop.horizontal->Ascender)/ 
                     (prop.horizontal->Ascender-prop.horizontal->Descender))); 
} 
 
static void image_ttf_faceinstance_create(INT32 args) 
{ 
   struct image_ttf_face_struct *face_s = NULL; 
   struct image_ttf_faceinstance_struct *face_i=THISi; 
   int res; 
 
   if (!args) 
      Pike_error("Image.TTF.FaceInstance(): too few arguments\n"); 
 
   if (TYPEOF(sp[-args]) != T_OBJECT || 
       !(face_s=get_storage(sp[-args].u.object,image_ttf_face_program))) 
      Pike_error("Image.TTF.FaceInstance(): illegal argument 1\n"); 
 
   if ((res=TT_New_Instance(face_s->face,&(face_i->instance)))) 
      my_tt_error("Image.TTF.FaceInstance()","TT_New_Instance: ",res); 
 
   face_i->load_flags = TTLOAD_SCALE_GLYPH|TTLOAD_HINT_GLYPH; 
   add_ref(face_i->faceobj=sp[-args].u.object); 
 
   ttf_instance_setc(face_s,face_i,32*64,"Image.TTF.FaceInstance()"); 
} 
 
static void image_ttf_faceinstance_set_height(INT32 args) 
{ 
   struct image_ttf_face_struct *face_s; 
   struct image_ttf_faceinstance_struct *face_i=THISi; 
   int h=0; 
 
   if (!args) 
      Pike_error("Image.TTF.FaceInstance->set_height(): missing arguments\n"); 
 
   if (TYPEOF(sp[-args]) == T_INT) 
      h = sp[-args].u.integer*64; 
   else if (TYPEOF(sp[-args]) == T_FLOAT) 
      h = DOUBLE_TO_INT(sp[-args].u.float_number*64); 
   else 
      Pike_error("Image.TTF.FaceInstance->set_height(): illegal argument 1\n"); 
   if (h<1) h=1; 
 
   if (!(face_s=get_storage(THISi->faceobj,image_ttf_face_program))) 
      Pike_error("Image.TTF.FaceInstance->write(): lost Face\n"); 
 
   ttf_instance_setc(face_s,face_i,h,"Image.TTF.FaceInstance->set_height()"); 
 
   pop_n_elems(args); 
   ref_push_object(THISOBJ); 
} 
 
static void ttf_translate_8bit(TT_CharMap charMap, 
                               unsigned char *what, 
                               int **dest, 
                               int len, 
                               int base) 
{ 
   int i; 
 
   dest[0]=xalloc(len*sizeof(int)); 
 
   THREADS_ALLOW(); 
   for (i=0; i<len; i++) 
      dest[0][i]=TT_Char_Index(charMap, (TT_UShort)(what[i]+base)); 
   THREADS_DISALLOW(); 
} 
 
 
static void ttf_translate_16bit(TT_CharMap charMap, 
                                unsigned short *what, 
                                int **dest, 
                                int len, 
                                int base) 
{ 
   int i; 
 
   dest[0]=xalloc(len*sizeof(int)); 
 
   THREADS_ALLOW(); 
   for (i=0; i<len; i++) 
      dest[0][i] = TT_Char_Index(charMap, (TT_UShort)(what[i] + base)); 
   THREADS_DISALLOW(); 
} 
 
static void ttf_get_nice_charmap(TT_Face face, 
                                 TT_CharMap *charMap, 
                                 char *where) 
{ 
   int n,i,res,got=-1,best=-1; 
   if (-1==(n=TT_Get_CharMap_Count(face))) 
      Pike_error("%s: illegal face handle\n",where); 
 
   for (i=0; i<n; i++) 
   { 
      int ihas=0; 
      TT_UShort platformID, encodingID; 
 
      if ((res=TT_Get_CharMap_ID(face, (TT_UShort)i, 
                                 &platformID, &encodingID))) 
         my_tt_error(where,"TT_Get_CharMap_ID: ",res); 
 
      switch (platformID*100+encodingID) 
      { 
         case 301: /* M$:  unicode */ 
         case 300: /* M$:  unicode (?) */ 
            ihas=30; 
            break; 
         case 202: /* ISO: iso-8859-1 */ 
            ihas=20; 
            break; 
       default: 
            ihas = 1; 
            break; 
      } 
 
      if (ihas>got) 
      { 
         best=i; 
         got=ihas; 
      } 
   } 
   if (got==-1) 
      Pike_error("%s: no charmaps at all\n",where); 
 
   if ((res=TT_Get_CharMap(face, (TT_UShort)best, charMap))) 
      my_tt_error(where,"TT_Get_CharMap: ",res); 
} 
 
static void ttf_please_translate_8bit(TT_Face face, 
                                      struct pike_string *s, 
                                      int **dest,int *dlen, 
                                      int base, 
                                      char *where) 
{ 
   TT_CharMap charMap; 
 
   ttf_get_nice_charmap(face,&charMap,where); 
   (*dlen) = s->len; 
   ttf_translate_8bit(charMap,(unsigned char*)(s->str),dest,*dlen,base); 
} 
 
static void ttf_please_translate_16bit(TT_Face face, 
                                      struct pike_string *s, 
                                      int **dest,int *dlen, 
                                      int base, 
                                      char *where) 
{ 
   TT_CharMap charMap; 
 
   ttf_get_nice_charmap(face,&charMap,where); 
   (*dlen) = s->len; 
   ttf_translate_16bit(charMap,(unsigned short*)(s->str),dest,*dlen,base); 
} 
 
 
static void image_ttf_faceinstance_ponder(INT32 args) 
{ 
   int *sstr; 
   int len,i,res,base=0; 
   struct image_ttf_face_struct *face_s; 
   struct image_ttf_faceinstance_struct *face_i=THISi; 
 
   int xmin=1000,xmax=-1000,pos=0; 
 
   if (!(face_s=get_storage(THISi->faceobj,image_ttf_face_program))) 
      Pike_error("Image.TTF.FaceInstance->ponder(): lost Face\n"); 
 
   if (args && TYPEOF(sp[-1]) == T_INT) 
   { 
      base=sp[-1].u.integer; 
      args--; 
      pop_stack(); 
   } 
 
   if (TYPEOF(sp[-args]) != T_STRING) 
      Pike_error("Image.TTF.FaceInstance->ponder(): illegal argument 1\n"); 
 
   switch( sp[-args].u.string->size_shift ) 
   { 
    case 0: 
      ttf_please_translate_8bit(face_s->face, 
                                sp[-args].u.string,&sstr,&len,base, 
                                "Image.TTF.FaceInstance->ponder()"); 
      break; 
    case 1: 
      ttf_please_translate_16bit(face_s->face, 
                                 sp[-args].u.string,&sstr,&len,base, 
                                 "Image.TTF.FaceInstance->ponder()"); 
      break; 
    default: 
     Pike_error("Too wide string for truetype\n"); 
   } 
   pop_n_elems(args); 
 
   for (i=0; i<len; i++) 
   { 
      TT_Glyph glyph; 
      TT_Glyph_Metrics metrics; 
      int ind; 
 
      ind=sstr[i]; 
/*       fprintf(stderr,"glyph: %d\n",ind); */ 
 
      if ((res=TT_New_Glyph(face_s->face,&glyph))) 
         my_tt_error("Image.TTF.FaceInstance->ponder()","TT_New_Glyph: ",res); 
 
      if ((res=TT_Load_Glyph(face_i->instance, glyph, 
                             (TT_UShort)ind, (TT_UShort)face_i->load_flags))) 
         my_tt_error("Image.TTF.FaceInstance->ponder()","TT_Load_Glyph: ",res); 
 
      if ((res=TT_Get_Glyph_Metrics(glyph,&metrics))) 
         my_tt_error("Image.TTF.FaceInstance->ponder()", 
                     "TT_Get_Glyph_Metrics: ",res); 
 
      if (pos+metrics.bbox.xMin<xmin) xmin=pos+metrics.bbox.xMin; 
      if (pos+metrics.bbox.xMax>xmax) xmax=pos+metrics.bbox.xMax; 
      pos+=metrics.advance; /* insert kerning stuff here */ 
 
/*       fprintf(stderr,"bbox: (%f,%f)-(%f,%f)\n", */ 
/*          metrics.bbox.xMin/64.0, */ 
/*          metrics.bbox.yMin/64.0, */ 
/*          metrics.bbox.xMax/64.0, */ 
/*          metrics.bbox.yMax/64.0); */ 
 
/*       fprintf(stderr,"BearingX: %f\n",metrics.bearingX/64.0); */ 
/*       fprintf(stderr,"BearingY: %f\n",metrics.bearingY/64.0); */ 
/*       fprintf(stderr,"advance: %f\n",metrics.advance/64.0); */ 
 
/*       fprintf(stderr,"\n"); */ 
   } 
 
   free(sstr); 
 
/*    fprintf(stderr,"xmin: %f\n",xmin/64.0); */ 
/*    fprintf(stderr,"xmax: %f\n",xmax/64.0); */ 
 
   ref_push_object(THISOBJ); 
} 
 
static int find_kerning( TT_Kerning kerning, int c1, int c2 ) 
{ 
  int j; 
/*   fprintf(stderr, "kern: %d %d\n", c1, c2); */ 
  for(j=0; j<kerning.nTables; j++) 
  { 
    if( (kerning.tables[j].coverage & 0x0f) != 1 ) /* We don't want it  */ 
      continue; 
    switch(kerning.tables[j].format) 
    { 
     case 2: /* The smart one */ 
       { 
         int ind; 
         int lclass=0, rclass=0; 
         TT_Kern_2 table = kerning.tables[j].t.kern2; 
         ind = c1 - table.leftClass.firstGlyph; 
         if(ind >= table.leftClass.nGlyphs || ind < 0) 
           continue; 
         lclass = table.leftClass.classes[ ind ]; 
         ind = c2 - table.rightClass.firstGlyph; 
         if(ind >= table.rightClass.nGlyphs || ind < 0) 
           continue; 
         rclass = table.rightClass.classes[ ind ]; 
 
         return table.array[ rclass+lclass ]; 
       } 
     case 0: /* The microsoft one */ 
       { 
         TT_Kern_0 table = kerning.tables[j].t.kern0; 
         int a=0,b=table.nPairs-1; 
 
         while (a<b) 
         { 
            int i=(a+b)/2; 
            if(table.pairs[i].left == c1) 
            { 
               if(table.pairs[i].right == c2) 
               { 
                  /* fprintf(stderr, "found kerning\n"); */ 
                  return table.pairs[i].value; 
               } 
               else if(table.pairs[i].right < c2) 
                  a=i+1; 
               else 
                  b=i-1; 
            } else if(table.pairs[i].left < c1) 
               a=i+1; 
            else 
               b=i-1; 
         } 
       } 
       break; 
     default: 
       fprintf(stderr, "Warning: Unknown kerning table format %d\n", 
               kerning.tables[j].format); 
    } 
  } 
  return 0; 
} 
 
static void image_ttf_faceinstance_write(INT32 args) 
{ 
   int **sstr; 
   int *slen; 
   int i,res=0,base=0,a; 
   struct image_ttf_face_struct *face_s; 
   struct image_ttf_faceinstance_struct *face_i=THISi; 
   TT_CharMap charMap; 
   TT_Kerning kerning; 
   int has_kerning = 0; 
   char *errs=NULL; 
   int scalefactor=0; 
   int xmin=1000,xmax=-1000,pos=0,ypos; 
   int width,height,mod; 
 
   unsigned char* pixmap; 
   int maxcharwidth = 0; 
 
   if (!(face_s=get_storage(THISi->faceobj,image_ttf_face_program))) 
      Pike_error("Image.TTF.FaceInstance->write(): lost Face\n"); 
 
   if(!TT_Get_Kerning_Directory( face_s->face, &kerning )) 
   { 
     TT_Instance_Metrics metrics; 
/*      fprintf(stderr, "has kerning!\n"); */ 
     has_kerning = 1; 
     if(TT_Get_Instance_Metrics( face_i->instance, &metrics )) 
       Pike_error("Nope. No way.\n"); 
     scalefactor = metrics.x_scale; 
/*      fprintf(stderr, "offset=%d\n", (int)metrics.x_scale); */ 
   } 
 
   if (args && TYPEOF(sp[-1]) == T_INT) 
   { 
      base=sp[-1].u.integer; 
      args--; 
      pop_stack(); 
   } 
 
   if (!args) 
   { 
      push_empty_string(); 
      args=1; 
   } 
 
   ttf_get_nice_charmap(face_s->face,&charMap, 
                        "Image.TTF.FaceInstance->write()"); 
 
   sstr=alloca(args*sizeof(int*)); 
   slen=alloca(args*sizeof(int)); 
 
   /* first pass: figure out total bounding box */ 
 
   for (a=0; a<args; a++) 
   { 
     char *errs=NULL; 
     TT_Glyph_Metrics metrics; 
 
      if (TYPEOF(sp[a-args]) != T_STRING) 
         Pike_error("Image.TTF.FaceInstance->write(): illegal argument %d\n",a+1); 
 
      switch(sp[a-args].u.string->size_shift) 
      { 
       case 0: 
         ttf_translate_8bit(charMap,(unsigned char*)sp[a-args].u.string->str, 
                            sstr+a, 
                            slen[a]=sp[a-args].u.string->len, 
                            base); 
         break; 
       case 1: 
         ttf_translate_16bit(charMap,(unsigned short*)sp[a-args].u.string->str, 
                             sstr+a, 
                             slen[a]=sp[a-args].u.string->len, 
                             base); 
         break; 
       case 2: 
         Pike_error("Too wide string for truetype\n"); 
         break; 
      } 
 
      pos=0; 
      for (i=0; i<slen[a]; i++) 
      { 
         TT_Glyph glyph; 
         int ind; 
 
         ind=sstr[a][i]; 
/*     fprintf(stderr,"glyph: %d\n",ind); */ 
 
         if ((res=TT_New_Glyph(face_s->face,&glyph))) 
            { errs="TT_New_Glyph: "; break; } 
 
         if ((res=TT_Load_Glyph(face_i->instance, glyph, (TT_UShort)ind, 
                                (TT_UShort)face_i->load_flags))) 
            { errs="TT_Load_Glyph: "; break; } 
 
         if ((res=TT_Get_Glyph_Metrics(glyph,&metrics))) 
            { errs="TT_Get_Glyph_Metrics: "; break; } 
 
         if (pos+metrics.bbox.xMin<xmin) 
           xmin=pos+metrics.bbox.xMin; 
         if (pos+metrics.bbox.xMax>xmax) 
           xmax=pos+metrics.bbox.xMax; 
 
         if((metrics.bbox.xMax-(metrics.bbox.xMin<0?metrics.bbox.xMin:0)) 
            >maxcharwidth) 
           maxcharwidth = 
             (metrics.bbox.xMax-(metrics.bbox.xMin<0?metrics.bbox.xMin:0)); 
 
         pos+=metrics.advance; 
         if(has_kerning && i<slen[a]-1) 
         { 
           int kern = find_kerning( kerning, ind, sstr[a][i+1] ); 
           pos += DOUBLE_TO_INT(kern * (scalefactor/65535.0)); 
         } 
         if ((res=TT_Done_Glyph(glyph))) 
            { errs="TT_Done_Glyph: "; break; } 
      } 
      pos -= metrics.advance; 
      pos += metrics.bbox.xMax-metrics.bbox.xMin; 
      if (pos>xmax) 
        xmax=pos; 
      if (errs) 
      { 
         for (i=0; i<a; i++) free(sstr[i]); 
         my_tt_error("Image.TTF.FaceInstance->write()",errs,res); 
      } 
   } 
 
   pop_n_elems(args); 
 
/*    fprintf(stderr,"xmin=%f xmax=%f\n",xmin/64.0,xmax/64.0); */ 
 
   xmin&=~63; 
   width=((xmax-xmin+63)>>6)+4; 
   height=face_i->height*args; 
   mod=(4-(maxcharwidth&3))&3; 
   if (width<1) width=1; 
 
   if ((pixmap=malloc((maxcharwidth+mod)*face_i->height))) 
   { 
      /* second pass: write the stuff */ 
 
      TT_Raster_Map rastermap; 
      struct object *o; 
      struct image *img; 
      rgb_group *d; 
 
 
      rastermap.rows=face_i->height; 
      rastermap.cols=rastermap.width=maxcharwidth+mod; 
      rastermap.flow=TT_Flow_Down; 
      rastermap.bitmap=pixmap; 
      rastermap.size=rastermap.cols*rastermap.rows; 
 
      ypos=0; 
 
/*       fprintf(stderr,"rastermap.rows=%d cols=%d width=%d\n", */ 
/*          rastermap.rows,rastermap.cols,rastermap.width); */ 
 
 
      push_int(width); 
      push_int(height); 
      o=clone_object(image_program,2); 
      img=get_storage(o,image_program); 
      d=img->img; 
 
      for (a=0; a<args; a++) 
      { 
         pos=-xmin; 
         for (i=0; i<slen[a]; i++) 
         { 
            int sw, xp; 
            TT_Glyph glyph; 
            TT_Glyph_Metrics metrics; 
            int ind, x, y; 
 
            ind=sstr[a][i]; 
/*        fprintf(stderr,"glyph: %d\n",ind); */ 
 
 
            if ((res=TT_New_Glyph(face_s->face,&glyph))) 
               { errs="TT_New_Glyph: "; break; } 
 
            if ((res=TT_Load_Glyph(face_i->instance, glyph, (TT_UShort)ind, 
                                   (TT_UShort)face_i->load_flags))) 
               { errs="TT_Load_Glyph: "; break; } 
 
            if ((res=TT_Get_Glyph_Metrics(glyph,&metrics))) 
               { errs="TT_Get_Glyph_Metrics: "; break; } 
 
            memset(pixmap,0,rastermap.size); 
 
            if ((res=TT_Get_Glyph_Pixmap(glyph, 
                                         &rastermap, 
                                         -metrics.bbox.xMin 
                                         /*+pos%64*/, 
                                         face_i->height*64- 
                                         face_i->trans))) 
               { errs="TT_Get_Glyph_Pixmap: "; break; } 
 
 
            sw = metrics.bbox.xMax-(metrics.bbox.xMin<0?metrics.bbox.xMin:0); 
            /* Copy source pixmap to destination image object. */ 
            for(y=0; y<face_i->height; y++) 
            { 
              unsigned int s; 
              unsigned char * source = pixmap+rastermap.width*y; 
              rgb_group *dt=d+(ypos+y)*width+(xp=(metrics.bbox.xMin+pos)/64); 
 
              for(x=0; x<sw && xp<width; x++,xp++,source++,dt++) 
                if(xp<0 || !(s = *source)) 
                  continue; 
                else if((s=dt->r+s) < 256) 
                  dt->r=dt->g=dt->b=s; 
                else 
                  dt->r=dt->g=dt->b=255; 
            } 
 
            pos+=metrics.advance; 
/*        if(metrics.bbox.xMin < 0) */ 
/*          pos += metrics.bbox.xMin; */ 
            if(has_kerning && i<slen[a]-1) 
            { 
              int kern = find_kerning( kerning, sstr[a][i], sstr[a][i+1] ); 
              pos += DOUBLE_TO_INT(kern * (scalefactor/65535.0)); 
/*          fprintf(stderr, "Adjusted is %d\n", */ 
/*                  (int)(kern * (scalefactor/65535.0))); */ 
            } 
 
            if ((res=TT_Done_Glyph(glyph))) 
               { errs="TT_Done_Glyph: "; break; } 
         } 
         if (errs) 
         { 
            for (a=0; a<args; a++) free(sstr[a]); 
            free(pixmap); 
            free_object(o); 
            my_tt_error("Image.TTF.FaceInstance->write()",errs,res); 
         } 
         ypos+=face_i->height; 
      } 
      free(pixmap); 
      push_object(o); 
   } 
   else 
   { 
      Pike_error("Image.TTF.FaceInstance->write(): out of memory\n"); 
   } 
 
   for (a=0; a<args; a++) 
      free(sstr[a]); 
} 
 
static void image_ttf_faceinstance_face(INT32 args) 
{ 
   pop_n_elems(args); 
   ref_push_object(THISi->faceobj); 
} 
 
#endif /* HAVE_LIBTTF */ 
 
/*** module init & exit & stuff *****************************************/ 
 
 
PIKE_MODULE_EXIT 
{ 
   free_string(param_baseline); 
   free_string(param_quality); 
 
#ifdef HAVE_LIBTTF 
   TT_Done_FreeType(engine); 
   if (image_ttf_faceinstance_program) { 
     free_program(image_ttf_faceinstance_program); 
     image_ttf_faceinstance_program = NULL; 
   } 
   if (image_ttf_face_program) { 
     free_program(image_ttf_face_program); 
     image_ttf_face_program = NULL; 
   } 
#endif /* HAVE_LIBTTF */ 
} 
 
PIKE_MODULE_INIT 
{ 
#ifdef HAVE_LIBTTF 
   unsigned char palette[5]={0,64,128,192,255}; 
   TT_Error errcode; 
#endif /* HAVE_LIBTTF */ 
 
   param_baseline=make_shared_string("baseline"); 
   param_quality=make_shared_string("quality"); 
 
#ifdef HAVE_LIBTTF 
   /* First check that we actually can initialize the FreeType library. */ 
   if ((errcode = TT_Init_FreeType(&engine))) { 
     DWERR("TT_Init_FreeType() failed with code 0x%03lx!\n", 
           (unsigned long)errcode); 
     return; 
   } 
 
   TT_Set_Raster_Gray_Palette(engine, palette); 
   TT_Init_Kerning_Extension( engine ); 
 
#ifdef DYNAMIC_MODULE 
   push_static_text("Image.Image"); 
   SAFE_APPLY_MASTER("resolv",1); 
   if (TYPEOF(sp[-1]) == T_PROGRAM) 
      image_program=program_from_svalue(sp-1); 
   pop_stack(); 
#endif /* DYNAMIC_MODULE */ 
 
   if (image_program) 
   { 
      /* function(string,void|mapping(string:int):object) */ 
  ADD_FUNCTION("`()",image_ttf_make,tFunc(tStr tOr(tVoid,tMap(tStr,tInt)),tObj),0); 
 
      /* make face program */ 
 
      start_new_program(); 
      ADD_STORAGE(struct image_ttf_face_struct); 
 
#ifdef TTF_DEBUG_INFO 
      /* function(:mapping) */ 
  ADD_FUNCTION("properties",image_ttf_face_properties,tFunc(tNone,tMapping),0); 
#endif /* TTF_DEBUG_INFO */ 
 
      /* function(:object) */ 
  ADD_FUNCTION("flush",image_ttf_face_flush,tFunc(tNone,tObj),0); 
      /* function(:mapping(string:string)) */ 
  ADD_FUNCTION("names",image_ttf_face_names,tFunc(tNone,tMap(tStr,tStr)),0); 
      /* function(:array(array)) */ 
  ADD_FUNCTION("_names",image_ttf_face__names,tFunc(tNone,tArr(tArray)),0); 
 
      /* function(:object) */ 
  ADD_FUNCTION("`()",image_ttf_face_make,tFunc(tNone,tObj),0); 
 
      set_exit_callback(image_ttf_face_exit); 
      image_ttf_face_program=end_program(); 
 
      /* make face instance program */ 
 
      start_new_program(); 
      ADD_STORAGE(struct image_ttf_faceinstance_struct); 
 
      /* function(object:void) */ 
  ADD_FUNCTION("create",image_ttf_faceinstance_create,tFunc(tObj,tVoid),0); 
      /* function(string:object) */ 
  ADD_FUNCTION("ponder",image_ttf_faceinstance_ponder,tFunc(tStr,tObj),0); 
      /* function(string...:object) */ 
  ADD_FUNCTION("write",image_ttf_faceinstance_write,tFuncV(tNone,tStr,tObj),0); 
      /* function(:object) */ 
  ADD_FUNCTION("face",image_ttf_faceinstance_face,tFunc(tNone,tObj),0); 
      /* function(int:object) */ 
  ADD_FUNCTION("set_height",image_ttf_faceinstance_set_height,tFunc(tInt,tObj),0); 
 
      set_exit_callback(image_ttf_faceinstance_exit); 
      image_ttf_faceinstance_program=end_program(); 
   } 
#endif /* HAVE_LIBTTF */ 
}