diff --git a/crates/benilla-ui/src/loader/regions.rs b/crates/benilla-ui/src/loader/regions.rs index 73d2cb359..fbb40f70d 100644 --- a/crates/benilla-ui/src/loader/regions.rs +++ b/crates/benilla-ui/src/loader/regions.rs @@ -198,6 +198,15 @@ impl Loader<'_> { } } + /// `" : Missing font height in element"` at severity 1 into the document's + /// record: `0x878e10` for a FontString (`0x77117d`), `0x87a4f0` for a Font or a SimpleHTML + /// element font (`0x783d9a`), with the object's name or `` (`0x84c7f0`) and the node's tag. + pub(super) fn report_missing_font_height(&mut self, kind: &str, name: &str, tag: &str) { + let line = format!("{kind} {name}: Missing font height in {tag} element"); + self.report.status.report(status::WARNING, line.clone()); + self.report.warnings.push(line); + } + /// A FontString's `nonspacewrap` and `maxLines` (`0x77103f`-`0x7710cd`), which the loader reads /// ahead of the font attributes, so a Button's `` takes them too. A boolean goes /// through the string-to-bool `0x6f1b30`; `maxLines` through `0x64ade0`, its leading digits @@ -450,17 +459,31 @@ impl Loader<'_> { } } Some(path) => { - // Applied directly, not through the Lua `SetFont`, which requires a height - // (`0x87c69c`) where XML may omit it. + // A file needs a first `` above 0 (`0x77112f`-`0x771154`); else the + // load reports `0x878e10` (`0x77117d`) and returns (`0x771191`), before `outline=` + // and with the string's font unset. let height = children_named(region, "FontHeight") .next() - .and_then(abs_value); + .and_then(abs_value) + .filter(|h| *h > 0.0); + let Some(height) = height else { + let name = wrapper + .call_method::>("GetName", ()) + .ok() + .flatten(); + self.report_missing_font_height( + "FontString", + name.as_deref().unwrap_or(""), + ®ion.tag, + ); + return; + }; let outline = region.attr("outline").map(str::to_string); if let Err(e) = crate::script::apply_font_parts( self.lua, wrapper, Some(path.to_string()), - height, + Some(height), outline, ) { self.report.fault(format!("{dbg}: region font attrs: {e}")); diff --git a/crates/benilla-ui/src/loader/tests.rs b/crates/benilla-ui/src/loader/tests.rs index 6e8e82408..ce02fa071 100644 --- a/crates/benilla-ui/src/loader/tests.rs +++ b/crates/benilla-ui/src/loader/tests.rs @@ -2202,6 +2202,53 @@ mod loader_tests { assert_eq!(s.eval::("return Gone:GetNumRegions()").unwrap(), 0); } + /// A `` element font is loaded by `CSimpleFont::LoadXML` (`0x78a26e` → `0x783c30`), + /// whose file needs a height above 0 (`0x783d6d`-`0x783d78`): else it reports + /// `Font %s: Missing font height in %s element` (`0x87a4f0`) and returns (`0x783dae`) before + /// the element's ``, so that element keeps no font and its default colour. + #[test] + fn a_simplehtml_element_font_file_without_a_height_is_refused() { + let mut s = UiScript::new().unwrap(); + s.set_screen_size(800.0, 600.0); + let doc = parse( + r#" + + + + + + + "#, + ); + let report = load(&s, &doc, &no_files); + assert!(report.errors.is_empty(), "errors: {:?}", report.errors); + assert_eq!( + report.warnings, + ["Font : Missing font height in FontString element"] + ); + assert_eq!( + s.eval::>("return (Page:GetFont())").unwrap(), + None + ); + assert_eq!( + s.eval::<(String, f32)>("local p, h = Page:GetFont(\"H1\"); return p, h") + .unwrap(), + ("Fonts\\SKURRI.TTF".to_string(), 24.0) + ); + let green = s + .eval::<(f32, f32, f32, f32)>("return Page:GetTextColor(\"H1\")") + .unwrap(); + assert_eq!(green, (0.0, 1.0, 0.0, 1.0)); + let p = s + .eval::<(f32, f32, f32, f32)>("return Page:GetTextColor()") + .unwrap(); + assert_ne!( + p, + (1.0, 0.0, 0.0, 1.0), + "the refused element's Color is not read" + ); + } + /// A direct-child `` declares `elementFont[0]` and `` /// `elementFont[1]`, neither a region, and `hyperlinkFormat=` lands at `+0x360`; stock /// `ItemTextFrame.xml:214` uses the first. diff --git a/crates/benilla-ui/src/loader/widgets.rs b/crates/benilla-ui/src/loader/widgets.rs index 73264f04c..40f0fa418 100644 --- a/crates/benilla-ui/src/loader/widgets.rs +++ b/crates/benilla-ui/src/loader/widgets.rs @@ -493,14 +493,24 @@ impl Loader<'_> { self.call(wrapper, "SetFontObject", (name, f.to_string()), dbg); } Some(path) => { - let height = children_named(el, "FontHeight").last().and_then(abs_value); + // As a FontString's file, `CSimpleFont::LoadXML` needs a height above 0 + // (`0x783d6d`-`0x783d78`); else it reports `0x87a4f0` and returns (`0x783dae`) + // before `outline=`, ``, ``, the justifies and `spacing=`. + let height = children_named(el, "FontHeight") + .last() + .and_then(abs_value) + .filter(|h| *h > 0.0); + let Some(height) = height else { + self.report_missing_font_height("Font", "", &el.tag); + return; + }; let outline = el.attr("outline").map(str::to_string); if let Err(e) = crate::script::apply_simplehtml_font_parts( self.lua, wrapper, elem, Some(path.to_string()), - height, + Some(height), outline, ) { self.report diff --git a/crates/benilla-ui/src/script/simplehtml/mod.rs b/crates/benilla-ui/src/script/simplehtml/mod.rs index 0babfb1f3..82b0a1f25 100644 --- a/crates/benilla-ui/src/script/simplehtml/mod.rs +++ b/crates/benilla-ui/src/script/simplehtml/mod.rs @@ -363,7 +363,8 @@ fn anchor_for(prev_block: Option, frame_id: u32, point: Point, next_y: f32) /// Apply the `font=`, `` and `outline=` an XML `` or `` /// child supplies, any of them absent. The reference applies these in C++ (`CSimpleFont::LoadXML` -/// `0x783c30`, from `0x78a1fe`), not through `SetFont`, which requires both path and height. +/// `0x783c30`, from `0x78a1fe`), not through `SetFont`, which requires both path and height. The +/// loader hands a file only with a height above 0 (`0x783d6d`), so `height` is `Some` there. pub(crate) fn apply_element_font_parts( lua: &Lua, this: &Table, diff --git a/crates/benilla-ui/src/script/tests/no_font.rs b/crates/benilla-ui/src/script/tests/no_font.rs index d7ad0938c..f7115699a 100644 --- a/crates/benilla-ui/src/script/tests/no_font.rs +++ b/crates/benilla-ui/src/script/tests/no_font.rs @@ -230,3 +230,84 @@ fn a_button_label_takes_the_buttons_font_and_its_own_font_attribute() { s.run(r#"TabBtn:SetText("Tab")"#).unwrap(); assert!(draws(&mut s, "Tab")); } + +/// An XML `font=` that names a file needs a first `` above 0 (`0x77112f`-`0x771154`). +/// Else the loader reports `FontString %s: Missing font height in %s element` (`0x878e10`) at +/// severity 1 and returns (`0x771191`) before it sets the font, so the string has none. +#[test] +fn an_xml_font_file_without_a_positive_height_leaves_the_string_fontless() { + let s = vm(); + let doc = crate::framexml::parse( + r#" + + + + + + + + + + + + "#, + ) + .unwrap(); + let report = crate::loader::load(&s, &doc, &|_| None); + assert!(report.errors.is_empty(), "{:?}", report.errors); + for name in ["NoHeight", "ZeroHeight", "NegHeight", ""] { + let line = format!("FontString {name}: Missing font height in FontString element"); + assert!( + report.warnings.contains(&line), + "{line}: {:?}", + report.warnings + ); + assert!( + report.status.lines().contains(&line), + "{line} in the status" + ); + } + assert_eq!(report.warnings.len(), 4, "{:?}", report.warnings); + + for name in ["NoHeight", "ZeroHeight", "NegHeight"] { + assert!( + s.eval::>(&format!("return (({name}):GetFont())")) + .unwrap() + .is_none(), + "{name} has no font" + ); + let err = s + .run(&format!(r#"{name}:SetText("x")"#)) + .expect_err("a string with no font raises"); + assert!( + err.to_string() + .contains(&format!("{name}:SetText(): Font not set")), + "{err}" + ); + } + + // A positive height still sets the font, outline and all. + assert_eq!( + s.eval::<(String, f32, String)>("return Sized:GetFont()") + .unwrap(), + ("Fonts\\FRIZQT__.TTF".to_string(), 12.0, "".to_string()) + ); + s.run(r#"Sized:SetText("ok")"#).unwrap(); +} + +/// A `` is the same loader (`0x7789d0` → `0x770f40`), and names itself by its tag. +#[test] +fn a_button_text_font_file_without_a_height_reports_and_stays_fontless() { + let s = vm(); + let doc = crate::framexml::parse( + r#""#, + ) + .unwrap(); + let report = crate::loader::load(&s, &doc, &|_| None); + assert!(report.errors.is_empty(), "{:?}", report.errors); + assert_eq!( + report.warnings, + ["FontString PlainText: Missing font height in ButtonText element"] + ); + assert!(s.run(r#"PlainText:SetText("x")"#).is_err()); +} diff --git a/crates/benilla-ui/src/script/tests/text_wrap.rs b/crates/benilla-ui/src/script/tests/text_wrap.rs index fab861f97..57f68a456 100644 --- a/crates/benilla-ui/src/script/tests/text_wrap.rs +++ b/crates/benilla-ui/src/script/tests/text_wrap.rs @@ -43,16 +43,19 @@ const XML: &str = r#" + + +