Flex.xs

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