Make.xs

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