-
Notifications
You must be signed in to change notification settings - Fork 0
/
qahirah.py
7953 lines (7024 loc) · 280 KB
/
qahirah.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 the Cairo graphics library <http://cairographics.org/>
using ctypes. This is modelled on Pycairo, but differs from that in
some important ways. It also operates at a higher level than the underlying
Cairo API where this makes sense. For example, it defines a “Vector”
class for representing a 2D point, with operations on the Vector as a
whole, rather than having to operate on individual x- and y-coordinates.
Also, get/set API calls are collapsed into read/write Python properties
where this makes sense; for example, instead of Context.get_line_width()
and Context.set_line_width() calls, there is a Context.line_width property
that may be read and written.
"""
#+
# Copyright 2015-2020 Lawrence D'Oliveiro <[email protected]>.
# Licensed under the GNU Lesser General Public License v2.1 or later.
#-
import sys
import math
from numbers import \
Real, \
Complex
from collections import \
namedtuple
import io
import colorsys
import array
import ctypes as ct
from weakref import \
WeakKeyDictionary, \
WeakValueDictionary
import atexit
try :
import fontconfig
# my Fontconfig wrapper, get from <https://github.com/ldo/python_fontconfig>
except ImportError :
fontconfig = None
#end try
try :
import freetype2
# my FreeType wrapper, get from <https://github.com/ldo/python_freetype>
except ImportError :
freetype2 = None
#end try
LIBNAME = \
{
"linux" :
{
"cairo" : "libcairo.so.2",
"freetype" : "libfreetype.so.6",
"fontconfig" : "libfontconfig.so.1",
},
"openbsd6" :
{
"cairo" : "libcairo.so.12",
"freetype" : "libfreetype.so.28",
"fontconfig" : "libfontconfig.so.11",
},
"darwin" :
{
"cairo" : "libcairo.2.dylib",
"freetype" : "libfreetype.6.dylib",
"fontconfig" : "libfontconfig.1.dylib",
},
"win32" :
{
"cairo" : "libcairo-2.dll",
"freetype" : "libfreetype-6.dll",
"fontconfig" : "libfontconfig-1.dll",
},
}[sys.platform]
cairo = ct.cdll.LoadLibrary(LIBNAME["cairo"])
if freetype2 == None :
_ft = ct.cdll.LoadLibrary(LIBNAME["freetype"])
#end if
if fontconfig == None :
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
#end if
class CAIRO :
"useful definitions adapted from cairo.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.
status_t = ct.c_uint
c_ptr_p = ct.POINTER(ct.c_void_p)
c_ubyte_p = ct.POINTER(ct.c_ubyte)
c_int_p = ct.POINTER(ct.c_int)
c_uint_p = ct.POINTER(ct.c_uint)
c_ulong_p = ct.POINTER(ct.c_ulong)
# cairo_status_t codes
STATUS_SUCCESS = 0
STATUS_NO_MEMORY = 1
STATUS_INVALID_RESTORE = 2
STATUS_INVALID_POP_GROUP = 3
STATUS_NO_CURRENT_POINT = 4
STATUS_INVALID_MATRIX = 5
STATUS_INVALID_STATUS = 6
STATUS_NULL_POINTER = 7
STATUS_INVALID_STRING = 8
STATUS_INVALID_PATH_DATA = 9
STATUS_READ_ERROR = 10
STATUS_WRITE_ERROR = 11
STATUS_SURFACE_FINISHED = 12
STATUS_SURFACE_TYPE_MISMATCH = 13
STATUS_PATTERN_TYPE_MISMATCH = 14
STATUS_INVALID_CONTENT = 15
STATUS_INVALID_FORMAT = 16
STATUS_INVALID_VISUAL = 17
STATUS_FILE_NOT_FOUND = 18
STATUS_INVALID_DASH = 19
STATUS_INVALID_DSC_COMMENT = 20
STATUS_INVALID_INDEX = 21
STATUS_CLIP_NOT_REPRESENTABLE = 22
STATUS_TEMP_FILE_ERROR = 23
STATUS_INVALID_STRIDE = 24
STATUS_FONT_TYPE_MISMATCH = 25
STATUS_USER_FONT_IMMUTABLE = 26
STATUS_USER_FONT_ERROR = 27
STATUS_NEGATIVE_COUNT = 28
STATUS_INVALID_CLUSTERS = 29
STATUS_INVALID_SLANT = 30
STATUS_INVALID_WEIGHT = 31
STATUS_INVALID_SIZE = 32
STATUS_USER_FONT_NOT_IMPLEMENTED = 33
STATUS_DEVICE_TYPE_MISMATCH = 34
STATUS_DEVICE_ERROR = 35
STATUS_INVALID_MESH_CONSTRUCTION = 36
STATUS_DEVICE_FINISHED = 37
STATUS_JBIG2_GLOBAL_MISSING = 38
STATUS_PNG_ERROR = 39
STATUS_FREETYPE_ERROR = 40
STATUS_WIN32_GDI_ERROR = 41
STATUS_TAG_ERROR = 42
STATUS_LAST_STATUS = 43
# codes for cairo_surface_type_t
SURFACE_TYPE_IMAGE = 0
SURFACE_TYPE_PDF = 1
SURFACE_TYPE_PS = 2
SURFACE_TYPE_XLIB = 3
SURFACE_TYPE_XCB = 4
SURFACE_TYPE_GLITZ = 5
SURFACE_TYPE_QUARTZ = 6
SURFACE_TYPE_WIN32 = 7
SURFACE_TYPE_BEOS = 8
SURFACE_TYPE_DIRECTFB = 9
SURFACE_TYPE_SVG = 10
SURFACE_TYPE_OS2 = 11
SURFACE_TYPE_WIN32_PRINTING = 12
SURFACE_TYPE_QUARTZ_IMAGE = 13
SURFACE_TYPE_SCRIPT = 14
SURFACE_TYPE_QT = 15
SURFACE_TYPE_RECORDING = 16
SURFACE_TYPE_VG = 17
SURFACE_TYPE_GL = 18
SURFACE_TYPE_DRM = 19
SURFACE_TYPE_TEE = 20
SURFACE_TYPE_XML = 21
SURFACE_TYPE_SKIA = 22
SURFACE_TYPE_SUBSURFACE = 23
SURFACE_TYPE_COGL = 24
# cairo_format_t codes
FORMAT_INVALID = -1
FORMAT_ARGB32 = 0
FORMAT_RGB24 = 1
FORMAT_A8 = 2
FORMAT_A1 = 3
FORMAT_RGB16_565 = 4
FORMAT_RGB30 = 5
# cairo_content_t codes
CONTENT_COLOR = 0x1000
CONTENT_COLOUR = 0x1000
CONTENT_ALPHA = 0x2000
CONTENT_COLOR_ALPHA = 0x3000
CONTENT_COLOUR_ALPHA = 0x3000
# cairo_extend_t codes
EXTEND_NONE = 0
EXTEND_REPEAT = 1
EXTEND_REFLECT = 2
EXTEND_PAD = 3
# cairo_filter_t codes
FILTER_FAST = 0
FILTER_GOOD = 1
FILTER_BEST = 2
FILTER_NEAREST = 3
FILTER_BILINEAR = 4
FILTER_GAUSSIAN = 5
# cairo_operator_t codes
OPERATOR_CLEAR = 0
OPERATOR_SOURCE = 1
OPERATOR_OVER = 2
OPERATOR_IN = 3
OPERATOR_OUT = 4
OPERATOR_ATOP = 5
OPERATOR_DEST = 6
OPERATOR_DEST_OVER = 7
OPERATOR_DEST_IN = 8
OPERATOR_DEST_OUT = 9
OPERATOR_DEST_ATOP = 10
OPERATOR_XOR = 11
OPERATOR_ADD = 12
OPERATOR_SATURATE = 13
OPERATOR_MULTIPLY = 14
OPERATOR_SCREEN = 15
OPERATOR_OVERLAY = 16
OPERATOR_DARKEN = 17
OPERATOR_LIGHTEN = 18
OPERATOR_COLOR_DODGE = 19
OPERATOR_COLOUR_DODGE = 19
OPERATOR_COLOR_BURN = 20
OPERATOR_COLOUR_BURN = 20
OPERATOR_HARD_LIGHT = 21
OPERATOR_SOFT_LIGHT = 22
OPERATOR_DIFFERENCE = 23
OPERATOR_EXCLUSION = 24
OPERATOR_HSL_HUE = 25
OPERATOR_HSL_SATURATION = 26
OPERATOR_HSL_COLOR = 27
OPERATOR_HSL_COLOUR = 27
OPERATOR_HSL_LUMINOSITY = 28
class matrix_t(ct.Structure) :
_fields_ = \
[
("xx", ct.c_double),
("yx", ct.c_double),
("xy", ct.c_double),
("yy", ct.c_double),
("x0", ct.c_double),
("y0", ct.c_double),
]
#end matrix_t
# cairo_antialias_t codes
ANTIALIAS_DEFAULT = 0
# method
ANTIALIAS_NONE = 1
ANTIALIAS_GRAY = 2
ANTIALIAS_SUBPIXEL = 3
# hints
ANTIALIAS_FAST = 4
ANTIALIAS_GOOD = 5
ANTIALIAS_BEST = 6
# cairo_subpixel_order_t codes
SUBPIXEL_ORDER_DEFAULT = 0
SUBPIXEL_ORDER_RGB = 1
SUBPIXEL_ORDER_BGR = 2
SUBPIXEL_ORDER_VRGB = 3
SUBPIXEL_ORDER_VBGR = 4
# cairo_hint_style_t codes
HINT_STYLE_DEFAULT = 0
HINT_STYLE_NONE = 1
HINT_STYLE_SLIGHT = 2
HINT_STYLE_MEDIUM = 3
HINT_STYLE_FULL = 4
# cairo_hint_metrics_t codes
HINT_METRICS_DEFAULT = 0
HINT_METRICS_OFF = 1
HINT_METRICS_ON = 2
# cairo_fill_rule_t codes
FILL_RULE_WINDING = 0
FILL_RULE_EVEN_ODD = 1
# cairo_line_cap_t codes
LINE_CAP_BUTT = 0
LINE_CAP_ROUND = 1
LINE_CAP_SQUARE = 2
# cairo_line_join_t codes
LINE_JOIN_MITER = 0
LINE_JOIN_MITRE = 0
LINE_JOIN_ROUND = 1
LINE_JOIN_BEVEL = 2
# cairo_path_data_type_t codes
PATH_MOVE_TO = 0
PATH_LINE_TO = 1
PATH_CURVE_TO = 2
PATH_CLOSE_PATH = 3
path_data_type_t = ct.c_uint
class path_data_t(ct.Union) :
class header_t(ct.Structure) :
"followed by header_t.length point_t structs."
_fields_ = \
[
("type", ct.c_uint), # path_data_type_t
("length", ct.c_int), # number of following points
]
#end header_t
header_ptr_t = ct.POINTER(header_t)
class point_t(ct.Structure) :
_fields_ = \
[
("x", ct.c_double),
("y", ct.c_double),
]
#end point_t
point_ptr_t = ct.POINTER(point_t)
_fields_ = \
[
("header", header_t),
("point" , point_t),
]
#end path_data_t
path_data_t_ptr = ct.POINTER(path_data_t)
class path_t(ct.Structure) :
pass
path_t._fields_ = \
[
("status", status_t),
("data", ct.c_void_p), # path_data_t_ptr
("num_data", ct.c_int), # number of elements in data
]
#end path_t
path_ptr_t = ct.POINTER(path_t)
class rectangle_t(ct.Structure) :
_fields_ = \
[
("x", ct.c_double),
("y", ct.c_double),
("width", ct.c_double),
("height", ct.c_double),
]
#end rectangle_t
rectangle_ptr_t = ct.POINTER(rectangle_t)
class rectangle_list_t(ct.Structure) :
pass
rectangle_list_t._fields_ = \
[
("status", status_t),
("rectangles", rectangle_ptr_t),
("num_rectangles", ct.c_int),
]
#end rectangle_list_t
rectangle_list_ptr_t = ct.POINTER(rectangle_list_t)
class rectangle_int_t(ct.Structure) :
_fields_ = \
[
("x", ct.c_int),
("y", ct.c_int),
("width", ct.c_int),
("height", ct.c_int),
]
#end rectangle_int_t
rectangle_int_ptr_t = ct.POINTER(rectangle_int_t)
# codes for cairo_region_overlap_t
REGION_OVERLAP_IN = 0
REGION_OVERLAP_OUT = 1
REGION_OVERLAP_PART = 2
# since 1.16, for tag_begin/end
TAG_DEST = "cairo.dest"
TAG_LINK = "Link"
class glyph_t(ct.Structure) :
_fields_ = \
[
("index", ct.c_ulong), # glyph index
("x", ct.c_double), # position relative to origin
("y", ct.c_double),
]
#end glyph_t
glyph_ptr_t = ct.POINTER(glyph_t)
class cluster_t(ct.Structure) :
_fields_ = \
[
("num_bytes", ct.c_int),
("num_glyphs", ct.c_int),
]
#end cluster_t
cluster_ptr_t = ct.POINTER(cluster_t)
# cairo_text_cluster_flags_t codes
TEXT_CLUSTER_FLAG_BACKWARD = 0x00000001
# cairo_font_type_t codes
FONT_TYPE_TOY = 0
FONT_TYPE_FT = 1
FONT_TYPE_WIN32 = 2
FONT_TYPE_QUARTZ = 3
FONT_TYPE_USER = 4
destroy_func_t = ct.CFUNCTYPE(None, ct.c_void_p)
class font_extents_t(ct.Structure) :
_fields_ = \
[
("ascent", ct.c_double),
("descent", ct.c_double),
("height", ct.c_double),
("max_x_advance", ct.c_double),
("max_y_advance", ct.c_double),
]
#end font_extents_t
font_extents_ptr_t = ct.POINTER(font_extents_t)
class text_extents_t(ct.Structure) :
_fields_ = \
[
("x_bearing", ct.c_double),
("y_bearing", ct.c_double),
("width", ct.c_double),
("height", ct.c_double),
("x_advance", ct.c_double),
("y_advance", ct.c_double),
]
#end text_extents_t
text_extents_ptr_t = ct.POINTER(text_extents_t)
user_scaled_font_init_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_void_p, font_extents_ptr_t)
user_scaled_font_render_glyph_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_ulong, ct.c_void_p, text_extents_ptr_t)
user_scaled_font_text_to_glyphs_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, c_ubyte_p, ct.c_int, c_ptr_p, c_int_p, c_ptr_p, c_int_p, c_uint_p)
user_scaled_font_unicode_to_glyph_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_ulong, c_ulong_p)
# codes for cairo_font_slant_t
FONT_SLANT_NORMAL = 0
FONT_SLANT_ITALIC = 1
FONT_SLANT_OBLIQUE = 2
# codes for cairo_font_weight_t
FONT_WEIGHT_NORMAL = 0
FONT_WEIGHT_BOLD = 1
# bit masks for cairo_ft_synthesize_t
FT_SYNTHESIZE_BOLD = 1 << 0
FT_SYNTHESIZE_OBLIQUE = 1 << 1
read_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_void_p, ct.c_uint)
write_func_t = ct.CFUNCTYPE(ct.c_int, ct.c_void_p, ct.c_void_p, ct.c_uint)
cairo_pdf_version_t = ct.c_uint
# codes for cairo_pdf_version_t
PDF_VERSION_1_4 = 0
PDF_VERSION_1_5 = 1
pdf_outline_flags_t = ct.c_uint # since 1.16
# flags for pdf_outline_flags_t
PDF_OUTLINE_FLAG_OPEN = 0x1
PDF_OUTLINE_FLAG_BOLD = 0x2
PDF_OUTLINE_FLAG_ITALIC = 0x4
PDF_OUTLINE_ROOT = 0
pdf_metadata_t = ct.c_uint # since 1.16
PDF_METADATA_TITLE = 0
PDF_METADATA_AUTHOR = 1
PDF_METADATA_SUBJECT = 2
PDF_METADATA_KEYWORDS = 3
PDF_METADATA_CREATOR = 4
PDF_METADATA_CREATE_DATE = 5
PDF_METADATA_MOD_DATE = 6
# cairo_ps_level_t
PS_LEVEL_2 = 0
PS_LEVEL_3 = 1
# codes for cairo_svg_version_t
SVG_VERSION_1_1 = 0
SVG_VERSION_1_2 = 1
svg_unit_t = ct.c_uint # since 1.16
# values for svg_unit_t
SVG_UNIT_USER = 0
SVG_UNIT_EM = 1
SVG_UNIT_EX = 2
SVG_UNIT_PX = 3
SVG_UNIT_IN = 4
SVG_UNIT_CM = 5
SVG_UNIT_MM = 6
SVG_UNIT_PT = 7
SVG_UNIT_PC = 8
SVG_UNIT_PERCENT = 9
# codes for cairo_device_type_t
DEVICE_TYPE_DRM = 0
DEVICE_TYPE_GL = 1
DEVICE_TYPE_SCRIPT = 2
DEVICE_TYPE_XCB = 3
DEVICE_TYPE_XLIB = 4
DEVICE_TYPE_XML = 5
DEVICE_TYPE_COGL = 6
DEVICE_TYPE_WIN32 = 7
DEVICE_TYPE_INVALID = -1
# codes for cairo_script_mode_t
SCRIPT_MODE_ASCII = 0
SCRIPT_MODE_BINARY = 1
#end CAIRO
class XCB :
"minimal needed XCB-related definitions."
# from xproto.h:
window_t = ct.c_uint
pixmap_t = ct.c_uint
visualid_t = ct.c_uint
drawable_t = ct.c_uint
colormap_t = ct.c_uint
visual_class_t = ct.c_uint
# values for visual_class_t:
VISUAL_CLASS_STATIC_GRAY = 0
VISUAL_CLASS_GRAY_SCALE = 1
VISUAL_CLASS_STATIC_COLOR = 2
VISUAL_CLASS_PSEUDO_COLOR = 3
VISUAL_CLASS_TRUE_COLOR = 4
VISUAL_CLASS_DIRECT_COLOR = 5
class visualtype_t(ct.Structure) :
pass
visualtype_t._fields_ = \
[
("visual_id", visualid_t),
("_class", ct.c_ubyte),
("bits_per_rgb_value", ct.c_ubyte),
("colormap_entries", ct.c_ushort),
("red_mask", ct.c_uint),
("green_mask", ct.c_uint),
("blue_mask", ct.c_uint),
("pad0", ct.c_ubyte * 4),
]
#end visualtype_t
visualtype_ptr_t = ct.POINTER(visualtype_t)
class screen_t(ct.Structure) :
pass
screen_t._fields_ = \
[
("root", window_t),
("default_colormap", colormap_t),
("white_pixel", ct.c_uint),
("black_pixel", ct.c_uint),
("current_input_masks", ct.c_uint),
("width_in_pixels", ct.c_ushort),
("height_in_pixels", ct.c_ushort),
("width_in_millimeters", ct.c_ushort),
("height_in_millimeters", ct.c_ushort),
("min_installed_maps", ct.c_ushort),
("max_installed_maps", ct.c_ushort),
("root_visual", visualid_t),
("backing_stores", ct.c_ubyte),
("save_unders", ct.c_ubyte),
("root_depth", ct.c_ubyte),
("allowed_depths_len", ct.c_ubyte),
]
#end screen_t
screen_ptr_t = ct.POINTER(screen_t)
# from xrender.h:
render_pictformat_t = ct.c_uint
class render_directformat_t(ct.Structure) :
_fields_ = \
[
("red_shift", ct.c_short),
("red_mask", ct.c_short),
("green_shift", ct.c_short),
("green_mask", ct.c_short),
("blue_shift", ct.c_short),
("blue_mask", ct.c_short),
("alpha_shift", ct.c_short),
("alpha_mask", ct.c_short),
]
#end render_directformat_t
# values for render_pictforminfo_t.type
RENDER_PICT_TYPE_INDEXED = 0
RENDER_PICT_TYPE_DIRECT = 1
class render_pictforminfo_t(ct.Structure) :
pass
render_pictforminfo_t._fields_ = \
[
("id", render_pictformat_t),
("type", ct.c_ubyte),
("depth", ct.c_ubyte),
("pad0", ct.c_ubyte * 2),
("direct", render_directformat_t),
("colormap", colormap_t),
]
#end render_pictforminfo_t
render_pictforminfo_ptr_t = ct.POINTER(render_pictforminfo_t)
#end XCB
class XLIB :
"minimal needed Xlib-related definitions."
XID = ct.c_uint
Colormap = XID
Drawable = XID
PictFormat = XID
# values for PictFormat
PictFormatID = (1 << 0)
PictFormatType = (1 << 1)
PictFormatDepth = (1 << 2)
PictFormatRed = (1 << 3)
PictFormatRedMask = (1 << 4)
PictFormatGreen = (1 << 5)
PictFormatGreenMask = (1 << 6)
PictFormatBlue = (1 << 7)
PictFormatBlueMask = (1 << 8)
PictFormatAlpha = (1 << 9)
PictFormatAlphaMask = (1 << 10)
PictFormatColormap = (1 << 11)
class XRenderDirectFormat(ct.Structure) :
_fields_ = \
[
("red", ct.c_ushort),
("redMask", ct.c_ushort),
("green", ct.c_ushort),
("greenMask", ct.c_ushort),
("blue", ct.c_ushort),
("blueMask", ct.c_ushort),
("alpha", ct.c_ushort),
("alphaMask", ct.c_ushort),
]
#end XRenderDirectFormat
class XRenderPictFormat(ct.Structure) :
pass
#end XRenderPictFormat
XRenderPictFormat._fields_ = \
[
("id", PictFormat),
("type", ct.c_int),
("depth", ct.c_int),
("direct", XRenderDirectFormat),
("colormap", Colormap),
]
#end XLIB
class HAS :
"functionality queries. These are implemented by checking for the presence" \
" of particular library functions."
ISCLOSE = hasattr(math, "isclose") # introduced in Python 3.5
# rest filled in below
#end HAS
for \
symname, funcname \
in \
(
("FT_FONT", "ft_font_face_create_for_ft_face"),
("FC_FONT", "ft_font_face_create_for_pattern"),
("IMAGE_SURFACE", "image_surface_create"),
# TODO: MIME_SURFACE, OBSERVER_SURFACE?
("PDF_SURFACE", "pdf_surface_create"),
("PNG_FUNCTIONS", "surface_write_to_png"),
("PS_SURFACE", "ps_surface_create"),
("RECORDING_SURFACE", "recording_surface_create"),
("SCRIPT_SURFACE", "script_create"),
("SVG_SURFACE", "svg_surface_create"),
("USER_FONT", "user_font_face_create"),
("XCB_SURFACE", "xcb_surface_create"),
("XCB_SHM_FUNCTIONS", "xcb_device_debug_cap_xshm_version"),
("XLIB_SURFACE", "xlib_surface_create"),
("XLIB_XRENDER", "xlib_surface_create_with_xrender_format"),
) \
:
setattr \
(
HAS,
symname,
hasattr(cairo, "cairo_" + funcname)
)
#end for
del symname, funcname
if HAS.ISCLOSE :
# copy same defaults as math.isclose
default_rel_tol = 1.0e-9
default_abs_tol = 0
#end if
def def_struct_class(name, ctname, extra = None) :
# defines a class with attributes that are a straightforward mapping
# of a ctypes struct. Optionally includes extra members from extra
# if specified.
ctstruct = getattr(CAIRO, ctname)
class result_class :
__slots__ = tuple(field[0] for field in ctstruct._fields_) # to forestall typos
def to_cairo(self) :
"returns a Cairo representation of the structure."
result = ctstruct()
for name, cttype in ctstruct._fields_ :
setattr(result, name, getattr(self, name))
#end for
return \
result
#end to_cairo
@classmethod
def from_cairo(celf, r) :
"decodes the Cairo representation of the structure."
result = celf()
for name, cttype in ctstruct._fields_ :
setattr(result, name, getattr(r, name))
#end for
return \
result
#end from_cairo
def __getitem__(self, i) :
"allows the object to be coerced to a tuple."
return \
getattr(self, ctstruct._fields_[i][0])
#end __getitem__
def __repr__(self) :
return \
(
"%s(%s)"
%
(
name,
", ".join
(
"%s = %s" % (field[0], getattr(self, field[0]))
for field in ctstruct._fields_
),
)
)
#end __repr__
#end result_class
#begin def_struct_class
result_class.__name__ = name
result_class.__doc__ = \
(
"representation of a Cairo %s structure. Fields are %s."
"\nCreate by decoding the Cairo form with the from_cairo method;"
" convert an instance to Cairo form with the to_cairo method."
%
(
ctname,
", ".join(f[0] for f in ctstruct._fields_),
)
)
if extra != None :
for attr in dir(extra) :
if not attr.startswith("__") :
setattr(result_class, attr, getattr(extra, attr))
#end if
#end for
#end if
return \
result_class
#end def_struct_class
#+
# Routine arg/result types
#-
cairo.cairo_version_string.restype = ct.c_char_p
cairo.cairo_status_to_string.restype = ct.c_char_p
cairo.cairo_status.argtypes = (ct.c_void_p,)
cairo.cairo_create.argtypes = (ct.c_void_p,)
cairo.cairo_create.restype = ct.c_void_p
cairo.cairo_reference.restype = ct.c_void_p
cairo.cairo_reference.argtypes = (ct.c_void_p,)
cairo.cairo_destroy.argtypes = (ct.c_void_p,)
cairo.cairo_destroy.restype = ct.c_void_p
cairo.cairo_save.argtypes = (ct.c_void_p,)
cairo.cairo_save.restype = ct.c_void_p
cairo.cairo_restore.argtypes = (ct.c_void_p,)
cairo.cairo_restore.restype = ct.c_void_p
cairo.cairo_get_target.restype = ct.c_void_p
cairo.cairo_get_target.argtypes = (ct.c_void_p,)
cairo.cairo_push_group.argtypes = (ct.c_void_p,)
cairo.cairo_push_group_with_content.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_pop_group.restype = ct.c_void_p
cairo.cairo_pop_group.argtypes = (ct.c_void_p,)
cairo.cairo_pop_group_to_source.argtypes = (ct.c_void_p,)
cairo.cairo_get_group_target.restype = ct.c_void_p
cairo.cairo_get_group_target.argtypes = (ct.c_void_p,)
cairo.cairo_copy_path.argtypes = (ct.c_void_p,)
cairo.cairo_copy_path.restype = ct.c_void_p
cairo.cairo_copy_path_flat.argtypes = (ct.c_void_p,)
cairo.cairo_copy_path_flat.restype = ct.c_void_p
cairo.cairo_append_path.argtypes = (ct.c_void_p, ct.c_void_p) # not used
cairo.cairo_has_current_point.argtypes = (ct.c_void_p,)
cairo.cairo_get_current_point.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_new_path.argtypes = (ct.c_void_p,)
cairo.cairo_new_sub_path.argtypes = (ct.c_void_p,)
cairo.cairo_close_path.argtypes = (ct.c_void_p,)
cairo.cairo_arc.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_arc_negative.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_curve_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_line_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_move_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_rectangle.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_glyph_path.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_int)
cairo.cairo_text_path.argtypes = (ct.c_void_p, ct.c_char_p)
cairo.cairo_rel_curve_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_rel_line_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_rel_move_to.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_path_extents.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_translate.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_scale.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_rotate.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_transform.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_set_matrix.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_get_matrix.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_identity_matrix.argtypes = (ct.c_void_p,)
cairo.cairo_user_to_device.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_user_to_device_distance.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_device_to_user.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_device_to_user_distance.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_get_source.argtypes = (ct.c_void_p,)
cairo.cairo_get_source.restype = ct.c_void_p
cairo.cairo_set_source_rgb.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double) # not used
cairo.cairo_set_source_rgba.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_set_source.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_set_source_surface.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_get_antialias.argtypes = (ct.c_void_p,)
cairo.cairo_set_dash.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_int, ct.c_double)
cairo.cairo_get_dash_count.argtypes = (ct.c_void_p,)
cairo.cairo_get_dash.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_set_antialias.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_set_fill_rule.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_get_fill_rule.argtypes = (ct.c_void_p,)
cairo.cairo_set_line_cap.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_get_line_cap.argtypes = (ct.c_void_p,)
cairo.cairo_set_line_join.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_get_line_join.argtypes = (ct.c_void_p,)
cairo.cairo_get_line_width.argtypes = (ct.c_void_p,)
cairo.cairo_get_line_width.restype = ct.c_double
cairo.cairo_set_line_width.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_get_miter_limit.restype = ct.c_double
cairo.cairo_set_miter_limit.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_get_operator.argtypes = (ct.c_void_p,)
cairo.cairo_set_operator.argtypes = (ct.c_void_p, ct.c_int)
cairo.cairo_get_tolerance.argtypes = (ct.c_void_p,)
cairo.cairo_get_tolerance.restype = ct.c_double
cairo.cairo_set_tolerance.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_clip.argtypes = (ct.c_void_p,)
cairo.cairo_clip_preserve.argtypes = (ct.c_void_p,)
cairo.cairo_clip_extents.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_in_clip.restype = ct.c_bool
cairo.cairo_in_clip.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_copy_clip_rectangle_list.restype = CAIRO.rectangle_list_ptr_t
cairo.cairo_copy_clip_rectangle_list.argtypes = (ct.c_void_p,)
cairo.cairo_rectangle_list_destroy.argtypes = (CAIRO.rectangle_list_ptr_t,)
if hasattr(cairo, "cairo_tag_begin") : # since 1.16
cairo.cairo_tag_begin.restype = None
cairo.cairo_tag_begin.argtypes = (ct.c_void_p, ct.c_char_p, ct.c_char_p)
cairo.cairo_tag_end.restype = None
cairo.cairo_tag_end.argtypes = (ct.c_void_p, ct.c_char_p)
#end if
cairo.cairo_reset_clip.argtypes = (ct.c_void_p,)
cairo.cairo_fill.argtypes = (ct.c_void_p,)
cairo.cairo_fill_preserve.argtypes = (ct.c_void_p,)
cairo.cairo_fill_extents.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_in_fill.restype = ct.c_bool
cairo.cairo_in_fill.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_mask.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_mask_surface.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_paint.argtypes = (ct.c_void_p,)
cairo.cairo_paint_with_alpha.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_stroke.argtypes = (ct.c_void_p,)
cairo.cairo_stroke_preserve.argtypes = (ct.c_void_p,)
cairo.cairo_stroke_extents.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_in_stroke.restype = ct.c_bool
cairo.cairo_in_stroke.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_copy_page.argtypes = (ct.c_void_p,)
cairo.cairo_show_page.argtypes = (ct.c_void_p,)
cairo.cairo_get_reference_count.restype = ct.c_uint
cairo.cairo_get_reference_count.argtypes = (ct.c_void_p,)
cairo.cairo_path_destroy.argtypes = (ct.c_void_p,)
cairo.cairo_select_font_face.argtypes = (ct.c_void_p, ct.c_char_p, ct.c_int, ct.c_int)
cairo.cairo_set_font_size.argtypes = (ct.c_void_p, ct.c_double)
cairo.cairo_set_font_matrix.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_get_font_matrix.argtypes = (ct.c_void_p,)
cairo.cairo_get_font_matrix.restype = ct.c_void_p
cairo.cairo_set_font_options.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_get_font_options.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_set_font_face.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_get_font_face.restype = ct.c_void_p
cairo.cairo_get_font_face.argtypes = (ct.c_void_p,)
cairo.cairo_font_face_get_reference_count.restype = ct.c_uint
cairo.cairo_font_face_get_reference_count.argtypes = (ct.c_void_p,)
cairo.cairo_set_scaled_font.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_get_scaled_font.restype = ct.c_void_p
cairo.cairo_get_scaled_font.argtypes = (ct.c_void_p,)
cairo.cairo_scaled_font_get_reference_count.restype = ct.c_uint
cairo.cairo_scaled_font_get_reference_count.argtypes = (ct.c_void_p,)
cairo.cairo_user_font_face_create.restype = ct.c_void_p
cairo.cairo_user_font_face_set_init_func.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_user_font_face_get_init_func.restype = ct.c_void_p
cairo.cairo_user_font_face_get_init_func.argtypes = (ct.c_void_p,)
cairo.cairo_user_font_face_set_render_glyph_func.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_user_font_face_get_render_glyph_func.restype = ct.c_void_p
cairo.cairo_user_font_face_get_render_glyph_func.argtypes = (ct.c_void_p,)
cairo.cairo_user_font_face_set_unicode_to_glyph_func.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_user_font_face_get_unicode_to_glyph_func.restype = ct.c_void_p
cairo.cairo_user_font_face_get_unicode_to_glyph_func.argtypes = (ct.c_void_p,)
cairo.cairo_user_font_face_set_text_to_glyphs_func.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_user_font_face_get_text_to_glyphs_func.restype = ct.c_void_p
cairo.cairo_user_font_face_get_text_to_glyphs_func.argtypes = (ct.c_void_p,)
cairo.cairo_show_text.argtypes = (ct.c_void_p, ct.c_char_p)
cairo.cairo_show_glyphs.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_show_text_glyphs.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_int, ct.c_void_p, ct.c_int, ct.c_void_p, ct.c_int, ct.c_uint)
cairo.cairo_font_extents.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_text_extents.argtypes = (ct.c_void_p, ct.c_char_p, ct.c_void_p)
cairo.cairo_glyph_extents.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_int, ct.c_void_p)
cairo.cairo_device_reference.restype = ct.c_void_p
cairo.cairo_device_reference.argtypes = (ct.c_void_p,)
cairo.cairo_device_destroy.argtypes = (ct.c_void_p,)
cairo.cairo_device_get_reference_count.restype = ct.c_uint
cairo.cairo_device_get_reference_count.argtypes = (ct.c_void_p,)
cairo.cairo_surface_status.argtypes = (ct.c_void_p,)
cairo.cairo_surface_get_type.argtypes = (ct.c_void_p,)
cairo.cairo_surface_create_similar.restype = ct.c_void_p
cairo.cairo_surface_create_similar.argtypes = (ct.c_void_p, ct.c_int, ct.c_int, ct.c_int)
cairo.cairo_surface_create_similar_image.restype = ct.c_void_p
cairo.cairo_surface_create_similar_image.argtypes = (ct.c_void_p, ct.c_int, ct.c_int, ct.c_int)
cairo.cairo_surface_create_for_rectangle.restype = ct.c_void_p
cairo.cairo_surface_create_for_rectangle.argtypes = (ct.c_void_p, ct.c_double, ct.c_double, ct.c_double, ct.c_double)
cairo.cairo_surface_reference.restype = ct.c_void_p
cairo.cairo_surface_reference.argtypes = (ct.c_void_p,)
cairo.cairo_surface_destroy.argtypes = (ct.c_void_p,)
cairo.cairo_surface_flush.argtypes = (ct.c_void_p,)
cairo.cairo_surface_get_device.restype = ct.c_void_p
cairo.cairo_surface_get_device.argtypes = (ct.c_void_p,)
cairo.cairo_surface_get_font_options.argtypes = (ct.c_void_p, ct.c_void_p)
cairo.cairo_surface_get_content.restype = ct.c_int
cairo.cairo_surface_get_content.argtypes = (ct.c_void_p,)
cairo.cairo_surface_mark_dirty.argtypes = (ct.c_void_p,)
cairo.cairo_surface_mark_dirty_rectangle.argtypes = (ct.c_void_p, ct.c_int, ct.c_int, ct.c_int, ct.c_int)
cairo.cairo_surface_set_device_offset.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_surface_get_device_offset.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_surface_set_device_scale.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_surface_get_device_scale.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)
cairo.cairo_surface_set_fallback_resolution.argtypes = (ct.c_void_p, ct.c_double, ct.c_double)
cairo.cairo_surface_get_fallback_resolution.argtypes = (ct.c_void_p, ct.c_void_p, ct.c_void_p)