PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/src/platform/osx/pycairo/cairomodule.c

https://github.com/curaloucura/Enso-Ubuntu
C | 476 lines | 382 code | 54 blank | 40 comment | 28 complexity | e8b64aa9109ce33631cb9386155552b4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. /* -*- mode: C; c-basic-offset: 4 -*-
  2. *
  3. * Pycairo - Python bindings for cairo
  4. *
  5. * Copyright © 2003-2005 James Henstridge
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it either under the terms of the GNU Lesser General Public
  9. * License version 2.1 as published by the Free Software Foundation
  10. * (the "LGPL") or, at your option, under the terms of the Mozilla
  11. * Public License Version 1.1 (the "MPL"). If you do not alter this
  12. * notice, a recipient may use your version of this file under either
  13. * the MPL or the LGPL.
  14. *
  15. * You should have received a copy of the LGPL along with this library
  16. * in the file COPYING-LGPL-2.1; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. * You should have received a copy of the MPL along with this library
  19. * in the file COPYING-MPL-1.1
  20. *
  21. * The contents of this file are subject to the Mozilla Public License
  22. * Version 1.1 (the "License"); you may not use this file except in
  23. * compliance with the License. You may obtain a copy of the License at
  24. * http://www.mozilla.org/MPL/
  25. *
  26. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
  27. * OF ANY KIND, either express or implied. See the LGPL or the MPL for
  28. * the specific language governing rights and limitations.
  29. */
  30. #define PY_SSIZE_T_CLEAN
  31. #include <Python.h>
  32. #ifdef HAVE_CONFIG_H
  33. # include <config.h>
  34. #endif
  35. #include "pycairo-private.h"
  36. #define VERSION_MAJOR 1
  37. #define VERSION_MINOR 4
  38. #define VERSION_MICRO 12
  39. static char pycairo_version_string[] = "1.4.12";
  40. /* A module specific exception */
  41. PyObject *CairoError = NULL;
  42. int
  43. Pycairo_Check_Status (cairo_status_t status)
  44. {
  45. if (PyErr_Occurred() != NULL)
  46. return 1;
  47. switch (status) {
  48. case CAIRO_STATUS_SUCCESS:
  49. return 0;
  50. /* if appropriate - translate the status string into Python,
  51. * else - use cairo_status_to_string()
  52. */
  53. case CAIRO_STATUS_NO_MEMORY:
  54. PyErr_NoMemory();
  55. break;
  56. case CAIRO_STATUS_READ_ERROR:
  57. case CAIRO_STATUS_WRITE_ERROR:
  58. PyErr_SetString(PyExc_IOError, cairo_status_to_string (status));
  59. break;
  60. case CAIRO_STATUS_INVALID_RESTORE:
  61. PyErr_SetString(CairoError, "Context.restore without matching "
  62. "Context.save");
  63. break;
  64. case CAIRO_STATUS_INVALID_POP_GROUP:
  65. PyErr_SetString(CairoError, "Context.pop_group without matching "
  66. "Context.push_group");
  67. break;
  68. default:
  69. PyErr_SetString(CairoError, cairo_status_to_string (status));
  70. }
  71. return 1;
  72. }
  73. /* C API. Clients get at this via Pycairo_IMPORT, defined in pycairo.h.
  74. */
  75. static Pycairo_CAPI_t CAPI = {
  76. &PycairoContext_Type, PycairoContext_FromContext,
  77. &PycairoFontFace_Type, PycairoFontFace_FromFontFace,
  78. &PycairoFontOptions_Type, PycairoFontOptions_FromFontOptions,
  79. &PycairoMatrix_Type, PycairoMatrix_FromMatrix,
  80. &PycairoPath_Type, PycairoPath_FromPath,
  81. &PycairoPattern_Type,
  82. &PycairoSolidPattern_Type,
  83. &PycairoSurfacePattern_Type,
  84. &PycairoGradient_Type,
  85. &PycairoLinearGradient_Type,
  86. &PycairoRadialGradient_Type,
  87. PycairoPattern_FromPattern,
  88. &PycairoScaledFont_Type, PycairoScaledFont_FromScaledFont,
  89. &PycairoSurface_Type,
  90. &PycairoImageSurface_Type,
  91. #ifdef CAIRO_HAS_PDF_SURFACE
  92. &PycairoPDFSurface_Type,
  93. #else
  94. 0,
  95. #endif
  96. #ifdef CAIRO_HAS_PS_SURFACE
  97. &PycairoPSSurface_Type,
  98. #else
  99. 0,
  100. #endif
  101. #ifdef CAIRO_HAS_SVG_SURFACE
  102. &PycairoSVGSurface_Type,
  103. #else
  104. 0,
  105. #endif
  106. #ifdef CAIRO_HAS_WIN32_SURFACE
  107. &PycairoWin32Surface_Type,
  108. #else
  109. 0,
  110. #endif
  111. #ifdef CAIRO_HAS_XLIB_SURFACE
  112. &PycairoXlibSurface_Type,
  113. #else
  114. 0,
  115. #endif
  116. #ifdef CAIRO_HAS_QUARTZ_SURFACE
  117. &PycairoQuartzSurface_Type,
  118. #else
  119. 0,
  120. #endif
  121. PycairoSurface_FromSurface,
  122. Pycairo_Check_Status,
  123. };
  124. static PyObject *
  125. pycairo_cairo_version (PyObject *self)
  126. {
  127. return PyInt_FromLong (cairo_version());
  128. }
  129. static PyObject *
  130. pycairo_cairo_version_string (PyObject *self)
  131. {
  132. return PyString_FromString (cairo_version_string());
  133. }
  134. static PyMethodDef cairo_functions[] = {
  135. {"cairo_version", (PyCFunction)pycairo_cairo_version, METH_NOARGS},
  136. {"cairo_version_string", (PyCFunction)pycairo_cairo_version_string,
  137. METH_NOARGS},
  138. {NULL, NULL, 0, NULL},
  139. };
  140. DL_EXPORT(void)
  141. init_cairo(void)
  142. {
  143. PyObject *m;
  144. PycairoContext_Type.tp_base = &PyBaseObject_Type;
  145. if (PyType_Ready(&PycairoContext_Type) < 0)
  146. return;
  147. PycairoFontFace_Type.tp_base = &PyBaseObject_Type;
  148. if (PyType_Ready(&PycairoFontFace_Type) < 0)
  149. return;
  150. PycairoFontOptions_Type.tp_base = &PyBaseObject_Type;
  151. if (PyType_Ready(&PycairoFontOptions_Type) < 0)
  152. return;
  153. PycairoMatrix_Type.tp_base = &PyBaseObject_Type;
  154. if (PyType_Ready(&PycairoMatrix_Type) < 0)
  155. return;
  156. PycairoPath_Type.tp_base = &PyBaseObject_Type;
  157. if (PyType_Ready(&PycairoPath_Type) < 0)
  158. return;
  159. PycairoPathiter_Type.tp_iter=&PyObject_SelfIter;
  160. PycairoPattern_Type.tp_base = &PyBaseObject_Type;
  161. if (PyType_Ready(&PycairoPattern_Type) < 0)
  162. return;
  163. PycairoSolidPattern_Type.tp_base = &PycairoPattern_Type;
  164. if (PyType_Ready(&PycairoSolidPattern_Type) < 0)
  165. return;
  166. PycairoSurfacePattern_Type.tp_base = &PycairoPattern_Type;
  167. if (PyType_Ready(&PycairoSurfacePattern_Type) < 0)
  168. return;
  169. PycairoGradient_Type.tp_base = &PycairoPattern_Type;
  170. if (PyType_Ready(&PycairoGradient_Type) < 0)
  171. return;
  172. PycairoLinearGradient_Type.tp_base = &PycairoGradient_Type;
  173. if (PyType_Ready(&PycairoLinearGradient_Type) < 0)
  174. return;
  175. PycairoRadialGradient_Type.tp_base = &PycairoGradient_Type;
  176. if (PyType_Ready(&PycairoRadialGradient_Type) < 0)
  177. return;
  178. PycairoScaledFont_Type.tp_base = &PyBaseObject_Type;
  179. if (PyType_Ready(&PycairoScaledFont_Type) < 0)
  180. return;
  181. PycairoSurface_Type.tp_base = &PyBaseObject_Type;
  182. if (PyType_Ready(&PycairoSurface_Type) < 0)
  183. return;
  184. PycairoImageSurface_Type.tp_base = &PycairoSurface_Type;
  185. if (PyType_Ready(&PycairoImageSurface_Type) < 0)
  186. return;
  187. #ifdef CAIRO_HAS_PDF_SURFACE
  188. PycairoPDFSurface_Type.tp_base = &PycairoSurface_Type;
  189. if (PyType_Ready(&PycairoPDFSurface_Type) < 0)
  190. return;
  191. #endif
  192. #ifdef CAIRO_HAS_PS_SURFACE
  193. PycairoPSSurface_Type.tp_base = &PycairoSurface_Type;
  194. if (PyType_Ready(&PycairoPSSurface_Type) < 0)
  195. return;
  196. #endif
  197. #ifdef CAIRO_HAS_SVG_SURFACE
  198. PycairoSVGSurface_Type.tp_base = &PycairoSurface_Type;
  199. if (PyType_Ready(&PycairoSVGSurface_Type) < 0)
  200. return;
  201. #endif
  202. #ifdef CAIRO_HAS_WIN32_SURFACE
  203. PycairoWin32Surface_Type.tp_base = &PycairoSurface_Type;
  204. if (PyType_Ready(&PycairoWin32Surface_Type) < 0)
  205. return;
  206. #endif
  207. #ifdef CAIRO_HAS_XLIB_SURFACE
  208. PycairoXlibSurface_Type.tp_base = &PycairoSurface_Type;
  209. if (PyType_Ready(&PycairoXlibSurface_Type) < 0)
  210. return;
  211. #endif
  212. #ifdef CAIRO_HAS_QUARTZ_SURFACE
  213. PycairoQuartzSurface_Type.tp_base = &PycairoSurface_Type;
  214. if (PyType_Ready(&PycairoQuartzSurface_Type) < 0)
  215. return;
  216. #endif
  217. m = Py_InitModule("enso.platform.osx.cairo._cairo", cairo_functions);
  218. PyModule_AddStringConstant(m, "version", pycairo_version_string);
  219. PyModule_AddObject(m, "version_info",
  220. Py_BuildValue("(iii)", VERSION_MAJOR, VERSION_MINOR,
  221. VERSION_MICRO));
  222. Py_INCREF(&PycairoContext_Type);
  223. PyModule_AddObject(m, "Context", (PyObject *)&PycairoContext_Type);
  224. Py_INCREF(&PycairoFontFace_Type);
  225. PyModule_AddObject(m, "FontFace",(PyObject *)&PycairoFontFace_Type);
  226. Py_INCREF(&PycairoFontOptions_Type);
  227. PyModule_AddObject(m, "FontOptions",(PyObject *)&PycairoFontOptions_Type);
  228. Py_INCREF(&PycairoMatrix_Type);
  229. PyModule_AddObject(m, "Matrix", (PyObject *)&PycairoMatrix_Type);
  230. Py_INCREF(&PycairoPath_Type);
  231. /* Don't add Path object since it is not accessed directly as 'cairo.Path'
  232. * PyModule_AddObject(m, "Path", (PyObject *)&PycairoPath_Type);
  233. */
  234. Py_INCREF(&PycairoPattern_Type);
  235. PyModule_AddObject(m, "Pattern", (PyObject *)&PycairoPattern_Type);
  236. Py_INCREF(&PycairoSolidPattern_Type);
  237. PyModule_AddObject(m, "SolidPattern",
  238. (PyObject *)&PycairoSolidPattern_Type);
  239. Py_INCREF(&PycairoSurfacePattern_Type);
  240. PyModule_AddObject(m, "SurfacePattern",
  241. (PyObject *)&PycairoSurfacePattern_Type);
  242. Py_INCREF(&PycairoGradient_Type);
  243. PyModule_AddObject(m, "Gradient", (PyObject *)&PycairoGradient_Type);
  244. Py_INCREF(&PycairoLinearGradient_Type);
  245. PyModule_AddObject(m, "LinearGradient",
  246. (PyObject *)&PycairoLinearGradient_Type);
  247. Py_INCREF(&PycairoRadialGradient_Type);
  248. PyModule_AddObject(m, "RadialGradient",
  249. (PyObject *)&PycairoRadialGradient_Type);
  250. Py_INCREF(&PycairoScaledFont_Type);
  251. PyModule_AddObject(m, "ScaledFont", (PyObject *)&PycairoScaledFont_Type);
  252. Py_INCREF(&PycairoSurface_Type);
  253. PyModule_AddObject(m, "Surface", (PyObject *)&PycairoSurface_Type);
  254. Py_INCREF(&PycairoImageSurface_Type);
  255. PyModule_AddObject(m, "ImageSurface",
  256. (PyObject *)&PycairoImageSurface_Type);
  257. #ifdef CAIRO_HAS_PDF_SURFACE
  258. Py_INCREF(&PycairoPDFSurface_Type);
  259. PyModule_AddObject(m, "PDFSurface", (PyObject *)&PycairoPDFSurface_Type);
  260. #endif
  261. #ifdef CAIRO_HAS_PS_SURFACE
  262. Py_INCREF(&PycairoPSSurface_Type);
  263. PyModule_AddObject(m, "PSSurface", (PyObject *)&PycairoPSSurface_Type);
  264. #endif
  265. #ifdef CAIRO_HAS_SVG_SURFACE
  266. Py_INCREF(&PycairoSVGSurface_Type);
  267. PyModule_AddObject(m, "SVGSurface", (PyObject *)&PycairoSVGSurface_Type);
  268. #endif
  269. #ifdef CAIRO_HAS_WIN32_SURFACE
  270. Py_INCREF(&PycairoWin32Surface_Type);
  271. PyModule_AddObject(m, "Win32Surface",
  272. (PyObject *)&PycairoWin32Surface_Type);
  273. #endif
  274. #ifdef CAIRO_HAS_XLIB_SURFACE
  275. Py_INCREF(&PycairoXlibSurface_Type);
  276. PyModule_AddObject(m, "XlibSurface",
  277. (PyObject *)&PycairoXlibSurface_Type);
  278. #endif
  279. #ifdef CAIRO_HAS_QUARTZ_SURFACE
  280. Py_INCREF(&PycairoQuartzSurface_Type);
  281. PyModule_AddObject(m, "QuartzSurface",
  282. (PyObject *)&PycairoQuartzSurface_Type);
  283. #endif
  284. PyModule_AddObject(m, "CAPI", PyCObject_FromVoidPtr(&CAPI, NULL));
  285. /* Add 'cairo.Error' to the module */
  286. if (CairoError == NULL) {
  287. CairoError = PyErr_NewException("enso.platform.osx.cairo.Error", NULL, NULL);
  288. if (CairoError == NULL)
  289. return;
  290. }
  291. Py_INCREF(CairoError);
  292. if (PyModule_AddObject(m, "Error", CairoError) < 0)
  293. return;
  294. /* constants */
  295. #if CAIRO_HAS_ATSUI_FONT
  296. PyModule_AddIntConstant(m, "HAS_ATSUI_FONT", 1);
  297. #else
  298. PyModule_AddIntConstant(m, "HAS_ATSUI_FONT", 0);
  299. #endif
  300. #if CAIRO_HAS_FT_FONT
  301. PyModule_AddIntConstant(m, "HAS_FT_FONT", 1);
  302. #else
  303. PyModule_AddIntConstant(m, "HAS_FT_FONT", 0);
  304. #endif
  305. #if CAIRO_HAS_GLITZ_SURFACE
  306. PyModule_AddIntConstant(m, "HAS_GLITZ_SURFACE", 1);
  307. #else
  308. PyModule_AddIntConstant(m, "HAS_GLITZ_SURFACE", 0);
  309. #endif
  310. #if CAIRO_HAS_PDF_SURFACE
  311. PyModule_AddIntConstant(m, "HAS_PDF_SURFACE", 1);
  312. #else
  313. PyModule_AddIntConstant(m, "HAS_PDF_SURFACE", 0);
  314. #endif
  315. #if CAIRO_HAS_PNG_FUNCTIONS
  316. PyModule_AddIntConstant(m, "HAS_PNG_FUNCTIONS", 1);
  317. #else
  318. PyModule_AddIntConstant(m, "HAS_PNG_FUNCTIONS", 0);
  319. #endif
  320. #if CAIRO_HAS_PS_SURFACE
  321. PyModule_AddIntConstant(m, "HAS_PS_SURFACE", 1);
  322. #else
  323. PyModule_AddIntConstant(m, "HAS_PS_SURFACE", 0);
  324. #endif
  325. #if CAIRO_HAS_SVG_SURFACE
  326. PyModule_AddIntConstant(m, "HAS_SVG_SURFACE", 1);
  327. #else
  328. PyModule_AddIntConstant(m, "HAS_SVG_SURFACE", 0);
  329. #endif
  330. #if CAIRO_HAS_QUARTZ_SURFACE
  331. PyModule_AddIntConstant(m, "HAS_QUARTZ_SURFACE", 1);
  332. #else
  333. PyModule_AddIntConstant(m, "HAS_QUARTZ_SURFACE", 0);
  334. #endif
  335. #if CAIRO_HAS_WIN32_FONT
  336. PyModule_AddIntConstant(m, "HAS_WIN32_FONT", 1);
  337. #else
  338. PyModule_AddIntConstant(m, "HAS_WIN32_FONT", 0);
  339. #endif
  340. #if CAIRO_HAS_WIN32_SURFACE
  341. PyModule_AddIntConstant(m, "HAS_WIN32_SURFACE", 1);
  342. #else
  343. PyModule_AddIntConstant(m, "HAS_WIN32_SURFACE", 0);
  344. #endif
  345. #if CAIRO_HAS_XCB_SURFACE
  346. PyModule_AddIntConstant(m, "HAS_XCB_SURFACE", 1);
  347. #else
  348. PyModule_AddIntConstant(m, "HAS_XCB_SURFACE", 0);
  349. #endif
  350. #if CAIRO_HAS_XLIB_SURFACE
  351. PyModule_AddIntConstant(m, "HAS_XLIB_SURFACE", 1);
  352. #else
  353. PyModule_AddIntConstant(m, "HAS_XLIB_SURFACE", 0);
  354. #endif
  355. #define CONSTANT(x) PyModule_AddIntConstant(m, #x, CAIRO_##x)
  356. CONSTANT(ANTIALIAS_DEFAULT);
  357. CONSTANT(ANTIALIAS_NONE);
  358. CONSTANT(ANTIALIAS_GRAY);
  359. CONSTANT(ANTIALIAS_SUBPIXEL);
  360. CONSTANT(CONTENT_COLOR);
  361. CONSTANT(CONTENT_ALPHA);
  362. CONSTANT(CONTENT_COLOR_ALPHA);
  363. CONSTANT(EXTEND_NONE);
  364. CONSTANT(EXTEND_REPEAT);
  365. CONSTANT(EXTEND_REFLECT);
  366. CONSTANT(FILL_RULE_WINDING);
  367. CONSTANT(FILL_RULE_EVEN_ODD);
  368. CONSTANT(FILTER_FAST);
  369. CONSTANT(FILTER_GOOD);
  370. CONSTANT(FILTER_BEST);
  371. CONSTANT(FILTER_NEAREST);
  372. CONSTANT(FILTER_BILINEAR);
  373. CONSTANT(FILTER_GAUSSIAN);
  374. CONSTANT(FONT_WEIGHT_NORMAL);
  375. CONSTANT(FONT_WEIGHT_BOLD);
  376. CONSTANT(FONT_SLANT_NORMAL);
  377. CONSTANT(FONT_SLANT_ITALIC);
  378. CONSTANT(FONT_SLANT_OBLIQUE);
  379. CONSTANT(FORMAT_ARGB32);
  380. CONSTANT(FORMAT_RGB24);
  381. CONSTANT(FORMAT_A8);
  382. CONSTANT(FORMAT_A1);
  383. CONSTANT(FORMAT_RGB16_565);
  384. CONSTANT(HINT_METRICS_DEFAULT);
  385. CONSTANT(HINT_METRICS_OFF);
  386. CONSTANT(HINT_METRICS_ON);
  387. CONSTANT(HINT_STYLE_DEFAULT);
  388. CONSTANT(HINT_STYLE_NONE);
  389. CONSTANT(HINT_STYLE_SLIGHT);
  390. CONSTANT(HINT_STYLE_MEDIUM);
  391. CONSTANT(HINT_STYLE_FULL);
  392. CONSTANT(LINE_CAP_BUTT);
  393. CONSTANT(LINE_CAP_ROUND);
  394. CONSTANT(LINE_CAP_SQUARE);
  395. CONSTANT(LINE_JOIN_MITER);
  396. CONSTANT(LINE_JOIN_ROUND);
  397. CONSTANT(LINE_JOIN_BEVEL);
  398. CONSTANT(OPERATOR_CLEAR);
  399. CONSTANT(OPERATOR_SOURCE);
  400. CONSTANT(OPERATOR_OVER);
  401. CONSTANT(OPERATOR_IN);
  402. CONSTANT(OPERATOR_OUT);
  403. CONSTANT(OPERATOR_ATOP);
  404. CONSTANT(OPERATOR_DEST);
  405. CONSTANT(OPERATOR_DEST_OVER);
  406. CONSTANT(OPERATOR_DEST_IN);
  407. CONSTANT(OPERATOR_DEST_OUT);
  408. CONSTANT(OPERATOR_DEST_ATOP);
  409. CONSTANT(OPERATOR_XOR);
  410. CONSTANT(OPERATOR_ADD);
  411. CONSTANT(OPERATOR_SATURATE);
  412. CONSTANT(PATH_MOVE_TO);
  413. CONSTANT(PATH_LINE_TO);
  414. CONSTANT(PATH_CURVE_TO);
  415. CONSTANT(PATH_CLOSE_PATH);
  416. CONSTANT(SUBPIXEL_ORDER_DEFAULT);
  417. CONSTANT(SUBPIXEL_ORDER_RGB);
  418. CONSTANT(SUBPIXEL_ORDER_BGR);
  419. CONSTANT(SUBPIXEL_ORDER_VRGB);
  420. CONSTANT(SUBPIXEL_ORDER_VBGR);
  421. #undef CONSTANT
  422. }