-
Notifications
You must be signed in to change notification settings - Fork 2
/
aggdraw.cxx
1749 lines (1483 loc) · 46.9 KB
/
aggdraw.cxx
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
/*
* AGG Draw Library
* $Id: aggdraw.cxx 2550 2005-10-10 12:54:55Z fredrik $
*
* WCK-style drawing using the AGG library.
*
* history:
* 2004-09-14 fl created, based on experimental code
* 2004-09-15 fl added pen/brush objects (from ironpil), multiple modes
* 2004-09-16 fl added text, arc/ellipse support
* 2005-03-25 fl added BGRA support
* 2005-05-02 fl added (experimental) symbol support
* 2005-05-12 fl added image constructor and flush method
* 2005-05-18 fl fixed possible image constructor crash
* 2005-05-18 fl make sure to keep a reference to the image
* 2005-05-19 fl improved symbol path support
* 2005-06-12 fl added support for S and T path operators
* 2005-06-15 fl added support for outline fonts
* 2005-06-15 fl support settransform for basic primitives and text
* 2005-06-19 fl use ImageColor.getrgb to resolve colors
* 2005-06-30 fl added Path object (stub)
* 2005-07-04 fl added Path methods (moveto, lineto, etc)
* 2005-07-05 fl added Path support to the line and polygon primitives
* 2005-08-10 fl fixed Draw(im) buffer memory leak (ouch!)
* 2005-08-20 fl fixed background color setting for RGB modes
* 2005-08-30 fl expand polygons by 0.5 pixels by default (experimental)
* 2005-08-30 fl fixed proper clipping in rasterizer
* 2005-09-23 fl added antialias setting
* 2005-09-24 fl don't recreate draw adaptor for each operation
* 2005-09-26 fl added coords method to Path type
* 2005-10-10 fl fixed broken add_path calls in symbol renderer
*
* Copyright (c) 2003-2005 by Secret Labs AB
*/
#define VERSION "1.1"
#include "Python.h"
#ifndef M_PI
#define M_PI 3.1415926535897931
#endif
#if defined(PY_VERSION_HEX) && PY_VERSION_HEX >= 0x01060000
#if PY_VERSION_HEX < 0x02020000 || defined(Py_USING_UNICODE)
/* defining this enables unicode support (default under 1.6a1 and later) */
#define HAVE_UNICODE
#endif
#endif
/* agg2 components */
#include "agg_arc.h"
#include "agg_conv_contour.h"
#include "agg_conv_curve.h"
// #include "agg_conv_dash.h"
#include "agg_conv_stroke.h"
#include "agg_conv_transform.h"
#include "agg_ellipse.h"
#if defined(HAVE_FREETYPE2)
#include "agg_font_freetype.h"
#endif
#include "agg_path_storage.h"
#include "agg_pixfmt_gray8.h"
#include "agg_pixfmt_rgb24.h"
#include "agg_pixfmt_rgba32.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_renderer_scanline.h"
#include "agg_rendering_buffer.h"
#include "agg_scanline_p.h"
#include "platform/agg_platform_support.h" // agg::pix_format_*
/* -------------------------------------------------------------------- */
/* AGG Drawing Surface */
#if defined(HAVE_FREETYPE2)
typedef agg::font_engine_freetype_int32 font_engine_type;
typedef agg::font_cache_manager<font_engine_type> font_manager_type;
static font_engine_type font_engine;
static font_manager_type font_manager(font_engine);
#endif
/* forward declaration */
class draw_adaptor_base;
template<class PixFmt> class draw_adaptor;
typedef struct {
PyObject_HEAD
draw_adaptor_base *draw;
agg::rendering_buffer* buffer;
agg::trans_affine* transform;
unsigned char* buffer_data;
int mode; // agg::pix_format_*
int xsize, ysize;
int buffer_size;
PyObject* image;
} DrawObject;
/* glue functions (see the init function for details) */
static PyObject* aggdraw_getcolor_obj;
static void draw_dealloc(DrawObject* self);
static PyObject* draw_getattr(DrawObject* self, char* name);
static PyTypeObject DrawType = {
PyObject_HEAD_INIT(NULL)
0, "Draw", sizeof(DrawObject), 0,
/* methods */
(destructor) draw_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc) draw_getattr, /* tp_getattr */
0, /* tp_setattr */
};
typedef struct {
PyObject_HEAD
agg::rgba8 color;
float width;
} PenObject;
static void pen_dealloc(PenObject* self);
static PyTypeObject PenType = {
PyObject_HEAD_INIT(NULL)
0, "Pen", sizeof(PenObject), 0,
/* methods */
(destructor) pen_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
};
#define Pen_Check(op) ((op) != NULL && (op)->ob_type == &PenType)
typedef struct {
PyObject_HEAD
agg::rgba8 color;
} BrushObject;
static void brush_dealloc(BrushObject* self);
static PyTypeObject BrushType = {
PyObject_HEAD_INIT(NULL)
0, "Brush", sizeof(BrushObject), 0,
/* methods */
(destructor) brush_dealloc, /* tp_dealloc */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
};
#define Brush_Check(op) ((op) != NULL && (op)->ob_type == &BrushType)
typedef struct {
PyObject_HEAD
char* filename;
float height;
agg::rgba8 color;
} FontObject;
#if defined(HAVE_FREETYPE2)
static FT_Face font_activate(FontObject* font, bool outline=false);
#endif
static void font_dealloc(FontObject* self);
static PyObject* font_getattr(FontObject* self, char* name);
static PyTypeObject FontType = {
PyObject_HEAD_INIT(NULL)
0, "Font", sizeof(FontObject), 0,
/* methods */
(destructor) font_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc) font_getattr, /* tp_getattr */
0, /* tp_setattr */
};
#define Font_Check(op) ((op) != NULL && (op)->ob_type == &FontType)
typedef struct {
PyObject_HEAD
agg::path_storage* path;
} PathObject;
static void path_dealloc(PathObject* self);
static PyObject* path_getattr(PathObject* self, char* name);
static PyTypeObject PathType = {
PyObject_HEAD_INIT(NULL)
0, "Path", sizeof(PathObject), 0,
/* methods */
(destructor) path_dealloc, /* tp_dealloc */
0, /* tp_print */
(getattrfunc) path_getattr, /* tp_getattr */
0, /* tp_setattr */
};
#define Path_Check(op) ((op) != NULL && (op)->ob_type == &PathType)
static agg::rgba8 getcolor(PyObject* color, int opacity=255);
/* -------------------------------------------------------------------- */
/* This template class is used to automagically instantiate drawing
code for all pixel formats used by the library. */
class draw_adaptor_base
{
public:
char* mode;
virtual void setantialias(bool flag) = 0;
virtual void draw(agg::path_storage &path, PyObject* obj1,
PyObject* obj2=NULL) = 0;
virtual void drawtext(float xy[2], PyObject* text, FontObject* font) {};
};
template<class PixFmt> class draw_adaptor : public draw_adaptor_base {
DrawObject* self;
typedef agg::renderer_base<PixFmt> renderer_base;
typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_aa;
agg::rasterizer_scanline_aa<> rasterizer;
agg::scanline_p8 scanline;
public:
draw_adaptor(DrawObject* self_, char* mode_)
{
self = self_;
mode = mode_;
setantialias(true);
rasterizer.clip_box(0,0, self->xsize, self->ysize);
}
void setantialias(bool flag)
{
if (flag)
rasterizer.gamma(agg::gamma_linear());
else
rasterizer.gamma(agg::gamma_threshold(0.5));
};
void draw(agg::path_storage &path, PyObject* obj1, PyObject* obj2=NULL)
{
PixFmt pf(*self->buffer);
renderer_base rb(pf);
renderer_aa renderer(rb);
agg::path_storage* p;
PenObject* pen;
if (Pen_Check(obj1))
pen = (PenObject*) obj1;
else if (Pen_Check(obj2))
pen = (PenObject*) obj2;
else
pen = NULL;
BrushObject* brush;
if (Brush_Check(obj2))
brush = (BrushObject*) obj2;
else if (Brush_Check(obj1))
brush = (BrushObject*) obj1;
else
brush = NULL;
if (self->transform) {
p = new agg::path_storage();
agg::conv_transform<agg::path_storage, agg::trans_affine>
tp(path, *self->transform);
p->add_path(tp, 0, false);
} else
p = &path;
if (brush) {
/* interior */
agg::conv_contour<agg::path_storage> contour(*p);
contour.auto_detect_orientation(true);
if (pen)
contour.width(pen->width / 2.0);
else
contour.width(0.5);
rasterizer.reset();
rasterizer.add_path(contour);
renderer.color(brush->color);
agg::render_scanlines(rasterizer, scanline, renderer);
}
if (pen) {
/* outline */
/* FIXME: add path for dashed lines */
agg::conv_stroke<agg::path_storage> stroke(*p);
stroke.width(pen->width);
rasterizer.reset();
rasterizer.add_path(stroke);
renderer.color(pen->color);
agg::render_scanlines(rasterizer, scanline, renderer);
}
if (self->transform)
delete p;
}
#if defined(HAVE_FREETYPE2)
void drawtext(float xy[2], PyObject* text, FontObject* font)
{
PixFmt pf(*self->buffer);
renderer_base rb(pf);
renderer_aa renderer(rb);
typedef agg::conv_curve<font_manager_type::path_adaptor_type> curve_t;
curve_t curves(font_manager.path_adaptor());
bool outline = (self->transform != NULL);
FT_Face face = font_activate(font, outline);
if (!face)
return;
double x = xy[0];
double y = xy[1] + face->size->metrics.ascender/64.0;
renderer.color(font->color);
curves.approximation_scale(1);
unsigned long ch;
int index = 0;
while (text_getchar(text, index, &ch)) {
const agg::glyph_cache* glyph;
glyph = font_manager.glyph(ch);
if (!glyph)
continue;
font_manager.add_kerning(&x, &y);
font_manager.init_embedded_adaptors(glyph, x, y);
if (outline) {
rasterizer.reset();
if (self->transform) {
agg::conv_transform<curve_t, agg::trans_affine>
tp(curves, *self->transform);
rasterizer.add_path(tp);
} else
rasterizer.add_path(curves);
agg::render_scanlines(rasterizer, scanline, renderer);
} else {
agg::render_scanlines(
font_manager.gray8_adaptor(),
font_manager.gray8_scanline(), renderer
);
}
x += glyph->advance_x;
y += glyph->advance_y;
index++;
}
}
#endif
};
/* -------------------------------------------------------------------- */
static PyObject*
draw_new(PyObject* self_, PyObject* args)
{
DrawObject* self;
char buffer[10];
int ok;
PyObject* image;
char* mode;
int xsize, ysize;
PyObject* color = NULL;
if (PyArg_ParseTuple(args, "O:Draw", &image)) {
/* get mode (use a local buffer to avoid GC issues) */
PyObject* mode_obj = PyObject_GetAttrString(image, "mode");
if (!mode_obj)
return NULL;
if (PyString_Check(mode_obj)) {
strncpy(buffer, PyString_AS_STRING(mode_obj), sizeof buffer);
buffer[sizeof(buffer)-1] = '\0'; /* to be on the safe side */
mode = buffer;
} else
mode = NULL;
Py_DECREF(mode_obj);
if (!mode) {
PyErr_SetString(
PyExc_TypeError,
"bad 'mode' attribute (expected string)"
);
return NULL;
}
PyObject* size_obj = PyObject_GetAttrString(image, "size");
if (!size_obj)
return NULL;
if (PyTuple_Check(size_obj))
ok = PyArg_ParseTuple(size_obj, "ii", &xsize, &ysize);
else {
PyErr_SetString(
PyExc_TypeError,
"bad 'size' attribute (expected 2-tuple)"
);
ok = 0;
}
Py_DECREF(size_obj);
if (!ok)
return NULL;
} else {
PyErr_Clear();
if (!PyArg_ParseTuple(args, "s(ii)|O:Draw",
&mode, &xsize, &ysize, &color))
return NULL;
image = NULL;
}
self = PyObject_NEW(DrawObject, &DrawType);
if (self == NULL)
return NULL;
int stride;
if (!strcmp(mode, "L")) {
self->mode = agg::pix_format_gray8;
stride = xsize;
} else if (!strcmp(mode, "RGB")) {
self->mode = agg::pix_format_rgb24;
stride = xsize * 3;
} else if (!strcmp(mode, "BGR")) {
self->mode = agg::pix_format_bgr24;
stride = xsize * 3;
} else if (!strcmp(mode, "RGBA")) {
self->mode = agg::pix_format_rgba32;
stride = xsize * 4;
} else if (!strcmp(mode, "BGRA")) {
self->mode = agg::pix_format_bgra32;
stride = xsize * 4;
} else {
PyErr_SetString(PyExc_ValueError, "bad mode");
PyObject_DEL(self);
return NULL;
}
self->buffer_size = ysize * stride;
self->buffer_data = new unsigned char[self->buffer_size];
if (color && color != Py_None) {
agg::rgba8 ink = getcolor(color);
unsigned char* p = self->buffer_data;
int c, i;
switch (self->mode) {
case agg::pix_format_gray8:
c = (ink.r*299 + ink.g*587 + ink.b*114) / 1000;
memset(self->buffer_data, c, self->buffer_size);
break;
case agg::pix_format_rgb24:
for (i = 0; i < self->buffer_size; i += 3) {
p[i+0] = ink.r;
p[i+1] = ink.g;
p[i+2] = ink.b;
}
break;
case agg::pix_format_bgr24:
for (i = 0; i < self->buffer_size; i += 3) {
p[i+0] = ink.b;
p[i+1] = ink.g;
p[i+2] = ink.r;
}
break;
case agg::pix_format_rgba32:
for (i = 0; i < self->buffer_size; i += 4) {
p[i+0] = ink.r;
p[i+1] = ink.g;
p[i+2] = ink.b;
p[i+3] = ink.a;
}
break;
case agg::pix_format_bgra32:
for (i = 0; i < self->buffer_size; i += 4) {
p[i+0] = ink.b;
p[i+1] = ink.g;
p[i+2] = ink.r;
p[i+3] = ink.a;
}
break;
}
} else
memset(self->buffer_data, 255, self->buffer_size);
self->buffer = new agg::rendering_buffer(
self->buffer_data, xsize, ysize, stride
);
self->xsize = xsize;
self->ysize = ysize;
self->transform = NULL;
self->image = image;
if (image) {
PyObject* buffer = PyObject_CallMethod(image,
const_cast<char *>("tobytes"),
NULL);
if (!buffer)
return NULL; /* FIXME: release resources */
if (!PyString_Check(buffer)) {
PyErr_SetString(
PyExc_TypeError,
"bad 'tobytes' return value (expected string)"
);
Py_DECREF(buffer);
return NULL;
}
char* data = PyString_AS_STRING(buffer);
int data_size = PyString_GET_SIZE(buffer);
if (data_size >= self->buffer_size)
memcpy(self->buffer_data, data, self->buffer_size);
else {
PyErr_SetString(PyExc_ValueError, "not enough data");
Py_DECREF(buffer);
return NULL; /* FIXME: release resources */
}
Py_INCREF(image); /* hang on to this image */
Py_DECREF(buffer);
}
switch (self->mode) {
case agg::pix_format_gray8:
self->draw = new draw_adaptor<agg::pixfmt_gray8>(self,
const_cast<char *>("L"));
break;
case agg::pix_format_rgb24:
self->draw = new draw_adaptor<agg::pixfmt_rgb24>(self,
const_cast<char *>("RGB"));
break;
case agg::pix_format_bgr24:
self->draw = new draw_adaptor<agg::pixfmt_bgr24>(self,
const_cast<char *>("BGR"));
break;
default:
self->draw = new draw_adaptor<agg::pixfmt_rgba32>(self,
const_cast<char *>("RGBA"));
break;
}
return (PyObject*) self;
}
struct PointF {
float X;
float Y;
};
#define GETFLOAT(op)\
(PyInt_Check(op) ? (float) PyInt_AS_LONG((op)) :\
PyFloat_Check(op) ? (float) PyFloat_AS_DOUBLE((op)) :\
(float) PyFloat_AsDouble(op))
static PointF*
getpoints(PyObject* xyIn, int* count)
{
PointF *xy;
int i, n;
/* FIXME: use local buffer (provided by caller) for short
sequences */
if (!PySequence_Check(xyIn)) {
PyErr_SetString(PyExc_TypeError, "argument must be a sequence");
return NULL;
}
n = PyObject_Length(xyIn);
if (PyErr_Occurred())
return NULL;
if (n & 1) {
PyErr_SetString(PyExc_TypeError,
"expected even number of coordinates");
return NULL;
}
n /= 2;
xy = new PointF[n+1];
if (!xy) {
PyErr_NoMemory();
*count = -1;
return NULL;
}
if (PyList_Check(xyIn))
for (i = 0; i < n; i++) {
xy[i].X = GETFLOAT(PyList_GET_ITEM(xyIn, i+i));
xy[i].Y = GETFLOAT(PyList_GET_ITEM(xyIn, i+i+1));
}
else if (PyTuple_Check(xyIn))
for (i = 0; i < n; i++) {
xy[i].X = GETFLOAT(PyTuple_GET_ITEM(xyIn, i+i));
xy[i].Y = GETFLOAT(PyTuple_GET_ITEM(xyIn, i+i+1));
}
else
for (i = 0; i < n; i++) {
PyObject *op;
op = PySequence_GetItem(xyIn, i+i);
xy[i].X = GETFLOAT(op);
Py_DECREF(op);
op = PySequence_GetItem(xyIn, i+i+1);
xy[i].Y = GETFLOAT(op);
Py_DECREF(op);
}
PyErr_Clear();
*count = n;
return xy;
}
static agg::rgba8
getcolor(PyObject* color, int opacity)
{
if (PyInt_Check(color)) {
int ink = PyInt_AsLong(color);
return agg::rgba8(ink, ink, ink, opacity);
}
if (PyString_Check(color)) {
/* hex colors */
char* ink = PyString_AS_STRING(color);
if (ink[0] == '#' && strlen(ink) == 7) {
int i = strtol(ink+1, NULL, 16); /* FIXME: rough parsing */
return agg::rgba8((i>>16)&255,(i>>8)&255,i&255,opacity);
}
}
int red, green, blue, alpha = opacity;
if (PyArg_ParseTuple(color, "iii|i", &red, &green, &blue, &alpha))
return agg::rgba8(red, green, blue, alpha);
PyErr_Clear();
/* unknown color: pass it to the Python layer */
if (aggdraw_getcolor_obj) {
PyObject* result;
result = PyObject_CallFunction(aggdraw_getcolor_obj,
const_cast<char *>("O"), color);
if (result) {
int ok = PyArg_ParseTuple(result, "iii", &red, &green, &blue);
Py_DECREF(result);
if (ok)
return agg::rgba8(red, green, blue, opacity);
}
PyErr_Clear();
}
/* check for well-known color names (HTML) */
if (PyString_Check(color)) {
char* ink = PyString_AS_STRING(color);
if (!strcmp(ink, "aqua"))
return agg::rgba8(0x00,0xFF,0xFF,opacity);
if (!strcmp(ink, "black"))
return agg::rgba8(0x00,0x00,0x00,opacity);
if (!strcmp(ink, "blue"))
return agg::rgba8(0x00,0x00,0xFF,opacity);
if (!strcmp(ink, "fuchsia"))
return agg::rgba8(0xFF,0x00,0xFF,opacity);
if (!strcmp(ink, "gray"))
return agg::rgba8(0x80,0x80,0x80,opacity);
if (!strcmp(ink, "green"))
return agg::rgba8(0x00,0x80,0x00,opacity);
if (!strcmp(ink, "lime"))
return agg::rgba8(0x00,0xFF,0x00,opacity);
if (!strcmp(ink, "maroon"))
return agg::rgba8(0x80,0x00,0x00,opacity);
if (!strcmp(ink, "navy"))
return agg::rgba8(0x00,0x00,0x80,opacity);
if (!strcmp(ink, "olive"))
return agg::rgba8(0x80,0x80,0x00,opacity);
if (!strcmp(ink, "purple"))
return agg::rgba8(0x80,0x00,0x80,opacity);
if (!strcmp(ink, "red"))
return agg::rgba8(0xFF,0x00,0x00,opacity);
if (!strcmp(ink, "silver"))
return agg::rgba8(0xC0,0xC0,0xC0,opacity);
if (!strcmp(ink, "teal"))
return agg::rgba8(0x00,0x80,0x80,opacity);
if (!strcmp(ink, "white"))
return agg::rgba8(0xFF,0xFF,0xFF,opacity);
if (!strcmp(ink, "yellow"))
return agg::rgba8(0xFF,0xFF,0x00,opacity);
}
/* default to black (FIXME: raise an exception instead?) */
return agg::rgba8(0, 0, 0, opacity);
}
#if defined(HAVE_FREETYPE2)
static int
text_getchar(PyObject* string, int index, unsigned long* char_out)
{
#if defined(HAVE_UNICODE)
if (PyUnicode_Check(string)) {
Py_UNICODE* p = PyUnicode_AS_UNICODE(string);
int size = PyUnicode_GET_SIZE(string);
if (index >= size)
return 0;
*char_out = p[index];
return 1;
}
#endif
if (PyString_Check(string)) {
unsigned char* p = (unsigned char*) PyString_AS_STRING(string);
int size = PyString_GET_SIZE(string);
if (index >= size)
return 0;
*char_out = (unsigned char) p[index];
return 1;
}
return 0;
}
#endif
/* -------------------------------------------------------------------- */
static PyObject*
draw_arc(DrawObject* self, PyObject* args)
{
float x0, y0, x1, y1;
float start, end;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "(ffff)ff|O:arc",
&x0, &y0, &x1, &y1, &start, &end, &pen))
return NULL;
agg::path_storage path;
agg::arc arc(
(x1+x0)/2, (y1+y0)/2, (x1-x0)/2, (y1-y0)/2,
-start * (float) (M_PI / 180.0), -end * (float) (M_PI / 180.0),
false
);
arc.approximation_scale(1);
path.add_path(arc);
self->draw->draw(path, pen);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_chord(DrawObject* self, PyObject* args)
{
float x0, y0, x1, y1;
float start, end;
PyObject* pen = NULL;
PyObject* brush = NULL;
if (!PyArg_ParseTuple(args, "(ffff)ff|OO:chord",
&x0, &y0, &x1, &y1, &start, &end, &pen, &brush))
return NULL;
agg::path_storage path;
agg::arc arc(
(x1+x0)/2, (y1+y0)/2, (x1-x0)/2, (y1-y0)/2,
-start * (float) (M_PI / 180.0), -end * (float) (M_PI / 180.0),
false
);
arc.approximation_scale(1);
path.add_path(arc);
path.close_polygon();
self->draw->draw(path, pen, brush);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_ellipse(DrawObject* self, PyObject* args)
{
float x0, y0, x1, y1;
PyObject* brush = NULL;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "(ffff)|OO:ellipse",
&x0, &y0, &x1, &y1, &brush, &pen))
return NULL;
agg::path_storage path;
agg::ellipse ellipse((x1+x0)/2, (y1+y0)/2, (x1-x0)/2, (y1-y0)/2, 8);
ellipse.approximation_scale(1);
path.add_path(ellipse);
self->draw->draw(path, pen, brush);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_line(DrawObject* self, PyObject* args)
{
PyObject* xyIn;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "O|O:line", &xyIn, &pen))
return NULL;
if (Path_Check(xyIn)) {
self->draw->draw(*((PathObject*) xyIn)->path, pen);
} else {
int count;
PointF *xy = getpoints(xyIn, &count);
if (!xy)
return NULL;
agg::path_storage path;
path.move_to(xy[0].X, xy[0].Y);
for (int i = 1; i < count; i++)
path.line_to(xy[i].X, xy[i].Y);
delete xy;
self->draw->draw(path, pen);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_pieslice(DrawObject* self, PyObject* args)
{
float x0, y0, x1, y1;
float start, end;
PyObject* pen = NULL;
PyObject* brush = NULL;
if (!PyArg_ParseTuple(args, "(ffff)ff|OO:pieslice",
&x0, &y0, &x1, &y1, &start, &end, &pen, &brush))
return NULL;
float x = (x1+x0)/2;
float y = (y1+y0)/2;
agg::path_storage path;
agg::arc arc(
x, y, (x1-x0)/2, (y1-y0)/2,
-start * (float) (M_PI / 180.0), -end * (float) (M_PI / 180.0),
false
);
arc.approximation_scale(1);
path.add_path(arc);
path.line_to(x, y);
path.close_polygon();
self->draw->draw(path, pen, brush);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_polygon(DrawObject* self, PyObject* args)
{
PyObject* xyIn;
PyObject* brush = NULL;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "O|OO:polygon", &xyIn, &brush, &pen))
return NULL;
if (Path_Check(xyIn)) {
self->draw->draw(*((PathObject*) xyIn)->path, pen, brush);
} else {
int count;
PointF *xy = getpoints(xyIn, &count);
if (!xy)
return NULL;
agg::path_storage path;
path.move_to(xy[0].X, xy[0].Y);
for (int i = 1; i < count; i++)
path.line_to(xy[i].X, xy[i].Y);
path.close_polygon();
delete xy;
self->draw->draw(path, pen, brush);
}
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_rectangle(DrawObject* self, PyObject* args)
{
float x0, y0, x1, y1;
PyObject* brush = NULL;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "(ffff)|OO:rectangle",
&x0, &y0, &x1, &y1, &brush, &pen))
return NULL;
agg::path_storage path;
path.move_to(x0, y0);
path.line_to(x1, y0);
path.line_to(x1, y1);
path.line_to(x0, y1);
path.close_polygon();
self->draw->draw(path, pen, brush);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_symbol(DrawObject* self, PyObject* args)
{
PyObject* xyIn;
PathObject* symbol;
PyObject* brush = NULL;
PyObject* pen = NULL;
if (!PyArg_ParseTuple(args, "OO!|OO:symbol",
&xyIn, &PathType, &symbol, &brush, &pen))
return NULL;
int count;
PointF *xy = getpoints(xyIn, &count);
if (!xy)
return NULL;
for (int i = 0; i < count; i++) {
agg::trans_affine_translation transform(xy[i].X,xy[i].Y);
agg::conv_transform<agg::path_storage, agg::trans_affine>
tp(*symbol->path, transform);
agg::path_storage p;
p.add_path(tp, 0, false);
self->draw->draw(p, pen, brush);
}
delete xy;
Py_INCREF(Py_None);
return Py_None;
}
#if defined(HAVE_FREETYPE2)
static PyObject*
draw_text(DrawObject* self, PyObject* args)
{
float xy[2];
PyObject* text;
FontObject* font;
if (!PyArg_ParseTuple(args, "(ff)OO!:text", xy+0, xy+1, &text,
&FontType, &font))
return NULL;
self->draw->drawtext(xy, text, font);
Py_INCREF(Py_None);
return Py_None;
}
#endif
#if defined(HAVE_FREETYPE2)
static PyObject*
draw_textsize(DrawObject* self, PyObject* args)
{
PyObject* text;
FontObject* font;
if (!PyArg_ParseTuple(args, "OO!:text", &text, &FontType, &font))
return NULL;
FT_Face face = font_activate(font);
if (!face) {
Py_INCREF(Py_None);
return Py_None;
}
int x, i;
unsigned long ch;
for (x = i = 0; text_getchar(text, i, &ch); i++) {
int index = FT_Get_Char_Index(face, ch);
int error = FT_Load_Glyph(face, index, FT_LOAD_DEFAULT);
x += face->glyph->metrics.horiAdvance;
}
return Py_BuildValue("ff", x/64.0, face->size->metrics.height/64.0);
}
#endif
static PyObject*
draw_setantialias(DrawObject* self, PyObject* args)
{
int i;
if (!PyArg_ParseTuple(args, "i:setantialias", &i))
return NULL;
self->draw->setantialias(i != 0);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject*
draw_settransform(DrawObject* self, PyObject* args)
{
double a=1, b=0, c=0, d=0, e=1, f=0;
if (!PyArg_ParseTuple(args, "|(dd):settransform", &c, &f)) {