-
Notifications
You must be signed in to change notification settings - Fork 0
/
freetype2.py
3544 lines (3194 loc) · 122 KB
/
freetype2.py
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
"""A Python 3 wrapper for FreeType <http://www.freetype.org/> using
ctypes. This is not a complete wrapper for all FreeType
functionality, but it should be comprehensive enough to be
useful. Functionality that is (mostly) covered (as per topics at
<http://www.freetype.org/freetype2/docs/reference/ft2-toc.html>):
* base interface
* glyph management
* multiple masters
* TrueType tables
* computations
* outline processing
* quick retrieval of advance values
* bitmap handling
* scanline converter
* glyph stroker
in addition to which, a convenience function is supplied to use
Fontconfig to find matching fonts, and functions are available to
interface to Pycairo, if installed:
* convert a Bitmap to an ImageSurface (requires that your Pycairo
support ImageSurface.create_for_data)
* draw the contours of an Outline as a Path
"""
#+
# Copyright 2015-2020 Lawrence D'Oliveiro <[email protected]>.
# Dual-licensed under the FreeType licence
# <http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/FTL.TXT>
# and GPLv2 <http://git.savannah.gnu.org/cgit/freetype/freetype2.git/tree/docs/GPLv2.TXT>
# or later, to be compatible with FreeType itself.
#-
import sys
import math
from numbers import \
Real
import array
import ctypes as ct
import ctypes.util
import struct
import weakref
try :
import cairo
except ImportError :
cairo = None
#end try
LIBNAME = \
{
"linux" :
{
"freetype" : "libfreetype.so.6",
"fontconfig" : "libfontconfig.so.1",
},
"openbsd6" :
{
"freetype" : "libfreetype.so.28",
"fontconfig" : "libfontconfig.so.11",
},
"darwin" :
{
"freetype" : "libfreetype.6.dylib",
"fontconfig" : "libfontconfig.1.dylib",
},
"win32" :
{
"freetype" : "libfreetype-6.dll",
"fontconfig" : "libfontconfig-1.dll",
},
}[sys.platform]
ft = ct.cdll.LoadLibrary(LIBNAME["freetype"])
try :
fc = ct.cdll.LoadLibrary(LIBNAME["fontconfig"])
except OSError as fail :
if True : # if fail.errno == 2 : # ENOENT
# no point checking, because it is None! (Bug?)
fc = None
else :
raise
#end if
#end try
libc = ct.cdll.LoadLibrary(ct.util.find_library("c"))
def struct_to_dict(item, itemtype, indirect, extra_decode = None) :
"decodes the elements of a ctypes Structure into a dict. extra_decode" \
" optionally specifies special conversions for particular fields."
if indirect :
item = item.contents
#end if
result = {}
for k in itemtype._fields_ :
k = k[0]
field = getattr(item, k)
if extra_decode != None :
decode = extra_decode.get(k)
if decode == None :
decode = extra_decode.get(None)
#end if
if decode != None :
field = decode(field)
#end if
#end if
result[k] = field
#end for
return \
result
#end struct_to_dict
class FT :
"useful definitions adapted from freetype.h. You will need to use the constants," \
" but apart from that, see the more Pythonic wrappers defined outside this" \
" class in preference to accessing low-level structures directly."
# General ctypes gotcha: when passing addresses of ctypes-constructed objects
# to routine calls, do not construct the objects directly in the call. Otherwise
# the refcount goes to 0 before the routine is actually entered, and the object
# can get prematurely disposed. Always store the object reference into a local
# variable, and pass the value of the variable instead.
Error = ct.c_int # hopefully this is always correct
c_ubyte_ptr = ct.POINTER(ct.c_ubyte)
class LibraryRec(ct.Structure) :
pass # private
#end LibraryRec
Library = ct.POINTER(LibraryRec)
Encoding = ct.c_uint
def ENC_TAG(*args) :
"creates an Encoding or Glyph_Format value from four byte values" \
" or a bytes or str value of length 4."
if len(args) == 4 :
c1, c2, c3, c4 = args
elif len(args) == 1 :
arg = args[0]
if isinstance(arg, (bytes, bytearray)) :
c1, c2, c3, c4 = tuple(arg)
elif isinstance(arg, str) :
args = tuple(ord(c) for c in arg)
if len(args) != 4 or not all(i < 128 for i in args) :
raise TypeError("TAG string must be 4 ASCII chars in [0 .. 255]")
#end if
c1, c2, c3, c4 = args
else :
raise TypeError("TAG arg must be bytes or string")
#end if
else :
raise TypeError("wrong nr of TAG args")
#end if
return \
c1 << 24 | c2 << 16 | c3 << 8 | c4
#end ENC_TAG
def DEC_TAG(tag, printable = False) :
"decomposes an Encoding value into a tuple or bytes object of" \
" four byte values."
result = (tag >> 24 & 255, tag >> 16 & 255, tag >> 8 & 255, tag & 255)
if printable :
result = bytes(result)
#end if
return \
result
#end DEC_TAG
ENCODING_NONE = ENC_TAG('\x00\x00\x00\x00')
ENCODING_MS_SYMBOL = ENC_TAG('symb')
ENCODING_UNICODE = ENC_TAG('unic')
ENCODING_SJIS = ENC_TAG('sjis')
ENCODING_GB2312 = ENC_TAG('gb ')
ENCODING_BIG5 = ENC_TAG('big5')
ENCODING_WANSUNG = ENC_TAG('wans')
ENCODING_JOHAB = ENC_TAG('joha')
# for backwards compatibility
ENCODING_MS_SJIS = ENCODING_SJIS
ENCODING_MS_GB2312 = ENCODING_GB2312
ENCODING_MS_BIG5 = ENCODING_BIG5
ENCODING_MS_WANSUNG = ENCODING_WANSUNG
ENCODING_MS_JOHAB = ENCODING_JOHAB
ENCODING_ADOBE_STANDARD = ENC_TAG('ADOB')
ENCODING_ADOBE_EXPERT = ENC_TAG('ADBE')
ENCODING_ADOBE_CUSTOM = ENC_TAG('ADBC')
ENCODING_ADOBE_LATIN_1 = ENC_TAG('lat1')
ENCODING_OLD_LATIN_2 = ENC_TAG('lat2')
ENCODING_APPLE_ROMAN = ENC_TAG('armn')
Glyph_Format = ct.c_uint
IMAGE_TAG = ENC_TAG
GLYPH_FORMAT_NONE = IMAGE_TAG('\x00\x00\x00\x00')
GLYPH_FORMAT_COMPOSITE = IMAGE_TAG('comp')
GLYPH_FORMAT_BITMAP = IMAGE_TAG('bits')
GLYPH_FORMAT_OUTLINE = IMAGE_TAG('outl')
GLYPH_FORMAT_PLOTTER = IMAGE_TAG('plot')
Pos = ct.c_long # might be integer, or 16.16 fixed, or 26.6 fixed
Fixed = ct.c_ulong # 16.16 fixed-point
Fixed_ptr = ct.POINTER(Fixed)
class Vector(ct.Structure) :
pass
Vector._fields_ = \
[
("x", Pos),
("y", Pos),
]
#end Vector
class Generic(ct.Structure) :
Finalizer = ct.CFUNCTYPE(None, ct.c_void_p)
_fields_ = \
[
("data", ct.c_void_p),
("finalizer", Finalizer),
]
#end Generic
class BBox(ct.Structure) :
pass
BBox._fields_ = \
[
("xMin", Pos),
("yMin", Pos),
("xMax", Pos),
("yMax", Pos),
]
#end BBox
class Bitmap_Size(ct.Structure) :
pass
Bitmap_Size._fields_ = \
[
("height", ct.c_short),
("width", ct.c_short),
("size", Pos),
("x_ppem", Pos),
("y_ppem", Pos),
]
#end Bitmap_Size
class Size_Metrics(ct.Structure) :
pass
Size_Metrics._fields_ = \
[
("x_ppem", ct.c_ushort), # horizontal pixels per EM
("y_ppem", ct.c_ushort), # vertical pixels per EM
("x_scale", Fixed), # scaling values used to convert font
("y_scale", Fixed), # units to 26.6 fractional pixels
("ascender", Pos), # ascender in 26.6 frac. pixels
("descender", Pos), # descender in 26.6 frac. pixels
("height", Pos), # text height in 26.6 frac. pixels
("max_advance", Pos), # max horizontal advance, in 26.6 pixels
]
#end Size_Metrics
class Glyph_Metrics(ct.Structure) :
pass
Glyph_Metrics._fields_ = \
[
("width", Pos),
("height", Pos),
("horiBearingX", Pos),
("horiBearingY", Pos),
("horiAdvance", Pos),
("vertBearingX", Pos),
("vertBearingY", Pos),
("vertAdvance", Pos),
]
#end Glyph_Metrics
class FaceRec(ct.Structure) :
"initial public part of an FT_Face"
pass # forward
#end FaceRec
Face = ct.POINTER(FaceRec)
class CharMapRec(ct.Structure) :
pass
CharMapRec._fields_ = \
[
("face", Face),
("encoding", Encoding),
("platform_id", ct.c_ushort),
("encoding_id", ct.c_ushort),
]
#end CharMapRec
CharMap = ct.POINTER(CharMapRec)
# CharMapRec.platform_id codes
# from <https://developer.apple.com/fonts/TrueType-Reference-Manual/RM06/Chap6cmap.html>
PLATFORM_UNICODE = 0
PLATFORM_MACINTOSH = 1
PLATFORM_ISO = 2 # deprecated
PLATFORM_MICROSOFT = 3
PLATFORM_CUSTOM = 4 # app-specific use
PLATFORM_ADOBE = 7
# CharMapRec.encoding_id values for PLATFORM_UNICODE
ENCODING_UNICODE_DEFAULT = 0
ENCODING_UNICODE_11 = 1 # Unicode 1.1
ENCODING_UNICODE_ISO10646_1993 = 2
ENCODING_UNICODE_20_BMP = 3 # Unicode 2.0+, BMP only
ENCODING_UNICODE_20_BMP_PLUS = 4 # Unicode 2.0+, BMP and beyond
ENCODING_UNICODE_VARSEQ = 5 # Unicode Variation Sequences
ENCODING_UNICODE_FULL = 6 # full Unicode coverage
class SizeRec(ct.Structure) :
Size_Internal = ct.c_void_p
SizeRec._fields_ = \
[
("face", Face), # parent face object
("generic", Generic), # generic pointer for client uses
("metrics", Size_Metrics), # size metrics
("internal", SizeRec.Size_Internal),
]
#end SizeRec
Size = ct.POINTER(SizeRec)
# values for Outline.flags
OUTLINE_NONE = 0x0
OUTLINE_OWNER = 0x1
OUTLINE_EVEN_ODD_FILL = 0x2
OUTLINE_REVERSE_FILL = 0x4
OUTLINE_IGNORE_DROPOUTS = 0x8
OUTLINE_SMART_DROPOUTS = 0x10
OUTLINE_INCLUDE_STUBS = 0x20
OUTLINE_HIGH_PRECISION = 0x100
OUTLINE_SINGLE_PASS = 0x200
class Outline(ct.Structure) :
pass
Outline._fields_ = \
[
("n_contours", ct.c_short), # number of contours in glyph
("n_points", ct.c_short), # number of points in the glyph
("points", ct.POINTER(Vector)), # the outline's points
("tags", c_ubyte_ptr), # the points flags
("contours", ct.POINTER(ct.c_short)), # the contour end points
("flags", ct.c_uint), # outline masks
]
#end Outline
Pixel_Mode = ct.c_uint
# values for Pixel_Mode
PIXEL_MODE_NONE = 0
PIXEL_MODE_MONO = 1
PIXEL_MODE_GRAY = 2
PIXEL_MODE_GRAY2 = 3
PIXEL_MODE_GRAY4 = 4
PIXEL_MODE_LCD = 5
PIXEL_MODE_LCD_V = 6
class Bitmap(ct.Structure) :
_fields_ = \
[
("rows", ct.c_int),
("width", ct.c_int),
("pitch", ct.c_int),
("buffer", ct.c_void_p),
("num_grays", ct.c_short),
("pixel_mode", ct.c_byte),
("palette_mode", ct.c_byte),
("palette", ct.c_void_p),
]
#end Bitmap
BitmapPtr = ct.POINTER(Bitmap)
class GlyphSlotRec(ct.Structure) :
Slot_Internal = ct.c_void_p
SubGlyph = ct.c_void_p
pass # forward
GlyphSlot = ct.POINTER(GlyphSlotRec)
GlyphSlotRec._fields_ = \
[
("library", Library),
("face", Face),
("next", GlyphSlot),
("reserved", ct.c_uint), # retained for binary compatibility
("generic", Generic),
("metrics", Glyph_Metrics),
("linearHoriAdvance", Fixed),
("linearVertAdvance", Fixed),
("advance", Vector),
("format", Glyph_Format),
("bitmap", Bitmap),
("bitmap_left", ct.c_int),
("bitmap_top", ct.c_int),
("outline", Outline),
("num_subglyphs", ct.c_uint),
("subglyphs", GlyphSlotRec.SubGlyph),
("control_data", ct.c_void_p),
("control_len", ct.c_long),
("lsb_delta", Pos),
("rsb_delta", Pos),
("other", ct.c_void_p),
("internal", GlyphSlotRec.Slot_Internal),
]
#end GlyphSlotRec
FACE_FLAG_SCALABLE = ( 1 << 0 )
FACE_FLAG_FIXED_SIZES = ( 1 << 1 )
FACE_FLAG_FIXED_WIDTH = ( 1 << 2 )
FACE_FLAG_SFNT = ( 1 << 3 )
FACE_FLAG_HORIZONTAL = ( 1 << 4 )
FACE_FLAG_VERTICAL = ( 1 << 5 )
FACE_FLAG_KERNING = ( 1 << 6 )
FACE_FLAG_FAST_GLYPHS = ( 1 << 7 )
FACE_FLAG_MULTIPLE_MASTERS = ( 1 << 8 )
FACE_FLAG_GLYPH_NAMES = ( 1 << 9 )
FACE_FLAG_EXTERNAL_STREAM = ( 1 << 10 )
FACE_FLAG_HINTER = ( 1 << 11 )
FACE_FLAG_CID_KEYED = ( 1 << 12 )
FACE_FLAG_TRICKY = ( 1 << 13 )
FACE_FLAG_COLOR = ( 1 << 14 )
STYLE_FLAG_ITALIC = ( 1 << 0 )
STYLE_FLAG_BOLD = ( 1 << 1 )
KERNING_DEFAULT = 0 # scaled and grid-fitted
KERNING_UNFITTED = 1 # scaled but not grid-fitted
KERNING_UNSCALED = 2 # return value in original font units
#class FaceRec(ct.Structure) :
# "initial public part of an FT_Face"
FaceRec._fields_ = \
[
("num_faces", ct.c_long),
("face_index", ct.c_long),
("face_flags", ct.c_ulong),
("style_flags", ct.c_ulong),
("num_glyphs", ct.c_long),
("family_name", ct.c_char_p),
("style_name", ct.c_char_p),
("num_fixed_sizes", ct.c_long),
("available_sizes", ct.POINTER(Bitmap_Size)),
("num_charmaps", ct.c_long),
("charmaps", ct.POINTER(CharMap)),
("generic", Generic),
# The following member variables (down to `underline_thickness')
# are only relevant to scalable outlines; cf. @FT_Bitmap_Size
# for bitmap fonts.
("bbox", BBox),
("units_per_EM", ct.c_ushort),
("ascender", ct.c_short),
("descender", ct.c_short),
("height", ct.c_short),
("max_advance_width", ct.c_short),
("max_advance_height", ct.c_short),
("underline_position", ct.c_short),
("underline_thickness", ct.c_short),
("glyph", GlyphSlot),
("size", Size),
("charmap", CharMap),
# additional private fields follow
]
#end FaceRec
Size_Request_Type = ct.c_uint
# values for Size_Request_Type
SIZE_REQUEST_TYPE_NOMINAL = 0
SIZE_REQUEST_TYPE_REAL_DIM = 1
SIZE_REQUEST_TYPE_BBOX = 2
SIZE_REQUEST_TYPE_CELL = 3
SIZE_REQUEST_TYPE_SCALES = 4
SIZE_REQUEST_TYPE_MAX = 5
class Size_RequestRec(ct.Structure) :
pass
Size_RequestRec._fields_ = \
[
("type", Size_Request_Type),
("width", ct.c_long),
("height", ct.c_long),
("horiResolution", ct.c_uint), # actually 26.6, it appears
("vertResolution", ct.c_uint), # actually 26.6, it appears
]
#end Size_RequestRec
Size_Request = ct.POINTER(Size_RequestRec)
LOAD_DEFAULT = 0x0
LOAD_NO_SCALE = ( 1 << 0 )
LOAD_NO_HINTING = ( 1 << 1 )
LOAD_RENDER = ( 1 << 2 )
LOAD_NO_BITMAP = ( 1 << 3 )
LOAD_VERTICAL_LAYOUT = ( 1 << 4 )
LOAD_FORCE_AUTOHINT = ( 1 << 5 )
LOAD_CROP_BITMAP = ( 1 << 6 )
LOAD_PEDANTIC = ( 1 << 7 )
LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH = ( 1 << 9 )
LOAD_NO_RECURSE = ( 1 << 10 )
LOAD_IGNORE_TRANSFORM = ( 1 << 11 )
LOAD_MONOCHROME = ( 1 << 12 )
LOAD_LINEAR_DESIGN = ( 1 << 13 )
LOAD_NO_AUTOHINT = ( 1 << 15 )
# Bits 16..19 are used by `FT_LOAD_TARGET_'
LOAD_COLOR = ( 1 << 20 )
# extra load flag for FT_Get_Advance and FT_Get_Advances functions
ADVANCE_FLAG_FAST_ONLY = 0x20000000
SUBGLYPH_FLAG_ARGS_ARE_WORDS = 1
SUBGLYPH_FLAG_ARGS_ARE_XY_VALUES = 2
SUBGLYPH_FLAG_ROUND_XY_TO_GRID = 4
SUBGLYPH_FLAG_SCALE = 8
SUBGLYPH_FLAG_XY_SCALE = 0x40
SUBGLYPH_FLAG_2X2 = 0x80
SUBGLYPH_FLAG_USE_MY_METRICS = 0x200
# FSType flags
FSTYPE_INSTALLABLE_EMBEDDING = 0x0000
FSTYPE_RESTRICTED_LICENSE_EMBEDDING = 0x0002
FSTYPE_PREVIEW_AND_PRINT_EMBEDDING = 0x0004
FSTYPE_EDITABLE_EMBEDDING = 0x0008
FSTYPE_NO_SUBSETTING = 0x0100
FSTYPE_BITMAP_EMBEDDING_ONLY = 0x0200
Render_Mode = ct.c_uint
# values for Render_Mode
RENDER_MODE_NORMAL = 0
RENDER_MODE_LIGHT = 1
RENDER_MODE_MONO = 2
RENDER_MODE_LCD = 3
RENDER_MODE_LCD_V = 4
RENDER_MODE_MAX = 5
class Matrix(ct.Structure) :
pass
Matrix._fields_ = \
[
("xx", Fixed),
("xy", Fixed),
("yx", Fixed),
("yy", Fixed),
]
#end Matrix
Glyph_BBox_Mode = ct.c_uint
# values for Glyph_BBox_Mode
GLYPH_BBOX_UNSCALED = 0
GLYPH_BBOX_SUBPIXELS = 0
GLYPH_BBOX_GRIDFIT = 1
GLYPH_BBOX_TRUNCATE = 2
GLYPH_BBOX_PIXELS = 3
class GlyphRec(ct.Structure) :
pass
GlyphRec._fields_ = \
[
("library", Library),
("clazz", ct.c_void_p), # const FT_Glyph_Class*
("format", Glyph_Format),
("advance", Vector),
]
#end GlyphRec
Glyph = ct.POINTER(GlyphRec)
class BitmapGlyphRec(ct.Structure) :
pass
BitmapGlyphRec._fields_ = \
[
("root", GlyphRec),
("left", ct.c_int),
("top", ct.c_int),
("bitmap", Bitmap),
]
#end BitmapGlyphRec
BitmapGlyph = ct.POINTER(BitmapGlyphRec)
class OutlineGlyphRec(ct.Structure) :
pass
OutlineGlyphRec._fields_ = \
[
("root", GlyphRec),
("outline", Outline),
]
#end OutlineGlyphRec
OutlineGlyph = ct.POINTER(OutlineGlyphRec)
Outline_MoveToFunc = ct.CFUNCTYPE(ct.c_int, ct.POINTER(Vector), ct.c_void_p)
Outline_LineToFunc = ct.CFUNCTYPE(ct.c_int, ct.POINTER(Vector), ct.c_void_p)
Outline_ConicToFunc = ct.CFUNCTYPE(ct.c_int, ct.POINTER(Vector), ct.POINTER(Vector), ct.c_void_p)
Outline_CubicToFunc = ct.CFUNCTYPE(ct.c_int, ct.POINTER(Vector), ct.POINTER(Vector), ct.POINTER(Vector), ct.c_void_p)
class Outline_Funcs(ct.Structure) :
pass
Outline_Funcs._fields_ = \
[
("move_to", Outline_MoveToFunc),
("line_to", Outline_LineToFunc),
("conic_to", Outline_ConicToFunc),
("cubic_to", Outline_CubicToFunc),
("shift", ct.c_int),
("delta", Pos),
]
#end Outline_Funcs
class MM_Axis(ct.Structure) :
_fields_ = \
[
("name", ct.c_char_p),
("minimum", ct.c_long),
("maximum", ct.c_long),
]
#end MM_Axis
T1_MAX_MM_AXIS = 16
class Multi_Master(ct.Structure) :
pass
Multi_Master._fields_ = \
[
("num_axis", ct.c_uint),
("num_designs", ct.c_uint), # normally 2 * T1_MAX_MM_AXIS
("axis", T1_MAX_MM_AXIS * MM_Axis),
]
#end Multi_Master
class Var_Axis(ct.Structure) :
pass
Var_Axis._fields_ = \
[
("name", ct.c_char_p),
("minimum", Fixed),
("default", Fixed), # actually “def” in libfreetype
("maximum", Fixed),
("tag", ct.c_ulong),
("strid", ct.c_uint),
]
#end Var_Axis
Var_Axis_ptr = ct.POINTER(Var_Axis)
class Var_Named_Style(ct.Structure) :
pass
Var_Named_Style._fields_ = \
[
("coords", Fixed_ptr), # pointer to array with one entry per axis
("strid", ct.c_uint), # ID of name for style
]
#end Var_Named_Style
Var_Named_Style_ptr = ct.POINTER(Var_Named_Style)
class MM_Var(ct.Structure) :
pass
MM_Var._fields_ = \
[
("num_axis", ct.c_uint),
("num_designs", ct.c_uint), # not meaningful for GX
("num_namedstyles", ct.c_int), # should be c_uint, but I can get -1 if invalid
("axis", Var_Axis_ptr), # array of axis descriptors
("namedstyle", Var_Named_Style_ptr), # array of named styles
]
#end MM_Var
MM_Var_ptr = ct.POINTER(MM_Var)
# bits returned by FT_Get_Gasp
GASP_NO_TABLE = -1
GASP_DO_GRIDFIT = 0x01
GASP_DO_GRAY = 0x02
GASP_SYMMETRIC_SMOOTHING = 0x08
GASP_SYMMETRIC_GRIDFIT = 0x10
# FT_Sfnt_Tag enum
SFNT_HEAD = 0
SFNT_MAXP = 1
SFNT_OS2 = 2
SFNT_HHEA = 3
SFNT_VHEA = 4
SFNT_POST = 5
SFNT_PCLT = 6
# TrueType table structs
# <http://freetype.org/freetype2/docs/reference/ft2-truetype_tables.html>
class TT_Header(ct.Structure) :
pass
TT_Header._fields_ = \
[
("Table_Version", Fixed),
("Font_Revision", Fixed),
("CheckSum_Adjust", ct.c_int),
("Magic_Number", ct.c_int),
("Flags", ct.c_ushort),
("Units_Per_EM", ct.c_ushort),
("Created", ct.c_int * 2),
("Modified", ct.c_int * 2),
("xMin", ct.c_short),
("yMin", ct.c_short),
("xMax", ct.c_short),
("yMax", ct.c_short),
("Mac_Style", ct.c_ushort),
("Lowest_Rec_PPEM", ct.c_ushort),
("Font_Direction", ct.c_short),
("Index_To_Loc_Format", ct.c_short),
("Glyph_Data_Format", ct.c_short),
]
#end TT_Header
class TT_HoriHeader(ct.Structure) :
pass
TT_HoriHeader._fields_ = \
[
("Version", Fixed),
("Ascender", ct.c_short),
("Descender", ct.c_short),
("Line_Gap", ct.c_short),
("advance_Width_Max", ct.c_ushort),
("min_Left_Side_Bearing", ct.c_short),
("min_Right_Side_Bearing", ct.c_short),
("xMax_Extent", ct.c_short),
("caret_Slope_Rise", ct.c_short),
("caret_Slope_Run", ct.c_short),
("caret_Offset", ct.c_short),
("Reserved", ct.c_short * 4),
("metric_Data_Format", ct.c_short),
("number_Of_HMetrics", ct.c_ushort),
# The following fields are not defined by the TrueType specification
# but they are used to connect the metrics header to the relevant
# `HMTX' table.
("long_metrics", ct.c_void_p),
("short_metrics", ct.c_void_p),
]
#end TT_HoriHeader
class TT_VertHeader(ct.Structure) :
pass
TT_VertHeader._fields_ = \
[
("Version", Fixed),
("Ascender", ct.c_short),
("Descender", ct.c_short),
("Line_Gap", ct.c_short),
("advance_Height_Max", ct.c_ushort),
("min_Top_Side_Bearing", ct.c_short),
("min_Bottom_Side_Bearing", ct.c_short),
("yMax_Extent", ct.c_short),
("caret_Slope_Rise", ct.c_short),
("caret_Slope_Run", ct.c_short),
("caret_Offset", ct.c_short),
("Reserved", ct.c_short * 4),
("metric_Data_Format", ct.c_short),
("number_Of_VMetrics", ct.c_short),
# The following fields are not defined by the TrueType specification
# but they're used to connect the metrics header to the relevant
# `HMTX' or `VMTX' table.
("long_metrics", ct.c_void_p),
("short_metrics", ct.c_void_p),
]
#end TT_VertHeader
class TT_OS2(ct.Structure) :
pass
TT_OS2._fields_ = \
[
("version", ct.c_ushort),
("xAvgCharWidth", ct.c_short),
("usWeightClass", ct.c_ushort),
("usWidthClass", ct.c_ushort),
("fsType", ct.c_ushort),
("ySubscriptXSize", ct.c_short),
("ySubscriptYSize", ct.c_short),
("ySubscriptXOffset", ct.c_short),
("ySubscriptYOffset", ct.c_short),
("ySuperscriptXSize", ct.c_short),
("ySuperscriptYSize", ct.c_short),
("ySuperscriptXOffset", ct.c_short),
("ySuperscriptYOffset", ct.c_short),
("yStrikeoutSize", ct.c_short),
("yStrikeoutPosition", ct.c_short),
("sFamilyClass", ct.c_short),
("panose", ct.c_byte * 10),
("ulUnicodeRange1", ct.c_uint), # Bits 0-31
("ulUnicodeRange2", ct.c_uint), # Bits 32-63
("ulUnicodeRange3", ct.c_uint), # Bits 64-95
("ulUnicodeRange4", ct.c_uint), # Bits 96-127
("achVendID", ct.c_char * 4),
("fsSelection", ct.c_ushort),
("usFirstCharIndex", ct.c_ushort),
("usLastCharIndex", ct.c_ushort),
("sTypoAscender", ct.c_short),
("sTypoDescender", ct.c_short),
("sTypoLineGap", ct.c_short),
("usWinAscent", ct.c_ushort),
("usWinDescent", ct.c_ushort),
# only version 1 and higher:
("ulCodePageRange1", ct.c_uint), # Bits 0-31
("ulCodePageRange2", ct.c_uint), # Bits 32-63
# only version 2 and higher:
("sxHeight", ct.c_short),
("sCapHeight", ct.c_short),
("usDefaultChar", ct.c_ushort),
("usBreakChar", ct.c_ushort),
("usMaxContext", ct.c_ushort),
# only version 5 and higher:
("usLowerOpticalPointSize", ct.c_ushort), # in twips (1/20th points)
("usUpperOpticalPointSize", ct.c_ushort), # in twips (1/20th points)
]
#end TT_OS2
class TT_Postscript(ct.Structure) :
pass
TT_Postscript._fields_ = \
[
("FormatType", Fixed),
("italicAngle", Fixed),
("underlinePosition", ct.c_short),
("underlineThickness", ct.c_short),
("isFixedPitch", ct.c_uint),
("minMemType42", ct.c_uint),
("maxMemType42", ct.c_uint),
("minMemType1", ct.c_uint),
("maxMemType1", ct.c_uint),
# Glyph names follow in the file, but we don't
# load them by default. See the ttpost.c file.
]
#end TT_Postscript
class TT_PCLT(ct.Structure) :
pass
TT_PCLT._fields_ = \
[
("Version", Fixed),
("FontNumber", ct.c_uint),
("Pitch", ct.c_ushort),
("xHeight", ct.c_ushort),
("Style", ct.c_ushort),
("TypeFamily", ct.c_ushort),
("CapHeight", ct.c_ushort),
("SymbolSet", ct.c_ushort),
("TypeFace", ct.c_char * 16),
("CharacterComplement", ct.c_char * 8),
("FileName", ct.c_char * 6),
("StrokeWeight", ct.c_char),
("WidthType", ct.c_char),
("SerifStyle", ct.c_byte),
("Reserved", ct.c_byte),
]
#end TT_PCLT
class TT_MaxProfile(ct.Structure) :
pass
TT_MaxProfile._fields_ = \
[
("version", Fixed),
("numGlyphs", ct.c_ushort),
("maxPoints", ct.c_ushort),
("maxContours", ct.c_ushort),
("maxCompositePoints", ct.c_ushort),
("maxCompositeContours", ct.c_ushort),
("maxZones", ct.c_ushort),
("maxTwilightPoints", ct.c_ushort),
("maxStorage", ct.c_ushort),
("maxFunctionDefs", ct.c_ushort),
("maxInstructionDefs", ct.c_ushort),
("maxStackElements", ct.c_ushort),
("maxSizeOfInstructions", ct.c_ushort),
("maxComponentElements", ct.c_ushort),
("maxComponentDepth", ct.c_ushort),
]
#end TT_MaxProfile
class SfntName(ct.Structure) :
pass
SfntName._fields_ = \
[
("platform_id", ct.c_ushort),
("encoding_id", ct.c_ushort),
("language_id", ct.c_ushort),
("name_id", ct.c_ushort),
("string", c_ubyte_ptr), # *not* null-terminated
("string_len", ct.c_uint),
]
#end SfntName
Orientation = ct.c_uint
ORIENTATION_TRUETYPE = 0
ORIENTATION_POSTSCRIPT = 1
ORIENTATION_FILL_RIGHT = ORIENTATION_TRUETYPE
ORIENTATION_FILL_LEFT = ORIENTATION_POSTSCRIPT
ORIENTATION_NONE = 2
Stroker_LineJoin = ct.c_uint
STROKER_LINEJOIN_ROUND = 0
STROKER_LINEJOIN_BEVEL = 1
STROKER_LINEJOIN_MITER_VARIABLE = 2
STROKER_LINEJOIN_MITER = STROKER_LINEJOIN_MITER_VARIABLE
STROKER_LINEJOIN_MITER_FIXED = 3
Stroker_LineCap = ct.c_uint
STROKER_LINECAP_BUTT = 0
STROKER_LINECAP_ROUND = 1
STROKER_LINECAP_SQUARE = 2
StrokerBorder = ct.c_uint
STROKER_BORDER_LEFT = 0
STROKER_BORDER_RIGHT = 1
class RasterRec(ct.Structure) :
pass # private
#end RasterRec
Raster = ct.POINTER(RasterRec)
class Span(ct.Structure) :
_fields_ = \
[
("x", ct.c_short),
("len", ct.c_ushort),
("coverage", ct.c_ubyte),
]
#end Span
SpanPtr = ct.POINTER(Span)
SpanFunc = ct.CFUNCTYPE(None, ct.c_int, ct.c_int, SpanPtr, ct.c_void_p)
Raster_BitTest_Func = ct.CFUNCTYPE(ct.c_int, ct.c_int, ct.c_int, ct.c_void_p)
Raster_BitSet_Func = ct.CFUNCTYPE(None, ct.c_int, ct.c_int, ct.c_void_p)
class Raster_Params(ct.Structure) :
pass
#end Raster_Params
Raster_Params._fields_ = \
[
("target", BitmapPtr),
("source", ct.c_void_p),
("flags", ct.c_int),
("gray_spans", SpanFunc),
("black_spans", SpanFunc), # unused
("bit_test", Raster_BitTest_Func), # unused
("bit_set", Raster_BitSet_Func), # unused
("user", ct.c_void_p),
("clip_box", BBox),
]
Raster_ParamsPtr = ct.POINTER(Raster_Params)
# bit masks for Raster_Params.flags
RASTER_FLAG_DEFAULT = 0x0
RASTER_FLAG_AA = 0x1
RASTER_FLAG_DIRECT = 0x2
RASTER_FLAG_CLIP = 0x4
Raster_NewFunc = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, Raster)
Raster_DoneFunc = ct.CFUNCTYPE(None, Raster)
Raster_ResetFunc = ct.CFUNCTYPE(None, Raster, c_ubyte_ptr, ct.c_ulong)
Raster_SetModeFunc = ct.CFUNCTYPE(ct.c_int, Raster, ct.c_ulong, ct.c_void_p)
Raster_RenderFunc = ct.CFUNCTYPE(ct.c_int, Raster, Raster_ParamsPtr)
class Raster_Funcs(ct.Structure) :
pass
#end Raster_Funcs
Raster_Funcs._fields_ = \
[
("glyph_format", Glyph_Format),
("raster_new", Raster_NewFunc),
("raster_reset", Raster_ResetFunc),
("raster_set_mode", Raster_SetModeFunc),
("raster_render", Raster_RenderFunc),
("raster_done", Raster_DoneFunc),
]
# codes for FT_TrueTypeEngineType
TRUETYPE_ENGINE_TYPE_NONE = 0
TRUETYPE_ENGINE_TYPE_UNPATENTED = 1
TRUETYPE_ENGINE_TYPE_PATENTED = 2