| 1 |
|
| /* |
| 2 |
|
| * flex.h — CSS Flexible Box layout engine (single + multi-line) |
| 3 |
|
| * |
| 4 |
|
| * Supports flex-wrap, align-content, gap (main_gap/cross_gap), |
| 5 |
|
| * and per-item margins (margin_main_start/end, margin_cross_start/end). |
| 6 |
|
| * Zero dependencies beyond <stdlib.h> and <float.h>. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #ifndef LAYOUT_FLEX_H |
| 10 |
|
| #define LAYOUT_FLEX_H |
| 11 |
|
| |
| 12 |
|
| #include <stdlib.h> |
| 13 |
|
| #include <float.h> |
| 14 |
|
| |
| 15 |
|
| /* ── direction ──────────────────────────────────────────────────── */ |
| 16 |
|
| #define LF_DIR_ROW 0 |
| 17 |
|
| #define LF_DIR_COLUMN 1 |
| 18 |
|
| |
| 19 |
|
| /* ── justify-content ────────────────────────────────────────────── */ |
| 20 |
|
| #define LF_JUST_START 0 |
| 21 |
|
| #define LF_JUST_END 1 |
| 22 |
|
| #define LF_JUST_CENTER 2 |
| 23 |
|
| #define LF_JUST_SPACE_BETWEEN 3 |
| 24 |
|
| #define LF_JUST_SPACE_AROUND 4 |
| 25 |
|
| #define LF_JUST_SPACE_EVENLY 5 |
| 26 |
|
| |
| 27 |
|
| /* ── align-items / align-self ───────────────────────────────────── */ |
| 28 |
|
| #define LF_ALIGN_STRETCH 0 |
| 29 |
|
| #define LF_ALIGN_START 1 |
| 30 |
|
| #define LF_ALIGN_END 2 |
| 31 |
|
| #define LF_ALIGN_CENTER 3 |
| 32 |
|
| |
| 33 |
|
| /* ── flex-wrap ──────────────────────────────────────────────────── */ |
| 34 |
|
| #define LF_WRAP_NOWRAP 0 |
| 35 |
|
| #define LF_WRAP_WRAP 1 |
| 36 |
|
| #define LF_WRAP_REVERSE 2 |
| 37 |
|
| |
| 38 |
|
| /* ── align-content ──────────────────────────────────────────────── */ |
| 39 |
|
| #define LF_ACONT_STRETCH 0 |
| 40 |
|
| #define LF_ACONT_START 1 |
| 41 |
|
| #define LF_ACONT_END 2 |
| 42 |
|
| #define LF_ACONT_CENTER 3 |
| 43 |
|
| #define LF_ACONT_SPACE_BETWEEN 4 |
| 44 |
|
| #define LF_ACONT_SPACE_AROUND 5 |
| 45 |
|
| #define LF_ACONT_SPACE_EVENLY 6 |
| 46 |
|
| |
| 47 |
|
| /* ── item descriptor ─────────────────────────────────────────────── */ |
| 48 |
|
| |
| 49 |
|
| typedef struct { |
| 50 |
|
| float basis; |
| 51 |
|
| float grow; |
| 52 |
|
| float shrink; |
| 53 |
|
| float min_main; |
| 54 |
|
| float max_main; /* 0 = unconstrained */ |
| 55 |
|
| float min_cross; |
| 56 |
|
| float max_cross; /* 0 = unconstrained */ |
| 57 |
|
| float cross; /* natural cross size (non-stretch align) */ |
| 58 |
|
| int align_self; /* LF_ALIGN_* or -1 to inherit */ |
| 59 |
|
| float margin_main_start; /* margin before item on main axis */ |
| 60 |
|
| float margin_main_end; /* margin after item on main axis */ |
| 61 |
|
| float margin_cross_start; /* margin before item on cross axis */ |
| 62 |
|
| float margin_cross_end; /* margin after item on cross axis */ |
| 63 |
|
| } lf_item_t; |
| 64 |
|
| |
| 65 |
|
| /* ── container context ───────────────────────────────────────────── */ |
| 66 |
|
| |
| 67 |
|
| typedef struct { |
| 68 |
|
| float main_size; |
| 69 |
|
| float cross_size; |
| 70 |
|
| int direction; /* LF_DIR_* */ |
| 71 |
|
| int justify; /* LF_JUST_* */ |
| 72 |
|
| int align_items; /* LF_ALIGN_* */ |
| 73 |
|
| int wrap; /* LF_WRAP_* */ |
| 74 |
|
| int align_content; /* LF_ACONT_* */ |
| 75 |
|
| float main_gap; /* fixed gap between items on main axis */ |
| 76 |
|
| float cross_gap; /* fixed gap between lines on cross axis */ |
| 77 |
|
| lf_item_t *items; |
| 78 |
|
| int n_items; |
| 79 |
|
| } lf_ctx_t; |
| 80 |
|
| |
| 81 |
|
| /* ── output rectangle ────────────────────────────────────────────── */ |
| 82 |
|
| |
| 83 |
|
| typedef struct { float x, y, w, h; } lf_rect_t; |
| 84 |
|
| |
| 85 |
|
| /* ── internal: per-line descriptor ──────────────────────────────── */ |
| 86 |
|
| |
| 87 |
|
| typedef struct { |
| 88 |
|
| int start; |
| 89 |
|
| int count; |
| 90 |
|
| float cross_size; |
| 91 |
|
| float cross_pos; |
| 92 |
|
| } lf__line_t; |
| 93 |
|
| |
| 94 |
|
| /* ── internal helpers ─────────────────────────────────────────────── */ |
| 95 |
|
| |
| 96 |
209 |
▶ | static float lf__fmax(float a, float b) { return a > b ? a : b; } |
Branch: 1 branch
| Branch | True | False | Total |
| static float lf__fmax(float a, float b) { return a > b ? a : b; } |
124 |
85 |
209 |
|
| 97 |
82 |
▶ | static float lf__fmin(float a, float b) { return a < b ? a : b; } |
Branch: 1 branch
| Branch | True | False | Total |
| static float lf__fmin(float a, float b) { return a < b ? a : b; } |
1 |
81 |
82 |
|
| 98 |
432 |
| static float lf__clamp(float v, float lo, float hi) { |
| 99 |
432 |
▶ | return v < lo ? lo : (v > hi ? hi : v); |
Branch: 2 branches
| Branch | True | False | Total |
| return v < lo ? lo : (v > hi ? hi : v); |
6 |
426 |
432 |
| return v < lo ? lo : (v > hi ? hi : v); |
6 |
420 |
426 |
|
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| /* outer main-axis size of one item (content size + margins) */ |
| 103 |
598 |
| static float lf__outer_main(const lf_item_t *it, float size) { |
| 104 |
598 |
| return size + it->margin_main_start + it->margin_main_end; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /* outer cross-axis size for line-cross contribution */ |
| 108 |
41 |
| static float lf__outer_cross(const lf_item_t *it, float ic) { |
| 109 |
41 |
| return ic + it->margin_cross_start + it->margin_cross_end; |
| 110 |
|
| } |
| 111 |
|
| |
| 112 |
|
| /* ── grow/shrink violation-freezing loop for one line ──────────── */ |
| 113 |
|
| |
| 114 |
116 |
| static void lf__flex_line(const lf_item_t *items, int start, int count, |
| 115 |
|
| float main_size, float main_gap, float *sizes) { |
| 116 |
116 |
| int i, end = start + count; |
| 117 |
|
| float *proposed; |
| 118 |
|
| int *frozen; |
| 119 |
|
| float free_space; |
| 120 |
|
| float gaps; |
| 121 |
|
| |
| 122 |
116 |
▶ | if (count <= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count <= 0) return; |
0 |
116 |
116 |
|
| 123 |
|
| |
| 124 |
116 |
| proposed = (float *)calloc((size_t)count, sizeof(float)); |
| 125 |
116 |
| frozen = (int *)calloc((size_t)count, sizeof(int)); |
| 126 |
|
| |
| 127 |
116 |
▶ | gaps = main_gap * (count > 1 ? count - 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| gaps = main_gap * (count > 1 ? count - 1 : 0); |
63 |
53 |
116 |
|
| 128 |
|
| |
| 129 |
|
| /* free space = main_size - gaps - sum(outer sizes) */ |
| 130 |
116 |
| free_space = main_size - gaps; |
| 131 |
307 |
▶ | for (i = start; i < end; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) |
191 |
116 |
307 |
|
| 132 |
191 |
| free_space -= lf__outer_main(&items[i], sizes[i]); |
| 133 |
|
| |
| 134 |
116 |
▶ | if (free_space > 0.0f) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (free_space > 0.0f) { |
85 |
31 |
116 |
|
| 135 |
2 |
| for (;;) { |
| 136 |
87 |
| float remaining, tg = 0.0f; |
| 137 |
87 |
| int viol = 0, j; |
| 138 |
87 |
| remaining = main_size - gaps; |
| 139 |
228 |
▶ | for (i = start; i < end; i++) remaining -= lf__outer_main(&items[i], sizes[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) remaining -= lf__outer_main(&items[i], sizes[i]); |
141 |
87 |
228 |
|
| 140 |
228 |
▶ | for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) tg += items[i].grow; } |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) tg += items[i].grow; } |
139 |
2 |
141 |
| for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) tg += items[i].grow; } |
141 |
87 |
228 |
|
| 141 |
87 |
▶ | if (tg <= 0.0f) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tg <= 0.0f) break; |
73 |
14 |
87 |
|
| 142 |
40 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
26 |
14 |
40 |
|
| 143 |
26 |
| j = i - start; |
| 144 |
26 |
▶ | if (frozen[j]) { proposed[j] = sizes[i]; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frozen[j]) { proposed[j] = sizes[i]; continue; } |
2 |
24 |
26 |
|
| 145 |
24 |
| proposed[j] = sizes[i] + remaining * (items[i].grow / tg); |
| 146 |
|
| } |
| 147 |
40 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
26 |
14 |
40 |
|
| 148 |
|
| float mn, mx, c; |
| 149 |
26 |
| j = i - start; |
| 150 |
26 |
▶ | if (frozen[j]) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frozen[j]) continue; |
2 |
24 |
26 |
|
| 151 |
24 |
| mn = items[i].min_main; |
| 152 |
24 |
▶ | mx = items[i].max_main > 0.0f ? items[i].max_main : FLT_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| mx = items[i].max_main > 0.0f ? items[i].max_main : FLT_MAX; |
2 |
22 |
24 |
|
| 153 |
24 |
| c = lf__clamp(proposed[j], mn, mx); |
| 154 |
24 |
▶ | if (c != proposed[j]) { sizes[i] = c; frozen[j] = 1; viol = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != proposed[j]) { sizes[i] = c; frozen[j] = 1; viol = 1; } |
2 |
22 |
24 |
|
| 155 |
22 |
| else sizes[i] = proposed[j]; |
| 156 |
|
| } |
| 157 |
14 |
▶ | if (!viol) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!viol) break; |
12 |
2 |
14 |
|
| 158 |
|
| } |
| 159 |
31 |
▶ | } else if (free_space < 0.0f) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (free_space < 0.0f) { |
16 |
15 |
31 |
|
| 160 |
2 |
| for (;;) { |
| 161 |
18 |
| float remaining, ts = 0.0f; |
| 162 |
18 |
| int viol = 0, j; |
| 163 |
18 |
| remaining = main_size - gaps; |
| 164 |
50 |
▶ | for (i = start; i < end; i++) remaining -= lf__outer_main(&items[i], sizes[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) remaining -= lf__outer_main(&items[i], sizes[i]); |
32 |
18 |
50 |
|
| 165 |
50 |
▶ | for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) ts += items[i].shrink * sizes[i]; } |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) ts += items[i].shrink * sizes[i]; } |
30 |
2 |
32 |
| for (i = start; i < end; i++) { j = i-start; if (!frozen[j]) ts += items[i].shrink * sizes[i]; } |
32 |
18 |
50 |
|
| 166 |
18 |
▶ | if (ts <= 0.0f) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ts <= 0.0f) break; |
2 |
16 |
18 |
|
| 167 |
44 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
28 |
16 |
44 |
|
| 168 |
28 |
| j = i - start; |
| 169 |
28 |
▶ | if (frozen[j]) { proposed[j] = sizes[i]; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (frozen[j]) { proposed[j] = sizes[i]; continue; } |
2 |
26 |
28 |
|
| 170 |
26 |
| proposed[j] = sizes[i] + remaining * (items[i].shrink * sizes[i] / ts); |
| 171 |
|
| } |
| 172 |
44 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
28 |
16 |
44 |
|
| 173 |
|
| float mn, mx, c; |
| 174 |
28 |
| j = i - start; |
| 175 |
28 |
▶ | if (frozen[j]) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (frozen[j]) continue; |
2 |
26 |
28 |
|
| 176 |
26 |
| mn = items[i].min_main; |
| 177 |
26 |
▶ | mx = items[i].max_main > 0.0f ? items[i].max_main : FLT_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| mx = items[i].max_main > 0.0f ? items[i].max_main : FLT_MAX; |
0 |
26 |
26 |
|
| 178 |
26 |
| c = lf__clamp(proposed[j], mn, mx); |
| 179 |
26 |
▶ | if (c != proposed[j]) { sizes[i] = c; frozen[j] = 1; viol = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != proposed[j]) { sizes[i] = c; frozen[j] = 1; viol = 1; } |
2 |
24 |
26 |
|
| 180 |
24 |
| else sizes[i] = proposed[j]; |
| 181 |
|
| } |
| 182 |
16 |
▶ | if (!viol) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!viol) break; |
14 |
2 |
16 |
|
| 183 |
|
| } |
| 184 |
|
| } |
| 185 |
|
| |
| 186 |
116 |
| free(proposed); |
| 187 |
116 |
| free(frozen); |
| 188 |
|
| } |
| 189 |
|
| |
| 190 |
|
| /* ── justify spacing for one line ────────────────────────────────── */ |
| 191 |
|
| /* |
| 192 |
|
| * gap_before — offset before the first item's margin |
| 193 |
|
| * gap_between — space between consecutive items' outer edges (includes main_gap) |
| 194 |
|
| */ |
| 195 |
116 |
| static void lf__justify(int justify, float main_size, float main_gap, |
| 196 |
|
| const lf_item_t *items, float *sizes, |
| 197 |
|
| int start, int count, |
| 198 |
|
| float *gap_before, float *gap_between) { |
| 199 |
116 |
| int i, end = start + count; |
| 200 |
116 |
▶ | float ff = main_size - main_gap * (count > 1 ? count - 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| float ff = main_size - main_gap * (count > 1 ? count - 1 : 0); |
63 |
53 |
116 |
|
| 201 |
307 |
▶ | for (i = start; i < end; i++) ff -= lf__outer_main(&items[i], sizes[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) ff -= lf__outer_main(&items[i], sizes[i]); |
191 |
116 |
307 |
|
| 202 |
116 |
| ff = lf__fmax(ff, 0.0f); |
| 203 |
|
| |
| 204 |
116 |
| *gap_before = 0.0f; |
| 205 |
116 |
| *gap_between = main_gap; /* base is the fixed gap */ |
| 206 |
|
| |
| 207 |
116 |
| switch (justify) { |
| 208 |
109 |
| case LF_JUST_START: break; |
| 209 |
1 |
| case LF_JUST_END: *gap_before = ff; break; |
| 210 |
1 |
| case LF_JUST_CENTER: *gap_before = ff / 2.0f; break; |
| 211 |
3 |
| case LF_JUST_SPACE_BETWEEN: |
| 212 |
3 |
▶ | *gap_between += (count > 1) ? ff / (float)(count - 1) : 0.0f; break; |
Branch: 1 branch
| Branch | True | False | Total |
| *gap_between += (count > 1) ? ff / (float)(count - 1) : 0.0f; break; |
2 |
1 |
3 |
|
| 213 |
1 |
| case LF_JUST_SPACE_AROUND: { |
| 214 |
1 |
▶ | float g = (count > 0) ? ff / (float)count : 0.0f; |
Branch: 1 branch
| Branch | True | False | Total |
| float g = (count > 0) ? ff / (float)count : 0.0f; |
1 |
0 |
1 |
|
| 215 |
1 |
| *gap_between += g; *gap_before = g / 2.0f; |
| 216 |
1 |
| } break; |
| 217 |
1 |
| case LF_JUST_SPACE_EVENLY: { |
| 218 |
1 |
▶ | float g = (count > 0) ? ff / (float)(count + 1) : 0.0f; |
Branch: 1 branch
| Branch | True | False | Total |
| float g = (count > 0) ? ff / (float)(count + 1) : 0.0f; |
1 |
0 |
1 |
|
| 219 |
1 |
| *gap_between += g; *gap_before = g; |
| 220 |
1 |
| } break; |
| 221 |
|
| } |
| 222 |
116 |
| } |
| 223 |
|
| |
| 224 |
|
| /* ── main algorithm ──────────────────────────────────────────────── */ |
| 225 |
|
| |
| 226 |
101 |
| static void lf_compute(const lf_ctx_t *ctx, lf_rect_t *out) { |
| 227 |
101 |
| int n = ctx->n_items; |
| 228 |
|
| int i, li; |
| 229 |
|
| float *sizes; |
| 230 |
|
| lf__line_t *lines; |
| 231 |
101 |
| int n_lines = 0; |
| 232 |
|
| |
| 233 |
101 |
▶ | if (n <= 0) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) return; |
0 |
101 |
101 |
|
| 234 |
|
| |
| 235 |
101 |
| sizes = (float *)calloc((size_t)n, sizeof(float)); |
| 236 |
101 |
| lines = (lf__line_t *)calloc((size_t)n, sizeof(lf__line_t)); |
| 237 |
|
| |
| 238 |
|
| /* ── 1. hypothetical main sizes ─────────────────────────────── */ |
| 239 |
292 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
191 |
101 |
292 |
|
| 240 |
191 |
| float mn = ctx->items[i].min_main; |
| 241 |
191 |
▶ | float mx = ctx->items[i].max_main > 0.0f ? ctx->items[i].max_main : FLT_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| float mx = ctx->items[i].max_main > 0.0f ? ctx->items[i].max_main : FLT_MAX; |
3 |
188 |
191 |
|
| 242 |
191 |
| sizes[i] = lf__clamp(ctx->items[i].basis, mn, mx); |
| 243 |
|
| } |
| 244 |
|
| |
| 245 |
|
| /* ── 2. line breaking ────────────────────────────────────────── */ |
| 246 |
101 |
▶ | if (ctx->wrap == LF_WRAP_NOWRAP) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->wrap == LF_WRAP_NOWRAP) { |
86 |
15 |
101 |
|
| 247 |
86 |
| lines[0].start = 0; |
| 248 |
86 |
| lines[0].count = n; |
| 249 |
86 |
| n_lines = 1; |
| 250 |
|
| } else { |
| 251 |
15 |
| int ls = 0; |
| 252 |
15 |
| float lsz = 0.0f; |
| 253 |
58 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
43 |
15 |
58 |
|
| 254 |
43 |
| float outer_i = lf__outer_main(&ctx->items[i], sizes[i]); |
| 255 |
43 |
▶ | float gap_i = (i > ls) ? ctx->main_gap : 0.0f; |
Branch: 1 branch
| Branch | True | False | Total |
| float gap_i = (i > ls) ? ctx->main_gap : 0.0f; |
28 |
15 |
43 |
|
| 256 |
43 |
▶ | if (i > ls && lsz + gap_i + outer_i > ctx->main_size) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (i > ls && lsz + gap_i + outer_i > ctx->main_size) { |
28 |
15 |
43 |
| if (i > ls && lsz + gap_i + outer_i > ctx->main_size) { |
15 |
13 |
28 |
|
| 257 |
15 |
| lines[n_lines].start = ls; |
| 258 |
15 |
| lines[n_lines].count = i - ls; |
| 259 |
15 |
| n_lines++; |
| 260 |
15 |
| ls = i; |
| 261 |
15 |
| lsz = outer_i; |
| 262 |
|
| } else { |
| 263 |
28 |
| lsz += gap_i + outer_i; |
| 264 |
|
| } |
| 265 |
|
| } |
| 266 |
15 |
| lines[n_lines].start = ls; |
| 267 |
15 |
| lines[n_lines].count = n - ls; |
| 268 |
15 |
| n_lines++; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
|
| /* ── 3. grow / shrink per line ───────────────────────────────── */ |
| 272 |
217 |
▶ | for (li = 0; li < n_lines; li++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; li < n_lines; li++) |
116 |
101 |
217 |
|
| 273 |
116 |
| lf__flex_line(ctx->items, lines[li].start, lines[li].count, |
| 274 |
116 |
| ctx->main_size, ctx->main_gap, sizes); |
| 275 |
|
| |
| 276 |
|
| /* ── 4. line cross sizes ─────────────────────────────────────── */ |
| 277 |
101 |
▶ | if (n_lines == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n_lines == 1) { |
87 |
14 |
101 |
|
| 278 |
87 |
| lines[0].cross_size = ctx->cross_size; |
| 279 |
87 |
| lines[0].cross_pos = 0.0f; |
| 280 |
|
| } else { |
| 281 |
14 |
| float total_cs = 0.0f; |
| 282 |
|
| float ff; |
| 283 |
|
| |
| 284 |
43 |
▶ | for (li = 0; li < n_lines; li++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; li < n_lines; li++) { |
29 |
14 |
43 |
|
| 285 |
29 |
| int start = lines[li].start; |
| 286 |
29 |
| int end = start + lines[li].count; |
| 287 |
29 |
| float lcs = 0.0f; |
| 288 |
70 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
41 |
29 |
70 |
|
| 289 |
82 |
| int al = (ctx->items[i].align_self >= 0) |
| 290 |
41 |
▶ | ? ctx->items[i].align_self : ctx->align_items; |
Branch: 1 branch
| Branch | True | False | Total |
| ? ctx->items[i].align_self : ctx->align_items; |
0 |
41 |
41 |
|
| 291 |
41 |
| float ic = (al == LF_ALIGN_STRETCH) |
| 292 |
3 |
| ? ctx->items[i].min_cross |
| 293 |
41 |
▶ | : lf__fmax(ctx->items[i].cross, ctx->items[i].min_cross); |
Branch: 1 branch
| Branch | True | False | Total |
| : lf__fmax(ctx->items[i].cross, ctx->items[i].min_cross); |
3 |
38 |
41 |
|
| 294 |
41 |
| lcs = lf__fmax(lcs, lf__outer_cross(&ctx->items[i], ic)); |
| 295 |
|
| } |
| 296 |
29 |
| lines[li].cross_size = lcs; |
| 297 |
29 |
| total_cs += lcs; |
| 298 |
|
| } |
| 299 |
|
| |
| 300 |
28 |
| ff = ctx->cross_size |
| 301 |
14 |
| - total_cs |
| 302 |
14 |
▶ | - ctx->cross_gap * (n_lines > 1 ? n_lines - 1 : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| - ctx->cross_gap * (n_lines > 1 ? n_lines - 1 : 0); |
14 |
0 |
14 |
|
| 303 |
|
| |
| 304 |
|
| /* stretch distributes remaining cross space equally among lines */ |
| 305 |
14 |
▶ | if (ctx->align_content == LF_ACONT_STRETCH && ff > 0.0f) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ctx->align_content == LF_ACONT_STRETCH && ff > 0.0f) { |
1 |
13 |
14 |
| if (ctx->align_content == LF_ACONT_STRETCH && ff > 0.0f) { |
1 |
0 |
1 |
|
| 306 |
1 |
| float add = ff / (float)n_lines; |
| 307 |
3 |
▶ | for (li = 0; li < n_lines; li++) lines[li].cross_size += add; |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; li < n_lines; li++) lines[li].cross_size += add; |
2 |
1 |
3 |
|
| 308 |
1 |
| ff = 0.0f; |
| 309 |
|
| } |
| 310 |
|
| |
| 311 |
|
| /* ── 5. align-content → line cross positions ─────────────── */ |
| 312 |
|
| { |
| 313 |
14 |
| float gb = 0.0f; |
| 314 |
14 |
| float gbw = ctx->cross_gap; |
| 315 |
|
| float pos; |
| 316 |
14 |
| float pff = lf__fmax(ff, 0.0f); |
| 317 |
|
| |
| 318 |
14 |
| switch (ctx->align_content) { |
| 319 |
9 |
| case LF_ACONT_STRETCH: |
| 320 |
9 |
| case LF_ACONT_START: break; |
| 321 |
1 |
| case LF_ACONT_END: gb = pff; break; |
| 322 |
1 |
| case LF_ACONT_CENTER: gb = pff / 2.0f; break; |
| 323 |
1 |
| case LF_ACONT_SPACE_BETWEEN: |
| 324 |
1 |
▶ | gbw += (n_lines > 1) ? pff / (float)(n_lines - 1) : 0.0f; break; |
Branch: 1 branch
| Branch | True | False | Total |
| gbw += (n_lines > 1) ? pff / (float)(n_lines - 1) : 0.0f; break; |
1 |
0 |
1 |
|
| 325 |
1 |
| case LF_ACONT_SPACE_AROUND: { |
| 326 |
1 |
▶ | float g = (n_lines > 0) ? pff / (float)n_lines : 0.0f; |
Branch: 1 branch
| Branch | True | False | Total |
| float g = (n_lines > 0) ? pff / (float)n_lines : 0.0f; |
1 |
0 |
1 |
|
| 327 |
1 |
| gbw += g; gb = g / 2.0f; |
| 328 |
1 |
| } break; |
| 329 |
1 |
| case LF_ACONT_SPACE_EVENLY: { |
| 330 |
1 |
▶ | float g = (n_lines > 0) ? pff / (float)(n_lines + 1) : 0.0f; |
Branch: 1 branch
| Branch | True | False | Total |
| float g = (n_lines > 0) ? pff / (float)(n_lines + 1) : 0.0f; |
1 |
0 |
1 |
|
| 331 |
1 |
| gbw += g; gb = g; |
| 332 |
1 |
| } break; |
| 333 |
|
| } |
| 334 |
|
| |
| 335 |
14 |
▶ | if (ctx->wrap == LF_WRAP_REVERSE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->wrap == LF_WRAP_REVERSE) { |
1 |
13 |
14 |
|
| 336 |
1 |
| pos = gb; |
| 337 |
3 |
▶ | for (li = n_lines - 1; li >= 0; li--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = n_lines - 1; li >= 0; li--) { |
2 |
1 |
3 |
|
| 338 |
2 |
| lines[li].cross_pos = pos; |
| 339 |
2 |
| pos += lines[li].cross_size; |
| 340 |
2 |
▶ | if (li > 0) pos += gbw; |
Branch: 1 branch
| Branch | True | False | Total |
| if (li > 0) pos += gbw; |
1 |
1 |
2 |
|
| 341 |
|
| } |
| 342 |
|
| } else { |
| 343 |
13 |
| pos = gb; |
| 344 |
40 |
▶ | for (li = 0; li < n_lines; li++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; li < n_lines; li++) { |
27 |
13 |
40 |
|
| 345 |
27 |
| lines[li].cross_pos = pos; |
| 346 |
27 |
| pos += lines[li].cross_size; |
| 347 |
27 |
▶ | if (li < n_lines - 1) pos += gbw; |
Branch: 1 branch
| Branch | True | False | Total |
| if (li < n_lines - 1) pos += gbw; |
14 |
13 |
27 |
|
| 348 |
|
| } |
| 349 |
|
| } |
| 350 |
|
| } |
| 351 |
|
| } |
| 352 |
|
| |
| 353 |
|
| /* ── 6. place items ─────────────────────────────────────────── */ |
| 354 |
217 |
▶ | for (li = 0; li < n_lines; li++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (li = 0; li < n_lines; li++) { |
116 |
101 |
217 |
|
| 355 |
116 |
| int start = lines[li].start; |
| 356 |
116 |
| int count = lines[li].count; |
| 357 |
116 |
| int end = start + count; |
| 358 |
116 |
| float lcs = lines[li].cross_size; |
| 359 |
116 |
| float cross_origin = lines[li].cross_pos; |
| 360 |
|
| float gb, gbw, main_pos; |
| 361 |
|
| |
| 362 |
116 |
| lf__justify(ctx->justify, ctx->main_size, ctx->main_gap, |
| 363 |
116 |
| ctx->items, sizes, start, count, &gb, &gbw); |
| 364 |
116 |
| main_pos = gb; |
| 365 |
|
| |
| 366 |
307 |
▶ | for (i = start; i < end; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = start; i < end; i++) { |
191 |
116 |
307 |
|
| 367 |
191 |
| const lf_item_t *it = &ctx->items[i]; |
| 368 |
191 |
▶ | int al = (it->align_self >= 0) ? it->align_self : ctx->align_items; |
Branch: 1 branch
| Branch | True | False | Total |
| int al = (it->align_self >= 0) ? it->align_self : ctx->align_items; |
4 |
187 |
191 |
|
| 369 |
191 |
| float mn_c = it->min_cross; |
| 370 |
191 |
▶ | float mx_c = it->max_cross > 0.0f ? it->max_cross : FLT_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| float mx_c = it->max_cross > 0.0f ? it->max_cross : FLT_MAX; |
3 |
188 |
191 |
|
| 371 |
191 |
| float nat = it->cross; |
| 372 |
191 |
| float mcs = it->margin_cross_start; |
| 373 |
191 |
| float mce = it->margin_cross_end; |
| 374 |
191 |
| float avail = lcs - mcs - mce; /* cross space inside margins */ |
| 375 |
|
| float ic, cp; |
| 376 |
|
| |
| 377 |
191 |
| switch (al) { |
| 378 |
105 |
| case LF_ALIGN_STRETCH: |
| 379 |
105 |
| ic = lf__clamp(avail, mn_c, mx_c); |
| 380 |
105 |
| cp = mcs; |
| 381 |
105 |
| break; |
| 382 |
78 |
| case LF_ALIGN_START: |
| 383 |
74 |
| ic = (nat > 0.0f) ? lf__clamp(nat, mn_c, lf__fmin(mx_c, avail)) |
| 384 |
78 |
▶ | : lf__clamp(avail, mn_c, mx_c); |
Branch: 1 branch
| Branch | True | False | Total |
| : lf__clamp(avail, mn_c, mx_c); |
74 |
4 |
78 |
|
| 385 |
78 |
| cp = mcs; |
| 386 |
78 |
| break; |
| 387 |
4 |
| case LF_ALIGN_END: |
| 388 |
4 |
| ic = (nat > 0.0f) ? lf__clamp(nat, mn_c, lf__fmin(mx_c, avail)) |
| 389 |
4 |
▶ | : lf__clamp(avail, mn_c, mx_c); |
Branch: 1 branch
| Branch | True | False | Total |
| : lf__clamp(avail, mn_c, mx_c); |
4 |
0 |
4 |
|
| 390 |
4 |
| cp = lcs - ic - mce; |
| 391 |
4 |
| break; |
| 392 |
4 |
| case LF_ALIGN_CENTER: |
| 393 |
4 |
| ic = (nat > 0.0f) ? lf__clamp(nat, mn_c, lf__fmin(mx_c, avail)) |
| 394 |
4 |
▶ | : lf__clamp(avail, mn_c, mx_c); |
Branch: 1 branch
| Branch | True | False | Total |
| : lf__clamp(avail, mn_c, mx_c); |
4 |
0 |
4 |
|
| 395 |
4 |
| cp = mcs + (avail - ic) / 2.0f; |
| 396 |
4 |
| break; |
| 397 |
0 |
| default: |
| 398 |
0 |
| ic = avail; cp = mcs; |
| 399 |
|
| } |
| 400 |
|
| |
| 401 |
|
| /* place content box after margin_main_start */ |
| 402 |
191 |
| main_pos += it->margin_main_start; |
| 403 |
|
| |
| 404 |
191 |
▶ | if (ctx->direction == LF_DIR_ROW) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->direction == LF_DIR_ROW) { |
179 |
12 |
191 |
|
| 405 |
179 |
| out[i].x = main_pos; |
| 406 |
179 |
| out[i].y = cross_origin + cp; |
| 407 |
179 |
| out[i].w = sizes[i]; |
| 408 |
179 |
| out[i].h = ic; |
| 409 |
|
| } else { |
| 410 |
12 |
| out[i].x = cross_origin + cp; |
| 411 |
12 |
| out[i].y = main_pos; |
| 412 |
12 |
| out[i].w = ic; |
| 413 |
12 |
| out[i].h = sizes[i]; |
| 414 |
|
| } |
| 415 |
|
| |
| 416 |
191 |
| main_pos += sizes[i] + it->margin_main_end; |
| 417 |
191 |
▶ | if (i < end - 1) main_pos += gbw; |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < end - 1) main_pos += gbw; |
75 |
116 |
191 |
|
| 418 |
|
| } |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
101 |
| free(sizes); |
| 422 |
101 |
| free(lines); |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
|
| #endif /* LAYOUT_FLEX_H */ |