mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 12:38:18 +00:00
Type: the missing-font question takes its answer through tool.setOption
- `tool.setOption {key: "missingFontEdit", value: {token, ok}}` now passes
the Type tool's option check, so a client can answer the question that
a click on type in a missing font asks. Before, it failed as an unknown
option.
- A press on such type that reaches the Type tool while another dialog is
open (sent with `pointer_gesture`) leaves that dialog open and starts no
edit, instead of replacing it.
- docs/mcp.md says which double-clicks ask, and that OK starts no edit
when the document changed after the question opened.
- The doc comment of `GENERIC_FAMILIES` notes that `emoji` is kept
although CSS Fonts Module Level 4 dropped it.
This commit is contained in:
@@ -4129,3 +4129,34 @@ fn new_type_takes_the_normal_character_style_fill_not_the_current_fill() {
|
||||
let made = s.execute("text.create", &json!({"x": 100, "y": 400, "text": "Green", "color": "#00ff00"})).unwrap();
|
||||
assert_eq!(obj(&s, NodeId(made["id"].as_u64().unwrap())).runs[0].style.fill, Paint::solid(Color::rgb(0.0, 1.0, 0.0)));
|
||||
}
|
||||
|
||||
/// A client answers the question a Type tool click asks about type in a missing font through
|
||||
/// `tool.setOption {key: "missingFontEdit", value: {token, ok}}`: OK starts the edit at the next poll.
|
||||
#[test]
|
||||
fn tool_set_option_answers_the_missing_font_question() {
|
||||
let mut s = session();
|
||||
let r = s.execute("text.create", &json!({"x": 100, "y": 100, "text": "Hello", "size": 20, "font": "Missing Test Family 8408"})).unwrap();
|
||||
let id = NodeId(r["id"].as_u64().unwrap());
|
||||
s.execute("select.none", &json!({})).unwrap();
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("type", v).unwrap();
|
||||
let t = obj(&s, id);
|
||||
let (a, b) = vectorcraft_text::caret_position(&vectorcraft_text::layout(vectorcraft_text::FontDb::global(), &t), 2);
|
||||
let at = t.xf * a.midpoint(b);
|
||||
let mut asked = vec![];
|
||||
for k in [PointerKind::Down, PointerKind::Up] {
|
||||
asked.extend(s.pointer(&PointerEvent::new(k, at.x + 0.1, at.y), v).unwrap());
|
||||
}
|
||||
let token = asked
|
||||
.iter()
|
||||
.find_map(|r| match r {
|
||||
UiRequest::Dialog(k, p) if k == "missingFontEdit" => p["token"].as_u64(),
|
||||
_ => None,
|
||||
})
|
||||
.unwrap_or_else(|| panic!("the missing-font question: {asked:?}"));
|
||||
assert!(!s.tool_wants_text());
|
||||
s.set_tool_option_cmd(&json!({"key": "missingFontEdit", "value": {"token": token, "ok": true}})).unwrap();
|
||||
s.tool_poll(v, false).unwrap();
|
||||
assert!(s.tool_wants_text());
|
||||
assert_eq!(s.tool_options()["editing"], json!(id.0));
|
||||
}
|
||||
|
||||
@@ -433,9 +433,11 @@ impl FontMatch {
|
||||
}
|
||||
}
|
||||
|
||||
/// The CSS generic font families (CSS Fonts Module Level 4), which SVG and HTML files name in place
|
||||
/// of a font and no font file provides; compared in any ASCII case ([`is_generic_family`]). The
|
||||
/// list leaves out `fangsong`, which is also the family name of a Chinese font (FangSong).
|
||||
/// The generic font family names that SVG and HTML files name in place of a font and no font file
|
||||
/// provides; compared in any ASCII case ([`is_generic_family`]). The names follow CSS Fonts Module
|
||||
/// Level 4, except `emoji`, which that module dropped and this list keeps. The list does not match
|
||||
/// the functional `generic(...)` forms, and it leaves out `fangsong`, which is also the family name
|
||||
/// of a Chinese font (FangSong).
|
||||
pub const GENERIC_FAMILIES: &[&str] = &[
|
||||
"serif",
|
||||
"sans-serif",
|
||||
|
||||
@@ -2236,6 +2236,22 @@ fn a_click_on_type_in_a_missing_font_asks_before_editing() {
|
||||
assert_eq!((tool.editing, tool.caret), (Some(id), 1));
|
||||
}
|
||||
|
||||
/// `tool.setOption` checks each option first: the answer to the question is a whole-number token
|
||||
/// and a true or false `ok`, so a client can give it through that command.
|
||||
#[test]
|
||||
fn the_answer_to_the_missing_font_question_passes_the_option_check() {
|
||||
let tool = TypeTool::new("type");
|
||||
assert_eq!(tool.check_option("missingFontEdit", &json!({"token": 7, "ok": true})), Ok(()));
|
||||
assert_eq!(tool.check_option("missingFontEdit", &json!({"token": 7, "ok": false})), Ok(()));
|
||||
for bad in [json!({"token": "7", "ok": true}), json!({"token": 7, "ok": 1}), json!({"ok": true}), json!({"token": -1, "ok": true}), json!(null)] {
|
||||
assert_eq!(
|
||||
tool.check_option("missingFontEdit", &bad),
|
||||
Err("`missingFontEdit` must be {token, ok} with a whole number and true or false".to_string()),
|
||||
"{bad}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Cancel starts no edit, and the edit in progress on other type goes on; an answer that names
|
||||
/// another question, or one given to a document changed since, starts nothing either.
|
||||
#[test]
|
||||
|
||||
@@ -629,7 +629,8 @@ impl Tool for TypeTool {
|
||||
}
|
||||
}
|
||||
/// The calls [`Self::set_option`] takes: `commitTyping`, `endTyping`, `selectAll` and
|
||||
/// `stopEditing` take any value, the others the shapes they read.
|
||||
/// `stopEditing` take any value, the others the shapes they read (`missingFontEdit` a whole-number
|
||||
/// `token` and a true or false `ok`).
|
||||
fn check_option(&self, key: &str, v: &Value) -> Result<(), String> {
|
||||
let index = |a: &str, b: &str| v.get(a).or_else(|| v.get(b)).and_then(Value::as_u64).is_some();
|
||||
let ok = match key {
|
||||
@@ -638,11 +639,13 @@ impl Tool for TypeTool {
|
||||
"prevTool" | "previousTool" => return crate::option_check::string_or_null(key, v),
|
||||
"copy" => serde_json::from_value::<Vec<TextRun>>(v.clone()).is_ok(),
|
||||
"commitTyping" | "endTyping" | "selectAll" | "stopEditing" => true,
|
||||
"missingFontEdit" => v.get("token").and_then(Value::as_u64).is_some() && v.get("ok").and_then(Value::as_bool).is_some(),
|
||||
_ => return Err(crate::option_check::unknown(key)),
|
||||
};
|
||||
let shape = match key {
|
||||
"textSelection" => "{editing, anchor, caret} with whole numbers",
|
||||
"select" => "{start, end} with whole numbers",
|
||||
"missingFontEdit" => "{token, ok} with a whole number and true or false",
|
||||
_ => "a list of text runs",
|
||||
};
|
||||
if ok { Ok(()) } else { Err(format!("`{key}` must be {shape}")) }
|
||||
|
||||
@@ -9166,6 +9166,31 @@ fn double_clicking_type_in_a_missing_font_asks_before_editing() {
|
||||
assert!(t.runs.iter().all(|r| r.style.font_family == "Missing Test Family 7258"));
|
||||
}
|
||||
|
||||
/// A Type tool press that reaches type in a missing font while another dialog is open (as one sent
|
||||
/// with `ui.pointer` does) leaves that dialog open and starts no edit.
|
||||
#[test]
|
||||
fn a_press_on_type_in_a_missing_font_keeps_an_open_dialog() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.session.execute("file.new", &json!({"width": 400, "height": 300})).unwrap();
|
||||
let params = json!({"x": 100, "y": 150, "text": "Hello world", "size": 24, "font": "Missing Test Family 8408"});
|
||||
let id = app.session.execute("text.create", ¶ms).unwrap()["id"].as_u64().unwrap();
|
||||
app.session.execute("select.none", &json!({})).unwrap();
|
||||
app.select_tool("type");
|
||||
let doc = app.session.active().unwrap().uid;
|
||||
crate::dialogs::confirm::ask_replace_files(&mut app, doc, &["/tmp/a.svg".to_string()], &[], "file.export", json!({}));
|
||||
let b = app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().geometric_bounds().unwrap();
|
||||
let view = app.view_info();
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
crate::canvas::dispatch(&mut app, &PointerEvent::new(kind, b.center().x, b.center().y), view);
|
||||
}
|
||||
assert_eq!(app.ui.dialog.as_ref().map(|d| d.kind.as_str()), Some(crate::dialogs::confirm::REPLACE_FILES));
|
||||
crate::dialogs::cancel(&mut app);
|
||||
let r = app.session.tool_poll(view, false);
|
||||
crate::canvas::apply_requests(&mut app, r);
|
||||
assert!(app.ui.dialog.is_none());
|
||||
assert_eq!(app.session.tool_options()["editing"], json!(null));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escape_while_editing_in_isolation_returns_to_selection_tool_and_keeps_isolation_until_second_escape() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
|
||||
@@ -167,7 +167,13 @@ pub(super) const MISSING_FONT_EDIT_SPEC: DialogSpec = DialogSpec { confirm: edit
|
||||
/// tool names in `p` (`token`, `missing` and `style`: "family style" names, `more`: how many more
|
||||
/// there are). The detail names each font (family and style, the family's own names, which are
|
||||
/// not translated). OK starts the edit where the type was clicked; Cancel and Escape start nothing.
|
||||
/// While another dialog is open (a press sent with `ui.pointer` reaches the tool then), that dialog
|
||||
/// stays and the Type tool forgets the edit, as Cancel would.
|
||||
pub(crate) fn ask_missing_font_edit(app: &mut VectorcraftApp, p: &Value) {
|
||||
if app.ui.dialog.as_ref().is_some_and(|d| d.kind != MISSING_FONT_EDIT) {
|
||||
app.session.set_tool_option("missingFontEdit", &json!({"token": p.get("token"), "ok": false}));
|
||||
return;
|
||||
}
|
||||
let names = |k: &str| p.get(k).and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_str).map(str::to_string).collect::<Vec<_>>();
|
||||
let mut fonts = names("missing");
|
||||
fonts.extend(names("style"));
|
||||
|
||||
+7
-3
@@ -12416,9 +12416,13 @@ folder and no folder search.
|
||||
|
||||
A Type tool click, or a Selection or Direct Selection double-click, that would start editing type whose story (every
|
||||
frame threaded with it) is drawn in a font that isn't available as named, as `text.missingFonts` reads them, opens a
|
||||
`missingFontEdit` question instead (fields `message`, `detail`, `__token`) and does not start editing. `ui.dialog.confirm`
|
||||
starts the edit with the caret where the type was clicked; `ui.dialog.cancel` leaves the document, its undo history and
|
||||
the selection as they were. Answering does not change the fonts the type names, install a font or open Find Font. The
|
||||
`missingFontEdit` question instead (fields `message`, `detail`, `__token`) and does not start editing. The double-click
|
||||
is one on the canvas or `ui.click` with `count: 2`. A `pointer_gesture` `doubleclick` event only switches to the Type
|
||||
tool; send the Type tool's `down` and `up` after it to start the edit. `ui.dialog.confirm` starts the edit with the
|
||||
caret where the type was clicked; `ui.dialog.cancel` leaves the document, its undo history and the selection as they
|
||||
were. When the document changed after the question opened, or the type is gone, `ui.dialog.confirm` closes the question
|
||||
and starts no edit; click the type again before sending text. A `pointer_gesture` press on such type while another
|
||||
dialog is open leaves that dialog open and starts no edit. Answering does not change the fonts the type names, install a font or open Find Font. The
|
||||
question comes again for each new edit, and not for a click within the type being edited or in another frame of its
|
||||
story. The Type tool's `missingFontEdit {token, ok}` option is how the question gives its answer. Commands that change
|
||||
text (`text.editRange`, `text.setRangeStyle` and the others) do not ask. This follows the help page
|
||||
|
||||
Reference in New Issue
Block a user