fix local font loading
This commit is contained in:
@@ -27,6 +27,10 @@ static func load_font(font_info: Dictionary) -> void:
|
||||
|
||||
if src.begins_with("http://") or src.begins_with("https://"):
|
||||
load_web_font(font_info)
|
||||
elif src.begins_with("gurt://"):
|
||||
load_gurt_font(font_info)
|
||||
else:
|
||||
load_local_font(font_info)
|
||||
|
||||
static func load_web_font(font_info: Dictionary) -> void:
|
||||
var src = font_info["src"]
|
||||
@@ -48,13 +52,8 @@ static func load_web_font(font_info: Dictionary) -> void:
|
||||
font_info["font_resource"] = font
|
||||
loaded_fonts[name] = font
|
||||
|
||||
# Trigger font refresh if callback is available
|
||||
if refresh_callback.is_valid():
|
||||
refresh_callback.call(name)
|
||||
else:
|
||||
print("FontManager: Empty font data received for ", name)
|
||||
else:
|
||||
print("FontManager: Failed to load font ", name, " - HTTP ", response_code)
|
||||
|
||||
if is_instance_valid(temp_parent):
|
||||
temp_parent.queue_free()
|
||||
@@ -65,6 +64,50 @@ static func load_web_font(font_info: Dictionary) -> void:
|
||||
|
||||
http_request.request(src, headers)
|
||||
|
||||
static func load_local_font(font_info: Dictionary) -> void:
|
||||
var src = font_info["src"]
|
||||
var name = font_info["name"]
|
||||
|
||||
if not FileAccess.file_exists(src):
|
||||
return
|
||||
|
||||
var file = FileAccess.open(src, FileAccess.READ)
|
||||
if file == null:
|
||||
return
|
||||
|
||||
var font_data = file.get_buffer(file.get_length())
|
||||
file.close()
|
||||
|
||||
if font_data.size() == 0:
|
||||
return
|
||||
|
||||
var font = FontFile.new()
|
||||
font.data = font_data
|
||||
|
||||
font_info["font_resource"] = font
|
||||
loaded_fonts[name] = font
|
||||
|
||||
if refresh_callback.is_valid():
|
||||
refresh_callback.call(name)
|
||||
|
||||
static func load_gurt_font(font_info: Dictionary) -> void:
|
||||
var src = font_info["src"]
|
||||
var name = font_info["name"]
|
||||
|
||||
var font_data = Network.fetch_gurt_resource(src, true)
|
||||
|
||||
if font_data.size() == 0:
|
||||
return
|
||||
|
||||
var font = FontFile.new()
|
||||
font.data = font_data
|
||||
|
||||
font_info["font_resource"] = font
|
||||
loaded_fonts[name] = font
|
||||
|
||||
if refresh_callback.is_valid():
|
||||
refresh_callback.call(name)
|
||||
|
||||
static func get_font(family_name: String) -> Font:
|
||||
if family_name == "sans-serif":
|
||||
var sys_font = SystemFont.new()
|
||||
|
||||
@@ -427,15 +427,15 @@ static func apply_styles_to_label(label: Control, styles: Dictionary, element: H
|
||||
var font_family = styles["font-family"]
|
||||
var font_resource = FontManager.get_font(font_family)
|
||||
|
||||
# set a sans-serif fallback first
|
||||
if font_family not in ["sans-serif", "serif", "monospace"]:
|
||||
if not FontManager.loaded_fonts.has(font_family):
|
||||
# Font not loaded yet, use sans-serif as fallback
|
||||
if FontManager.loaded_fonts.has(font_family) and font_resource:
|
||||
apply_font_to_label(label, font_resource, styles)
|
||||
else:
|
||||
var fallback_font = FontManager.get_font("sans-serif")
|
||||
apply_font_to_label(label, fallback_font, styles)
|
||||
|
||||
if font_resource:
|
||||
apply_font_to_label(label, font_resource, styles)
|
||||
else:
|
||||
if font_resource:
|
||||
apply_font_to_label(label, font_resource, styles)
|
||||
else:
|
||||
# No custom font family, but check if we need to apply font weight
|
||||
if styles.has("font-thin") or styles.has("font-extralight") or styles.has("font-light") or styles.has("font-normal") or styles.has("font-medium") or styles.has("font-semibold") or styles.has("font-extrabold") or styles.has("font-black"):
|
||||
@@ -576,50 +576,64 @@ static func parse_radius(radius_str: String) -> int:
|
||||
return SizeUtils.parse_radius(radius_str)
|
||||
|
||||
static func apply_font_to_label(label: RichTextLabel, font_resource: Font, styles: Dictionary = {}) -> void:
|
||||
# Create normal font with appropriate weight
|
||||
var normal_font = SystemFont.new()
|
||||
normal_font.font_names = font_resource.font_names if font_resource is SystemFont else ["Arial"]
|
||||
if font_resource is FontFile:
|
||||
label.add_theme_font_override("normal_font", font_resource)
|
||||
label.add_theme_font_override("bold_font", font_resource)
|
||||
label.add_theme_font_override("italics_font", font_resource)
|
||||
|
||||
# Set weight based on styles
|
||||
var font_weight = 400 # Default normal weight
|
||||
if styles.has("font-thin"):
|
||||
font_weight = 100
|
||||
elif styles.has("font-extralight"):
|
||||
font_weight = 200
|
||||
elif styles.has("font-light"):
|
||||
font_weight = 300
|
||||
elif styles.has("font-normal"):
|
||||
font_weight = 400
|
||||
elif styles.has("font-medium"):
|
||||
font_weight = 500
|
||||
elif styles.has("font-semibold"):
|
||||
font_weight = 600
|
||||
elif styles.has("font-bold"):
|
||||
font_weight = 700
|
||||
elif styles.has("font-extrabold"):
|
||||
font_weight = 800
|
||||
elif styles.has("font-black"):
|
||||
font_weight = 900
|
||||
elif font_resource is SystemFont:
|
||||
var font_weight = 400
|
||||
if styles.has("font-thin"):
|
||||
font_weight = 100
|
||||
elif styles.has("font-extralight"):
|
||||
font_weight = 200
|
||||
elif styles.has("font-light"):
|
||||
font_weight = 300
|
||||
elif styles.has("font-normal"):
|
||||
font_weight = 400
|
||||
elif styles.has("font-medium"):
|
||||
font_weight = 500
|
||||
elif styles.has("font-semibold"):
|
||||
font_weight = 600
|
||||
elif styles.has("font-bold"):
|
||||
font_weight = 700
|
||||
elif styles.has("font-extrabold"):
|
||||
font_weight = 800
|
||||
elif styles.has("font-black"):
|
||||
font_weight = 900
|
||||
|
||||
var normal_font = SystemFont.new()
|
||||
normal_font.font_names = font_resource.font_names
|
||||
normal_font.font_weight = font_weight
|
||||
label.add_theme_font_override("normal_font", normal_font)
|
||||
|
||||
# Create bold variant
|
||||
var bold_font = SystemFont.new()
|
||||
bold_font.font_names = font_resource.font_names
|
||||
bold_font.font_weight = 700
|
||||
label.add_theme_font_override("bold_font", bold_font)
|
||||
|
||||
# Create italic variant
|
||||
var italic_font = SystemFont.new()
|
||||
italic_font.font_names = font_resource.font_names
|
||||
italic_font.font_italic = true
|
||||
italic_font.font_weight = font_weight
|
||||
label.add_theme_font_override("italics_font", italic_font)
|
||||
|
||||
else:
|
||||
label.add_theme_font_override("normal_font", font_resource)
|
||||
label.add_theme_font_override("bold_font", font_resource)
|
||||
label.add_theme_font_override("italics_font", font_resource)
|
||||
|
||||
normal_font.font_weight = font_weight
|
||||
|
||||
label.add_theme_font_override("normal_font", normal_font)
|
||||
|
||||
var bold_font = SystemFont.new()
|
||||
bold_font.font_names = font_resource.font_names if font_resource is SystemFont else ["Arial"]
|
||||
bold_font.font_weight = 700 # Bold weight
|
||||
label.add_theme_font_override("bold_font", bold_font)
|
||||
|
||||
var italic_font = SystemFont.new()
|
||||
italic_font.font_names = font_resource.font_names if font_resource is SystemFont else ["Arial"]
|
||||
italic_font.font_italic = true
|
||||
label.add_theme_font_override("italics_font", italic_font)
|
||||
|
||||
var bold_italic_font = SystemFont.new()
|
||||
bold_italic_font.font_names = font_resource.font_names if font_resource is SystemFont else ["Arial"]
|
||||
bold_italic_font.font_weight = 700 # Bold weight
|
||||
bold_italic_font.font_italic = true
|
||||
label.add_theme_font_override("bold_italics_font", bold_italic_font)
|
||||
# Handle bold_italics_font
|
||||
if font_resource is FontFile:
|
||||
label.add_theme_font_override("bold_italics_font", font_resource)
|
||||
elif font_resource is SystemFont:
|
||||
var bold_italic_font = SystemFont.new()
|
||||
bold_italic_font.font_names = font_resource.font_names
|
||||
bold_italic_font.font_weight = 700
|
||||
bold_italic_font.font_italic = true
|
||||
label.add_theme_font_override("bold_italics_font", bold_italic_font)
|
||||
|
||||
static func apply_font_to_button(button: Button, styles: Dictionary) -> void:
|
||||
if styles.has("font-family"):
|
||||
|
||||
@@ -142,9 +142,9 @@ func fetch_external_resource(url: String, base_url: String = "") -> String:
|
||||
else:
|
||||
return ""
|
||||
|
||||
func fetch_gurt_resource(url: String) -> String:
|
||||
func fetch_gurt_resource(url: String, as_binary: bool = false):
|
||||
if not GurtProtocol.is_gurt_domain(url):
|
||||
return ""
|
||||
return PackedByteArray() if as_binary else ""
|
||||
|
||||
var gurt_url = url
|
||||
if not gurt_url.begins_with("gurt://"):
|
||||
@@ -184,11 +184,17 @@ func fetch_gurt_resource(url: String) -> String:
|
||||
status_code = response.status_code
|
||||
error_msg += ": " + str(response.status_code) + " " + response.status_message
|
||||
NetworkManager.complete_request(network_request.id, status_code, error_msg, {}, "")
|
||||
return ""
|
||||
return PackedByteArray() if as_binary else ""
|
||||
|
||||
var response_headers = response.headers if response.headers else {}
|
||||
var response_body = response.body.get_string_from_utf8()
|
||||
|
||||
NetworkManager.complete_request(network_request.id, response.status_code, "OK", response_headers, response_body)
|
||||
var response_body = response.body
|
||||
|
||||
return response_body
|
||||
if as_binary:
|
||||
var size_info = "Binary data: " + str(response_body.size()) + " bytes"
|
||||
NetworkManager.complete_request(network_request.id, response.status_code, "OK", response_headers, size_info, response_body)
|
||||
return response_body
|
||||
else:
|
||||
var response_body_str = response_body.get_string_from_utf8()
|
||||
NetworkManager.complete_request(network_request.id, response.status_code, "OK", response_headers, response_body_str)
|
||||
return response_body_str
|
||||
|
||||
@@ -810,7 +810,6 @@ func register_font_dependent_element(label: Control, styles: Dictionary, element
|
||||
})
|
||||
|
||||
func refresh_fonts(font_name: String) -> void:
|
||||
# Find all elements that should use this font and refresh them
|
||||
for element_info in font_dependent_elements:
|
||||
var label = element_info["label"]
|
||||
var styles = element_info["styles"]
|
||||
|
||||
Reference in New Issue
Block a user