XML font= with no positive FontHeight warns and sets no font (#1488)
An XML `font=` naming a file with no `<FontHeight>` still got that font at a default height. `CSimpleFontString::LoadXML` (`0x770f40`) reads `FontHeight` and refuses a height that is not above 0 (`0x771146`–`0x771154`). It warns `FontString %s: Missing font height in %s element` (`0x878e10`) and returns at `0x771191`, setting no font. SimpleHTML's element fonts go through `CSimpleFont::LoadXML`, which has the same check (`0x783d6d`, `0x87a4f0`). Both loaders now refuse a missing, zero or negative height with the reference's warning and set no font or outline, so `SetText` on such a string raises "Font not set". None of the 219 corpus addons trips it. Tests: absent, 0 and negative heights, the ButtonText message, and a SimpleHTML element; all fail without the fix. Fixes #1469 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -198,6 +198,15 @@ impl Loader<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// `"<kind> <name>: Missing font height in <tag> 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 `<unnamed>` (`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 `<NormalText>` 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 `<FontHeight>` 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::<Option<String>>("GetName", ())
|
||||
.ok()
|
||||
.flatten();
|
||||
self.report_missing_font_height(
|
||||
"FontString",
|
||||
name.as_deref().unwrap_or("<unnamed>"),
|
||||
®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}"));
|
||||
|
||||
@@ -2202,6 +2202,53 @@ mod loader_tests {
|
||||
assert_eq!(s.eval::<i64>("return Gone:GetNumRegions()").unwrap(), 0);
|
||||
}
|
||||
|
||||
/// A `<SimpleHTML>` 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 `<Color>`, 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#"<Ui>
|
||||
<SimpleHTML name="Page">
|
||||
<FontString font="Fonts\MORPHEUS.TTF"><Color r="1" g="0" b="0"/></FontString>
|
||||
<FontStringHeader1 font="Fonts\SKURRI.TTF">
|
||||
<FontHeight val="24"/><Color r="0" g="1" b="0"/>
|
||||
</FontStringHeader1>
|
||||
</SimpleHTML>
|
||||
</Ui>"#,
|
||||
);
|
||||
let report = load(&s, &doc, &no_files);
|
||||
assert!(report.errors.is_empty(), "errors: {:?}", report.errors);
|
||||
assert_eq!(
|
||||
report.warnings,
|
||||
["Font <unnamed>: Missing font height in FontString element"]
|
||||
);
|
||||
assert_eq!(
|
||||
s.eval::<Option<String>>("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 `<FontString>` declares `elementFont[0]` and `<FontStringHeader1>`
|
||||
/// `elementFont[1]`, neither a region, and `hyperlinkFormat=` lands at `+0x360`; stock
|
||||
/// `ItemTextFrame.xml:214` uses the first.
|
||||
|
||||
@@ -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=`, `<Color>`, `<Shadow>`, 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", "<unnamed>", &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
|
||||
|
||||
@@ -363,7 +363,8 @@ fn anchor_for(prev_block: Option<u32>, frame_id: u32, point: Point, next_y: f32)
|
||||
|
||||
/// Apply the `font=`, `<FontHeight>` and `outline=` an XML `<FontString>` or `<FontStringHeaderN>`
|
||||
/// 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,
|
||||
|
||||
@@ -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 `<FontHeight>` 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#"<Ui><Frame name="XmlHost" setAllPoints="true"><Layers><Layer level="ARTWORK">
|
||||
<FontString name="NoHeight" font="Fonts\FRIZQT__.TTF" outline="NORMAL" text="a"/>
|
||||
<FontString name="ZeroHeight" font="Fonts\FRIZQT__.TTF">
|
||||
<FontHeight><AbsValue val="0"/></FontHeight>
|
||||
</FontString>
|
||||
<FontString name="NegHeight" font="Fonts\FRIZQT__.TTF">
|
||||
<FontHeight><AbsValue val="-4"/></FontHeight>
|
||||
</FontString>
|
||||
<FontString font="Fonts\FRIZQT__.TTF"/>
|
||||
<FontString name="Sized" font="Fonts\FRIZQT__.TTF" outline="NORMAL">
|
||||
<FontHeight><AbsValue val="12"/></FontHeight>
|
||||
</FontString>
|
||||
</Layer></Layers></Frame></Ui>"#,
|
||||
)
|
||||
.unwrap();
|
||||
let report = crate::loader::load(&s, &doc, &|_| None);
|
||||
assert!(report.errors.is_empty(), "{:?}", report.errors);
|
||||
for name in ["NoHeight", "ZeroHeight", "NegHeight", "<unnamed>"] {
|
||||
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::<Option<String>>(&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 `<ButtonText>` 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#"<Ui><Button name="Plain"><ButtonText name="PlainText" font="Fonts\FRIZQT__.TTF"/></Button></Ui>"#,
|
||||
)
|
||||
.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());
|
||||
}
|
||||
|
||||
@@ -43,16 +43,19 @@ const XML: &str = r#"<Ui>
|
||||
<Layers><Layer level="ARTWORK">
|
||||
<FontString name="WrapPlain" text="plain" font="Fonts\FRIZQT__.TTF">
|
||||
<Size><AbsDimension x="100" y="12"/></Size>
|
||||
<FontHeight val="12"/>
|
||||
<Anchors><Anchor point="TOPLEFT"/></Anchors>
|
||||
</FontString>
|
||||
<FontString name="WrapFlagged" text="flagged" font="Fonts\FRIZQT__.TTF"
|
||||
nonspacewrap="true" maxLines="3">
|
||||
<Size><AbsDimension x="100" y="12"/></Size>
|
||||
<FontHeight val="12"/>
|
||||
<Anchors><Anchor point="TOPRIGHT"/></Anchors>
|
||||
</FontString>
|
||||
<FontString name="WrapOff" text="off" font="Fonts\FRIZQT__.TTF" nonspacewrap="false"
|
||||
maxLines="1x">
|
||||
<Size><AbsDimension x="100" y="12"/></Size>
|
||||
<FontHeight val="12"/>
|
||||
<Anchors><Anchor point="BOTTOMLEFT"/></Anchors>
|
||||
</FontString>
|
||||
</Layer></Layers>
|
||||
|
||||
Reference in New Issue
Block a user