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 : | Author: Marcel Bolten <github@marcelbolten.de> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : #ifdef HAVE_CONFIG_H
20 : #include "config.h"
21 : #endif
22 :
23 : #include "php.h"
24 : #include "php_pango.h"
25 : #include "layout_line_arginfo.h"
26 :
27 : #include <string.h>
28 : #include "zend_exceptions.h"
29 :
30 : zend_class_entry *pango_ce_pango_layout_line;
31 :
32 : static zend_object_handlers pango_layout_line_object_handlers;
33 :
34 128 : pango_layout_line_object *pango_layout_line_fetch_object(zend_object *object)
35 : {
36 128 : return (pango_layout_line_object *) ((char*)(object) - XtOffsetOf(pango_layout_line_object, std));
37 : }
38 :
39 45 : PHP_PANGO_API zend_class_entry* php_pango_get_layout_line_ce()
40 : {
41 45 : return pango_ce_pango_layout_line;
42 : }
43 :
44 : /* {{{ Get the logical and ink extents for the line */
45 3 : ZEND_METHOD(Pango_LayoutLine, getExtents)
46 : {
47 : pango_layout_line_object *layout_line_object;
48 : PangoRectangle pango_ink_rect;
49 : PangoRectangle pango_logical_rect;
50 : zval ink_rect_zv;
51 : zval logical_rect_zv;
52 :
53 3 : ZEND_PARSE_PARAMETERS_NONE();
54 :
55 4 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
56 2 : pango_layout_line_get_extents(layout_line_object->line, &pango_ink_rect, &pango_logical_rect);
57 :
58 2 : array_init(return_value);
59 2 : object_init_ex(&ink_rect_zv, php_pango_get_rectangle_ce());
60 2 : *pango_rectangle_object_get_rectangle(&ink_rect_zv) = pango_ink_rect;
61 : add_assoc_zval(return_value, "ink", &ink_rect_zv);
62 :
63 2 : object_init_ex(&logical_rect_zv, php_pango_get_rectangle_ce());
64 2 : *pango_rectangle_object_get_rectangle(&logical_rect_zv) = pango_logical_rect;
65 : add_assoc_zval(return_value, "logical", &logical_rect_zv);
66 : }
67 : /* }}} */
68 :
69 : /* {{{ Computes the height of the line, as the maximum of the heights of fonts used in this line. */
70 3 : ZEND_METHOD(Pango_LayoutLine, getHeight)
71 : {
72 3 : pango_layout_line_object *layout_line_object = NULL;
73 3 : int height = 0;
74 :
75 3 : ZEND_PARSE_PARAMETERS_NONE();
76 :
77 4 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
78 2 : pango_layout_line_get_height(layout_line_object->line, &height);
79 2 : RETURN_LONG((zend_long) height);
80 : }
81 : /* }}} */
82 :
83 : /* {{{ Returns the length of the line, in bytes. */
84 5 : ZEND_METHOD(Pango_LayoutLine, getLength)
85 : {
86 5 : pango_layout_line_object *layout_line_object = NULL;
87 :
88 5 : ZEND_PARSE_PARAMETERS_NONE();
89 :
90 8 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
91 4 : RETURN_LONG(pango_layout_line_get_length(layout_line_object->line));
92 : }
93 : /* }}} */
94 :
95 :
96 : /* {{{ Get the logical and ink extents for the line, in device units */
97 3 : ZEND_METHOD(Pango_LayoutLine, getPixelExtents)
98 : {
99 3 : pango_layout_line_object *layout_line_object = NULL;
100 : PangoRectangle pango_ink_rect;
101 : PangoRectangle pango_logical_rect;
102 : zval ink_rect_zv;
103 : zval logical_rect_zv;
104 :
105 3 : ZEND_PARSE_PARAMETERS_NONE();
106 :
107 4 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
108 2 : pango_layout_line_get_pixel_extents(layout_line_object->line, &pango_ink_rect, &pango_logical_rect);
109 :
110 2 : array_init(return_value);
111 2 : object_init_ex(&ink_rect_zv, php_pango_get_rectangle_ce());
112 2 : *pango_rectangle_object_get_rectangle(&ink_rect_zv) = pango_ink_rect;
113 : add_assoc_zval(return_value, "ink", &ink_rect_zv);
114 :
115 2 : object_init_ex(&logical_rect_zv, php_pango_get_rectangle_ce());
116 2 : *pango_rectangle_object_get_rectangle(&logical_rect_zv) = pango_logical_rect;
117 : add_assoc_zval(return_value, "logical", &logical_rect_zv);
118 : }
119 : /* }}} */
120 :
121 : /* {{{ Draws a PangoLayoutLine in the specified cairo context. If no context
122 : is specified, use the cached one from when the LayoutLine was created */
123 8 : ZEND_METHOD(Pango_LayoutLine, showLayoutLine)
124 : {
125 8 : zval *cairo_context_zval = NULL;
126 8 : pango_layout_line_object *layout_line_object = NULL;
127 8 : pango_layout_object *layout_object = NULL;
128 8 : cairo_context_object *cairo_context_object = NULL;
129 :
130 8 : ZEND_PARSE_PARAMETERS_START(0, 1)
131 7 : Z_PARAM_OPTIONAL
132 9 : Z_PARAM_OBJECT_OF_CLASS(cairo_context_zval, php_cairo_get_context_ce())
133 8 : ZEND_PARSE_PARAMETERS_END();
134 :
135 12 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
136 :
137 6 : if (cairo_context_zval == NULL) {
138 5 : layout_object = Z_PANGO_LAYOUT_P(&layout_line_object->layout_zval);
139 5 : cairo_context_zval = &layout_object->cairo_context_zv;
140 : }
141 :
142 6 : cairo_context_object = Z_CAIRO_CONTEXT_P(cairo_context_zval);
143 :
144 6 : pango_cairo_show_layout_line(cairo_context_object->context, layout_line_object->line);
145 : }
146 : /* }}} */
147 :
148 : /** {{{ Returns the resolved direction of the line. */
149 4 : PHP_METHOD(Pango_LayoutLine, getResolvedDirection)
150 : {
151 4 : pango_layout_line_object *layout_line_object = NULL;
152 : zend_object *direction_case;
153 :
154 4 : ZEND_PARSE_PARAMETERS_NONE();
155 :
156 6 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
157 3 : zend_enum_get_case_by_value(
158 : &direction_case, php_pango_get_direction_ce(),
159 3 : pango_layout_line_get_resolved_direction(layout_line_object->line),
160 : NULL, false
161 : );
162 :
163 6 : RETURN_OBJ_COPY(direction_case);
164 : }
165 : /** }}} */
166 :
167 : /** {{{ Returns the start index of the line, as byte index into the text of the layout. */
168 5 : PHP_METHOD(Pango_LayoutLine, getStartIndex)
169 : {
170 5 : pango_layout_line_object *layout_line_object = NULL;
171 :
172 5 : ZEND_PARSE_PARAMETERS_NONE();
173 :
174 8 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
175 4 : RETURN_LONG(pango_layout_line_get_start_index(layout_line_object->line));
176 : }
177 : /** }}} */
178 :
179 : /** {{{ Returns whether this is the first line of the paragraph. */
180 6 : PHP_METHOD(Pango_LayoutLine, isParagraphStart)
181 : {
182 6 : pango_layout_line_object *layout_line_object = NULL;
183 :
184 6 : ZEND_PARSE_PARAMETERS_NONE();
185 :
186 10 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(getThis());
187 5 : RETURN_BOOL(pango_layout_line_is_paragraph_start(layout_line_object->line));
188 : }
189 : /** }}} */
190 :
191 : /** {{{ Returns the runs (glyph items) in the line, from left to right. */
192 11 : PHP_METHOD(Pango_LayoutLine, getRuns)
193 : {
194 : zval *layout_line;
195 11 : pango_layout_line_object *layout_line_object = NULL;
196 : GSList *runs;
197 : GSList *iter;
198 : zval run_zv;
199 : pango_glyph_item_object *glyph_item_object;
200 :
201 11 : ZEND_PARSE_PARAMETERS_NONE();
202 :
203 20 : layout_line = getThis();
204 10 : layout_line_object = Z_PANGO_LAYOUT_LINE_P(layout_line);
205 10 : runs = layout_line_object->line->runs;
206 :
207 10 : array_init(return_value);
208 46 : for (iter = runs; iter != NULL; iter = iter->next) {
209 36 : object_init_ex(&run_zv, php_pango_get_glyph_item_ce());
210 36 : glyph_item_object = Z_PANGO_GLYPH_ITEM_P(&run_zv);
211 36 : glyph_item_object->glyph_item = pango_glyph_item_copy((PangoGlyphItem *)iter->data);
212 36 : ZVAL_COPY(&glyph_item_object->layout_line_zv, layout_line);
213 :
214 : add_next_index_zval(return_value, &run_zv);
215 : }
216 : }
217 : /** }}} */
218 :
219 : /* ----------------------------------------------------------------
220 : \Pango\Layout Object management
221 : ------------------------------------------------------------------*/
222 :
223 : /* {{{ */
224 45 : static void pango_layout_line_free_obj(zend_object *zobj)
225 : {
226 45 : pango_layout_line_object *intern = pango_layout_line_fetch_object(zobj);
227 :
228 45 : if (!intern) {
229 0 : return;
230 : }
231 :
232 45 : if (intern->line != NULL) {
233 45 : pango_layout_line_unref(intern->line);
234 45 : intern->line = NULL;
235 : }
236 45 : zval_ptr_dtor(&intern->layout_zval);
237 :
238 45 : zend_object_std_dtor(&intern->std);
239 : }
240 : /* }}} */
241 :
242 : /* {{{ */
243 45 : static zend_object* pango_layout_line_obj_ctor(zend_class_entry *ce, pango_layout_line_object **intern)
244 : {
245 45 : pango_layout_line_object *object = ecalloc(1, sizeof(pango_layout_line_object) + zend_object_properties_size(ce));
246 :
247 45 : ZVAL_UNDEF(&object->layout_zval);
248 :
249 45 : zend_object_std_init(&object->std, ce);
250 :
251 45 : object->std.handlers = &pango_layout_line_object_handlers;
252 45 : *intern = object;
253 :
254 45 : return &object->std;
255 : }
256 : /* }}} */
257 :
258 : /* {{{ */
259 45 : static zend_object* pango_layout_line_create_object(zend_class_entry *ce)
260 : {
261 45 : pango_layout_line_object *intern = NULL;
262 45 : zend_object *return_value = pango_layout_line_obj_ctor(ce, &intern);
263 :
264 45 : object_properties_init(&intern->std, ce);
265 45 : return return_value;
266 : }
267 : /* }}} */
268 :
269 : /* {{{ PHP_MINIT_FUNCTION */
270 194 : PHP_MINIT_FUNCTION(pango_layout_line)
271 : {
272 194 : memcpy(
273 : &pango_layout_line_object_handlers,
274 : zend_get_std_object_handlers(),
275 : sizeof(zend_object_handlers)
276 : );
277 :
278 194 : pango_layout_line_object_handlers.offset = XtOffsetOf(pango_layout_line_object, std);
279 194 : pango_layout_line_object_handlers.free_obj = pango_layout_line_free_obj;
280 :
281 194 : pango_ce_pango_layout_line = register_class_Pango_LayoutLine();
282 194 : pango_ce_pango_layout_line->create_object = pango_layout_line_create_object;
283 :
284 194 : return SUCCESS;
285 : }
286 : /* }}} */
|