File: cairo.lua

package info (click to toggle)
lua-lgi 0.9.2-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,388 kB
  • sloc: ansic: 5,082; makefile: 169; sh: 31
file content (906 lines) | stat: -rw-r--r-- 29,803 bytes parent folder | download | duplicates (5)
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
------------------------------------------------------------------------------
--
--  LGI Cairo override module.
--
--  Copyright (c) 2012 Pavel Holejsovsky
--  Licensed under the MIT license:
--  http://www.opensource.org/licenses/mit-license.php
--
------------------------------------------------------------------------------

local assert, pairs, ipairs, setmetatable, table, rawget, type
   = assert, pairs, ipairs, setmetatable, table, rawget, type
local lgi = require 'lgi'
local cairo = lgi.cairo

local core = require 'lgi.core'
local ffi = require 'lgi.ffi'
local component = require 'lgi.component'
local record = require 'lgi.record'
local enum = require 'lgi.enum'
local ti = ffi.types

cairo._module = core.module('cairo', 2)
local module_gobject = core.gi.cairo.resolve

-- Versioning support.
function cairo.version_encode(major, minor, micro)
   return 10000 * major + 100 * minor + micro
end
cairo.version = core.callable.new {
   addr = cairo._module.cairo_version, ret = ti.int } ()
cairo.version_string = core.callable.new {
   addr = cairo._module.cairo_version_string, ret = ti.utf8 } ()

-- Load some constants.
cairo._constant = {
   MIME_TYPE_JP2 = 'image/jp2',
   MIME_TYPE_JPEG = 'image/jpeg',
   MIME_TYPE_PNG = 'image/png',
   MIME_TYPE_URI = 'text/x-uri',
}

-- Load definitions of all enums.
cairo._enum = cairo._enum or {}
for _, name in pairs {
   'Status', 'Content', 'Operator', 'Antialias', 'FillRule', 'LineCap',
   'LineJoin', 'TextClusterFlags', 'FontSlant', 'FontWeight', 'SubpixelOrder',
   'HintStyle', 'HintMetrics', 'FontType', 'PathDataType', 'DeviceType',
   'SurfaceType', 'Format', 'PatternType', 'Extend', 'Filter', 'RegionOverlap',
   'PdfVersion', 'PsLevel', 'SvgVersion',
} do
   local gtype = ffi.load_gtype(
      module_gobject, 'cairo_gobject_' .. core.uncamel(name) .. '_get_type')
   if gtype then
      cairo._enum[name] = ffi.load_enum(gtype, 'cairo.' .. name)
   else
      cairo._enum[name] = component.create(nil, enum.enum_mt, 'cairo.' .. name)
   end
end

-- Load definitions of all boxed records.
cairo._struct = cairo._struct or {}
for index, struct in pairs {
   'Context', 'Device', 'Surface', 'Rectangle', 'ScaledFont', 'FontFace',
   'FontOptions', 'Region', 'RectangleInt', 'Path', 'TextExtents',
   'FontExtents', 'Matrix', 'ImageSurface', 'PdfSurface', 'PsSurface',
   'RecordingSurface', 'SvgSurface', 'ToyFontFace', 'RectangleList',
   'SolidPattern', 'SurfacePattern', 'GradientPattern', 'LinearPattern',
   'RadialPattern',
   MeshPattern = cairo.version_encode(1, 12, 0),
} do
   local since = 0
   if type(index) == 'string' then
      since = struct
      struct = index
   end
   if cairo.version >= since then
      local gtype = ffi.load_gtype(
	 module_gobject,
	 'cairo_gobject_' .. core.uncamel(struct) .. '_get_type')
      local obj = component.create(gtype, record.struct_mt, 'cairo.' .. struct)
      cairo._struct[struct] = obj
   end
end

local path_data_header = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_header, { { 'type', cairo.PathDataType },
				    { 'length', ti.int } })
local path_data_point = component.create(nil, record.struct_mt)
ffi.load_fields(path_data_point, { { 'x', ti.double }, { 'y', ti.double } })
cairo._union = rawget(cairo, '_union') or {}
cairo._union.PathData = component.create(nil, record.union_mt, 'cairo.PathData')

-- Populate methods into records.
for _, info in ipairs {
   {  'Status',
      methods = {
	 to_string = { static = true, ret = ti.utf8, cairo.Status }
      },
   },

   {  'Rectangle',
      fields = {
	 { 'x', ti.double }, { 'y', ti.double },
	 { 'width', ti.double }, { 'height', ti.double },
      },
   },

   {  'RectangleList',
      fields = {
	 { 'status', cairo.Status },
	 { 'rectangles', cairo.Rectangle, ptr = true },
	 { 'num_rectangles', ti.int },
      },
   },

   {  'RectangleInt',
      fields = {
	 { 'x', ti.int }, { 'y', ti.int },
	 { 'width', ti.int }, { 'height', ti.int },
      },
   },

   {  'Matrix',
      fields = {
	 { 'xx', ti.double }, { 'yx', ti.double },
	 { 'xy', ti.double }, { 'yy', ti.double },
	 { 'x0', ti.double }, { 'y0', ti.double },
      },
      methods = {
	 init = { ti.double, ti.double, ti.double,
		  ti.double, ti.double, ti.double },
	 init_identity = {},
	 init_translate = { ti.double, ti.double },
	 init_scale = { ti.double, ti.double },
	 init_rotate = { ti.double },
	 translate = { ti.double, ti.double },
	 scale = { ti.double, ti.double },
	 rotate = { ti.double },
	 invert = { ret = cairo.Status },
	 multiply = { cairo.Matrix, cairo.Matrix },
	 transform_distance = { { ti.double, dir = 'inout' },
				{ ti.double, dir = 'inout' } },
	 transform_point = { { ti.double, dir = 'inout' },
			     { ti.double, dir = 'inout' } },
      },
   },

   {  'PathData',
      fields = {
	 { 'header', path_data_header },
	 { 'point', path_data_point },
      },
   },

   {  'Path',
      fields = {
	 { 'status', cairo.Status },
	 { 'data', cairo.PathData, ptr = true },
	 { 'num_data', ti.int },
      },
   },

   {  'FontExtents',
      fields = {
	 { 'ascent', ti.double },
	 { 'descent', ti.double },
	 { 'height', ti.double },
	 { 'max_x_advance', ti.double },
	 { 'max_y_advance', ti.double },
      },
   },

   {  'TextExtents',
      fields = {
	 { 'x_bearing', ti.double },
	 { 'y_bearing', ti.double },
	 { 'width', ti.double },
	 { 'height', ti.double },
	 { 'x_advance', ti.double },
	 { 'y_advance', ti.double },
      },
   },

   {  'Context',
      cprefix = '',
      methods = {
	 create = { static = true, ret = { cairo.Context, xfer = true },
		    cairo.Surface },
	 status = { ret = cairo.Status },
	 save = {},
	 restore = {},
	 get_target = { ret = cairo.Surface},
	 push_group = {},
	 push_group_with_content = { cairo.Content },
	 pop_group = { ret = { cairo.Pattern, xfer = true } },
	 pop_group_to_source = {},
	 get_group_target = { ret = cairo.Surface },
	 set_source_rgb = { ti.double, ti.double, ti.double },
	 set_source_rgba = { ti.double, ti.double, ti.double, ti.double },
	 set_source = { cairo.Pattern },
	 set_source_surface = { cairo.Surface, ti.double, ti.double },
	 get_source = { ret = cairo.Pattern },
	 set_antialias = { cairo.Antialias },
	 get_antialias = { ret = cairo.Antialias },
	 get_dash_count = { ret = ti.int },
	 set_fill_rule = { cairo.FillRule },
	 get_fill_rule = { ret = cairo.FillRule },
	 set_line_cap = { cairo.LineCap },
	 get_line_cap = { ret = cairo.LineCap },
	 set_line_join = { cairo.LineJoin },
	 get_line_join = { ret = cairo.LineJoin },
	 set_line_width = { ti.double },
	 get_line_width = { ret = ti.double },
	 set_miter_limit = { ti.double },
	 get_miter_limit = { ret = ti.double },
	 set_operator = { cairo.Operator },
	 get_operator = { ret = cairo.Operator },
	 set_tolerance = { ti.double },
	 get_tolerance = { ret = ti.double },
	 clip = {},
	 clip_preserve = {},
	 clip_extents = { { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' } },
	 in_clip = { ret = ti.boolean, ti.double, ti.double },
	 reset_clip = {},
	 copy_clip_rectangle_list = {
	    ret = { cairo.RectangleList, xfer = true } },
	 fill = {},
	 fill_preserve = {},
	 fill_extents = { { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' } },
	 in_fill = { ret = ti.boolean, ti.double, ti.double },
	 mask = { cairo.Pattern },
	 mask_surface = { cairo.Surface, ti.double, ti.double },
	 paint = {},
	 paint_with_alpha = { ti.double },
	 stroke = {},
	 stroke_preserve = {},
	 stroke_extents = { { ti.double, dir = 'out' },
			    { ti.double, dir = 'out' },
			    { ti.double, dir = 'out' },
			    { ti.double, dir = 'out' } },
	 in_stroke = { ret = ti.boolean, ti.double, ti.double },
	 copy_page = {},
	 show_page = {},

	 copy_path = { ret = { cairo.Path, xfer = true } },
	 copy_path_flat = { ret = { cairo.Path, xfer = true } },
	 append_path = { cairo.Path },
	 has_current_point = { ret = ti.boolean },
	 get_current_point = { { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' } },
	 new_path = {},
	 new_sub_path = {},
	 close_path = {},
	 arc = { ti.double, ti.double, ti.double, ti.double, ti.double },
	 arc_negative = { ti.double, ti.double, ti.double,
			  ti.double, ti.double },
	 curve_to = { ti.double, ti.double, ti.double,
		      ti.double, ti.double, ti.double },
	 line_to = { ti.double, ti.double },
	 move_to = { ti.double, ti.double },
	 rectangle = { ti.double, ti.double, ti.double, ti.double },
	 -- glyph_path,
	 text_path = { ti.utf8 },
	 rel_curve_to = { ti.double, ti.double, ti.double,
			  ti.double, ti.double, ti.double },
	 rel_line_to = { ti.double, ti.double },
	 rel_move_to = { ti.double, ti.double },
	 path_extents = { { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' },
			  { ti.double, dir = 'out' } },

	 translate = { ti.double, ti.double },
	 scale = { ti.double, ti.double },
	 rotate = { ti.double },
	 transform = { cairo.Matrix },
	 set_matrix = { cairo.Matrix },
	 get_matrix = { cairo.Matrix },
	 identity_matrix = {},
	 user_to_device = { { ti.double, dir = 'inout' },
			    { ti.double, dir = 'inout' } },
	 user_to_device_distance = { { ti.double, dir = 'inout' },
				     { ti.double, dir = 'inout' } },
	 device_to_user = { { ti.double, dir = 'inout' },
			    { ti.double, dir = 'inout' } },
	 device_to_user_distance = { { ti.double, dir = 'inout' },
				     { ti.double, dir = 'inout' } },

	 select_font_face = { ti.utf8, cairo.FontSlant, cairo.FontWeight },
	 set_font_size = { ti.double },
	 set_font_matrix = { cairo.Matrix },
	 get_font_matrix = { cairo.Matrix },
	 set_font_options = { cairo.FontOptions },
	 get_font_options = { cairo.FontOptions },
	 set_font_face = { cairo.FontFace },
	 get_font_face = { ret = cairo.FontFace },
	 set_scaled_font = { cairo.ScaledFont },
	 get_scaled_font = { ret = cairo.ScaledFont },
	 show_text = { ti.utf8 },
	 -- show_glyphs, show_text_glyphs
	 font_extents = { cairo.FontExtents },
	 text_extents = { ti.utf8, cairo.TextExtents },
	 -- glyph extents
      },

      properties = {
	 'status', 'target', 'group_target', 'source', 'antialias', 'fill_rule',
	 'line_cap', 'line_join', 'line_width', 'miter_limit', 'operator',
	 'tolerance', 'font_size', 'font_face', 'font_matrix', 'scaled_font',
	 'matrix',
      },
   },

   {  'Pattern',
      methods = {
	 status = { ret = cairo.Status },
	 set_extend = { cairo.Extend },
	 get_extend = { ret = cairo.Extend },
	 set_filter = { cairo.Filter },
	 get_filter = { ret = cairo.Filter },
	 set_matrix = { cairo.Matrix },
	 get_matrix = { cairo.Matrix },
	 get_type = { ret = cairo.PatternType },
      },
      properties = { 'status', 'extend', 'filter', 'type' },
   },

   {  'SolidPattern', parent = cairo.Pattern,
      cprefix = 'pattern_',
      methods = {
	 create_rgb = { static = true, ret = { cairo.Pattern, xfer = true },
			ti.double, ti.double, ti.double },
	 create_rgba = { static = true, ret = { cairo.Pattern, xfer = true },
			 ti.double, ti.double, ti.double, ti.double },
	 get_rgba = { ret = cairo.Status,
		      { ti.double, dir = 'out' },
		      { ti.double, dir = 'out' },
		      { ti.double, dir = 'out' },
		      { ti.double, dir = 'out' } },
      },
   },

   {  'SurfacePattern', parent = cairo.Pattern,
      cprefix = 'pattern_',
      methods = {
	 create = {
	    cname = 'cairo_pattern_create_for_surface', static = true,
	    ret = { cairo.Pattern, xfer = true },
	    cairo.Surface },
	 get_surface = { ret = cairo.Status, { cairo.Surface, dir = 'out' } },
      },
   },

   {  'GradientPattern', parent = cairo.Pattern,
      cprefix = 'pattern_',
      methods = {
	 add_color_stop_rgb = { ti.double, ti.double, ti.double, ti.double },
	 add_color_stop_rgba = { ti.double, ti.double, ti.double,
				 ti.double, ti.double },
	 get_color_stop_count = { ret = cairo.Status, { ti.int, dir = 'out' } },
	 get_color_stop_rgba = { ret = cairo.Status, ti.int,
				 { ti.double, dir = 'out' },
				 { ti.double, dir = 'out' },
				 { ti.double, dir = 'out' },
				 { ti.double, dir = 'out' },
				 { ti.double, dir = 'out' } },
      },
   },

   {  'LinearPattern', parent = cairo.GradientPattern,
      cprefix = 'pattern_',
      methods = {
	 create = { cname = 'cairo_pattern_create_linear', static = true,
		    ret = { cairo.Pattern, xfer = true },
		    ti.double, ti.double, ti.double, ti.double },
	 get_linear_points = { ret = cairo.Status,
			       { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' } },
      }
   },

   {  'RadialPattern', parent = cairo.GradientPattern,
      cprefix = 'pattern_',
      methods = {
	 create = { cname = 'cairo_pattern_create_radial', static = true,
		    ret = { cairo.Pattern, xfer = true },
		    ti.double, ti.double, ti.double, ti.double,
		    ti.double, ti.double },
	 get_radial_circles = { ret = cairo.Status,
				{ ti.double, dir = 'out' },
				{ ti.double, dir = 'out' },
				{ ti.double, dir = 'out' },
				{ ti.double, dir = 'out' },
				{ ti.double, dir = 'out' },
				{ ti.double, dir = 'out' } },
      },
   },

   {  'MeshPattern', parent = cairo.Pattern,
      since = cairo.version_encode(1, 12, 0),
      methods = {
	 create = { cname = 'cairo_pattern_create_mesh', static = true,
		    ret = { cairo.Pattern, xfer = true } },
	 begin_patch = {},
	 end_patch = {},
	 move_to = { ti.double, ti.double },
	 line_to = { ti.double, ti.double },
	 curve_to = { ti.double, ti.double, ti.double, ti.double,
		      ti.double, ti.double },
	 set_control_point = { ti.int, ti.double, ti.double },
	 set_corner_color_rgb = { ti.int, ti.double, ti.double, ti.double },
	 set_corner_color_rgba = { ti.int, ti.double, ti.double, ti.double,
				   ti.double },
	 get_patch_count = { ret = cairo.Status, { ti.int, dir = 'out' } },
	 get_path = { ret = { cairo.Path, xfer = true }, ti.int },
	 get_control_point = { ret = cairo.Status, ti.int, ti.int,
			       { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' } },
	 get_corner_color_rgba = { ret = cairo.Status, ti.int, ti.int,
				   { ti.double, dir = 'out' },
				   { ti.double, dir = 'out' },
				   { ti.double, dir = 'out' },
				   { ti.double, dir = 'out' } },
      },
   },

   {  'Device',
      methods = {
	 status = { ret = cairo.Status },
	 finish = {},
	 flush = {},
	 get_type = { ret = cairo.DeviceType },
	 acquire = { ret = cairo.Status },
	 release = {},
      },
      properties = { 'status' },
   },

   {  'Format',
      methods = {
	 stride_for_width = { ret = ti.int, ti.int },
      },
   },

   {  'Surface',
      methods = {
	 create_similar = { ret = { cairo.Surface, xfer = true },
			    cairo.Content, ti.int, ti.int },
	 create_for_rectangle = { ret = { cairo.Surface, xfer = true },
				  ti.double, ti.double, ti.double, ti.double },
	 status = { ret = cairo.Status },
	 finish = {},
	 flush = {},
	 get_device = { ret = cairo.Device },
	 get_font_options = { cairo.FontOptions },
	 get_content = { ret = cairo.Content },
	 mark_dirty = {},
	 mark_dirty_rectangle = { ti.int, ti.int, ti.int, ti.int },
	 set_device_offset = { ti.double, ti.double },
	 get_device_offset = { { ti.double, dir = 'out' },
			       { ti.double, dir = 'out' } },
	 set_fallback_resolution = { ti.double, ti.double },
	 get_fallback_resolution = { { ti.double, dir = 'out' },
				     { ti.double, dir = 'out' } },
	 get_type = { ret = cairo.SurfaceType },
	 copy_page = {},
	 show_page = {},
	 has_show_text_glyphs = { ret = ti.boolean },
	 -- set_mime_data, get_mime_data
	 write_to_png = { ret = cairo.Status, ti.filename },
      },

      properties = { 'status', 'device', 'content', 'type' },
   },

   {  'ImageSurface', parent = cairo.Surface,
      methods = {
	 create = { static = true, ret = { cairo.Surface, xfer = true },
		    cairo.Format, ti.int, ti.int },
	 create_for_data = { static = true,
			     ret = { cairo.Surface, xfer = true },
			     ti.ptr, cairo.Format, ti.int, ti.int, ti.int },
	 create_from_png = { static = true,
			     ret = { cairo.Surface, xfer = true },
			     ti.filename },
	 get_data = { ret = ti.ptr },
	 get_format = { ret = cairo.Format },
	 get_width = { ret = ti.int },
	 get_height = { ret = ti.int },
	 get_stride = { ret = ti.int },
      },
      properties = { 'data', 'format', 'width', 'height', 'stride' },
   },

   {  'PdfVersion',
      values = {
	 ['1_4'] = 0,
	 ['1_5'] = 1,
      },
   },

   {  'PdfSurface', parent = cairo.Surface,
      methods = {
	 create = { static = true, ret = { cairo.Surface, xfer = true },
		    ti.filename, ti.double, ti.double },
	 restrict_to_version = { cairo.PdfVersion },
	 set_size = { ti.double, ti.double },
      },
   },

   {  'PsLevel',
      values = {
	 ['2'] = 0,
	 ['3'] = 1,
      },
   },

   {  'PsSurface', parent = cairo.Surface,
      methods = {
	 create = { static = true, ret = { cairo.Surface, xfer = true },
		    ti.filename, ti.double, ti.double },
	 restrict_to_level = { cairo.PsLevel },
	 set_eps = { ti.boolean },
	 get_eps = { ret = ti.boolean },
	 set_size = { ti.double, ti.double },
	 dsc_begin_setup = {},
	 dsc_begin_page_setup = {},
	 dsc_comment = { ti.utf8 },
      },
      properties = { 'eps' },
   },

   {  'RecordingSurface', parent = cairo.Surface,
      methods = {
	 create = { static = true, ret = { cairo.Surface, xfer = true },
		    cairo.Content, cairo.Rectangle },
	 ink_extents = {
	    { ti.double, dir = 'out' }, { ti.double, dir = 'out' },
	    { ti.double, dir = 'out' }, { ti.double, dir = 'out' } },
	 get_extents = { since = cairo.version_encode(1, 12, 0),
			 ret = ti.boolean, cairo.Rectangle },
      },
   },

   {  'SvgVersion',
      values = {
	 ['1_1'] = 0,
	 ['1_2'] = 1,
      },
   },

   {  'SvgSurface', parent = cairo.Surface,
      methods = {
	 create = { static = true, ret = { cairo.Surface, xfer = true },
		    ti.filename, ti.double, ti.double },
	 restrict_to_version = { cairo.SvgVersion },
      },
   },

   {  'FontFace',
      methods = {
	 status = { ret = cairo.Status },
	 get_type = { ret = cairo.FontType },
      },
      properties = { 'status', 'type' },
   },

   {  'ToyFontFace', parent = cairo.FontFace,
      methods = {
	 create = { static = true, ret = { cairo.ToyFontFace, xfer = true },
		    ti.utf8, cairo.FontSlant, cairo.FontWeight },
	 get_family = { ret = ti.utf8 },
	 get_slant = { ret = cairo.Slant },
	 get_weight = { ret = cairo.Weight },
      },
      properties = { 'family', 'slant', 'weight' },
   },

   {  'FontOptions',
      methods = {
	 create = { static = true, ret = { cairo.FontOptions, xfer = true } },
	 copy = { ret = { cairo.FontOptions, xfer = true } },
	 status = { ret = cairo.Status },
	 merge = { cairo.FontOptions },
	 hash = { ret = ti.ulong },
	 equal = { ret = ti.boolean, cairo.FontOptions },
	 set_antialias = { cairo.Antialias },
	 get_antialias = { ret = cairo.Antialias },
	 set_subpixel_order = { cairo.SubpixelOrder },
	 get_subpixel_order = { ret = cairo.SubpixelOrder },
	 set_hint_style = { cairo.HintStyle },
	 get_hint_style = { ret = cairo.HintStyle },
	 set_hint_metrics = { cairo.HintMetrics },
	 get_hint_metrics = { ret = cairo.HintMetrics },
      },
      properties = { 'status', 'antialias', 'subpixel_order',
		     'hint_style', 'hint_metrics' },
   },

   {  'ScaledFont',
      methods = {
	 create = { static = true, ret = { cairo.ScaledFont, xfer = true },
		    cairo.FontFace, cairo.Matrix, cairo.Matrix,
		    cairo.FontOptions },
	 status = { ret = cairo.Status },
	 extents = { cairo.FontExtents },
	 text_extents = { ti.utf8, cairo.TextExtents },
	 -- glyph_extents, text_to_glyphs
	 get_font_face = { ret = cairo.FontFace },
	 get_font_options = { cairo.FontOptions },
	 get_font_matrix = { cairo.Matrix },
	 get_ctm = { cairo.Matrix },
	 get_scale_matrix = { cairo.Matrix },
	 get_type = { ret = cairo.FontType },
      },
      properties = { 'status', 'font_face' },
   },

   {  'Region',
      methods = {
	 create = { static = true, ret = { cairo.Region, xfer = true } },
	 create_rectangle = { static = true,
			      ret = { cairo.Region, xfer = true },
			      cairo.RectangleInt },
	 copy = { ret = cairo.Region },
	 status = { ret = cairo.Status },
	 get_extents = { cairo.RectangleInt },
	 num_rectangles = { ret = ti.int },
	 get_rectangle = { ti.int, cairo.RectangleInt },
	 is_empty = { ret = ti.boolean },
	 contains_point = { ret = ti.boolean, ti.int, ti.int },
	 contains_rectangle = { ret = cairo.RegionOverlap, cairo.RectangleInt },
	 equal = { ret = ti.boolean, cairo.Region },
	 translate = { ti.int, ti.int },
	 intersect = { ret = cairo.Status, cairo.Region },
	 intersect_rectangle = { ret = cairo.Status, cairo.RectangleInt },
	 subtract = { ret = cairo.Status, cairo.Region },
	 subtract_rectangle = { ret = cairo.Status, cairo.RectangleInt },
	 union = { ret = cairo.Status, cairo.Region },
	 union_rectangle = { ret = cairo.Status, cairo.RectangleInt },
	 xor = { ret = cairo.Status, cairo.Region },
	 xor_rectangle = { ret = cairo.Status, cairo.RectangleInt },
      },
      properties = { 'status', 'extents', },
   },
} do
   if cairo.version >= (info.since or 0) then
      local name = info[1]
      local obj = assert(cairo[name], name)
      obj._parent = info.parent
      if info.methods then
	 -- Go through description of the methods and create functions
	 -- from them.
	 obj._method = {}
	 local cprefix = 'cairo_' .. (info.cprefix or core.uncamel(name) .. '_')
	 local self_arg = { obj }
	 for method_name, method_info in pairs(info.methods) do
	    if cairo.version >= (method_info.since or 0) then
	       method_info.name = 'cairo.' .. name .. '.' .. method_name
	       method_info.addr = assert(
		  cairo._module[
		     method_info.cname or (cprefix .. method_name)])
	       if not method_info.static then
		  table.insert(method_info, 1, self_arg)
	       end
	       method_info.ret = method_info.ret or ti.void
	       obj._method[method_name] = core.callable.new(method_info)
	    end
	 end
      end
      if info.values then
	 -- Fill in addition enum/bitflag values.
	 for n, v in pairs(info.values) do
	    obj[n] = v
	 end
      end
      if info.properties then
	 -- Aggregate getters/setters into pseudoproperties
	 -- implemented as attributes.
	 obj._attribute = {}
	 for _, attr in pairs(info.properties) do
	    obj._attribute[attr] = {
	       get = obj._method['get_' .. attr] or obj._method[attr],
	       set = obj._method['set_' .. attr],
	    }
	 end
      end
      if info.fields then
	 -- Load record fields definition
	 ffi.load_fields(obj, info.fields)
      end
   end
end

-- Teach non-boxed structs how to destroy itself.
cairo.RectangleList._free = cairo._module.cairo_rectangle_list_destroy
cairo.Path._free = cairo._module.cairo_path_destroy
cairo.FontOptions._free = cairo._module.cairo_font_options_destroy

-- Add Matrix creation routines.
for _, name in pairs { 'identity', 'scale', 'rotate', 'translate' } do
   local init = cairo.Matrix._method['init_' .. name]
   cairo.Matrix._method['create_' .. name] = function(...)
      local matrix = core.record.new(cairo.Matrix)
      init(matrix, ...)
      return matrix
   end
end

-- FontOptions can be created only by 'create' method.
function cairo.FontOptions._method:_new(props)
   local font_options = self.create()
   for k, v in pairs(props or {}) do
      font_options[k] = v
   end
   return font_options
end

-- Fix all methods using caller-alloc attribute, which is not
-- supported by ffi.  Emulate it 'by-hand', creating Lua wrapper for
-- them.
for _, info in pairs {
   { cairo.Context, 'matrix', cairo.Matrix },
   { cairo.Context, 'font_matrix', cairo.Matrix },
   { cairo.Context, 'font_options', cairo.FontOptions },
   { cairo.Context, 'font_extents', cairo.FontExtents },
   { cairo.Context, 'text_extents', cairo.TextExtents, args = 1 },
   { cairo.Pattern, 'matrix', cairo.Matrix },
   { cairo.Surface, 'font_options', cairo.FontOptions },
   { cairo.ScaledFont, 'font_matrix', cairo.Matrix },
   { cairo.ScaledFont, 'ctm', cairo.Matrix },
   { cairo.ScaledFont, 'scale_matrix', cairo.Matrix },
   { cairo.ScaledFont, 'font_options', cairo.FontOptions },
   { cairo.ScaledFont, 'extents', cairo.FontExtents },
   { cairo.ScaledFont, 'text_extents', cairo.TextExtents, args = 1 },
   { cairo.RecordingSurface, 'extents', cairo.Rectangle },
   { cairo.Region, 'extents', cairo.RectangleInt },
   { cairo.Region, 'get_rectangle', cairo.RectangleInt, args = 1 },
} do
   local getter_name = 'get_' ..info[2]
   local raw_getter = info[1]._method[getter_name]
   if not raw_getter then
      getter_name = info[2]
      raw_getter = info[1]._method[getter_name]
   end
   if raw_getter then
      if info.args == 1 then
	 info[1]._method[getter_name] = function(self, arg1)
	    local ret = info[3]()
	    local res = raw_getter(self, arg1, ret)
	    return (res == nil or res) and ret or nil
	 end
      else
	 info[1]._method[getter_name] = function(self)
	    local ret = info[3]()
	    local res = raw_getter(self, ret)
	    return (res == nil or res) and ret or nil
	 end
	 info[1]._attribute[info[2]] = {
	    get = info[1][getter_name],
	    set = info[1]['set_' .. info[2]],
	 }
      end
   end
end

-- Choose correct 'subclass' of surface on attaching to surface instances.
local surface_type_map = {
   IMAGE = cairo.ImageSurface,
   PDF = cairo.PdfSurface,
   PS = cairo.PsSurface,
   SVG = cairo.SvgSurface,
   RECORDING = cairo.RecordingSurface,
}
function cairo.Surface:_attach(surface)
   local type = cairo.Surface._method.get_type(surface)
   local repotype = surface_type_map[type]
   if repotype then
      core.record.set(surface, repotype)
   end
end

-- Also choose correct 'subclass' for patterns.
local pattern_type_map = {
   SOLID = cairo.SolidPattern,
   SURFACE = cairo.SurfacePattern,
   LINEAR = cairo.LinearPattern,
   RADIAL = cairo.RadialPattern,
   MESH = cairo.MeshPattern
}
function cairo.Pattern:_attach(pattern)
   local type = cairo.Pattern._method.get_type(pattern)
   local repotype = pattern_type_map[type]
   if repotype then
      core.record.set(pattern, repotype)
   end
end

-- Solid pattern constructor, calling either rgb or rgba variant
-- according to the number of arguments.
function cairo.SolidPattern._method.create(red, green, blue, alpha)
   if alpha then
      return cairo.SolidPattern._method.create_rgba(red, green, blue, alpha)
   else
      return cairo.SolidPattern._method.create_rgb(red, green, blue)
   end
end

-- For backward compatibility, propagate pattern creation methods from
-- specific subpattern types into base cairo.Pattern class.
cairo.Pattern._method.create_rgb = cairo.SolidPattern.create_rgb
cairo.Pattern._method.create_rgba = cairo.SolidPattern.create_rgba
cairo.Pattern._method.create_for_surface = cairo.SurfacePattern.create
cairo.Pattern._method.create_linear = cairo.LinearPattern.create
cairo.Pattern._method.create_radial = cairo.RadialPattern.create
if cairo.MeshPattern then
   cairo.Pattern._method.create_mesh = cairo.MeshPattern.create
end

-- Map all 'create' methods to constructors.
for _, struct in pairs(cairo._struct) do
   local create = struct._method and struct._method.create
   if create then
      function struct._new(typetable, ...)
	 return create(...)
      end
   end
end

-- Implementation of Context.dash operations.  Since ffi does not
-- support arrays of doubles, we cheat here and use array of structs
-- containing only single 'double' field.
local wrapped_double = component.create(nil, record.struct_mt)
ffi.load_fields(wrapped_double, { { 'v', ti.double } })
local raw_set_dash = core.callable.new {
   addr = cairo._module.cairo_set_dash, ret = ti.void,
   cairo.Context, wrapped_double, ti.int, ti.double }
local raw_get_dash = core.callable.new {
   addr = cairo._module.cairo_get_dash, ret = ti.void,
   cairo.Context, wrapped_double, { ti.double, dir = 'out' } }
function cairo.Context:set_dash(dashes, offset)
   local count, array = 0
   if dashes and #dashes > 0 then
      -- Convert 'dashes' array into the native array of wrapped_double
      -- records.
      count = #dashes
      array = core.record.new(wrapped_double, nil, count)
      for i = 1, count do
	 core.record.fromarray(array, i - 1).v = dashes[i]
      end
   end
   raw_set_dash(self, array, count, offset)
end

function cairo.Context:get_dash()
   local dashes, offset = {}, 0
   local count = self:get_dash_count()
   if count > 0 then
      -- Prepare native array of wrapped doubles of specified size.
      local array = core.record.new(wrapped_double, nil, count)

      -- Get the dashes.
      offset = raw_get_dash(self, array)

      -- Convert output to the table.
      for i = 1, count do
	 dashes[i] = core.record.fromarray(array, i - 1).v
      end
   end
   return dashes, offset
end

-- Implementation of iteration protocol over the cairo.Path
function cairo.Path:pairs()
   local index = 0
   return function()
      -- Bounds check, and get appropriate header PathData element.
      if index >= self.num_data then return nil end
      local path_data = core.record.fromarray(self.data, index)
      local type, length = path_data.header.type, path_data.header.length

      -- Create 'points' table.
      local points = {}
      if type ~= 'CLOSE_PATH' then
	 points[1] = core.record.fromarray(self.data, index + 1).point
	 if type == 'CURVE_TO' then
	    points[2] = core.record.fromarray(self.data, index + 2).point
	    points[3] = core.record.fromarray(self.data, index + 3).point
	 end
      end

      -- Skip this logical item.
      index = index + length
      return type, points
   end
end