|
8 | 8 | from pyglet.text import LinearGradient |
9 | 9 | from pyglet.text.caret import Caret |
10 | 10 | from pyglet.text.document import AbstractDocument |
| 11 | +from pyglet.text.formats.html import HTMLDecoder |
11 | 12 | from typing_extensions import override |
12 | 13 |
|
13 | 14 | import arcade |
|
31 | 32 | from arcade.gui.surface import Surface |
32 | 33 | from arcade.gui.widgets import UIInteractiveWidget, UIWidget |
33 | 34 | from arcade.gui.widgets.layout import UIAnchorLayout |
34 | | -from arcade.text import FontNameOrNames, _to_text_color |
| 35 | +from arcade.text import FontNameOrNames, _to_text_color, _to_weight |
35 | 36 | from arcade.types import LBWH, RGBA255, Color, RGBOrA255 |
36 | 37 |
|
37 | 38 |
|
@@ -825,6 +826,8 @@ class UITextArea(UIWidget): |
825 | 826 | PLAIN will decode the text as plain text, ATTRIBUTED and HTML will |
826 | 827 | decode the text as pyglet documents here |
827 | 828 | https://pyglet.readthedocs.io/en/latest/programming_guide/text.html |
| 829 | + With ATTRIBUTED and HTML, styles set in the text win, and the font, |
| 830 | + color, bold and italic arguments apply to the rest of it. |
828 | 831 | **kwargs: passed to :py:class:`~arcade.gui.UIWidget`. |
829 | 832 | """ |
830 | 833 |
|
@@ -864,25 +867,36 @@ def __init__( |
864 | 867 | # Measured in pixels per 'click' |
865 | 868 | self.scroll_speed = scroll_speed if scroll_speed is not None else font_size |
866 | 869 |
|
| 870 | + # pyglet 3 reads "weight" and "style", not "bold" and "italic" |
| 871 | + text_style = dict( |
| 872 | + font_name=font_name, |
| 873 | + font_size=font_size, |
| 874 | + color=Color.from_iterable(text_color), |
| 875 | + weight=_to_weight(bold), |
| 876 | + style="italic" if italic else "normal", |
| 877 | + ) |
| 878 | + |
867 | 879 | self.doc: AbstractDocument |
868 | 880 | if document_mode == "PLAIN": |
869 | 881 | self.doc = pyglet.text.decode_text(text) |
870 | | - elif document_mode == "ATTRIBUTED": |
871 | | - self.doc = pyglet.text.decode_attributed(text) |
872 | | - elif document_mode == "HTML": |
873 | | - self.doc = pyglet.text.decode_html(text) |
874 | | - |
875 | | - self.doc.set_style( |
876 | | - 0, |
877 | | - len(text), |
878 | | - dict( |
879 | | - font_name=font_name, |
880 | | - font_size=font_size, |
881 | | - color=Color.from_iterable(text_color), |
882 | | - bold=bold, |
883 | | - italic=italic, |
884 | | - ), |
885 | | - ) |
| 882 | + self.doc.set_style(0, len(self.doc.text), text_style) |
| 883 | + else: |
| 884 | + if document_mode == "ATTRIBUTED": |
| 885 | + self.doc = pyglet.text.decode_attributed(text) |
| 886 | + elif document_mode == "HTML": |
| 887 | + # The decoder's own defaults (such as Times New Roman) would |
| 888 | + # otherwise apply wherever the HTML doesn't set a style |
| 889 | + class _Decoder(HTMLDecoder): |
| 890 | + default_style = {**HTMLDecoder.default_style, **text_style} |
| 891 | + |
| 892 | + self.doc = _Decoder().decode(text) |
| 893 | + else: |
| 894 | + raise ValueError(f"Unknown document_mode: {document_mode!r}") |
| 895 | + # Styles set in the text win. Only fill in the rest. |
| 896 | + for attribute, value in text_style.items(): |
| 897 | + runs = self.doc.get_style_runs(attribute).ranges(0, len(self.doc.text)) |
| 898 | + for start, end in [(start, end) for start, end, run in runs if run is None]: |
| 899 | + self.doc.set_style(start, end, {attribute: value}) |
886 | 900 |
|
887 | 901 | self.layout = pyglet.text.layout.IncrementalTextLayout( |
888 | 902 | self.doc, |
|
0 commit comments