261 const config& parsed_text,
263 const unsigned init_width,
267 DBG_GUI_RL <<
"Initial width: " << init_width;
271 unsigned prev_blk_height = origin.y;
272 unsigned text_height = 0;
280 std::vector<shape_ptr> shapes;
281 std::unique_ptr<gui2::text_shape> curr_item =
nullptr;
289 std::size_t first_area, last_area;
291 std::vector<item_link> item_links;
293 bool is_text =
false;
294 bool is_image =
false;
295 bool wrap_mode =
false;
296 bool new_text_block =
false;
299 point float_pos, float_size;
305 for(
auto it = children.begin(); it != children.end(); ++it) {
306 const auto [orig_key,
child] = *it;
308 const std::string key = (orig_key ==
"img" && !
child[
"float"].to_bool(
false)) ?
"inline_image" : orig_key;
310 DBG_GUI_RL <<
"\n Trying to layout tag: " << key;
313 prev_blk_height += text_height;
316 const std::string& align =
child[
"align"].str(
"left");
319 if (align ==
"right") {
320 float_pos.x = init_width - curr_img_size.x;
321 }
else if (align ==
"middle" || align ==
"center") {
323 float_pos.x = float_size.x + (init_width - curr_img_size.x)/2;
327 float_pos.y += float_size.y;
330 shapes.emplace_back(std::make_unique<image_shape>(
point{float_pos.x, pos.y + float_pos.y},
child[
"src"]));
332 float_size.x = curr_img_size.x +
padding_;
333 float_size.y += curr_img_size.y +
padding_;
335 x = ((align ==
"left") ? float_size.x : 0);
336 pos.x += ((align ==
"left") ? float_size.x : 0);
344 new_text_block =
true;
346 DBG_GUI_RL << key <<
": src=" <<
child[
"src"] <<
", size=" << img_size;
348 }
else if(key ==
"clear") {
351 prev_blk_height += float_size.y;
352 pos.y += float_size.y;
353 float_size =
point(0, 0);
358 }
else if(key ==
"table") {
359 if(curr_item ==
nullptr) {
361 new_text_block =
false;
365 img_size =
point(0,0);
366 float_size =
point(0,0);
368 prev_blk_height += text_height +
padding_;
373 unsigned col_idx = 0, row_idx = 0;
374 unsigned rows =
child.child_count(
"row");
375 unsigned columns = 1;
377 columns =
child.mandatory_child(
"row").child_count(
"col");
379 columns = (columns == 0) ? 1 : columns;
381 if(
child[
"width"] ==
"fill") {
382 init_cell_width = init_width/columns;
384 init_cell_width =
child[
"width"].to_int(init_width)/columns;
386 std::vector<int> col_widths(columns, 0);
387 std::vector<int> row_heights(rows, 0);
390 new_text_block =
true;
393 DBG_GUI_RL <<
"start table: " <<
"row=" << rows <<
" col=" << columns
394 <<
" width=" << init_cell_width*columns;
401 if(paddings.size() == 1) {
402 return std::array{ std::stoi(paddings[0]), std::stoi(paddings[0]) };
404 return std::array{ std::stoi(paddings[0]), std::stoi(paddings[1]) };
409 std::array<int, 2> row_paddings;
410 boost::multi_array<point, 2> cell_sizes(boost::extents[rows][columns]);
413 for(
const config& row :
child.child_range(
"row")) {
418 row_paddings = get_padding(row[
"padding"]);
420 pos.y += row_paddings[0];
421 for(
const config& col : row.child_range(
"col")) {
422 DBG_GUI_RL <<
"table cell origin (pre-layout): " << pos.x <<
", " << pos.y;
429 std::array<int, 2> col_paddings = get_padding(col[
"padding"]);
430 int cell_width = init_cell_width - col_paddings[0] - col_paddings[1];
432 pos.x += col_paddings[0];
435 cell_sizes[row_idx][col_idx] =
get_parsed_text(col_cfg, pos, init_cell_width).second;
439 row_heights[row_idx] = std::max(row_heights[row_idx], cell_sizes[row_idx][col_idx].y);
440 if(!
child[
"width"].empty()) {
441 col_widths[col_idx] = cell_width;
443 col_widths[col_idx] = std::max(col_widths[col_idx], cell_sizes[row_idx][col_idx].x);
444 if(
child[
"width"].empty()) {
445 col_widths[col_idx] = std::min(col_widths[col_idx], cell_width);
448 DBG_GUI_RL <<
"table row " << row_idx <<
" height: " << row_heights[row_idx]
449 <<
"col " << col_idx <<
" width: " << col_widths[col_idx];
452 pos.x += col_paddings[1];
456 pos.y += row_heights[row_idx] + row_paddings[1];
462 pos =
point(origin.x, prev_blk_height);
463 for(
const config& row :
child.child_range(
"row")) {
467 if(!row[
"bgcolor"].blank()) {
468 unsigned width = std::accumulate(col_widths.begin(), col_widths.end(), 0) + 2*(row_paddings[0] + row_paddings[1])*columns;
469 unsigned height = row_paddings[0] + row_heights[row_idx] + row_paddings[1];
470 shapes.emplace_back(std::make_unique<rectangle_shape>(
471 rect{origin.x, pos.y,
static_cast<int>(width),
static_cast<int>(height)},
475 row_paddings = get_padding(row[
"padding"]);
476 pos.y += row_paddings[0];
478 for(
const config& col : row.child_range(
"col")) {
479 DBG_GUI_RL <<
"table row " << row_idx <<
" height: " << row_heights[row_idx]
480 <<
"col " << col_idx <<
" width: " << col_widths[col_idx];
489 std::array<int, 2> col_paddings = get_padding(col[
"padding"]);
491 pos.x += col_paddings[0];
493 const std::string& valign = row[
"valign"].str(
"center");
494 const std::string& halign = col[
"halign"].str(
"left");
498 if (valign ==
"center" || valign ==
"middle") {
499 text_pos.y += (row_heights[row_idx] - cell_sizes[row_idx][col_idx].y)/2;
500 }
else if (valign ==
"bottom") {
501 text_pos.y += row_heights[row_idx] - cell_sizes[row_idx][col_idx].y;
503 if (halign ==
"center" || halign ==
"middle") {
504 text_pos.x += (col_widths[col_idx] - cell_sizes[row_idx][col_idx].x)/2;
505 }
else if (halign ==
"right") {
506 text_pos.x += col_widths[col_idx] - cell_sizes[row_idx][col_idx].x;
510 std::vector<shape_ptr> table_shapes =
get_parsed_text(col_cfg, text_pos, col_widths[col_idx]).first;
511 std::move(table_shapes.begin(), table_shapes.end(), std::back_inserter(shapes));
512 pos.x += col_widths[col_idx];
513 pos.x += col_paddings[1];
515 if(!shapes.empty()) {
516 if(
auto* tshape =
dynamic_cast<text_shape*
>(shapes.back().get())) {
517 tshape->set_wrap_width(col_widths[col_idx]);
524 new_text_block =
true;
530 pos.y += row_heights[row_idx];
531 pos.y += row_paddings[1];
532 DBG_GUI_RL <<
"row height: " << row_heights[row_idx];
536 w = std::max(
w,
static_cast<unsigned>(pos.x));
537 prev_blk_height = pos.y;
547 std::string line =
child[
"text"];
549 if(!finalize && (line.empty() && key ==
"text")) {
553 if(curr_item ==
nullptr || new_text_block) {
554 if (new_text_block && curr_item !=
nullptr) {
555 shapes.emplace_back(std::move(curr_item));
558 curr_item =
new_text_shape(pos, init_width - (pos.x - origin.x) - float_size.x);
560 new_text_block =
false;
564 int tmp_h =
get_text_size(*curr_item, init_width - (x == 0 ? float_size.x : x)).y;
566 if(is_text && key ==
"text") {
573 if(key ==
"inline_image") {
579 curr_item->add_text(
"\u200b");
584 }
else if(key ==
"ref") {
586 const std::size_t first_area =
links_.size();
587 const auto [start, end] =
add_link(*curr_item, line,
child[
"dst"],
point(x + origin.x, prev_blk_height), float_size.x);
588 item_links.push_back({
child[
"dst"].str(), start, end, first_area,
links_.size() });
600 child_origin.y += prev_blk_height;
603 for(
auto&& shape : child_shapes) {
604 if(
auto* tshape =
dynamic_cast<text_shape*
>(shape.get())) {
605 const auto [start, end] = curr_item->add_text(tshape->get_text());
606 curr_item->add_attributes_from(*tshape, start);
607 curr_item->add_attribute(key,
"", start, end);
609 shapes.emplace_back(std::move(shape));
615 }
else if(key ==
"header" || key ==
"h") {
617 const auto [start, end] = curr_item->add_text(line);
618 curr_item->add_attribute(
"weight",
"heavy", start, end);
619 curr_item->add_attribute(
"color",
"white", start, end);
620 curr_item->add_attribute(
"size", std::to_string(
font::SIZE_TITLE - 2), start, end);
624 }
else if(key ==
"character_entity") {
626 line =
"&" +
child[
"name"].str() +
";";
628 const auto [start, end] = curr_item->add_text(line);
629 curr_item->add_attribute(
"face",
"monospace", start, end);
630 curr_item->add_attribute(
"color",
"red", start, end);
634 }
else if(key ==
"span" || key ==
"format") {
636 const auto [start, end] = curr_item->add_text(line);
640 for (
const auto& [key,
value] :
child.attribute_range()) {
642 curr_item->add_attribute(key,
value, start, end);
647 }
else if (key ==
"text") {
651 curr_item->add_text(line);
653 point text_size =
get_text_size(*curr_item, init_width - (x == 0 ? float_size.x : x));
663 const std::string full_text = curr_item->get_text();
664 const auto next = std::next(it);
665 const auto starts_new_block = [](
const auto& tag) {
666 const auto [tag_key, tag_child] = tag;
667 return tag_key ==
"table" || tag_key ==
"clear"
668 || (tag_key ==
"img" && tag_child[
"float"].to_bool(
false));
670 const bool block_ends = next == children.end() || starts_new_block(*next);
671 const bool line_complete = block_ends || (!full_text.empty() && full_text.back() ==
'\n');
673 if(wrap_mode && line_complete && (float_size.y > 0) && (text_size.y > float_size.y)) {
678 DBG_GUI_RL <<
"wrap around area: " << float_size;
681 const point first_origin(x + origin.x, prev_blk_height);
682 const int first_img_width = float_size.x;
686 curr_item->set_text(full_text.substr(0, len));
689 curr_item->set_wrap_width(init_width - float_size.x);
690 float_size =
point(0,0);
697 int ah =
get_text_size(*curr_item, init_width - float_size.x).y;
698 text_height = std::max(0,
static_cast<int>(text_height) + ah - tmp_h);
699 prev_blk_height += text_height;
700 pos =
point(origin.x, prev_blk_height);
702 DBG_GUI_RL <<
"wrap: " << prev_blk_height <<
"," << text_height;
712 shapes.emplace_back(std::move(curr_item));
717 const std::size_t start = curr_item->add_text(full_text.substr(len+1)).first;
719 curr_item->add_attributes_from(first_item, start, len+1);
724 const unsigned first_end =
utf8::size(std::string_view(full_text).substr(0, len));
725 const unsigned rest_start = first_end + 1;
726 for(
auto link = item_links.rbegin(); link != item_links.rend(); ++link) {
727 if(link->end > first_end) {
732 std::vector<item_link> rest_links;
733 for(
const item_link& link : item_links) {
734 if(link.end <= first_end) {
738 if(link.start < first_end) {
739 add_link_area(first_item, link.dest, link.start, first_end, first_origin, first_img_width);
742 if(link.end > rest_start) {
743 const unsigned link_start = std::max(link.start, rest_start) - rest_start;
744 const unsigned link_end = link.end - rest_start;
745 const std::size_t first_area =
links_.size();
746 add_link_area(*curr_item, link.dest, link_start, link_end, pos, 0);
747 rest_links.push_back({ link.dest, link_start, link_end, first_area,
links_.size() });
750 item_links = std::move(rest_links);
752 }
else if((float_size.y > 0) && (text_size.y < float_size.y)) {
756 pos.y += text_size.y;
760 float_size =
point(0,0);
769 w = std::max(
w, x +
static_cast<unsigned>(size.x));
771 text_height += size.y - tmp_h;
772 pos.y += size.y - tmp_h;
775 if(!is_image && !wrap_mode && img_size.y > 0) {
776 img_size =
point(0,0);
780 DBG_GUI_RL <<
"Prev block height: " << prev_blk_height <<
" Current text block height: " << text_height;
782 h = text_height + prev_blk_height;
787 #if LINK_DEBUG_BORDER
789 for(
const auto& entry :
links_) {
790 shapes.emplace_back(std::make_unique<rectangle_shape>(
791 rect{ entry.first.x, entry.first.y, entry.first.w, entry.first.h },
797 if(curr_item !=
nullptr) {
798 shapes.emplace_back(std::move(curr_item));
805 h = std::max(
static_cast<unsigned>(img_size.y),
h);
807 DBG_GUI_RL <<
"Width: " <<
w <<
" Height: " <<
h <<
" Origin: " << origin;
808 return { std::move(shapes),
point(
w,
h - origin.y) };