Make.xs

filter: Statementstmt 56.1% 219 / 390 Branchbran 29.1% 103 / 354 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 43.2% 322 / 744 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/action.xs ↑ Summary xs/crypt.xs »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "pdfmake.h"
7 #include "pdfmake_buf.h"
8 #include "pdfmake_writer.h"
9 #include "pdfmake_doc.h"
10 #include "pdfmake_meta.h"
11 #include "pdfmake_page.h"
12 #include "pdfmake_content.h"
13 #include "pdfmake_parser.h"
14 #include "pdfmake_arena.h"
15 #include "pdfmake_reader.h"
16 #include "pdfmake_image.h"
17 #include "pdfmake_font.h"
18 #include "pdfmake_interpreter.h"
19 #include "pdfmake_textract.h"
20 #include "pdfmake_edit.h"
21 #include "pdfmake_import.h"
22 #include "pdfmake_ocg.h"
23 #include "pdfmake_attach.h"
24 #include "pdfmake_tag.h"
25 #include "pdfmake_redact.h"
26 #include "pdfmake_color_mgmt.h"
27 #include "pdfmake_annot.h"
28 #include "pdfmake_outline.h"
29 #include "pdfmake_action.h"
30 #include "pdfmake_crypt.h"
31 #include "pdfmake_form.h"
32 #include "pdfmake_watermark.h"
33 #include "pdfmake_signature.h"
34 #include "pdfmake_x509.h"
35 #include "pdfmake_pkcs12.h"
36 #include "pdfmake_linear.h"
37 #include "pdfmake_image_render.h"
38 #include "pdfmake_custom_ops.h"
39 #include "pdfmake_filter.h"
40 #include "pdfmake_markup.h"
41 #include "pdfmake_markup_style.h"
42 #include "pdfmake_markup_profile.h"
43  
44 /* -------------------------------------------------------------------------
45 * Render API forward declarations
46 * ------------------------------------------------------------------------- */
47 struct pdfmake_render_ctx {
48 uint32_t *pixels;
49 int width;
50 int height;
51 int stride;
52 };
53 #ifndef PDFMAKE_RENDER_CTX_T_DEFINED
54 #define PDFMAKE_RENDER_CTX_T_DEFINED
55 typedef struct pdfmake_render_ctx pdfmake_render_ctx_t;
56 #endif
57  
58 typedef pdfmake_render_ctx_t* PDF__Make__Render;
59  
60 typedef enum {
61 PDFMAKE_RENDER_OK = 0,
62 PDFMAKE_RENDER_ERR_NULL,
63 PDFMAKE_RENDER_ERR_MEMORY,
64 PDFMAKE_RENDER_ERR_INVALID,
65 PDFMAKE_RENDER_ERR_OVERFLOW,
66 PDFMAKE_RENDER_ERR_EMPTY_PATH,
67 } pdfmake_render_err_t;
68  
69 typedef int pdfmake_line_cap_t;
70 typedef int pdfmake_line_join_t;
71 typedef int pdfmake_fill_rule_t;
72  
73 typedef struct {
74 double a, b, c, d, e, f;
75 } pdfmake_matrix_t;
76  
77 pdfmake_render_ctx_t *pdfmake_render_create(int width, int height);
78 void pdfmake_render_destroy(pdfmake_render_ctx_t *ctx);
79 void pdfmake_render_clear(pdfmake_render_ctx_t *ctx, pdfmake_color_t color);
80 pdfmake_render_err_t pdfmake_render_save(pdfmake_render_ctx_t *ctx);
81 pdfmake_render_err_t pdfmake_render_restore(pdfmake_render_ctx_t *ctx);
82 void pdfmake_render_set_fill_color(pdfmake_render_ctx_t *ctx, double r, double g, double b, double a);
83 void pdfmake_render_set_stroke_color(pdfmake_render_ctx_t *ctx, double r, double g, double b, double a);
84 void pdfmake_render_set_line_width(pdfmake_render_ctx_t *ctx, double width);
85 void pdfmake_render_set_line_cap(pdfmake_render_ctx_t *ctx, pdfmake_line_cap_t cap);
86 void pdfmake_render_set_line_join(pdfmake_render_ctx_t *ctx, pdfmake_line_join_t join);
87 void pdfmake_render_set_miter_limit(pdfmake_render_ctx_t *ctx, double limit);
88 pdfmake_render_err_t pdfmake_render_set_dash(pdfmake_render_ctx_t *ctx, double *array, size_t count, double phase);
89 void pdfmake_render_set_fill_rule(pdfmake_render_ctx_t *ctx, pdfmake_fill_rule_t rule);
90 void pdfmake_render_translate(pdfmake_render_ctx_t *ctx, double tx, double ty);
91 void pdfmake_render_scale(pdfmake_render_ctx_t *ctx, double sx, double sy);
92 void pdfmake_render_rotate(pdfmake_render_ctx_t *ctx, double angle);
93 void pdfmake_render_set_matrix(pdfmake_render_ctx_t *ctx, pdfmake_matrix_t *m);
94 pdfmake_render_err_t pdfmake_render_move_to(pdfmake_render_ctx_t *ctx, double x, double y);
95 pdfmake_render_err_t pdfmake_render_line_to(pdfmake_render_ctx_t *ctx, double x, double y);
96 pdfmake_render_err_t pdfmake_render_curve_to(pdfmake_render_ctx_t *ctx, double x1, double y1, double x2, double y2, double x3, double y3);
97 pdfmake_render_err_t pdfmake_render_close_path(pdfmake_render_ctx_t *ctx);
98 pdfmake_render_err_t pdfmake_render_rect(pdfmake_render_ctx_t *ctx, double x, double y, double w, double h);
99 void pdfmake_render_new_path(pdfmake_render_ctx_t *ctx);
100 pdfmake_render_err_t pdfmake_render_fill(pdfmake_render_ctx_t *ctx);
101 pdfmake_render_err_t pdfmake_render_fill_preserve(pdfmake_render_ctx_t *ctx);
102 pdfmake_render_err_t pdfmake_render_stroke(pdfmake_render_ctx_t *ctx);
103 pdfmake_render_err_t pdfmake_render_clip(pdfmake_render_ctx_t *ctx);
104 void pdfmake_render_reset_clip(pdfmake_render_ctx_t *ctx);
105 uint32_t pdfmake_render_get_pixel(pdfmake_render_ctx_t *ctx, int x, int y);
106  
107 #ifndef PDFMAKE_FILL_NONZERO
108 #define PDFMAKE_FILL_NONZERO 0
109 #endif
110 #ifndef PDFMAKE_FILL_EVENODD
111 #define PDFMAKE_FILL_EVENODD 1
112 #endif
113  
114 /* -------------------------------------------------------------------------
115 * Render-page API forward declarations
116 * ------------------------------------------------------------------------- */
117 typedef enum {
118 PDFMAKE_SCALE_NEAREST = 0,
119 PDFMAKE_SCALE_BILINEAR = 1,
120 PDFMAKE_SCALE_BICUBIC = 2,
121 } pdfmake_scale_mode_t;
122  
123 typedef struct pdfmake_render_opts {
124 double dpi;
125 double scale;
126 pdfmake_scale_mode_t scale_mode;
127 int antialias;
128 double flatness;
129 pdfmake_rotation_t rotation;
130 uint32_t background;
131 double clip_x, clip_y;
132 double clip_width, clip_height;
133 int use_clip;
134 int render_text;
135 int render_images;
136 int render_vectors;
137 int render_annotations;
138 int show_text_bounds;
139 int show_image_bounds;
140 int show_clip_regions;
141 } pdfmake_render_opts_t;
142  
143 typedef struct pdfmake_page_render {
144 uint32_t *pixels;
145 int width;
146 int height;
147 int stride;
148 double page_width;
149 double page_height;
150 double effective_dpi;
151 int text_objects;
152 int path_objects;
153 int image_objects;
154 double render_time_ms;
155 int error_count;
156 char error_msg[256];
157 } pdfmake_page_render_t;
158  
159 void pdfmake_render_opts_init(pdfmake_render_opts_t *opts);
160 pdfmake_err_t pdfmake_render_page_to_pixels(pdfmake_reader_t *reader, int page_num, const pdfmake_render_opts_t *opts, pdfmake_page_render_t *result);
161 pdfmake_err_t pdfmake_render_page_region(pdfmake_reader_t *reader, int page_num, double region_x, double region_y, double region_w, double region_h, const pdfmake_render_opts_t *opts, pdfmake_page_render_t *result);
162 void pdfmake_page_render_free(pdfmake_page_render_t *result);
163 void pdfmake_page_get_render_size(pdfmake_reader_t *reader, int page_num, double dpi, int *width, int *height);
164  
165 /* Missing prototype in header, implemented in src/pdfmake_image_scale.c */
166 pdfmake_imgr_err_t pdfmake_decoded_image_resize(
167 pdfmake_decoded_image_t *img,
168 int new_w, int new_h,
169 pdfmake_interp_mode_t mode,
170 pdfmake_arena_t *arena);
171  
172 /* Portable strndup: POSIX 2008 / GNU only, not in C99/C11 and missing from
173 * Windows UCRT. Uses Newx so callers must Safefree the returned buffer.
174 * pTHX_ is required: XSUB.h redefines malloc→PerlMem_malloc on Windows
175 * PERL_IMPLICIT_SYS builds, which needs the thread context. */
176 0 static char *pdfmake_xs_strndup(pTHX_ const char *s, size_t n)
177 {
178 char *p;
179 size_t i;
180  
181 0 ▶ if (!s) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return NULL; 0 0 0
182 0 ▶ for (i = 0; i < n && s[i]; i++) ;
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && s[i]; i++) ; 0 0 0
for (i = 0; i < n && s[i]; i++) ; 0 0 0
183 0 Newx(p, i + 1, char);
184 0 memcpy(p, s, i);
185 0 p[i] = '\0';
186 0 return p;
187 }
188  
189 /* Shared helper for markup.xs: the C node tree as a plain Perl structure.
190 *
191 * Elements become { kind => 'elem', tag, attrs, children, line, col } and text
192 * becomes { kind => 'text', text, line, col }. The conversion happens while
193 * the arena is still alive and the caller frees it immediately afterwards, so
194 * nothing Perl-side ever points into C memory.
195 *
196 * Recursion is bounded by the parser's own depth limit, so the C stack here
197 * is bounded too. */
198 22665 static SV *pdfmake_markup_node_to_sv(pTHX_ const pdfmake_markup_node_t *n)
199 {
200 HV *h;
201 const pdfmake_markup_node_t *c;
202  
203 22665 ▶ if (!n) return newSV(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return newSV(0); 0 22665 22665
204  
205 22665 h = newHV();
206 22665 (void)hv_stores(h, "line", newSVuv(n->line));
207 22665 (void)hv_stores(h, "col", newSVuv(n->col));
208  
209 22665 ▶ if (n->kind == PDFMAKE_MARKUP_TEXT) {
Branch: 1 branch
BranchTrueFalseTotal
if (n->kind == PDFMAKE_MARKUP_TEXT) { 883 21782 22665
210 883 ▶ SV *t = newSVpvn(n->text ? n->text : "", n->text_len);
Branch: 1 branch
BranchTrueFalseTotal
SV *t = newSVpvn(n->text ? n->text : "", n->text_len); 883 0 883
211 883 SvUTF8_on(t);
212 883 (void)hv_stores(h, "kind", newSVpvs("text"));
213 883 (void)hv_stores(h, "text", t);
214 883 return newRV_noinc((SV *)h);
215 }
216  
217 {
218 21782 const char *name = pdfmake_markup_tag_name(n->tag);
219 21782 HV *attrs = newHV();
220 21782 AV *kids = newAV();
221 const pdfmake_markup_attr_t *a;
222  
223 21782 (void)hv_stores(h, "kind", newSVpvs("elem"));
224 21782 ▶ (void)hv_stores(h, "tag", newSVpv(name ? name : "", 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(h, "tag", newSVpv(name ? name : "", 0)); 21782 0 21782
225  
226 22613 ▶ for (a = n->attrs; a; a = a->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (a = n->attrs; a; a = a->next) { 831 21782 22613
227 831 SV *v = newSVpvn(a->value, a->value_len);
228 831 SvUTF8_on(v);
229 831 (void)hv_store(attrs, a->name, (I32)a->name_len, v, 0);
230 }
231 21782 (void)hv_stores(h, "attrs", newRV_noinc((SV *)attrs));
232  
233 43965 ▶ for (c = n->first_child; c; c = c->next_sibling)
Branch: 1 branch
BranchTrueFalseTotal
for (c = n->first_child; c; c = c->next_sibling) 22183 21782 43965
234 22183 av_push(kids, pdfmake_markup_node_to_sv(aTHX_ c));
235 21782 (void)hv_stores(h, "children", newRV_noinc((SV *)kids));
236 }
237  
238 21782 return newRV_noinc((SV *)h);
239 }
240  
241 /* Defined below, with the rest of the markup SV plumbing. */
242 static const char *pdfmake_markup_sv_bytes(pTHX_ SV *sv, STRLEN *len);
243  
244 /* Parse markup and hand back the result hash: { ok => 1, root => ... } or
245 * { ok => 0, error, line, col }. The arena is freed before returning, so
246 * nothing Perl-side ever points into C memory. */
247 2416 static SV *pdfmake_markup_parse_sv(pTHX_ SV *src)
248 {
249 STRLEN len;
250 const char *bytes;
251 pdfmake_markup_doc_t *doc;
252 HV *out;
253  
254 /* Characters or UTF-8 bytes, whichever the caller has: one is encoded,
255 * the other passed through. Upgrading unconditionally would re-encode a
256 * byte string that was already UTF-8, turning every accented character
257 * into mojibake. */
258 2416 bytes = pdfmake_markup_sv_bytes(aTHX_ src, &len);
259 2416 doc = pdfmake_markup_parse(bytes, len);
260 2416 ▶ if (!doc) croak("PDF::Make::Markup::Parse: out of memory");
Branch: 1 branch
BranchTrueFalseTotal
if (!doc) croak("PDF::Make::Markup::Parse: out of memory"); 0 2416 2416
261  
262 2416 out = newHV();
263 2416 ▶ if (!doc->ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (!doc->ok) { 1934 482 2416
264 1934 (void)hv_stores(out, "ok", newSViv(0));
265 1934 (void)hv_stores(out, "error", newSVpv(doc->err, 0));
266 1934 (void)hv_stores(out, "line", newSVuv(doc->err_line));
267 1934 (void)hv_stores(out, "col", newSVuv(doc->err_col));
268 } else {
269 482 (void)hv_stores(out, "ok", newSViv(1));
270 482 (void)hv_stores(out, "root", pdfmake_markup_node_to_sv(aTHX_ doc->root));
271 }
272 2416 pdfmake_markup_free(doc);
273 2416 return newRV_noinc((SV *)out);
274 }
275  
276 /* ---- markup style: SV plumbing only, every decision is in the C ---------- */
277  
278 /* Bytes out of an SV without re-encoding one that is already UTF-8 bytes. */
279 2946 static const char *pdfmake_markup_sv_bytes(pTHX_ SV *sv, STRLEN *len)
280 {
281 2946 ▶ if (!sv || !SvOK(sv)) { *len = 0; return ""; }
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) { *len = 0; return ""; } 2946 0 2946
if (!sv || !SvOK(sv)) { *len = 0; return ""; } 189 2757 2946
282 2757 ▶ if (SvUTF8(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) { 73 2684 2757
283 73 SV *copy = sv_2mortal(newSVsv(sv));
284 73 sv_utf8_encode(copy);
285 73 return SvPV(copy, *len);
286 }
287 2684 return SvPV(sv, *len);
288 }
289  
290 /* Croak with the position the node carries, which is what makes an error in a
291 * template actionable rather than a puzzle. */
292 23 static void pdfmake_markup_croak_at(pTHX_ SV *node, SV *what, const char *msg)
293 {
294 23 SV *out = sv_2mortal(newSVpvs(""));
295  
296 23 ▶ if (what && SvOK(what))
Branch: 2 branches
BranchTrueFalseTotal
if (what && SvOK(what)) 0 23 23
if (what && SvOK(what)) 0 0 0
297 0 sv_catpvf(out, "%" SVf " ", SVfARG(what));
298 23 sv_catpv(out, msg);
299  
300 23 ▶ if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) {
Branch: 3 branches
BranchTrueFalseTotal
if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { 8 15 23
if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { 8 0 8
if (node && SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { 8 0 8
301 8 HV *h = (HV *)SvRV(node);
302 8 SV **line = hv_fetchs(h, "line", 0);
303 8 SV **col = hv_fetchs(h, "col", 0);
304 8 ▶ if (line && col && SvOK(*line))
Branch: 3 branches
BranchTrueFalseTotal
if (line && col && SvOK(*line)) 8 0 8
if (line && col && SvOK(*line)) 8 0 8
if (line && col && SvOK(*line)) 8 0 8
305 8 sv_catpvf(out, " at line %" IVdf ", column %" IVdf,
306 (IV)SvIV(*line), (IV)SvIV(*col));
307 }
308 23 croak("%" SVf, SVfARG(out));
309 }
310  
311 /* A resolved value as the SV the Perl side expects: a number, a colour
312 * string, an alignment word, or the string itself. */
313 99 static SV *pdfmake_markup_value_sv(pTHX_ pdfmake_prop_t p,
314 const pdfmake_value_t *v)
315 {
316 99 switch (pdfmake_style_type(p)) {
317 15 case PDFMAKE_T_COLOUR:
318 15 return newSVpv(v->colour, 0);
319 24 case PDFMAKE_T_ALIGN:
320 24 ▶ return newSVpv(v->num == PDFMAKE_ALIGN_CENTER ? "center"
Branch: 2 branches
BranchTrueFalseTotal
return newSVpv(v->num == PDFMAKE_ALIGN_CENTER ? "center" 23 1 24
return newSVpv(v->num == PDFMAKE_ALIGN_CENTER ? "center" 23 0 23
321 : v->num == PDFMAKE_ALIGN_RIGHT ? "right" : "left", 0);
322 0 case PDFMAKE_T_VALIGN:
323 0 ▶ return newSVpv(v->num == PDFMAKE_VALIGN_MIDDLE ? "middle"
Branch: 2 branches
BranchTrueFalseTotal
return newSVpv(v->num == PDFMAKE_VALIGN_MIDDLE ? "middle" 0 0 0
return newSVpv(v->num == PDFMAKE_VALIGN_MIDDLE ? "middle" 0 0 0
324 : v->num == PDFMAKE_VALIGN_BOTTOM ? "bottom" : "top", 0);
325 13 case PDFMAKE_T_STR:
326 {
327 13 ▶ SV *s = newSVpvn(v->str ? v->str : "", v->str_len);
Branch: 1 branch
BranchTrueFalseTotal
SV *s = newSVpvn(v->str ? v->str : "", v->str_len); 13 0 13
328 13 SvUTF8_on(s);
329 13 return s;
330 }
331 8 case PDFMAKE_T_BOOL:
332 8 return newSViv((IV)v->num);
333 39 default:
334 /* An integral value comes back as an integer, the way "1" + 0 does in
335 * Perl. Handing back 1.0 instead broke an Int type constraint on
336 * add_page(columns => ...), which is the sort of thing a port turns
337 * up only when a document happens to use it. */
338 39 ▶ if (v->num == (double)(IV)v->num &&
Branch: 1 branch
BranchTrueFalseTotal
if (v->num == (double)(IV)v->num && 39 0 39
339 39 ▶ v->num >= (double)IV_MIN && v->num <= (double)IV_MAX)
Branch: 2 branches
BranchTrueFalseTotal
v->num >= (double)IV_MIN && v->num <= (double)IV_MAX) 39 0 39
v->num >= (double)IV_MIN && v->num <= (double)IV_MAX) 39 0 39
340 39 return newSViv((IV)v->num);
341 0 return newSVnv(v->num);
342 }
343 }
344  
345 418 static SV *pdfmake_markup_style_hv(pTHX_ const pdfmake_style_t *st)
346 {
347 418 HV *out = newHV();
348 int p;
349 12122 ▶ for (p = 1; p < PDFMAKE_P_MAX; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 1; p < PDFMAKE_P_MAX; p++) { 11704 418 12122
350 const char *name;
351 11704 ▶ if (!st->v[p].set) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!st->v[p].set) continue; 11605 99 11704
352 99 name = pdfmake_style_prop_name((pdfmake_prop_t)p);
353 99 ▶ if (!name) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) continue; 0 99 99
354 99 (void)hv_store(out, name, (I32)strlen(name),
355 pdfmake_markup_value_sv(aTHX_ (pdfmake_prop_t)p,
356 &st->v[p]), 0);
357 }
358 418 return newRV_noinc((SV *)out);
359 }
360  
361 /* Read a style hash back into the C struct, for inherit() and font_args(),
362 * which take the hashes attrs() handed out. */
363 575 static void pdfmake_markup_style_from_hv(pTHX_ SV *sv, pdfmake_style_t *st)
364 {
365 HV *h;
366 int p;
367  
368 575 pdfmake_style_init(st);
369 575 ▶ if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return; 575 0 575
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return; 510 65 575
if (!sv || !SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) return; 0 510 510
370 510 h = (HV *)SvRV(sv);
371  
372 14790 ▶ for (p = 1; p < PDFMAKE_P_MAX; p++) {
Branch: 1 branch
BranchTrueFalseTotal
for (p = 1; p < PDFMAKE_P_MAX; p++) { 14280 510 14790
373 14280 const char *name = pdfmake_style_prop_name((pdfmake_prop_t)p);
374 SV **slot;
375 STRLEN len;
376 const char *bytes;
377 char err[PDFMAKE_STYLE_ERR_LEN];
378  
379 28404 ▶ if (!name) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!name) continue; 0 14280 14280
380 14280 slot = hv_fetch(h, name, (I32)strlen(name), 0);
381 14280 ▶ if (!slot || !SvOK(*slot)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!slot || !SvOK(*slot)) continue; 156 14124 14280
if (!slot || !SvOK(*slot)) continue; 0 156 156
382  
383 156 bytes = pdfmake_markup_sv_bytes(aTHX_ *slot, &len);
384 156 switch (pdfmake_style_type((pdfmake_prop_t)p)) {
385 19 case PDFMAKE_T_COLOUR:
386 19 ▶ if (!pdfmake_style_colour(bytes, len, st->v[p].colour,
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_style_colour(bytes, len, st->v[p].colour, 0 19 19
387 err, sizeof(err)))
388 0 continue;
389 19 break;
390 29 case PDFMAKE_T_ALIGN:
391 58 ▶ st->v[p].num = (len == 6 && memcmp(bytes, "center", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
st->v[p].num = (len == 6 && memcmp(bytes, "center", 6) == 0) 0 29 29
st->v[p].num = (len == 6 && memcmp(bytes, "center", 6) == 0) 0 0 0
392 ? PDFMAKE_ALIGN_CENTER
393 29 ▶ : (len == 5 && memcmp(bytes, "right", 5) == 0)
Branch: 2 branches
BranchTrueFalseTotal
: (len == 5 && memcmp(bytes, "right", 5) == 0) 29 0 29
: (len == 5 && memcmp(bytes, "right", 5) == 0) 29 0 29
394 ? PDFMAKE_ALIGN_RIGHT : PDFMAKE_ALIGN_LEFT;
395 29 break;
396 0 case PDFMAKE_T_VALIGN:
397 0 ▶ st->v[p].num = (len == 6 && memcmp(bytes, "middle", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
st->v[p].num = (len == 6 && memcmp(bytes, "middle", 6) == 0) 0 0 0
st->v[p].num = (len == 6 && memcmp(bytes, "middle", 6) == 0) 0 0 0
398 ? PDFMAKE_VALIGN_MIDDLE
399 0 ▶ : (len == 6 && memcmp(bytes, "bottom", 6) == 0)
Branch: 2 branches
BranchTrueFalseTotal
: (len == 6 && memcmp(bytes, "bottom", 6) == 0) 0 0 0
: (len == 6 && memcmp(bytes, "bottom", 6) == 0) 0 0 0
400 ? PDFMAKE_VALIGN_BOTTOM : PDFMAKE_VALIGN_TOP;
401 0 break;
402 22 case PDFMAKE_T_STR:
403 22 st->v[p].str = bytes;
404 22 st->v[p].str_len = len;
405 22 break;
406 86 default:
407 86 st->v[p].num = SvNV(*slot);
408 86 break;
409 }
410 156 st->v[p].set = 1;
411 }
412 }
413  
414 /* One element's own attributes. The node comes from the parser as a hash, so
415 * the attribute names and values are walked here and handed to the C for the
416 * decisions. */
417 218 static SV *pdfmake_markup_style_attrs_sv(pTHX_ SV *node)
418 {
419 pdfmake_style_t st;
420 HV *nh, *attrs;
421 SV **slot;
422 STRLEN tlen;
423 const char *tag_name;
424 pdfmake_markup_tag_t tag;
425 HE *he;
426 char err[PDFMAKE_STYLE_ERR_LEN];
427  
428 218 pdfmake_style_init(&st);
429 218 ▶ if (!node || !SvROK(node) || SvTYPE(SvRV(node)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!node || !SvROK(node) || SvTYPE(SvRV(node)) != SVt_PVHV) 218 0 218
if (!node || !SvROK(node) || SvTYPE(SvRV(node)) != SVt_PVHV) 218 0 218
if (!node || !SvROK(node) || SvTYPE(SvRV(node)) != SVt_PVHV) 0 218 218
430 0 croak("PDF::Make::Markup::Style::attrs: not a node");
431 218 nh = (HV *)SvRV(node);
432  
433 218 slot = hv_fetchs(nh, "tag", 0);
434 218 ▶ tag_name = slot ? pdfmake_markup_sv_bytes(aTHX_ *slot, &tlen) : "";
Branch: 1 branch
BranchTrueFalseTotal
tag_name = slot ? pdfmake_markup_sv_bytes(aTHX_ *slot, &tlen) : ""; 218 0 218
435 218 ▶ if (!slot) tlen = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!slot) tlen = 0; 0 218 218
436 218 tag = pdfmake_markup_tag_id(tag_name, tlen);
437 218 ▶ if (tag == PDFMAKE_MK_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag == PDFMAKE_MK_INVALID) { 0 218 218
438 0 snprintf(err, sizeof(err), "no such tag '<%.*s>'", (int)tlen, tag_name);
439 0 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
440 }
441  
442 218 slot = hv_fetchs(nh, "attrs", 0);
443 218 ▶ if (!slot || !SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVHV)
Branch: 3 branches
BranchTrueFalseTotal
if (!slot || !SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVHV) 218 0 218
if (!slot || !SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVHV) 218 0 218
if (!slot || !SvROK(*slot) || SvTYPE(SvRV(*slot)) != SVt_PVHV) 0 218 218
444 0 return pdfmake_markup_style_hv(aTHX_ &st);
445 218 attrs = (HV *)SvRV(*slot);
446  
447 218 hv_iterinit(attrs);
448 261 ▶ while ((he = hv_iternext(attrs))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(attrs))) { 48 213 261
449 I32 klen;
450 48 const char *key = hv_iterkey(he, &klen);
451 48 SV *val = hv_iterval(attrs, he);
452 STRLEN vlen;
453 const char *vbytes;
454 48 pdfmake_prop_t p = pdfmake_style_prop(key, (size_t)klen);
455  
456 48 ▶ if (p == PDFMAKE_P_NONE) {
Branch: 1 branch
BranchTrueFalseTotal
if (p == PDFMAKE_P_NONE) { 1 47 48
457 1 snprintf(err, sizeof(err), "unknown attribute '%.*s' on <%.*s>",
458 (int)klen, key, (int)tlen, tag_name);
459 1 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
460 }
461 47 ▶ if (!pdfmake_style_tag_allows(tag, p)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_style_tag_allows(tag, p)) { 1 46 47
462 1 snprintf(err, sizeof(err),
463 "<%.*s> does not take '%.*s' (it takes: %s)",
464 (int)tlen, tag_name, (int)klen, key,
465 pdfmake_style_tag_allowed(tag));
466 1 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
467 }
468  
469 46 vbytes = pdfmake_markup_sv_bytes(aTHX_ val, &vlen);
470 {
471 /* One attribute through the same coercion the C uses everywhere,
472 * so a value means the same thing wherever it is written. */
473 pdfmake_markup_node_t tmp;
474 pdfmake_markup_attr_t attr;
475 pdfmake_style_t one;
476  
477 46 memset(&tmp, 0, sizeof(tmp));
478 46 memset(&attr, 0, sizeof(attr));
479 46 tmp.kind = PDFMAKE_MARKUP_ELEM;
480 46 tmp.tag = tag;
481 46 tmp.attrs = &attr;
482 46 attr.name = key;
483 46 attr.name_len = (size_t)klen;
484 46 attr.value = vbytes;
485 46 attr.value_len = vlen;
486  
487 46 ▶ if (!pdfmake_style_attrs(&tmp, &one, err, sizeof(err)))
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_style_attrs(&tmp, &one, err, sizeof(err))) 3 43 46
488 3 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
489 43 st.v[p] = one.v[p];
490 }
491 }
492 213 return pdfmake_markup_style_hv(aTHX_ &st);
493 }
494  
495 10 static SV *pdfmake_markup_style_decls_sv(pTHX_ SV *string, SV *node, SV *tag)
496 {
497 pdfmake_style_t st;
498 STRLEN slen, tlen;
499 const char *src, *tag_name;
500 pdfmake_markup_tag_t t;
501 char err[PDFMAKE_STYLE_ERR_LEN];
502  
503 10 src = pdfmake_markup_sv_bytes(aTHX_ string, &slen);
504 10 tag_name = pdfmake_markup_sv_bytes(aTHX_ tag, &tlen);
505 10 t = pdfmake_markup_tag_id(tag_name, tlen);
506  
507 10 ▶ if (t == PDFMAKE_MK_INVALID) {
Branch: 1 branch
BranchTrueFalseTotal
if (t == PDFMAKE_MK_INVALID) { 1 9 10
508 1 snprintf(err, sizeof(err), "no such tag '<%.*s>' in <style>",
509 (int)tlen, tag_name);
510 1 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
511 }
512 9 ▶ if (!pdfmake_style_declarations(src, slen, t, &st, err, sizeof(err)))
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_style_declarations(src, slen, t, &st, err, sizeof(err))) 2 7 9
513 2 pdfmake_markup_croak_at(aTHX_ node, NULL, err);
514  
515 7 return pdfmake_markup_style_hv(aTHX_ &st);
516 }
517  
518 198 static SV *pdfmake_markup_style_inherit_sv(pTHX_ SV *parent, SV *own)
519 {
520 pdfmake_style_t p, o, out;
521 198 pdfmake_markup_style_from_hv(aTHX_ parent, &p);
522 198 pdfmake_markup_style_from_hv(aTHX_ own, &o);
523 198 pdfmake_style_inherit(&p, &o, &out);
524 198 return pdfmake_markup_style_hv(aTHX_ &out);
525 }
526  
527 179 static SV *pdfmake_markup_style_font_sv(pTHX_ SV *style)
528 {
529 pdfmake_style_t st;
530 179 HV *out = newHV();
531  
532 179 pdfmake_markup_style_from_hv(aTHX_ style, &st);
533  
534 179 ▶ if (st.v[PDFMAKE_P_SIZE].set)
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_SIZE].set) 6 173 179
535 6 (void)hv_stores(out, "size", newSVnv(st.v[PDFMAKE_P_SIZE].num));
536 179 ▶ if (st.v[PDFMAKE_P_COLOUR].set)
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_COLOUR].set) 5 174 179
537 5 (void)hv_stores(out, "colour", newSVpv(st.v[PDFMAKE_P_COLOUR].colour, 0));
538 179 ▶ if (st.v[PDFMAKE_P_FONT].set) {
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_FONT].set) { 1 178 179
539 1 SV *s = newSVpvn(st.v[PDFMAKE_P_FONT].str, st.v[PDFMAKE_P_FONT].str_len);
540 1 SvUTF8_on(s);
541 1 (void)hv_stores(out, "family", s);
542 }
543 179 ▶ if (st.v[PDFMAKE_P_BOLD].set)
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_BOLD].set) 18 161 179
544 18 (void)hv_stores(out, "bold", newSViv((IV)st.v[PDFMAKE_P_BOLD].num));
545 179 ▶ if (st.v[PDFMAKE_P_ITALIC].set)
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_ITALIC].set) 2 177 179
546 2 (void)hv_stores(out, "italic", newSViv((IV)st.v[PDFMAKE_P_ITALIC].num));
547 179 ▶ if (st.v[PDFMAKE_P_LINE_HEIGHT].set)
Branch: 1 branch
BranchTrueFalseTotal
if (st.v[PDFMAKE_P_LINE_HEIGHT].set) 1 178 179
548 1 (void)hv_stores(out, "line_height",
549 newSVnv(st.v[PDFMAKE_P_LINE_HEIGHT].num));
550  
551 179 return newRV_noinc((SV *)out);
552 }
553  
554 /* ---- markup profile ------------------------------------------------------
555 *
556 * Template::Stencil is constructed and driven through its own interface,
557 * which is Perl methods. That call happens here rather than in a .pm so that
558 * the arguments - escaping on, strictness on, this filter map and no other -
559 * are not something a caller can reach past. Using Stencil's C ABI instead
560 * would save a method dispatch per render and cost a build-time dependency on
561 * its headers; the dispatch is not what a render spends its time on, so it
562 * stays a method call until a profile says otherwise.
563 */
564  
565 0 static SV *pdfmake_markup_profile_filters(pTHX)
566 {
567 0 HV *out = newHV();
568 0 CV *money = get_cv("PDF::Make::Markup::Profile::_filter_money", 0);
569 0 CV *number = get_cv("PDF::Make::Markup::Profile::_filter_number", 0);
570 0 ▶ if (money) (void)hv_stores(out, "money", newRV_inc((SV *)money));
Branch: 1 branch
BranchTrueFalseTotal
if (money) (void)hv_stores(out, "money", newRV_inc((SV *)money)); 0 0 0
571 0 ▶ if (number) (void)hv_stores(out, "number", newRV_inc((SV *)number));
Branch: 1 branch
BranchTrueFalseTotal
if (number) (void)hv_stores(out, "number", newRV_inc((SV *)number)); 0 0 0
572 0 return newRV_noinc((SV *)out);
573 }
574  
575 0 static SV *pdfmake_markup_profile_engine(pTHX_ HV *args)
576 {
577 0 dSP;
578 SV *engine;
579 HE *he;
580 int count;
581  
582 /* Load it here rather than relying on the caller having done so. Without
583 * this the constructor call fails method resolution, which surfaces as an
584 * error with no message at all - the least useful failure there is. */
585 0 ▶ if (!gv_stashpvs("Template::Stencil", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_stashpvs("Template::Stencil", 0)) { 0 0 0
586 0 load_module(PERL_LOADMOD_NOIMPORT,
587 newSVpvs("Template::Stencil"), NULL);
588 0 ▶ if (!gv_stashpvs("Template::Stencil", 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_stashpvs("Template::Stencil", 0)) { 0 0 0
589 0 SvREFCNT_dec((SV *)args);
590 0 croak("PDF::Make::Markup::Profile needs Template::Stencil");
591 }
592 }
593  
594 0 ENTER;
595 0 SAVETMPS;
596 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
597 0 ▶ XPUSHs(sv_2mortal(newSVpvs("Template::Stencil")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("Template::Stencil"))); 0 0 0
598  
599 0 hv_iterinit(args);
600 0 ▶ while ((he = hv_iternext(args))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(args))) { 0 0 0
601 I32 klen;
602 0 const char *k = hv_iterkey(he, &klen);
603 0 ▶ XPUSHs(sv_2mortal(newSVpvn(k, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(k, klen))); 0 0 0
604 0 ▶ XPUSHs(sv_2mortal(newSVsv(hv_iterval(args, he))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(hv_iterval(args, he)))); 0 0 0
605 }
606 0 SvREFCNT_dec((SV *)args);
607 0 PUTBACK;
608  
609 0 count = call_method("new", G_SCALAR | G_EVAL);
610 0 SPAGAIN;
611  
612 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
613 /* Copy before FREETMPS, which frees anything mortalised in here. */
614 char msg[512];
615 STRLEN elen;
616 0 ▶ const char *etext = SvPV(ERRSV, elen);
Branch: 1 branch
BranchTrueFalseTotal
const char *etext = SvPV(ERRSV, elen); 0 0 0
617 0 ▶ if (elen >= sizeof(msg)) elen = sizeof(msg) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (elen >= sizeof(msg)) elen = sizeof(msg) - 1; 0 0 0
618 0 memcpy(msg, etext, elen);
619 0 msg[elen] = 0;
620 0 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 0 0 0
621 0 PUTBACK;
622 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
623 0 LEAVE;
624 0 ▶ croak("PDF::Make::Markup::Profile: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("PDF::Make::Markup::Profile: %s", 0 0 0
625 elen ? msg : "Template::Stencil->new failed with no message");
626 }
627  
628 /* Pop into a local before touching it: before 5.30 the SvREFCNT_inc
629 * macros evaluated their argument twice, so inc(POPs) popped twice. */
630 {
631 0 ▶ SV *tmp = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *tmp = count > 0 ? POPs : &PL_sv_undef; 0 0 0
632 0 engine = newSVsv(tmp);
633 }
634 0 PUTBACK;
635 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
636 0 LEAVE;
637 0 return engine;
638 }
639  
640 0 static SV *pdfmake_markup_profile_render(pTHX_ SV *src, SV *data, SV *engine,
641 HV *opts, UV max)
642 {
643 0 dSP;
644 STRLEN len;
645 const char *bytes;
646 char err[PDFMAKE_PROFILE_ERR_LEN];
647 0 uint32_t line = 0;
648 SV *out;
649 int count;
650  
651 0 bytes = pdfmake_markup_sv_bytes(aTHX_ src, &len);
652 0 ▶ if (!pdfmake_profile_check_source(bytes, len, &line, err, sizeof(err)))
Branch: 1 branch
BranchTrueFalseTotal
if (!pdfmake_profile_check_source(bytes, len, &line, err, sizeof(err))) 0 0 0
653 0 croak("template error at line %" UVuf ": %s", (UV)line, err);
654  
655 0 ▶ if (!engine || !SvOK(engine)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!engine || !SvOK(engine)) { 0 0 0
if (!engine || !SvOK(engine)) { 0 0 0
656 0 HV *args = newHV();
657 HE *he;
658 0 (void)hv_stores(args, "auto_escape", newSViv(1));
659 0 (void)hv_stores(args, "strict", newSViv(1));
660 0 (void)hv_stores(args, "sort_keys", newSViv(1));
661 0 (void)hv_stores(args, "cache", newSViv(1));
662 0 (void)hv_stores(args, "filters", pdfmake_markup_profile_filters(aTHX));
663 0 hv_iterinit(opts);
664 0 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 0 0 0
665 I32 klen;
666 0 const char *k = hv_iterkey(he, &klen);
667 0 (void)hv_store(args, k, klen, newSVsv(hv_iterval(opts, he)), 0);
668 }
669 0 engine = sv_2mortal(pdfmake_markup_profile_engine(aTHX_ args));
670 }
671  
672 /* Building the engine called into Perl, which can reallocate the stack.
673 * The SP taken at the top of this function may now point into freed
674 * memory, and pushing through it puts garbage where the arguments should
675 * be - which shows up as "uninitialized value in subroutine entry" and a
676 * call that receives nothing it was given. */
677 0 SPAGAIN;
678  
679 0 ENTER;
680 0 SAVETMPS;
681 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
682 0 ▶ XPUSHs(engine);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(engine); 0 0 0
683 0 ▶ XPUSHs(src);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(src); 0 0 0
684 0 ▶ XPUSHs(data && SvOK(data) ? data
Branch: 3 branches
BranchTrueFalseTotal
XPUSHs(data && SvOK(data) ? data 0 0 0
XPUSHs(data && SvOK(data) ? data 0 0 0
XPUSHs(data && SvOK(data) ? data 0 0 0
685 : sv_2mortal(newRV_noinc((SV *)newHV())));
686 0 PUTBACK;
687  
688 0 count = call_method("render", G_SCALAR | G_EVAL);
689 0 SPAGAIN;
690  
691 0 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 0 0 0
if (SvTRUE(ERRSV)) { 0 0 0
692 /* Copy the message out before FREETMPS: an SV mortalised inside this
693 * scope is freed by it, and croaking with the corpse prints nothing
694 * at all. */
695 char msg[512];
696 STRLEN elen;
697 0 ▶ const char *etext = SvPV(ERRSV, elen);
Branch: 1 branch
BranchTrueFalseTotal
const char *etext = SvPV(ERRSV, elen); 0 0 0
698 0 ▶ if (elen >= sizeof(msg)) elen = sizeof(msg) - 1;
Branch: 1 branch
BranchTrueFalseTotal
if (elen >= sizeof(msg)) elen = sizeof(msg) - 1; 0 0 0
699 0 memcpy(msg, etext, elen);
700 0 msg[elen] = 0;
701 0 ▶ if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) (void)POPs; 0 0 0
702 0 PUTBACK;
703 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
704 0 LEAVE;
705 0 ▶ croak("%s", elen ? msg : "template render failed with no message");
Branch: 1 branch
BranchTrueFalseTotal
croak("%s", elen ? msg : "template render failed with no message"); 0 0 0
706 }
707  
708 {
709 0 ▶ SV *tmp = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *tmp = count > 0 ? POPs : &PL_sv_undef; 0 0 0
710 0 out = newSVsv(tmp);
711 }
712 0 PUTBACK;
713 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
714 0 LEAVE;
715  
716 0 ▶ if (max && SvOK(out)) {
Branch: 2 branches
BranchTrueFalseTotal
if (max && SvOK(out)) { 0 0 0
if (max && SvOK(out)) { 0 0 0
717 STRLEN olen;
718 (void)SvPV(out, olen);
719 0 ▶ if ((UV)olen > max) {
Branch: 1 branch
BranchTrueFalseTotal
if ((UV)olen > max) { 0 0 0
720 0 SvREFCNT_dec(out);
721 0 croak("template produced %" UVuf " bytes of markup, over the "
722 "%" UVuf " byte limit", (UV)olen, max);
723 }
724 }
725 0 return out;
726 }
727  
728 /* ---- markup render -------------------------------------------------------
729 *
730 * Profile, then parse, then build. The build stage is still Perl - it drives
731 * PDF::Make::Builder, which is Perl - so this chains three calls rather than
732 * pretending otherwise. When the document compiler is C, only this function
733 * changes.
734 */
735  
736 #ifndef PDFMAKE_ENGINE_VERSION
737 #define PDFMAKE_ENGINE_VERSION 1
738 #endif
739  
740 42 static SV *pdfmake_markup_call_1(pTHX_ const char *pkg, const char *method,
741 SV *a, SV *b, SV *c)
742 {
743 42 dSP;
744 SV *out;
745 int count;
746  
747 42 ENTER;
748 42 SAVETMPS;
749 42 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 42 42
750 42 ▶ XPUSHs(sv_2mortal(newSVpv(pkg, 0)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpv(pkg, 0))); 0 42 42
751 42 ▶ if (a) XPUSHs(a);
Branch: 2 branches
BranchTrueFalseTotal
if (a) XPUSHs(a); 42 0 42
if (a) XPUSHs(a); 0 42 42
752 42 ▶ if (b) XPUSHs(b);
Branch: 2 branches
BranchTrueFalseTotal
if (b) XPUSHs(b); 21 21 42
if (b) XPUSHs(b); 0 21 21
753 42 ▶ if (c) XPUSHs(c);
Branch: 2 branches
BranchTrueFalseTotal
if (c) XPUSHs(c); 21 21 42
if (c) XPUSHs(c); 0 21 21
754 42 PUTBACK;
755  
756 42 count = call_method(method, G_SCALAR);
757 42 SPAGAIN;
758  
759 {
760 42 ▶ SV *tmp = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *tmp = count > 0 ? POPs : &PL_sv_undef; 42 0 42
761 42 out = newSVsv(tmp);
762 }
763 42 PUTBACK;
764 42 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 42 0 42
765 42 LEAVE;
766 42 return out;
767 }
768  
769 21 static SV *pdfmake_markup_render_sv(pTHX_ SV *template, SV *data,
770 HV *opts, SV *file_name)
771 {
772 SV *markup, *root, *pdf, *bytes;
773 HE *he;
774  
775 21 ▶ if (data) {
Branch: 1 branch
BranchTrueFalseTotal
if (data) { 0 21 21
776 /* Through the profile: escaping on, filters fixed, {% raw %} refused. */
777 0 dSP;
778 int count;
779 0 ENTER;
780 0 SAVETMPS;
781 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
782 0 ▶ XPUSHs(sv_2mortal(newSVpvs("PDF::Make::Markup::Profile")));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvs("PDF::Make::Markup::Profile"))); 0 0 0
783 0 ▶ XPUSHs(template);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(template); 0 0 0
784 0 ▶ XPUSHs(SvOK(data) ? data : sv_2mortal(newRV_noinc((SV *)newHV())));
Branch: 2 branches
BranchTrueFalseTotal
XPUSHs(SvOK(data) ? data : sv_2mortal(newRV_noinc((SV *)newHV()))); 0 0 0
XPUSHs(SvOK(data) ? data : sv_2mortal(newRV_noinc((SV *)newHV()))); 0 0 0
785 0 hv_iterinit(opts);
786 0 ▶ while ((he = hv_iternext(opts))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(opts))) { 0 0 0
787 I32 klen;
788 0 const char *k = hv_iterkey(he, &klen);
789 /* Only what the profile understands; the rest is for the build. */
790 0 ▶ if ((klen == 6 && memcmp(k, "engine", 6) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
if ((klen == 6 && memcmp(k, "engine", 6) == 0) || 0 0 0
if ((klen == 6 && memcmp(k, "engine", 6) == 0) || 0 0 0
791 0 ▶ (klen == 12 && memcmp(k, "template_dir", 12) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
(klen == 12 && memcmp(k, "template_dir", 12) == 0) || 0 0 0
(klen == 12 && memcmp(k, "template_dir", 12) == 0) || 0 0 0
792 0 ▶ (klen == 5 && memcmp(k, "cache", 5) == 0) ||
Branch: 2 branches
BranchTrueFalseTotal
(klen == 5 && memcmp(k, "cache", 5) == 0) || 0 0 0
(klen == 5 && memcmp(k, "cache", 5) == 0) || 0 0 0
793 0 ▶ (klen == 10 && memcmp(k, "max_output", 10) == 0)) {
Branch: 2 branches
BranchTrueFalseTotal
(klen == 10 && memcmp(k, "max_output", 10) == 0)) { 0 0 0
(klen == 10 && memcmp(k, "max_output", 10) == 0)) { 0 0 0
794 0 ▶ XPUSHs(sv_2mortal(newSVpvn(k, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(k, klen))); 0 0 0
795 0 ▶ XPUSHs(sv_2mortal(newSVsv(hv_iterval(opts, he))));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVsv(hv_iterval(opts, he)))); 0 0 0
796 }
797 }
798 0 PUTBACK;
799 0 count = call_method("render", G_SCALAR);
800 0 SPAGAIN;
801 {
802 /* Not mortal yet: a mortal made after SAVETMPS is freed by the
803 * FREETMPS below, and the markup would be gone before it could be
804 * parsed. Mortalise once the scope it belongs to is the caller's. */
805 0 ▶ SV *tmp = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *tmp = count > 0 ? POPs : &PL_sv_undef; 0 0 0
806 0 markup = newSVsv(tmp);
807 }
808 0 PUTBACK;
809 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
810 0 LEAVE;
811 0 markup = sv_2mortal(markup);
812 } else {
813 21 markup = template;
814 }
815  
816 21 root = sv_2mortal(pdfmake_markup_call_1(aTHX_
817 "PDF::Make::Markup::Parse", "parse", markup, NULL, NULL));
818  
819 /* The builder writes through a file name; when the caller only wants
820 * bytes, give it one it will never use rather than a temporary file on
821 * disk. Nothing is written unless save() is called. */
822 21 ▶ pdf = sv_2mortal(pdfmake_markup_call_1(aTHX_
Branch: 2 branches
BranchTrueFalseTotal
pdf = sv_2mortal(pdfmake_markup_call_1(aTHX_ 0 21 21
pdf = sv_2mortal(pdfmake_markup_call_1(aTHX_ 0 0 0
823 "PDF::Make::Markup::Build", "build", root,
824 sv_2mortal(newSVpvs("file_name")),
825 file_name && SvOK(file_name) ? file_name
826 : sv_2mortal(newSVpvs("unsaved"))));
827  
828 21 ▶ if (file_name && SvOK(file_name)) {
Branch: 2 branches
BranchTrueFalseTotal
if (file_name && SvOK(file_name)) { 0 21 21
if (file_name && SvOK(file_name)) { 0 0 0
829 0 dSP;
830 0 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 0 0
831 0 ▶ XPUSHs(pdf);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(pdf); 0 0 0
832 0 PUTBACK;
833 0 (void)call_method("save", G_VOID | G_DISCARD);
834 0 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 0 0
835 }
836  
837 {
838 21 dSP;
839 int count;
840 21 ▶ ENTER; SAVETMPS; PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); 0 21 21
841 21 ▶ XPUSHs(pdf);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(pdf); 0 21 21
842 21 PUTBACK;
843 21 count = call_method("to_bytes", G_SCALAR);
844 21 SPAGAIN;
845 {
846 21 ▶ SV *tmp = count > 0 ? POPs : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
SV *tmp = count > 0 ? POPs : &PL_sv_undef; 21 0 21
847 21 bytes = newSVsv(tmp);
848 }
849 21 PUTBACK;
850 21 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 21 0 21
851 }
852 21 return bytes;
853 }
854  
855 /* Shared helper for render_page.xs */
856 0 static void hv_to_render_opts(pTHX_ HV *hv, pdfmake_render_opts_t *opts)
857 {
858 SV **sv;
859  
860 0 pdfmake_render_opts_init(opts);
861  
862 0 ▶ if (!hv) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!hv) return; 0 0 0
863  
864 0 ▶ if ((sv = hv_fetchs(hv, "dpi", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "dpi", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "dpi", 0)) && SvOK(*sv)) { 0 0 0
865 0 opts->dpi = SvNV(*sv);
866 }
867 0 ▶ if ((sv = hv_fetchs(hv, "scale", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "scale", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "scale", 0)) && SvOK(*sv)) { 0 0 0
868 0 opts->scale = SvNV(*sv);
869 }
870 0 ▶ if ((sv = hv_fetchs(hv, "scale_mode", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "scale_mode", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "scale_mode", 0)) && SvOK(*sv)) { 0 0 0
871 0 opts->scale_mode = (pdfmake_scale_mode_t)SvIV(*sv);
872 }
873 0 ▶ if ((sv = hv_fetchs(hv, "antialias", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "antialias", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "antialias", 0)) && SvOK(*sv)) { 0 0 0
874 0 opts->antialias = SvIV(*sv);
875 }
876 0 ▶ if ((sv = hv_fetchs(hv, "flatness", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "flatness", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "flatness", 0)) && SvOK(*sv)) { 0 0 0
877 0 opts->flatness = SvNV(*sv);
878 }
879 0 ▶ if ((sv = hv_fetchs(hv, "rotation", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "rotation", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "rotation", 0)) && SvOK(*sv)) { 0 0 0
880 0 opts->rotation = (pdfmake_rotation_t)SvIV(*sv);
881 }
882 0 ▶ if ((sv = hv_fetchs(hv, "background", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "background", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "background", 0)) && SvOK(*sv)) { 0 0 0
883 0 opts->background = (uint32_t)SvUV(*sv);
884 }
885 0 ▶ if ((sv = hv_fetchs(hv, "render_text", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "render_text", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "render_text", 0)) && SvOK(*sv)) { 0 0 0
886 0 opts->render_text = SvIV(*sv);
887 }
888 0 ▶ if ((sv = hv_fetchs(hv, "render_images", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "render_images", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "render_images", 0)) && SvOK(*sv)) { 0 0 0
889 0 opts->render_images = SvIV(*sv);
890 }
891 0 ▶ if ((sv = hv_fetchs(hv, "render_vectors", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "render_vectors", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "render_vectors", 0)) && SvOK(*sv)) { 0 0 0
892 0 opts->render_vectors = SvIV(*sv);
893 }
894 0 ▶ if ((sv = hv_fetchs(hv, "render_annotations", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "render_annotations", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "render_annotations", 0)) && SvOK(*sv)) { 0 0 0
895 0 opts->render_annotations = SvIV(*sv);
896 }
897 0 ▶ if ((sv = hv_fetchs(hv, "show_text_bounds", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "show_text_bounds", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "show_text_bounds", 0)) && SvOK(*sv)) { 0 0 0
898 0 opts->show_text_bounds = SvIV(*sv);
899 }
900 0 ▶ if ((sv = hv_fetchs(hv, "show_image_bounds", 0)) && SvOK(*sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if ((sv = hv_fetchs(hv, "show_image_bounds", 0)) && SvOK(*sv)) { 0 0 0
if ((sv = hv_fetchs(hv, "show_image_bounds", 0)) && SvOK(*sv)) { 0 0 0
901 0 opts->show_image_bounds = SvIV(*sv);
902 }
903 }
904  
905 /* XOP descriptors — registered once in main BOOT, used by all modules */
906 static XOP pdfmake_chain_xop;
907 static XOP pdfmake_getter_xop;
908  
909 /* Writer wrapper struct for XS binding. */
910 typedef struct {
911 pdfmake_buf_t buf;
912 } pdfmake_writer_xs_t;
913  
914 /* Arena wrapper struct - owns the arena. */
915 typedef struct {
916 pdfmake_arena_t *arena;
917 } pdfmake_arena_xs_t;
918  
919 /* Parser wrapper struct - owns parser, keeps buffer alive. */
920 typedef struct {
921 pdfmake_parser_t *parser;
922 SV *bytes_sv; /* Keep input buffer alive for parser */
923 pdfmake_doc_t *doc; /* Parsed document (NULL until parsed) */
924 int parsed; /* Whether parse() has been called */
925 } pdfmake_parser_xs_t;
926  
927 /* Reader wrapper struct - owns reader, references parser. */
928 typedef struct {
929 pdfmake_reader_t *reader;
930 SV *parser_sv; /* SV ref to parser to keep it alive */
931 } pdfmake_reader_xs_t;
932  
933 /* Reader page wrapper - references reader. */
934 typedef struct {
935 pdfmake_reader_page_t *page;
936 SV *reader_sv; /* SV ref to reader to keep it alive */
937 } pdfmake_reader_page_xs_t;
938  
939 /* Obj wrapper struct - holds arena and pointer to obj in arena. */
940 typedef struct {
941 pdfmake_arena_xs_t *arena_xs; /* Ref to arena wrapper (kept alive via SV) */
942 SV *arena_sv; /* SV ref to arena to prevent GC */
943 pdfmake_obj_t *obj; /* Pointer to obj allocated in arena */
944 } pdfmake_obj_xs_t;
945 /* Encryption context wrapper. */
946 typedef struct {
947 pdfmake_crypt_ctx_t ctx;
948 } pdfmake_crypt_xs_t;
949  
950 MODULE = PDF::Make PACKAGE = PDF::Make
951 PROTOTYPES: ENABLE
952  
953 const char *
954 version()
955 CODE:
956 3 RETVAL = pdfmake_version();
957 OUTPUT:
958 RETVAL
959  
960 BOOT:
961 {
962 /* Register custom op XOPs */
963 104 PDFMAKE_REGISTER_XOP(pdfmake_chain_xop, pp_pdfmake_chain,
964 "pdfmake_chain", "PDF::Make chainable method");
965 104 PDFMAKE_REGISTER_XOP(pdfmake_getter_xop, pp_pdfmake_getter,
966 "pdfmake_getter", "PDF::Make struct getter");
967  
968 /* Register additional XOPs */
969 static XOP pdfmake_meta_xop;
970 104 PDFMAKE_REGISTER_XOP(pdfmake_meta_xop, pp_pdfmake_meta,
971 "pdfmake_meta", "PDF::Make metadata getter/setter");
972 static XOP pdfmake_indirect_xop;
973 104 PDFMAKE_REGISTER_XOP(pdfmake_indirect_xop, pp_pdfmake_indirect_getter,
974 "pdfmake_indirect", "PDF::Make indirect getter");
975 static XOP pdfmake_typetest_xop;
976 104 PDFMAKE_REGISTER_XOP(pdfmake_typetest_xop, pp_pdfmake_typetest,
977 "pdfmake_typetest", "PDF::Make type test");
978 static XOP pdfmake_arena_ctor_xop;
979 104 PDFMAKE_REGISTER_XOP(pdfmake_arena_ctor_xop, pp_pdfmake_arena_ctor,
980 "pdfmake_arena_ctor", "PDF::Make arena constructor");
981  
982 /* Per-module BOOT sections register their own getters, constants,
983 * and chain dispatch tables via INCLUDE: directives below */
984 }
985  
986 INCLUDE: xs/writer.xs
987 INCLUDE: xs/document.xs
988 INCLUDE: xs/page.xs
989 INCLUDE: xs/canvas.xs
990 INCLUDE: xs/parser.xs
991 INCLUDE: xs/arena.xs
992 INCLUDE: xs/obj.xs
993 INCLUDE: xs/reader.xs
994 INCLUDE: xs/image.xs
995 INCLUDE: xs/font.xs
996 INCLUDE: xs/extract.xs
997 INCLUDE: xs/outline.xs
998 INCLUDE: xs/action.xs
999 INCLUDE: xs/crypt.xs
1000 INCLUDE: xs/form.xs
1001 INCLUDE: xs/layer.xs
1002 INCLUDE: xs/attach.xs
1003 INCLUDE: xs/tag.xs
1004 INCLUDE: xs/redact.xs
1005 INCLUDE: xs/color.xs
1006 INCLUDE: xs/watermark.xs
1007 INCLUDE: xs/signature.xs
1008 INCLUDE: xs/linear.xs
1009 INCLUDE: xs/render.xs
1010 INCLUDE: xs/render_page.xs
1011 INCLUDE: xs/image_render.xs
1012 INCLUDE: xs/import.xs
1013 INCLUDE: xs/filter.xs
1014 INCLUDE: xs/markup.xs
1015 INCLUDE: xs/markup_style.xs
1016 INCLUDE: xs/markup_profile.xs
1017 INCLUDE: xs/markup_render.xs
« xs/action.xs ↑ Summary xs/crypt.xs »