Flex.xs

filter: Statementstmt 97.6% 289 / 296 Branchbran 73.5% 197 / 268 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 86.1% 486 / 564 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary include/flex.h »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #ifdef WIN32
7 # undef malloc
8 # undef calloc
9 # undef realloc
10 # undef free
11 #endif
12  
13 #include "flex.h"
14 #include "fm.h"
15  
16 /* Parse an align string into an LF_ALIGN_* constant, -1 if unrecognised */
17 46 static int lf_parse_align(const char *s) {
18 46 ▶ if (!s) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return -1; 0 46 46
19 46 ▶ if (strEQ(s, "stretch")) return LF_ALIGN_STRETCH;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(s, "stretch")) return LF_ALIGN_STRETCH; 2 44 46
20 44 ▶ if (strEQ(s, "start")) return LF_ALIGN_START;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(s, "start")) return LF_ALIGN_START; 38 6 44
21 6 ▶ if (strEQ(s, "end")) return LF_ALIGN_END;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(s, "end")) return LF_ALIGN_END; 3 3 6
22 3 ▶ if (strEQ(s, "center")) return LF_ALIGN_CENTER;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(s, "center")) return LF_ALIGN_CENTER; 3 0 3
23 0 return -1;
24 }
25  
26 /* Fetch a float from a hashref field; no-op if key absent */
27 2386 static void lf_hv_float(pTHX_ HV *hv, const char *key, I32 klen, float *dst) {
28 2386 SV **sv = hv_fetch(hv, key, klen, 0);
29 2386 ▶ if (sv && SvOK(*sv)) *dst = (float)SvNV(*sv);
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(*sv)) *dst = (float)SvNV(*sv); 302 2084 2386
if (sv && SvOK(*sv)) *dst = (float)SvNV(*sv); 302 0 302
30 2386 }
31  
32 /* Built-in proportional text measurement: 0.6em wide per char, 1.4em tall */
33 15 static void lf_measure_simple(const char *text, float font_size,
34 float *w, float *h) {
35 15 size_t len = strlen(text);
36 15 *w = (float)len * font_size * 0.6f;
37 15 *h = font_size * 1.4f;
38 15 }
39  
40 /* Built-in wrapped measurement: reflow text into avail_w, stack lines */
41 5 static void lf_measure_simple_wrapped(const char *text, float font_size,
42 float avail_w, float *w, float *h) {
43 5 float char_w = font_size * 0.6f;
44 5 float line_h = font_size * 1.4f;
45 5 size_t len = strlen(text);
46 5 ▶ int cpl = (char_w > 0.0f && avail_w > 0.0f)
Branch: 1 branch
BranchTrueFalseTotal
int cpl = (char_w > 0.0f && avail_w > 0.0f) 5 0 5
47 10 ▶ ? (int)(avail_w / char_w) : (int)len;
Branch: 1 branch
BranchTrueFalseTotal
? (int)(avail_w / char_w) : (int)len; 5 0 5
48 5 ▶ if (cpl < 1) cpl = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (cpl < 1) cpl = 1; 0 5 5
49 5 int n_lines = ((int)len + cpl - 1) / cpl;
50 5 ▶ if (n_lines < 1) n_lines = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (n_lines < 1) n_lines = 1; 0 5 5
51 5 *w = avail_w;
52 5 *h = (float)n_lines * line_h;
53 5 }
54  
55 /* Call a Perl code-ref measure(hashref) → (w, h) */
56 8 static void lf_measure_cb(pTHX_ SV *cb, HV *item_hv, float *w, float *h) {
57 8 dSP;
58 int count;
59 8 *w = 0.0f; *h = 0.0f;
60 8 ENTER; SAVETMPS;
61 8 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 8 8
62 8 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)item_hv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)item_hv))); 0 8 8
63 8 PUTBACK;
64 8 count = call_sv(cb, G_ARRAY);
65 8 SPAGAIN;
66 8 ▶ if (count >= 2) { *h = (float)POPn; *w = (float)POPn; }
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 2) { *h = (float)POPn; *w = (float)POPn; } 8 0 8
67 0 ▶ else if (count == 1) { *w = (float)POPn; }
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 1) { *w = (float)POPn; } 0 0 0
68 8 PUTBACK;
69 8 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 8 0 8
70 8 }
71  
72 /* Call a Perl code-ref measure(hashref, avail_w) → (w, h) for wrapped text */
73 3 static void lf_measure_cb_wrapped(pTHX_ SV *cb, HV *item_hv,
74 float avail_w, float *w, float *h) {
75 3 dSP;
76 int count;
77 3 *w = avail_w; *h = 0.0f;
78 3 ENTER; SAVETMPS;
79 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
80 3 ▶ XPUSHs(sv_2mortal(newRV_inc((SV *)item_hv)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newRV_inc((SV *)item_hv))); 0 3 3
81 3 ▶ XPUSHs(sv_2mortal(newSVnv((NV)avail_w)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVnv((NV)avail_w))); 0 3 3
82 3 PUTBACK;
83 3 count = call_sv(cb, G_ARRAY);
84 3 SPAGAIN;
85 3 ▶ if (count >= 2) { *h = (float)POPn; *w = (float)POPn; }
Branch: 1 branch
BranchTrueFalseTotal
if (count >= 2) { *h = (float)POPn; *w = (float)POPn; } 3 0 3
86 0 ▶ else if (count == 1) { *h = (float)POPn; }
Branch: 1 branch
BranchTrueFalseTotal
else if (count == 1) { *h = (float)POPn; } 0 0 0
87 3 PUTBACK;
88 3 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 3 0 3
89 3 }
90  
91 /* ── Font::Metrics measure helpers (direct C calls, no Perl dispatch) ── */
92  
93 static void
94 10 lf_measure_fm(const fm_font_t *fm, const char *text, int is_utf8, float size, float line_h,
95 float *w, float *h)
96 {
97 10 ▶ *w = is_utf8 ? fm_string_width_utf8(fm, text, size) : fm_string_width(fm, text, size);
Branch: 1 branch
BranchTrueFalseTotal
*w = is_utf8 ? fm_string_width_utf8(fm, text, size) : fm_string_width(fm, text, size); 0 10 10
98 10 *h = line_h;
99 10 }
100  
101 static void
102 3 lf_measure_fm_wrapped(const fm_font_t *fm, const char *text, int is_utf8, float size,
103 float line_h, float avail_w, float *w, float *h)
104 {
105 3 size_t tlen = strlen(text);
106 3 char *buf = (char *)malloc(tlen + 2);
107 const char *p, *ws; size_t wlen, llen; int lines; float cw;
108 3 buf[0] = '\0'; llen = 0; lines = 0;
109 3 p = text;
110 3 ▶ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++;
Branch: 4 branches
BranchTrueFalseTotal
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 3 3
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 3 3
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 3 3
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 3 3
111 10 ▶ while (*p) {
Branch: 1 branch
BranchTrueFalseTotal
while (*p) { 7 3 10
112 7 ws = p;
113 24 ▶ while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++;
Branch: 5 branches
BranchTrueFalseTotal
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; 21 3 24
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; 17 4 21
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; 17 0 17
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; 17 0 17
while (*p && *p != ' ' && *p != '\t' && *p != '\n' && *p != '\r') p++; 17 0 17
114 7 wlen = (size_t)(p - ws);
115 7 ▶ if (llen > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (llen > 0) { 4 3 7
116 4 buf[llen] = ' ';
117 4 memcpy(buf + llen + 1, ws, wlen);
118 4 buf[llen + 1 + wlen] = '\0';
119 0 cw = is_utf8 ? fm_string_width_utf8(fm, buf, size)
120 4 ▶ : fm_string_width(fm, buf, size);
Branch: 1 branch
BranchTrueFalseTotal
: fm_string_width(fm, buf, size); 0 4 4
121 4 ▶ if (cw > avail_w) {
Branch: 1 branch
BranchTrueFalseTotal
if (cw > avail_w) { 3 1 4
122 3 lines++;
123 3 memcpy(buf, ws, wlen);
124 3 buf[wlen] = '\0'; llen = wlen;
125 1 } else { llen += 1 + wlen; }
126 } else {
127 3 memcpy(buf, ws, wlen);
128 3 buf[wlen] = '\0'; llen = wlen; lines = 1;
129 }
130 11 ▶ while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++;
Branch: 4 branches
BranchTrueFalseTotal
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 4 7 11
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 7 7
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 7 7
while (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r') p++; 0 7 7
131 }
132 3 free(buf);
133 3 *w = avail_w;
134 3 ▶ *h = lines > 0 ? (float)lines * line_h : 0.0f;
Branch: 1 branch
BranchTrueFalseTotal
*h = lines > 0 ? (float)lines * line_h : 0.0f; 3 0 3
135 3 }
136  
137 MODULE = Layout::Flex PACKAGE = Layout::Flex
138  
139 PROTOTYPES: DISABLE
140  
141 BOOT:
142 13 fm_std14_init();
143  
144 void
145 compute(class, ...)
146 SV * class
147 PPCODE:
148 {
149 lf_ctx_t ctx;
150 95 lf_item_t *lf_items = NULL;
151 95 lf_rect_t *rects = NULL;
152 95 AV *items_av = NULL;
153 95 SV *measure_sv = NULL;
154 95 SV *fm_measure_sv = NULL;
155 95 fm_font_t *fm_ptr = NULL;
156 95 float fm_size = 12.0f, fm_line_h = 14.4f;
157 int i, n;
158  
159 PERL_UNUSED_VAR(class);
160  
161 /* ── defaults ── */
162 95 ctx.main_size = 0.0f;
163 95 ctx.cross_size = 0.0f;
164 95 ctx.direction = LF_DIR_ROW;
165 95 ctx.justify = LF_JUST_START;
166 95 ctx.align_items = LF_ALIGN_STRETCH;
167 95 ctx.wrap = LF_WRAP_NOWRAP;
168 95 ctx.align_content = LF_ACONT_STRETCH;
169 95 ctx.main_gap = 0.0f;
170 95 ctx.cross_gap = 0.0f;
171 95 ctx.items = NULL;
172 95 ctx.n_items = 0;
173  
174 /* ── parse named args ── */
175 499 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 404 95 499
176 404 const char *key = SvPV_nolen(ST(i));
177 404 SV *val = ST(i + 1);
178  
179 404 ▶ if (strEQ(key, "main_size")) {
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(key, "main_size")) { 95 309 404
180 95 ctx.main_size = (float)SvNV(val);
181 309 ▶ } else if (strEQ(key, "cross_size")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "cross_size")) { 95 214 309
182 95 ctx.cross_size = (float)SvNV(val);
183 214 ▶ } else if (strEQ(key, "direction")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "direction")) { 6 208 214
184 6 const char *d = SvPV_nolen(val);
185 6 ctx.direction = strEQ(d, "column") ? LF_DIR_COLUMN : LF_DIR_ROW;
186 208 ▶ } else if (strEQ(key, "justify")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "justify")) { 7 201 208
187 7 const char *j = SvPV_nolen(val);
188 7 ▶ if (strEQ(j, "end")) ctx.justify = LF_JUST_END;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(j, "end")) ctx.justify = LF_JUST_END; 1 6 7
189 6 ▶ else if (strEQ(j, "center")) ctx.justify = LF_JUST_CENTER;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(j, "center")) ctx.justify = LF_JUST_CENTER; 1 5 6
190 5 ▶ else if (strEQ(j, "space-between")) ctx.justify = LF_JUST_SPACE_BETWEEN;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(j, "space-between")) ctx.justify = LF_JUST_SPACE_BETWEEN; 3 2 5
191 2 ▶ else if (strEQ(j, "space-around")) ctx.justify = LF_JUST_SPACE_AROUND;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(j, "space-around")) ctx.justify = LF_JUST_SPACE_AROUND; 1 1 2
192 1 ▶ else if (strEQ(j, "space-evenly")) ctx.justify = LF_JUST_SPACE_EVENLY;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(j, "space-evenly")) ctx.justify = LF_JUST_SPACE_EVENLY; 1 0 1
193 0 else ctx.justify = LF_JUST_START;
194 201 ▶ } else if (strEQ(key, "align")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "align")) { 42 159 201
195 42 int a = lf_parse_align(SvPV_nolen(val));
196 42 ctx.align_items = (a >= 0) ? a : LF_ALIGN_STRETCH;
197 159 ▶ } else if (strEQ(key, "wrap")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "wrap")) { 15 144 159
198 15 const char *w = SvPV_nolen(val);
199 15 ▶ if (strEQ(w, "wrap")) ctx.wrap = LF_WRAP_WRAP;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(w, "wrap")) ctx.wrap = LF_WRAP_WRAP; 14 1 15
200 1 ▶ else if (strEQ(w, "wrap-reverse")) ctx.wrap = LF_WRAP_REVERSE;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(w, "wrap-reverse")) ctx.wrap = LF_WRAP_REVERSE; 1 0 1
201 0 else ctx.wrap = LF_WRAP_NOWRAP;
202 144 ▶ } else if (strEQ(key, "align_content")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "align_content")) { 15 129 144
203 15 const char *ac = SvPV_nolen(val);
204 15 ▶ if (strEQ(ac, "start")) ctx.align_content = LF_ACONT_START;
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(ac, "start")) ctx.align_content = LF_ACONT_START; 8 7 15
205 7 ▶ else if (strEQ(ac, "end")) ctx.align_content = LF_ACONT_END;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(ac, "end")) ctx.align_content = LF_ACONT_END; 2 5 7
206 5 ▶ else if (strEQ(ac, "center")) ctx.align_content = LF_ACONT_CENTER;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(ac, "center")) ctx.align_content = LF_ACONT_CENTER; 1 4 5
207 4 ▶ else if (strEQ(ac, "space-between")) ctx.align_content = LF_ACONT_SPACE_BETWEEN;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(ac, "space-between")) ctx.align_content = LF_ACONT_SPACE_BETWEEN; 1 3 4
208 3 ▶ else if (strEQ(ac, "space-around")) ctx.align_content = LF_ACONT_SPACE_AROUND;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(ac, "space-around")) ctx.align_content = LF_ACONT_SPACE_AROUND; 1 2 3
209 2 ▶ else if (strEQ(ac, "space-evenly")) ctx.align_content = LF_ACONT_SPACE_EVENLY;
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(ac, "space-evenly")) ctx.align_content = LF_ACONT_SPACE_EVENLY; 1 1 2
210 1 else ctx.align_content = LF_ACONT_STRETCH;
211 129 ▶ } else if (strEQ(key, "measure")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "measure")) { 27 102 129
212 27 ▶ if (SvROK(val) && sv_isa(val, "Layout::Flex::FMeasure"))
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && sv_isa(val, "Layout::Flex::FMeasure")) 15 12 27
if (SvROK(val) && sv_isa(val, "Layout::Flex::FMeasure")) 9 6 15
213 9 fm_measure_sv = val;
214 18 ▶ else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV)
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) 6 12 18
else if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVCV) 6 0 6
215 6 measure_sv = val;
216 12 ▶ else if (SvOK(val))
Branch: 1 branch
BranchTrueFalseTotal
else if (SvOK(val)) 12 0 12
217 12 measure_sv = val; /* treat any non-ref as 'simple' */
218 102 ▶ } else if (strEQ(key, "gap")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "gap")) { 2 100 102
219 2 ctx.main_gap = ctx.cross_gap = (float)SvNV(val);
220 100 ▶ } else if (strEQ(key, "main_gap")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "main_gap")) { 4 96 100
221 4 ctx.main_gap = (float)SvNV(val);
222 96 ▶ } else if (strEQ(key, "cross_gap")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "cross_gap")) { 1 95 96
223 1 ctx.cross_gap = (float)SvNV(val);
224 95 ▶ } else if (strEQ(key, "items")) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (strEQ(key, "items")) { 95 0 95
225 95 ▶ if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) 95 0 95
if (SvROK(val) && SvTYPE(SvRV(val)) == SVt_PVAV) 95 0 95
226 95 items_av = (AV *)SvRV(val);
227 }
228 }
229  
230 95 ▶ if (fm_measure_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (fm_measure_sv) { 9 86 95
231 9 HV *fmhv = (HV *)SvRV(fm_measure_sv);
232 SV **svp;
233 9 ▶ if ((svp = hv_fetch(fmhv, "fmp", 3, 0)) && SvOK(*svp))
Branch: 2 branches
BranchTrueFalseTotal
if ((svp = hv_fetch(fmhv, "fmp", 3, 0)) && SvOK(*svp)) 9 0 9
if ((svp = hv_fetch(fmhv, "fmp", 3, 0)) && SvOK(*svp)) 9 0 9
234 9 fm_ptr = INT2PTR(fm_font_t *, SvUV(*svp));
235 9 ▶ if ((svp = hv_fetch(fmhv, "size", 4, 0)) && SvOK(*svp)) fm_size = (float)SvNV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if ((svp = hv_fetch(fmhv, "size", 4, 0)) && SvOK(*svp)) fm_size = (float)SvNV(*svp); 9 0 9
if ((svp = hv_fetch(fmhv, "size", 4, 0)) && SvOK(*svp)) fm_size = (float)SvNV(*svp); 9 0 9
236 9 ▶ if ((svp = hv_fetch(fmhv, "line_h", 6, 0)) && SvOK(*svp)) fm_line_h = (float)SvNV(*svp);
Branch: 2 branches
BranchTrueFalseTotal
if ((svp = hv_fetch(fmhv, "line_h", 6, 0)) && SvOK(*svp)) fm_line_h = (float)SvNV(*svp); 9 0 9
if ((svp = hv_fetch(fmhv, "line_h", 6, 0)) && SvOK(*svp)) fm_line_h = (float)SvNV(*svp); 9 0 9
237 }
238  
239 96 ▶ if (!items_av) XSRETURN(0);
Branch: 1 branch
BranchTrueFalseTotal
if (!items_av) XSRETURN(0); 0 95 95
240  
241 95 n = (int)(av_len(items_av) + 1);
242 95 ▶ if (n <= 0) XSRETURN(0);
Branch: 1 branch
BranchTrueFalseTotal
if (n <= 0) XSRETURN(0); 1 94 95
243  
244 94 lf_items = (lf_item_t *)calloc((size_t)n, sizeof(lf_item_t));
245 94 rects = (lf_rect_t *)calloc((size_t)n, sizeof(lf_rect_t));
246  
247 /* ── populate items from hashrefs ── */
248 276 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 182 94 276
249 182 SV **elem = av_fetch(items_av, (SSize_t)i, 0);
250 HV *hv;
251  
252 /* defaults */
253 182 lf_items[i].basis = 0.0f;
254 182 lf_items[i].grow = 0.0f;
255 182 lf_items[i].shrink = 1.0f;
256 182 lf_items[i].min_main = 0.0f;
257 182 lf_items[i].max_main = 0.0f;
258 182 lf_items[i].min_cross = 0.0f;
259 182 lf_items[i].max_cross = 0.0f;
260 182 lf_items[i].cross = 0.0f;
261 182 lf_items[i].align_self = -1;
262 182 lf_items[i].margin_main_start = 0.0f;
263 182 lf_items[i].margin_main_end = 0.0f;
264 182 lf_items[i].margin_cross_start = 0.0f;
265 182 lf_items[i].margin_cross_end = 0.0f;
266  
267 182 ▶ if (!elem || !SvOK(*elem) || !SvROK(*elem)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem || !SvOK(*elem) || !SvROK(*elem) 182 0 182
if (!elem || !SvOK(*elem) || !SvROK(*elem) 182 0 182
if (!elem || !SvOK(*elem) || !SvROK(*elem) 182 0 182
268 182 ▶ || SvTYPE(SvRV(*elem)) != SVt_PVHV)
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*elem)) != SVt_PVHV) 0 182 182
269 0 continue;
270  
271 182 hv = (HV *)SvRV(*elem);
272  
273 182 lf_hv_float(aTHX_ hv, "basis", 5, &lf_items[i].basis);
274 182 lf_hv_float(aTHX_ hv, "grow", 4, &lf_items[i].grow);
275 182 lf_hv_float(aTHX_ hv, "shrink", 6, &lf_items[i].shrink);
276 182 lf_hv_float(aTHX_ hv, "min_main", 8, &lf_items[i].min_main);
277 182 lf_hv_float(aTHX_ hv, "max_main", 8, &lf_items[i].max_main);
278 182 lf_hv_float(aTHX_ hv, "min_cross", 9, &lf_items[i].min_cross);
279 182 lf_hv_float(aTHX_ hv, "max_cross", 9, &lf_items[i].max_cross);
280 182 lf_hv_float(aTHX_ hv, "cross", 5, &lf_items[i].cross);
281 {
282 182 SV **sv = hv_fetch(hv, "align_self", 10, 0);
283 182 ▶ if (sv && SvOK(*sv))
Branch: 2 branches
BranchTrueFalseTotal
if (sv && SvOK(*sv)) 4 178 182
if (sv && SvOK(*sv)) 4 0 4
284 8 lf_items[i].align_self = lf_parse_align(SvPV_nolen(*sv));
285 }
286 /* margins: read as top/right/bottom/left (CSS convention),
287 then map to main/cross axes after all items are read */
288 {
289 182 float mt = 0.0f, mr = 0.0f, mb = 0.0f, ml = 0.0f, mall = 0.0f;
290 182 lf_hv_float(aTHX_ hv, "margin", 6, &mall);
291 182 mt = mr = mb = ml = mall;
292 182 lf_hv_float(aTHX_ hv, "margin_top", 10, &mt);
293 182 lf_hv_float(aTHX_ hv, "margin_right", 12, &mr);
294 182 lf_hv_float(aTHX_ hv, "margin_bottom", 13, &mb);
295 182 lf_hv_float(aTHX_ hv, "margin_left", 11, &ml);
296 /* direction is already parsed; map T/R/B/L to main/cross */
297 182 ▶ if (ctx.direction == LF_DIR_ROW) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.direction == LF_DIR_ROW) { 170 12 182
298 170 lf_items[i].margin_main_start = ml;
299 170 lf_items[i].margin_main_end = mr;
300 170 lf_items[i].margin_cross_start = mt;
301 170 lf_items[i].margin_cross_end = mb;
302 } else {
303 12 lf_items[i].margin_main_start = mt;
304 12 lf_items[i].margin_main_end = mb;
305 12 lf_items[i].margin_cross_start = ml;
306 12 lf_items[i].margin_cross_end = mr;
307 }
308 }
309 /* content-driven sizing: if item has 'text' and measure is set,
310 derive basis/cross unless already explicitly provided */
311 182 ▶ if (measure_sv || fm_measure_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (measure_sv || fm_measure_sv) { 156 26 182
if (measure_sv || fm_measure_sv) { 10 146 156
312 36 SV **text_sv = hv_fetch(hv, "text", 4, 0);
313 36 ▶ if (text_sv && SvOK(*text_sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (text_sv && SvOK(*text_sv)) { 33 3 36
if (text_sv && SvOK(*text_sv)) { 33 0 33
314 33 int no_basis = !hv_exists(hv, "basis", 5);
315 33 int no_cross = !hv_exists(hv, "cross", 5);
316 33 ▶ if (no_basis || no_cross) {
Branch: 2 branches
BranchTrueFalseTotal
if (no_basis || no_cross) { 5 28 33
if (no_basis || no_cross) { 5 0 5
317 33 float tw = 0.0f, th = 0.0f;
318 33 ▶ if (fm_measure_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (fm_measure_sv) { 10 23 33
319 10 lf_measure_fm(fm_ptr, SvPV_nolen(*text_sv),
320 10 SvUTF8(*text_sv),
321 fm_size, fm_line_h, &tw, &th);
322 23 ▶ } else if (SvROK(measure_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(measure_sv)) { 8 15 23
323 8 lf_measure_cb(aTHX_ measure_sv, hv, &tw, &th);
324 } else {
325 15 float font_size = 12.0f;
326 15 lf_hv_float(aTHX_ hv, "font_size", 9, &font_size);
327 30 lf_measure_simple(SvPV_nolen(*text_sv),
328 font_size, &tw, &th);
329 }
330 33 ▶ if (ctx.direction == LF_DIR_ROW) {
Branch: 1 branch
BranchTrueFalseTotal
if (ctx.direction == LF_DIR_ROW) { 31 2 33
331 31 ▶ if (no_basis) lf_items[i].basis = tw;
Branch: 1 branch
BranchTrueFalseTotal
if (no_basis) lf_items[i].basis = tw; 26 5 31
332 31 ▶ if (no_cross) lf_items[i].cross = th;
Branch: 1 branch
BranchTrueFalseTotal
if (no_cross) lf_items[i].cross = th; 30 1 31
333 } else {
334 2 ▶ if (no_basis) lf_items[i].basis = th;
Branch: 1 branch
BranchTrueFalseTotal
if (no_basis) lf_items[i].basis = th; 2 0 2
335 2 ▶ if (no_cross) lf_items[i].cross = tw;
Branch: 1 branch
BranchTrueFalseTotal
if (no_cross) lf_items[i].cross = tw; 2 0 2
336 }
337 }
338 }
339 }
340 }
341  
342 94 ctx.items = lf_items;
343 94 ctx.n_items = n;
344  
345 94 lf_compute(&ctx, rects);
346  
347 /* ── second pass: wrap text items ───────────────────────── */
348 94 ▶ if (measure_sv || fm_measure_sv) {
Branch: 2 branches
BranchTrueFalseTotal
if (measure_sv || fm_measure_sv) { 76 18 94
if (measure_sv || fm_measure_sv) { 9 67 76
349 27 int needs_recompute = 0;
350 63 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 36 27 63
351 36 SV **elem2 = av_fetch(items_av, (SSize_t)i, 0);
352 HV *hv2;
353 SV **wt_sv, **text_sv2;
354 float avail, tw, th, new_cross;
355  
356 36 ▶ if (!elem2 || !SvOK(*elem2) || !SvROK(*elem2)
Branch: 3 branches
BranchTrueFalseTotal
if (!elem2 || !SvOK(*elem2) || !SvROK(*elem2) 36 0 36
if (!elem2 || !SvOK(*elem2) || !SvROK(*elem2) 36 0 36
if (!elem2 || !SvOK(*elem2) || !SvROK(*elem2) 36 0 36
357 61 ▶ || SvTYPE(SvRV(*elem2)) != SVt_PVHV) continue;
Branch: 1 branch
BranchTrueFalseTotal
|| SvTYPE(SvRV(*elem2)) != SVt_PVHV) continue; 0 36 36
358 36 hv2 = (HV *)SvRV(*elem2);
359  
360 36 wt_sv = hv_fetch(hv2, "wrap_text", 9, 0);
361 36 ▶ if (!wt_sv || !SvOK(*wt_sv) || !SvTRUE(*wt_sv)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!wt_sv || !SvOK(*wt_sv) || !SvTRUE(*wt_sv)) continue; 12 24 36
if (!wt_sv || !SvOK(*wt_sv) || !SvTRUE(*wt_sv)) continue; 12 0 12
if (!wt_sv || !SvOK(*wt_sv) || !SvTRUE(*wt_sv)) continue; 1 11 12
362  
363 11 text_sv2 = hv_fetch(hv2, "text", 4, 0);
364 11 ▶ if (!text_sv2 || !SvOK(*text_sv2)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!text_sv2 || !SvOK(*text_sv2)) continue; 11 0 11
if (!text_sv2 || !SvOK(*text_sv2)) continue; 0 11 11
365  
366 11 ▶ avail = (ctx.direction == LF_DIR_ROW) ? rects[i].w : rects[i].h;
Branch: 1 branch
BranchTrueFalseTotal
avail = (ctx.direction == LF_DIR_ROW) ? rects[i].w : rects[i].h; 11 0 11
367 11 tw = 0.0f; th = 0.0f;
368  
369 11 ▶ if (fm_measure_sv) {
Branch: 1 branch
BranchTrueFalseTotal
if (fm_measure_sv) { 3 8 11
370 3 lf_measure_fm_wrapped(fm_ptr, SvPV_nolen(*text_sv2),
371 3 SvUTF8(*text_sv2),
372 fm_size, fm_line_h, avail, &tw, &th);
373 8 ▶ } else if (SvROK(measure_sv)) {
Branch: 1 branch
BranchTrueFalseTotal
} else if (SvROK(measure_sv)) { 3 5 8
374 3 lf_measure_cb_wrapped(aTHX_ measure_sv, hv2, avail, &tw, &th);
375 } else {
376 5 float font_size = 12.0f;
377 5 lf_hv_float(aTHX_ hv2, "font_size", 9, &font_size);
378 10 lf_measure_simple_wrapped(SvPV_nolen(*text_sv2),
379 font_size, avail, &tw, &th);
380 }
381  
382 11 ▶ new_cross = (ctx.direction == LF_DIR_ROW) ? th : tw;
Branch: 1 branch
BranchTrueFalseTotal
new_cross = (ctx.direction == LF_DIR_ROW) ? th : tw; 11 0 11
383 11 ▶ if (new_cross != lf_items[i].cross) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_cross != lf_items[i].cross) { 7 4 11
384 7 lf_items[i].cross = new_cross;
385 7 needs_recompute = 1;
386 }
387 }
388 27 ▶ if (needs_recompute) lf_compute(&ctx, rects);
Branch: 1 branch
BranchTrueFalseTotal
if (needs_recompute) lf_compute(&ctx, rects); 7 20 27
389 }
390  
391 94 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 94 0 94
EXTEND(SP, n); 0 94 94
392 276 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 182 94 276
393 182 AV *rect = newAV();
394 182 av_push(rect, newSVnv((NV)rects[i].x));
395 182 av_push(rect, newSVnv((NV)rects[i].y));
396 182 av_push(rect, newSVnv((NV)rects[i].w));
397 182 av_push(rect, newSVnv((NV)rects[i].h));
398 182 PUSHs(sv_2mortal(newRV_noinc((SV *)rect)));
399 }
400  
401 94 free(lf_items);
402 94 free(rects);
403 }
404  
405 void
406 font_metrics_measure(class, ...)
407 SV *class
408 PPCODE:
409 {
410 11 SV *fm_sv = NULL;
411 11 NV size = 12.0;
412 11 NV line_h = 0.0;
413 HV *hv;
414 SV *obj;
415 int i;
416  
417 PERL_UNUSED_VAR(class);
418  
419 33 ▶ for (i = 1; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i + 1 < items; i += 2) { 22 11 33
420 22 const char *k = SvPV_nolen(ST(i));
421 22 SV *v = ST(i + 1);
422 22 ▶ if (strEQ(k, "fm")) { fm_sv = v; }
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(k, "fm")) { fm_sv = v; } 10 12 22
423 12 ▶ else if (strEQ(k, "size")) { size = SvNV(v); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "size")) { size = SvNV(v); } 11 1 12
424 1 ▶ else if (strEQ(k, "line_height")) { line_h = SvNV(v); }
Branch: 1 branch
BranchTrueFalseTotal
else if (strEQ(k, "line_height")) { line_h = SvNV(v); } 1 0 1
425 }
426  
427 11 ▶ if (!fm_sv || !SvOK(fm_sv) || !SvROK(fm_sv))
Branch: 3 branches
BranchTrueFalseTotal
if (!fm_sv || !SvOK(fm_sv) || !SvROK(fm_sv)) 10 1 11
if (!fm_sv || !SvOK(fm_sv) || !SvROK(fm_sv)) 10 0 10
if (!fm_sv || !SvOK(fm_sv) || !SvROK(fm_sv)) 0 10 10
428 1 croak("font_metrics_measure: fm must be a Font::Metrics object");
429 10 ▶ if (line_h <= 0.0) line_h = size * 1.2;
Branch: 1 branch
BranchTrueFalseTotal
if (line_h <= 0.0) line_h = size * 1.2; 9 1 10
430  
431 10 hv = newHV();
432 10 hv_store(hv, "fm", 2, SvREFCNT_inc(fm_sv), 0);
433 10 hv_store(hv, "fmp", 3, newSVuv(PTR2UV(INT2PTR(fm_font_t *,
434 SvIV(SvRV(fm_sv))))), 0);
435 10 hv_store(hv, "size", 4, newSVnv(size), 0);
436 10 hv_store(hv, "line_h", 6, newSVnv(line_h), 0);
437  
438 10 obj = sv_bless(newRV_noinc((SV *)hv),
439 gv_stashpv("Layout::Flex::FMeasure", GV_ADD));
440 10 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 10 10
441 10 PUSHs(sv_2mortal(obj));
442 10 XSRETURN(1);
443 }
↑ Summary include/flex.h »