Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | For PHP Version 8.2+ |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Michael Maclean <mgdm@php.net> |
16 : | Marcel Bolten <github@marcelbolten.de> |
17 : +----------------------------------------------------------------------+
18 : */
19 :
20 : #ifdef HAVE_CONFIG_H
21 : #include "config.h"
22 : #endif
23 :
24 : #include "php.h"
25 : #include "php_pango.h"
26 :
27 : #include "zend_exceptions.h"
28 : #include "font_description_arginfo.h"
29 :
30 : zend_class_entry *pango_ce_pango_font_description;
31 : zend_class_entry *pango_ce_pango_style;
32 : zend_class_entry *pango_ce_pango_weight;
33 : zend_class_entry *pango_ce_pango_variant;
34 : zend_class_entry *pango_ce_pango_stretch;
35 : zend_class_entry *pango_ce_pango_font_mask;
36 :
37 : static zend_object_handlers pango_font_description_object_handlers;
38 :
39 116 : pango_font_description_object *pango_font_description_fetch_object(zend_object *object)
40 : {
41 116 : return (pango_font_description_object *) ((char*)(object) - XtOffsetOf(pango_font_description_object, std));
42 : }
43 :
44 11 : PHP_PANGO_API zend_class_entry* php_pango_get_font_description_ce()
45 : {
46 11 : return pango_ce_pango_font_description;
47 : }
48 :
49 : /* {{{ Creates a new font description object. */
50 28 : PHP_METHOD(Pango_FontDescription, __construct)
51 : {
52 28 : pango_font_description_object *font_description_object = NULL;
53 28 : char *text = NULL;
54 28 : size_t text_len = 0;
55 :
56 28 : ZEND_PARSE_PARAMETERS_START(0, 1)
57 27 : Z_PARAM_OPTIONAL
58 39 : Z_PARAM_STRING(text, text_len)
59 28 : ZEND_PARSE_PARAMETERS_END();
60 :
61 54 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
62 :
63 27 : if (text && text_len > 0) {
64 11 : font_description_object->font_description = pango_font_description_from_string(text);
65 : } else {
66 16 : font_description_object->font_description = pango_font_description_new();
67 : }
68 :
69 27 : if (!font_description_object->font_description) {
70 0 : zend_throw_exception(
71 : pango_ce_pango_exception,
72 : "Could not instantiate Pango\\FontDescription",
73 : 0
74 : );
75 0 : RETURN_THROWS();
76 : }
77 : }
78 : /* }}} */
79 :
80 : /* {{{ Gets the variant of the font description. */
81 4 : PHP_METHOD(Pango_FontDescription, getVariant)
82 : {
83 : pango_font_description_object *font_description_object;
84 : zend_object *variant_case;
85 :
86 4 : ZEND_PARSE_PARAMETERS_NONE();
87 :
88 6 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
89 :
90 3 : zend_enum_get_case_by_value(
91 : &variant_case, pango_ce_pango_variant,
92 3 : pango_font_description_get_variant(font_description_object->font_description),
93 : NULL, false
94 : );
95 :
96 6 : RETURN_OBJ_COPY(variant_case);
97 : }
98 : /* }}} */
99 :
100 : /* {{{ Sets the variant of the layout. */
101 5 : PHP_METHOD(Pango_FontDescription, setVariant)
102 : {
103 : pango_font_description_object *font_description_object;
104 : zend_object *variant;
105 :
106 5 : ZEND_PARSE_PARAMETERS_START(1, 1)
107 8 : Z_PARAM_OBJ_OF_CLASS(variant, pango_ce_pango_variant)
108 5 : ZEND_PARSE_PARAMETERS_END();
109 :
110 6 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
111 3 : pango_font_description_set_variant(
112 : font_description_object->font_description,
113 6 : Z_LVAL_P(zend_enum_fetch_case_value(variant))
114 : );
115 : }
116 : /* }}} */
117 :
118 : /* {{{ Compares two font description objects for equality. */
119 5 : PHP_METHOD(Pango_FontDescription, equal)
120 : {
121 5 : zval *font_description_2_zval = NULL;
122 : pango_font_description_object *font_description_object;
123 : pango_font_description_object *font_description_2_object;
124 :
125 5 : ZEND_PARSE_PARAMETERS_START(1, 1)
126 6 : Z_PARAM_OBJECT_OF_CLASS(font_description_2_zval, pango_ce_pango_font_description)
127 5 : ZEND_PARSE_PARAMETERS_END();
128 :
129 4 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
130 2 : font_description_2_object = Z_PANGO_FONT_DESC_P(font_description_2_zval);
131 :
132 2 : RETURN_BOOL(pango_font_description_equal(
133 : font_description_object->font_description,
134 : font_description_2_object->font_description
135 : ));
136 : }
137 : /* }}} */
138 :
139 : /* {{{ Sets the family name field of a font description. */
140 7 : PHP_METHOD(Pango_FontDescription, setFamily)
141 : {
142 : pango_font_description_object *font_description_object;
143 : char *family;
144 : size_t family_len;
145 :
146 7 : ZEND_PARSE_PARAMETERS_START(1, 1)
147 12 : Z_PARAM_STRING(family, family_len)
148 7 : ZEND_PARSE_PARAMETERS_END();
149 :
150 10 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
151 5 : pango_font_description_set_family(font_description_object->font_description, family);
152 : }
153 : /* }}} */
154 :
155 : /* {{{ Gets the family name field of a font description. */
156 6 : PHP_METHOD(Pango_FontDescription, getFamily)
157 : {
158 : pango_font_description_object *font_description_object;
159 : const char *family;
160 :
161 6 : ZEND_PARSE_PARAMETERS_NONE();
162 :
163 10 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
164 5 : if ((family = pango_font_description_get_family(font_description_object->font_description))) {
165 8 : RETURN_STRING((char *)family);
166 : }
167 :
168 : // return empty string if family is not set
169 1 : RETURN_EMPTY_STRING();
170 : }
171 : /* }}} */
172 :
173 : /* {{{ Sets the size field of a font description. */
174 7 : PHP_METHOD(Pango_FontDescription, setSize)
175 : {
176 : pango_font_description_object *font_description_object;
177 : zend_long size;
178 :
179 7 : ZEND_PARSE_PARAMETERS_START(1, 1)
180 10 : Z_PARAM_LONG(size)
181 7 : ZEND_PARSE_PARAMETERS_END();
182 :
183 8 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
184 : // Should size automatically be scaled by Pango::SCALE?
185 4 : pango_font_description_set_size(font_description_object->font_description, size);
186 : }
187 : /* }}} */
188 :
189 : /* {{{ Gets the size field of a font description. */
190 5 : PHP_METHOD(Pango_FontDescription, getSize)
191 : {
192 : pango_font_description_object *font_description_object;
193 :
194 5 : ZEND_PARSE_PARAMETERS_NONE();
195 :
196 8 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
197 4 : RETURN_LONG(pango_font_description_get_size(font_description_object->font_description));
198 : }
199 : /* }}} */
200 :
201 : /* {{{ Gets the style of the font description. */
202 5 : PHP_METHOD(Pango_FontDescription, getStyle)
203 : {
204 : pango_font_description_object *font_description_object;
205 : zend_object *style_case;
206 :
207 5 : ZEND_PARSE_PARAMETERS_NONE();
208 :
209 8 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
210 4 : zend_enum_get_case_by_value(
211 : &style_case, pango_ce_pango_style,
212 4 : pango_font_description_get_style(font_description_object->font_description),
213 : NULL, false
214 : );
215 :
216 8 : RETURN_OBJ_COPY(style_case);
217 : }
218 : /* }}} */
219 :
220 : /* {{{ Sets the style of the layout. */
221 5 : PHP_METHOD(Pango_FontDescription, setStyle)
222 : {
223 : pango_font_description_object *font_description_object;
224 : zend_object *style;
225 :
226 5 : ZEND_PARSE_PARAMETERS_START(1, 1)
227 8 : Z_PARAM_OBJ_OF_CLASS(style, pango_ce_pango_style)
228 5 : ZEND_PARSE_PARAMETERS_END();
229 :
230 6 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
231 3 : pango_font_description_set_style(
232 : font_description_object->font_description,
233 6 : Z_LVAL_P(zend_enum_fetch_case_value(style))
234 : );
235 : }
236 : /* }}} */
237 :
238 : /* {{{ Gets the weight of the font description. */
239 5 : PHP_METHOD(Pango_FontDescription, getWeight)
240 : {
241 : pango_font_description_object *font_description_object;
242 : zend_object *weight_case;
243 :
244 5 : ZEND_PARSE_PARAMETERS_NONE();
245 :
246 8 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
247 4 : zend_enum_get_case_by_value(
248 : &weight_case, pango_ce_pango_weight,
249 4 : pango_font_description_get_weight(font_description_object->font_description),
250 : NULL, false
251 : );
252 :
253 8 : RETURN_OBJ_COPY(weight_case);
254 : }
255 : /* }}} */
256 :
257 : /* {{{ Sets the weight of the layout. */
258 5 : PHP_METHOD(Pango_FontDescription, setWeight)
259 : {
260 : pango_font_description_object *font_description_object;
261 : zend_object *weight;
262 :
263 5 : ZEND_PARSE_PARAMETERS_START(1, 1)
264 8 : Z_PARAM_OBJ_OF_CLASS(weight, pango_ce_pango_weight)
265 5 : ZEND_PARSE_PARAMETERS_END();
266 :
267 6 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
268 3 : pango_font_description_set_weight(
269 : font_description_object->font_description,
270 6 : Z_LVAL_P(zend_enum_fetch_case_value(weight))
271 : );
272 : }
273 : /* }}} */
274 :
275 : /* {{{ Gets the stretch of the font description. */
276 5 : PHP_METHOD(Pango_FontDescription, getStretch)
277 : {
278 : pango_font_description_object *font_description_object;
279 : zend_object *stretch_case;
280 :
281 5 : ZEND_PARSE_PARAMETERS_NONE();
282 :
283 8 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
284 4 : zend_enum_get_case_by_value(
285 : &stretch_case, pango_ce_pango_stretch,
286 4 : pango_font_description_get_stretch(font_description_object->font_description),
287 : NULL, false
288 : );
289 :
290 8 : RETURN_OBJ_COPY(stretch_case);
291 : }
292 : /* }}} */
293 :
294 : /* {{{ Sets the stretch of the layout. */
295 5 : PHP_METHOD(Pango_FontDescription, setStretch)
296 : {
297 : pango_font_description_object *font_description_object;
298 : zend_object *stretch;
299 :
300 5 : ZEND_PARSE_PARAMETERS_START(1, 1)
301 8 : Z_PARAM_OBJ_OF_CLASS(stretch, pango_ce_pango_stretch)
302 5 : ZEND_PARSE_PARAMETERS_END();
303 :
304 6 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
305 3 : pango_font_description_set_stretch(
306 : font_description_object->font_description,
307 6 : Z_LVAL_P(zend_enum_fetch_case_value(stretch))
308 : );
309 : }
310 : /* }}} */
311 :
312 : /* {{{ Creates a string representation of a font description. */
313 7 : PHP_METHOD(Pango_FontDescription, toString)
314 : {
315 : pango_font_description_object *font_description_object;
316 : char *result;
317 :
318 7 : ZEND_PARSE_PARAMETERS_NONE();
319 :
320 12 : font_description_object = Z_PANGO_FONT_DESC_P(getThis());
321 6 : if (result = pango_font_description_to_string(font_description_object->font_description)) {
322 6 : RETVAL_STRING((const char *)result);
323 6 : g_free(result);
324 6 : return;
325 : }
326 0 : RETURN_EMPTY_STRING();
327 : }
328 : /* }}} */
329 :
330 : /**
331 : * \Pango\FontDescription Object management
332 : */
333 :
334 : /* {{{ */
335 30 : static void pango_font_description_free_obj(zend_object *zobj)
336 : {
337 30 : pango_font_description_object *intern = pango_font_description_fetch_object(zobj);
338 :
339 30 : if (!intern) {
340 0 : return;
341 : }
342 :
343 30 : if (intern->font_description) {
344 29 : pango_font_description_free(intern->font_description);
345 29 : intern->font_description = NULL;
346 : }
347 :
348 30 : zend_object_std_dtor(&intern->std);
349 : }
350 : /* }}} */
351 :
352 : /* {{{ */
353 30 : static zend_object* pango_font_description_obj_ctor(zend_class_entry *ce, pango_font_description_object **intern)
354 : {
355 30 : pango_font_description_object *object = ecalloc(1, sizeof(pango_font_description_object) + zend_object_properties_size(ce));
356 :
357 :
358 30 : zend_object_std_init(&object->std, ce);
359 :
360 30 : object->std.handlers = &pango_font_description_object_handlers;
361 30 : *intern = object;
362 :
363 30 : return &object->std;
364 : }
365 : /* }}} */
366 :
367 : /* {{{ */
368 30 : static zend_object* pango_font_description_create_object(zend_class_entry *ce)
369 : {
370 30 : pango_font_description_object *intern = NULL;
371 30 : zend_object *return_value = pango_font_description_obj_ctor(ce, &intern);
372 :
373 30 : object_properties_init(&intern->std, ce);
374 30 : return return_value;
375 : }
376 : /* }}} */
377 :
378 : /* {{{ PHP_MINIT_FUNCTION */
379 194 : PHP_MINIT_FUNCTION(pango_font_description)
380 : {
381 194 : memcpy(
382 : &pango_font_description_object_handlers,
383 : zend_get_std_object_handlers(),
384 : sizeof(zend_object_handlers)
385 : );
386 :
387 194 : pango_font_description_object_handlers.offset = XtOffsetOf(pango_font_description_object, std);
388 194 : pango_font_description_object_handlers.free_obj = pango_font_description_free_obj;
389 :
390 194 : pango_ce_pango_font_description = register_class_Pango_FontDescription();
391 194 : pango_ce_pango_font_description->create_object = pango_font_description_create_object;
392 :
393 194 : pango_ce_pango_style = register_class_Pango_Style();
394 194 : pango_ce_pango_weight = register_class_Pango_Weight();
395 194 : pango_ce_pango_variant = register_class_Pango_Variant();
396 194 : pango_ce_pango_stretch = register_class_Pango_Stretch();
397 194 : pango_ce_pango_font_mask = register_class_Pango_FontMask();
398 194 : pango_ce_pango_font_mask->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS | ZEND_ACC_FINAL;
399 :
400 194 : return SUCCESS;
401 : }
402 : /* }}} */
|