MyreMylar / pygame_gui

A GUI system for pygame.
MIT License
698 stars 83 forks source link

HTML Size Tagging #248

Closed Saccharine-Coal closed 2 years ago

Saccharine-Coal commented 2 years ago

Submitting a new issue as I accidently wrote my issue in pygame_gui_examples instead of this repository.

Is your feature request related to a problem? Please describe. I have been attempting to use HTML supported tags as enumerated in the documentation. However, when using the HTML size tagging: eg. <font face='verdana' color='#000000' size=3.5></font> the documentation fails to state that only discrete sizes are allowed as written in the html parser source code. Thus, when size values are entered that are not 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7 a key error occurs: eg. <font face='verdana' color='#000000' size=3.6></font> Results in a key error.

Describe the solution you'd like I am unsure if this is intended, but I only realized that only discrete values are accepted after looking at the source code. More explicit value handling would be helpful, or perhaps allow dynamic html font sizing, similar to the dynamic pygame font sizing that already exists. Html formatting is great for detailed text customization, but this issue makes it more difficult to use since it is not obvious, at least to me, what causes this issue during development.

Additional context Version: pygame 2.1.0 (SDL 2.0.16, Python 3.9.9) pygame_gui is 0.6.0

MyreMylar commented 2 years ago

I will add a better error for the unsupported html style sizes. I will probably also add an alternative pixel_size param that just lets you directly set the pygame/freetype font size without the html style conversion. I believe the half-point sizes were and html convention at some point which is why I adopted them and mapped them to actual pixel sizes - fractional sizes don't really make that much sense for fonts in general because they have to rasterize to an actual pixel size at some point and there aren't fractional pixels.

Saccharine-Coal commented 2 years ago

I gathered that the HTML font sizes likely carried over from HTML itself.

Direct pygame "font size" assignment would be great for HTML text! I encountered this problem while using live theming, which is awesome, to modify text size on all elements during runtime. From my understanding, HTML "font size" and pygame "font size" are two unrelated values in the context of element theming. A value that can set both values pygame and HMTL "font size" would allow for more consistent font sizing across all elements.

I have a couple of suggestions from my experience using this package. If possible, an HTML tag like the current tag <font size=3.5> for HTML "font size" that directly references the pygame "font size" could be a solution. HTML tag formatting seems like a more user-friendly approach when compared to object ID's so this would be a great solution in my opinion. ie. <font pygame_size=4> would directly reference the pygame fonts which are only ints.

Or perhaps handle this with object ID's? If an HTML UI element receives an object ID with set font size, then set the size of the HTML text to be the set pygame font size. I believe you alluded to this with a potential pixel_size parameter. eg. in the theme.json file

{
    "@my_element": 
    {
        "font": 
        {
            "name": "MyFont",
            "size": "24",
            "bold": "0",
            "italic": "0"
        }
    }
}
MyreMylar commented 2 years ago

In 0.6.2 I've added 'pixel_size' as an option for the font tag letting you directly set the pygame font size - and you should get a helpful warning message if you try to set a unsupported 'size' value now.