Line data Source code
1 : /*
2 : +----------------------------------------------------------------------+
3 : | For PHP Version 8 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 2015 Elizabeth M Smith |
6 : +----------------------------------------------------------------------+
7 : | http://www.opensource.org/licenses/mit-license.php MIT License |
8 : | Also available in LICENSE |
9 : +----------------------------------------------------------------------+
10 : | Authors: Elizabeth M Smith <auroraeosrose@gmail.com> |
11 : | Swen Zanon <swen.zanon@geoglis.de> |
12 : +----------------------------------------------------------------------+
13 : */
14 :
15 : #ifdef HAVE_CONFIG_H
16 : #include "config.h"
17 : #endif
18 :
19 : #include <cairo.h>
20 : #include <php.h>
21 : #include <zend_exceptions.h>
22 :
23 : #include "php_cairo.h"
24 : #include "php_cairo_internal.h"
25 : #include "font_face_arginfo.h"
26 :
27 :
28 : zend_class_entry *ce_cairo_fontface;
29 : zend_class_entry *ce_cairo_fonttype;
30 :
31 : static zend_object_handlers cairo_font_face_object_handlers;
32 :
33 129 : cairo_font_face_object *cairo_font_face_fetch_object(zend_object *object)
34 : {
35 129 : return (cairo_font_face_object *) ((char*)(object) - XtOffsetOf(cairo_font_face_object, std));
36 : }
37 :
38 23 : cairo_font_face_object *cairo_font_face_object_get(zval *zv)
39 : {
40 23 : cairo_font_face_object *object = Z_CAIRO_FONT_FACE_P(zv);
41 :
42 23 : if (object->font_face == NULL) {
43 1 : zend_throw_exception_ex(ce_cairo_exception, 0,
44 : "Internal font face object missing in %s, you must call parent::__construct in extended classes.",
45 1 : ZSTR_VAL(Z_OBJCE_P(zv)->name));
46 1 : return NULL;
47 : }
48 :
49 22 : return object;
50 : }
51 :
52 :
53 : /* ----------------------------------------------------------------
54 : \Cairo\FontOptions C API
55 : ------------------------------------------------------------------*/
56 :
57 : /* {{{ */
58 866 : zend_class_entry * php_cairo_get_fontface_ce()
59 : {
60 866 : return ce_cairo_fontface;
61 : }
62 : /* }}} */
63 :
64 : /* {{{ */
65 0 : zend_class_entry * php_ce_cairo_fonttype()
66 : {
67 0 : return ce_cairo_fonttype;
68 : }
69 : /* }}} */
70 :
71 : /* {{{ */
72 15 : cairo_font_face_t *cairo_font_face_object_get_font_face(zval *zv)
73 : {
74 15 : return cairo_font_face_object_get(zv)->font_face;
75 : }
76 : /* }}} */
77 :
78 : /* ----------------------------------------------------------------
79 : \Cairo\FontOptions Class API
80 : ------------------------------------------------------------------*/
81 : /* {{{ proto void \Cairo\FontFace::__contruct()
82 : \Cairo\FontFace CANNOT be extended in userspace, this will throw an exception on use */
83 1 : PHP_METHOD(Cairo_FontFace, __construct)
84 : {
85 1 : ZEND_PARSE_PARAMETERS_NONE();
86 1 : zend_throw_exception(ce_cairo_exception, "Cairo\\FontFace cannot be constructed", 0);
87 : }
88 : /* }}} */
89 :
90 : /* {{{ proto \Cairo\Status \Cairo\FontFace::getStatus()
91 : Returns the current integer status of the CairoFontFace */
92 2 : PHP_METHOD(Cairo_FontFace, getStatus)
93 : {
94 : cairo_font_face_object *font_face_object;
95 : zval status_case;
96 :
97 3 : ZEND_PARSE_PARAMETERS_NONE();
98 :
99 2 : font_face_object = cairo_font_face_object_get(getThis());
100 1 : if (!font_face_object) {
101 0 : RETURN_THROWS();
102 : }
103 :
104 1 : status_case = php_enum_from_cairo_c_enum(
105 : ce_cairo_status,
106 1 : cairo_font_face_status(font_face_object->font_face)
107 : );
108 :
109 1 : if (Z_TYPE(status_case) == IS_OBJECT) {
110 2 : RETURN_ZVAL(&status_case, 1, 1);
111 : }
112 : }
113 : /* }}} */
114 :
115 : /* {{{ proto \Cairo\FontFace \Cairo\FontFace::getType()
116 : Returns the current integer type of the Cairo\FontFace backend */
117 3 : PHP_METHOD(Cairo_FontFace, getType)
118 : {
119 : cairo_font_face_object *font_face_object;
120 : zval fonttype_case;
121 :
122 5 : ZEND_PARSE_PARAMETERS_NONE();
123 :
124 4 : font_face_object = cairo_font_face_object_get(getThis());
125 2 : if (!font_face_object) {
126 1 : RETURN_THROWS();
127 : }
128 :
129 1 : fonttype_case = php_enum_from_cairo_c_enum(
130 : ce_cairo_fonttype,
131 1 : cairo_font_face_get_type(font_face_object->font_face)
132 : );
133 :
134 1 : if (Z_TYPE(fonttype_case) == IS_OBJECT) {
135 2 : RETURN_ZVAL(&fonttype_case, 1, 1);
136 : }
137 : }
138 : /* }}} */
139 :
140 :
141 : /* ----------------------------------------------------------------
142 : \Cairo\FontFace Object management
143 : ------------------------------------------------------------------*/
144 :
145 : /* {{{ */
146 50 : static void cairo_font_face_free_obj(zend_object *object)
147 : {
148 50 : cairo_font_face_object *intern = cairo_font_face_fetch_object(object);
149 :
150 50 : if (!intern) {
151 0 : return;
152 : }
153 :
154 50 : if (intern->font_face) {
155 40 : cairo_font_face_destroy(intern->font_face);
156 40 : intern->font_face = NULL;
157 : }
158 :
159 50 : if (intern->closure != NULL) {
160 5 : if (intern->closure->owned_stream) {
161 5 : php_stream_close(intern->closure->stream);
162 : }
163 5 : efree(intern->closure);
164 : }
165 :
166 50 : zend_object_std_dtor(&intern->std);
167 : }
168 :
169 : /* {{{ */
170 50 : static zend_object* cairo_font_face_obj_ctor(zend_class_entry *ce, cairo_font_face_object **intern)
171 : {
172 50 : cairo_font_face_object *object = ecalloc(1, sizeof(cairo_font_face_object) + zend_object_properties_size(ce));
173 :
174 50 : object->font_face = NULL;
175 50 : object->closure = NULL;
176 :
177 50 : zend_object_std_init(&object->std, ce);
178 50 : object->std.handlers = &cairo_font_face_object_handlers;
179 50 : *intern = object;
180 :
181 50 : return &object->std;
182 : }
183 : /* }}} */
184 :
185 : /* {{{ */
186 49 : zend_object* cairo_font_face_create_object(zend_class_entry *ce)
187 : {
188 49 : cairo_font_face_object *font_face_obj = NULL;
189 49 : zend_object *return_value = cairo_font_face_obj_ctor(ce, &font_face_obj);
190 :
191 49 : object_properties_init(&font_face_obj->std, ce);
192 49 : return return_value;
193 : }
194 : /* }}} */
195 :
196 : /* {{{ */
197 1 : static zend_object* cairo_font_face_clone_obj(zend_object *old_object)
198 : {
199 : cairo_font_face_object *new_font, *old_font;
200 1 : old_font = cairo_font_face_fetch_object(old_object);
201 :
202 1 : zend_object *return_value = cairo_font_face_obj_ctor(old_object->ce, &new_font);
203 1 : zend_objects_clone_members(&new_font->std, &old_font->std);
204 :
205 : /* Fonts are created and then never changed, with the exception of
206 : * the set_user_data stuff. That means we don't have to do any
207 : * real cloning of the font -- just increase it's ref count and
208 : * point the new font to the old one. Simples.
209 : */
210 1 : cairo_font_face_reference(old_font->font_face);
211 1 : new_font->font_face = old_font->font_face;
212 :
213 1 : return return_value;
214 : }
215 : /* }}} */
216 :
217 : /* ----------------------------------------------------------------
218 : Cairo\FontFace Definition and registration
219 : ------------------------------------------------------------------*/
220 :
221 : /* {{{ PHP_MINIT_FUNCTION */
222 433 : PHP_MINIT_FUNCTION(cairo_font_face)
223 : {
224 433 : memcpy(
225 : &cairo_font_face_object_handlers,
226 : zend_get_std_object_handlers(),
227 : sizeof(zend_object_handlers)
228 : );
229 :
230 : /* FontFace */
231 433 : cairo_font_face_object_handlers.offset = XtOffsetOf(cairo_font_face_object, std);
232 433 : cairo_font_face_object_handlers.free_obj = cairo_font_face_free_obj;
233 433 : cairo_font_face_object_handlers.clone_obj = cairo_font_face_clone_obj;
234 :
235 433 : ce_cairo_fontface = register_class_Cairo_FontFace();
236 433 : ce_cairo_fontface->create_object = cairo_font_face_create_object;
237 :
238 : /* FontType */
239 433 : ce_cairo_fonttype = register_class_Cairo_FontType();
240 :
241 433 : return SUCCESS;
242 : }
|