NetSurf
layout.c
Go to the documentation of this file.
1/*
2 * Copyright 2005 Richard Wilson <info@tinct.net>
3 * Copyright 2006 James Bursa <bursa@users.sourceforge.net>
4 * Copyright 2008 Michael Drake <tlsa@netsurf-browser.org>
5 * Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
6 *
7 * This file is part of NetSurf, http://www.netsurf-browser.org/
8 *
9 * NetSurf is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * NetSurf is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22/**
23 * \file
24 * HTML layout implementation.
25 *
26 * Layout is carried out in two stages:
27 *
28 * 1. + calculation of minimum / maximum box widths, and
29 * + determination of whether block level boxes will have >zero height
30 *
31 * 2. + layout (position and dimensions)
32 *
33 * In most cases the functions for the two stages are a corresponding pair
34 * layout_minmax_X() and layout_X().
35 */
36
37#include <assert.h>
38#include <limits.h>
39#include <stdbool.h>
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
43#include <math.h>
44#include <dom/dom.h>
45
46#include "utils/log.h"
47#include "utils/talloc.h"
48#include "utils/utils.h"
49#include "utils/nsoption.h"
50#include "utils/corestrings.h"
51#include "utils/nsurl.h"
52#include "netsurf/inttypes.h"
53#include "netsurf/content.h"
55#include "netsurf/layout.h"
56#include "content/content.h"
58#include "css/utils.h"
59#include "desktop/scrollbar.h"
60#include "desktop/textarea.h"
61
62#include "html/html.h"
63#include "html/html_save.h"
64#include "html/private.h"
65#include "html/box.h"
66#include "html/box_inspect.h"
67#include "html/font.h"
68#include "html/form_internal.h"
69#include "html/layout.h"
71#include "html/table.h"
72
73/** Array of per-side access functions for computed style margins. */
75 [TOP] = css_computed_margin_top,
76 [RIGHT] = css_computed_margin_right,
77 [BOTTOM] = css_computed_margin_bottom,
78 [LEFT] = css_computed_margin_left,
79};
80
81/** Array of per-side access functions for computed style paddings. */
83 [TOP] = css_computed_padding_top,
84 [RIGHT] = css_computed_padding_right,
85 [BOTTOM] = css_computed_padding_bottom,
86 [LEFT] = css_computed_padding_left,
87};
88
89/** Array of per-side access functions for computed style border_widths. */
91 [TOP] = css_computed_border_top_width,
92 [RIGHT] = css_computed_border_right_width,
93 [BOTTOM] = css_computed_border_bottom_width,
94 [LEFT] = css_computed_border_left_width,
95};
96
97/** Array of per-side access functions for computed style border styles. */
99 [TOP] = css_computed_border_top_style,
100 [RIGHT] = css_computed_border_right_style,
101 [BOTTOM] = css_computed_border_bottom_style,
102 [LEFT] = css_computed_border_left_style,
103};
104
105/** Array of per-side access functions for computed style border colors. */
107 [TOP] = css_computed_border_top_color,
108 [RIGHT] = css_computed_border_right_color,
109 [BOTTOM] = css_computed_border_bottom_color,
110 [LEFT] = css_computed_border_left_color,
111};
112
113/* forward declaration to break cycles */
114static void layout_minmax_block(
115 struct box *block,
116 const struct gui_layout_table *font_func,
117 const html_content *content);
118
119/**
120 * Compute the size of replaced boxes with auto dimensions, according to
121 * content.
122 *
123 * \param box Box with object
124 * \param width Width value in px or AUTO. If AUTO, updated to value in px.
125 * \param height Height value in px or AUTO. If AUTO, updated to value in px.
126 * \param min_width Box's min width, as given by layout_find_dimensions.
127 * \param max_width Box's max width, as given by layout_find_dimensions.
128 * \param min_height Box's min height, as given by layout_find_dimensions.
129 * \param max_height Box's max height, as given by layout_find_dimensions.
130 *
131 * See CSS 2.1 sections 10.3 and 10.6.
132 */
133static void
135 int *width, int *height,
136 int min_width, int max_width,
137 int min_height, int max_height)
138{
139 assert(box->object != NULL);
140 assert(width != NULL && height != NULL);
141
142 if (*width == AUTO && *height == AUTO) {
143 /* No given dimensions */
144
145 bool scaled = false;
146 int intrinsic_width = content_get_width(box->object);
147 int intrinsic_height = content_get_height(box->object);
148
149 /* use intrinsic dimensions */
150 *width = intrinsic_width;
151 *height = intrinsic_height;
152
153 /* Deal with min/max-width first */
154 if (min_width > 0 && min_width > *width) {
155 *width = min_width;
156 scaled = true;
157 }
158 if (max_width >= 0 && max_width < *width) {
159 *width = max_width;
160 scaled = true;
161 }
162
163 if (scaled && (intrinsic_width != 0)) {
164 /* Update height */
165 *height = (*width * intrinsic_height) /
166 intrinsic_width;
167 }
168
169 scaled = false;
170 /* Deal with min/max-height */
171 if (min_height > 0 && min_height > *height) {
172 *height = min_height;
173 scaled = true;
174 }
175 if (max_height >= 0 && max_height < *height) {
176 *height = max_height;
177 scaled = true;
178 }
179
180 if (scaled && (intrinsic_height != 0)) {
181 /* Update width */
182 *width = (*height * intrinsic_width) /
183 intrinsic_height;
184 }
185
186 } else if (*width == AUTO) {
187 /* Have given height; width is calculated from the given height
188 * and ratio of intrinsic dimensions */
189 int intrinsic_width = content_get_width(box->object);
190 int intrinsic_height = content_get_height(box->object);
191
192 if (intrinsic_height != 0)
193 *width = (*height * intrinsic_width) /
194 intrinsic_height;
195 else
196 *width = intrinsic_width;
197
198 if (min_width > 0 && min_width > *width)
199 *width = min_width;
200 if (max_width >= 0 && max_width < *width)
201 *width = max_width;
202
203 } else if (*height == AUTO) {
204 /* Have given width; height is calculated from the given width
205 * and ratio of intrinsic dimensions */
206 int intrinsic_width = content_get_width(box->object);
207 int intrinsic_height = content_get_height(box->object);
208
209 if (min_width > 0 && min_width > *width)
210 *width = min_width;
211 if (max_width >= 0 && max_width < *width)
212 *width = max_width;
213
214 if (intrinsic_width != 0)
215 *height = (*width * intrinsic_height) /
216 intrinsic_width;
217 else
218 *height = intrinsic_height;
219 }
220}
221
222
223/**
224 * Calculate the text-indent length.
225 *
226 * \param style style of block
227 * \param width width of containing block
228 * \return length of indent
229 */
231 const css_unit_ctx *unit_len_ctx,
232 const css_computed_style *style, int width)
233{
234 css_fixed value = 0;
235 css_unit unit = CSS_UNIT_PX;
236
237 css_computed_text_indent(style, &value, &unit);
238
239 if (unit == CSS_UNIT_PCT) {
240 return FPCT_OF_INT_TOINT(value, width);
241 } else {
242 return FIXTOINT(css_unit_len2device_px(style, unit_len_ctx,
243 value, unit));
244 }
245}
246
247
248/**
249 * Calculate minimum and maximum width of a table.
250 *
251 * \param table box of type TABLE
252 * \param font_func Font functions
253 * \param content The HTML content we are laying out.
254 * \post table->min_width and table->max_width filled in,
255 * 0 <= table->min_width <= table->max_width
256 */
257static void layout_minmax_table(struct box *table,
258 const struct gui_layout_table *font_func,
259 const html_content *content)
260{
261 unsigned int i, j;
262 int border_spacing_h = 0;
263 int table_min = 0, table_max = 0;
264 int extra_fixed = 0;
265 float extra_frac = 0;
266 struct column *col;
267 struct box *row_group, *row, *cell;
268 enum css_width_e wtype;
269 css_fixed value = 0;
270 css_unit unit = CSS_UNIT_PX;
271
272 /* check if the widths have already been calculated */
273 if (table->max_width != UNKNOWN_MAX_WIDTH)
274 return;
275
276 if (table_calculate_column_types(&content->unit_len_ctx, table) == false) {
277 NSLOG(netsurf, ERROR,
278 "Could not establish table column types.");
279 return;
280 }
281 col = table->col;
282
283 /* start with 0 except for fixed-width columns */
284 for (i = 0; i != table->columns; i++) {
285 if (col[i].type == COLUMN_WIDTH_FIXED)
286 col[i].min = col[i].max = col[i].width;
287 else
288 col[i].min = col[i].max = 0;
289 }
290
291 /* border-spacing is used in the separated borders model */
292 if (css_computed_border_collapse(table->style) ==
293 CSS_BORDER_COLLAPSE_SEPARATE) {
294 css_fixed h = 0, v = 0;
295 css_unit hu = CSS_UNIT_PX, vu = CSS_UNIT_PX;
296
297 css_computed_border_spacing(table->style, &h, &hu, &v, &vu);
298
299 border_spacing_h = FIXTOINT(css_unit_len2device_px(
300 table->style,
301 &content->unit_len_ctx,
302 h, hu));
303 }
304
305 /* 1st pass: consider cells with colspan 1 only */
306 for (row_group = table->children; row_group; row_group =row_group->next)
307 for (row = row_group->children; row; row = row->next)
308 for (cell = row->children; cell; cell = cell->next) {
309 assert(cell->type == BOX_TABLE_CELL);
310 assert(cell->style);
311 /** TODO: Handle colspan="0" correctly.
312 * It's currently converted to 1 in box normaisation */
313 assert(cell->columns != 0);
314
315 if (cell->columns != 1)
316 continue;
317
318 layout_minmax_block(cell, font_func, content);
319 i = cell->start_column;
320
321 if (col[i].positioned)
322 continue;
323
324 /* update column min, max widths using cell widths */
325 if (col[i].min < cell->min_width)
326 col[i].min = cell->min_width;
327 if (col[i].max < cell->max_width)
328 col[i].max = cell->max_width;
329 }
330
331 /* 2nd pass: cells which span multiple columns */
332 for (row_group = table->children; row_group; row_group =row_group->next)
333 for (row = row_group->children; row; row = row->next)
334 for (cell = row->children; cell; cell = cell->next) {
335 unsigned int flexible_columns = 0;
336 int min = 0, max = 0, fixed_width = 0, extra;
337
338 if (cell->columns == 1)
339 continue;
340
341 layout_minmax_block(cell, font_func, content);
342 i = cell->start_column;
343
344 /* find min width so far of spanned columns, and count
345 * number of non-fixed spanned columns and total fixed width */
346 for (j = 0; j != cell->columns; j++) {
347 min += col[i + j].min;
348 if (col[i + j].type == COLUMN_WIDTH_FIXED)
349 fixed_width += col[i + j].width;
350 else
351 flexible_columns++;
352 }
353 min += (cell->columns - 1) * border_spacing_h;
354
355 /* distribute extra min to spanned columns */
356 if (min < cell->min_width) {
357 if (flexible_columns == 0) {
358 extra = 1 + (cell->min_width - min) /
359 cell->columns;
360 for (j = 0; j != cell->columns; j++) {
361 col[i + j].min += extra;
362 if (col[i + j].max < col[i + j].min)
363 col[i + j].max = col[i + j].min;
364 }
365 } else {
366 extra = 1 + (cell->min_width - min) /
367 flexible_columns;
368 for (j = 0; j != cell->columns; j++) {
369 if (col[i + j].type !=
370 COLUMN_WIDTH_FIXED) {
371 col[i + j].min += extra;
372 if (col[i + j].max <
373 col[i + j].min)
374 col[i + j].max =
375 col[i + j].min;
376 }
377 }
378 }
379 }
380
381 /* find max width so far of spanned columns */
382 for (j = 0; j != cell->columns; j++)
383 max += col[i + j].max;
384 max += (cell->columns - 1) * border_spacing_h;
385
386 /* distribute extra max to spanned columns */
387 if (max < cell->max_width && flexible_columns) {
388 extra = 1 + (cell->max_width - max) / flexible_columns;
389 for (j = 0; j != cell->columns; j++)
390 if (col[i + j].type != COLUMN_WIDTH_FIXED)
391 col[i + j].max += extra;
392 }
393 }
394
395 for (i = 0; i != table->columns; i++) {
396 if (col[i].max < col[i].min) {
397 box_dump(stderr, table, 0, true);
398 assert(0);
399 }
400 table_min += col[i].min;
401 table_max += col[i].max;
402 }
403
404 /* fixed width takes priority, unless it is too narrow */
405 wtype = css_computed_width(table->style, &value, &unit);
406 if (wtype == CSS_WIDTH_SET && unit != CSS_UNIT_PCT) {
407 int width = FIXTOINT(css_unit_len2device_px(
408 table->style,
409 &content->unit_len_ctx,
410 value, unit));
411 if (table_min < width)
412 table_min = width;
413 if (table_max < width)
414 table_max = width;
415 }
416
417 /* add margins, border, padding to min, max widths */
418 calculate_mbp_width(&content->unit_len_ctx,
419 table->style, LEFT, true, true, true,
420 &extra_fixed, &extra_frac);
421 calculate_mbp_width(&content->unit_len_ctx,
422 table->style, RIGHT, true, true, true,
423 &extra_fixed, &extra_frac);
424 if (extra_fixed < 0)
425 extra_fixed = 0;
426 if (extra_frac < 0)
427 extra_frac = 0;
428 if (1.0 <= extra_frac)
429 extra_frac = 0.9;
430 table->min_width = (table_min + extra_fixed) / (1.0 - extra_frac);
431 table->max_width = (table_max + extra_fixed) / (1.0 - extra_frac);
432 table->min_width += (table->columns + 1) * border_spacing_h;
433 table->max_width += (table->columns + 1) * border_spacing_h;
434
435 assert(0 <= table->min_width && table->min_width <= table->max_width);
436}
437
438/**
439 * Helper to check if a box has percentage max width.
440 *
441 * \param[in] b Box to check.
442 * \return true iff box has percnetage max width.
443 */
444static inline bool box_has_percentage_max_width(struct box *b)
445{
446 css_unit unit = CSS_UNIT_PX;
447 enum css_max_width_e type;
448 css_fixed value = 0;
449
450 assert(b != NULL);
451
452 type = css_computed_max_width(b->style, &value, &unit);
453 return ((type == CSS_MAX_WIDTH_SET) && (unit == CSS_UNIT_PCT));
454}
455
456/**
457 * Calculate minimum and maximum width of a line.
458 *
459 * \param first a box in an inline container
460 * \param line_min updated to minimum width of line starting at first
461 * \param line_max updated to maximum width of line starting at first
462 * \param first_line true iff this is the first line in the inline container
463 * \param line_has_height updated to true or false, depending on line
464 * \param font_func Font functions.
465 * \return first box in next line, or 0 if no more lines
466 * \post 0 <= *line_min <= *line_max
467 */
468static struct box *
470 int *line_min,
471 int *line_max,
472 bool first_line,
473 bool *line_has_height,
474 const struct gui_layout_table *font_func,
475 const html_content *content)
476{
477 int min = 0, max = 0, width, height, fixed;
478 float frac;
479 size_t i, j;
480 struct box *b;
481 struct box *block;
482 plot_font_style_t fstyle;
483 bool no_wrap;
484
485 assert(first->parent);
486 assert(first->parent->parent);
487 assert(first->parent->parent->style);
488
489 block = first->parent->parent;
490 no_wrap = (css_computed_white_space(block->style) ==
491 CSS_WHITE_SPACE_NOWRAP ||
492 css_computed_white_space(block->style) ==
493 CSS_WHITE_SPACE_PRE);
494
495 *line_has_height = false;
496
497 /* corresponds to the pass 1 loop in layout_line() */
498 for (b = first; b; b = b->next) {
499 enum css_width_e wtype;
500 enum css_height_e htype;
501 enum css_box_sizing_e bs;
502 css_fixed value = 0;
503 css_unit unit = CSS_UNIT_PX;
504
505 assert(lh__box_is_inline_content(b));
506
507 NSLOG(layout, DEBUG, "%p: min %i, max %i", b, min, max);
508
509 if (b->type == BOX_BR) {
510 b = b->next;
511 break;
512 }
513
514 if (lh__box_is_float_box(b)) {
515 assert(b->children);
516 if (b->children->type == BOX_TABLE)
517 layout_minmax_table(b->children, font_func,
518 content);
519 else
520 layout_minmax_block(b->children, font_func,
521 content);
524 if (min < b->min_width)
525 min = b->min_width;
526 max += b->max_width;
527 continue;
528 }
529
530 if (b->type == BOX_INLINE_BLOCK || b->type == BOX_INLINE_FLEX) {
531 layout_minmax_block(b, font_func, content);
532 if (min < b->min_width)
533 min = b->min_width;
534 max += b->max_width;
535
536 if (b->flags & HAS_HEIGHT)
537 *line_has_height = true;
538 continue;
539 }
540
541 assert(b->style);
542 font_plot_style_from_css(&content->unit_len_ctx, b->style, &fstyle);
543
544 if (b->type == BOX_INLINE && !b->object &&
545 !(b->flags & REPLACE_DIM) &&
546 !(b->flags & IFRAME)) {
547 fixed = frac = 0;
548 calculate_mbp_width(&content->unit_len_ctx,
549 b->style, LEFT, true, true, true,
550 &fixed, &frac);
551 if (!b->inline_end)
552 calculate_mbp_width(&content->unit_len_ctx,
553 b->style, RIGHT,
554 true, true, true,
555 &fixed, &frac);
556 if (0 < fixed)
557 max += fixed;
558 *line_has_height = true;
559 /* \todo update min width, consider fractional extra */
560 } else if (b->type == BOX_INLINE_END) {
561 fixed = frac = 0;
562 calculate_mbp_width(&content->unit_len_ctx,
564 true, true, true,
565 &fixed, &frac);
566 if (0 < fixed)
567 max += fixed;
568
569 if (b->next) {
570 if (b->space == UNKNOWN_WIDTH) {
571 font_func->width(&fstyle, " ", 1,
572 &b->space);
573 }
574 max += b->space;
575 }
576
577 *line_has_height = true;
578 continue;
579 }
580
581 if (lh__box_is_replace(b) == false) {
582 /* inline non-replaced, 10.3.1 and 10.6.1 */
583 bool no_wrap_box;
584 if (!b->text)
585 continue;
586
587 no_wrap_box = (css_computed_white_space(b->style) ==
588 CSS_WHITE_SPACE_NOWRAP ||
589 css_computed_white_space(b->style) ==
590 CSS_WHITE_SPACE_PRE);
591
592 if (b->width == UNKNOWN_WIDTH) {
593 /** \todo handle errors */
594
595 /* If it's a select element, we must use the
596 * width of the widest option text */
597 if (b->parent->parent->gadget &&
599 == GADGET_SELECT) {
600 int opt_maxwidth = 0;
601 struct form_option *o;
602
603 for (o = b->parent->parent->gadget->
604 data.select.items; o;
605 o = o->next) {
606 int opt_width;
607 font_func->width(&fstyle,
608 o->text,
609 strlen(o->text),
610 &opt_width);
611
612 if (opt_maxwidth < opt_width)
613 opt_maxwidth =opt_width;
614 }
615
616 b->width = opt_maxwidth;
617 if (nsoption_bool(core_select_menu))
619
620 } else {
621 font_func->width(&fstyle, b->text,
622 b->length, &b->width);
623 b->flags |= MEASURED;
624 }
625 }
626 max += b->width;
627 if (b->next) {
628 if (b->space == UNKNOWN_WIDTH) {
629 font_func->width(&fstyle, " ", 1,
630 &b->space);
631 }
632 max += b->space;
633 }
634
635 if (no_wrap) {
636 /* Don't wrap due to block style,
637 * so min is the same as max */
638 min = max;
639
640 } else if (no_wrap_box) {
641 /* This inline box can't be wrapped,
642 * for min, consider box's width */
643 if (min < b->width)
644 min = b->width;
645
646 } else if (b->parent->flags & NEED_MIN) {
647 /* If we care what the minimum width is,
648 * calculate it. (It's only needed if we're
649 * shrinking-to-fit.) */
650 /* min = widest single word */
651 i = 0;
652 do {
653 for (j = i; j != b->length &&
654 b->text[j] != ' '; j++)
655 ;
656 font_func->width(&fstyle, b->text + i,
657 j - i, &width);
658 if (min < width)
659 min = width;
660 i = j + 1;
661 } while (j != b->length);
662 }
663
664 *line_has_height = true;
665
666 continue;
667 }
668
669 /* inline replaced, 10.3.2 and 10.6.2 */
670 assert(b->style);
671
672 /* calculate box width */
673 wtype = css_computed_width(b->style, &value, &unit);
674 bs = css_computed_box_sizing(block->style);
675 if (wtype == CSS_WIDTH_SET) {
676 if (unit == CSS_UNIT_PCT) {
677 width = AUTO;
678 } else {
679 width = FIXTOINT(css_unit_len2device_px(
680 b->style,
681 &content->unit_len_ctx,
682 value, unit));
683
684 if (bs == CSS_BOX_SIZING_BORDER_BOX) {
685 fixed = frac = 0;
686 calculate_mbp_width(&content->unit_len_ctx,
687 block->style, LEFT,
688 false, true, true,
689 &fixed, &frac);
690 calculate_mbp_width(&content->unit_len_ctx,
691 block->style, RIGHT,
692 false, true, true,
693 &fixed, &frac);
694 if (width < fixed) {
695 width = fixed;
696 }
697 }
698 if (width < 0)
699 width = 0;
700 }
701 } else {
702 width = AUTO;
703 }
704
705 /* height */
706 htype = css_computed_height(b->style, &value, &unit);
707 if (htype == CSS_HEIGHT_SET) {
708 height = FIXTOINT(css_unit_len2device_px(
709 b->style,
710 &content->unit_len_ctx,
711 value, unit));
712 } else {
713 height = AUTO;
714 }
715
716 if (b->object || (b->flags & REPLACE_DIM)) {
717 if (b->object) {
718 int temp_height = height;
720 &width, &temp_height,
721 INT_MIN, INT_MAX,
722 INT_MIN, INT_MAX);
723 }
724
725 fixed = frac = 0;
726 if (bs == CSS_BOX_SIZING_BORDER_BOX) {
727 calculate_mbp_width(&content->unit_len_ctx,
728 b->style, LEFT,
729 true, false, false,
730 &fixed, &frac);
731 calculate_mbp_width(&content->unit_len_ctx,
732 b->style, RIGHT,
733 true, false, false,
734 &fixed, &frac);
735 } else {
736 calculate_mbp_width(&content->unit_len_ctx,
737 b->style, LEFT,
738 true, true, true,
739 &fixed, &frac);
740 calculate_mbp_width(&content->unit_len_ctx,
741 b->style, RIGHT,
742 true, true, true,
743 &fixed, &frac);
744 }
745 if (0 < width + fixed)
746 width += fixed;
747 } else if (b->flags & IFRAME) {
748 /* TODO: handle percentage widths properly */
749 if (width == AUTO)
750 width = 400;
751
752 fixed = frac = 0;
753 if (bs == CSS_BOX_SIZING_BORDER_BOX) {
754 calculate_mbp_width(&content->unit_len_ctx,
755 b->style, LEFT,
756 true, false, false,
757 &fixed, &frac);
758 calculate_mbp_width(&content->unit_len_ctx,
759 b->style, RIGHT,
760 true, false, false,
761 &fixed, &frac);
762 } else {
763 calculate_mbp_width(&content->unit_len_ctx,
764 b->style, LEFT,
765 true, true, true,
766 &fixed, &frac);
767 calculate_mbp_width(&content->unit_len_ctx,
768 b->style, RIGHT,
769 true, true, true,
770 &fixed, &frac);
771 }
772
773 if (0 < width + fixed)
774 width += fixed;
775
776 } else {
777 /* form control with no object */
778 if (width == AUTO)
779 width = FIXTOINT(css_unit_len2device_px(
780 b->style,
781 &content->unit_len_ctx,
782 INTTOFIX(1), CSS_UNIT_EM));
783 }
784
786 min = width;
787 if (width > 0)
788 max += width;
789
790 *line_has_height = true;
791 }
792
793 if (first_line) {
794 /* todo: handle percentage values properly */
795 /* todo: handle text-indent interaction with floats */
796 int text_indent = layout_text_indent(&content->unit_len_ctx,
797 first->parent->parent->style, 100);
798 min = (min + text_indent < 0) ? 0 : min + text_indent;
799 max = (max + text_indent < 0) ? 0 : max + text_indent;
800 }
801
802 *line_min = min;
803 *line_max = max;
804
805 NSLOG(layout, DEBUG, "line_min %i, line_max %i", min, max);
806
807 assert(b != first);
808 assert(0 <= *line_min);
809 assert(*line_min <= *line_max);
810 return b;
811}
812
813
814/**
815 * Calculate minimum and maximum width of an inline container.
816 *
817 * \param inline_container box of type INLINE_CONTAINER
818 * \param[out] has_height set to true if container has height
819 * \param font_func Font functions.
820 * \post inline_container->min_width and inline_container->max_width filled in,
821 * 0 <= inline_container->min_width <= inline_container->max_width
822 */
823static void
824layout_minmax_inline_container(struct box *inline_container,
825 bool *has_height,
826 const struct gui_layout_table *font_func,
827 const html_content *content)
828{
829 struct box *child;
830 int line_min = 0, line_max = 0;
831 int min = 0, max = 0;
832 bool first_line = true;
833 bool line_has_height;
834
835 assert(inline_container->type == BOX_INLINE_CONTAINER);
836
837 /* check if the widths have already been calculated */
838 if (inline_container->max_width != UNKNOWN_MAX_WIDTH)
839 return;
840
841 *has_height = false;
842
843 for (child = inline_container->children; child; ) {
844 child = layout_minmax_line(child, &line_min, &line_max,
845 first_line, &line_has_height, font_func,
846 content);
847 if (min < line_min)
848 min = line_min;
849 if (max < line_max)
850 max = line_max;
851 first_line = false;
852 *has_height |= line_has_height;
853 }
854
855 inline_container->min_width = min;
856 inline_container->max_width = max;
857
858 assert(0 <= inline_container->min_width &&
859 inline_container->min_width <=
860 inline_container->max_width);
861}
862
863/**
864 * Calculate minimum and maximum width of a block.
865 *
866 * \param block box of type BLOCK, INLINE_BLOCK, or TABLE_CELL
867 * \param font_func font functions
868 * \param content The HTML content being layed out.
869 * \post block->min_width and block->max_width filled in,
870 * 0 <= block->min_width <= block->max_width
871 */
873 struct box *block,
874 const struct gui_layout_table *font_func,
875 const html_content *content)
876{
877 struct box *child;
878 int min = 0, max = 0;
879 int extra_fixed = 0;
880 float extra_frac = 0;
881 enum css_width_e wtype = CSS_WIDTH_AUTO;
882 css_fixed width = 0;
883 css_unit wunit = CSS_UNIT_PX;
884 enum css_height_e htype = CSS_HEIGHT_AUTO;
885 css_fixed height = 0;
886 css_unit hunit = CSS_UNIT_PX;
887 enum css_box_sizing_e bs = CSS_BOX_SIZING_CONTENT_BOX;
888 bool using_min_border_box = false;
889 bool using_max_border_box = false;
890 bool child_has_height = false;
891
892 assert(block->type == BOX_BLOCK ||
893 block->type == BOX_FLEX ||
894 block->type == BOX_INLINE_FLEX ||
895 block->type == BOX_INLINE_BLOCK ||
896 block->type == BOX_TABLE_CELL);
897
898 /* check if the widths have already been calculated */
899 if (block->max_width != UNKNOWN_MAX_WIDTH)
900 return;
901
902 if (block->style != NULL) {
903 wtype = css_computed_width(block->style, &width, &wunit);
904 htype = css_computed_height(block->style, &height, &hunit);
905 bs = css_computed_box_sizing(block->style);
906 }
907
908 /* set whether the minimum width is of any interest for this box */
909 if (((block->parent && lh__box_is_float_box(block->parent)) ||
910 block->type == BOX_INLINE_BLOCK ||
911 block->type == BOX_INLINE_FLEX) &&
912 wtype != CSS_WIDTH_SET) {
913 /* box shrinks to fit; need minimum width */
914 block->flags |= NEED_MIN;
915 } else if (block->type == BOX_TABLE_CELL) {
916 /* box shrinks to fit; need minimum width */
917 block->flags |= NEED_MIN;
918 } else if (block->parent && (block->parent->flags & NEED_MIN) &&
919 wtype != CSS_WIDTH_SET) {
920 /* box inside shrink-to-fit context; need minimum width */
921 block->flags |= NEED_MIN;
922 } else if (block->parent && (block->parent->type == BOX_FLEX)) {
923 /* box is flex item */
924 block->flags |= NEED_MIN;
925 }
926
927 if (block->gadget && (block->gadget->type == GADGET_TEXTBOX ||
928 block->gadget->type == GADGET_PASSWORD ||
929 block->gadget->type == GADGET_FILE ||
930 block->gadget->type == GADGET_TEXTAREA) &&
931 block->style && wtype == CSS_WIDTH_AUTO) {
932 css_fixed size = INTTOFIX(10);
933 css_unit unit = CSS_UNIT_EM;
934
935 min = max = FIXTOINT(css_unit_len2device_px(block->style,
936 &content->unit_len_ctx, size, unit));
937
938 block->flags |= HAS_HEIGHT;
939 }
940
941 if (block->gadget && (block->gadget->type == GADGET_RADIO ||
942 block->gadget->type == GADGET_CHECKBOX) &&
943 block->style && wtype == CSS_WIDTH_AUTO) {
944 css_fixed size = INTTOFIX(1);
945 css_unit unit = CSS_UNIT_EM;
946
947 /* form checkbox or radio button
948 * if width is AUTO, set it to 1em */
949 min = max = FIXTOINT(css_unit_len2device_px(block->style,
950 &content->unit_len_ctx, size, unit));
951
952 block->flags |= HAS_HEIGHT;
953 }
954
955 if (block->object) {
956 if (content_get_type(block->object) == CONTENT_HTML) {
958 font_func, content);
961 } else {
962 min = max = content_get_width(block->object);
963 }
964
965 block->flags |= HAS_HEIGHT;
966 } else if (block->flags & IFRAME) {
967 /** \todo do we need to know the min/max width of the iframe's
968 * content? */
969 block->flags |= HAS_HEIGHT;
970 } else {
971 /* recurse through children */
972 for (child = block->children; child; child = child->next) {
973 switch (child->type) {
974 case BOX_FLEX:
975 case BOX_BLOCK:
976 layout_minmax_block(child, font_func,
977 content);
978 if (child->flags & HAS_HEIGHT)
979 child_has_height = true;
980 break;
982 if (block->flags & NEED_MIN)
983 child->flags |= NEED_MIN;
984
986 &child_has_height, font_func,
987 content);
988 if (child_has_height &&
989 child ==
990 child->parent->children) {
991 block->flags |= MAKE_HEIGHT;
992 }
993 break;
994 case BOX_TABLE:
995 layout_minmax_table(child, font_func,
996 content);
997 /* todo: fix for zero height tables */
998 child_has_height = true;
999 child->flags |= MAKE_HEIGHT;
1000 break;
1001 default:
1002 assert(0);
1003 }
1004 assert(child->max_width != UNKNOWN_MAX_WIDTH);
1005
1006 if (child->style &&
1007 (css_computed_position(child->style) ==
1008 CSS_POSITION_ABSOLUTE ||
1009 css_computed_position(child->style) ==
1010 CSS_POSITION_FIXED)) {
1011 /* This child is positioned out of normal flow,
1012 * so it will have no affect on width */
1013 continue;
1014 }
1015
1016 if (lh__box_is_flex_container(block) &&
1018 if (block->style != NULL &&
1019 css_computed_flex_wrap(block->style) ==
1020 CSS_FLEX_WRAP_NOWRAP) {
1021 min += child->min_width;
1022 } else {
1023 if (min < child->min_width)
1024 min = child->min_width;
1025 }
1026 max += child->max_width;
1027
1028 } else {
1029 if (min < child->min_width)
1030 min = child->min_width;
1031 if (max < child->max_width)
1032 max = child->max_width;
1033 }
1034
1035 if (child_has_height)
1036 block->flags |= HAS_HEIGHT;
1037 }
1038 }
1039
1040 if (max < min) {
1041 box_dump(stderr, block, 0, true);
1042 assert(0);
1043 }
1044
1045 /* fixed width takes priority */
1046 if (block->type != BOX_TABLE_CELL && !lh__box_is_flex_item(block)) {
1047 bool border_box = bs == CSS_BOX_SIZING_BORDER_BOX;
1048 enum css_max_width_e max_type;
1049 enum css_min_width_e min_type;
1050 css_unit unit = CSS_UNIT_PX;
1051 css_fixed value = 0;
1052
1053 if (wtype == CSS_WIDTH_SET && wunit != CSS_UNIT_PCT) {
1054 min = max = FIXTOINT(
1055 css_unit_len2device_px(block->style,
1056 &content->unit_len_ctx, width, wunit));
1057 using_max_border_box = border_box;
1058 using_min_border_box = border_box;
1059 }
1060
1061 min_type = css_computed_min_width(block->style, &value, &unit);
1062 if (min_type == CSS_MIN_WIDTH_SET && unit != CSS_UNIT_PCT) {
1063 int val = FIXTOINT(css_unit_len2device_px(block->style,
1064 &content->unit_len_ctx, value, unit));
1065
1066 if (min < val) {
1067 min = val;
1068 using_min_border_box = border_box;
1069 }
1070 }
1071
1072 max_type = css_computed_max_width(block->style, &value, &unit);
1073 if (max_type == CSS_MAX_WIDTH_SET && unit != CSS_UNIT_PCT) {
1074 int val = FIXTOINT(css_unit_len2device_px(block->style,
1075 &content->unit_len_ctx, value, unit));
1076
1077 if (val >= 0 && max > val) {
1078 max = val;
1079 using_max_border_box = border_box;
1080 }
1081 }
1082 }
1083
1084 if (htype == CSS_HEIGHT_SET && hunit != CSS_UNIT_PCT &&
1085 height > INTTOFIX(0)) {
1086 block->flags |= MAKE_HEIGHT;
1087 block->flags |= HAS_HEIGHT;
1088 }
1089
1090 /* add margins, border, padding to min, max widths */
1091 /* Note: we don't know available width here so percentage margin
1092 * and paddings are wrong. */
1093 calculate_mbp_width(&content->unit_len_ctx, block->style, LEFT,
1094 false, true, true, &extra_fixed, &extra_frac);
1095 calculate_mbp_width(&content->unit_len_ctx, block->style, RIGHT,
1096 false, true, true, &extra_fixed, &extra_frac);
1097
1098 if (using_max_border_box) {
1099 max -= extra_fixed;
1100 max = max(max, 0);
1101 }
1102
1103 if (using_min_border_box) {
1104 min -= extra_fixed;
1105 min = max(min, 0);
1106 }
1107
1108 if (max < min) {
1109 min = max;
1110 }
1111
1112 calculate_mbp_width(&content->unit_len_ctx, block->style, LEFT,
1113 true, false, false, &extra_fixed, &extra_frac);
1114 calculate_mbp_width(&content->unit_len_ctx, block->style, RIGHT,
1115 true, false, false, &extra_fixed, &extra_frac);
1116
1117 if (extra_fixed < 0)
1118 extra_fixed = 0;
1119 if (extra_frac < 0)
1120 extra_frac = 0;
1121 if (1.0 <= extra_frac)
1122 extra_frac = 0.9;
1123 if (block->style != NULL &&
1124 (css_computed_float(block->style) == CSS_FLOAT_LEFT ||
1125 css_computed_float(block->style) == CSS_FLOAT_RIGHT)) {
1126 /* floated boxs */
1127 block->min_width = min + extra_fixed;
1128 block->max_width = max + extra_fixed;
1129 } else {
1130 /* not floated */
1131 block->min_width = (min + extra_fixed) / (1.0 - extra_frac);
1132 block->max_width = (max + extra_fixed) / (1.0 - extra_frac);
1133 }
1134
1135 assert(0 <= block->min_width);
1136 assert(block->min_width <= block->max_width);
1137}
1138
1139
1140/**
1141 * Find next block that current margin collapses to.
1142 *
1143 * \param unit_len_ctx Length conversion context
1144 * \param box box to start tree-order search from (top margin is included)
1145 * \param block box responsible for current block fromatting context
1146 * \param viewport_height height of viewport in px
1147 * \param max_pos_margin updated to to maximum positive margin encountered
1148 * \param max_neg_margin updated to to maximum negative margin encountered
1149 * \return next box that current margin collapses to, or NULL if none.
1150 */
1151static struct box*
1152layout_next_margin_block(const css_unit_ctx *unit_len_ctx,
1153 struct box *box,
1154 struct box *block,
1155 int viewport_height,
1156 int *max_pos_margin,
1157 int *max_neg_margin)
1158{
1159 assert(block != NULL);
1160
1161 while (box != NULL) {
1162
1163 if (box->type == BOX_INLINE_CONTAINER || (box->style &&
1164 (css_computed_position(box->style) !=
1165 CSS_POSITION_ABSOLUTE &&
1166 css_computed_position(box->style) !=
1167 CSS_POSITION_FIXED))) {
1168 /* Not positioned */
1169
1170 /* Get margins */
1171 if (box->style) {
1172 layout_find_dimensions(unit_len_ctx,
1173 box->parent->width,
1174 viewport_height, box,
1175 box->style,
1176 NULL, NULL, NULL, NULL,
1177 NULL, NULL, box->margin,
1178 box->padding, box->border);
1179
1180 /* Apply top margin */
1181 if (*max_pos_margin < box->margin[TOP])
1182 *max_pos_margin = box->margin[TOP];
1183 else if (*max_neg_margin < -box->margin[TOP])
1184 *max_neg_margin = -box->margin[TOP];
1185 }
1186
1187 /* Check whether box is the box current margin collapses
1188 * to */
1189 if (box->flags & MAKE_HEIGHT ||
1190 box->border[TOP].width ||
1191 box->padding[TOP] ||
1192 (box->style &&
1193 css_computed_overflow_y(box->style) !=
1194 CSS_OVERFLOW_VISIBLE) ||
1197 /* Collapse to this box; return it */
1198 return box;
1199 }
1200 }
1201
1202
1203 /* Find next box */
1204 if (box->type == BOX_BLOCK && !box->object && box->children &&
1205 box->style &&
1206 css_computed_overflow_y(box->style) ==
1207 CSS_OVERFLOW_VISIBLE) {
1208 /* Down into children. */
1209 box = box->children;
1210 } else {
1211 if (!box->next) {
1212 /* No more siblings:
1213 * Go up to first ancestor with a sibling. */
1214 do {
1215 /* Apply bottom margin */
1216 if (*max_pos_margin <
1217 box->margin[BOTTOM])
1218 *max_pos_margin =
1219 box->margin[BOTTOM];
1220 else if (*max_neg_margin <
1221 -box->margin[BOTTOM])
1222 *max_neg_margin =
1223 -box->margin[BOTTOM];
1224
1225 box = box->parent;
1226 } while (box != block && !box->next);
1227
1228 if (box == block) {
1229 /* Margins don't collapse with stuff
1230 * outside the block formatting context
1231 */
1232 return block;
1233 }
1234 }
1235
1236 /* Apply bottom margin */
1237 if (*max_pos_margin < box->margin[BOTTOM])
1238 *max_pos_margin = box->margin[BOTTOM];
1239 else if (*max_neg_margin < -box->margin[BOTTOM])
1240 *max_neg_margin = -box->margin[BOTTOM];
1241
1242 /* To next sibling. */
1243 box = box->next;
1244
1245 /* Get margins */
1246 if (box->style) {
1247 layout_find_dimensions(unit_len_ctx,
1248 box->parent->width,
1249 viewport_height, box,
1250 box->style,
1251 NULL, NULL, NULL, NULL,
1252 NULL, NULL, box->margin,
1253 box->padding, box->border);
1254 }
1255 }
1256 }
1257
1258 return NULL;
1259}
1260
1261
1262/**
1263 * Find y coordinate which clears all floats on left and/or right.
1264 *
1265 * \param fl first float in float list
1266 * \param clear type of clear
1267 * \return y coordinate relative to ancestor box for floats
1268 */
1269static int layout_clear(struct box *fl, enum css_clear_e clear)
1270{
1271 int y = 0;
1272 for (; fl; fl = fl->next_float) {
1273 if ((clear == CSS_CLEAR_LEFT || clear == CSS_CLEAR_BOTH) &&
1274 fl->type == BOX_FLOAT_LEFT)
1275 if (y < fl->y + fl->height)
1276 y = fl->y + fl->height;
1277 if ((clear == CSS_CLEAR_RIGHT || clear == CSS_CLEAR_BOTH) &&
1278 fl->type == BOX_FLOAT_RIGHT)
1279 if (y < fl->y + fl->height)
1280 y = fl->y + fl->height;
1281 }
1282 return y;
1283}
1284
1285
1286/**
1287 * Find left and right edges in a vertical range.
1288 *
1289 * \param fl first float in float list
1290 * \param y0 start of y range to search
1291 * \param y1 end of y range to search
1292 * \param x0 start left edge, updated to available left edge
1293 * \param x1 start right edge, updated to available right edge
1294 * \param left returns float on left if present
1295 * \param right returns float on right if present
1296 */
1297static void
1298find_sides(struct box *fl,
1299 int y0, int y1,
1300 int *x0, int *x1,
1301 struct box **left,
1302 struct box **right)
1303{
1304 int fy0, fy1, fx0, fx1;
1305
1306 NSLOG(layout, DEBUG, "y0 %i, y1 %i, x0 %i, x1 %i", y0, y1, *x0, *x1);
1307
1308 *left = *right = 0;
1309 for (; fl; fl = fl->next_float) {
1310 fy1 = fl->y + fl->height;
1311 if (fy1 < y0) {
1312 /* Floats are sorted in order of decreasing bottom pos.
1313 * Past here, all floats will be too high to concern us.
1314 */
1315 return;
1316 }
1317 fy0 = fl->y;
1318 if (y0 < fy1 && fy0 <= y1) {
1319 if (fl->type == BOX_FLOAT_LEFT) {
1320 fx1 = fl->x + fl->width;
1321 if (*x0 < fx1) {
1322 *x0 = fx1;
1323 *left = fl;
1324 }
1325 } else {
1326 fx0 = fl->x;
1327 if (fx0 < *x1) {
1328 *x1 = fx0;
1329 *right = fl;
1330 }
1331 }
1332 }
1333 }
1334
1335 NSLOG(layout, DEBUG, "x0 %i, x1 %i, left %p, right %p", *x0, *x1,
1336 *left, *right);
1337}
1338
1339
1340
1341
1342/**
1343 * Solve the width constraint as given in CSS 2.1 section 10.3.3.
1344 *
1345 * \param box Box to solve constraint for
1346 * \param available_width Max width available in pixels
1347 * \param width Current box width
1348 * \param lm Min left margin required to avoid floats in px.
1349 * zero if not applicable
1350 * \param rm Min right margin required to avoid floats in px.
1351 * zero if not applicable
1352 * \param max_width Box max-width ( -ve means no max-width to apply)
1353 * \param min_width Box min-width ( <=0 means no min-width to apply)
1354 * \return New box width
1355 *
1356 * \post \a box's left/right margins will be updated.
1357 */
1358static int
1360 int available_width,
1361 int width,
1362 int lm,
1363 int rm,
1364 int max_width,
1365 int min_width)
1366{
1367 bool auto_width = false;
1368
1369 /* Increase specified left/right margins */
1370 if (box->margin[LEFT] != AUTO && box->margin[LEFT] < lm &&
1371 box->margin[LEFT] >= 0)
1372 box->margin[LEFT] = lm;
1373 if (box->margin[RIGHT] != AUTO && box->margin[RIGHT] < rm &&
1374 box->margin[RIGHT] >= 0)
1375 box->margin[RIGHT] = rm;
1376
1377 /* Find width */
1378 if (width == AUTO) {
1379 int margin_left = box->margin[LEFT];
1380 int margin_right = box->margin[RIGHT];
1381
1382 if (margin_left == AUTO) {
1383 margin_left = lm;
1384 }
1385 if (margin_right == AUTO) {
1386 margin_right = rm;
1387 }
1388
1389 width = available_width -
1390 (margin_left + box->border[LEFT].width +
1392 box->border[RIGHT].width + margin_right);
1393 width = width < 0 ? 0 : width;
1394 auto_width = true;
1395 }
1396
1397 if (max_width >= 0 && width > max_width) {
1398 /* max-width is admissable and width exceeds max-width */
1399 width = max_width;
1400 auto_width = false;
1401 }
1402
1403 if (min_width > 0 && width < min_width) {
1404 /* min-width is admissable and width is less than max-width */
1405 width = min_width;
1406 auto_width = false;
1407 }
1408
1409 /* Width was auto, and unconstrained by min/max width, so we're done */
1410 if (auto_width) {
1411 /* any other 'auto' become 0 or the minimum required values */
1412 if (box->margin[LEFT] == AUTO) {
1413 box->margin[LEFT] = lm;
1414 }
1415 if (box->margin[RIGHT] == AUTO) {
1416 box->margin[RIGHT] = rm;
1417 }
1418 return width;
1419 }
1420
1421 /* Width was not auto, or was constrained by min/max width
1422 * Need to compute left/right margins */
1423
1424 /* HTML alignment (only applies to over-constrained boxes) */
1425 if (box->margin[LEFT] != AUTO && box->margin[RIGHT] != AUTO &&
1426 box->parent != NULL && box->parent->style != NULL) {
1427 switch (css_computed_text_align(box->parent->style)) {
1428 case CSS_TEXT_ALIGN_LIBCSS_RIGHT:
1429 box->margin[LEFT] = AUTO;
1430 box->margin[RIGHT] = 0;
1431 break;
1432 case CSS_TEXT_ALIGN_LIBCSS_CENTER:
1433 box->margin[LEFT] = box->margin[RIGHT] = AUTO;
1434 break;
1435 case CSS_TEXT_ALIGN_LIBCSS_LEFT:
1436 box->margin[LEFT] = 0;
1437 box->margin[RIGHT] = AUTO;
1438 break;
1439 default:
1440 /* Leave it alone; no HTML alignment */
1441 break;
1442 }
1443 }
1444
1445 if (box->margin[LEFT] == AUTO && box->margin[RIGHT] == AUTO) {
1446 /* make the margins equal, centering the element */
1447 box->margin[LEFT] = box->margin[RIGHT] =
1448 (available_width - lm - rm -
1450 width + box->padding[RIGHT] +
1451 box->border[RIGHT].width)) / 2;
1452
1453 if (box->margin[LEFT] < 0) {
1454 box->margin[RIGHT] += box->margin[LEFT];
1455 box->margin[LEFT] = 0;
1456 }
1457
1458 box->margin[LEFT] += lm;
1459
1460 } else if (box->margin[LEFT] == AUTO) {
1461 box->margin[LEFT] = available_width - lm -
1463 width + box->padding[RIGHT] +
1465 box->margin[LEFT] = box->margin[LEFT] < lm
1466 ? lm : box->margin[LEFT];
1467 } else {
1468 /* margin-right auto or "over-constrained" */
1469 box->margin[RIGHT] = available_width - rm -
1470 (box->margin[LEFT] + box->border[LEFT].width +
1471 box->padding[LEFT] + width +
1472 box->padding[RIGHT] +
1473 box->border[RIGHT].width);
1474 }
1475
1476 return width;
1477}
1478
1479
1480/**
1481 * Compute dimensions of box, margins, paddings, and borders for a block-level
1482 * element.
1483 *
1484 * \param unit_len_ctx Length conversion context
1485 * \param available_width Max width available in pixels
1486 * \param viewport_height Height of viewport in pixels or -ve if unknown
1487 * \param lm min left margin required to avoid floats in px.
1488 * zero if not applicable
1489 * \param rm min right margin required to avoid floats in px.
1490 * zero if not applicable
1491 * \param box box to find dimensions of. updated with new width,
1492 * height, margins, borders and paddings
1493 *
1494 * See CSS 2.1 10.3.3, 10.3.4, 10.6.2, and 10.6.3.
1495 */
1496static void
1497layout_block_find_dimensions(const css_unit_ctx *unit_len_ctx,
1498 int available_width,
1499 int viewport_height,
1500 int lm,
1501 int rm,
1502 struct box *box)
1503{
1505 int height, max_height, min_height;
1506 int *margin = box->margin;
1507 int *padding = box->padding;
1508 struct box_border *border = box->border;
1509 const css_computed_style *style = box->style;
1510
1511 layout_find_dimensions(unit_len_ctx, available_width, viewport_height, box,
1512 style, &width, &height, &max_width, &min_width,
1513 &max_height, &min_height, margin, padding, border);
1514
1515 if (box->object && !(box->flags & REPLACE_DIM) &&
1517 /* block-level replaced element, see 10.3.4 and 10.6.2 */
1519 min_width, max_width, min_height, max_height);
1520 }
1521
1522 box->width = layout_solve_width(box, available_width, width, lm, rm,
1523 max_width, min_width);
1524 box->height = height;
1525
1526 if (margin[TOP] == AUTO)
1527 margin[TOP] = 0;
1528 if (margin[BOTTOM] == AUTO)
1529 margin[BOTTOM] = 0;
1530}
1531
1532
1533/**
1534 * Manipulate a block's [RB]padding/height/width to accommodate scrollbars
1535 *
1536 * \param box Box to apply scrollbar space too. Must be BOX_BLOCK.
1537 * \param which Which scrollbar to make space for. Must be RIGHT or BOTTOM.
1538 */
1539static void layout_block_add_scrollbar(struct box *box, int which)
1540{
1541 enum css_overflow_e overflow_x, overflow_y;
1542
1543 assert(box->type == BOX_BLOCK && (which == RIGHT || which == BOTTOM));
1544
1545 if (box->style == NULL)
1546 return;
1547
1548 overflow_x = css_computed_overflow_x(box->style);
1549 overflow_y = css_computed_overflow_y(box->style);
1550
1551 if (which == BOTTOM &&
1552 (overflow_x == CSS_OVERFLOW_SCROLL ||
1553 overflow_x == CSS_OVERFLOW_AUTO ||
1554 (box->object &&
1556 /* make space for scrollbar, unless height is AUTO */
1557 if (box->height != AUTO &&
1558 (overflow_x == CSS_OVERFLOW_SCROLL ||
1561 }
1562
1563 } else if (which == RIGHT &&
1564 (overflow_y == CSS_OVERFLOW_SCROLL ||
1565 overflow_y == CSS_OVERFLOW_AUTO ||
1566 (box->object &&
1568 /* make space for scrollbars, unless width is AUTO */
1569 enum css_height_e htype;
1570 css_fixed height = 0;
1571 css_unit hunit = CSS_UNIT_PX;
1572 htype = css_computed_height(box->style, &height, &hunit);
1573
1574 if (which == RIGHT && box->width != AUTO &&
1575 htype == CSS_HEIGHT_SET &&
1576 (overflow_y == CSS_OVERFLOW_SCROLL ||
1580 }
1581 }
1582}
1583
1584
1585/**
1586 * Moves the children of a box by a specified amount
1587 *
1588 * \param box top of tree of boxes
1589 * \param x the amount to move children by horizontally
1590 * \param y the amount to move children by vertically
1591 */
1592static void layout_move_children(struct box *box, int x, int y)
1593{
1594 assert(box);
1595
1596 for (box = box->children; box; box = box->next) {
1597 box->x += x;
1598 box->y += y;
1599 }
1600}
1601
1602
1603/* Documented in layout_internal.h */
1605 struct box *table,
1606 int available_width,
1608{
1609 unsigned int columns = table->columns; /* total columns */
1610 unsigned int i;
1611 unsigned int *row_span;
1612 int *excess_y;
1613 int table_width, min_width = 0, max_width = 0;
1614 int required_width = 0;
1615 int x, remainder = 0, count = 0;
1616 int table_height = 0;
1617 int min_height = 0;
1618 int *xs; /* array of column x positions */
1619 int auto_width;
1620 int spare_width;
1621 int relative_sum = 0;
1622 int border_spacing_h = 0, border_spacing_v = 0;
1623 int spare_height;
1624 int positioned_columns = 0;
1625 struct box *containing_block = NULL;
1626 struct box *c;
1627 struct box *row;
1628 struct box *row_group;
1629 struct box **row_span_cell;
1630 struct column *col;
1631 const css_computed_style *style = table->style;
1632 enum css_width_e wtype;
1633 enum css_height_e htype;
1634 css_fixed value = 0;
1635 css_unit unit = CSS_UNIT_PX;
1636
1637 assert(table->type == BOX_TABLE);
1638 assert(style);
1639 assert(table->children && table->children->children);
1640 assert(columns);
1641
1642 /* allocate working buffers */
1643 col = malloc(columns * sizeof col[0]);
1644 excess_y = malloc(columns * sizeof excess_y[0]);
1645 row_span = malloc(columns * sizeof row_span[0]);
1646 row_span_cell = malloc(columns * sizeof row_span_cell[0]);
1647 xs = malloc((columns + 1) * sizeof xs[0]);
1648 if (!col || !xs || !row_span || !excess_y || !row_span_cell) {
1649 free(col);
1650 free(excess_y);
1651 free(row_span);
1652 free(row_span_cell);
1653 free(xs);
1654 return false;
1655 }
1656
1657 memcpy(col, table->col, sizeof(col[0]) * columns);
1658
1659 /* find margins, paddings, and borders for table and cells */
1660 layout_find_dimensions(&content->unit_len_ctx, available_width, -1, table,
1661 style, 0, 0, 0, 0, 0, 0, table->margin, table->padding,
1662 table->border);
1663 for (row_group = table->children; row_group;
1664 row_group = row_group->next) {
1665 for (row = row_group->children; row; row = row->next) {
1666 for (c = row->children; c; c = c->next) {
1667 enum css_overflow_e overflow_x;
1668 enum css_overflow_e overflow_y;
1669
1670 assert(c->style);
1672 &content->unit_len_ctx, c);
1673 layout_find_dimensions(&content->unit_len_ctx,
1674 available_width, -1, c,
1675 c->style, 0, 0, 0, 0, 0, 0,
1676 0, c->padding, c->border);
1677
1678 overflow_x = css_computed_overflow_x(c->style);
1679 overflow_y = css_computed_overflow_y(c->style);
1680
1681 if (overflow_x == CSS_OVERFLOW_SCROLL ||
1682 overflow_x ==
1683 CSS_OVERFLOW_AUTO) {
1685 }
1686 if (overflow_y == CSS_OVERFLOW_SCROLL ||
1687 overflow_y ==
1688 CSS_OVERFLOW_AUTO) {
1690 }
1691 }
1692 }
1693 }
1694
1695 /* border-spacing is used in the separated borders model */
1696 if (css_computed_border_collapse(style) ==
1697 CSS_BORDER_COLLAPSE_SEPARATE) {
1698 css_fixed h = 0, v = 0;
1699 css_unit hu = CSS_UNIT_PX, vu = CSS_UNIT_PX;
1700
1701 css_computed_border_spacing(style, &h, &hu, &v, &vu);
1702
1703 border_spacing_h = FIXTOINT(css_unit_len2device_px(
1704 style, &content->unit_len_ctx, h, hu));
1705 border_spacing_v = FIXTOINT(css_unit_len2device_px(
1706 style, &content->unit_len_ctx, v, vu));
1707 }
1708
1709 /* find specified table width, or available width if auto-width */
1710 wtype = css_computed_width(style, &value, &unit);
1711 if (wtype == CSS_WIDTH_SET) {
1712 if (unit == CSS_UNIT_PCT) {
1713 table_width = FPCT_OF_INT_TOINT(value, available_width);
1714 } else {
1715 table_width =
1716 FIXTOINT(css_unit_len2device_px(
1717 style, &content->unit_len_ctx,
1718 value, unit));
1719 }
1720
1721 /* specified width includes border */
1722 table_width -= table->border[LEFT].width +
1723 table->border[RIGHT].width;
1724 table_width = table_width < 0 ? 0 : table_width;
1725
1726 auto_width = table_width;
1727 } else {
1728 table_width = AUTO;
1729 auto_width = available_width -
1730 ((table->margin[LEFT] == AUTO ? 0 :
1731 table->margin[LEFT]) +
1732 table->border[LEFT].width +
1733 table->padding[LEFT] +
1734 table->padding[RIGHT] +
1735 table->border[RIGHT].width +
1736 (table->margin[RIGHT] == AUTO ? 0 :
1737 table->margin[RIGHT]));
1738 }
1739
1740 /* Find any table height specified within CSS/HTML */
1741 htype = css_computed_height(style, &value, &unit);
1742 if (htype == CSS_HEIGHT_SET) {
1743 if (unit == CSS_UNIT_PCT) {
1744 /* This is the minimum height for the table
1745 * (see 17.5.3) */
1746 if (css_computed_position(table->style) ==
1747 CSS_POSITION_ABSOLUTE) {
1748 /* Table is absolutely positioned */
1749 assert(table->float_container);
1750 containing_block = table->float_container;
1751 } else if (table->float_container &&
1752 css_computed_position(table->style) !=
1753 CSS_POSITION_ABSOLUTE &&
1754 (css_computed_float(table->style) ==
1755 CSS_FLOAT_LEFT ||
1756 css_computed_float(table->style) ==
1757 CSS_FLOAT_RIGHT)) {
1758 /* Table is a float */
1759 assert(table->parent && table->parent->parent &&
1760 table->parent->parent->parent);
1761 containing_block =
1762 table->parent->parent->parent;
1763 } else if (table->parent && table->parent->type !=
1765 /* Table is a block level element */
1766 containing_block = table->parent;
1767 } else if (table->parent && table->parent->type ==
1769 /* Table is an inline block */
1770 assert(table->parent->parent);
1771 containing_block = table->parent->parent;
1772 }
1773
1774 if (containing_block) {
1775 css_fixed ignored = 0;
1776
1777 htype = css_computed_height(
1778 containing_block->style,
1779 &ignored, &unit);
1780 }
1781
1782 if (containing_block &&
1783 containing_block->height != AUTO &&
1784 (css_computed_position(table->style) ==
1785 CSS_POSITION_ABSOLUTE ||
1786 htype == CSS_HEIGHT_SET)) {
1787 /* Table is absolutely positioned or its
1788 * containing block has a valid specified
1789 * height. (CSS 2.1 Section 10.5) */
1790 min_height = FPCT_OF_INT_TOINT(value,
1791 containing_block->height);
1792 }
1793 } else {
1794 /* This is the minimum height for the table
1795 * (see 17.5.3) */
1796 min_height = FIXTOINT(css_unit_len2device_px(
1797 style, &content->unit_len_ctx,
1798 value, unit));
1799 }
1800 }
1801
1802 /* calculate width required by cells */
1803 for (i = 0; i != columns; i++) {
1804
1805 NSLOG(layout, DEBUG,
1806 "table %p, column %u: type %s, width %i, min %i, max %i",
1807 table,
1808 i,
1809 ((const char *[]){
1810 "UNKNOWN",
1811 "FIXED",
1812 "AUTO",
1813 "PERCENT",
1814 "RELATIVE",
1815 })[col[i].type],
1816 col[i].width,
1817 col[i].min,
1818 col[i].max);
1819
1820
1821 if (col[i].positioned) {
1822 positioned_columns++;
1823 continue;
1824 } else if (col[i].type == COLUMN_WIDTH_FIXED) {
1825 if (col[i].width < col[i].min)
1826 col[i].width = col[i].max = col[i].min;
1827 else
1828 col[i].min = col[i].max = col[i].width;
1829 required_width += col[i].width;
1830 } else if (col[i].type == COLUMN_WIDTH_PERCENT) {
1831 int width = col[i].width * auto_width / 100;
1832 required_width += col[i].min < width ? width :
1833 col[i].min;
1834 } else
1835 required_width += col[i].min;
1836
1837 NSLOG(layout, DEBUG, "required_width %i", required_width);
1838 }
1839 required_width += (columns + 1 - positioned_columns) *
1840 border_spacing_h;
1841
1842 NSLOG(layout, DEBUG,
1843 "width %i, min %i, max %i, auto %i, required %i", table_width,
1844 table->min_width, table->max_width, auto_width, required_width);
1845
1846 if (auto_width < required_width) {
1847 /* table narrower than required width for columns:
1848 * treat percentage widths as maximums */
1849 for (i = 0; i != columns; i++) {
1850 if (col[i].type == COLUMN_WIDTH_RELATIVE)
1851 continue;
1852 if (col[i].type == COLUMN_WIDTH_PERCENT) {
1853 col[i].max = auto_width * col[i].width / 100;
1854 if (col[i].max < col[i].min)
1855 col[i].max = col[i].min;
1856 }
1857 min_width += col[i].min;
1858 max_width += col[i].max;
1859 }
1860 } else {
1861 /* take percentages exactly */
1862 for (i = 0; i != columns; i++) {
1863 if (col[i].type == COLUMN_WIDTH_RELATIVE)
1864 continue;
1865 if (col[i].type == COLUMN_WIDTH_PERCENT) {
1866 int width = auto_width * col[i].width / 100;
1867 if (width < col[i].min)
1868 width = col[i].min;
1869 col[i].min = col[i].width = col[i].max = width;
1870 col[i].type = COLUMN_WIDTH_FIXED;
1871 }
1872 min_width += col[i].min;
1873 max_width += col[i].max;
1874 }
1875 }
1876
1877 /* allocate relative widths */
1878 spare_width = auto_width;
1879 for (i = 0; i != columns; i++) {
1880 if (col[i].type == COLUMN_WIDTH_RELATIVE)
1881 relative_sum += col[i].width;
1882 else if (col[i].type == COLUMN_WIDTH_FIXED)
1883 spare_width -= col[i].width;
1884 else
1885 spare_width -= col[i].min;
1886 }
1887 spare_width -= (columns + 1) * border_spacing_h;
1888 if (relative_sum != 0) {
1889 if (spare_width < 0)
1890 spare_width = 0;
1891 for (i = 0; i != columns; i++) {
1892 if (col[i].type == COLUMN_WIDTH_RELATIVE) {
1893 col[i].min = ceil(col[i].max =
1894 (float) spare_width
1895 * (float) col[i].width
1896 / relative_sum);
1897 min_width += col[i].min;
1898 max_width += col[i].max;
1899 }
1900 }
1901 }
1902 min_width += (columns + 1) * border_spacing_h;
1903 max_width += (columns + 1) * border_spacing_h;
1904
1905 if (auto_width <= min_width) {
1906 /* not enough space: minimise column widths */
1907 for (i = 0; i < columns; i++) {
1908 col[i].width = col[i].min;
1909 }
1910 table_width = min_width;
1911 } else if (max_width <= auto_width) {
1912 /* more space than maximum width */
1913 if (table_width == AUTO) {
1914 /* for auto-width tables, make columns max width */
1915 for (i = 0; i < columns; i++) {
1916 col[i].width = col[i].max;
1917 }
1918 table_width = max_width;
1919 } else {
1920 /* for fixed-width tables, distribute the extra space
1921 * too */
1922 unsigned int flexible_columns = 0;
1923 for (i = 0; i != columns; i++)
1924 if (col[i].type != COLUMN_WIDTH_FIXED)
1925 flexible_columns++;
1926 if (flexible_columns == 0) {
1927 int extra = (table_width - max_width) / columns;
1928 remainder = (table_width - max_width) -
1929 (extra * columns);
1930 for (i = 0; i != columns; i++) {
1931 col[i].width = col[i].max + extra;
1932 count -= remainder;
1933 if (count < 0) {
1934 col[i].width++;
1935 count += columns;
1936 }
1937 }
1938
1939 } else {
1940 int extra = (table_width - max_width) /
1941 flexible_columns;
1942 remainder = (table_width - max_width) -
1943 (extra * flexible_columns);
1944 for (i = 0; i != columns; i++)
1945 if (col[i].type != COLUMN_WIDTH_FIXED) {
1946 col[i].width = col[i].max +
1947 extra;
1948 count -= remainder;
1949 if (count < 0) {
1950 col[i].width++;
1951 count += flexible_columns;
1952 }
1953 }
1954 }
1955 }
1956 } else {
1957 /* space between min and max: fill it exactly */
1958 float scale = (float) (auto_width - min_width) /
1959 (float) (max_width - min_width);
1960 /* fprintf(stderr, "filling, scale %f\n", scale); */
1961 for (i = 0; i < columns; i++) {
1962 col[i].width = col[i].min + (int) (0.5 +
1963 (col[i].max - col[i].min) * scale);
1964 }
1965 table_width = auto_width;
1966 }
1967
1968 xs[0] = x = border_spacing_h;
1969 for (i = 0; i != columns; i++) {
1970 if (!col[i].positioned)
1971 x += col[i].width + border_spacing_h;
1972 xs[i + 1] = x;
1973 row_span[i] = 0;
1974 excess_y[i] = 0;
1975 row_span_cell[i] = 0;
1976 }
1977
1978 /* position cells */
1979 table_height = border_spacing_v;
1980 for (row_group = table->children; row_group;
1981 row_group = row_group->next) {
1982 int row_group_height = 0;
1983 for (row = row_group->children; row; row = row->next) {
1984 int row_height = 0;
1985
1986 htype = css_computed_height(row->style, &value, &unit);
1987 if (htype == CSS_HEIGHT_SET && unit != CSS_UNIT_PCT) {
1988 row_height = FIXTOINT(css_unit_len2device_px(
1989 row->style,
1990 &content->unit_len_ctx,
1991 value, unit));
1992 }
1993 for (c = row->children; c; c = c->next) {
1994 assert(c->style);
1995 c->width = xs[c->start_column + c->columns] -
1996 xs[c->start_column] -
1997 border_spacing_h -
1998 c->border[LEFT].width -
1999 c->padding[LEFT] -
2000 c->padding[RIGHT] -
2001 c->border[RIGHT].width;
2002 c->float_children = 0;
2004
2005 c->height = AUTO;
2006 if (!layout_block_context(c, -1, content)) {
2007 free(col);
2008 free(excess_y);
2009 free(row_span);
2010 free(row_span_cell);
2011 free(xs);
2012 return false;
2013 }
2014 /* warning: c->descendant_y0 and
2015 * c->descendant_y1 used as temporary storage
2016 * until after vertical alignment is complete */
2017 c->descendant_y0 = c->height;
2018 c->descendant_y1 = c->padding[BOTTOM];
2019
2020 htype = css_computed_height(c->style,
2021 &value, &unit);
2022
2023 if (htype == CSS_HEIGHT_SET &&
2024 unit != CSS_UNIT_PCT) {
2025 /* some sites use height="1" or similar
2026 * to attempt to make cells as small as
2027 * possible, so treat it as a minimum */
2028 int h = FIXTOINT(css_unit_len2device_px(
2029 c->style,
2030 &content->unit_len_ctx,
2031 value, unit));
2032 if (c->height < h)
2033 c->height = h;
2034 }
2035 /* specified row height is treated as a minimum
2036 */
2037 if (c->height < row_height)
2038 c->height = row_height;
2039 c->x = xs[c->start_column] +
2040 c->border[LEFT].width;
2041 c->y = c->border[TOP].width;
2042 for (i = 0; i != c->columns; i++) {
2043 row_span[c->start_column + i] = c->rows;
2044 excess_y[c->start_column + i] =
2045 c->border[TOP].width +
2046 c->padding[TOP] +
2047 c->height +
2048 c->padding[BOTTOM] +
2049 c->border[BOTTOM].width;
2050 row_span_cell[c->start_column + i] = 0;
2051 }
2052 row_span_cell[c->start_column] = c;
2053 c->padding[BOTTOM] = -border_spacing_v -
2054 c->border[TOP].width -
2055 c->padding[TOP] -
2056 c->height -
2057 c->border[BOTTOM].width;
2058 }
2059 for (i = 0; i != columns; i++)
2060 if (row_span[i] != 0)
2061 row_span[i]--;
2062 else
2063 row_span_cell[i] = 0;
2064 if (row->next || row_group->next) {
2065 /* row height is greatest excess of a cell
2066 * which ends in this row */
2067 for (i = 0; i != columns; i++)
2068 if (row_span[i] == 0 && row_height <
2069 excess_y[i])
2070 row_height = excess_y[i];
2071 } else {
2072 /* except in the last row */
2073 for (i = 0; i != columns; i++)
2074 if (row_height < excess_y[i])
2075 row_height = excess_y[i];
2076 }
2077 for (i = 0; i != columns; i++) {
2078 if (row_height < excess_y[i])
2079 excess_y[i] -= row_height;
2080 else
2081 excess_y[i] = 0;
2082 if (row_span_cell[i] != 0)
2083 row_span_cell[i]->padding[BOTTOM] +=
2084 row_height +
2085 border_spacing_v;
2086 }
2087
2088 row->x = 0;
2089 row->y = row_group_height;
2090 row->width = table_width;
2091 row->height = row_height;
2092 row_group_height += row_height + border_spacing_v;
2093 }
2094 row_group->x = 0;
2095 row_group->y = table_height;
2096 row_group->width = table_width;
2097 row_group->height = row_group_height;
2098 table_height += row_group_height;
2099 }
2100 /* Table height is either the height of the contents, or specified
2101 * height if greater */
2102 table_height = max(table_height, min_height);
2103 /** \todo distribute spare height over the row groups / rows / cells */
2104
2105 /* perform vertical alignment */
2106 for (row_group = table->children; row_group;
2107 row_group = row_group->next) {
2108 for (row = row_group->children; row; row = row->next) {
2109 for (c = row->children; c; c = c->next) {
2110 enum css_vertical_align_e vertical_align;
2111
2112 /* unextended bottom padding is in
2113 * c->descendant_y1, and unextended
2114 * cell height is in c->descendant_y0 */
2115 spare_height = (c->padding[BOTTOM] -
2116 c->descendant_y1) +
2117 (c->height - c->descendant_y0);
2118
2119 vertical_align = css_computed_vertical_align(
2120 c->style, &value, &unit);
2121
2122 switch (vertical_align) {
2123 case CSS_VERTICAL_ALIGN_SUB:
2124 case CSS_VERTICAL_ALIGN_SUPER:
2125 case CSS_VERTICAL_ALIGN_TEXT_TOP:
2126 case CSS_VERTICAL_ALIGN_TEXT_BOTTOM:
2127 case CSS_VERTICAL_ALIGN_SET:
2128 case CSS_VERTICAL_ALIGN_BASELINE:
2129 /* todo: baseline alignment, for now
2130 * just use ALIGN_TOP */
2131 case CSS_VERTICAL_ALIGN_TOP:
2132 break;
2133 case CSS_VERTICAL_ALIGN_MIDDLE:
2134 c->padding[TOP] += spare_height / 2;
2135 c->padding[BOTTOM] -= spare_height / 2;
2137 spare_height / 2);
2138 break;
2139 case CSS_VERTICAL_ALIGN_BOTTOM:
2140 c->padding[TOP] += spare_height;
2141 c->padding[BOTTOM] -= spare_height;
2143 spare_height);
2144 break;
2145 case CSS_VERTICAL_ALIGN_INHERIT:
2146 assert(0);
2147 break;
2148 }
2149 }
2150 }
2151 }
2152
2153 /* Top and bottom margins of 'auto' are set to 0. CSS2.1 10.6.3 */
2154 if (table->margin[TOP] == AUTO)
2155 table->margin[TOP] = 0;
2156 if (table->margin[BOTTOM] == AUTO)
2157 table->margin[BOTTOM] = 0;
2158
2159 free(col);
2160 free(excess_y);
2161 free(row_span);
2162 free(row_span_cell);
2163 free(xs);
2164
2165 table->width = table_width;
2166 table->height = table_height;
2167
2168 return true;
2169}
2170
2171
2172/**
2173 * Manimpulate box height according to CSS min-height and max-height properties
2174 *
2175 * \param unit_len_ctx CSS length conversion context for document.
2176 * \param box block to modify with any min-height or max-height
2177 * \param container containing block for absolutely positioned elements, or
2178 * NULL for non absolutely positioned elements.
2179 * \return whether the height has been changed
2180 */
2182 const css_unit_ctx *unit_len_ctx,
2183 struct box *box,
2184 struct box *container)
2185{
2186 int h;
2187 struct box *containing_block = NULL;
2188 bool updated = false;
2189
2190 /* Find containing block for percentage heights */
2191 if (box->style != NULL && css_computed_position(box->style) ==
2192 CSS_POSITION_ABSOLUTE) {
2193 /* Box is absolutely positioned */
2194 assert(container);
2195 containing_block = container;
2196 } else if (box->float_container && box->style != NULL &&
2197 (css_computed_float(box->style) == CSS_FLOAT_LEFT ||
2198 css_computed_float(box->style) == CSS_FLOAT_RIGHT)) {
2199 /* Box is a float */
2200 assert(box->parent && box->parent->parent &&
2201 box->parent->parent->parent);
2202 containing_block = box->parent->parent->parent;
2203 } else if (box->parent && box->parent->type != BOX_INLINE_CONTAINER) {
2204 /* Box is a block level element */
2205 containing_block = box->parent;
2206 } else if (box->parent && box->parent->type == BOX_INLINE_CONTAINER) {
2207 /* Box is an inline block */
2208 assert(box->parent->parent);
2209 containing_block = box->parent->parent;
2210 }
2211
2212 if (box->style) {
2213 enum css_height_e htype = CSS_HEIGHT_AUTO;
2214 css_fixed value = 0;
2215 css_unit unit = CSS_UNIT_PX;
2216
2217 if (containing_block) {
2218 htype = css_computed_height(containing_block->style,
2219 &value, &unit);
2220 }
2221
2222 /* max-height */
2223 if (css_computed_max_height(box->style, &value, &unit) ==
2224 CSS_MAX_HEIGHT_SET) {
2225 if (unit == CSS_UNIT_PCT) {
2226 if (containing_block &&
2227 containing_block->height != AUTO &&
2228 (css_computed_position(box->style) ==
2229 CSS_POSITION_ABSOLUTE ||
2230 htype == CSS_HEIGHT_SET)) {
2231 /* Box is absolutely positioned or its
2232 * containing block has a valid
2233 * specified height. (CSS 2.1
2234 * Section 10.5) */
2235 h = FPCT_OF_INT_TOINT(value,
2236 containing_block->height);
2237 if (h < box->height) {
2238 box->height = h;
2239 updated = true;
2240 }
2241 }
2242 } else {
2243 h = FIXTOINT(css_unit_len2device_px(
2244 box->style, unit_len_ctx,
2245 value, unit));
2246 if (h < box->height) {
2247 box->height = h;
2248 updated = true;
2249 }
2250 }
2251 }
2252
2253 /* min-height */
2254 if (ns_computed_min_height(box->style, &value, &unit) ==
2255 CSS_MIN_HEIGHT_SET) {
2256 if (unit == CSS_UNIT_PCT) {
2257 if (containing_block &&
2258 containing_block->height != AUTO &&
2259 (css_computed_position(box->style) ==
2260 CSS_POSITION_ABSOLUTE ||
2261 htype == CSS_HEIGHT_SET)) {
2262 /* Box is absolutely positioned or its
2263 * containing block has a valid
2264 * specified height. (CSS 2.1
2265 * Section 10.5) */
2266 h = FPCT_OF_INT_TOINT(value,
2267 containing_block->height);
2268 if (h > box->height) {
2269 box->height = h;
2270 updated = true;
2271 }
2272 }
2273 } else {
2274 h = FIXTOINT(css_unit_len2device_px(
2275 box->style, unit_len_ctx,
2276 value, unit));
2277 if (h > box->height) {
2278 box->height = h;
2279 updated = true;
2280 }
2281 }
2282 }
2283 }
2284 return updated;
2285}
2286
2287
2288/**
2289 * Layout a block which contains an object.
2290 *
2291 * \param block box of type BLOCK, INLINE_BLOCK, TABLE, or TABLE_CELL
2292 * \return true on success, false on memory exhaustion
2293 */
2294static bool layout_block_object(struct box *block)
2295{
2296 assert(block);
2297 assert(block->type == BOX_BLOCK ||
2298 block->type == BOX_FLEX ||
2299 block->type == BOX_INLINE_BLOCK ||
2300 block->type == BOX_INLINE_FLEX ||
2301 block->type == BOX_TABLE ||
2302 block->type == BOX_TABLE_CELL);
2303 assert(block->object);
2304
2305 NSLOG(layout, DEBUG, "block %p, object %p, width %i", block,
2306 hlcache_handle_get_url(block->object), block->width);
2307
2308 if (content_can_reformat(block->object)) {
2309 content_reformat(block->object, false, block->width, 1);
2310 } else {
2311 /* Non-HTML objects */
2312 /* this case handled already in
2313 * layout_block_find_dimensions() */
2314 }
2315
2316 return true;
2317}
2318
2319
2320/**
2321 * Insert a float into a container.
2322 *
2323 * \param cont block formatting context block, used to contain float
2324 * \param b box to add to float
2325 *
2326 * This sorts floats in order of descending bottom edges.
2327 */
2328static void add_float_to_container(struct box *cont, struct box *b)
2329{
2330 struct box *box = cont->float_children;
2331 int b_bottom = b->y + b->height;
2332
2333 assert(b->type == BOX_FLOAT_LEFT || b->type == BOX_FLOAT_RIGHT);
2334
2335 if (box == NULL) {
2336 /* No other float children */
2337 b->next_float = NULL;
2338 cont->float_children = b;
2339 return;
2340 } else if (b_bottom >= box->y + box->height) {
2341 /* Goes at start of list */
2342 b->next_float = cont->float_children;
2343 cont->float_children = b;
2344 } else {
2345 struct box *prev = NULL;
2346 while (box != NULL && b_bottom < box->y + box->height) {
2347 prev = box;
2348 box = box->next_float;
2349 }
2350 if (prev != NULL) {
2352 prev->next_float = b;
2353 }
2354 }
2355}
2356
2357
2358/**
2359 * Split a text box.
2360 *
2361 * \param content memory pool for any new boxes
2362 * \param fstyle style for text in text box
2363 * \param split_box box with text to split
2364 * \param new_length new length for text in split_box, after splitting
2365 * \param new_width new width for text in split_box, after splitting
2366 * \return true on success, false on memory exhaustion
2367 *
2368 * A new box is created and inserted into the box tree after split_box,
2369 * containing the text after new_length excluding the initial space character.
2370 */
2371static bool
2373 plot_font_style_t *fstyle,
2374 struct box *split_box,
2375 size_t new_length,
2376 int new_width)
2377{
2378 int space_width = split_box->space;
2379 struct box *c2;
2380 const struct gui_layout_table *font_func = content->font_func;
2381 bool space = (split_box->text[new_length] == ' ');
2382 int used_length = new_length + (space ? 1 : 0);
2383
2384 if ((space && space_width == 0) || space_width == UNKNOWN_WIDTH) {
2385 /* We're need to add a space, and we don't know how big
2386 * it's to be, OR we have a space of unknown width anyway;
2387 * Calculate space width */
2388 font_func->width(fstyle, " ", 1, &space_width);
2389 }
2390
2391 if (split_box->space == UNKNOWN_WIDTH)
2392 split_box->space = space_width;
2393 if (!space)
2394 space_width = 0;
2395
2396 /* Create clone of split_box, c2 */
2397 c2 = talloc_memdup(content->bctx, split_box, sizeof *c2);
2398 if (!c2)
2399 return false;
2400 c2->flags |= CLONE;
2401
2402 /* Set remaining text in c2 */
2403 c2->text += used_length;
2404
2405 /* Set c2 according to the remaining text */
2406 c2->width -= new_width + space_width;
2407 c2->flags &= ~MEASURED; /* width has been estimated */
2408 c2->length = split_box->length - used_length;
2409
2410 /* Update split_box for its reduced text */
2411 split_box->width = new_width;
2412 split_box->flags |= MEASURED;
2413 split_box->length = new_length;
2414 split_box->space = space_width;
2415
2416 /* Insert c2 into box list */
2417 c2->next = split_box->next;
2418 split_box->next = c2;
2419 c2->prev = split_box;
2420 if (c2->next)
2421 c2->next->prev = c2;
2422 else
2423 c2->parent->last = c2;
2424
2425 NSLOG(layout, DEBUG,
2426 "split_box %p len: %" PRIsizet " \"%.*s\"",
2427 split_box,
2428 split_box->length,
2429 (int)split_box->length,
2430 split_box->text);
2431 NSLOG(layout, DEBUG,
2432 " new_box %p len: %" PRIsizet " \"%.*s\"",
2433 c2,
2434 c2->length,
2435 (int)c2->length,
2436 c2->text);
2437
2438 return true;
2439}
2440
2441
2442/**
2443 * Compute dimensions of box, margins, paddings, and borders for a floating
2444 * element using shrink-to-fit. Also used for inline-blocks.
2445 *
2446 * \param unit_len_ctx CSS length conversion context for document.
2447 * \param available_width Max width available in pixels
2448 * \param style Box's style
2449 * \param box Box for which to find dimensions
2450 * Box margins, borders, paddings, width and
2451 * height are updated.
2452 */
2453static void
2455 const css_unit_ctx *unit_len_ctx,
2456 int available_width,
2457 const css_computed_style *style,
2458 struct box *box)
2459{
2460 int width, height, max_width, min_width, max_height, min_height;
2461 int *margin = box->margin;
2462 int *padding = box->padding;
2463 struct box_border *border = box->border;
2464 enum css_overflow_e overflow_x = css_computed_overflow_x(style);
2465 enum css_overflow_e overflow_y = css_computed_overflow_y(style);
2466 int scrollbar_width_x =
2467 (overflow_x == CSS_OVERFLOW_SCROLL ||
2468 overflow_x == CSS_OVERFLOW_AUTO) ?
2469 SCROLLBAR_WIDTH : 0;
2470 int scrollbar_width_y =
2471 (overflow_y == CSS_OVERFLOW_SCROLL ||
2472 overflow_y == CSS_OVERFLOW_AUTO) ?
2473 SCROLLBAR_WIDTH : 0;
2474
2475 layout_find_dimensions(unit_len_ctx, available_width, -1, box, style,
2476 &width, &height, &max_width, &min_width,
2477 &max_height, &min_height, margin, padding, border);
2478
2479 if (margin[LEFT] == AUTO)
2480 margin[LEFT] = 0;
2481 if (margin[RIGHT] == AUTO)
2482 margin[RIGHT] = 0;
2483
2484 if (box->gadget == NULL) {
2485 padding[RIGHT] += scrollbar_width_y;
2486 padding[BOTTOM] += scrollbar_width_x;
2487 }
2488
2489 if (box->object && !(box->flags & REPLACE_DIM) &&
2491 /* Floating replaced element, with intrinsic width or height.
2492 * See 10.3.6 and 10.6.2 */
2494 min_width, max_width, min_height, max_height);
2495 } else if (box->gadget && (box->gadget->type == GADGET_TEXTBOX ||
2497 box->gadget->type == GADGET_FILE ||
2499 css_fixed size = 0;
2500 css_unit unit = CSS_UNIT_EM;
2501
2502 /* Give sensible dimensions to gadgets, with auto width/height,
2503 * that don't shrink to fit contained text. */
2504 assert(box->style);
2505
2506 if (box->gadget->type == GADGET_TEXTBOX ||
2508 box->gadget->type == GADGET_FILE) {
2509 if (width == AUTO) {
2510 size = INTTOFIX(10);
2511 width = FIXTOINT(css_unit_len2device_px(
2512 box->style, unit_len_ctx,
2513 size, unit));
2514 }
2515 if (box->gadget->type == GADGET_FILE &&
2516 height == AUTO) {
2517 size = FLTTOFIX(1.5);
2518 height = FIXTOINT(css_unit_len2device_px(
2519 box->style, unit_len_ctx,
2520 size, unit));
2521 }
2522 }
2523 if (box->gadget->type == GADGET_TEXTAREA) {
2524 if (width == AUTO) {
2525 size = INTTOFIX(10);
2526 width = FIXTOINT(css_unit_len2device_px(
2527 box->style, unit_len_ctx,
2528 size, unit));
2529 }
2530 if (height == AUTO) {
2531 size = INTTOFIX(4);
2532 height = FIXTOINT(css_unit_len2device_px(
2533 box->style, unit_len_ctx,
2534 size, unit));
2535 }
2536 }
2537 } else if (width == AUTO) {
2538 /* CSS 2.1 section 10.3.5 */
2539 width = min(max(box->min_width, available_width),
2540 box->max_width);
2541
2542 /* width includes margin, borders and padding */
2543 if (width == available_width) {
2544 width -= box->margin[LEFT] + box->border[LEFT].width +
2545 box->padding[LEFT] +
2546 box->padding[RIGHT] +
2547 box->border[RIGHT].width +
2548 box->margin[RIGHT];
2549 } else {
2550 /* width was obtained from a min_width or max_width
2551 * value, so need to use the same method for calculating
2552 * mbp as was used in layout_minmax_block() */
2553 int fixed = 0;
2554 float frac = 0;
2555 calculate_mbp_width(unit_len_ctx, box->style, LEFT,
2556 true, true, true, &fixed, &frac);
2557 calculate_mbp_width(unit_len_ctx, box->style, RIGHT,
2558 true, true, true, &fixed, &frac);
2559 if (fixed < 0)
2560 fixed = 0;
2561
2562 width -= fixed;
2563 }
2564
2565 if (max_width >= 0 && width > max_width) width = max_width;
2566 if (min_width > 0 && width < min_width) width = min_width;
2567
2568 } else {
2569 if (max_width >= 0 && width > max_width) width = max_width;
2570 if (min_width > 0 && width < min_width) width = min_width;
2571 width -= scrollbar_width_y;
2572 }
2573
2574 box->width = width;
2575 box->height = height;
2576
2577 if (margin[TOP] == AUTO)
2578 margin[TOP] = 0;
2579 if (margin[BOTTOM] == AUTO)
2580 margin[BOTTOM] = 0;
2581}
2582
2583
2584/**
2585 * Layout the contents of a float or inline block.
2586 *
2587 * \param b float or inline block box
2588 * \param width available width
2589 * \param content memory pool for any new boxes
2590 * \return true on success, false on memory exhaustion
2591 */
2592static bool layout_float(struct box *b, int width, html_content *content)
2593{
2594 assert(b->type == BOX_TABLE ||
2595 b->type == BOX_BLOCK ||
2596 b->type == BOX_INLINE_BLOCK ||
2597 b->type == BOX_FLEX ||
2598 b->type == BOX_INLINE_FLEX);
2599 layout_float_find_dimensions(&content->unit_len_ctx, width, b->style, b);
2600 if (b->type == BOX_TABLE || b->type == BOX_INLINE_FLEX) {
2601 if (b->type == BOX_TABLE) {
2602 if (!layout_table(b, width, content))
2603 return false;
2604 } else {
2605 if (!layout_flex(b, width, content))
2606 return false;
2607 }
2608 if (b->margin[LEFT] == AUTO)
2609 b->margin[LEFT] = 0;
2610 if (b->margin[RIGHT] == AUTO)
2611 b->margin[RIGHT] = 0;
2612 if (b->margin[TOP] == AUTO)
2613 b->margin[TOP] = 0;
2614 if (b->margin[BOTTOM] == AUTO)
2615 b->margin[BOTTOM] = 0;
2616 } else {
2617 return layout_block_context(b, -1, content);
2618 }
2619 return true;
2620}
2621
2622
2623/**
2624 * Position a float in the first available space.
2625 *
2626 * \param c float box to position
2627 * \param width available width
2628 * \param cx x coordinate relative to cont to place float right of
2629 * \param y y coordinate relative to cont to place float below
2630 * \param cont ancestor box which defines horizontal space, for floats
2631 */
2632static void
2633place_float_below(struct box *c, int width, int cx, int y, struct box *cont)
2634{
2635 int x0, x1, yy;
2636 struct box *left;
2637 struct box *right;
2638
2639 yy = y > cont->cached_place_below_level ?
2641
2642 NSLOG(layout, DEBUG,
2643 "c %p, width %i, cx %i, y %i, cont %p", c,
2644 width, cx, y, cont);
2645
2646 do {
2647 y = yy;
2648 x0 = cx;
2649 x1 = cx + width;
2650 find_sides(cont->float_children, y, y + c->height, &x0, &x1,
2651 &left, &right);
2652 if (left != 0 && right != 0) {
2653 yy = (left->y + left->height <
2654 right->y + right->height ?
2655 left->y + left->height :
2656 right->y + right->height);
2657 } else if (left == 0 && right != 0) {
2658 yy = right->y + right->height;
2659 } else if (left != 0 && right == 0) {
2660 yy = left->y + left->height;
2661 }
2662 } while ((left != 0 || right != 0) && (c->width > x1 - x0));
2663
2664 if (c->type == BOX_FLOAT_LEFT) {
2665 c->x = x0;
2666 } else {
2667 c->x = x1 - c->width;
2668 }
2669 c->y = y;
2671}
2672
2673
2674/**
2675 * Calculate line height from a style.
2676 */
2677static int line_height(
2678 const css_unit_ctx *unit_len_ctx,
2679 const css_computed_style *style)
2680{
2681 enum css_line_height_e lhtype;
2682 css_fixed lhvalue = 0;
2683 css_unit lhunit = CSS_UNIT_PX;
2684 css_fixed line_height;
2685
2686 assert(style);
2687
2688 lhtype = css_computed_line_height(style, &lhvalue, &lhunit);
2689 if (lhtype == CSS_LINE_HEIGHT_NORMAL) {
2690 /* Normal => use a constant of 1.3 * font-size */
2691 lhvalue = FLTTOFIX(1.3);
2692 lhtype = CSS_LINE_HEIGHT_NUMBER;
2693 }
2694
2695 if (lhtype == CSS_LINE_HEIGHT_NUMBER ||
2696 lhunit == CSS_UNIT_PCT) {
2697 line_height = css_unit_len2device_px(style, unit_len_ctx,
2698 lhvalue, CSS_UNIT_EM);
2699
2700 if (lhtype != CSS_LINE_HEIGHT_NUMBER)
2701 line_height = FDIV(line_height, F_100);
2702 } else {
2703 assert(lhunit != CSS_UNIT_PCT);
2704
2705 line_height = css_unit_len2device_px(style, unit_len_ctx,
2706 lhvalue, lhunit);
2707 }
2708
2709 return FIXTOINT(line_height);
2710}
2711
2712
2713/**
2714 * Position a line of boxes in inline formatting context.
2715 *
2716 * \param first box at start of line
2717 * \param width available width on input, updated with actual width on output
2718 * (may be incorrect if the line gets split?)
2719 * \param y coordinate of top of line, updated on exit to bottom
2720 * \param cx coordinate of left of line relative to cont
2721 * \param cy coordinate of top of line relative to cont
2722 * \param cont ancestor box which defines horizontal space, for floats
2723 * \param indent apply any first-line indent
2724 * \param has_text_children at least one TEXT in the inline_container
2725 * \param next_box updated to first box for next line, or 0 at end
2726 * \param content memory pool for any new boxes
2727 * \return true on success, false on memory exhaustion
2728 */
2729static bool
2730layout_line(struct box *first,
2731 int *width,
2732 int *y,
2733 int cx,
2734 int cy,
2735 struct box *cont,
2736 bool indent,
2737 bool has_text_children,
2739 struct box **next_box)
2740{
2741 int height, used_height;
2742 int x0 = 0;
2743 int x1 = *width;
2744 int x, h, x_previous;
2745 int fy = cy;
2746 struct box *left;
2747 struct box *right;
2748 struct box *b;
2749 struct box *split_box = 0;
2750 struct box *d;
2751 struct box *br_box = 0;
2752 bool move_y = false;
2753 bool place_below = false;
2754 int space_before = 0, space_after = 0;
2755 unsigned int inline_count = 0;
2756 unsigned int i;
2757 const struct gui_layout_table *font_func = content->font_func;
2758 plot_font_style_t fstyle;
2759
2760 NSLOG(layout, DEBUG,
2761 "first %p, first->text '%.*s', width %i, y %i, cx %i, cy %i",
2762 first,
2763 (int)first->length,
2764 first->text,
2765 *width,
2766 *y,
2767 cx,
2768 cy);
2769
2770 /* find sides at top of line */
2771 x0 += cx;
2772 x1 += cx;
2773 find_sides(cont->float_children, cy, cy, &x0, &x1, &left, &right);
2774 x0 -= cx;
2775 x1 -= cx;
2776
2777 if (indent)
2778 x0 += layout_text_indent(&content->unit_len_ctx,
2779 first->parent->parent->style, *width);
2780
2781 if (x1 < x0)
2782 x1 = x0;
2783
2784 /* get minimum line height from containing block.
2785 * this is the line-height if there are text children and also in the
2786 * case of an initially empty text input */
2787 if (has_text_children || first->parent->parent->gadget)
2788 used_height = height = line_height(&content->unit_len_ctx,
2789 first->parent->parent->style);
2790 else
2791 /* inline containers with no text are usually for layout and
2792 * look better with no minimum line-height */
2793 used_height = height = 0;
2794
2795 /* pass 1: find height of line assuming sides at top of line: loop
2796 * body executed at least once
2797 * keep in sync with the loop in layout_minmax_line() */
2798
2799 NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0);
2800
2801
2802 for (x = 0, b = first; x <= x1 - x0 && b != 0; b = b->next) {
2803 int min_width, max_width, min_height, max_height;
2804
2805 assert(lh__box_is_inline_content(b));
2806
2807 NSLOG(layout, DEBUG, "pass 1: b %p, x %i", b, x);
2808
2809 if (b->type == BOX_BR)
2810 break;
2811
2812 if (lh__box_is_float_box(b))
2813 continue;
2814 if (b->type == BOX_INLINE_BLOCK &&
2815 (css_computed_position(b->style) ==
2816 CSS_POSITION_ABSOLUTE ||
2817 css_computed_position(b->style) ==
2818 CSS_POSITION_FIXED))
2819 continue;
2820
2821 assert(b->style != NULL);
2822 font_plot_style_from_css(&content->unit_len_ctx, b->style, &fstyle);
2823
2824 x += space_after;
2825
2826 if (b->type == BOX_INLINE_BLOCK ||
2827 b->type == BOX_INLINE_FLEX) {
2828 if (b->max_width != UNKNOWN_WIDTH)
2829 if (!layout_float(b, *width, content))
2830 return false;
2831 h = b->border[TOP].width + b->padding[TOP] + b->height +
2832 b->padding[BOTTOM] +
2833 b->border[BOTTOM].width;
2834 if (height < h)
2835 height = h;
2836 x += b->margin[LEFT] + b->border[LEFT].width +
2837 b->padding[LEFT] + b->width +
2838 b->padding[RIGHT] +
2839 b->border[RIGHT].width +
2840 b->margin[RIGHT];
2841 space_after = 0;
2842 continue;
2843 }
2844
2845 if (b->type == BOX_INLINE) {
2846 /* calculate borders, margins, and padding */
2847 layout_find_dimensions(&content->unit_len_ctx,
2848 *width, -1, b, b->style, 0, 0, 0, 0,
2849 0, 0, b->margin, b->padding, b->border);
2850 for (i = 0; i != 4; i++)
2851 if (b->margin[i] == AUTO)
2852 b->margin[i] = 0;
2853 x += b->margin[LEFT] + b->border[LEFT].width +
2854 b->padding[LEFT];
2855 if (b->inline_end) {
2856 b->inline_end->margin[RIGHT] = b->margin[RIGHT];
2857 b->inline_end->padding[RIGHT] =
2858 b->padding[RIGHT];
2859 b->inline_end->border[RIGHT] =
2860 b->border[RIGHT];
2861 } else {
2862 x += b->padding[RIGHT] +
2863 b->border[RIGHT].width +
2864 b->margin[RIGHT];
2865 }
2866 } else if (b->type == BOX_INLINE_END) {
2867 b->width = 0;
2868 if (b->space == UNKNOWN_WIDTH) {
2869 font_func->width(&fstyle, " ", 1, &b->space);
2870 /** \todo handle errors */
2871 }
2872 space_after = b->space;
2873
2874 x += b->padding[RIGHT] + b->border[RIGHT].width +
2875 b->margin[RIGHT];
2876 continue;
2877 }
2878
2879 if (lh__box_is_replace(b) == false) {
2880 /* inline non-replaced, 10.3.1 and 10.6.1 */
2881 b->height = line_height(&content->unit_len_ctx,
2882 b->style ? b->style :
2883 b->parent->parent->style);
2884 if (height < b->height)
2885 height = b->height;
2886
2887 if (!b->text) {
2888 b->width = 0;
2889 space_after = 0;
2890 continue;
2891 }
2892
2893 if (b->width == UNKNOWN_WIDTH) {
2894 /** \todo handle errors */
2895
2896 /* If it's a select element, we must use the
2897 * width of the widest option text */
2898 if (b->parent->parent->gadget &&
2899 b->parent->parent->gadget->type
2900 == GADGET_SELECT) {
2901 int opt_maxwidth = 0;
2902 struct form_option *o;
2903
2904 for (o = b->parent->parent->gadget->
2905 data.select.items; o;
2906 o = o->next) {
2907 int opt_width;
2908 font_func->width(&fstyle,
2909 o->text,
2910 strlen(o->text),
2911 &opt_width);
2912
2913 if (opt_maxwidth < opt_width)
2914 opt_maxwidth =opt_width;
2915 }
2916 b->width = opt_maxwidth;
2917 if (nsoption_bool(core_select_menu))
2918 b->width += SCROLLBAR_WIDTH;
2919 } else {
2920 font_func->width(&fstyle, b->text,
2921 b->length, &b->width);
2922 b->flags |= MEASURED;
2923 }
2924 }
2925
2926 /* If the current text has not been measured (i.e. its
2927 * width was estimated after splitting), and it fits on
2928 * the line, measure it properly, so next box is placed
2929 * correctly. */
2930 if (b->text && (x + b->width < x1 - x0) &&
2931 !(b->flags & MEASURED) &&
2932 b->next) {
2933 font_func->width(&fstyle, b->text,
2934 b->length, &b->width);
2935 b->flags |= MEASURED;
2936 }
2937
2938 x += b->width;
2939 if (b->space == UNKNOWN_WIDTH) {
2940 font_func->width(&fstyle, " ", 1, &b->space);
2941 /** \todo handle errors */
2942 }
2943 space_after = b->space;
2944 continue;
2945 }
2946
2947 space_after = 0;
2948
2949 /* inline replaced, 10.3.2 and 10.6.2 */
2950 assert(b->style);
2951
2952 layout_find_dimensions(&content->unit_len_ctx,
2953 *width, -1, b, b->style,
2954 &b->width, &b->height,
2955 &max_width, &min_width,
2956 &max_height, &min_height,
2957 NULL, NULL, NULL);
2958
2959 if (b->object && !(b->flags & REPLACE_DIM)) {
2961 min_width, max_width,
2962 min_height, max_height);
2963 } else if (b->flags & IFRAME) {
2964 /* TODO: should we look at the content dimensions? */
2965 if (b->width == AUTO)
2966 b->width = 400;
2967 if (b->height == AUTO)
2968 b->height = 300;
2969
2970 /* We reformat the iframe browser window to new
2971 * dimensions in pass 2 */
2972 } else {
2973 /* form control with no object */
2974 if (b->width == AUTO)
2975 b->width = FIXTOINT(css_unit_len2device_px(
2976 b->style,
2977 &content->unit_len_ctx, INTTOFIX(1),
2978 CSS_UNIT_EM));
2979 if (b->height == AUTO)
2980 b->height = FIXTOINT(css_unit_len2device_px(
2981 b->style,
2982 &content->unit_len_ctx, INTTOFIX(1),
2983 CSS_UNIT_EM));
2984 }
2985
2986 /* Reformat object to new box size */
2987 if (b->object && content_can_reformat(b->object) &&
2988 b->width !=
2990 css_fixed value = 0;
2991 css_unit unit = CSS_UNIT_PX;
2992 enum css_height_e htype = css_computed_height(b->style,
2993 &value, &unit);
2994
2995 content_reformat(b->object, false, b->width, b->height);
2996
2997 if (htype == CSS_HEIGHT_AUTO)
2999 }
3000
3001 if (height < b->height)
3002 height = b->height;
3003
3004 x += b->width;
3005 }
3006
3007 /* find new sides using this height */
3008 x0 = cx;
3009 x1 = cx + *width;
3010 find_sides(cont->float_children, cy, cy + height, &x0, &x1,
3011 &left, &right);
3012 x0 -= cx;
3013 x1 -= cx;
3014
3015 if (indent)
3016 x0 += layout_text_indent(&content->unit_len_ctx,
3017 first->parent->parent->style, *width);
3018
3019 if (x1 < x0)
3020 x1 = x0;
3021
3022 space_after = space_before = 0;
3023
3024 /* pass 2: place boxes in line: loop body executed at least once */
3025
3026 NSLOG(layout, DEBUG, "x0 %i, x1 %i, x1 - x0 %i", x0, x1, x1 - x0);
3027
3028 for (x = x_previous = 0, b = first; x <= x1 - x0 && b; b = b->next) {
3029
3030 NSLOG(layout, DEBUG, "pass 2: b %p, x %i", b, x);
3031
3032 if (b->type == BOX_INLINE_BLOCK &&
3033 (css_computed_position(b->style) ==
3034 CSS_POSITION_ABSOLUTE ||
3035 css_computed_position(b->style) ==
3036 CSS_POSITION_FIXED)) {
3037 b->x = x + space_after;
3038
3039 } else if (lh__box_is_inline_flow(b)) {
3040 assert(b->width != UNKNOWN_WIDTH);
3041
3042 x_previous = x;
3043 x += space_after;
3044 b->x = x;
3045
3046 if ((b->type == BOX_INLINE && !b->inline_end) ||
3047 b->type == BOX_INLINE_BLOCK ||
3048 b->type == BOX_INLINE_FLEX) {
3049 b->x += b->margin[LEFT] + b->border[LEFT].width;
3050 x = b->x + b->padding[LEFT] + b->width +
3051 b->padding[RIGHT] +
3052 b->border[RIGHT].width +
3053 b->margin[RIGHT];
3054 } else if (b->type == BOX_INLINE) {
3055 b->x += b->margin[LEFT] + b->border[LEFT].width;
3056 x = b->x + b->padding[LEFT] + b->width;
3057 } else if (b->type == BOX_INLINE_END) {
3058 b->height = b->inline_end->height;
3059 x += b->padding[RIGHT] +
3060 b->border[RIGHT].width +
3061 b->margin[RIGHT];
3062 } else {
3063 x += b->width;
3064 }
3065
3066 space_before = space_after;
3067 if (b->object || b->flags & REPLACE_DIM ||
3068 b->flags & IFRAME)
3069 space_after = 0;
3070 else if (b->text || b->type == BOX_INLINE_END) {
3071 if (b->space == UNKNOWN_WIDTH) {
3073 &content->unit_len_ctx,
3074 b->style, &fstyle);
3075 /** \todo handle errors */
3076 font_func->width(&fstyle, " ", 1,
3077 &b->space);
3078 }
3079 space_after = b->space;
3080 } else {
3081 space_after = 0;
3082 }
3083 split_box = b;
3084 move_y = true;
3085 inline_count++;
3086 } else if (b->type == BOX_BR) {
3087 b->x = x;
3088 b->width = 0;
3089 br_box = b;
3090 b = b->next;
3091 split_box = 0;
3092 move_y = true;
3093 break;
3094
3095 } else {
3096 /* float */
3097 NSLOG(layout, DEBUG, "float %p", b);
3098
3099 d = b->children;
3100 d->float_children = 0;
3102 b->float_container = d->float_container = cont;
3103
3104 if (!layout_float(d, *width, content))
3105 return false;
3106
3107 NSLOG(layout, DEBUG,
3108 "%p : %d %d",
3109 d,
3110 d->margin[TOP],
3111 d->border[TOP].width);
3112
3113 d->x = d->margin[LEFT] + d->border[LEFT].width;
3114 d->y = d->margin[TOP] + d->border[TOP].width;
3115 b->width = d->margin[LEFT] + d->border[LEFT].width +
3116 d->padding[LEFT] + d->width +
3117 d->padding[RIGHT] +
3118 d->border[RIGHT].width +
3119 d->margin[RIGHT];
3120 b->height = d->margin[TOP] + d->border[TOP].width +
3121 d->padding[TOP] + d->height +
3122 d->padding[BOTTOM] +
3123 d->border[BOTTOM].width +
3124 d->margin[BOTTOM];
3125
3126 if (b->width > (x1 - x0) - x)
3127 place_below = true;
3128 if (d->style && (css_computed_clear(d->style) ==
3129 CSS_CLEAR_NONE ||
3130 (css_computed_clear(d->style) ==
3131 CSS_CLEAR_LEFT && left == 0) ||
3132 (css_computed_clear(d->style) ==
3133 CSS_CLEAR_RIGHT &&
3134 right == 0) ||
3135 (css_computed_clear(d->style) ==
3136 CSS_CLEAR_BOTH &&
3137 left == 0 && right == 0)) &&
3138 (!place_below ||
3139 (left == 0 && right == 0 && x == 0)) &&
3140 cy >= cont->clear_level &&
3141 cy >= cont->cached_place_below_level) {
3142 /* + not cleared or,
3143 * cleared and there are no floats to clear
3144 * + fits without needing to be placed below or,
3145 * this line is empty with no floats
3146 * + current y, cy, is below the clear level
3147 *
3148 * Float affects current line */
3149 if (b->type == BOX_FLOAT_LEFT) {
3150 b->x = cx + x0;
3151 if (b->width > 0)
3152 x0 += b->width;
3153 left = b;
3154 } else {
3155 b->x = cx + x1 - b->width;
3156 if (b->width > 0)
3157 x1 -= b->width;
3158 right = b;
3159 }
3160 b->y = cy;
3161 } else {
3162 /* cleared or doesn't fit on line */
3163 /* place below into next available space */
3164 int fcy = (cy > cont->clear_level) ? cy :
3165 cont->clear_level;
3166 fcy = (fcy > cont->cached_place_below_level) ?
3167 fcy :
3169 fy = (fy > fcy) ? fy : fcy;
3170 fy = (fy == cy) ? fy + height : fy;
3171
3172 place_float_below(b, *width, cx, fy, cont);
3173 fy = b->y;
3174 if (d->style && (
3175 (css_computed_clear(d->style) ==
3176 CSS_CLEAR_LEFT && left != 0) ||
3177 (css_computed_clear(d->style) ==
3178 CSS_CLEAR_RIGHT &&
3179 right != 0) ||
3180 (css_computed_clear(d->style) ==
3181 CSS_CLEAR_BOTH &&
3182 (left != 0 || right != 0)))) {
3183 /* to be cleared below existing
3184 * floats */
3185 if (b->type == BOX_FLOAT_LEFT)
3186 b->x = cx;
3187 else
3188 b->x = cx + *width - b->width;
3189
3190 fcy = layout_clear(cont->float_children,
3191 css_computed_clear(d->style));
3192 if (fcy > cont->clear_level)
3193 cont->clear_level = fcy;
3194 if (b->y < fcy)
3195 b->y = fcy;
3196 }
3197 if (b->type == BOX_FLOAT_LEFT)
3198 left = b;
3199 else
3200 right = b;
3201 }
3202 add_float_to_container(cont, b);
3203
3204 split_box = 0;
3205 }
3206 }
3207
3208 if (x1 - x0 < x && split_box) {
3209 /* the last box went over the end */
3210 size_t split = 0;
3211 int w;
3212 bool no_wrap = css_computed_white_space(
3213 split_box->style) == CSS_WHITE_SPACE_NOWRAP ||
3214 css_computed_white_space(
3215 split_box->style) == CSS_WHITE_SPACE_PRE;
3216
3217 x = x_previous;
3218
3219 if (!no_wrap &&
3220 (split_box->type == BOX_INLINE ||
3221 split_box->type == BOX_TEXT) &&
3222 !split_box->object &&
3223 !(split_box->flags & REPLACE_DIM) &&
3224 !(split_box->flags & IFRAME) &&
3225 !split_box->gadget && split_box->text) {
3226
3227 font_plot_style_from_css(&content->unit_len_ctx,
3228 split_box->style, &fstyle);
3229 /** \todo handle errors */
3230 font_func->split(&fstyle,
3231 split_box->text,
3232 split_box->length,
3233 x1 - x0 - x - space_before,
3234 &split,
3235 &w);
3236 }
3237
3238 /* split == 0 implies that text can't be split */
3239
3240 if (split == 0)
3241 w = split_box->width;
3242
3243
3244 NSLOG(layout, DEBUG,
3245 "splitting: split_box %p \"%.*s\", spilt %"PRIsizet
3246 ", w %i, left %p, right %p, inline_count %u",
3247 split_box,
3248 (int)split_box->length,
3249 split_box->text,
3250 split,
3251 w,
3252 left,
3253 right,
3254 inline_count);
3255
3256 if ((split == 0 || x1 - x0 <= x + space_before + w) &&
3257 !left && !right && inline_count == 1) {
3258 /* first word of box doesn't fit, but no floats and
3259 * first box on line so force in */
3260 if (split == 0 || split == split_box->length) {
3261 /* only one word in this box, or not text
3262 * or white-space:nowrap */
3263 b = split_box->next;
3264 } else {
3265 /* cut off first word for this line */
3266 if (!layout_text_box_split(content, &fstyle,
3267 split_box, split, w))
3268 return false;
3269 b = split_box->next;
3270 }
3271 x += space_before + w;
3272
3273 NSLOG(layout, DEBUG, "forcing");
3274
3275 } else if ((split == 0 || x1 - x0 <= x + space_before + w) &&
3276 inline_count == 1) {
3277 /* first word of first box doesn't fit, but a float is
3278 * taking some of the width so move below it */
3279 assert(left || right);
3280 used_height = 0;
3281 if (left) {
3282
3283 NSLOG(layout, DEBUG,
3284 "cy %i, left->y %i, left->height %i",
3285 cy,
3286 left->y,
3287 left->height);
3288
3289 used_height = left->y + left->height - cy + 1;
3290
3291 NSLOG(layout, DEBUG, "used_height %i",
3292 used_height);
3293
3294 }
3295 if (right && used_height <
3296 right->y + right->height - cy + 1)
3297 used_height = right->y + right->height - cy + 1;
3298
3299 if (used_height < 0)
3300 used_height = 0;
3301
3302 b = split_box;
3303
3304 NSLOG(layout, DEBUG, "moving below float");
3305
3306 } else if (split == 0 || x1 - x0 <= x + space_before + w) {
3307 /* first word of box doesn't fit so leave box for next
3308 * line */
3309 b = split_box;
3310
3311 NSLOG(layout, DEBUG, "leaving for next line");
3312
3313 } else {
3314 /* fit as many words as possible */
3315 assert(split != 0);
3316
3317 NSLOG(layout, DEBUG,
3318 "'%.*s' %i %"PRIsizet" %i",
3319 (int)split_box->length, split_box->text,
3320 x1 - x0, split, w);
3321
3322 if (split != split_box->length) {
3323 if (!layout_text_box_split(content, &fstyle,
3324 split_box, split, w))
3325 return false;
3326 b = split_box->next;
3327 }
3328 x += space_before + w;
3329
3330 NSLOG(layout, DEBUG, "fitting words");
3331
3332 }
3333 move_y = true;
3334 }
3335
3336 /* set positions */
3337 switch (css_computed_text_align(first->parent->parent->style)) {
3338 case CSS_TEXT_ALIGN_RIGHT:
3339 case CSS_TEXT_ALIGN_LIBCSS_RIGHT:
3340 x0 = x1 - x;
3341 break;
3342 case CSS_TEXT_ALIGN_CENTER:
3343 case CSS_TEXT_ALIGN_LIBCSS_CENTER:
3344 x0 = (x0 + (x1 - x)) / 2;
3345 break;
3346 case CSS_TEXT_ALIGN_LEFT:
3347 case CSS_TEXT_ALIGN_LIBCSS_LEFT:
3348 case CSS_TEXT_ALIGN_JUSTIFY:
3349 /* leave on left */
3350 break;
3351 case CSS_TEXT_ALIGN_DEFAULT:
3352 /* None; consider text direction */
3353 switch (css_computed_direction(first->parent->parent->style)) {
3354 case CSS_DIRECTION_LTR:
3355 /* leave on left */
3356 break;
3357 case CSS_DIRECTION_RTL:
3358 x0 = x1 - x;
3359 break;
3360 }
3361 break;
3362 }
3363
3364 for (d = first; d != b; d = d->next) {
3365 d->flags &= ~NEW_LINE;
3366
3367 if (d->type == BOX_INLINE_BLOCK &&
3368 (css_computed_position(d->style) ==
3369 CSS_POSITION_ABSOLUTE ||
3370 css_computed_position(d->style) ==
3371 CSS_POSITION_FIXED)) {
3372 /* positioned inline-blocks:
3373 * set static position (x,y) only, rest of positioning
3374 * is handled later */
3375 d->x += x0;
3376 d->y = *y;
3377 continue;
3378 } else if ((d->type == BOX_INLINE &&
3379 lh__box_is_replace(d) == false) ||
3380 d->type == BOX_BR ||
3381 d->type == BOX_TEXT ||
3382 d->type == BOX_INLINE_END) {
3383 /* regular (non-replaced) inlines */
3384 d->x += x0;
3385 d->y = *y - d->padding[TOP];
3386
3387 if (d->type == BOX_TEXT && d->height > used_height) {
3388 /* text */
3389 used_height = d->height;
3390 }
3391 } else if ((d->type == BOX_INLINE) ||
3392 d->type == BOX_INLINE_BLOCK) {
3393 /* replaced inlines and inline-blocks */
3394 d->x += x0;
3395 d->y = *y + d->border[TOP].width + d->margin[TOP];
3396 h = d->margin[TOP] + d->border[TOP].width +
3397 d->padding[TOP] + d->height +
3398 d->padding[BOTTOM] +
3399 d->border[BOTTOM].width +
3400 d->margin[BOTTOM];
3401 if (used_height < h)
3402 used_height = h;
3403 }
3404 }
3405
3406 first->flags |= NEW_LINE;
3407
3408 assert(b != first || (move_y && 0 < used_height && (left || right)));
3409
3410 /* handle vertical-align by adjusting box y values */
3411 /** \todo proper vertical alignment handling */
3412 for (d = first; d != b; d = d->next) {
3413 if ((d->type == BOX_INLINE && d->inline_end) ||
3414 d->type == BOX_BR ||
3415 d->type == BOX_TEXT ||
3416 d->type == BOX_INLINE_END) {
3417 css_fixed value = 0;
3418 css_unit unit = CSS_UNIT_PX;
3419 switch (css_computed_vertical_align(d->style, &value,
3420 &unit)) {
3421 case CSS_VERTICAL_ALIGN_SUPER:
3422 case CSS_VERTICAL_ALIGN_TOP:
3423 case CSS_VERTICAL_ALIGN_TEXT_TOP:
3424 /* already at top */
3425 break;
3426 case CSS_VERTICAL_ALIGN_SUB:
3427 case CSS_VERTICAL_ALIGN_BOTTOM:
3428 case CSS_VERTICAL_ALIGN_TEXT_BOTTOM:
3429 d->y += used_height - d->height;
3430 break;
3431 default:
3432 case CSS_VERTICAL_ALIGN_BASELINE:
3433 d->y += 0.75 * (used_height - d->height);
3434 break;
3435 }
3436 }
3437 }
3438
3439 /* handle clearance for br */
3440 if (br_box && css_computed_clear(br_box->style) != CSS_CLEAR_NONE) {
3441 int clear_y = layout_clear(cont->float_children,
3442 css_computed_clear(br_box->style));
3443 if (used_height < clear_y - cy)
3444 used_height = clear_y - cy;
3445 }
3446
3447 if (move_y)
3448 *y += used_height;
3449 *next_box = b;
3450 *width = x; /* return actual width */
3451 return true;
3452}
3453
3454
3455/**
3456 * Layout lines of text or inline boxes with floats.
3457 *
3458 * \param box inline container box
3459 * \param width horizontal space available
3460 * \param cont ancestor box which defines horizontal space, for floats
3461 * \param cx box position relative to cont
3462 * \param cy box position relative to cont
3463 * \param content memory pool for any new boxes
3464 * \return true on success, false on memory exhaustion
3465 */
3466static bool layout_inline_container(struct box *inline_container, int width,
3467 struct box *cont, int cx, int cy, html_content *content)
3468{
3469 bool first_line = true;
3470 bool has_text_children;
3471 struct box *c, *next;
3472 int y = 0;
3473 int curwidth,maxwidth = width;
3474
3475 assert(inline_container->type == BOX_INLINE_CONTAINER);
3476
3477 NSLOG(layout, DEBUG,
3478 "inline_container %p, width %i, cont %p, cx %i, cy %i",
3479 inline_container,
3480 width,
3481 cont,
3482 cx,
3483 cy);
3484
3485
3486 has_text_children = false;
3487 for (c = inline_container->children; c; c = c->next) {
3488 bool is_pre = false;
3489
3490 if (c->style) {
3491 enum css_white_space_e whitespace;
3492
3493 whitespace = css_computed_white_space(c->style);
3494
3495 is_pre = (whitespace == CSS_WHITE_SPACE_PRE ||
3496 whitespace == CSS_WHITE_SPACE_PRE_LINE ||
3497 whitespace == CSS_WHITE_SPACE_PRE_WRAP);
3498 }
3499
3500 if ((lh__box_is_object(c) == false &&
3501 c->text && (c->length || is_pre)) ||
3502 c->type == BOX_BR)
3503 has_text_children = true;
3504 }
3505
3506 /** \todo fix wrapping so that a box with horizontal scrollbar will
3507 * shrink back to 'width' if no word is wider than 'width' (Or just set
3508 * curwidth = width and have the multiword lines wrap to the min width)
3509 */
3510 for (c = inline_container->children; c; ) {
3511
3512 NSLOG(layout, DEBUG, "c %p", c);
3513
3514 curwidth = inline_container->width;
3515 if (!layout_line(c, &curwidth, &y, cx, cy + y, cont, first_line,
3516 has_text_children, content, &next))
3517 return false;
3518 maxwidth = max(maxwidth,curwidth);
3519 c = next;
3520 first_line = false;
3521 }
3522
3523 inline_container->width = maxwidth;
3524 inline_container->height = y;
3525
3526 return true;
3527}
3528
3529
3530/* Documented in layout_intertnal.h */
3532 struct box *block,
3533 int viewport_height,
3535{
3536 struct box *box;
3537 int cx, cy; /**< current coordinates */
3538 int max_pos_margin = 0;
3539 int max_neg_margin = 0;
3540 int y = 0;
3541 int lm, rm;
3542 struct box *margin_collapse = NULL;
3543 bool in_margin = false;
3544 css_fixed gadget_size;
3545 css_unit gadget_unit; /* Checkbox / radio buttons */
3546
3547 assert(block->type == BOX_BLOCK ||
3548 block->type == BOX_INLINE_BLOCK ||
3549 block->type == BOX_TABLE_CELL ||
3550 block->type == BOX_FLEX ||
3551 block->type == BOX_INLINE_FLEX);
3552 assert(block->width != UNKNOWN_WIDTH);
3553 assert(block->width != AUTO);
3554
3555 block->float_children = NULL;
3556 block->cached_place_below_level = 0;
3557 block->clear_level = 0;
3558
3559 /* special case if the block contains an object */
3560 if (block->object) {
3561 int temp_width = block->width;
3562 if (!layout_block_object(block))
3563 return false;
3564 layout_get_object_dimensions(block, &temp_width,
3565 &block->height, INT_MIN, INT_MAX,
3566 INT_MIN, INT_MAX);
3567 return true;
3568 } else if (block->flags & REPLACE_DIM) {
3569 return true;
3570 }
3571
3572 /* special case if the block contains an radio button or checkbox */
3573 if (block->gadget && (block->gadget->type == GADGET_RADIO ||
3574 block->gadget->type == GADGET_CHECKBOX)) {
3575 /* form checkbox or radio button
3576 * if width or height is AUTO, set it to 1em */
3577 gadget_unit = CSS_UNIT_EM;
3578 gadget_size = INTTOFIX(1);
3579 if (block->height == AUTO)
3580 block->height = FIXTOINT(css_unit_len2device_px(
3581 block->style,
3582 &content->unit_len_ctx,
3583 gadget_size, gadget_unit));
3584 }
3585
3586 box = block->children;
3587 /* set current coordinates to top-left of the block */
3588 cx = 0;
3589 y = cy = block->padding[TOP];
3590 if (box)
3591 box->y = block->padding[TOP];
3592
3593 /* Step through the descendants of the block in depth-first order, but
3594 * not into the children of boxes which aren't blocks. For example, if
3595 * the tree passed to this function looks like this (box->type shown):
3596 *
3597 * block -> BOX_BLOCK
3598 * BOX_BLOCK * (1)
3599 * BOX_INLINE_CONTAINER * (2)
3600 * BOX_INLINE
3601 * BOX_TEXT
3602 * ...
3603 * BOX_BLOCK * (3)
3604 * BOX_TABLE * (4)
3605 * BOX_TABLE_ROW
3606 * BOX_TABLE_CELL
3607 * ...
3608 * BOX_TABLE_CELL
3609 * ...
3610 * BOX_BLOCK * (5)
3611 * BOX_INLINE_CONTAINER * (6)
3612 * BOX_TEXT
3613 * ...
3614 * then the while loop will visit each box marked with *, setting box
3615 * to each in the order shown. */
3616 while (box) {
3617 enum css_overflow_e overflow_x = CSS_OVERFLOW_VISIBLE;
3618 enum css_overflow_e overflow_y = CSS_OVERFLOW_VISIBLE;
3619
3620 assert(box->type == BOX_BLOCK ||
3621 box->type == BOX_FLEX ||
3622 box->type == BOX_TABLE ||
3624
3625 /* Tables are laid out before being positioned, because the
3626 * position depends on the width which is calculated in
3627 * table layout. Blocks and inline containers are positioned
3628 * before being laid out, because width is not dependent on
3629 * content, and the position is required during layout for
3630 * correct handling of floats.
3631 */
3632
3633 if (box->style &&
3634 (css_computed_position(box->style) ==
3635 CSS_POSITION_ABSOLUTE ||
3636 css_computed_position(box->style) ==
3637 CSS_POSITION_FIXED)) {
3638 box->x = box->parent->padding[LEFT];
3639 /* absolute positioned; this element will establish
3640 * its own block context when it gets laid out later,
3641 * so no need to look at its children now. */
3642 goto advance_to_next_box;
3643 }
3644
3645 /* If we don't know which box the current margin collapses
3646 * through to, find out. Update the pos/neg margin values. */
3647 if (margin_collapse == NULL) {
3648 margin_collapse = layout_next_margin_block(
3649 &content->unit_len_ctx, box, block,
3650 viewport_height,
3651 &max_pos_margin, &max_neg_margin);
3652 /* We have a margin that has not yet been applied. */
3653 in_margin = true;
3654 }
3655
3656 /* Clearance. */
3657 y = 0;
3658 if (box->style && css_computed_clear(box->style) !=
3659 CSS_CLEAR_NONE)
3660 y = layout_clear(block->float_children,
3661 css_computed_clear(box->style));
3662
3663 /* Find box's overflow properties */
3664 if (box->style) {
3665 overflow_x = css_computed_overflow_x(box->style);
3666 overflow_y = css_computed_overflow_y(box->style);
3667 }
3668
3669 /* Blocks establishing a block formatting context get minimum
3670 * left and right margins to avoid any floats. */
3671 lm = rm = 0;
3672
3673 if (box->type == BOX_FLEX ||
3674 box->type == BOX_BLOCK ||
3675 box->flags & IFRAME) {
3676 if (lh__box_is_object(box) == false &&
3677 box->style &&
3678 (overflow_x != CSS_OVERFLOW_VISIBLE ||
3679 overflow_y != CSS_OVERFLOW_VISIBLE)) {
3680 /* box establishes new block formatting context
3681 * so available width may be diminished due to
3682 * floats. */
3683 int x0, x1, top;
3684 struct box *left, *right;
3685 top = cy + max_pos_margin - max_neg_margin;
3686 top = (top > y) ? top : y;
3687 x0 = cx;
3688 x1 = cx + box->parent->width -
3689 box->parent->padding[LEFT] -
3691 find_sides(block->float_children, top, top,
3692 &x0, &x1, &left, &right);
3693 /* calculate min required left & right margins
3694 * needed to avoid floats */
3695 lm = x0 - cx;
3696 rm = cx + box->parent->width -
3697 box->parent->padding[LEFT] -
3698 box->parent->padding[RIGHT] -
3699 x1;
3700 }
3702 box->parent->width,
3703 viewport_height, lm, rm, box);
3704 if (box->type == BOX_BLOCK && !(box->flags & IFRAME)) {
3707 }
3708 } else if (box->type == BOX_TABLE) {
3709 if (box->style != NULL) {
3710 enum css_width_e wtype;
3711 css_fixed width = 0;
3712 css_unit unit = CSS_UNIT_PX;
3713
3714 wtype = css_computed_width(box->style, &width,
3715 &unit);
3716
3717 if (wtype == CSS_WIDTH_AUTO) {
3718 /* max available width may be
3719 * diminished due to floats. */
3720 int x0, x1, top;
3721 struct box *left, *right;
3722 top = cy + max_pos_margin -
3723 max_neg_margin;
3724 top = (top > y) ? top : y;
3725 x0 = cx;
3726 x1 = cx + box->parent->width -
3727 box->parent->padding[LEFT] -
3730 top, top, &x0, &x1,
3731 &left, &right);
3732 /* calculate min required left & right
3733 * margins needed to avoid floats */
3734 lm = x0 - cx;
3735 rm = cx + box->parent->width -
3736 box->parent->padding[LEFT] -
3737 box->parent->padding[RIGHT] -
3738 x1;
3739 }
3740 }
3741 if (!layout_table(box, box->parent->width - lm - rm,
3742 content))
3743 return false;
3745 lm, rm, -1, -1);
3746 }
3747
3748 /* Position box: horizontal. */
3749 box->x = box->parent->padding[LEFT] + box->margin[LEFT] +
3750 box->border[LEFT].width;
3751 cx += box->x;
3752
3753 /* Position box: vertical. */
3754 if (box->border[TOP].width) {
3755 box->y += box->border[TOP].width;
3756 cy += box->border[TOP].width;
3757 }
3758
3759 /* Vertical margin */
3760 if (((box->type == BOX_BLOCK && (box->flags & HAS_HEIGHT)) ||
3761 box->type == BOX_FLEX ||
3762 box->type == BOX_TABLE ||
3765 margin_collapse == box) &&
3766 in_margin == true) {
3767 /* Margin goes above this box. */
3768 cy += max_pos_margin - max_neg_margin;
3769 box->y += max_pos_margin - max_neg_margin;
3770
3771 /* Current margin has been applied. */
3772 in_margin = false;
3773 max_pos_margin = max_neg_margin = 0;
3774 }
3775
3776 /* Handle clearance */
3777 if (box->type != BOX_INLINE_CONTAINER &&
3778 (y > 0) && (cy < y)) {
3779 /* box clears something*/
3780 box->y += y - cy;
3781 cy = y;
3782 }
3783
3784 /* Unless the box has an overflow style of visible, the box
3785 * establishes a new block context. */
3786 if (box->type == BOX_FLEX ||
3787 (box->type == BOX_BLOCK && box->style &&
3788 (overflow_x != CSS_OVERFLOW_VISIBLE ||
3789 overflow_y != CSS_OVERFLOW_VISIBLE))) {
3790
3791 if (box->type == BOX_FLEX) {
3792 if (!layout_flex(box, box->width, content)) {
3793 return false;
3794 }
3795 } else {
3797 viewport_height, content);
3798 }
3799
3800 cy += box->padding[TOP];
3801
3802 if (box->height == AUTO) {
3803 box->height = 0;
3805 }
3806
3807 cx -= box->x;
3808 cy += box->height + box->padding[BOTTOM] +
3810 y = box->y + box->padding[TOP] + box->height +
3811 box->padding[BOTTOM] +
3813
3814 /* Skip children, because they are done in the new
3815 * block context */
3816 goto advance_to_next_box;
3817 }
3818
3819 NSLOG(layout, DEBUG, "box %p, cx %i, cy %i, width %i",
3820 box, cx, cy, box->width);
3821
3822 /* Layout (except tables). */
3823 if (box->object) {
3825 return false;
3826
3827 } else if (box->type == BOX_INLINE_CONTAINER) {
3828 box->width = box->parent->width;
3829 if (!layout_inline_container(box, box->width, block,
3830 cx, cy, content))
3831 return false;
3832
3833 } else if (box->type == BOX_TABLE) {
3834 /* Move down to avoid floats if necessary. */
3835 int x0, x1;
3836 struct box *left, *right;
3837 y = cy;
3838 while (1) {
3839 enum css_width_e wtype;
3840 css_fixed width = 0;
3841 css_unit unit = CSS_UNIT_PX;
3842
3843 wtype = css_computed_width(box->style,
3844 &width, &unit);
3845
3846 x0 = cx;
3847 x1 = cx + box->parent->width;
3848 find_sides(block->float_children, y,
3849 y + box->height,
3850 &x0, &x1, &left, &right);
3851 if (wtype == CSS_WIDTH_AUTO)
3852 break;
3853 if (box->width <= x1 - x0)
3854 break;
3855 if (!left && !right)
3856 break;
3857 else if (!left)
3858 y = right->y + right->height + 1;
3859 else if (!right)
3860 y = left->y + left->height + 1;
3861 else if (left->y + left->height <
3862 right->y + right->height)
3863 y = left->y + left->height + 1;
3864 else
3865 y = right->y + right->height + 1;
3866 }
3867 box->x += x0 - cx;
3868 cx = x0;
3869 box->y += y - cy;
3870 cy = y;
3871 }
3872
3873 /* Advance to next box. */
3874 if (box->type == BOX_BLOCK && !box->object && !(box->iframe) &&
3875 box->children) {
3876 /* Down into children. */
3877
3878 if (box == margin_collapse) {
3879 /* Current margin collapsed though to this box.
3880 * Unset margin_collapse. */
3881 margin_collapse = NULL;
3882 }
3883
3884 y = box->padding[TOP];
3885 box = box->children;
3886 box->y = y;
3887 cy += y;
3888 continue;
3889 } else if (box->type == BOX_BLOCK || box->object ||
3890 box->flags & IFRAME)
3891 cy += box->padding[TOP];
3892
3893 if (box->type == BOX_BLOCK && box->height == AUTO) {
3894 box->height = 0;
3896 }
3897
3898 cy += box->height + box->padding[BOTTOM] +
3900 cx -= box->x;
3901 y = box->y + box->padding[TOP] + box->height +
3902 box->padding[BOTTOM] +
3904
3905 advance_to_next_box:
3906 if (!box->next) {
3907 /* No more siblings:
3908 * up to first ancestor with a sibling. */
3909
3910 do {
3911 if (box == margin_collapse) {
3912 /* Current margin collapsed though to
3913 * this box. Unset margin_collapse. */
3914 margin_collapse = NULL;
3915 }
3916
3917 /* Apply bottom margin */
3918 if (max_pos_margin < box->margin[BOTTOM])
3919 max_pos_margin = box->margin[BOTTOM];
3920 else if (max_neg_margin < -box->margin[BOTTOM])
3921 max_neg_margin = -box->margin[BOTTOM];
3922
3923 box = box->parent;
3924 if (box == block)
3925 break;
3926
3927 /* Margin is invalidated if this is a box
3928 * margins can't collapse through. */
3929 if (box->type == BOX_BLOCK &&
3930 box->flags & MAKE_HEIGHT) {
3931 margin_collapse = NULL;
3932 in_margin = false;
3933 max_pos_margin = max_neg_margin = 0;
3934 }
3935
3936 if (box->height == AUTO) {
3937 box->height = y - box->padding[TOP];
3938
3939 if (box->type == BOX_BLOCK)
3941 BOTTOM);
3942 } else
3943 cy += box->height -
3944 (y - box->padding[TOP]);
3945
3946 /* Apply any min-height and max-height to
3947 * boxes in normal flow */
3948 if (box->style &&
3949 css_computed_position(box->style) !=
3950 CSS_POSITION_ABSOLUTE &&
3952 &content->unit_len_ctx,
3953 box, NULL)) {
3954 /* Height altered */
3955 /* Set current cy */
3956 cy += box->height -
3957 (y - box->padding[TOP]);
3958 }
3959
3960 cy += box->padding[BOTTOM] +
3962 cx -= box->x;
3963 y = box->y + box->padding[TOP] + box->height +
3964 box->padding[BOTTOM] +
3966
3967 } while (box->next == NULL);
3968 if (box == block)
3969 break;
3970 }
3971
3972 /* To next sibling. */
3973
3974 if (box == margin_collapse) {
3975 /* Current margin collapsed though to this box.
3976 * Unset margin_collapse. */
3977 margin_collapse = NULL;
3978 }
3979
3980 if (max_pos_margin < box->margin[BOTTOM])
3981 max_pos_margin = box->margin[BOTTOM];
3982 else if (max_neg_margin < -box->margin[BOTTOM])
3983 max_neg_margin = -box->margin[BOTTOM];
3984
3985 box = box->next;
3986 box->y = y;
3987 }
3988
3989 /* Account for bottom margin of last contained block */
3990 cy += max_pos_margin - max_neg_margin;
3991
3992 /* Increase height to contain any floats inside (CSS 2.1 10.6.7). */
3993 for (box = block->float_children; box; box = box->next_float) {
3994 y = box->y + box->height + box->padding[BOTTOM] +
3996 if (cy < y)
3997 cy = y;
3998 }
3999
4000 if (block->height == AUTO) {
4001 block->height = cy - block->padding[TOP];
4002 if (block->type == BOX_BLOCK)
4004 }
4005
4006 if (block->style && css_computed_position(block->style) !=
4007 CSS_POSITION_ABSOLUTE) {
4008 /* Block is in normal flow */
4009 layout_apply_minmax_height(&content->unit_len_ctx, block, NULL);
4010 }
4011
4012 if (block->gadget &&
4013 (block->gadget->type == GADGET_TEXTAREA ||
4014 block->gadget->type == GADGET_PASSWORD ||
4015 block->gadget->type == GADGET_TEXTBOX)) {
4016 plot_font_style_t fstyle;
4017 int ta_width = block->padding[LEFT] + block->width +
4018 block->padding[RIGHT];
4019 int ta_height = block->padding[TOP] + block->height +
4020 block->padding[BOTTOM];
4021 font_plot_style_from_css(&content->unit_len_ctx,
4022 block->style, &fstyle);
4023 fstyle.background = NS_TRANSPARENT;
4024 textarea_set_layout(block->gadget->data.text.ta,
4025 &fstyle, ta_width, ta_height,
4026 block->padding[TOP], block->padding[RIGHT],
4027 block->padding[BOTTOM], block->padding[LEFT]);
4028 }
4029
4030 return true;
4031}
4032
4033/**
4034 * Get a dom node's element tag type.
4035 *
4036 * \param[in] node Node to get tag type of.
4037 * \param[in] type Returns element tag type on success.
4038 * \return true if on success, false otherwise.
4039 */
4040static bool
4042 const dom_node *node,
4043 dom_html_element_type *type)
4044{
4045 dom_html_element_type element_type;
4046 dom_node_type node_type;
4047 dom_exception exc;
4048
4049 exc = dom_node_get_node_type(node, &node_type);
4050 if (exc != DOM_NO_ERR ||
4051 node_type != DOM_ELEMENT_NODE) {
4052 return false;
4053 }
4054
4055 exc = dom_html_element_get_tag_type(node, &element_type);
4056 if (exc != DOM_NO_ERR) {
4057 return false;
4058 }
4059
4060 *type = element_type;
4061 return true;
4062}
4063
4064
4065/**
4066 * Check a node's tag type.
4067 *
4068 * \param[in] node Node to check tag type of.
4069 * \param[in] type Tag type to test for.
4070 * \return true if if node has given type, false otherwise.
4071 */
4072static inline bool
4074 const dom_node *node,
4075 dom_html_element_type type)
4076{
4077 dom_html_element_type element_type;
4078
4079 if (!layout__get_element_tag(node, &element_type)) {
4080 return false;
4081 }
4082
4083 return element_type == type;
4084}
4085
4086
4087/**
4088 * Helper to get attribute value from a LI node.
4089 *
4090 * \param[in] li_node DOM node for the LI element;
4091 * \param[out] value_out Returns the value on success.
4092 * \return true if node has value, otherwise false.
4093 */
4094static bool
4095layout__get_li_value(dom_node *li_node, dom_long *value_out)
4096{
4097 dom_exception exc;
4098 dom_long value;
4099 bool has_value;
4100
4101 /** \todo
4102 * dom_html_li_element_get_value() is rubbish and we can't tell
4103 * a lack of value attribute or invalid value from a perfectly
4104 * valid '-1'.
4105 *
4106 * This helps for the common case of no value. However we should
4107 * fix libdom to have some kind of sane interface to get numerical
4108 * attributes.
4109 */
4110 exc = dom_element_has_attribute(li_node,
4111 corestring_dom_value,
4112 &has_value);
4113 if (exc != DOM_NO_ERR || has_value == false) {
4114 return false;
4115 }
4116
4117 exc = dom_html_li_element_get_value(
4118 (dom_html_li_element *)li_node,
4119 &value);
4120 if (exc != DOM_NO_ERR) {
4121 return false;
4122 }
4123
4124 *value_out = value;
4125 return true;
4126}
4127
4128
4129/**
4130 * Helper to get start attribute value from a OL node.
4131 *
4132 * \param[in] ol_node DOM node for the OL element;
4133 * \param[out] start_out Returns the value on success.
4134 * \return true if node has value, otherwise false.
4135 */
4136static bool
4137layout__get_ol_start(dom_node *ol_node, dom_long *start_out)
4138{
4139 dom_exception exc;
4140 dom_long start;
4141 bool has_start;
4142
4143 /** \todo
4144 * see layout__get_li_value().
4145 */
4146 exc = dom_element_has_attribute(ol_node,
4147 corestring_dom_start,
4148 &has_start);
4149 if (exc != DOM_NO_ERR || has_start == false) {
4150 return false;
4151 }
4152
4153 exc = dom_html_olist_element_get_start(
4154 (dom_html_olist_element *)ol_node,
4155 &start);
4156 if (exc != DOM_NO_ERR) {
4157 return false;
4158 }
4159
4160 *start_out = start;
4161 return true;
4162}
4163
4164
4165/**
4166 * Helper to get reversed attribute value from a OL node.
4167 *
4168 * \param[in] ol_node DOM node for the OL element;
4169 * \return true if node has reversed, otherwise false.
4170 */
4171static bool
4172layout__get_ol_reversed(dom_node *ol_node)
4173{
4174 dom_exception exc;
4175 bool has_reversed;
4176
4177 exc = dom_element_has_attribute(ol_node,
4178 corestring_dom_reversed,
4179 &has_reversed);
4180 if (exc != DOM_NO_ERR) {
4181 return false;
4182 }
4183
4184 return has_reversed;
4185}
4186
4187
4188/**
4189 * Get the number of list items for a list owner.
4190 *
4191 * \param[in] list_owner DOM node to count list items for.
4192 * \param[in] count_out Returns list item count on success.
4193 * \return true on success, otherwise false.
4194 */
4195static bool
4197 dom_node *list_owner, dom_long *count_out)
4198{
4199 dom_html_element_type tag_type;
4200 dom_exception exc;
4201 dom_node *child;
4202 int count;
4203
4204 if (list_owner == NULL) {
4205 return false;
4206 }
4207
4208 if (!layout__get_element_tag(list_owner, &tag_type)) {
4209 return false;
4210 }
4211
4212 if (tag_type != DOM_HTML_ELEMENT_TYPE_OL &&
4213 tag_type != DOM_HTML_ELEMENT_TYPE_UL) {
4214 return false;
4215 }
4216
4217 exc = dom_node_get_first_child(list_owner, &child);
4218 if (exc != DOM_NO_ERR) {
4219 return false;
4220 }
4221
4222 count = 0;
4223 while (child != NULL) {
4224 dom_node *temp_node;
4225
4227 DOM_HTML_ELEMENT_TYPE_LI)) {
4228 struct box *child_box;
4229 if (dom_node_get_user_data(child,
4230 corestring_dom___ns_key_box_node_data,
4231 &child_box) != DOM_NO_ERR) {
4232 dom_node_unref(child);
4233 return false;
4234 }
4235
4236 if (child_box != NULL &&
4237 child_box->list_marker != NULL) {
4238 count++;
4239 }
4240 }
4241
4242 exc = dom_node_get_next_sibling(child, &temp_node);
4243 dom_node_unref(child);
4244 if (exc != DOM_NO_ERR) {
4245 return false;
4246 }
4247
4248 child = temp_node;
4249 }
4250
4251 *count_out = count;
4252 return true;
4253}
4254
4255
4256/**
4257 * Handle list item counting, if this is a list owner box.
4258 *
4259 * \param[in] box Box to do list item counting for.
4260 */
4261static void
4263 struct box *box)
4264{
4265 dom_html_element_type tag_type;
4266 dom_exception exc;
4267 dom_node *child;
4268 int step = 1;
4269 dom_long next;
4270
4271 if (box->node == NULL) {
4272 return;
4273 }
4274
4275 if (!layout__get_element_tag(box->node, &tag_type)) {
4276 return;
4277 }
4278
4279 if (tag_type != DOM_HTML_ELEMENT_TYPE_OL &&
4280 tag_type != DOM_HTML_ELEMENT_TYPE_UL) {
4281 return;
4282 }
4283
4284 next = 1;
4285 if (tag_type == DOM_HTML_ELEMENT_TYPE_OL) {
4286 bool have_start = layout__get_ol_start(box->node, &next);
4287 bool have_reversed = layout__get_ol_reversed(box->node);
4288
4289 if (have_reversed) {
4290 step = -1;
4291 }
4292
4293 if (!have_start && have_reversed) {
4295 }
4296 }
4297
4298 exc = dom_node_get_first_child(box->node, &child);
4299 if (exc != DOM_NO_ERR) {
4300 return;
4301 }
4302
4303 while (child != NULL) {
4304 dom_node *temp_node;
4305
4307 DOM_HTML_ELEMENT_TYPE_LI)) {
4308 struct box *child_box;
4309
4310 if (dom_node_get_user_data(child,
4311 corestring_dom___ns_key_box_node_data,
4312 &child_box) != DOM_NO_ERR) {
4313 dom_node_unref(child);
4314 return;
4315 }
4316
4317 if (child_box != NULL &&
4318 child_box->list_marker != NULL) {
4319 dom_long value;
4320 struct box *marker = child_box->list_marker;
4321 if (layout__get_li_value(child, &value)) {
4322 marker->list_value = value;
4323 next = marker->list_value;
4324 } else {
4325 marker->list_value = next;
4326 }
4327 next += step;
4328 }
4329 }
4330
4331 exc = dom_node_get_next_sibling(child, &temp_node);
4332 dom_node_unref(child);
4333 if (exc != DOM_NO_ERR) {
4334 return;
4335 }
4336
4337 child = temp_node;
4338 }
4339}
4340
4341/**
4342 * Set up the marker text for a numerical list item.
4343 *
4344 * \param[in] content The HTML content.
4345 * \param[in] box The list item's main box.
4346 */
4347static void
4349 const html_content *content,
4350 struct box *box)
4351{
4352 struct box *marker = box->list_marker;
4353 size_t counter_len;
4354 css_error css_res;
4355 enum {
4356 /**
4357 * initial length of a list marker buffer
4358 *
4359 * enough for 9,999,999,999,999,999,999 in decimal
4360 * or five characters for 4-byte UTF-8.
4361 */
4362 LIST_MARKER_SIZE = 20,
4363 };
4364
4365 marker->text = talloc_array(content->bctx, char, LIST_MARKER_SIZE);
4366 if (marker->text == NULL) {
4367 return;
4368 }
4369
4370 css_res = css_computed_format_list_style(box->style, marker->list_value,
4371 marker->text, LIST_MARKER_SIZE, &counter_len);
4372 if (css_res == CSS_OK) {
4373 if (counter_len > LIST_MARKER_SIZE) {
4374 /* Use computed size as marker did not fit in
4375 * default allocation. */
4376 marker->text = talloc_realloc(content->bctx,
4377 marker->text,
4378 char,
4379 counter_len);
4380 if (marker->text == NULL) {
4381 return;
4382 }
4383 css_computed_format_list_style(box->style,
4384 marker->list_value, marker->text,
4385 counter_len, &counter_len);
4386 }
4387 marker->length = counter_len;
4388 }
4389}
4390
4391/**
4392 * Find out if box's style represents a numerical list style type.
4393 *
4394 * \param[in] b Box with style to test.
4395 * \return true if box has numerical list style type, false otherwise.
4396 */
4397static bool
4399 const struct box *b)
4400{
4401 enum css_list_style_type_e t = css_computed_list_style_type(b->style);
4402
4403 switch (t) {
4404 case CSS_LIST_STYLE_TYPE_DISC: /* Fall through. */
4405 case CSS_LIST_STYLE_TYPE_CIRCLE: /* Fall through. */
4406 case CSS_LIST_STYLE_TYPE_SQUARE: /* Fall through. */
4407 case CSS_LIST_STYLE_TYPE_NONE:
4408 return false;
4409
4410 default:
4411 return true;
4412 }
4413}
4414
4415/**
4416 * Layout list markers.
4417 */
4418static void
4420{
4421 struct box *child;
4422
4424
4425 for (child = box->children; child; child = child->next) {
4426 if (child->list_marker) {
4427 struct box *marker = child->list_marker;
4428
4429 if (layout__list_item_is_numerical(child)) {
4430 if (marker->text == NULL) {
4432 content, child);
4433 }
4434 }
4435 if (marker->object) {
4436 marker->width =
4437 content_get_width(marker->object);
4438 marker->x = -marker->width;
4439 marker->height =
4440 content_get_height(marker->object);
4441 marker->y = (line_height(
4442 &content->unit_len_ctx,
4443 marker->style) -
4444 marker->height) / 2;
4445 } else if (marker->text) {
4446 if (marker->width == UNKNOWN_WIDTH) {
4447 plot_font_style_t fstyle;
4449 &content->unit_len_ctx,
4450 marker->style,
4451 &fstyle);
4452 content->font_func->width(&fstyle,
4453 marker->text,
4454 marker->length,
4455 &marker->width);
4456 marker->flags |= MEASURED;
4457 }
4458 marker->x = -marker->width;
4459 marker->y = 0;
4460 marker->height = line_height(
4461 &content->unit_len_ctx,
4462 marker->style);
4463 } else {
4464 marker->x = 0;
4465 marker->y = 0;
4466 marker->width = 0;
4467 marker->height = 0;
4468 }
4469 /* Gap between marker and content */
4470 marker->x -= 4;
4471 }
4472 layout_lists(content, child);
4473 }
4474}
4475
4476
4477/**
4478 * Compute box offsets for a relatively or absolutely positioned box with
4479 * respect to a box.
4480 *
4481 * \param unit_len_ctx Length conversion context
4482 * \param box box to compute offsets for
4483 * \param containing_block box to compute percentages with respect to
4484 * \param top updated to top offset, or AUTO
4485 * \param right updated to right offset, or AUTO
4486 * \param bottom updated to bottom offset, or AUTO
4487 * \param left updated to left offset, or AUTO
4488 *
4489 * See CSS 2.1 9.3.2. containing_block must have width and height.
4490 */
4491static void
4492layout_compute_offsets(const css_unit_ctx *unit_len_ctx,
4493 struct box *box,
4494 struct box *containing_block,
4495 int *top,
4496 int *right,
4497 int *bottom,
4498 int *left)
4499{
4500 uint32_t type;
4501 css_fixed value = 0;
4502 css_unit unit = CSS_UNIT_PX;
4503
4504 assert(containing_block->width != UNKNOWN_WIDTH);
4505 assert(containing_block->width != AUTO);
4506 assert(containing_block->height != AUTO);
4507
4508 /* left */
4509 type = css_computed_left(box->style, &value, &unit);
4510 if (type == CSS_LEFT_SET) {
4511 if (unit == CSS_UNIT_PCT) {
4512 *left = FPCT_OF_INT_TOINT(value,
4513 containing_block->width);
4514 } else {
4515 *left = FIXTOINT(css_unit_len2device_px(
4516 box->style, unit_len_ctx,
4517 value, unit));
4518 }
4519 } else {
4520 *left = AUTO;
4521 }
4522
4523 /* right */
4524 type = css_computed_right(box->style, &value, &unit);
4525 if (type == CSS_RIGHT_SET) {
4526 if (unit == CSS_UNIT_PCT) {
4527 *right = FPCT_OF_INT_TOINT(value,
4528 containing_block->width);
4529 } else {
4530 *right = FIXTOINT(css_unit_len2device_px(
4531 box->style, unit_len_ctx,
4532 value, unit));
4533 }
4534 } else {
4535 *right = AUTO;
4536 }
4537
4538 /* top */
4539 type = css_computed_top(box->style, &value, &unit);
4540 if (type == CSS_TOP_SET) {
4541 if (unit == CSS_UNIT_PCT) {
4542 *top = FPCT_OF_INT_TOINT(value,
4543 containing_block->height);
4544 } else {
4545 *top = FIXTOINT(css_unit_len2device_px(
4546 box->style, unit_len_ctx,
4547 value, unit));
4548 }
4549 } else {
4550 *top = AUTO;
4551 }
4552
4553 /* bottom */
4554 type = css_computed_bottom(box->style, &value, &unit);
4555 if (type == CSS_BOTTOM_SET) {
4556 if (unit == CSS_UNIT_PCT) {
4557 *bottom = FPCT_OF_INT_TOINT(value,
4558 containing_block->height);
4559 } else {
4560 *bottom = FIXTOINT(css_unit_len2device_px(
4561 box->style, unit_len_ctx,
4562 value, unit));
4563 }
4564 } else {
4565 *bottom = AUTO;
4566 }
4567}
4568
4569
4570/**
4571 * Layout and position an absolutely positioned box.
4572 *
4573 * \param box absolute box to layout and position
4574 * \param containing_block containing block
4575 * \param cx position of box relative to containing_block
4576 * \param cy position of box relative to containing_block
4577 * \param content memory pool for any new boxes
4578 * \return true on success, false on memory exhaustion
4579 */
4580static bool
4582 struct box *containing_block,
4583 int cx, int cy,
4585{
4586 int static_left, static_top; /* static position */
4587 int top, right, bottom, left;
4589 int *margin = box->margin;
4590 int *padding = box->padding;
4591 struct box_border *border = box->border;
4592 int available_width = containing_block->width;
4593 int space;
4594
4595 assert(box->type == BOX_BLOCK || box->type == BOX_TABLE ||
4597 box->type == BOX_FLEX ||
4599
4600 /* The static position is where the box would be if it was not
4601 * absolutely positioned. The x and y are filled in by
4602 * layout_block_context(). */
4603 static_left = cx + box->x;
4604 static_top = cy + box->y;
4605
4606 if (containing_block->type == BOX_BLOCK ||
4607 containing_block->type == BOX_INLINE_BLOCK ||
4608 containing_block->type == BOX_TABLE_CELL) {
4609 /* Block level container => temporarily increase containing
4610 * block dimensions to include padding (we restore this
4611 * again at the end) */
4612 containing_block->width += containing_block->padding[LEFT] +
4613 containing_block->padding[RIGHT];
4614 containing_block->height += containing_block->padding[TOP] +
4615 containing_block->padding[BOTTOM];
4616 }
4617
4618 layout_compute_offsets(&content->unit_len_ctx, box, containing_block,
4619 &top, &right, &bottom, &left);
4620
4621 /* Pass containing block into layout_find_dimensions via the float
4622 * containing block box member. This is unused for absolutely positioned
4623 * boxes because a box can't be floated and absolutely positioned. */
4624 box->float_container = containing_block;
4625 layout_find_dimensions(&content->unit_len_ctx, available_width, -1,
4626 box, box->style, &width, &height,
4627 &max_width, &min_width, 0, 0,
4628 margin, padding, border);
4629 box->float_container = NULL;
4630
4631 /* 10.3.7 */
4632 NSLOG(layout, DEBUG,
4633 "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i",
4634 left, margin[LEFT], border[LEFT].width, padding[LEFT], width,
4635 padding[RIGHT], border[RIGHT].width, margin[RIGHT], right,
4636 containing_block->width);
4637
4638
4639 if (left == AUTO && width == AUTO && right == AUTO) {
4640 if (margin[LEFT] == AUTO)
4641 margin[LEFT] = 0;
4642 if (margin[RIGHT] == AUTO)
4643 margin[RIGHT] = 0;
4644 left = static_left;
4645
4646 width = min(max(box->min_width, available_width),
4647 box->max_width);
4648 width -= box->margin[LEFT] + box->border[LEFT].width +
4651
4652 /* Adjust for {min|max}-width */
4653 if (max_width >= 0 && width > max_width) width = max_width;
4654 if (width < min_width) width = min_width;
4655
4656 right = containing_block->width -
4657 left -
4658 margin[LEFT] - border[LEFT].width - padding[LEFT] -
4659 width -
4660 padding[RIGHT] - border[RIGHT].width - margin[RIGHT];
4661 } else if (left != AUTO && width != AUTO && right != AUTO) {
4662
4663 /* Adjust for {min|max}-width */
4664 if (max_width >= 0 && width > max_width) width = max_width;
4665 if (min_width > 0 && width < min_width) width = min_width;
4666
4667 if (margin[LEFT] == AUTO && margin[RIGHT] == AUTO) {
4668 space = containing_block->width -
4669 left - border[LEFT].width -
4670 padding[LEFT] - width - padding[RIGHT] -
4671 border[RIGHT].width - right;
4672 if (space < 0) {
4673 margin[LEFT] = 0;
4674 margin[RIGHT] = space;
4675 } else {
4676 margin[LEFT] = margin[RIGHT] = space / 2;
4677 }
4678 } else if (margin[LEFT] == AUTO) {
4679 margin[LEFT] = containing_block->width -
4680 left - border[LEFT].width -
4681 padding[LEFT] - width - padding[RIGHT] -
4682 border[RIGHT].width - margin[RIGHT] -
4683 right;
4684 } else if (margin[RIGHT] == AUTO) {
4685 margin[RIGHT] = containing_block->width -
4686 left - margin[LEFT] -
4687 border[LEFT].width -
4688 padding[LEFT] - width - padding[RIGHT] -
4689 border[RIGHT].width - right;
4690 } else {
4691 right = containing_block->width -
4692 left - margin[LEFT] -
4693 border[LEFT].width -
4694 padding[LEFT] - width - padding[RIGHT] -
4695 border[RIGHT].width - margin[RIGHT];
4696 }
4697 } else {
4698 if (margin[LEFT] == AUTO)
4699 margin[LEFT] = 0;
4700 if (margin[RIGHT] == AUTO)
4701 margin[RIGHT] = 0;
4702
4703 if (left == AUTO && width == AUTO && right != AUTO) {
4704 available_width -= right;
4705
4706 width = min(max(box->min_width, available_width),
4707 box->max_width);
4708 width -= box->margin[LEFT] + box->border[LEFT].width +
4711
4712 /* Adjust for {min|max}-width */
4713 if (max_width >= 0 && width > max_width)
4714 width = max_width;
4715 if (width < min_width)
4716 width = min_width;
4717
4718 left = containing_block->width -
4719 margin[LEFT] - border[LEFT].width -
4720 padding[LEFT] - width - padding[RIGHT] -
4721 border[RIGHT].width - margin[RIGHT] -
4722 right;
4723 } else if (left == AUTO && width != AUTO && right == AUTO) {
4724
4725 /* Adjust for {min|max}-width */
4726 if (max_width >= 0 && width > max_width)
4727 width = max_width;
4728 if (min_width > 0 && width < min_width)
4729 width = min_width;
4730
4731 left = static_left;
4732 right = containing_block->width -
4733 left - margin[LEFT] -
4734 border[LEFT].width -
4735 padding[LEFT] - width - padding[RIGHT] -
4736 border[RIGHT].width - margin[RIGHT];
4737 } else if (left != AUTO && width == AUTO && right == AUTO) {
4738 available_width -= left;
4739
4740 width = min(max(box->min_width, available_width),
4741 box->max_width);
4742 width -= box->margin[LEFT] + box->border[LEFT].width +
4745
4746 /* Adjust for {min|max}-width */
4747 if (max_width >= 0 && width > max_width)
4748 width = max_width;
4749 if (width < min_width)
4750 width = min_width;
4751
4752 right = containing_block->width -
4753 left - margin[LEFT] -
4754 border[LEFT].width -
4755 padding[LEFT] - width - padding[RIGHT] -
4756 border[RIGHT].width - margin[RIGHT];
4757 } else if (left == AUTO && width != AUTO && right != AUTO) {
4758
4759 /* Adjust for {min|max}-width */
4760 if (max_width >= 0 && width > max_width)
4761 width = max_width;
4762 if (width < min_width)
4763 width = min_width;
4764
4765 left = containing_block->width -
4766 margin[LEFT] - border[LEFT].width -
4767 padding[LEFT] - width - padding[RIGHT] -
4768 border[RIGHT].width - margin[RIGHT] -
4769 right;
4770 } else if (left != AUTO && width == AUTO && right != AUTO) {
4771 width = containing_block->width -
4772 left - margin[LEFT] -
4773 border[LEFT].width -
4774 padding[LEFT] - padding[RIGHT] -
4775 border[RIGHT].width - margin[RIGHT] -
4776 right;
4777
4778 /* Adjust for {min|max}-width */
4779 if (max_width >= 0 && width > max_width)
4780 width = max_width;
4781 if (width < min_width)
4782 width = min_width;
4783
4784 } else if (left != AUTO && width != AUTO && right == AUTO) {
4785
4786 /* Adjust for {min|max}-width */
4787 if (max_width >= 0 && width > max_width)
4788 width = max_width;
4789 if (width < min_width)
4790 width = min_width;
4791
4792 right = containing_block->width -
4793 left - margin[LEFT] -
4794 border[LEFT].width -
4795 padding[LEFT] - width - padding[RIGHT] -
4796 border[RIGHT].width - margin[RIGHT];
4797 }
4798 }
4799
4800 NSLOG(layout, DEBUG,
4801 "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i",
4802 left, margin[LEFT], border[LEFT].width, padding[LEFT], width,
4803 padding[RIGHT], border[RIGHT].width, margin[RIGHT], right,
4804 containing_block->width);
4805
4806 box->x = left + margin[LEFT] + border[LEFT].width - cx;
4807 if (containing_block->type == BOX_BLOCK ||
4808 containing_block->type == BOX_INLINE_BLOCK ||
4809 containing_block->type == BOX_TABLE_CELL) {
4810 /* Block-level ancestor => reset container's width */
4811 containing_block->width -= containing_block->padding[LEFT] +
4812 containing_block->padding[RIGHT];
4813 } else {
4814 /** \todo inline ancestors */
4815 }
4816 box->width = width;
4817 box->height = height;
4818
4819 if (box->type == BOX_BLOCK || box->type == BOX_INLINE_BLOCK ||
4820 box->object || box->flags & IFRAME) {
4821 if (!layout_block_context(box, -1, content))
4822 return false;
4823 } else if (box->type == BOX_TABLE) {
4824 /* layout_table also expects the containing block to be
4825 * stored in the float_container field */
4826 box->float_container = containing_block;
4827 /* \todo layout_table considers margins etc. again */
4828 if (!layout_table(box, width, content))
4829 return false;
4830 box->float_container = NULL;
4832 -1, -1);
4833 } else if (box->type == BOX_FLEX || box->type == BOX_INLINE_FLEX) {
4834 /* layout_table also expects the containing block to be
4835 * stored in the float_container field */
4836 box->float_container = containing_block;
4837 if (!layout_flex(box, width, content))
4838 return false;
4839 box->float_container = NULL;
4840 }
4841
4842 /* 10.6.4 */
4843 NSLOG(layout, DEBUG,
4844 "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i",
4845 top, margin[TOP], border[TOP].width, padding[TOP], height,
4846 padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom,
4847 containing_block->height);
4848
4849 if (top == AUTO && height == AUTO && bottom == AUTO) {
4850 top = static_top;
4851 height = box->height;
4852 if (margin[TOP] == AUTO)
4853 margin[TOP] = 0;
4854 if (margin[BOTTOM] == AUTO)
4855 margin[BOTTOM] = 0;
4856 bottom = containing_block->height -
4857 top - margin[TOP] - border[TOP].width -
4858 padding[TOP] - height - padding[BOTTOM] -
4859 border[BOTTOM].width - margin[BOTTOM];
4860 } else if (top != AUTO && height != AUTO && bottom != AUTO) {
4861 if (margin[TOP] == AUTO && margin[BOTTOM] == AUTO) {
4862 space = containing_block->height -
4863 top - border[TOP].width - padding[TOP] -
4864 height - padding[BOTTOM] -
4865 border[BOTTOM].width - bottom;
4866 margin[TOP] = margin[BOTTOM] = space / 2;
4867 } else if (margin[TOP] == AUTO) {
4868 margin[TOP] = containing_block->height -
4869 top - border[TOP].width - padding[TOP] -
4870 height - padding[BOTTOM] -
4871 border[BOTTOM].width - margin[BOTTOM] -
4872 bottom;
4873 } else if (margin[BOTTOM] == AUTO) {
4874 margin[BOTTOM] = containing_block->height -
4875 top - margin[TOP] - border[TOP].width -
4876 padding[TOP] - height -
4877 padding[BOTTOM] - border[BOTTOM].width -
4878 bottom;
4879 } else {
4880 bottom = containing_block->height -
4881 top - margin[TOP] - border[TOP].width -
4882 padding[TOP] - height -
4883 padding[BOTTOM] - border[BOTTOM].width -
4884 margin[BOTTOM];
4885 }
4886 } else {
4887 if (margin[TOP] == AUTO)
4888 margin[TOP] = 0;
4889 if (margin[BOTTOM] == AUTO)
4890 margin[BOTTOM] = 0;
4891 if (top == AUTO && height == AUTO && bottom != AUTO) {
4892 height = box->height;
4893 top = containing_block->height -
4894 margin[TOP] - border[TOP].width -
4895 padding[TOP] - height -
4896 padding[BOTTOM] - border[BOTTOM].width -
4897 margin[BOTTOM] - bottom;
4898 } else if (top == AUTO && height != AUTO && bottom == AUTO) {
4899 top = static_top;
4900 bottom = containing_block->height -
4901 top - margin[TOP] - border[TOP].width -
4902 padding[TOP] - height -
4903 padding[BOTTOM] - border[BOTTOM].width -
4904 margin[BOTTOM];
4905 } else if (top != AUTO && height == AUTO && bottom == AUTO) {
4906 height = box->height;
4907 bottom = containing_block->height -
4908 top - margin[TOP] - border[TOP].width -
4909 padding[TOP] - height -
4910 padding[BOTTOM] - border[BOTTOM].width -
4911 margin[BOTTOM];
4912 } else if (top == AUTO && height != AUTO && bottom != AUTO) {
4913 top = containing_block->height -
4914 margin[TOP] - border[TOP].width -
4915 padding[TOP] - height -
4916 padding[BOTTOM] - border[BOTTOM].width -
4917 margin[BOTTOM] - bottom;
4918 } else if (top != AUTO && height == AUTO && bottom != AUTO) {
4919 height = containing_block->height -
4920 top - margin[TOP] - border[TOP].width -
4921 padding[TOP] - padding[BOTTOM] -
4922 border[BOTTOM].width - margin[BOTTOM] -
4923 bottom;
4924 } else if (top != AUTO && height != AUTO && bottom == AUTO) {
4925 bottom = containing_block->height -
4926 top - margin[TOP] - border[TOP].width -
4927 padding[TOP] - height -
4928 padding[BOTTOM] - border[BOTTOM].width -
4929 margin[BOTTOM];
4930 }
4931 }
4932
4933 NSLOG(layout, DEBUG,
4934 "%i + %i + %i + %i + %i + %i + %i + %i + %i = %i",
4935 top, margin[TOP], border[TOP].width, padding[TOP], height,
4936 padding[BOTTOM], border[BOTTOM].width, margin[BOTTOM], bottom,
4937 containing_block->height);
4938
4939 box->y = top + margin[TOP] + border[TOP].width - cy;
4940 if (containing_block->type == BOX_BLOCK ||
4941 containing_block->type == BOX_INLINE_BLOCK ||
4942 containing_block->type == BOX_TABLE_CELL) {
4943 /* Block-level ancestor => reset container's height */
4944 containing_block->height -= containing_block->padding[TOP] +
4945 containing_block->padding[BOTTOM];
4946 } else {
4947 /** \todo Inline ancestors */
4948 }
4949 box->height = height;
4950 layout_apply_minmax_height(&content->unit_len_ctx, box, containing_block);
4951
4952 return true;
4953}
4954
4955
4956/**
4957 * Recursively layout and position absolutely positioned boxes.
4958 *
4959 * \param box tree of boxes to layout
4960 * \param containing_block current containing block
4961 * \param cx position of box relative to containing_block
4962 * \param cy position of box relative to containing_block
4963 * \param content memory pool for any new boxes
4964 * \return true on success, false on memory exhaustion
4965 */
4966static bool
4968 struct box *containing_block,
4969 int cx, int cy,
4971{
4972 struct box *c;
4973
4974 for (c = box->children; c; c = c->next) {
4975 if ((c->type == BOX_BLOCK || c->type == BOX_TABLE ||
4976 c->type == BOX_INLINE_BLOCK ||
4977 c->type == BOX_FLEX ||
4978 c->type == BOX_INLINE_FLEX) &&
4979 (css_computed_position(c->style) ==
4980 CSS_POSITION_ABSOLUTE ||
4981 css_computed_position(c->style) ==
4982 CSS_POSITION_FIXED)) {
4983 if (!layout_absolute(c, containing_block,
4984 cx, cy, content))
4985 return false;
4986 if (!layout_position_absolute(c, c, 0, 0, content))
4987 return false;
4988 } else if (c->style && css_computed_position(c->style) ==
4989 CSS_POSITION_RELATIVE) {
4990 if (!layout_position_absolute(c, c, 0, 0, content))
4991 return false;
4992 } else {
4993 int px, py;
4994 if (c->style && (css_computed_float(c->style) ==
4995 CSS_FLOAT_LEFT ||
4996 css_computed_float(c->style) ==
4997 CSS_FLOAT_RIGHT)) {
4998 /* Float x/y coords are relative to nearest
4999 * ansestor with float_children, rather than
5000 * relative to parent. Need to get x/y relative
5001 * to parent */
5002 struct box *p;
5003 px = c->x;
5004 py = c->y;
5005 for (p = box->parent; p && !p->float_children;
5006 p = p->parent) {
5007 px -= p->x;
5008 py -= p->y;
5009 }
5010 } else {
5011 /* Not a float, so box x/y coords are relative
5012 * to parent */
5013 px = c->x;
5014 py = c->y;
5015 }
5016 if (!layout_position_absolute(c, containing_block,
5017 cx + px, cy + py, content))
5018 return false;
5019 }
5020 }
5021
5022 return true;
5023}
5024
5025
5026/**
5027 * Compute a box's relative offset as per CSS 2.1 9.4.3
5028 *
5029 * \param unit_len_ctx Length conversion context
5030 * \param box Box to compute relative offsets for.
5031 * \param x Receives relative offset in x.
5032 * \param y Receives relative offset in y.
5033 */
5035 const css_unit_ctx *unit_len_ctx,
5036 struct box *box,
5037 int *x,
5038 int *y)
5039{
5040 int left, right, top, bottom;
5041 struct box *containing_block;
5042
5043 assert(box && box->parent && box->style &&
5044 css_computed_position(box->style) ==
5045 CSS_POSITION_RELATIVE);
5046
5047 if (box->float_container &&
5048 (css_computed_float(box->style) == CSS_FLOAT_LEFT ||
5049 css_computed_float(box->style) == CSS_FLOAT_RIGHT)) {
5050 containing_block = box->float_container;
5051 } else {
5052 containing_block = box->parent;
5053 }
5054
5055 layout_compute_offsets(unit_len_ctx, box, containing_block,
5056 &top, &right, &bottom, &left);
5057
5058 if (left == AUTO && right == AUTO)
5059 left = right = 0;
5060 else if (left == AUTO)
5061 /* left is auto => computed = -right */
5062 left = -right;
5063 else if (right == AUTO)
5064 /* right is auto => computed = -left */
5065 right = -left;
5066 else {
5067 /* over constrained => examine direction property
5068 * of containing block */
5069 if (containing_block->style &&
5070 css_computed_direction(
5071 containing_block->style) ==
5072 CSS_DIRECTION_RTL) {
5073 /* right wins */
5074 left = -right;
5075 } else {
5076 /* assume LTR in all other cases */
5077 right = -left;
5078 }
5079 }
5080
5081 assert(left == -right);
5082
5083 if (top == AUTO && bottom == AUTO) {
5084 top = bottom = 0;
5085 } else if (top == AUTO) {
5086 top = -bottom;
5087 } else {
5088 /* bottom is AUTO, or neither are AUTO */
5089 bottom = -top;
5090 }
5091
5092 NSLOG(layout, DEBUG, "left %i, right %i, top %i, bottom %i", left,
5093 right, top, bottom);
5094
5095 *x = left;
5096 *y = top;
5097}
5098
5099
5100/**
5101 * Adjust positions of relatively positioned boxes.
5102 *
5103 * \param unit_len_ctx Length conversion context
5104 * \param root box to adjust the position of
5105 * \param fp box which forms the block formatting context for children of
5106 * "root" which are floats
5107 * \param fx x offset due to intervening relatively positioned boxes
5108 * between current box, "root", and the block formatting context
5109 * box, "fp", for float children of "root"
5110 * \param fy y offset due to intervening relatively positioned boxes
5111 * between current box, "root", and the block formatting context
5112 * box, "fp", for float children of "root"
5113 */
5114static void
5116 const css_unit_ctx *unit_len_ctx,
5117 struct box *root,
5118 struct box *fp,
5119 int fx,
5120 int fy)
5121{
5122 struct box *box; /* for children of "root" */
5123 struct box *fn; /* for block formatting context box for children of
5124 * "box" */
5125 struct box *fc; /* for float children of the block formatting context,
5126 * "fp" */
5127 int x, y; /* for the offsets resulting from any relative
5128 * positioning on the current block */
5129 int fnx, fny; /* for affsets which apply to flat children of "box" */
5130
5131 /**\todo ensure containing box is large enough after moving boxes */
5132
5133 assert(root);
5134
5135 /* Normal children */
5136 for (box = root->children; box; box = box->next) {
5137
5138 if (box->type == BOX_TEXT)
5139 continue;
5140
5141 /* If relatively positioned, get offsets */
5142 if (box->style && css_computed_position(box->style) ==
5143 CSS_POSITION_RELATIVE)
5145 unit_len_ctx, box, &x, &y);
5146 else
5147 x = y = 0;
5148
5149 /* Adjust float coordinates.
5150 * (note float x and y are relative to their block formatting
5151 * context box and not their parent) */
5152 if (box->style && (css_computed_float(box->style) ==
5153 CSS_FLOAT_LEFT ||
5154 css_computed_float(box->style) ==
5155 CSS_FLOAT_RIGHT) &&
5156 (fx != 0 || fy != 0)) {
5157 /* box is a float and there is a float offset to
5158 * apply */
5159 for (fc = fp->float_children; fc; fc = fc->next_float) {
5160 if (box == fc->children) {
5161 /* Box is floated in the block
5162 * formatting context block, fp.
5163 * Apply float offsets. */
5164 box->x += fx;
5165 box->y += fy;
5166 fx = fy = 0;
5167 }
5168 }
5169 }
5170
5171 if (box->float_children) {
5172 fn = box;
5173 fnx = fny = 0;
5174 } else {
5175 fn = fp;
5176 fnx = fx + x;
5177 fny = fy + y;
5178 }
5179
5180 /* recurse first */
5181 layout_position_relative(unit_len_ctx, box, fn, fnx, fny);
5182
5183 /* Ignore things we're not interested in. */
5184 if (!box->style || (box->style &&
5185 css_computed_position(box->style) !=
5186 CSS_POSITION_RELATIVE))
5187 continue;
5188
5189 box->x += x;
5190 box->y += y;
5191
5192 /* Handle INLINEs - their "children" are in fact
5193 * the sibling boxes between the INLINE and
5194 * INLINE_END boxes */
5195 if (box->type == BOX_INLINE && box->inline_end) {
5196 struct box *b;
5197 for (b = box->next; b && b != box->inline_end;
5198 b = b->next) {
5199 b->x += x;
5200 b->y += y;
5201 }
5202 }
5203 }
5204}
5205
5206
5207/**
5208 * Find a box's bounding box relative to itself, i.e. the box's border edge box
5209 *
5210 * \param unit_len_ctx Length conversion context
5211 * \param box box find bounding box of
5212 * \param desc_x0 updated to left of box's bbox
5213 * \param desc_y0 updated to top of box's bbox
5214 * \param desc_x1 updated to right of box's bbox
5215 * \param desc_y1 updated to bottom of box's bbox
5216 */
5217static void
5219 const css_unit_ctx *unit_len_ctx,
5220 struct box *box,
5221 int *desc_x0, int *desc_y0,
5222 int *desc_x1, int *desc_y1)
5223{
5224 *desc_x0 = -box->border[LEFT].width;
5225 *desc_y0 = -box->border[TOP].width;
5226 *desc_x1 = box->padding[LEFT] + box->width + box->padding[RIGHT] +
5228 *desc_y1 = box->padding[TOP] + box->height + box->padding[BOTTOM] +
5230
5231 /* To stop the top of text getting clipped when css line-height is
5232 * reduced, we increase the top of the descendant bbox. */
5233 if (box->type == BOX_BLOCK && box->style != NULL &&
5234 css_computed_overflow_y(box->style) ==
5235 CSS_OVERFLOW_VISIBLE &&
5236 box->object == NULL) {
5237 css_fixed font_size = 0;
5238 css_unit font_unit = CSS_UNIT_PT;
5239 int text_height;
5240
5241 css_computed_font_size(box->style, &font_size, &font_unit);
5242 text_height = css_unit_len2device_px(box->style, unit_len_ctx,
5243 font_size, font_unit);
5244 text_height = FIXTOINT(text_height * 3 / 4);
5245 *desc_y0 = (*desc_y0 < -text_height) ? *desc_y0 : -text_height;
5246 }
5247}
5248
5249
5250/**
5251 * Apply changes to box descendant_[xy][01] values due to given child.
5252 *
5253 * \param unit_len_ctx Length conversion context
5254 * \param box box to update
5255 * \param child a box, which may affect box's descendant bbox
5256 * \param off_x offset to apply to child->x coord to treat as child of box
5257 * \param off_y offset to apply to child->y coord to treat as child of box
5258 */
5259static void
5261 const css_unit_ctx *unit_len_ctx,
5262 struct box *box,
5263 struct box *child,
5264 int off_x,
5265 int off_y)
5266{
5267 int child_desc_x0, child_desc_y0, child_desc_x1, child_desc_y1;
5268
5269 /* get coordinates of child relative to box */
5270 int child_x = child->x - off_x;
5271 int child_y = child->y - off_y;
5272
5273 bool html_object = (child->object &&
5275
5276 enum css_overflow_e overflow_x = CSS_OVERFLOW_VISIBLE;
5277 enum css_overflow_e overflow_y = CSS_OVERFLOW_VISIBLE;
5278
5279 if (child->style != NULL) {
5280 overflow_x = css_computed_overflow_x(child->style);
5281 overflow_y = css_computed_overflow_y(child->style);
5282 }
5283
5284 /* Get child's border edge */
5285 layout_get_box_bbox(unit_len_ctx, child,
5286 &child_desc_x0, &child_desc_y0,
5287 &child_desc_x1, &child_desc_y1);
5288
5289 if (overflow_x == CSS_OVERFLOW_VISIBLE &&
5290 html_object == false) {
5291 /* get child's descendant bbox relative to box */
5292 child_desc_x0 = child->descendant_x0;
5293 child_desc_x1 = child->descendant_x1;
5294 }
5295 if (overflow_y == CSS_OVERFLOW_VISIBLE &&
5296 html_object == false) {
5297 /* get child's descendant bbox relative to box */
5298 child_desc_y0 = child->descendant_y0;
5299 child_desc_y1 = child->descendant_y1;
5300 }
5301
5302 child_desc_x0 += child_x;
5303 child_desc_y0 += child_y;
5304 child_desc_x1 += child_x;
5305 child_desc_y1 += child_y;
5306
5307 /* increase box's descendant bbox to contain descendants */
5308 if (child_desc_x0 < box->descendant_x0)
5309 box->descendant_x0 = child_desc_x0;
5310 if (child_desc_y0 < box->descendant_y0)
5311 box->descendant_y0 = child_desc_y0;
5312 if (box->descendant_x1 < child_desc_x1)
5313 box->descendant_x1 = child_desc_x1;
5314 if (box->descendant_y1 < child_desc_y1)
5315 box->descendant_y1 = child_desc_y1;
5316}
5317
5318
5319/**
5320 * Recursively calculate the descendant_[xy][01] values for a laid-out box tree
5321 * and inform iframe browser windows of their size and position.
5322 *
5323 * \param unit_len_ctx Length conversion context
5324 * \param box tree of boxes to update
5325 */
5327 const css_unit_ctx *unit_len_ctx,
5328 struct box *box)
5329{
5330 struct box *child;
5331
5332 assert(box->width != UNKNOWN_WIDTH);
5333 assert(box->height != AUTO);
5334 /* assert((box->width >= 0) && (box->height >= 0)); */
5335
5336 /* Initialise box's descendant box to border edge box */
5337 layout_get_box_bbox(unit_len_ctx, box,
5340
5341 /* Extend it to contain HTML contents if box is replaced */
5347 }
5348
5349 if (box->iframe != NULL) {
5350 int x, y;
5351 box_coords(box, &x, &y);
5352
5355 box->width, box->height);
5357 box->width, box->height);
5358 }
5359
5360 if (box->type == BOX_INLINE || box->type == BOX_TEXT)
5361 return;
5362
5363 if (box->type == BOX_INLINE_END) {
5364 box = box->inline_end;
5365 for (child = box->next; child;
5366 child = child->next) {
5367 if (child->type == BOX_FLOAT_LEFT ||
5368 child->type == BOX_FLOAT_RIGHT)
5369 continue;
5370
5371 layout_update_descendant_bbox(unit_len_ctx, box, child,
5372 box->x, box->y);
5373
5374 if (child == box->inline_end)
5375 break;
5376 }
5377 return;
5378 }
5379
5380 if (box->flags & REPLACE_DIM)
5381 /* Box's children aren't displayed if the box is replaced */
5382 return;
5383
5384 for (child = box->children; child; child = child->next) {
5385 if (child->type == BOX_FLOAT_LEFT ||
5386 child->type == BOX_FLOAT_RIGHT)
5387 continue;
5388
5389 layout_calculate_descendant_bboxes(unit_len_ctx, child);
5390
5391 if (box->style && css_computed_overflow_x(box->style) ==
5392 CSS_OVERFLOW_HIDDEN &&
5393 css_computed_overflow_y(box->style) ==
5394 CSS_OVERFLOW_HIDDEN)
5395 continue;
5396
5397 layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
5398 }
5399
5400 for (child = box->float_children; child; child = child->next_float) {
5401 assert(child->type == BOX_FLOAT_LEFT ||
5402 child->type == BOX_FLOAT_RIGHT);
5403
5404 layout_calculate_descendant_bboxes(unit_len_ctx, child);
5405
5406 layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
5407 }
5408
5409 if (box->list_marker) {
5410 child = box->list_marker;
5411 layout_calculate_descendant_bboxes(unit_len_ctx, child);
5412
5413 layout_update_descendant_bbox(unit_len_ctx, box, child, 0, 0);
5414 }
5415}
5416
5417
5418/* exported function documented in html/layout.h */
5420{
5421 bool ret;
5422 struct box *doc = content->layout;
5423 const struct gui_layout_table *font_func = content->font_func;
5424
5425 NSLOG(layout, DEBUG, "Doing layout to %ix%i of %s",
5427 &content->base)));
5428
5429 layout_minmax_block(doc, font_func, content);
5430
5432 width, height, 0, 0, doc);
5433 doc->x = doc->margin[LEFT] + doc->border[LEFT].width;
5434 doc->y = doc->margin[TOP] + doc->border[TOP].width;
5435 width -= doc->margin[LEFT] + doc->border[LEFT].width +
5436 doc->padding[LEFT] + doc->padding[RIGHT] +
5437 doc->border[RIGHT].width + doc->margin[RIGHT];
5438 if (width < 0) {
5439 width = 0;
5440 }
5441 doc->width = width;
5442
5443 ret = layout_block_context(doc, height, content);
5444
5445 /* make <html> and <body> fill available height */
5446 if (doc->y + doc->padding[TOP] + doc->height + doc->padding[BOTTOM] +
5447 doc->border[BOTTOM].width + doc->margin[BOTTOM] <
5448 height) {
5449 doc->height = height - (doc->y + doc->padding[TOP] +
5450 doc->padding[BOTTOM] +
5451 doc->border[BOTTOM].width +
5452 doc->margin[BOTTOM]);
5453 if (doc->children)
5454 doc->children->height = doc->height -
5455 (doc->children->margin[TOP] +
5456 doc->children->border[TOP].width +
5457 doc->children->padding[TOP] +
5458 doc->children->padding[BOTTOM] +
5459 doc->children->border[BOTTOM].width +
5460 doc->children->margin[BOTTOM]);
5461 }
5462
5463 layout_lists(content, doc);
5464 layout_position_absolute(doc, doc, 0, 0, content);
5465 layout_position_relative(&content->unit_len_ctx, doc, doc, 0, 0);
5466
5467 layout_calculate_descendant_bboxes(&content->unit_len_ctx, doc);
5468
5469 return ret;
5470}
Box interface.
#define UNKNOWN_WIDTH
Definition: box.h:45
#define UNKNOWN_MAX_WIDTH
Definition: box.h:46
@ BOX_BLOCK
Definition: box.h:56
@ BOX_FLOAT_LEFT
Definition: box.h:63
@ BOX_INLINE_BLOCK
Definition: box.h:65
@ BOX_FLOAT_RIGHT
Definition: box.h:64
@ BOX_INLINE_CONTAINER
Definition: box.h:57
@ BOX_TABLE_CELL
Definition: box.h:61
@ BOX_INLINE_END
Definition: box.h:68
@ BOX_TABLE
Definition: box.h:59
@ BOX_INLINE
Definition: box.h:58
@ BOX_TEXT
Definition: box.h:67
@ BOX_INLINE_FLEX
Definition: box.h:71
@ BOX_BR
Definition: box.h:66
@ BOX_FLEX
Definition: box.h:70
@ IFRAME
Definition: box.h:89
@ NEED_MIN
Definition: box.h:87
@ NEW_LINE
Definition: box.h:79
@ MEASURED
Definition: box.h:84
@ CLONE
Definition: box.h:83
@ MAKE_HEIGHT
Definition: box.h:86
@ HAS_HEIGHT
Definition: box.h:85
@ REPLACE_DIM
Definition: box.h:88
@ TOP
Definition: box.h:98
@ BOTTOM
Definition: box.h:98
@ LEFT
Definition: box.h:98
@ RIGHT
Definition: box.h:98
bool box_hscrollbar_present(const struct box *const box)
Determine if a box has a horizontal scrollbar.
Definition: box_inspect.c:839
bool box_vscrollbar_present(const struct box *const box)
Determine if a box has a vertical scrollbar.
Definition: box_inspect.c:829
void box_coords(struct box *box, int *x, int *y)
Find the absolute coordinates of a box.
Definition: box_inspect.c:549
void box_dump(FILE *stream, struct box *box, unsigned int depth, bool style)
Print a box tree to a file.
Definition: box_inspect.c:649
HTML Box tree inspection interface.
static bool box_is_first_child(struct box *b)
Check if layout box is a first child.
Definition: box_inspect.h:137
Browser window creation and manipulation interface.
void browser_window_set_position(struct browser_window *bw, int x, int y)
Set the position of the current browser window with respect to the parent browser window.
void browser_window_set_dimensions(struct browser_window *bw, int width, int height)
Set the dimensions of the area a browser window occupies.
void browser_window_reformat(struct browser_window *bw, bool background, int width, int height)
Reformat a browser window contents to a new width or height.
static uint32_t count(const http_directive *list, lwc_string *key)
Content handling interface.
static uint8_t ns_computed_min_height(const css_computed_style *style, css_fixed *length, css_unit *unit)
Definition: utils.h:75
void font_plot_style_from_css(const css_unit_ctx *unit_len_ctx, const css_computed_style *css, plot_font_style_t *fstyle)
Populate a font style using data from a computed CSS style.
Definition: font.c:135
Internal font handling interfaces.
static bool layout__get_li_value(dom_node *li_node, dom_long *value_out)
Helper to get attribute value from a LI node.
Definition: layout.c:4095
static bool layout__get_ol_reversed(dom_node *ol_node)
Helper to get reversed attribute value from a OL node.
Definition: layout.c:4172
static void layout_minmax_table(struct box *table, const struct gui_layout_table *font_func, const html_content *content)
Calculate minimum and maximum width of a table.
Definition: layout.c:257
static void layout_move_children(struct box *box, int x, int y)
Moves the children of a box by a specified amount.
Definition: layout.c:1592
static void layout_block_add_scrollbar(struct box *box, int which)
Manipulate a block's [RB]padding/height/width to accommodate scrollbars.
Definition: layout.c:1539
static bool layout_position_absolute(struct box *box, struct box *containing_block, int cx, int cy, html_content *content)
Recursively layout and position absolutely positioned boxes.
Definition: layout.c:4967
static bool layout_float(struct box *b, int width, html_content *content)
Layout the contents of a float or inline block.
Definition: layout.c:2592
static void layout_position_relative(const css_unit_ctx *unit_len_ctx, struct box *root, struct box *fp, int fx, int fy)
Adjust positions of relatively positioned boxes.
Definition: layout.c:5115
static bool layout_block_object(struct box *block)
Layout a block which contains an object.
Definition: layout.c:2294
bool layout_block_context(struct box *block, int viewport_height, html_content *content)
Layout a block formatting context.
Definition: layout.c:3531
static void layout_compute_offsets(const css_unit_ctx *unit_len_ctx, struct box *box, struct box *containing_block, int *top, int *right, int *bottom, int *left)
Compute box offsets for a relatively or absolutely positioned box with respect to a box.
Definition: layout.c:4492
static int line_height(const css_unit_ctx *unit_len_ctx, const css_computed_style *style)
Calculate line height from a style.
Definition: layout.c:2677
const css_border_style_func border_style_funcs[4]
Array of per-side access functions for computed style border styles.
Definition: layout.c:98
static void layout_get_box_bbox(const css_unit_ctx *unit_len_ctx, struct box *box, int *desc_x0, int *desc_y0, int *desc_x1, int *desc_y1)
Find a box's bounding box relative to itself, i.e.
Definition: layout.c:5218
static void layout_get_object_dimensions(struct box *box, int *width, int *height, int min_width, int max_width, int min_height, int max_height)
Compute the size of replaced boxes with auto dimensions, according to content.
Definition: layout.c:134
static bool layout__list_item_is_numerical(const struct box *b)
Find out if box's style represents a numerical list style type.
Definition: layout.c:4398
static bool layout_line(struct box *first, int *width, int *y, int cx, int cy, struct box *cont, bool indent, bool has_text_children, html_content *content, struct box **next_box)
Position a line of boxes in inline formatting context.
Definition: layout.c:2730
static void layout_compute_relative_offset(const css_unit_ctx *unit_len_ctx, struct box *box, int *x, int *y)
Compute a box's relative offset as per CSS 2.1 9.4.3.
Definition: layout.c:5034
static void layout__ordered_list_count(struct box *box)
Handle list item counting, if this is a list owner box.
Definition: layout.c:4262
static void layout__set_numerical_marker_text(const html_content *content, struct box *box)
Set up the marker text for a numerical list item.
Definition: layout.c:4348
static void place_float_below(struct box *c, int width, int cx, int y, struct box *cont)
Position a float in the first available space.
Definition: layout.c:2633
bool layout_table(struct box *table, int available_width, html_content *content)
Layout a table.
Definition: layout.c:1604
static bool box_has_percentage_max_width(struct box *b)
Helper to check if a box has percentage max width.
Definition: layout.c:444
static bool layout_inline_container(struct box *inline_container, int width, struct box *cont, int cx, int cy, html_content *content)
Layout lines of text or inline boxes with floats.
Definition: layout.c:3466
static void add_float_to_container(struct box *cont, struct box *b)
Insert a float into a container.
Definition: layout.c:2328
static bool layout__get_list_item_count(dom_node *list_owner, dom_long *count_out)
Get the number of list items for a list owner.
Definition: layout.c:4196
static int layout_text_indent(const css_unit_ctx *unit_len_ctx, const css_computed_style *style, int width)
Calculate the text-indent length.
Definition: layout.c:230
const css_border_color_func border_color_funcs[4]
Array of per-side access functions for computed style border colors.
Definition: layout.c:106
static void layout_block_find_dimensions(const css_unit_ctx *unit_len_ctx, int available_width, int viewport_height, int lm, int rm, struct box *box)
Compute dimensions of box, margins, paddings, and borders for a block-level element.
Definition: layout.c:1497
static bool layout__get_ol_start(dom_node *ol_node, dom_long *start_out)
Helper to get start attribute value from a OL node.
Definition: layout.c:4137
static void layout_calculate_descendant_bboxes(const css_unit_ctx *unit_len_ctx, struct box *box)
Recursively calculate the descendant_[xy][01] values for a laid-out box tree and inform iframe browse...
Definition: layout.c:5326
const css_len_func border_width_funcs[4]
Array of per-side access functions for computed style border_widths.
Definition: layout.c:90
static struct box * layout_minmax_line(struct box *first, int *line_min, int *line_max, bool first_line, bool *line_has_height, const struct gui_layout_table *font_func, const html_content *content)
Calculate minimum and maximum width of a line.
Definition: layout.c:469
static void layout_minmax_block(struct box *block, const struct gui_layout_table *font_func, const html_content *content)
Calculate minimum and maximum width of a block.
Definition: layout.c:872
bool layout_document(html_content *content, int width, int height)
Calculate positions of boxes in a document.
Definition: layout.c:5419
static void find_sides(struct box *fl, int y0, int y1, int *x0, int *x1, struct box **left, struct box **right)
Find left and right edges in a vertical range.
Definition: layout.c:1298
static struct box * layout_next_margin_block(const css_unit_ctx *unit_len_ctx, struct box *box, struct box *block, int viewport_height, int *max_pos_margin, int *max_neg_margin)
Find next block that current margin collapses to.
Definition: layout.c:1152
static bool layout_text_box_split(html_content *content, plot_font_style_t *fstyle, struct box *split_box, size_t new_length, int new_width)
Split a text box.
Definition: layout.c:2372
const css_len_func padding_funcs[4]
Array of per-side access functions for computed style paddings.
Definition: layout.c:82
static void layout_float_find_dimensions(const css_unit_ctx *unit_len_ctx, int available_width, const css_computed_style *style, struct box *box)
Compute dimensions of box, margins, paddings, and borders for a floating element using shrink-to-fit.
Definition: layout.c:2454
static void layout_minmax_inline_container(struct box *inline_container, bool *has_height, const struct gui_layout_table *font_func, const html_content *content)
Calculate minimum and maximum width of an inline container.
Definition: layout.c:824
static void layout_update_descendant_bbox(const css_unit_ctx *unit_len_ctx, struct box *box, struct box *child, int off_x, int off_y)
Apply changes to box descendant_[xy][01] values due to given child.
Definition: layout.c:5260
static int layout_clear(struct box *fl, enum css_clear_e clear)
Find y coordinate which clears all floats on left and/or right.
Definition: layout.c:1269
static int layout_solve_width(struct box *box, int available_width, int width, int lm, int rm, int max_width, int min_width)
Solve the width constraint as given in CSS 2.1 section 10.3.3.
Definition: layout.c:1359
static bool layout_apply_minmax_height(const css_unit_ctx *unit_len_ctx, struct box *box, struct box *container)
Manimpulate box height according to CSS min-height and max-height properties.
Definition: layout.c:2181
static bool layout__check_element_type(const dom_node *node, dom_html_element_type type)
Check a node's tag type.
Definition: layout.c:4073
static bool layout_absolute(struct box *box, struct box *containing_block, int cx, int cy, html_content *content)
Layout and position an absolutely positioned box.
Definition: layout.c:4581
const css_len_func margin_funcs[4]
Array of per-side access functions for computed style margins.
Definition: layout.c:74
static void layout_lists(const html_content *content, struct box *box)
Layout list markers.
Definition: layout.c:4419
static bool layout__get_element_tag(const dom_node *node, dom_html_element_type *type)
Get a dom node's element tag type.
Definition: layout.c:4041
interface to HTML layout.
nsurl * content_get_url(struct content *c)
Retrieve URL associated with content.
Definition: content.c:1051
bool content_can_reformat(hlcache_handle *h)
Get whether a content can reformat.
Definition: content.c:258
void content_reformat(hlcache_handle *h, bool background, int width, int height)
Reformat to new size.
Definition: content.c:321
int content_get_available_width(hlcache_handle *h)
Retrieve available width of content.
Definition: content.c:1192
Protected interface to Content handling.
@ CONTENT_HTML
content is HTML
Definition: content_type.h:58
Useful interned string pointers (interface).
void textarea_set_layout(struct textarea *ta, const plot_font_style_t *fstyle, int width, int height, int top, int right, int bottom, int left)
Set the dimensions and padding of a textarea.
Definition: textarea.c:3336
Single/Multi-line UTF-8 text area interface.
static struct directory * root
Definition: filename.c:55
const char * type
Definition: filetype.cpp:44
Interface to form handling functions internal to HTML content handler.
@ GADGET_TEXTAREA
Definition: form_internal.h:53
@ GADGET_PASSWORD
Definition: form_internal.h:55
@ GADGET_CHECKBOX
Definition: form_internal.h:51
@ GADGET_FILE
Definition: form_internal.h:58
@ GADGET_SELECT
Definition: form_internal.h:52
@ GADGET_TEXTBOX
Definition: form_internal.h:49
@ GADGET_RADIO
Definition: form_internal.h:50
struct box * html_get_box_tree(hlcache_handle *h)
Retrieve box tree.
Definition: html.c:1971
Interface to text/html content handler.
Interface to HTML content handler to save documents.
Public content interface.
struct nsurl * hlcache_handle_get_url(const struct hlcache_handle *handle)
Retrieve the URL associated with a high level cache handle.
int content_get_height(struct hlcache_handle *h)
Retrieve height of content.
Definition: content.c:1175
int content_get_width(struct hlcache_handle *h)
Retrieve width of content.
Definition: content.c:1158
content_type content_get_type(struct hlcache_handle *h)
Retrieve computed type of content.
Definition: content.c:1061
Interface to platform-specific layout operation table.
Netsurf additional integer type formatting macros.
#define PRIsizet
c99 standard printf formatting for size_t type
Definition: inttypes.h:53
bool layout_flex(struct box *flex, int available_width, html_content *content)
Layout a flex container.
Definition: layout_flex.c:1046
HTML layout private interface.
#define FPCT_OF_INT_TOINT(a, b)
static bool lh__flex_main_is_horizontal(const struct box *flex)
#define AUTO
static bool lh__box_is_object(const struct box *b)
Layout helper: Check whether box is an object.
uint8_t(* css_border_style_func)(const css_computed_style *style)
static bool lh__box_is_flex_container(const struct box *b)
Layout helper: Check whether box takes part in inline flow.
static bool lh__box_is_replace(const struct box *b)
Layout helper: Check whether box is replaced.
static void calculate_mbp_width(const css_unit_ctx *unit_len_ctx, const css_computed_style *style, unsigned int side, bool margin, bool border, bool padding, int *fixed, float *frac)
Determine width of margin, borders, and padding on one side of a box.
uint8_t(* css_len_func)(const css_computed_style *style, css_fixed *length, css_unit *unit)
static bool lh__box_is_inline_content(const struct box *b)
Layout helper: Check whether box is inline level.
static void layout_find_dimensions(const css_unit_ctx *unit_len_ctx, int available_width, int viewport_height, const struct box *box, const css_computed_style *style, int *width, int *height, int *max_width, int *min_width, int *max_height, int *min_height, int margin[4], int padding[4], struct box_border border[4])
Calculate width, height, and thickness of margins, paddings, and borders.
static bool lh__box_is_flex_item(const struct box *b)
Layout helper: Check whether box takes part in inline flow.
static bool lh__box_is_inline_flow(const struct box *b)
Layout helper: Check whether box takes part in inline flow.
uint8_t(* css_border_color_func)(const css_computed_style *style, css_color *color)
static bool lh__box_is_float_box(const struct box *b)
Layout helper: Check whether box is a float.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
NetSurf URL handling (interface).
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
#define NS_TRANSPARENT
Transparent colour value.
Definition: plot_style.h:39
Private data for text/html content.
int width
Definition: gui.c:159
int height
Definition: gui.c:160
Scrollbar widget interface.
#define SCROLLBAR_WIDTH
Definition: scrollbar.h:32
Interface to utility string handling.
Container for border values during table border calculations.
Definition: table.c:42
css_fixed width
border-width length
Definition: table.c:46
Container for box border details.
Definition: box.h:104
enum css_border_style_e style
border-style
Definition: box.h:105
css_color c
border-color value
Definition: box.h:106
int width
border-width (pixels)
Definition: box.h:107
Node in box tree.
Definition: box.h:177
int descendant_y1
bottom edge of descendants
Definition: box.h:312
struct box_border border[4]
Border: TOP, RIGHT, BOTTOM, LEFT.
Definition: box.h:327
int list_value
List item value.
Definition: box.h:412
int min_width
Width of box taking all line breaks (including margins etc).
Definition: box.h:343
int width
Width of content box (excluding padding etc.).
Definition: box.h:289
struct box * parent
Parent box, or NULL.
Definition: box.h:236
struct column * col
Array of table column data for TABLE only.
Definition: box.h:407
struct box * inline_end
INLINE_END box corresponding to this INLINE box, or INLINE box corresponding to this INLINE_END box.
Definition: box.h:242
struct box * children
First child box, or NULL.
Definition: box.h:226
int height
Height of content box (excluding padding etc.).
Definition: box.h:293
struct box * float_container
If box is a float, points to box's containing block.
Definition: box.h:261
struct box * prev
Previous sibling box, or NULL.
Definition: box.h:221
struct box * list_marker
List marker box if this is a list-item, or NULL.
Definition: box.h:417
int margin[4]
Margin: TOP, RIGHT, BOTTOM, LEFT.
Definition: box.h:317
int max_width
Width that would be taken with no line breaks.
Definition: box.h:349
int clear_level
Level below which subsequent floats must be cleared.
Definition: box.h:267
struct box * next_float
Next sibling float box.
Definition: box.h:256
struct box * last
Last child box, or NULL.
Definition: box.h:231
struct box * next
Next sibling box, or NULL.
Definition: box.h:216
int descendant_x0
left edge of descendants
Definition: box.h:309
unsigned int start_column
Start column for TABLE_CELL only.
Definition: box.h:402
box_type type
Type of box.
Definition: box.h:181
struct box * float_children
First float child box, or NULL.
Definition: box.h:251
int descendant_x1
right edge of descendants
Definition: box.h:311
struct browser_window * iframe
Iframe's browser_window, or NULL if none.
Definition: box.h:452
css_computed_style * style
Style for this box.
Definition: box.h:205
size_t length
Length of text.
Definition: box.h:360
struct hlcache_handle * object
Object in this box (usually an image), or NULL if none.
Definition: box.h:441
char * text
Text, or NULL if none.
Definition: box.h:355
int padding[4]
Padding: TOP, RIGHT, BOTTOM, LEFT.
Definition: box.h:322
int x
Coordinate of left padding edge relative to parent box, or relative to ancestor that contains this bo...
Definition: box.h:280
box_flags flags
Box flags.
Definition: box.h:186
int space
Width of space after current text (depends on font and size).
Definition: box.h:365
int cached_place_below_level
Level below which floats have been placed.
Definition: box.h:272
struct form_control * gadget
Form control data, or NULL if not a form control.
Definition: box.h:423
int descendant_y0
top edge of descendants
Definition: box.h:310
unsigned int rows
Number of rows for TABLE only.
Definition: box.h:397
struct dom_node * node
DOM node that generated this box or NULL.
Definition: box.h:191
unsigned int columns
Number of columns for TABLE / TABLE_CELL.
Definition: box.h:392
int y
Coordinate of top padding edge, relative as for x.
Definition: box.h:284
Table column data.
Definition: box.h:114
@ COLUMN_WIDTH_FIXED
Definition: box.h:120
@ COLUMN_WIDTH_PERCENT
Definition: box.h:122
@ COLUMN_WIDTH_RELATIVE
Definition: box.h:123
bool positioned
Whether all of column's cells are css positioned.
Definition: box.h:145
int width
Preferred width of column.
Definition: box.h:130
int max
Maximum width of content.
Definition: box.h:140
enum column::@131 type
Type of column.
int min
Minimum width of content.
Definition: box.h:135
Column record for a table.
Definition: box_normalise.c:59
Content which corresponds to a single URL.
int width
Width dimension, if applicable.
struct form_textarea_data data
struct form_control::@132::@134 select
form_control_type type
Type of control.
Definition: form_internal.h:79
struct form_option * items
Option in a select.
Definition: form.h:33
char * value
Definition: form.h:37
struct form_option * next
Definition: form.h:39
char * text
NUL terminated.
Definition: form.h:38
nserror(* width)(const struct plot_font_style *fstyle, const char *string, size_t length, int *width)
Measure the width of a string.
Definition: layout.h:49
nserror(* split)(const struct plot_font_style *fstyle, const char *string, size_t length, int x, size_t *char_offset, int *actual_x)
Find where to split a string to make it fit a width.
Definition: layout.h:88
Data specific to CONTENT_HTML.
Definition: private.h:93
Font style for plotting.
Definition: plot_style.h:111
colour background
Background colour to blend to, if appropriate.
Definition: plot_style.h:122
bool table_calculate_column_types(const css_unit_ctx *unit_len_ctx, struct box *table)
Determine the column width types for a table.
Definition: table.c:813
void table_used_border_for_cell(const css_unit_ctx *unit_len_ctx, struct box *cell)
Calculate used values of border-{trbl}-{style,color,width} for table cells.
Definition: table.c:988
Interface to HTML table processing and layout.
#define talloc_realloc(ctx, p, type, count)
Definition: talloc.h:99
#define talloc_array(ctx, type, count)
Definition: talloc.h:95
#define talloc_memdup(t, p, size)
Definition: talloc.h:102
Option reading and saving interface.
#define nsoption_bool(OPTION)
Get the value of a boolean option.
Definition: nsoption.h:270
Interface to a number of general purpose functionality.
#define min(x, y)
Definition: utils.h:46
#define max(x, y)
Definition: utils.h:50