|
|
@ -55,9 +55,6 @@ class HtmlRenderer(Widget): |
|
|
|
super().__init__() |
|
|
|
super().__init__() |
|
|
|
self._text_color = text_color |
|
|
|
self._text_color = text_color |
|
|
|
|
|
|
|
|
|
|
|
self._total_height = 0.0 |
|
|
|
|
|
|
|
self._height_needs_recompute = True |
|
|
|
|
|
|
|
self._prev_content_width = -1 |
|
|
|
|
|
|
|
self._normal_font = gui_app.font(FontWeight.NORMAL) |
|
|
|
self._normal_font = gui_app.font(FontWeight.NORMAL) |
|
|
|
self._bold_font = gui_app.font(FontWeight.BOLD) |
|
|
|
self._bold_font = gui_app.font(FontWeight.BOLD) |
|
|
|
self._indent_level = 0 |
|
|
|
self._indent_level = 0 |
|
|
@ -92,7 +89,6 @@ class HtmlRenderer(Widget): |
|
|
|
|
|
|
|
|
|
|
|
def parse_html_content(self, html_content: str) -> None: |
|
|
|
def parse_html_content(self, html_content: str) -> None: |
|
|
|
self.elements.clear() |
|
|
|
self.elements.clear() |
|
|
|
self._height_needs_recompute = True |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Remove HTML comments |
|
|
|
# Remove HTML comments |
|
|
|
html_content = re.sub(r'<!--.*?-->', '', html_content, flags=re.DOTALL) |
|
|
|
html_content = re.sub(r'<!--.*?-->', '', html_content, flags=re.DOTALL) |
|
|
@ -196,32 +192,27 @@ class HtmlRenderer(Widget): |
|
|
|
return current_y - rect.y |
|
|
|
return current_y - rect.y |
|
|
|
|
|
|
|
|
|
|
|
def get_total_height(self, content_width: int) -> float: |
|
|
|
def get_total_height(self, content_width: int) -> float: |
|
|
|
if not self._height_needs_recompute and self._prev_content_width == content_width: |
|
|
|
total_height = 0.0 |
|
|
|
return self._total_height |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self._total_height = 0.0 |
|
|
|
|
|
|
|
padding = 20 |
|
|
|
padding = 20 |
|
|
|
usable_width = content_width - (padding * 2) |
|
|
|
usable_width = content_width - (padding * 2) |
|
|
|
|
|
|
|
|
|
|
|
for element in self.elements: |
|
|
|
for element in self.elements: |
|
|
|
if element.type == ElementType.BR: |
|
|
|
if element.type == ElementType.BR: |
|
|
|
self._total_height += element.margin_bottom |
|
|
|
total_height += element.margin_bottom |
|
|
|
continue |
|
|
|
continue |
|
|
|
|
|
|
|
|
|
|
|
self._total_height += element.margin_top |
|
|
|
total_height += element.margin_top |
|
|
|
|
|
|
|
|
|
|
|
if element.content: |
|
|
|
if element.content: |
|
|
|
font = self._get_font(element.font_weight) |
|
|
|
font = get_font(element.font_weight) |
|
|
|
wrapped_lines = wrap_text(font, element.content, element.font_size, int(usable_width)) |
|
|
|
wrapped_lines = wrap_text(font, element.content, element.font_size, int(usable_width)) |
|
|
|
|
|
|
|
|
|
|
|
for _ in wrapped_lines: |
|
|
|
for _ in wrapped_lines: |
|
|
|
self._total_height += element.font_size * element.line_height |
|
|
|
total_height += element.font_size * element.line_height |
|
|
|
|
|
|
|
|
|
|
|
self._total_height += element.margin_bottom |
|
|
|
total_height += element.margin_bottom |
|
|
|
|
|
|
|
|
|
|
|
self._prev_content_width = content_width |
|
|
|
return total_height |
|
|
|
self._height_needs_recompute = False |
|
|
|
|
|
|
|
return self._total_height |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_font(self, weight: FontWeight): |
|
|
|
def _get_font(self, weight: FontWeight): |
|
|
|
if weight == FontWeight.BOLD: |
|
|
|
if weight == FontWeight.BOLD: |
|
|
|