-
Notifications
You must be signed in to change notification settings - Fork 0
/
cairomod.c
1487 lines (1359 loc) · 47.3 KB
/
cairomod.c
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
/* -*- Mode: c; indent-tabs-mode: nil; c-basic-offset: 2; c-file-style: "gnu" -*-
*
* pike bindings for Cairo (http://www.cairographics.org)
*
* Started 2006-02-03
* Copyright (C) 2006, David Vest <davve@davve.net>
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo binding module for Pike.
*
* The Initial Developer of the Original Code is David Vest
* <davve@davve.net>.
*
* Contributor(s):
* None
*/
#include "cairomod.h"
#include "image.h"
#define CHECK_CAIRO_CONTEXT_ERROR(CC) \
do { \
cairo_status_t status; \
status = cairo_status((CC)); \
if (status != CAIRO_STATUS_SUCCESS) \
{ \
cairo_pike_set_error(status); \
pike_throw(); \
} \
} while(0); \
struct program *cairo_mod_context_program = NULL;
struct program *gtk2_cairo_mod_context_program = NULL;
struct program *cairo_mod_image_surface_program = NULL;
struct program *pgtk_window_program = NULL;
/*! @module Cairo
*!
*! This module provides binding to the Cairo 2D grapics library. See
*! @url{http://www.cairographics.org/@} for more information about
*! Cairo.
*!
*/
/*! @class Context
*!
*! @[Cairo.Context] is the main object used when drawing with
*! cairo. To draw with cairo, you create a @[Cairo.Context], set the
*! target surface, and drawing options for the @[Cairo.Context],
*! create shapes with functions like @[move_to] and @[line_to], and
*! then draw shapes with @[stroke] or @[fill].
*!
*! @[Cairo.Context]'s can be pushed to a stack via @[save]. They may
*! then safely be changed, without loosing the current state. Use
*! @[restore] to restore to the saved state.
*/
#ifdef HAVE_CAIRO
#define THIS_CONTEXT ((struct cairo_mod_context *)Pike_fp->current_storage)
static void init_cairo_mod_context( struct object *o )
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cc->ctx = NULL;
}
static void exit_cairo_mod_context( struct object *o )
{
if (THIS_CONTEXT->ctx)
{
cairo_destroy(THIS_CONTEXT->ctx);
}
}
/*! @decl void create(Cairo.Surface surface)
*!
*! Create a @[Cairo.Context] to a @[Cairo.Surface]. See below for a
*! trivial example that draws a triangle to a image surface and saves
*! the result to a png-file with alpha-channel.
*!
*! @example
*! // Draw a triangle to a png-file
*! Cairo.Surface surface = Cairo.ImageSurface(Cairo.CAIRO_FORMAT_ARGB32, 100, 100);
*! Cairo.Context cr = Cairo.Context(surface);
*! cr->move_to(0,0);
*! cr->line_to(100,100);
*! cr->line_to(0,100);
*! cr->close_path();
*! cr->set_source(0.4, 0.5, 0.6, 1.0);
*! cr->fill_preserve();
*! cr->set_source(1.0, 1.0, 1.0, 1.0);
*! cr->set_line_width(1.0);
*! cr->stroke();
*! mapping(string:Image.Image) image = surface->get_image();
*! Image.Image im = image["image"];
*! Image.Image alpha = image["alpha"];
*! Stdio.write_file("output.png",
*! Image.PNG.encode(im, ([ "alpha" : alpha ])));
*/
static void f_context_create(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object *o=NULL;
struct cairo_mod_surface *sw=NULL;
get_all_args("create", args, "%o", &o);
pop_n_elems(args);
if (!(sw = (struct cairo_mod_surface*)get_storage(o, cairo_mod_surface_program)))
Pike_error("Object is not Surface\n");
cc->ctx = cairo_create(sw->surface);
}
/*! @decl Cairo.Context move_to(int|float x, int|float y)
*!
*! If the current subpath is not empty, begin a new subpath. After
*! this call the current point will be (@[x], @[y]).
*!
*! @param x
*! The X coordinate of the new position
*! @param y
*! The Y coordinate of the new position
*/
static void f_move_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE x, y;
get_all_args("move_to", args, "%F%F", &x, &y);
pop_n_elems(args);
cairo_move_to(cc->ctx, x, y);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context rel_move_to(int|float x, int|float y)
*!
*! If the current subpath is not empty, begin a new subpath. After
*! this call the current point will be offset by (@[x], @[y]).
*!
*! @param x
*! The X offset
*! @param y
*! The Y offset
*/
static void f_rel_move_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE dx, dy;
get_all_args("rel_move_to", args, "%F%F", &dx, &dy);
pop_n_elems(args);
cairo_rel_move_to(cc->ctx, dx, dy);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context arc(int|float xc, int|float yc, int|float radius, int|float angle1, int|float angle2)
*!
*! Adds a circular arc of the given @[radius] to the current path. The
*! arc is centered at (@[xc], @[yc]), begins at @[angle1] and proceeds in
*! the direction of increasing angles to end at @[angle2]. If @[angle2] is
*! less than @[angle1] it will be progressively increased by 2*PI
*! until it is greater than @[angle1].
*!
*! If there is a current point, an initial line segment will be added
*! to the path to connect the current point to the beginning of the
*! arc.
*!
*! Angles are measured in radians. An angle of 0 is in the direction
*! of the positive X axis (in user-space). An angle of PI radians
*! (90 degrees) is in the direction of the positive Y axis (in
*! user-space). Angles increase in the direction from the positive X
*! axis toward the positive Y axis. So with the default transformation
*! matrix, angles increase in a clockwise direction.
*!
*! (To convert from degrees to radians, use @expr{degrees * (Math.pi /
*! 180)@}.)
*!
*! This function gives the arc in the direction of increasing angles;
*! see @[arc_negative] to get the arc in the direction of
*! decreasing angles.
*!
*! The arc is circular in user-space. To achieve an elliptical arc,
*! you can scale the current transformation matrix by different
*! amounts in the X and Y directions. For example, to draw an ellipse
*! in the box given by @tt{x@}, @tt{y@}, @tt{width@}, @tt{height@}:
*!
*! @example
*! cr = Cairo.Context();
*! cr->save();
*! cr->translate(x + width / 2.0, y + height / 2.0);
*! cr->scale(1.0 / (height / 2.0), 1.0 / (width / 2.0));
*! cr->arc(0., 0.0, 1.0, 0.0, 2 * Math.pi);
*! cr->restore();
*/
static void f_arc(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE x, y, a, b, c;
get_all_args("arc", args, "%F%F%F%F%F", &x, &y, &a, &b, &c);
pop_n_elems(args);
cairo_arc(cc->ctx, x, y, a, b, c);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context arc_negative(int|float xc, int|float yc, int|float radius, int|float angle1, int|float angle2)
*!
*! Adds a circular arc of the given @[radius] to the current path. The
*! arc is centered at (@[xc], @[yc]), begins at @[angle1] and proceeds in
*! the direction of decreasing angles to end at @[angle2]. If @[angle2] is
*! greater than @[angle1] it will be progressively decreased by [2*PI]
*! until it is greater than @[angle1].
*!
*! See @[arc] for more details. This function differs only in the
*! direction of the arc between the two angles.
*/
static void f_arc_negative(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE xc, yc, radius, angle1, angle2;
get_all_args("arc", args, "%F%F%F%F%F", &xc, &yc, &radius, &angle1, &angle2);
pop_n_elems(args);
cairo_arc_negative(cc->ctx, xc, yc, radius, angle1, angle2);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context rel_line_to(int|float dx, int|float dy)
*/
static void f_rel_line_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE dx, dy;
get_all_args("rel_line_to", args, "%F%F", &dx, &dy);
pop_n_elems(args);
cairo_rel_line_to(cc->ctx, dx, dy);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context line_to(int|float x, int|float y)
*/
static void f_line_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE x, y;
get_all_args("line_to", args, "%F%F", &x, &y);
pop_n_elems(args);
cairo_line_to(cc->ctx, x, y);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context cairo_curve_to(float x1, float y1, @
*! float x2, float y2, @
*! float x3, float y3)
*!
*! @param x1
*! the X coordinate of the first control point
*! @param y1
*! the Y coordinate of the first control point
*! @param x2
*! the X coordinate of the second control point
*! @param y2
*! the Y coordinate of the second control point
*! @param x3
*! the X coordinate of the end of the curve
*! @param y3
*! the Y coordinate of the end of the curve
*!
*! Adds a cubic Bezier spline to the path from the current point to
*! position (@[x3], @[y3]) in user-space coordinates, using (@[x1], @[y1]) and
*! (@[x2], @[y2]) as the control points. After this call the current point
*! will be (@[x3], @[y3]).
*/
static void f_curve_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE x1, y1, x2, y2, x3, y3;
get_all_args("curve_to", args, "%F%F%F%F%F%F",
&x1, &y1, &x2, &y2, &x3, &y3);
pop_n_elems(args);
cairo_curve_to (cc->ctx, x1, y1,
x2, y2, x3, y3);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context cairo_rel_curve_to(float dx1, float dy1, @
*! float dx2, float dy2, @
*! float dx3, float dy3)
*/
static void f_rel_curve_to(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE dx1, dy1, dx2, dy2, dx3, dy3;
get_all_args("rel_curve_to", args, "%F%F%F%F%F%F",
&dx1, &dy1, &dx2, &dy2, &dx3, &dy3);
pop_n_elems(args);
cairo_rel_curve_to (cc->ctx, dx1, dy1,
dx2, dy2, dx3, dy3);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context close_path()
*/
static void f_close_path(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_close_path(cc->ctx);
ref_push_object(THIS_OBJ);
}
#ifdef DYNAMIC_MODULE
struct program *image_color_program;
#else
program *image_color_program;
#endif
static void assert_image_color_program()
{
#ifdef DYNAMIC_MODULE
if(!image_color_program)
{
image_color_program = PIKE_MODULE_IMPORT(Image, image_color_program);
if(!image_color_program)
{
yyerror("Could not load Image module.");
return;
}
}
#else
Pike_error("No dynamic modules. No Image.Color submodule.\n");
#endif
}
/*! @decl Cairo.Context set_source(Image.Color color)
*! @decl Cairo.Context set_source(Image.Color color, float a)
*! @decl Cairo.Context set_source(Cairo.Pattern pattern)
*! @decl Cairo.Context set_source(Cairo.Surface surface, float|int x, float|int y)
*! @decl Cairo.Context set_source(float r, float g, float b)
*! @decl Cairo.Context set_source(float r, float g, float b, float a)
*/
static void f_set_source(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object* o;
struct cairo_mod_pattern *cp;
struct cairo_mod_surface* s;
FLOAT_TYPE r, g, b, a, x, y;
struct color_struct *col;
if (args == 4)
{
get_all_args("set_source", args, "%F%F%F%F", &r, &g, &b, &a);
pop_n_elems(args);
cairo_set_source_rgba(cc->ctx, r, g, b, a);
}
else if (args == 3)
{
if (TYPEOF(Pike_sp[-3]) == PIKE_T_OBJECT)
{
get_all_args("set_source", args, "%o%F%F", &o, &x, &y);
pop_n_elems(args);
if (!(s = (struct cairo_mod_surface *)get_storage(o, cairo_mod_surface_program)))
SIMPLE_ARG_TYPE_ERROR("set_source", 0, "Image.Surface");
cairo_set_source_surface(cc->ctx, s->surface, x, y);
}
else
{
get_all_args("set_source", args, "%F%F%F", &r, &g, &b);
pop_n_elems(args);
cairo_set_source_rgb(cc->ctx, r, g, b);
}
}
else if (args == 2)
{
get_all_args("set_source", args, "%o%F", &o, &a);
pop_n_elems(args);
if (o->prog->id == PROG_IMAGE_COLOR_COLOR_ID)
{
assert_image_color_program();
col = (struct color_struct*)get_storage(o, image_color_program);
cairo_set_source_rgba(cc->ctx,
col->rgbl.r / (FLOAT_TYPE)COLORLMAX,
col->rgbl.g / (FLOAT_TYPE)COLORLMAX,
col->rgbl.b / (FLOAT_TYPE)COLORLMAX,
a);
}
else
SIMPLE_ARG_TYPE_ERROR("set_source", 0, "Image.Color");
}
else if (args == 1)
{
get_all_args("set_source", args, "%o", &o);
pop_n_elems(args);
if (o->prog->id == PROG_IMAGE_COLOR_COLOR_ID)
{
assert_image_color_program();
col = (struct color_struct*)get_storage(o, image_color_program);
cairo_set_source_rgb(cc->ctx,
col->rgbl.r / (FLOAT_TYPE)COLORLMAX,
col->rgbl.g / (FLOAT_TYPE)COLORLMAX,
col->rgbl.b / (FLOAT_TYPE)COLORLMAX);
}
else
{
// we got pattern hopefully
if (!(cp = (struct cairo_mod_pattern *)get_storage(o, cairo_mod_pattern_program)))
SIMPLE_ARG_TYPE_ERROR ("set_source", 0, "Image.Color|Cairo.Pattern");
cairo_set_source(cc->ctx, cp->pattern);
}
}
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context fill_preserve()
*/
static void f_fill_preserve(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_fill_preserve(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context fill()
*/
static void f_fill(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_fill(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl void clip()
*/
static void f_clip(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_clip(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
}
/*! @decl Cairo.Context clip_preserve()
*/
static void f_clip_preserve(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_clip_preserve(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context reset_clip()
*/
static void f_reset_clip(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_reset_clip(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context save()
*/
static void f_save(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_save(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl void restore()
*!
*! @note
*! This function does not return its own object. This is because the
*! state is cleared from the cairo context when restore is called, and
*! nesting of this command makes little sense.
*/
static void f_restore(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_restore(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
}
/*! @decl Cairo.Context translate(float|int tx, float|int ty)
*/
static void f_translate(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE tx, ty;
get_all_args("translate", args, "%F%F", &tx, &ty);
cairo_translate(cc->ctx, tx, ty);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context scale(float|int sx, float|int sy)
*/
static void f_scale(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE sx, sy;
get_all_args("scale", args, "%F%F", &sx, &sy);
cairo_scale(cc->ctx, sx, sy);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context rotate(float|int r)
*/
static void f_rotate(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE r;
get_all_args("rotate", args, "%F", &r);
cairo_rotate(cc->ctx, r);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context paint()
*/
static void f_paint(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_paint(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context paint_with_alpha()
*/
static void f_paint_with_alpha(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE alpha;
get_all_args("paint_with_alpha", args, "%f", &alpha);
cairo_paint_with_alpha(cc->ctx, alpha);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context new_path()
*/
static void f_new_path(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_new_path(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context rectangle(float x,float y,float width,float height)
*/
static void f_rectangle(INT32 args)
{
FLOAT_TYPE x, y, width, height;
get_all_args("rectangle", args, "%F%F%F%F", &x, &y, &width, &height);
pop_n_elems(args);
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_rectangle(cc->ctx, x, y, width, height);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context set_line_width(float width)
*/
static void f_set_line_width(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE width;
get_all_args("set_line_width", args, "%F", &width);
pop_n_elems(args);
cairo_set_line_width(cc->ctx, width);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl void stroke()
*!
*! @note
*! This function does not return its own object. This is because the
*! path is cleared from the cairo context when stroke is called, and
*! nesting of this command makes little sense.
*/
static void f_stroke(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_stroke(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
}
/*! @decl Cairo.Path copy_path()
*/
static void f_copy_path(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct cairo_mod_path *ps;
struct object *path_obj;
path_obj = clone_object(cairo_mod_path_program, 0);
ps = (struct cairo_mod_path*)get_storage(path_obj, cairo_mod_path_program);
assert(ps);
ps->path = cairo_copy_path(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
push_object(path_obj);
}
/*! @decl Cairo.Path copy_path_flat()
*/
static void f_copy_path_flat(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct cairo_mod_path *ps;
struct object *path_obj;
path_obj = clone_object(cairo_mod_path_program, 0);
ps = (struct cairo_mod_path*)get_storage(path_obj, cairo_mod_path_program);
assert(ps);
ps->path = cairo_copy_path_flat(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
push_object(path_obj);
}
/*! @decl Cairo.Context append_path(Cairo.Path)
*/
static void f_append_path(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct cairo_mod_path *ps;
struct object *path_obj;
get_all_args("append_path", args, "%o", &path_obj);
if (!(ps = (struct cairo_mod_path*)get_storage(path_obj, cairo_mod_path_program)))
Pike_error("Object is not a Cairo.Path");
cairo_append_path(cc->ctx, ps->path);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
push_object(path_obj);
}
/*! @decl Cairo.Context transform(Cairo.Matrix matrix)
*/
static void f_transform(INT32 args)
{
struct object *o;
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_matrix_t *m;
get_all_args("transform", args, "%o", &o);
pop_n_elems(args);
if (!(m = (cairo_matrix_t *)get_storage(o, cairo_mod_matrix_program)))
Pike_error("Object is not Matrix");
cairo_transform(cc->ctx, m);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context identity_matrix()
*! Reset the transformation matrix on this context to the identity matrix.
*/
static void f_identity_matrix(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
cairo_identity_matrix(cc->ctx);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context select_font_face(string font_family, int slant, int weight)
*! Select font.
*!
*! Selects a family and style of font from a simplified description as
*! a family name, slant and weight. This function is meant to be used
*! only for applications with simple font needs: Cairo doesn't provide
*! for operations such as listing all available fonts on the system,
*! and it is expected that most applications will need to use a more
*! comprehensive font handling and text layout library in addition to
*! cairo.
*/
static void f_select_font_face(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
int slant;
int weight;
struct pike_string *data;
get_all_args("select_font_face", args, "%t%d%d", &data, &slant, &weight);
pop_n_elems(args);
// FIXME: convert data to utf-8?
cairo_select_font_face(cc->ctx, (const char *)data->str, slant, weight);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context set_font_size(float|int size)
*! Sets the current font matrix to a scale by a factor of @[size], replacing
*! any font matrix previously set with cairo_set_font_size() or
*! cairo_set_font_matrix(). This results in a font size of size user space
*! units. (More precisely, this matrix will result in the font's
*! em-square being a @[size] by @[size] square in user space.)
*/
static void f_set_font_size(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE size;
get_all_args("set_font_size", args, "%F", &size);
pop_n_elems(args);
cairo_set_font_size(cc->ctx, size);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context set_font_matrix(Cairo.Matrix)
*/
static void f_set_font_matrix(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object *o=NULL;
cairo_matrix_t *m=NULL;
get_all_args("set_font_matrix", args, "%o", &o);
if (!(m = (cairo_matrix_t *)get_storage(o, cairo_mod_matrix_program)))
Pike_error("Object is not Matrix");
cairo_set_font_matrix (cc->ctx, m);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Matrix get_font_matrix()
*/
static void f_get_font_matrix(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object *matrix_object = NULL;
cairo_matrix_t* matrix;
matrix_object = clone_object(cairo_mod_matrix_program, 0);
matrix = (cairo_matrix_t*)get_storage(matrix_object,
cairo_mod_matrix_program);
assert(matrix);
cairo_get_font_matrix(cc->ctx, matrix);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
push_object(matrix_object);
}
/*! @decl Cairo.Context set_font_options(Cairo.FontOptions options)
*/
static void f_set_font_options(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object *o=NULL;
struct cairo_mod_font_options* options;
get_all_args("set_font_options", args, "%o", &o);
if (!(options = (struct cairo_mod_font_options*)get_storage(o, cairo_mod_font_options_program)))
Pike_error("Object is not FontOptions");
cairo_set_font_options(cc->ctx, options->opt);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.FontOptions get_font_options()
*/
static void f_get_font_options(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct object *font_options_object = NULL;
struct cairo_mod_font_options* options;
font_options_object = clone_object(cairo_mod_font_options_program, 0);
options = (struct cairo_mod_font_options*)get_storage(font_options_object,
cairo_mod_font_options_program);
assert(options);
cairo_get_font_options(cc->ctx, options->opt);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
push_object(font_options_object);
}
/*! @decl Cairo.Context show_text(string text)
*! A drawing operator that generates the shape from a string of UTF-8
*! characters, rendered according to the current font_face, font_size
*! (font_matrix), and font_options.
*!
*! This function first computes a set of glyphs for the string of
*! text. The first glyph is placed so that its origin is at the
*! current point. The origin of each subsequent glyph is offset from
*! that of the previous glyph by the advance values of the previous
*! glyph.
*!
*! After this call the current point is moved to the origin of where
*! the next glyph would be placed in this same progression. That is,
*! the current point will be at the origin of the final glyph offset
*! by its advance values. This allows for easy display of a single
*! logical string with multiple calls to cairo_show_text().
*!
*! NOTE: The cairo_show_text() function call is part of what the
*! cairo designers call the "toy" text API. It is convenient for
*! short demos and simple programs, but it is not expected to be
*! adequate for the most serious of text-using applications. See
*! cairo_show_glyphs() for the "real" text display API in cairo.
*/
static void f_show_text(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
struct pike_string *data;
if (args > 1)
f_sprintf(args);
f_string_to_utf8(1);
get_all_args("show_text", 1, "%t", &data);
cairo_show_text (cc->ctx, (char*)data->str);
pop_n_elems(1);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl Cairo.Context set_operator(int operator)
*!
*! Sets the compositing operator to be used for all drawing
*! operations. See #cairo_operator_t for details on the semantics of
*! each available compositing operator.
*!
*! @param operator
*! A compositing operator, specified as a Cairo.CAIRO_OPERATOR_*
*/
static void f_set_operator(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE operator;
get_all_args("set_operator", args, "%d", &operator);
cairo_set_operator(cc->ctx, operator);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl int get_operator()
*!
*! Gets the current compositing operator for a cairo context.
*!
*! @returns
*! A compositing operator, specified as a Cairo.CAIRO_OPERATOR_*
*/
static void f_get_operator(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE operator;
operator = cairo_get_operator(cc->ctx);
push_int(operator);
}
/*! @decl Cairo.Context set_tolerance(float tolerance)
*/
static void f_set_tolerance(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE tolerance;
get_all_args("set_tolerance", args, "%f", &tolerance);
cairo_set_tolerance(cc->ctx, tolerance);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl int get_miter_limit()
*!
*! Gets the miter limit for a cairo context.
*!
*/
static void f_get_miter_limit(INT32 args)
{
push_float(cairo_get_miter_limit(THIS_CONTEXT->ctx));
}
/*! @decl Cairo.Context set_miter_limit(float miter_limit)
*/
static void f_set_miter_limit(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE miter_limit;
get_all_args("set_tolerance", args, "%f", &miter_limit);
cairo_set_tolerance(cc->ctx, miter_limit);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl float get_tolerance()
*!
*! Gets the current tolerance for a cairo context.
*!
*! @returns
*! The tolerance number
*/
static void f_get_tolerance(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
FLOAT_TYPE tolerance;
tolerance = cairo_get_tolerance(cc->ctx);
push_float(tolerance);
}
/*! @decl Cairo.Context set_anti_alias(int setting)
*/
static void f_set_anti_alias(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE setting;
get_all_args("set_anti_alias", args, "%d", &setting);
cairo_set_antialias(cc->ctx, setting);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl int get_anti_alias()
*!
*! Gets the anti-alias setting of the cairo context.
*!
*! @returns
*! The anti-alias setting
*/
static void f_get_anti_alias(INT32 args)
{
push_int(cairo_get_antialias(THIS_CONTEXT->ctx));
}
/*! @decl Cairo.Context set_line_cap(int setting)
*/
static void f_set_line_cap(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE setting;
get_all_args("set_line_cap", args, "%d", &setting);
cairo_set_line_cap(cc->ctx, setting);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl int get_line_cap()
*!
*! Gets the line-cap setting of the cairo context.
*!
*! @returns
*! The line-cap setting
*/
static void f_get_line_cap(INT32 args)
{
push_int(cairo_get_line_cap(THIS_CONTEXT->ctx));
}
/*! @decl Cairo.Context set_line_join(int setting)
*/
static void f_set_line_join(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE setting;
get_all_args("set_line_join", args, "%d", &setting);
cairo_set_line_join(cc->ctx, setting);
CHECK_CAIRO_CONTEXT_ERROR(cc->ctx);
ref_push_object(THIS_OBJ);
}
/*! @decl int get_line_join()
*!
*! Gets the line-join setting of the cairo context.
*!
*! @returns
*! The line-join setting
*/
static void f_get_line_join(INT32 args)
{
push_int(cairo_get_line_join(THIS_CONTEXT->ctx));
}
/*! @decl Cairo.Context set_fill_rule(int rule)
*/
static void f_set_fill_rule(INT32 args)
{
struct cairo_mod_context *cc = THIS_CONTEXT;
INT_TYPE rule;