-
Notifications
You must be signed in to change notification settings - Fork 1
/
box.cpp
433 lines (381 loc) · 8.03 KB
/
box.cpp
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
#include "litehtml/html.h"
#include "litehtml/box.h"
#include "litehtml/html_tag.h"
litehtml::box_type litehtml::block_box::get_type()
{
return box_block;
}
int litehtml::block_box::height()
{
return m_element->height();
}
int litehtml::block_box::width()
{
return m_element->width();
}
void litehtml::block_box::add_element(const element::ptr &el)
{
m_element = el;
el->m_box = this;
}
void litehtml::block_box::finish(bool last_box)
{
if(!m_element) return;
m_element->apply_relative_shift(m_box_right - m_box_left);
}
bool litehtml::block_box::can_hold(const element::ptr &el, white_space ws)
{
if(m_element || el->is_inline_box())
{
return false;
}
return true;
}
bool litehtml::block_box::is_empty()
{
if(m_element)
{
return false;
}
return true;
}
int litehtml::block_box::baseline()
{
if(m_element)
{
return m_element->get_base_line();
}
return 0;
}
void litehtml::block_box::get_elements( elements_vector& els )
{
els.push_back(m_element);
}
int litehtml::block_box::top_margin()
{
if(m_element && m_element->collapse_top_margin())
{
return m_element->m_margins.top;
}
return 0;
}
int litehtml::block_box::bottom_margin()
{
if(m_element && m_element->collapse_bottom_margin())
{
return m_element->m_margins.bottom;
}
return 0;
}
void litehtml::block_box::y_shift( int shift )
{
m_box_top += shift;
if(m_element)
{
m_element->m_pos.y += shift;
}
}
void litehtml::block_box::new_width( int left, int right, elements_vector& els )
{
}
//////////////////////////////////////////////////////////////////////////
litehtml::box_type litehtml::line_box::get_type()
{
return box_line;
}
int litehtml::line_box::height()
{
return m_height;
}
int litehtml::line_box::width()
{
return m_width;
}
void litehtml::line_box::add_element(const element::ptr &el)
{
el->m_skip = false;
el->m_box = 0;
bool add = true;
if( (m_items.empty() && el->is_white_space()) || el->is_break() )
{
el->m_skip = true;
} else if(el->is_white_space())
{
if (have_last_space())
{
add = false;
el->m_skip = true;
}
}
if(add)
{
el->m_box = this;
m_items.push_back(el);
if(!el->m_skip)
{
int el_shift_left = el->get_inline_shift_left();
int el_shift_right = el->get_inline_shift_right();
el->m_pos.x = m_box_left + m_width + el_shift_left + el->content_margins_left();
el->m_pos.y = m_box_top + el->content_margins_top();
m_width += el->width() + el_shift_left + el_shift_right;
}
}
}
void litehtml::line_box::finish(bool last_box)
{
if( is_empty() || (!is_empty() && last_box && is_break_only()) )
{
m_height = 0;
return;
}
for(auto i = m_items.rbegin(); i != m_items.rend(); i++)
{
if((*i)->is_white_space() || (*i)->is_break())
{
if(!(*i)->m_skip)
{
(*i)->m_skip = true;
m_width -= (*i)->width();
}
} else
{
break;
}
}
int base_line = m_font_metrics.base_line();
int line_height = m_line_height;
int add_x = 0;
switch(m_text_align)
{
case text_align_right:
if(m_width < (m_box_right - m_box_left))
{
add_x = (m_box_right - m_box_left) - m_width;
}
break;
case text_align_center:
if(m_width < (m_box_right - m_box_left))
{
add_x = ((m_box_right - m_box_left) - m_width) / 2;
}
break;
default:
add_x = 0;
}
m_height = 0;
// find line box baseline and line-height
for(const auto& el : m_items)
{
if(el->get_display() == display_inline_text)
{
font_metrics fm;
el->get_font(&fm);
base_line = std::max(base_line, fm.base_line());
line_height = std::max(line_height, el->line_height());
m_height = std::max(m_height, fm.height);
}
el->m_pos.x += add_x;
}
if(m_height)
{
base_line += (line_height - m_height) / 2;
}
m_height = line_height;
int y1 = 0;
int y2 = m_height;
for (const auto& el : m_items)
{
if(el->get_display() == display_inline_text)
{
font_metrics fm;
el->get_font(&fm);
el->m_pos.y = m_height - base_line - fm.ascent;
} else
{
switch(el->get_vertical_align())
{
case va_super:
case va_sub:
case va_baseline:
el->m_pos.y = m_height - base_line - el->height() + el->get_base_line() + el->content_margins_top();
break;
case va_top:
el->m_pos.y = y1 + el->content_margins_top();
break;
case va_text_top:
el->m_pos.y = m_height - base_line - m_font_metrics.ascent + el->content_margins_top();
break;
case va_middle:
el->m_pos.y = m_height - base_line - m_font_metrics.x_height / 2 - el->height() / 2 + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = y2 - el->height() + el->content_margins_top();
break;
case va_text_bottom:
el->m_pos.y = m_height - base_line + m_font_metrics.descent - el->height() + el->content_margins_top();
break;
}
y1 = std::min(y1, el->top());
y2 = std::max(y2, el->bottom());
}
}
css_offsets offsets;
for (const auto& el : m_items)
{
el->m_pos.y -= y1;
el->m_pos.y += m_box_top;
if(el->get_display() != display_inline_text)
{
switch(el->get_vertical_align())
{
case va_top:
el->m_pos.y = m_box_top + el->content_margins_top();
break;
case va_bottom:
el->m_pos.y = m_box_top + (y2 - y1) - el->height() + el->content_margins_top();
break;
case va_baseline:
//TODO: process vertical align "baseline"
break;
case va_middle:
//TODO: process vertical align "middle"
break;
case va_sub:
//TODO: process vertical align "sub"
break;
case va_super:
//TODO: process vertical align "super"
break;
case va_text_bottom:
//TODO: process vertical align "text-bottom"
break;
case va_text_top:
//TODO: process vertical align "text-top"
break;
}
}
el->apply_relative_shift(m_box_right - m_box_left);
}
m_height = y2 - y1;
m_baseline = (base_line - y1) - (m_height - line_height);
}
bool litehtml::line_box::can_hold(const element::ptr &el, white_space ws)
{
if(!el->is_inline_box()) return false;
if(el->is_break())
{
return false;
}
if(ws == white_space_nowrap || ws == white_space_pre)
{
return true;
}
if(m_box_left + m_width + el->width() + el->get_inline_shift_left() + el->get_inline_shift_right() > m_box_right)
{
return false;
}
return true;
}
bool litehtml::line_box::have_last_space()
{
bool ret = false;
for (auto i = m_items.rbegin(); i != m_items.rend() && !ret; i++)
{
if((*i)->is_white_space() || (*i)->is_break())
{
ret = true;
} else
{
break;
}
}
return ret;
}
bool litehtml::line_box::is_empty()
{
if(m_items.empty()) return true;
for (auto i = m_items.rbegin(); i != m_items.rend(); i++)
{
if(!(*i)->m_skip || (*i)->is_break())
{
return false;
}
}
return true;
}
int litehtml::line_box::baseline()
{
return m_baseline;
}
void litehtml::line_box::get_elements( elements_vector& els )
{
els.insert(els.begin(), m_items.begin(), m_items.end());
}
int litehtml::line_box::top_margin()
{
return 0;
}
int litehtml::line_box::bottom_margin()
{
return 0;
}
void litehtml::line_box::y_shift( int shift )
{
m_box_top += shift;
for (auto& el : m_items)
{
el->m_pos.y += shift;
}
}
bool litehtml::line_box::is_break_only()
{
if(m_items.empty()) return true;
if(m_items.front()->is_break())
{
for (auto& el : m_items)
{
if(!el->m_skip)
{
return false;
}
}
return true;
}
return false;
}
void litehtml::line_box::new_width( int left, int right, elements_vector& els )
{
int add = left - m_box_left;
if(add)
{
m_box_left = left;
m_box_right = right;
m_width = 0;
auto remove_begin = m_items.end();
for (auto i = m_items.begin() + 1; i != m_items.end(); i++)
{
element::ptr el = (*i);
if(!el->m_skip)
{
if(m_box_left + m_width + el->width() + el->get_inline_shift_right() + el->get_inline_shift_left() > m_box_right)
{
remove_begin = i;
break;
} else
{
el->m_pos.x += add;
m_width += el->width() + el->get_inline_shift_right() + el->get_inline_shift_left();
}
}
}
if(remove_begin != m_items.end())
{
els.insert(els.begin(), remove_begin, m_items.end());
m_items.erase(remove_begin, m_items.end());
for(const auto& el : els)
{
el->m_box = 0;
}
}
}
}