diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 377e4e39b2..e7c1c11699 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -210,7 +210,7 @@ pub fn commands() -> Vec { r##""valign"?: "top"|"middle"|"bottom" (text, dynamicText: where its block of lines sits down its box; default top; a block taller than its box is drawn from the top, its last lines left out, which a warning says; refused for other kinds), "##, r##""lineSpacing"?: 0.5 to 3 (text, dynamicText: the distance between its lines, a multiple of the usual; default 1; refused outside that range, and for other kinds), "##, r##""case"?: "none"|"upper"|"lower" (text, dynamicText: drawn in upper or lower case once its dynamic text is filled in, its text kept as typed; default none; refused for other kinds), "##, - r##""font"?: "sans"|"serif"|an installed font family's name, as labels.fonts lists them (text, dynamicText; a family not installed here is drawn in its fallback, and a serif where none is installed in the sans, with a warning; refused for other kinds), "##, + r##""font"?: "sans"|"serif"|an installed font family's name, as labels.fonts lists them (text, dynamicText, and a northArrow's N; a family not installed here is drawn in its fallback, and a serif where none is installed in the sans, with a warning; refused for other kinds), "##, r##""fallback"?: "sans"|"serif" (a named font's, where it isn't installed; default its own class, else sans), "##, r##""title"?: legend title (default "Legend"); chart: its own title ("" for none; default: its field, or the statistic of its field by the group); profile: written over its curve ("" for none; default its layer's name), "##, r##""subtitle"?: text (legend: one line under its title, at the rows' text size, up to 200 characters; "" for none, the default; refused for other kinds), "##, @@ -327,8 +327,8 @@ pub fn commands() -> Vec { r##""valign"?, "##, r##""lineSpacing"?, "##, r##""case"? (text, as in layout.addElement; null leaves each as it is; refused for other elements), "##, - r##""font"?: "sans"|"serif"|an installed font family's name, "##, - r##""fallback"?: "sans"|"serif" (text, as in layout.addElement; refused for other elements), "##, + r##""font"?: "sans"|"serif"|an installed font family's name (text, dynamicText, a northArrow's N), "##, + r##""fallback"?: "sans"|"serif" (text, dynamicText, a northArrow's N, as in layout.addElement; refused for other elements), "##, r##""title"? (legend, colorbar; chart: its own title ("" for none; null giving it the title it is given again)), "##, r##""subtitle"?: text (legend, as in layout.addElement; "" takes it away, null leaves it as it is), "##, r##""allClasses"?: bool (legend), "##, diff --git a/crates/engine/src/cmd/layout/elements.rs b/crates/engine/src/cmd/layout/elements.rs index 3698c06d7e..7cd51a6396 100644 --- a/crates/engine/src/cmd/layout/elements.rs +++ b/crates/engine/src/cmd/layout/elements.rs @@ -394,7 +394,7 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu } // A family is a text's: refused elsewhere, as a legend's left-out layers are, so a script // doesn't take it as applied. - if !matches!(kind, "text" | "dynamicText") && gives_font(p) { + if !matches!(kind, "text" | "dynamicText" | "northArrow") && gives_font(p) { return Err(ONLY_TEXT_FONT.into()); } if kind != "rectangle" && p.get("radius").is_some_and(|v| !v.is_null()) { @@ -798,6 +798,10 @@ pub(in crate::cmd::layout) fn add_element(s: &mut Session, p: &Value) -> CmdResu ElementKind::Text { font, font_name, bold, italic, .. } => { font_warning(*font, font_name.as_deref(), p.get("fallback").is_some_and(|v| !v.is_null()), (*bold, *italic)) } + // Its `N` is bold; a call that gives no font is told nothing. + ElementKind::NorthArrow { font, font_name, .. } if gives_font(p) => { + font_warning(*font, font_name.as_deref(), p.get("fallback").is_some_and(|v| !v.is_null()), (true, false)) + } _ => None, }; // A colorbar with nothing to show says why, as a notice the app shows. @@ -1103,7 +1107,8 @@ pub fn add_element_reads_frame(kind: &str) -> bool { } /// Keys read by `layout.addElement` for a north arrow (#3074). -const ADD_NORTH_ARROW_KEYS: &[&str] = &["layout", "kind", "frame", "x", "y", "w", "h", "background", "style", "fill", "outline"]; +const ADD_NORTH_ARROW_KEYS: &[&str] = + &["layout", "kind", "frame", "x", "y", "w", "h", "background", "style", "fill", "outline", "font", "fallback"]; /// Keys read by `layout.addElement` for a scale bar (#3074). const ADD_SCALE_BAR_KEYS: &[&str] = &[ @@ -1593,7 +1598,7 @@ mod tests { let err = s.run("layout.addElement", &json!({"kind": "northArrow", "bold": true})).unwrap_err(); assert_eq!( err, - "layout.addElement: a north arrow takes no \"bold\" (it reads layout, kind, frame, x, y, w, h, background, style, fill, outline)" + "layout.addElement: a north arrow takes no \"bold\" (it reads layout, kind, frame, x, y, w, h, background, style, fill, outline, font, fallback)" ); let err = s.run("layout.addElement", &json!({"kind": "rectangle", "angle": 30})).unwrap_err(); @@ -1661,7 +1666,7 @@ mod tests { // Every key a kind reads, each given a value it takes. let calls = [ - json!({"kind": "northArrow", "layout": 0, "frame": frame, "x": 10, "y": 10, "w": 15, "h": 20, "style": "compass", "fill": "#112233", "outline": "#445566", "background": {"fill": "#FFFFFF"}}), + json!({"kind": "northArrow", "layout": 0, "frame": frame, "x": 10, "y": 10, "w": 15, "h": 20, "style": "compass", "fill": "#112233", "outline": "#445566", "font": "sans", "fallback": null, "background": {"fill": "#FFFFFF"}}), json!({"kind": "scaleBar", "frame": frame, "x": 10, "y": 10, "w": 60, "h": 12, "style": "stepped", "units": "miles", "divisions": 3, "subdivisions": 2, "divisionsBeforeZero": 1, "labelPosition": "below", "unitLabel": "mi", "unitLabelPosition": "afterBar", "fit": "adjustWidth", "decimals": 1, "textSize": 9, "color": "#112233", "fill": "#FFFFFF"}), json!({"kind": "legend", "frame": frame, "title": "T", "allClasses": true, "hideOther": true, "showNoData": true, "exclude": [], "layerOrder": [], "columns": 2, "labels": {}, "textSize": 8, "fit": "adjustSize", "showLayerNames": false, "showCounts": true, "sizes": "nested", "color": "#112233"}), json!({"kind": "colorbar", "frame": frame, "title": "T", "orientation": "vertical", "ticks": 3, "tickSide": "left", "reverse": true, "color": "#112233"}), diff --git a/crates/engine/src/cmd/layout/export.rs b/crates/engine/src/cmd/layout/export.rs index d6467efdf1..ada10439b2 100644 --- a/crates/engine/src/cmd/layout/export.rs +++ b/crates/engine/src/cmd/layout/export.rs @@ -2,13 +2,13 @@ use super::*; -/// What an export of a layout whose texts name installed families is told while the app is still +/// What an export of a layout whose texts or north arrows name installed families is told while the app is still /// indexing the fonts (its first seconds; the app's exports don't wait for them): those texts /// are drawn in their fallbacks. A script or the command line indexes them at once. pub(in crate::cmd::layout) fn named_fonts_unread(layout: &Layout) -> Option<&'static str> { - let named = layout.elements.iter().any(|e| matches!(&e.kind, ElementKind::Text { font_name: Some(_), .. })); + let named = layout.elements.iter().any(|e| e.kind.named_font().is_some()); (named && rustgis_render::families::available().is_none()).then_some( - "the installed fonts are still being read, so texts in a named font family are drawn in their fallback: export again in a moment", + "the installed fonts are still being read, so texts and north arrows in a named font family are drawn in their fallback: export again in a moment", ) } diff --git a/crates/engine/src/cmd/layout/text.rs b/crates/engine/src/cmd/layout/text.rs index e7a1dc8ce5..70a70ce914 100644 --- a/crates/engine/src/cmd/layout/text.rs +++ b/crates/engine/src/cmd/layout/text.rs @@ -345,10 +345,10 @@ pub(crate) fn text_block_settings(p: &Value, current: (TextVAlign, f64, TextCase Ok((valign, spacing, case)) } -/// What a `font` given for an element that isn't a text is told. -pub(in crate::cmd::layout) const ONLY_TEXT_FONT: &str = "only text elements take a font or a fallback"; +/// What a `font` given for an element that draws no text of a chosen family is told. +pub(in crate::cmd::layout) const ONLY_TEXT_FONT: &str = "only text elements and north arrows take a font or a fallback"; -/// Whether `p` gives a `font` or a `fallback`, which only text elements take. +/// Whether `p` gives a `font` or a `fallback`, which only text elements and north arrows take. pub(in crate::cmd::layout) fn gives_font(p: &Value) -> bool { ["font", "fallback"].iter().any(|k| p.get(*k).is_some_and(|v| !v.is_null())) } diff --git a/crates/engine/src/cmd/layout/update.rs b/crates/engine/src/cmd/layout/update.rs index 6d7bb6aed7..11ba999039 100644 --- a/crates/engine/src/cmd/layout/update.rs +++ b/crates/engine/src/cmd/layout/update.rs @@ -39,8 +39,8 @@ fn changed_indicators(kind: &ElementKind, v: &Value) -> Result Result, String> { match kind { - ElementKind::NorthArrow { frame, style, fill, outline } => { - if ["style", "fill", "outline"].iter().all(|k| p.get(k).is_none()) { + ElementKind::NorthArrow { frame, style, fill, outline, font, font_name } => { + if ["style", "fill", "outline", "font", "fallback"].iter().all(|k| p.get(k).is_none()) { return Ok(None); } let style = match given_name(p, "style", "classic, compass, triangle or star")? { @@ -48,11 +48,14 @@ pub(in crate::cmd::layout) fn restyle(kind: &ElementKind, p: &Value) -> Result *style, }; + let (font, font_name) = text_font(p, (*font, font_name.clone()))?; Ok(Some(ElementKind::NorthArrow { frame: *frame, style, fill: color(p, "fill", *fill)?, outline: color(p, "outline", *outline)?, + font, + font_name, })) } ElementKind::ScaleBar { @@ -395,6 +398,8 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { Some(ElementKind::Text { italic, align, letter_spacing, font, font_name, .. }) => { Some((text_style(p, *italic, *align, *letter_spacing)?, text_font(p, (*font, font_name.clone()))?)) } + // A north arrow's are read with its look, by `restyle`. + Some(ElementKind::NorthArrow { .. }) => None, _ if gives_font(p) => return Err(ONLY_TEXT_FONT.into()), _ => None, }; @@ -423,12 +428,15 @@ pub(in crate::cmd::layout) fn update(s: &mut Session, p: &Value) -> CmdResult { Some(ElementKind::Text { bold, .. }) => b(p, "bold").unwrap_or(*bold), _ => false, }; - let font_told = match &new_text_style { - Some(((italic, _, _), (f, n))) if gives_font(p) => { - font_warning(*f, n.as_deref(), p.get("fallback").is_some_and(|v| !v.is_null()), (bold_now, *italic)) - } + // The family asked for: a text's, or a north arrow's, whose `N` is bold. + let asked = match (&new_text_style, &restyled) { + (Some(((italic, _, _), (f, n))), _) => Some((*f, n.as_deref(), (bold_now, *italic))), + (None, Some(ElementKind::NorthArrow { font, font_name, .. })) => Some((*font, font_name.as_deref(), (true, false))), _ => None, }; + let font_told = asked + .filter(|_| gives_font(p)) + .and_then(|(f, n, style)| font_warning(f, n, p.get("fallback").is_some_and(|v| !v.is_null()), style)); if p.get("radius").is_some_and(|v| !v.is_null()) && !matches!(s.project.layouts.get(li).and_then(|l| l.element(id)).map(|e| &e.kind), Some(ElementKind::Rectangle { .. })) { diff --git a/crates/engine/src/project.rs b/crates/engine/src/project.rs index 71c74eea19..d726224f3e 100644 --- a/crates/engine/src/project.rs +++ b/crates/engine/src/project.rs @@ -112,6 +112,7 @@ pub const CAPABILITIES: &[&str] = &[ "layout-chart-outline", "layout-table-look", "layout-colorbar-labels", + "layout-north-arrow-font", "field-generation", "field-package", FIELD_CONFLICTS, diff --git a/crates/engine/src/project_capabilities.rs b/crates/engine/src/project_capabilities.rs index 9d6c11e5e3..c4ae8328b9 100644 --- a/crates/engine/src/project_capabilities.rs +++ b/crates/engine/src/project_capabilities.rs @@ -176,6 +176,7 @@ const COMMAND_CAPABILITIES: &[(&str, CommandCheck)] = &[ ("layout-chart-outline", command_uses_chart_outline), ("layout-table-look", command_uses_table_look), ("layout-colorbar-labels", command_uses_colorbar_labels), + ("layout-north-arrow-font", command_uses_north_arrow_font), ("blank-paper", command_uses_blank_paper), ("layout-frame-border", command_uses_frame_border), ("layout-section", command_uses_section), @@ -1233,6 +1234,38 @@ fn command_uses_colorbar_labels(id: &str, params: &Value) -> bool { } } +fn north_arrows_with_font(e: &rustgis_map::layout::Element) -> bool { + e.kind.north_arrow_uses_font() +} + +/// A layout command that gives a north arrow a font (#3467). A saved `layout.update` isn't told +/// what kind of element it changes (a text takes a font too), so it requires nothing here on +/// purpose: a reader without this refuses a font on a north arrow when the step runs. +fn command_uses_north_arrow_font(id: &str, params: &Value) -> bool { + match id { + "layout.addElement" => { + params.get("kind").and_then(Value::as_str).is_some_and(|k| k.trim().eq_ignore_ascii_case("northArrow")) + && params + .get("font") + .and_then(Value::as_str) + .is_some_and(|f| rustgis_map::layout::FontFamily::from_name(f.trim()) != Some(rustgis_map::layout::FontFamily::Sans)) + } + "layout.import" | "report.existingTemplate" => imported_layout_uses(params, &["\"font"], north_arrows_with_font), + // It opens its `text` only, whatever else the step holds. + "project.openText" => params.get("text").and_then(Value::as_str).is_some_and(text_gives_north_arrow_font), + "script.run" => params.get("script").and_then(Value::as_str).is_some_and(|s| script_uses(s, command_uses_north_arrow_font)), + _ => false, + } +} + +/// Whether a project's saved text requires `layout-north-arrow-font`, as one saved would: its +/// north arrows', its steps' and its notebooks'. Parsed only when the text may name a font, may +/// hide the key ([`may_hide_a_key`]) or holds a step's base64 `data` ([`names_data`]). +fn text_gives_north_arrow_font(text: &str) -> bool { + (text.contains("font") || may_hide_a_key(text) || names_data(text)) + && Project::from_json(text).is_ok_and(|p| p.required_capabilities().contains(&"layout-north-arrow-font")) +} + /// Whether a command carries its layout inline with an element `uses`, as `layout.import` and /// `report.existingTemplate` do: an older reader reads it without the keys it doesn't know. One /// read from a `"path"` is a layout file, whose own header requires what it uses (#2277). Every @@ -1813,6 +1846,7 @@ fn layout_capabilities(layouts: &[rustgis_map::layout::Layout]) -> Vec<&'static (elements().any(charts_outlined), "layout-chart-outline"), (elements().any(tables_with_a_look), "layout-table-look"), (elements().any(colorbars_with_labels), "layout-colorbar-labels"), + (elements().any(north_arrows_with_font), "layout-north-arrow-font"), (elements().any(|e| e.kind.has_custom_border()), "layout-frame-border"), (elements().any(|e| e.kind.has_sections()), "layout-section"), (elements().any(|e| e.kind.is_profile()), "layout-profile"), @@ -2299,6 +2333,7 @@ mod tests { (&[], "buffer", json!({"input": "Cities", "distance": 1})), (&[], "layout.addElement", chart), (&["layout-colorbar-labels"], "layout.addElement", json!({"kind": "colorbar", "decimals": 2})), + (&["layout-north-arrow-font"], "layout.addElement", json!({"kind": "northArrow", "font": "serif"})), ]; let mut covered = std::collections::BTreeSet::new(); for (capability, id, params) in samples { @@ -2601,6 +2636,7 @@ mod tests { "layout-chart-outline", "layout-table-look", "layout-colorbar-labels", + "layout-north-arrow-font", "layout-frame-border", "layout-text-size", "layout-scale-bar-details", @@ -3768,6 +3804,68 @@ mod tests { } } + /// A north arrow's font (#3467) requires `layout-north-arrow-font`, in a saved layout and in + /// a saved command. + #[test] + fn north_arrow_font_capability_follows_saved_layouts_and_commands() { + use rustgis_map::layout::{ElementKind, FontFamily}; + let fixture = Project::from_json(include_str!("../../../schema/fixtures/layout_north_arrow_font.rgis")).unwrap(); + let saved = serde_json::to_value(&fixture).unwrap(); + assert_eq!(saved["requires"], json!(["layout-north-arrow-font"])); + let mut clean = fixture; + for e in &mut clean.layouts[0].elements { + if let ElementKind::NorthArrow { font, font_name, .. } = &mut e.kind { + (*font, *font_name) = (FontFamily::Sans, None); + } + } + let base = serde_json::to_value(&clean).unwrap(); + assert!(base.get("requires").is_none(), "a plain north arrow needs nothing"); + // Project texts a step opens: without the header an older reader would drop the font. + let mut headerless = saved.clone(); + headerless.as_object_mut().unwrap().remove("requires"); + let mut generic = headerless.clone(); + generic["layouts"][0]["elements"][1]["kind"]["NorthArrow"].as_object_mut().unwrap().remove("font_name"); + // Its key written with an escape, in a project whose name doesn't spell it. + let mut escaped = headerless.clone(); + escaped["name"] = json!("Arrow"); + let escaped = escaped.to_string().replace("\"font", "\"\\u0066ont"); + let stepped = |params: Value| { + let mut p = base.clone(); + p["tasks"] = json!([{"name": "Inner", "steps": [{"title": "Add", "command": "layout.addElement", "params": params}]}]); + p.to_string() + }; + for (id, params, required) in [ + ("project.openText", json!({"text": headerless.to_string()}), true), + ("project.openText", json!({"text": saved.to_string()}), true), + ("project.openText", json!({"text": generic.to_string()}), true), + ("project.openText", json!({"text": escaped}), true), + ("project.openText", json!({"text": stepped(json!({"kind": "northArrow", "font": "serif"}))}), true), + ("project.openText", json!({"text": stepped(json!({"kind": "text", "font": "serif"}))}), false), + ("project.openText", json!({"text": base.to_string()}), false), + // Left out on purpose: it names no element kind, and a text takes a font too. + ("layout.update", json!({"element": 2, "font": "serif"}), false), + ("layout.addElement", json!({"kind": "northArrow", "font": "serif"}), true), + ("layout.addElement", json!({"kind": "northArrow", "font": "DejaVu Serif"}), true), + ("layout.addElement", json!({"kind": "northArrow", "font": "sans-serif"}), false), + ("layout.addElement", json!({"kind": "northArrow", "font": null}), false), + ("layout.addElement", json!({"kind": "text", "font": "serif"}), false), + ("layout.import", json!({"text": saved["layouts"][0].to_string()}), true), + ("layout.import", json!({"text": base["layouts"][0].to_string()}), false), + ] { + let script = format!("{id} {params}"); + for value in [ + json!([{"name": "Task", "steps": [{"title": "Arrow", "command": id, "params": params}]}]), + json!([{"name": "Script", "steps": [{"title": "Arrow", "command": "script.run", "params": {"script": script}}]}]), + ] { + let mut document = base.clone(); + document["tasks"] = value; + let saved = serde_json::to_value(Project::from_json(&document.to_string()).unwrap()).unwrap(); + let has = saved["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-north-arrow-font"))); + assert_eq!(has, required, "{id} {params}"); + } + } + } + #[test] fn field_identity_is_preserved_and_required_in_a_plain_saved_project() { let project = Project::from_json(include_str!("../../../schema/fixtures/field_origin.rgis")).unwrap(); diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 561e973547..48d1dbb99c 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -194,6 +194,10 @@ mod layout_colorbar; #[path = "tests/layout_open.rs"] mod layout_open; +/// A north arrow's font (#3467). +#[path = "tests/layout_north_font.rs"] +mod layout_north_font; + #[path = "tests/sde_connections.rs"] mod sde_connections; @@ -49026,11 +49030,11 @@ fn text_elements_take_a_font_family() { assert!(s.run("layout.addElement", &json!({"kind": "text", "font": bad})).is_err()); } assert_eq!(font(&s, title), FontFamily::Serif); - // Only a text takes a family: another element refuses it rather than ignore it. - let arrow = s.run("layout.addElement", &json!({"kind": "northArrow"})).unwrap()["element"].clone(); - let err = s.run("layout.update", &json!({"element": arrow, "font": "serif"})).unwrap_err(); + // Only a text or a north arrow takes a family: another element refuses it rather than ignore it. + let bar = s.run("layout.addElement", &json!({"kind": "scaleBar"})).unwrap()["element"].clone(); + let err = s.run("layout.update", &json!({"element": bar, "font": "serif"})).unwrap_err(); assert!(err.contains("only text elements"), "{err}"); - assert!(s.run("layout.addElement", &json!({"kind": "northArrow", "font": "serif"})).unwrap_err().contains("only text elements")); + assert!(s.run("layout.addElement", &json!({"kind": "scaleBar", "font": "serif"})).unwrap_err().contains("only text elements")); // The page draws either way. assert!(s.run("layout.update", &json!({"element": title, "text": "Mapa do Brasil", "bold": true, "italic": true})).is_ok()); assert!(!crate::cmd::layout::layout_bytes(&mut s, 0, false, 72.0, false).unwrap().is_empty()); @@ -49099,8 +49103,8 @@ fn text_elements_take_an_installed_font_family_by_name() { assert!(s.run("layout.update", &p).is_err(), "{bad}"); } assert_eq!(font(&s, id), (FontFamily::Sans, None), "a refused call changes nothing"); - let arrow = s.run("layout.addElement", &json!({"kind": "northArrow"})).unwrap()["element"].clone(); - assert!(s.run("layout.update", &json!({"element": arrow, "fallback": "serif"})).unwrap_err().contains("only text elements")); + let bar = s.run("layout.addElement", &json!({"kind": "scaleBar"})).unwrap()["element"].clone(); + assert!(s.run("layout.update", &json!({"element": bar, "fallback": "serif"})).unwrap_err().contains("only text elements")); // An installed family, where this computer has a small one (its file read whatever the // session's budget): kept as the font names it, its fallback its own class, and nothing said. let small = |f: &&rustgis_render::families::Family| { diff --git a/crates/engine/src/tests/layout_north_font.rs b/crates/engine/src/tests/layout_north_font.rs new file mode 100644 index 0000000000..5add012570 --- /dev/null +++ b/crates/engine/src/tests/layout_north_font.rs @@ -0,0 +1,138 @@ +//! A layout north arrow's font (#3467). + +use super::*; + +/// A map layout with a north arrow of its own; the arrow's element id. +fn arrowed(params: Value) -> (Session, u64) { + let mut s = world(); + s.run("layout.new", &json!({"template": "map"})).unwrap(); + let mut p = params; + p["kind"] = json!("northArrow"); + let id = s.run("layout.addElement", &p).unwrap()["element"].as_u64().unwrap(); + (s, id) +} + +/// The stored north arrow `id`, as the project saves it. +fn arrow(s: &Session, id: u64) -> Value { + let e = s.project.layouts[0].elements.iter().find(|e| u64::from(e.id) == id).unwrap(); + serde_json::to_value(&e.kind).unwrap()["NorthArrow"].clone() +} + +/// The font and its name as `arrow` saves them. +fn font(s: &Session, id: u64) -> (Value, Value) { + let a = arrow(s, id); + (a["font"].clone(), a["font_name"].clone()) +} + +fn update(s: &mut Session, id: u64, mut params: Value) -> Result { + params["element"] = json!(id); + s.run("layout.update", ¶ms) +} + +/// Whether the project, as saved, requires the north arrow's font capability. +fn requires_font(s: &Session) -> bool { + let saved = serde_json::to_value(&s.project).unwrap(); + saved["requires"].as_array().is_some_and(|r| r.contains(&json!("layout-north-arrow-font"))) +} + +/// Runs `command` and expects it refused naming `said`, with nothing changed and no undo step. +fn refused(s: &mut Session, command: &str, params: Value, said: &str) { + let before = serde_json::to_value(&s.project).unwrap(); + let undo = s.undo_label(); + let err = s.run(command, ¶ms).expect_err(¶ms.to_string()); + assert!(err.contains(said), "{params}: {err}"); + assert_eq!(serde_json::to_value(&s.project).unwrap(), before, "{params}"); + assert_eq!(s.undo_label(), undo, "{params}"); +} + +fn svg(s: &mut Session) -> String { + rustgis_render::svg::layout_svg(&s.project.layouts[0], &mut s.project.maps, 72.0, &Default::default()).unwrap().0 +} + +#[test] +fn a_north_arrow_without_a_font_saves_none() { + for params in [json!({}), json!({"font": null, "fallback": null}), json!({"font": "sans"}), json!({"font": "sans-serif"})] { + let (s, id) = arrowed(params.clone()); + assert_eq!(font(&s, id), (Value::Null, Value::Null), "{params}"); + assert!(!requires_font(&s), "{params}"); + } +} + +#[test] +fn a_north_arrow_takes_the_serif_and_draws_its_n_in_it() { + let (mut s, id) = arrowed(json!({})); + let plain = svg(&mut s); + let out = update(&mut s, id, json!({"font": "serif"})).unwrap(); + assert_eq!(font(&s, id), (json!("Serif"), Value::Null)); + assert!(requires_font(&s)); + let serif = rustgis_render::fonts::serif_available(); + assert_eq!(out.get("warning").is_some(), !serif, "{out}"); + if serif { + assert_ne!(svg(&mut s), plain, "the N is drawn in the serif"); + } + // Another change keeps the font and says nothing of it. + assert_eq!(update(&mut s, id, json!({"style": "star"})).unwrap(), Value::Null); + assert_eq!(font(&s, id).0, json!("Serif")); + s.run("edit.undo", &json!({})).unwrap(); + s.run("edit.undo", &json!({})).unwrap(); + assert_eq!(font(&s, id), (Value::Null, Value::Null), "one step each"); + assert_eq!(svg(&mut s), plain); +} + +#[test] +fn a_family_not_installed_is_kept_drawn_in_its_fallback_and_said() { + let (mut s, id) = arrowed(json!({"font": " No Such Family 3467 "})); + assert_eq!(font(&s, id), (Value::Null, json!("No Such Family 3467"))); + assert!(requires_font(&s)); + let out = s.run("layout.addElement", &json!({"kind": "northArrow", "font": "No Such Family 3467"})).unwrap(); + assert!(out["warning"].as_str().is_some_and(|w| w.starts_with("No Such Family 3467 is not installed")), "{out}"); + let out = update(&mut s, id, json!({"fallback": "serif"})).unwrap(); + assert!(out["warning"].as_str().is_some_and(|w| w.starts_with("No Such Family 3467")), "{out}"); + assert_eq!(font(&s, id), (json!("Serif"), json!("No Such Family 3467"))); + // Through a save and back. + let back: rustgis_map::layout::Layout = serde_json::from_str(&serde_json::to_string(&s.project.layouts[0]).unwrap()).unwrap(); + assert_eq!(back, s.project.layouts[0]); + // A generic family clears the name. + update(&mut s, id, json!({"font": "sans"})).unwrap(); + assert_eq!(font(&s, id), (Value::Null, Value::Null)); +} + +#[test] +fn an_installed_family_is_kept_as_the_font_names_it() { + let small = |f: &&rustgis_render::families::Family| { + f.listed && f.faces.iter().all(|face| std::fs::metadata(&face.path).is_ok_and(|m| m.len() < 4 << 20)) + }; + let Some(f) = rustgis_render::families::installed().iter().find(small) else { return }; + let (s, id) = arrowed(json!({"font": f.name.to_uppercase()})); + assert_eq!(font(&s, id).1, json!(f.name)); +} + +#[test] +fn a_bad_font_or_fallback_is_refused() { + let (mut s, id) = arrowed(json!({})); + let name = "font must be sans, serif or an installed font family's name"; + for (bad, said) in [ + (json!({"font": " "}), name), + (json!({"font": 2}), name), + (json!({"font": ["serif"]}), name), + (json!({"fallback": "serif"}), "fallback goes with a font family's name"), + (json!({"font": "serif", "fallback": "sans"}), "fallback goes with a font family's name, not sans or serif"), + (json!({"font": "Arial", "fallback": "mono"}), "fallback must be sans or serif"), + ] { + let mut add = bad.clone(); + add["kind"] = json!("northArrow"); + refused(&mut s, "layout.addElement", add, said); + let mut change = bad; + change["element"] = json!(id); + refused(&mut s, "layout.update", change, said); + } +} + +#[test] +fn an_element_without_a_font_still_refuses_one() { + let (mut s, _) = arrowed(json!({})); + let said = "only text elements and north arrows take a font or a fallback"; + let bar = s.run("layout.addElement", &json!({"kind": "scaleBar"})).unwrap()["element"].clone(); + refused(&mut s, "layout.update", json!({"element": bar, "font": "serif"}), said); + refused(&mut s, "layout.addElement", json!({"kind": "rectangle", "font": "serif"}), said); +} diff --git a/crates/map/src/layout/elements.rs b/crates/map/src/layout/elements.rs index d705073ffc..45bf450a93 100644 --- a/crates/map/src/layout/elements.rs +++ b/crates/map/src/layout/elements.rs @@ -61,6 +61,12 @@ pub enum ElementKind { /// The outline and the `N`. #[serde(default = "ink")] outline: Rgba, + /// The `N`'s family, as a text's `font`; left out when sans. + #[serde(default, skip_serializing_if = "is_sans")] + font: FontFamily, + /// An installed family for the `N`, as a text's `font_name`. + #[serde(default, deserialize_with = "crate::symbology::de_font", skip_serializing_if = "Option::is_none")] + font_name: Option, }, ScaleBar { frame: u32, @@ -463,7 +469,14 @@ impl ElementKind { /// A north arrow for `frame`, in the default style. pub fn north_arrow(frame: u32) -> ElementKind { - ElementKind::NorthArrow { frame, style: ArrowStyle::default(), fill: ink(), outline: ink() } + ElementKind::NorthArrow { + frame, + style: ArrowStyle::default(), + fill: ink(), + outline: ink(), + font: FontFamily::Sans, + font_name: None, + } } /// A scale bar for `frame`, in the default style. @@ -745,6 +758,20 @@ impl ElementKind { matches!(self, ElementKind::Colorbar { text_size, decimals, .. } if !is_colorbar_text_pt(text_size) || decimals.is_some()) } + /// The installed family a text or a north arrow's `N` is set in by name. + pub fn named_font(&self) -> Option<&str> { + match self { + ElementKind::Text { font_name, .. } | ElementKind::NorthArrow { font_name, .. } => font_name.as_deref(), + _ => None, + } + } + + /// Whether this is a north arrow with a font of its own (#3467): an older reader would draw + /// its `N` in the sans and save it again without. + pub fn north_arrow_uses_font(&self) -> bool { + matches!(self, ElementKind::NorthArrow { font, font_name, .. } if !is_sans(font) || font_name.is_some()) + } + /// Whether a chart uses what a chart had none of before (grouping, its statistic, its order, /// its limit, an "Other" bar): an older reader would draw it as one bar per feature, the 20 /// largest, and save it again without them; a statistic kept while ungrouped is lost so too. diff --git a/crates/render/src/layout.rs b/crates/render/src/layout.rs index 2136197a9a..050751457e 100644 --- a/crates/render/src/layout.rs +++ b/crates/render/src/layout.rs @@ -570,10 +570,11 @@ fn draw_page( ); } } - ElementKind::NorthArrow { frame: f, style, fill, outline } => { + ElementKind::NorthArrow { frame: f, style, fill, outline, font, font_name } => { // Turned with its frame's map. let bearing = frame(*f).map_or(0.0, |fr| fr.2.bearing); - north_arrow(&mut pm, r, *style, *fill, *outline, ppp, bearing) + let face = text_style(true, false, *font, font_name.as_deref(), TextAlign::Center, 0.0).face; + north_arrow(&mut pm, r, *style, *fill, *outline, face, ppp, bearing) } ElementKind::ScaleBar { frame: f, color, fill, .. } => { if let Some(&(_, mi, cam, _, _)) = frame(*f) diff --git a/crates/render/src/layout/north.rs b/crates/render/src/layout/north.rs index 93e062b865..2f294be6f7 100644 --- a/crates/render/src/layout/north.rs +++ b/crates/render/src/layout/north.rs @@ -2,9 +2,19 @@ use super::*; -/// A north arrow in `style`, with an `N` above it, pointing north on a map turned to `bearing` +/// A north arrow in `style`, with an `N` in `face` above it, pointing north on a map turned to `bearing` /// (degrees clockwise from north): turned about the middle of `r`, the `N` kept upright. -pub(super) fn north_arrow(pm: &mut Canvas, r: Rect, style: ArrowStyle, fill: Rgba, outline: Rgba, ppp: f64, bearing: f64) { +#[expect(clippy::too_many_arguments, reason = "the box, the look, the N's face and the page's scale and turn")] +pub(super) fn north_arrow( + pm: &mut Canvas, + r: Rect, + style: ArrowStyle, + fill: Rgba, + outline: Rgba, + face: text::Face, + ppp: f64, + bearing: f64, +) { let white = Rgba::rgb(0xFF, 0xFF, 0xFF); let sw = 0.8 * ppp as f32; let (cx, top) = (r.x() + r.width() / 2.0, r.y()); @@ -56,5 +66,7 @@ pub(super) fn north_arrow(pm: &mut Canvas, r: Rect, style: ArrowStyle, fill: Rgb } } let (nx, ny) = turn((cx, top + label_h / 2.0)); - text::text_at(pm, "N", nx, ny, label_h * 1.1, outline, true, text::Align::Center, false); + let px = label_h * 1.1; + let left = nx - text::measure_in("N", px / 1.33, face) / 2.0; + text::styled_at(pm, "N", left, ny, px, outline, face, 0.0); } diff --git a/crates/render/src/layout/tests.rs b/crates/render/src/layout/tests.rs index 344e4d31e4..7432c4e1d2 100644 --- a/crates/render/src/layout/tests.rs +++ b/crates/render/src/layout/tests.rs @@ -6222,7 +6222,16 @@ fn north_arrow_styles_differ_and_take_their_colours() { let mut pm = Pixmap::new(80, 120).unwrap(); pm.fill(tiny_skia::Color::WHITE); crate::canvas::with_canvas(&mut pm, |c| { - north_arrow(c, Rect::from_xywh(0.0, 0.0, 80.0, 120.0).unwrap(), style, fill, Rgba::rgb(0x22, 0x22, 0x22), 1.0, 0.0) + north_arrow( + c, + Rect::from_xywh(0.0, 0.0, 80.0, 120.0).unwrap(), + style, + fill, + Rgba::rgb(0x22, 0x22, 0x22), + crate::text::Face { bold: true, ..Default::default() }, + 1.0, + 0.0, + ) }); pm }; @@ -6246,7 +6255,16 @@ fn a_north_arrow_turns_with_its_frame() { pm.fill(tiny_skia::Color::WHITE); let r = Rect::from_xywh(0.0, 0.0, 80.0, 120.0).unwrap(); crate::canvas::with_canvas(&mut pm, |c| { - north_arrow(c, r, ArrowStyle::Triangle, Rgba::rgb(0xFF, 0, 0), Rgba::rgb(0x22, 0x22, 0x22), 1.0, bearing) + north_arrow( + c, + r, + ArrowStyle::Triangle, + Rgba::rgb(0xFF, 0, 0), + Rgba::rgb(0x22, 0x22, 0x22), + crate::text::Face { bold: true, ..Default::default() }, + 1.0, + bearing, + ) }); let pts: Vec<(f64, f64)> = (0..120) .flat_map(|y| (0..80).map(move |x| (x, y))) diff --git a/crates/ui-egui/src/layoutview/format/north.rs b/crates/ui-egui/src/layoutview/format/north.rs index 3aa9398526..507facb44d 100644 --- a/crates/ui-egui/src/layoutview/format/north.rs +++ b/crates/ui-egui/src/layoutview/format/north.rs @@ -7,7 +7,7 @@ use serde_json::{Value, json}; /// A north arrow's rows in the grid. pub(super) fn north_arrow_rows(app: &mut RustGisApp, ui: &mut Ui, e: &Element, update: &dyn Fn(&mut RustGisApp, Value)) { - let ElementKind::NorthArrow { style, fill, outline, .. } = &e.kind else { return }; + let ElementKind::NorthArrow { style, fill, outline, font, font_name, .. } = &e.kind else { return }; let id = e.id; let input_label = ui.label(tl!("Style")); egui::ComboBox::from_id_salt(("arrow_style", id)) @@ -32,4 +32,14 @@ pub(super) fn north_arrow_rows(app: &mut RustGisApp, ui: &mut Ui, e: &Element, u } ui.end_row(); } + // The N's font, chosen as a text's is. + let (mut family, mut named) = (*font, font_name.clone()); + if crate::textform::font_rows(ui, egui::Id::unique(("arrow_font", id)), &mut family, &mut named, true) { + // A named family with its fallback, else the generic one. + let change = match &named { + Some(n) => json!({"font": n, "fallback": family.code()}), + None => json!({"font": family.code()}), + }; + update(app, change); + } } diff --git a/crates/ui-egui/src/layoutview/tests.rs b/crates/ui-egui/src/layoutview/tests.rs index 7a563e0d3e..5cde32bdc7 100644 --- a/crates/ui-egui/src/layoutview/tests.rs +++ b/crates/ui-egui/src/layoutview/tests.rs @@ -1460,6 +1460,48 @@ fn a_text_element_takes_italic_alignment_and_spacing() { assert_eq!(said, !rustgis_engine::render::fonts::serif_available()); } +/// A north arrow's Font in the Format pane (#3467): Serif picked from the list is stored as one +/// undoable step. +#[test] +fn a_north_arrows_font_is_picked_in_the_format_pane() { + use egui::accesskit::Role; + use egui_kittest::kittest::Queryable; + use rustgis_engine::map::layout::{ElementKind, FontFamily}; + let mut app = RustGisApp::with_sample(); + app.run("layout.new", json!({"name": "Compass"})).unwrap(); + let li = app.session.active_layout.unwrap(); + let arrow = app.session.project.layouts[li].elements.iter().find(|e| e.name == "North Arrow").unwrap().id; + app.layout.selected = Some(arrow); + let family = move |app: &RustGisApp| match &app.session.project.layouts[li].element(arrow).unwrap().kind { + ElementKind::NorthArrow { font, .. } => *font, + other => panic!("{other:?}"), + }; + let mut fonts = false; + let mut h = egui_kittest::Harness::builder().with_size(vec2(1200.0, 900.0)).build_ui_state( + move |ui, app: &mut RustGisApp| { + if !fonts { + crate::theme::install_fonts(ui.ctx(), &[], None); + fonts = true; + return; + } + with_format(app, ui); + }, + app, + ); + h.run_steps(3); + assert_eq!(family(h.state()), FontFamily::Sans); + h.get_by_role_and_label(Role::ComboBox, "Font").click(); + h.run_steps(2); + h.get_by_label("Serif").click(); + h.run_steps(3); + assert_eq!(family(h.state()), FontFamily::Serif); + // Said where no serif is installed, and only there. + let said = h.query_by_label_contains("No serif font is installed").is_some(); + assert_eq!(said, !rustgis_engine::render::fonts::serif_available()); + h.state_mut().run("edit.undo", json!({})).unwrap(); + assert_eq!(family(h.state()), FontFamily::Sans, "one step undoes it"); +} + /// A text element's "Wrap to the box" (#2346): off as it is added, on when ticked, as one /// undoable step, and off again. #[test] diff --git a/crates/ui-egui/src/textform.rs b/crates/ui-egui/src/textform.rs index 0b19586db0..7e4bd62c2a 100644 --- a/crates/ui-egui/src/textform.rs +++ b/crates/ui-egui/src/textform.rs @@ -381,110 +381,115 @@ pub fn show( } if let Some(fam) = f.font { let mut no_name = None; - let name = f.font_name.unwrap_or(&mut no_name); - // The installed families (#762) once indexed, on a thread of their own; until then the - // two generic ones. - let installed = if f.named_fonts { families::installed_now() } else { None }; - if f.named_fonts && installed.is_none() { - families::warm_up(); - ui.ctx().request_repaint_after(std::time::Duration::from_millis(250)); - } - let missing = name.as_deref().is_some_and(|n| installed.is_some() && families::find_ready(n).is_none()); - let shown = match name.as_deref() { - Some(n) if missing => crate::i18n::fmt("{name} (not installed)", &[("name", n)]), - Some(n) => n.to_string(), - None => tl!(fam.label()).to_string(), - }; - let input_label = ui.label(tl!("Font")); - let search_id = id.with("font_search"); - let list = egui::ComboBox::from_id_salt(id.with("font")) - .selected_text(shown) - .height(320.0) - .close_behavior(crate::labelpane::SEARCHED_LIST_CLOSES) - .show_ui(ui, |ui| { - // Sans-serif and Serif first, then the installed families, found by typing. - let search = - if installed.is_some() { crate::labelpane::list_search(ui, search_id, tl!("Search fonts")) } else { String::new() }; - for v in FontFamily::ALL { - if ui.selectable_label(name.is_none() && *fam == v, tl!(v.label())).clicked() { - if name.is_some() || *fam != v { - (*fam, *name) = (v, None); - out.font = true; - } - crate::labelpane::end_search(ui, search_id); - } - } - let Some(installed) = installed else { return }; - ui.separator(); - // A family named as a generic one ("Serif") is the generic one: not listed again. - let mut matching = installed.iter().filter(|f| { - f.listed - && FontFamily::from_name(&f.name).is_none() - && (search.is_empty() || f.name.to_lowercase().contains(&search)) - }); - for v in matching.by_ref().take(MAX_FONTS_SHOWN) { - let on = name.as_deref().is_some_and(|n| n.eq_ignore_ascii_case(&v.name)); - if ui.selectable_label(on, &v.name).clicked() { - if !on { - // Its fallback, from its class, as `layout.update` gives it. - (*fam, *name) = (if v.serif { FontFamily::Serif } else { FontFamily::Sans }, Some(v.name.clone())); - out.font = true; - } - crate::labelpane::end_search(ui, search_id); - } + out.font = font_rows(ui, id, fam, f.font_name.unwrap_or(&mut no_name), f.named_fonts); + } + }); + out +} + +/// A grid's Font row, its Fallback row for a named family, and the note when the family isn't drawn +/// as asked. Whether the choice changed. +pub(crate) fn font_rows(ui: &mut Ui, id: egui::Id, fam: &mut FontFamily, name: &mut Option, named_fonts: bool) -> bool { + let mut changed = false; + // The installed families (#762) once indexed, on a thread of their own; until then the + // two generic ones. + let installed = if named_fonts { families::installed_now() } else { None }; + if named_fonts && installed.is_none() { + families::warm_up(); + ui.ctx().request_repaint_after(std::time::Duration::from_millis(250)); + } + let missing = name.as_deref().is_some_and(|n| installed.is_some() && families::find_ready(n).is_none()); + let shown = match name.as_deref() { + Some(n) if missing => crate::i18n::fmt("{name} (not installed)", &[("name", n)]), + Some(n) => n.to_string(), + None => tl!(fam.label()).to_string(), + }; + let input_label = ui.label(tl!("Font")); + let search_id = id.with("font_search"); + let list = egui::ComboBox::from_id_salt(id.with("font")) + .selected_text(shown) + .height(320.0) + .close_behavior(crate::labelpane::SEARCHED_LIST_CLOSES) + .show_ui(ui, |ui| { + // Sans-serif and Serif first, then the installed families, found by typing. + let search = + if installed.is_some() { crate::labelpane::list_search(ui, search_id, tl!("Search fonts")) } else { String::new() }; + for v in FontFamily::ALL { + if ui.selectable_label(name.is_none() && *fam == v, tl!(v.label())).clicked() { + if name.is_some() || *fam != v { + (*fam, *name) = (v, None); + changed = true; } - let more = matching.count(); - if more > 0 { - ui.weak(crate::i18n::fmt("{count} more: type part of a name", &[("count", &more.to_string())])); + crate::labelpane::end_search(ui, search_id); + } + } + let Some(installed) = installed else { return }; + ui.separator(); + // A family named as a generic one ("Serif") is the generic one: not listed again. + let mut matching = installed.iter().filter(|f| { + f.listed && FontFamily::from_name(&f.name).is_none() && (search.is_empty() || f.name.to_lowercase().contains(&search)) + }); + for v in matching.by_ref().take(MAX_FONTS_SHOWN) { + let on = name.as_deref().is_some_and(|n| n.eq_ignore_ascii_case(&v.name)); + if ui.selectable_label(on, &v.name).clicked() { + if !on { + // Its fallback, from its class, as `layout.update` gives it. + (*fam, *name) = (if v.serif { FontFamily::Serif } else { FontFamily::Sans }, Some(v.name.clone())); + changed = true; } - }) - .response - .labelled_by(input_label.id); - // Named for assistive technology, as its label sits in the other column. - list.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, tl!("Font"))); - ui.end_row(); - // A named family is drawn in its fallback where it isn't installed (a project from - // another computer): which one, chosen here. - if name.is_some() { - let input_label = ui.label(tl!("Fallback")); - let list = egui::ComboBox::from_id_salt(id.with("fallback")) - .selected_text(tl!(fam.label())) - .show_ui(ui, |ui| { - for v in FontFamily::ALL { - if ui.selectable_label(*fam == v, tl!(v.label())).clicked() && *fam != v { - *fam = v; - out.font = true; - } - } - }) - .response - .labelled_by(input_label.id) - .on_hover_text(tl!("Where the font isn't installed, the text is drawn in this family")); - list.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, tl!("Fallback"))); - ui.end_row(); + crate::labelpane::end_search(ui, search_id); + } } - // A text not drawn as asked says so here, so the page never differs from what was - // asked unsaid: a family this computer hasn't, or a serif where none is installed. - let serif_drawn = *fam == FontFamily::Serif && rustgis_engine::render::fonts::serif_available(); - let note = if missing { - Some(if serif_drawn { - tl!("This font isn't installed here, so the text is drawn in the serif") - } else { - tl!("This font isn't installed here, so the text is drawn in the sans-serif") - }) - } else if name.is_none() && *fam == FontFamily::Serif && !serif_drawn { - Some(tl!("No serif font is installed, so this text is drawn in the sans-serif")) - } else { - None - }; - if let Some(note) = note { - ui.label(""); - ui.label(egui::RichText::new(note).weak().small()); - ui.end_row(); + let more = matching.count(); + if more > 0 { + ui.weak(crate::i18n::fmt("{count} more: type part of a name", &[("count", &more.to_string())])); } - } - }); - out + }) + .response + .labelled_by(input_label.id); + // Named for assistive technology, as its label sits in the other column. + list.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, tl!("Font"))); + ui.end_row(); + // A named family is drawn in its fallback where it isn't installed (a project from + // another computer): which one, chosen here. + if name.is_some() { + let input_label = ui.label(tl!("Fallback")); + let list = egui::ComboBox::from_id_salt(id.with("fallback")) + .selected_text(tl!(fam.label())) + .show_ui(ui, |ui| { + for v in FontFamily::ALL { + if ui.selectable_label(*fam == v, tl!(v.label())).clicked() && *fam != v { + *fam = v; + changed = true; + } + } + }) + .response + .labelled_by(input_label.id) + .on_hover_text(tl!("Where the font isn't installed, the text is drawn in this family")); + list.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, tl!("Fallback"))); + ui.end_row(); + } + // A text not drawn as asked says so here, so the page never differs from what was + // asked unsaid: a family this computer hasn't, or a serif where none is installed. + let serif_drawn = *fam == FontFamily::Serif && rustgis_engine::render::fonts::serif_available(); + let note = if missing { + Some(if serif_drawn { + tl!("This font isn't installed here, so the text is drawn in the serif") + } else { + tl!("This font isn't installed here, so the text is drawn in the sans-serif") + }) + } else if name.is_none() && *fam == FontFamily::Serif && !serif_drawn { + Some(tl!("No serif font is installed, so this text is drawn in the sans-serif")) + } else { + None + }; + if let Some(note) = note { + ui.label(""); + ui.add(egui::Label::new(egui::RichText::new(note).weak().small()).wrap()); + ui.end_row(); + } + changed } #[cfg(test)] diff --git a/docs/maintaining.md b/docs/maintaining.md index 9f58f2c75f..39c87921ce 100644 --- a/docs/maintaining.md +++ b/docs/maintaining.md @@ -318,6 +318,14 @@ text size outside 4 to 100 points or a count of decimals past 6 is brought into one that is no count reads as automatic. Saved `layout.addElement` commands for a colorbar with either require it too, as does a saved `layout.import` whose `text` has such a colorbar. +Projects with a layout north arrow whose `N` has a font of its own (`font` or `font_name` on a +north arrow) require `layout-north-arrow-font`, so an older reader refuses them instead of drawing +the sans-serif and saving the arrow again without (#3467). Saved `layout.addElement` commands for a +north arrow with a font other than the sans require it too. A saved `layout.import` or +`report.existingTemplate` carrying such an arrow requires it as well, as does a saved +`project.openText` whose project needs it. A saved `layout.update` doesn't: it names no element +kind, and an older reader refuses a font on a north arrow when the step runs. + Projects with a map frame whose border isn't the default dark grey 0.8 pt line (`border_color`, `border_width` on a map frame, a width of 0 drawing none) require `layout-frame-border`, so an older reader refuses them instead of drawing the default border and saving the frame again diff --git a/docs/user-guide/layouts.md b/docs/user-guide/layouts.md index d71178f205..c7f645d558 100644 --- a/docs/user-guide/layouts.md +++ b/docs/user-guide/layouts.md @@ -431,7 +431,9 @@ Layouts open as tabs next to the maps; while one is open, its groups lead the Pu - **Map surrounds**: north arrow, scale bar, legend, colorbar, table frame (a layer's attribute table) and chart frame (a bar chart of a numeric field), plus a neatline or backdrop. A north arrow's properties pick its **Style** (classic arrow, compass rose, triangle or star) and its **Fill** - and **Outline** colours (`"style":"compass","fill":"#B91C1C"`). A scale bar's pick its + and **Outline** colours (`"style":"compass","fill":"#B91C1C"`), and the **Font** of its N + (sans-serif by default), with its **Fallback**, both chosen as a text's are (`"font":"serif"`, + or an installed family's name, with `"fallback"` if wanted). A scale bar's pick its **Style** (alternating bar, stepped line or single line), its **Units** (metres or kilometres to suit the scale, or kilometres, metres, miles or feet), its **Divisions** (1 to 10), each labelled where there is room, its **Color** (the lines, the dark divisions and the labels), its diff --git a/schema/fixtures/layout_north_arrow_font.rgis b/schema/fixtures/layout_north_arrow_font.rgis new file mode 100644 index 0000000000..1a48a3f6f6 --- /dev/null +++ b/schema/fixtures/layout_north_arrow_font.rgis @@ -0,0 +1 @@ +{"format":2,"requires":["layout-north-arrow-font"],"name":"North arrow font","maps":[{"name":"Map","layers":[],"basemap":"Paper","camera":{"center":{"x":0.0,"y":2000000.0},"res":30000.0,"bearing":0.0,"pitch":0.0},"bookmarks":[],"graticule":true,"projection":"WebMercator","popup_mode":"Topmost","label_display":{"paused":false,"locked":false,"show_unplaced":false},"editing":{"relationships":[],"snapping":{"enabled":false,"tolerance_px":10.0,"vertex":true,"edge":true,"end":true},"topology":[],"settings":{"enabled":true,"show_vertices":true,"elevation":"None","ground_to_grid":null}},"swipe":null,"view":{"effects":{"stars":true,"sun_time":null,"weather":"None","vignette":false,"edges":false,"tilt_shift":false,"grain":false,"brightness":0.0,"contrast":0.0,"saturation":0.0,"sepia":0.0},"wireframe":false,"clip_distance":null,"terrain":null},"animation":[],"next_layer_id":1}],"active_map":0,"tables":[],"next_table_id":1,"gp_history":[],"layouts":[{"name":"Listed","width":120,"height":90,"elements":[{"id":1,"name":"Frame","x":10,"y":10,"w":60,"h":70,"kind":{"MapFrame":{"map":"Map","center":{"x":0,"y":0},"scale":1000000}}},{"id":2,"name":"North Arrow","x":90,"y":10,"w":16,"h":24,"kind":{"NorthArrow":{"frame":1,"font":"Serif","font_name":"DejaVu Serif"}}}]}],"folders":[],"styles":[],"ramps":[],"tasks":[],"gp_env":{"extent":null,"cell_size":null},"models":[],"workspaces":[],"link_views":false,"jobs":[],"offline_areas":[],"reports":[],"presentations":[],"notebooks":[],"dl_models":[],"shared":[],"data_stores":[]} diff --git a/schema/project.schema.json b/schema/project.schema.json index 6fa3014cd4..00562ae933 100644 --- a/schema/project.schema.json +++ b/schema/project.schema.json @@ -1281,6 +1281,7 @@ "layout-chart-outline", "layout-table-look", "layout-colorbar-labels", + "layout-north-arrow-font", "field-generation", "field-package", "field-conflicts", @@ -1424,6 +1425,7 @@ "layout-chart-outline", "layout-table-look", "layout-colorbar-labels", + "layout-north-arrow-font", "field-generation", "field-package", "field-conflicts",