Debian Codegraph @

cairo/textcluster.c

raw symbols SCIP
1 /* -*- mode: C; c-basic-offset: 2 -*-
2 *
3 * Pycairo - Python bindings for cairo
4 *
5 * Copyright © 2017 Christoph Reiter
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
31 #define PY_SSIZE_T_CLEAN
32 #include <Python.h>
33
34 #include "private.h"
35
36 /* 0 on success */
37 int
38 _PyTextCluster_AsTextCluster (PyObject *pyobj, cairo_text_cluster_t *cluster) {
39 long num_bytes, num_glyphs;
40
41 if (!PyObject_TypeCheck (pyobj, &PycairoTextCluster_Type)) {
42 PyErr_SetString (PyExc_TypeError,
43 "item must be of type cairo.TextCluster");
44 return -1;
45 }
46
47 num_bytes = PyLong_AsLong (
48 PySequence_Fast_GET_ITEM (pyobj, 0));
49 if (PyErr_Occurred ())
50 return -1;
51 if (num_bytes > INT_MAX || num_bytes < INT_MIN) {
52 PyErr_SetString (PyExc_ValueError, "num_bytes out of range");
53 return -1;
54 }
55 cluster->num_bytes = (int)num_bytes;
56
57 num_glyphs = PyLong_AsLong (
58 PySequence_Fast_GET_ITEM (pyobj, 1));
59 if (PyErr_Occurred ())
60 return -1;
61 if (num_glyphs > INT_MAX || num_glyphs < INT_MIN) {
62 PyErr_SetString (PyExc_ValueError, "num_glyphs out of range");
63 return -1;
64 }
65 cluster->num_glyphs = (int)num_glyphs;
66
67 return 0;
68 }
69
70 static char *KWDS[] = {"num_bytes", "num_glyphs", NULL};
71
72 static PyObject *
73 text_cluster_new (PyTypeObject *type, PyObject *args, PyObject *kwds) {
74 int num_bytes, num_glyphs;
75 PyObject *tuple_args, *result;
76
77 if (!PyArg_ParseTupleAndKeywords (args, kwds, "ii:TextCluster.__new__",
78 KWDS, &num_bytes, &num_glyphs))
79 return NULL;
80
81 tuple_args = Py_BuildValue ("((ii))", num_bytes, num_glyphs);
82 if (tuple_args == NULL)
83 return NULL;
84 result = PyTuple_Type.tp_new (type, tuple_args, NULL);
85 Py_DECREF (tuple_args);
86 return result;
87 }
88
89 static PyObject*
90 text_cluster_repr(PyObject *self) {
91 PyObject *format, *result;
92
93 format = PyUnicode_FromString (
94 "cairo.TextCluster(num_bytes=%r, num_glyphs=%r)");
95 if (format == NULL)
96 return NULL;
97 result = PyUnicode_Format (format, self);
98 Py_DECREF (format);
99 return result;
100 }
101
102 static PyObject *
103 text_cluster_get_num_bytes(PyObject *self, void *closure)
104 {
105 PyObject *obj = PyTuple_GetItem (self, 0);
106 Py_XINCREF (obj);
107 return obj;
108 }
109
110 static PyObject *
111 text_cluster_get_num_glyphs(PyObject *self, void *closure)
112 {
113 PyObject *obj = PyTuple_GetItem (self, 1);
114 Py_XINCREF (obj);
115 return obj;
116 }
117
118 static PyGetSetDef text_cluster_getset[] = {
119 {"num_bytes", (getter)text_cluster_get_num_bytes},
120 {"num_glyphs", (getter)text_cluster_get_num_glyphs},
121 {NULL,},
122 };
123
124 PyTypeObject PycairoTextCluster_Type = {
125 PyVarObject_HEAD_INIT(NULL, 0)
126 "cairo.TextCluster", /* tp_name */
127 sizeof(PycairTextCluster), /* tp_basicsize */
128 0, /* tp_itemsize */
129 0, /* tp_dealloc */
130 0, /* tp_print */
131 0, /* tp_getattr */
132 0, /* tp_setattr */
133 0, /* tp_compare */
134 (reprfunc)text_cluster_repr, /* tp_repr */
135 0, /* tp_as_number */
136 0, /* tp_as_sequence */
137 0, /* tp_as_mapping */
138 0, /* tp_hash */
139 0, /* tp_call */
140 0, /* tp_str */
141 0, /* tp_getattro */
142 0, /* tp_setattro */
143 0, /* tp_as_buffer */
144 Py_TPFLAGS_DEFAULT, /* tp_flags */
145 0, /* tp_doc */
146 0, /* tp_traverse */
147 0, /* tp_clear */
148 0, /* tp_richcompare */
149 0, /* tp_weaklistoffset */
150 0, /* tp_iter */
151 0, /* tp_iternext */
152 0, /* tp_methods */
153 0, /* tp_members */
154 text_cluster_getset, /* tp_getset */
155 0, /* tp_base */
156 0, /* tp_dict */
157 0, /* tp_descr_get */
158 0, /* tp_descr_set */
159 0, /* tp_dictoffset */
160 0, /* tp_init */
161 0, /* tp_alloc */
162 (newfunc)text_cluster_new, /* tp_new */
163 0, /* tp_free */
164 0, /* tp_is_gc */
165 0, /* tp_bases */
166 };