summaryrefslogtreecommitdiff
path: root/src/py3cairo.h
blob: 590582b0ba6978b75ffd5f3615f377c4666b48d1 (plain)
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
/* -*- mode: C; c-basic-offset: 2 -*-
 *
 * Copyright © 2003 James Henstridge
 * Copyright © 2004-2011 Steven Chaplin
 *
 * This file is part of pycairo.
 *
 * Pycairo is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License version 3 as published
 * by the Free Software Foundation.
 *
 * Pycairo is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with pycairo. If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _PYCAIRO_H_
#define _PYCAIRO_H_

#include <Python.h>

#include <cairo.h>


typedef struct {
  PyObject_HEAD
  cairo_t *ctx;
  PyObject *base; /* base object used to create context, or NULL */
} PycairoContext;

typedef struct {
  PyObject_HEAD
  cairo_font_face_t *font_face;
} PycairoFontFace;

#define PycairoToyFontFace PycairoFontFace

typedef struct {
  PyObject_HEAD
  cairo_font_options_t *font_options;
} PycairoFontOptions;

typedef struct {
  PyObject_HEAD
  cairo_matrix_t matrix;
} PycairoMatrix;

typedef struct {
  PyObject_HEAD
  cairo_path_t *path;
} PycairoPath;

typedef struct {
  PyObject_HEAD
  cairo_pattern_t *pattern;
  PyObject *base; /* base object used to create pattern, or NULL */
} PycairoPattern;

#define PycairoSolidPattern   PycairoPattern
#define PycairoSurfacePattern PycairoPattern
#define PycairoGradient       PycairoPattern
#define PycairoLinearGradient PycairoPattern
#define PycairoRadialGradient PycairoPattern

typedef struct {
  PyObject_HEAD
  cairo_rectangle_int_t rectangle_int;
} PycairoRectangleInt;

typedef struct {
  PyObject_HEAD
  cairo_region_t *region;
} PycairoRegion;

typedef struct {
  PyObject_HEAD
  cairo_scaled_font_t *scaled_font;
} PycairoScaledFont;

typedef struct {
  PyObject_HEAD
  cairo_surface_t *surface;
  PyObject *base; /* base object used to create surface, or NULL */
  Py_buffer buffer;
} PycairoSurface;

#define PycairoImageSurface         PycairoSurface
#define PycairoPDFSurface           PycairoSurface
#define PycairoPSSurface            PycairoSurface
#define PycairoRecordingSurface     PycairoSurface
#define PycairoSVGSurface           PycairoSurface
#define PycairoWin32Surface         PycairoSurface
#define PycairoWin32PrintingSurface PycairoSurface
#define PycairoXCBSurface           PycairoSurface
#define PycairoXlibSurface          PycairoSurface

/* get C object out of the Python wrapper */
#define PycairoContext_GET(obj)    (((PycairoContext *)(obj))->ctx)

/* Define structure for C API. */
typedef struct {
  /* (type object, constructor) pairs */
  PyTypeObject *Context_Type;
  PyObject *(*Context_FromContext)(cairo_t *ctx, PyTypeObject *type,
				   PyObject *base);
  PyTypeObject *FontFace_Type;
  PyTypeObject *ToyFontFace_Type;
  PyObject *(*FontFace_FromFontFace)(cairo_font_face_t *font_face);
  PyTypeObject *FontOptions_Type;
  PyObject *(*FontOptions_FromFontOptions)(
					   cairo_font_options_t *font_options);
  PyTypeObject *Matrix_Type;
  PyObject *(*Matrix_FromMatrix)(const cairo_matrix_t *matrix);
  PyTypeObject *Path_Type;
  PyObject *(*Path_FromPath)(cairo_path_t *path);

  PyTypeObject *Pattern_Type;
  PyTypeObject *SolidPattern_Type;
  PyTypeObject *SurfacePattern_Type;
  PyTypeObject *Gradient_Type;
  PyTypeObject *LinearGradient_Type;
  PyTypeObject *RadialGradient_Type;
  PyObject *(*Pattern_FromPattern)(cairo_pattern_t *pattern, PyObject *base);

  PyTypeObject *ScaledFont_Type;
  PyObject *(*ScaledFont_FromScaledFont)(cairo_scaled_font_t *scaled_font);

  PyTypeObject *Surface_Type;
  PyTypeObject *ImageSurface_Type;
  PyTypeObject *PDFSurface_Type;
  PyTypeObject *PSSurface_Type;
  PyTypeObject *RecordingSurface_Type;
  PyTypeObject *SVGSurface_Type;
  PyTypeObject *Win32Surface_Type;
  PyTypeObject *Win32PrintingSurface_Type;
  PyTypeObject *XCBSurface_Type;
  PyTypeObject *XlibSurface_Type;
  PyObject *(*Surface_FromSurface)(cairo_surface_t *surface, PyObject *base);

  /* misc functions */
  int (*Check_Status)(cairo_status_t status);

  PyTypeObject *RectangleInt_Type;
  PyObject *(*RectangleInt_FromRectangleInt)(
      const cairo_rectangle_int_t *rectangle_int);

  PyTypeObject *Region_Type;
  PyObject *(*Region_FromRegion)(const cairo_region_t *region);

} Pycairo_CAPI_t;


#ifndef _INSIDE_PYCAIRO_

/* Macros for accessing the C API */
#define PycairoContext_Type         *(Pycairo_CAPI->Context_Type)
#define PycairoContext_FromContext   (Pycairo_CAPI->Context_FromContext)
#define PycairoFontFace_Type        *(Pycairo_CAPI->FontFace_Type)
#define PycairoToyFontFace_Type     *(Pycairo_CAPI->ToyFontFace_Type)
#define PycairoFontFace_FromFontFace (Pycairo_CAPI->FontFace_FromFontFace)
#define PycairoFontOptions_Type     *(Pycairo_CAPI->FontOptions_Type)
#define PycairoFontOptions_FromFontOptions \
                                    (Pycairo_CAPI->FontOptions_FromFontOptions)
#define PycairoMatrix_Type          *(Pycairo_CAPI->Matrix_Type)
#define PycairoMatrix_FromMatrix     (Pycairo_CAPI->Matrix_FromMatrix)
#define PycairoPath_Type            *(Pycairo_CAPI->Path_Type)
#define PycairoPath_FromPath         (Pycairo_CAPI->Path_FromPath)

#define PycairoPattern_Type         *(Pycairo_CAPI->Pattern_Type)
#define PycairoSolidPattern_Type    *(Pycairo_CAPI->SolidPattern_Type)
#define PycairoSurfacePattern_Type  *(Pycairo_CAPI->SurfacePattern_Type)
#define PycairoGradient_Type        *(Pycairo_CAPI->Gradient_Type)
#define PycairoLinearGradient_Type  *(Pycairo_CAPI->LinearGradient_Type)
#define PycairoRadialGradient_Type  *(Pycairo_CAPI->RadialGradient_Type)
#define PycairoPattern_FromPattern   (Pycairo_CAPI->Pattern_FromPattern)

#define PycairoRectangleInt_Type    *(Pycairo_CAPI->RectangleInt_Type)
#define PycairoRectangleInt_FromRectangleInt  \
                                  (Pycairo_CAPI->RectangleInt_FromRectangleInt)

#define PycairoRegion_Type          *(Pycairo_CAPI->Region_Type)
#define PycairoRegion_FromRegion     (Pycairo_CAPI->Region_FromRegion)

#define PycairoScaledFont_Type      *(Pycairo_CAPI->ScaledFont_Type)
#define PycairoScaledFont_FromScaledFont \
                                     (Pycairo_CAPI->ScaledFont_FromScaledFont)

#define PycairoSurface_Type         *(Pycairo_CAPI->Surface_Type)
#define PycairoImageSurface_Type    *(Pycairo_CAPI->ImageSurface_Type)

#if CAIRO_HAS_PDF_SURFACE
#define PycairoPDFSurface_Type      *(Pycairo_CAPI->PDFSurface_Type)
#endif

#if CAIRO_HAS_PS_SURFACE
#define PycairoPSSurface_Type       *(Pycairo_CAPI->PSSurface_Type)
#endif

#if CAIRO_HAS_RECORDING_SURFACE
#define PycairoRecordingSurface_Type \
                                    *(Pycairo_CAPI->RecordingSurface_Type)
#endif

#if CAIRO_HAS_SVG_SURFACE
#define PycairoSVGSurface_Type      *(Pycairo_CAPI->SVGSurface_Type)
#endif

#if CAIRO_HAS_WIN32_SURFACE
#define PycairoWin32Surface_Type    *(Pycairo_CAPI->Win32Surface_Type)
#define PycairoWin32PrintingSurface_Type    *(Pycairo_CAPI->Win32PrintingSurface_Type)
#endif

#if CAIRO_HAS_XCB_SURFACE
#define PycairoXCBSurface_Type      *(Pycairo_CAPI->XCBSurface_Type)
#endif

#if CAIRO_HAS_XLIB_SURFACE
#define PycairoXlibSurface_Type     *(Pycairo_CAPI->XlibSurface_Type)
#endif

#define PycairoSurface_FromSurface   (Pycairo_CAPI->Surface_FromSurface)

#define Pycairo_Check_Status         (Pycairo_CAPI->Check_Status)


/* To access the Pycairo C API, the client module should call 'import_cairo()'
 * from the init<module> function, and check the return value, < 0 means the
 * import failed.
 */
static Pycairo_CAPI_t *Pycairo_CAPI;

/* Return -1 on error, 0 on success.
 * PyCapsule_Import will set an exception if there's an error.
 */
static int
import_cairo(void)
{
  Pycairo_CAPI = (Pycairo_CAPI_t*) PyCapsule_Import("cairo.CAPI", 0);
  return (Pycairo_CAPI != 0) ? 0 : -1;
}

#endif /* ifndef _INSIDE_PYCAIRO_ */

#endif /* ifndef _PYCAIRO_H_ */