From bfcca39e24c3f24f023dd609daace7d4d1879452 Mon Sep 17 00:00:00 2001 From: Yuri Kobets Date: Tue, 10 Jun 2025 01:43:14 +0300 Subject: [PATCH 1/2] refactor: simplify document size calculation by removing content size handling document::width() and document::height() can be used in all cases to calculate the document size. --- containers/cairo/render2png.cpp | 4 ++-- include/litehtml/document.h | 3 --- include/litehtml/render_item.h | 2 +- src/document.cpp | 15 +-------------- src/render_item.cpp | 20 ++++---------------- 5 files changed, 8 insertions(+), 36 deletions(-) diff --git a/containers/cairo/render2png.cpp b/containers/cairo/render2png.cpp index bd6e7bb8..79b5e832 100644 --- a/containers/cairo/render2png.cpp +++ b/containers/cairo/render2png.cpp @@ -219,8 +219,8 @@ namespace html2png std::swap(m_screen_width, best_width); } - width = cfg.get_int("width", doc->content_width() > 0 ? doc->content_width() : 1); - height = cfg.get_int("height", doc->content_height() > 0 ? doc->content_height() : 1); + width = cfg.get_int("width", doc->width() > 0 ? doc->width() : 1); + height = cfg.get_int("height", doc->height() > 0 ? doc->height() : 1); auto surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height); if(surface) diff --git a/include/litehtml/document.h b/include/litehtml/document.h index 9be84654..0898b444 100644 --- a/include/litehtml/document.h +++ b/include/litehtml/document.h @@ -59,7 +59,6 @@ namespace litehtml litehtml::css m_master_css; litehtml::css m_user_css; litehtml::size m_size; - litehtml::size m_content_size; position::vector m_fixed_boxes; std::shared_ptr m_over_element; std::shared_ptr m_active_element; @@ -84,8 +83,6 @@ namespace litehtml int to_pixels(const css_length& val, const font_metrics& metrics, int size) const; int width() const; int height() const; - int content_width() const; - int content_height() const; void add_stylesheet(const char* str, const char* baseurl, const char* media); bool on_mouse_over(int x, int y, int client_x, int client_y, position::vector& redraw_boxes); bool on_lbutton_down(int x, int y, int client_x, int client_y, position::vector& redraw_boxes); diff --git a/include/litehtml/render_item.h b/include/litehtml/render_item.h index fedc1f38..0a52b143 100644 --- a/include/litehtml/render_item.h +++ b/include/litehtml/render_item.h @@ -412,7 +412,7 @@ namespace litehtml std::tuple element_static_offset(const std::shared_ptr &el); void add_positioned(const std::shared_ptr &el); void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0); - void calc_document_size( litehtml::size& sz, litehtml::size& content_size, int x = 0, int y = 0 ); + void calc_document_size( litehtml::size& sz, int x = 0, int y = 0 ); virtual void get_inline_boxes( position::vector& /*boxes*/ ) const {}; virtual void set_inline_boxes( position::vector& /*boxes*/ ) {}; virtual void add_inline_box( const position& /*box*/ ) {}; diff --git a/src/document.cpp b/src/document.cpp index d4989140..7e1b7519 100644 --- a/src/document.cpp +++ b/src/document.cpp @@ -512,9 +512,7 @@ int document::render( int max_width, render_type rt ) } m_size.width = 0; m_size.height = 0; - m_content_size.width = 0; - m_content_size.height = 0; - m_root_render->calc_document_size(m_size, m_content_size); + m_root_render->calc_document_size(m_size); } } return ret; @@ -612,17 +610,6 @@ int document::height() const return m_size.height; } -int document::content_width() const -{ - return m_content_size.width; -} - -int document::content_height() const -{ - return m_content_size.height; -} - - void document::add_stylesheet( const char* str, const char* baseurl, const char* media ) { if(str && str[0]) diff --git a/src/render_item.cpp b/src/render_item.cpp index 3794807a..7292373a 100644 --- a/src/render_item.cpp +++ b/src/render_item.cpp @@ -707,7 +707,7 @@ void litehtml::render_item::get_redraw_box(litehtml::position& pos, int x /*= 0* } } -void litehtml::render_item::calc_document_size( litehtml::size& sz, litehtml::size& content_size, int x /*= 0*/, int y /*= 0*/ ) +void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0*/, int y /*= 0*/ ) { if(css().get_display() != display_inline && css().get_display() != display_table_row) { @@ -716,27 +716,15 @@ void litehtml::render_item::calc_document_size( litehtml::size& sz, litehtml::si sz.width = std::max(sz.width, x + right()); sz.height = std::max(sz.height, y + bottom()); - if (!src_el()->is_root() && !src_el()->is_body()) - { - content_size.width = std::max(content_size.width, x + right()); - content_size.height = std::max(content_size.height, y + bottom()); - } - // All children of tables and blocks with style other than "overflow: visible" are inside element. // We can skip calculating size of children if (src_el()->css().get_overflow() == overflow_visible && src_el()->css().get_display() != display_table) { for (auto &el: m_children) { - el->calc_document_size(sz, content_size, x + m_pos.x, y + m_pos.y); + el->calc_document_size(sz, x + m_pos.x, y + m_pos.y); } } - - if (src_el()->is_root() || src_el()->is_body()) - { - content_size.width = std::max(content_size.width, x + right()); - content_size.height = std::max(content_size.height, y + bottom()); - } } } else { @@ -744,8 +732,8 @@ void litehtml::render_item::calc_document_size( litehtml::size& sz, litehtml::si get_inline_boxes(boxes); for(auto& box : boxes) { - content_size.width = std::max(content_size.width, x + box.x + box.width); - content_size.height = std::max(content_size.height, y + box.y + box.height); + sz.width = std::max(sz.width, x + box.x + box.width); + sz.height = std::max(sz.height, y + box.y + box.height); } } } From 6f36dd247277a98a3eaa82c65efc112044c19898 Mon Sep 17 00:00:00 2001 From: Yuri Kobets Date: Tue, 10 Jun 2025 01:46:11 +0300 Subject: [PATCH 2/2] Add const qualifier to calc_document_size method --- include/litehtml/render_item.h | 2 +- src/render_item.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/litehtml/render_item.h b/include/litehtml/render_item.h index 0a52b143..21c943b4 100644 --- a/include/litehtml/render_item.h +++ b/include/litehtml/render_item.h @@ -412,7 +412,7 @@ namespace litehtml std::tuple element_static_offset(const std::shared_ptr &el); void add_positioned(const std::shared_ptr &el); void get_redraw_box(litehtml::position& pos, int x = 0, int y = 0); - void calc_document_size( litehtml::size& sz, int x = 0, int y = 0 ); + void calc_document_size( litehtml::size& sz, int x = 0, int y = 0 ) const; virtual void get_inline_boxes( position::vector& /*boxes*/ ) const {}; virtual void set_inline_boxes( position::vector& /*boxes*/ ) {}; virtual void add_inline_box( const position& /*box*/ ) {}; diff --git a/src/render_item.cpp b/src/render_item.cpp index 7292373a..0362685e 100644 --- a/src/render_item.cpp +++ b/src/render_item.cpp @@ -707,7 +707,7 @@ void litehtml::render_item::get_redraw_box(litehtml::position& pos, int x /*= 0* } } -void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0*/, int y /*= 0*/ ) +void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0*/, int y /*= 0*/ ) const { if(css().get_display() != display_inline && css().get_display() != display_table_row) { @@ -717,7 +717,7 @@ void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0* sz.height = std::max(sz.height, y + bottom()); // All children of tables and blocks with style other than "overflow: visible" are inside element. - // We can skip calculating size of children + // We can skip calculating the size of children if (src_el()->css().get_overflow() == overflow_visible && src_el()->css().get_display() != display_table) { for (auto &el: m_children) @@ -730,7 +730,7 @@ void litehtml::render_item::calc_document_size( litehtml::size& sz, int x /*= 0* { position::vector boxes; get_inline_boxes(boxes); - for(auto& box : boxes) + for(const auto& box : boxes) { sz.width = std::max(sz.width, x + box.x + box.width); sz.height = std::max(sz.height, y + box.y + box.height);