1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
#include "html.h"
#include "types.h"
#include "render_flex.h"
#include <cmath>
namespace litehtml
{
class flex_justify_content_spread
{
flex_justify_content m_type;
int m_num_items;
int m_free_space;
bool m_row_direction;
bool m_reverse;
public:
flex_justify_content_spread(flex_justify_content type, int num_items, int free_space, bool row_direction, bool reverse) :
m_type(type), m_num_items(num_items), m_free_space(0), m_row_direction(row_direction), m_reverse(reverse)
{
set_free_space(free_space);
}
void set_free_space(int free_space)
{
m_free_space = free_space;
switch (m_type)
{
case flex_justify_content_space_between:
// If the leftover free-space is negative or there is only a single flex item on the line, this
// value is identical to flex-start.
if(m_num_items == 1 || m_free_space < 0) m_type = flex_justify_content_flex_start;
break;
case flex_justify_content_space_around:
case flex_justify_content_space_evenly:
// If the leftover free-space is negative or there is only a single flex item on the line, this
// value is identical to center
if(m_num_items == 1 || m_free_space < 0) m_type = flex_justify_content_center;
break;
case flex_justify_content_right:
case flex_justify_content_left:
if(!m_row_direction)
{
m_type = flex_justify_content_start;
}
break;
default:
break;
}
}
int start()
{
switch (m_type)
{
case flex_justify_content_right:
if(!m_reverse)
{
return m_free_space;
}
return 0;
case flex_justify_content_start:
case flex_justify_content_left:
if(m_reverse)
{
return m_free_space;
}
return 0;
case flex_justify_content_flex_end:
case flex_justify_content_end:
return m_free_space;
case flex_justify_content_center:
return m_free_space / 2;
case flex_justify_content_space_between:
case flex_justify_content_space_around:
default:
// using flex-start by default
return 0;
}
}
int before_item()
{
switch (m_type)
{
case flex_justify_content_space_evenly:
return m_free_space / (m_num_items + 1);
case flex_justify_content_space_between:
return 0;
case flex_justify_content_space_around:
return m_free_space / (m_num_items * 2);
default:
return 0;
}
}
int after_item()
{
switch (m_type)
{
case flex_justify_content_space_between:
return m_free_space / (m_num_items - 1);
case flex_justify_content_space_around:
return m_free_space / (m_num_items * 2);
default:
return 0;
}
}
};
class flex_align_content_spread
{
flex_align_content m_type;
int m_num_lines;
int m_free_space;
flex_wrap m_wrap;
public:
flex_align_content_spread(flex_align_content type, flex_wrap wrap, int num_lines, int free_space) :
m_type(type), m_num_lines(num_lines), m_free_space(0), m_wrap(wrap)
{
if(m_wrap == flex_wrap_nowrap)
{
m_type = flex_align_content_stretch;
}
set_free_space(free_space);
}
void set_free_space(int free_space)
{
m_free_space = free_space;
switch (m_type)
{
case flex_align_content_space_between:
// If the leftover free-space is negative or there is only a single flex line in the flex
// container, this value is identical to flex-start.
if(m_num_lines == 1 || m_free_space < 0) m_type = flex_align_content_flex_start;
break;
case flex_align_content_space_around:
// If the leftover free-space is negative this value is identical to center.
if(m_num_lines == 1 || m_free_space < 0) m_type = flex_align_content_center;
break;
default:
break;
}
}
int start()
{
switch (m_type)
{
case flex_align_content_flex_end:
case flex_align_content_end:
return m_free_space;
case flex_align_content_center:
return m_free_space / 2;
case flex_align_content_stretch:
case flex_align_content_space_between:
case flex_align_content_space_around:
default:
// using stretch by default
return 0;
}
}
int add_line_size()
{
switch (m_type)
{
case flex_align_content_flex_start:
case flex_align_content_flex_end:
case flex_align_content_start:
case flex_align_content_end:
case flex_align_content_center:
case flex_align_content_space_between:
case flex_align_content_space_around:
return 0;
case flex_align_content_stretch:
default:
return m_free_space / m_num_lines;
}
}
int before_line()
{
switch (m_type)
{
case flex_align_content_space_between:
return 0;
case flex_align_content_space_around:
return m_free_space / (m_num_lines * 2);
default:
return 0;
}
}
int after_line()
{
switch (m_type)
{
case flex_align_content_space_between:
return m_free_space / (m_num_lines - 1);
case flex_align_content_space_around:
return m_free_space / (m_num_lines * 2);
default:
return 0;
}
}
};
}
int litehtml::render_item_flex::_render_content(int x, int y, bool second_pass, const containing_block_context &self_size, formatting_context* fmt_ctx)
{
bool is_row_direction = true;
bool reverse = false;
int container_main_size = self_size.render_width;
switch (css().get_flex_direction())
{
case flex_direction_column:
is_row_direction = false;
reverse = false;
break;
case flex_direction_column_reverse:
is_row_direction = false;
reverse = true;
break;
case flex_direction_row:
is_row_direction = true;
reverse = false;
break;
case flex_direction_row_reverse:
is_row_direction = true;
reverse = true;
break;
}
bool single_line = css().get_flex_wrap() == flex_wrap_nowrap;
bool fit_container = false;
if(!is_row_direction)
{
if(self_size.height.type != containing_block_context::cbc_value_type_auto)
{
container_main_size = self_size.height;
if (css().get_box_sizing() == box_sizing_border_box)
{
container_main_size -= box_sizing_height();
}
} else
{
// Direction columns, height is auto - always in single line
container_main_size = 0;
single_line = true;
fit_container = true;
}
if(self_size.min_height.type != containing_block_context::cbc_value_type_auto && self_size.min_height > container_main_size)
{
container_main_size = self_size.min_height;
}
if(self_size.max_height.type != containing_block_context::cbc_value_type_auto && self_size.max_height > container_main_size)
{
container_main_size = self_size.max_height;
single_line = false;
}
}
/////////////////////////////////////////////////////////////////
/// Split flex items to lines
/////////////////////////////////////////////////////////////////
m_lines = get_lines(self_size, fmt_ctx, is_row_direction, container_main_size, single_line);
int el_y = 0;
int el_x = 0;
int sum_cross_size = 0;
int sum_main_size = 0;
int ret_width = 0;
/////////////////////////////////////////////////////////////////
/// Resolving Flexible Lengths
/// REF: https://www.w3.org/TR/css-flexbox-1/#resolve-flexible-lengths
/////////////////////////////////////////////////////////////////
for(auto& ln : m_lines)
{
if(is_row_direction)
{
ret_width += ln.base_size;
}
ln.init(container_main_size, fit_container, is_row_direction, self_size, fmt_ctx);
sum_cross_size += ln.cross_size;
sum_main_size = std::max(sum_main_size, ln.main_size);
if(reverse)
{
ln.items.reverse();
}
}
int free_cross_size = 0;
int cross_start = 0;
bool is_wrap_reverse = css().get_flex_wrap() == flex_wrap_wrap_reverse;
if(container_main_size == 0)
{
container_main_size = sum_main_size;
}
/////////////////////////////////////////////////////////////////
/// Calculate free cross size
/////////////////////////////////////////////////////////////////
if (is_row_direction)
{
cross_start = content_offset_top();
if (self_size.height.type != containing_block_context::cbc_value_type_auto)
{
int height = self_size.height;
if (src_el()->css().get_box_sizing() == box_sizing_border_box)
{
height -= box_sizing_height();
}
free_cross_size = height - sum_cross_size;
}
} else
{
cross_start = content_offset_left();
free_cross_size = self_size.render_width - sum_cross_size;
ret_width = sum_cross_size;
}
/////////////////////////////////////////////////////////////////
/// Fix align-content property
/////////////////////////////////////////////////////////////////
flex_align_content align_content = css().get_flex_align_content();
if(align_content == flex_align_content_space_between)
{
// If the leftover free-space is negative or there is only a single flex line in the flex
// container, this value is identical to flex-start.
if (m_lines.size() == 1 || free_cross_size < 0) align_content = flex_align_content_flex_start;
}
if(align_content == flex_align_content_space_around)
{
// If the leftover free-space is negative or there is only a single flex line in the flex
// container, this value is identical to flex-start.
if (m_lines.size() == 1 || free_cross_size < 0) align_content = flex_align_content_center;
}
/////////////////////////////////////////////////////////////////
/// Distribute free cross size for align-content: stretch
/////////////////////////////////////////////////////////////////
if(css().get_flex_align_content() == flex_align_content_stretch && free_cross_size > 0)
{
int add = (int)((double) free_cross_size / (double) m_lines.size());
if(add > 0)
{
for (auto &ln: m_lines)
{
ln.cross_size += add;
free_cross_size -= add;
}
}
if(!m_lines.empty())
{
while (free_cross_size > 0)
{
for (auto &ln: m_lines)
{
ln.cross_size++;
free_cross_size--;
}
}
}
}
/// Reverse lines for flex-wrap: wrap-reverse
if(css().get_flex_wrap() == flex_wrap_wrap_reverse)
{
m_lines.reverse();
}
/////////////////////////////////////////////////////////////////
/// Align flex lines
/////////////////////////////////////////////////////////////////
int line_pos = 0;
int add_before_line = 0;
int add_after_line = 0;
switch (align_content)
{
case flex_align_content_flex_start:
if(is_wrap_reverse)
{
line_pos = free_cross_size;
}
break;
case flex_align_content_flex_end:
if(!is_wrap_reverse)
{
line_pos = free_cross_size;
}
break;
case flex_align_content_end:
line_pos = free_cross_size;
break;
case flex_align_content_center:
line_pos = free_cross_size / 2;
break;
case flex_align_content_space_between:
add_after_line = free_cross_size / ((int) m_lines.size() - 1);
break;
case flex_align_content_space_around:
add_before_line = add_after_line = free_cross_size / ((int) m_lines.size() * 2);
break;
default:
if(is_wrap_reverse)
{
line_pos = free_cross_size;
}
break;
}
for(auto &ln : m_lines)
{
line_pos += add_before_line;
ln.cross_start = line_pos;
line_pos += ln.cross_size + add_after_line;
}
/// Fix justify-content property
flex_justify_content justify_content = css().get_flex_justify_content();
if((justify_content == flex_justify_content_right || justify_content == flex_justify_content_left) && !is_row_direction)
{
justify_content = flex_justify_content_start;
}
/////////////////////////////////////////////////////////////////
/// Align flex items in flex lines
/////////////////////////////////////////////////////////////////
int line_num = 0;
for(auto &ln : m_lines)
{
int height = ln.calculate_items_position(container_main_size,
justify_content,
is_row_direction,
self_size,
fmt_ctx);
line_num++;
m_pos.height = std::max(m_pos.height, height);
}
// calculate the final position
m_pos.move_to(x, y);
m_pos.x += content_offset_left();
m_pos.y += content_offset_top();
return ret_width;
}
std::list<litehtml::flex_line> litehtml::render_item_flex::get_lines(const litehtml::containing_block_context &self_size,
litehtml::formatting_context *fmt_ctx,
bool is_row_direction, int container_main_size,
bool single_line)
{
bool reverse_main;
bool reverse_cross = css().get_flex_wrap() == flex_wrap_wrap_reverse;
if(is_row_direction)
{
reverse_main = css().get_flex_direction() == flex_direction_row_reverse;
} else
{
reverse_main = css().get_flex_direction() == flex_direction_column_reverse;
}
std::list<flex_line> lines;
flex_line line(reverse_main, reverse_cross);
std::list<std::shared_ptr<flex_item>> items;
int src_order = 0;
bool sort_required = false;
def_value<int> prev_order(0);
for( auto& el : m_children)
{
std::shared_ptr<flex_item> item = nullptr;
if(is_row_direction)
{
item = std::make_shared<flex_item_row_direction>(el);
} else
{
item = std::make_shared<flex_item_column_direction>(el);
}
item->init(self_size, fmt_ctx, css().get_flex_align_items());
item->src_order = src_order++;
if(prev_order.is_default())
{
prev_order = item->order;
} else if(!sort_required && item->order != prev_order)
{
sort_required = true;
}
items.emplace_back(item);
}
if(sort_required)
{
items.sort([](const std::shared_ptr<flex_item>& item1, const std::shared_ptr<flex_item>& item2)
{
if(item1->order < item2->order) return true;
if(item1->order == item2->order)
{
return item1->src_order < item2->src_order;
}
return false;
});
}
// Add flex items to lines
for(auto& item : items)
{
if(!line.items.empty() && !single_line && line.base_size + item->base_size > container_main_size)
{
lines.emplace_back(line);
line = flex_line(reverse_main, reverse_cross);
}
line.base_size += item->base_size;
line.total_grow += item->grow;
line.total_shrink += item->shrink;
if(!item->auto_margin_main_start.is_default()) line.num_auto_margin_main_start++;
if(!item->auto_margin_main_end.is_default()) line.num_auto_margin_main_end++;
line.items.push_back(item);
}
// Add the last line to the lines list
if(!line.items.empty())
{
lines.emplace_back(line);
}
return lines;
}
std::shared_ptr<litehtml::render_item> litehtml::render_item_flex::init()
{
auto doc = src_el()->get_document();
decltype(m_children) new_children;
decltype(m_children) inlines;
auto convert_inlines = [&]()
{
if(!inlines.empty())
{
// Find last not space
auto not_space = std::find_if(inlines.rbegin(), inlines.rend(), [&](const std::shared_ptr<render_item>& el)
{
return !el->src_el()->is_space();
});
if(not_space != inlines.rend())
{
// Erase all spaces at the end
inlines.erase((not_space.base()), inlines.end());
}
auto anon_el = std::make_shared<html_tag>(src_el());
auto anon_ri = std::make_shared<render_item_block>(anon_el);
for(const auto& inl : inlines)
{
anon_ri->add_child(inl);
}
anon_ri->parent(shared_from_this());
new_children.push_back(anon_ri->init());
inlines.clear();
}
};
for (const auto& el : m_children)
{
if(el->src_el()->css().get_display() == display_inline_text)
{
if(!inlines.empty())
{
inlines.push_back(el);
} else
{
if (!el->src_el()->is_white_space())
{
inlines.push_back(el);
}
}
} else
{
convert_inlines();
if(el->src_el()->is_block_box())
{
// Add block boxes as is
el->parent(shared_from_this());
new_children.push_back(el->init());
} else
{
// Wrap inlines with anonymous block box
auto anon_el = std::make_shared<html_tag>(el->src_el());
auto anon_ri = std::make_shared<render_item_block>(anon_el);
anon_ri->add_child(el->init());
anon_ri->parent(shared_from_this());
new_children.push_back(anon_ri->init());
}
}
}
convert_inlines();
children() = new_children;
return shared_from_this();
}
int litehtml::render_item_flex::get_first_baseline()
{
if(css().get_flex_direction() == flex_direction_row || css().get_flex_direction() == flex_direction_row_reverse)
{
if(!m_lines.empty())
{
const auto &first_line = m_lines.front();
if(first_line.first_baseline.type() != baseline::baseline_type_none)
{
return first_line.cross_start + first_line.first_baseline.get_offset_from_top(first_line.cross_size) + content_offset_top();
}
if(first_line.last_baseline.type() != baseline::baseline_type_none)
{
return first_line.cross_start + first_line.last_baseline.get_offset_from_top(first_line.cross_size) + content_offset_top();
}
}
}
if(!m_lines.empty())
{
if(!m_lines.front().items.empty())
{
return m_lines.front().items.front()->el->get_first_baseline() + content_offset_top();
}
}
return height();
}
int litehtml::render_item_flex::get_last_baseline()
{
if(css().get_flex_direction() == flex_direction_row || css().get_flex_direction() == flex_direction_row_reverse)
{
if(!m_lines.empty())
{
const auto &first_line = m_lines.front();
if(first_line.last_baseline.type() != baseline::baseline_type_none)
{
return first_line.cross_start + first_line.last_baseline.get_offset_from_top(first_line.cross_size) + content_offset_top();
}
if(first_line.first_baseline.type() != baseline::baseline_type_none)
{
return first_line.cross_start + first_line.first_baseline.get_offset_from_top(first_line.cross_size) + content_offset_top();
}
}
}
if(!m_lines.empty())
{
if(!m_lines.front().items.empty())
{
return m_lines.front().items.front()->el->get_last_baseline() + content_offset_top();
}
}
return height();
}
|