From e0f0a545e407254a8a450852e5b573e851778ffd Mon Sep 17 00:00:00 2001 From: Face <69168154+face-hh@users.noreply.github.com> Date: Thu, 31 Jul 2025 22:02:03 +0300 Subject: [PATCH] custom font (, font-[name]) --- README.md | 2 +- Scripts/B9/CSSParser.gd | 16 ++++- Scripts/B9/HTMLParser.gd | 15 ++++- Scripts/Constants.gd | 19 ++++-- Scripts/FontManager.gd | 91 ++++++++++++++++++++++++++ Scripts/FontManager.gd.uid | 1 + Scripts/StyleManager.gd | 31 ++++++++- Scripts/Tags/font.gd.uid | 1 + Scripts/Utils/UtilityClassValidator.gd | 4 +- Scripts/main.gd | 29 ++++++++ 10 files changed, 198 insertions(+), 11 deletions(-) create mode 100644 Scripts/FontManager.gd create mode 100644 Scripts/FontManager.gd.uid create mode 100644 Scripts/Tags/font.gd.uid diff --git a/README.md b/README.md index 9787971..afe4251 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Issues: Notes: - **< input />** is sort-of inline in normal web. We render it as a block element (new-line). - A single `RichTextLabel` for inline text tags should stop, we should use invididual ones so it's easier to style and achieve separation through a `vboxcontainer`. - +- Fonts use **Flash of Unstyled Text (FOUT)** as opposed to **Flash of Invisible Text (FOIT)**, meaning the text with custom fonts will render with a generic font (sans-serif) while the custom ones downloads. Supported styles: diff --git a/Scripts/B9/CSSParser.gd b/Scripts/B9/CSSParser.gd index eaa6296..3d8a294 100644 --- a/Scripts/B9/CSSParser.gd +++ b/Scripts/B9/CSSParser.gd @@ -238,10 +238,24 @@ static func parse_utility_class_internal(rule: CSSRule, utility_name: String) -> rule.properties["font-bold"] = true return - # Handle font mono + # Handle font family + if utility_name == "font-sans": + rule.properties["font-family"] = "sans-serif" + return + if utility_name == "font-serif": + rule.properties["font-family"] = "serif" + return if utility_name == "font-mono": + rule.properties["font-family"] = "monospace" rule.properties["font-mono"] = true return + + var reserved_font_styles = ["font-sans", "font-serif", "font-mono", "font-bold", "font-italic"] + # Handle custom font families like font-roboto + if utility_name.begins_with("font-") and not utility_name in reserved_font_styles: + var font_name = utility_name.substr(5) # after 'font-' + rule.properties["font-family"] = font_name + return # Handle font style italic if utility_name == "font-italic": diff --git a/Scripts/B9/HTMLParser.gd b/Scripts/B9/HTMLParser.gd index fd6da2b..99278b4 100644 --- a/Scripts/B9/HTMLParser.gd +++ b/Scripts/B9/HTMLParser.gd @@ -12,8 +12,8 @@ class HTMLElement: func _init(tag: String = ""): tag_name = tag - func get_attribute(name_: String) -> String: - return attributes.get(name_, "") + func get_attribute(name_: String, default: String = "") -> String: + return attributes.get(name_, default) func has_attribute(name_: String) -> bool: return attributes.has(name_) @@ -293,6 +293,17 @@ func get_icon() -> String: var icon_element = find_first("icon") return icon_element.get_attribute("src") +func process_fonts() -> void: + var font_elements = find_all("font") + + for font_element in font_elements: + var name = font_element.get_attribute("name") + var src = font_element.get_attribute("src") + var weight = font_element.get_attribute("weight", "400") + + if name and src: + FontManager.register_font(name, src, weight) + func get_meta_content(name_: String) -> String: var meta_elements = find_all("meta", "name") for element in meta_elements: diff --git a/Scripts/Constants.gd b/Scripts/Constants.gd index b14432d..ecbc9aa 100644 --- a/Scripts/Constants.gd +++ b/Scripts/Constants.gd @@ -24,12 +24,14 @@ pre { text-xl font-mono } button { bg-[#1b1b1b] rounded-md text-white hover:bg-[#2a2a2a] active:bg-[#101010] } """ -var HTML_CONTENT = """ +var HTML_CONTENT2 = """ My Custom Dashboard + +