mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-19 17:58:10 +00:00
Type: Kinsoku sets keep fitting hanging marks and sets added while open
- text.setFormat's glyphForm again takes away the characters' glyph choice for a form other than "default", as text.setRangeStyle does. - With Burasagari, a line may end after a custom set's hanging punctuation where it fits, not only where it overflows and hangs. - A break after spaces checks Cannot End Line against the character before the spaces, which ends the line once they are left off. - Kinsoku Shori Settings OK keeps a set the document gained while the dialog was open, and a sets field that is not a list of sets goes to type.kinsokuSets.set as it is, which refuses it.
This commit is contained in:
@@ -834,7 +834,7 @@ fn set_format(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
st.digits = v;
|
||||
}
|
||||
if let Some(v) = glyph_form {
|
||||
st.glyph_form = v;
|
||||
st.set_glyph_form(v);
|
||||
}
|
||||
if let Some(v) = &language {
|
||||
st.language.clone_from(v);
|
||||
|
||||
@@ -632,7 +632,10 @@ pub(super) fn break_line<'k>(
|
||||
ink += cluster_ink;
|
||||
back += cluster_back;
|
||||
let last = &g[cluster_end - 1];
|
||||
if last.break_after() && last.len > 0 && kinsoku_allows(g, cluster_end - 1, kinsoku) && !splits_no_break(g, cluster_end) {
|
||||
// Hanging punctuation that would hang alone where it overflows may end the line where it
|
||||
// fits too: a custom set's may be a character no other rule breaks after.
|
||||
let hang_break = burasagari != Burasagari::None && cluster_end == j + 1 && hangs(last, kinsoku);
|
||||
if (last.break_after() || hang_break) && last.len > 0 && kinsoku_allows(g, cluster_end - 1, kinsoku) && !splits_no_break(g, cluster_end) {
|
||||
// Once the line holds a tab, whether a break fits as drawn is settled when the line ends.
|
||||
let hyphenated = last.is_soft_hyphen();
|
||||
if !hyphenated || (tabbed && fits.is_some()) || x + hyphen_glyph(last).adv <= width + EPS {
|
||||
@@ -822,11 +825,19 @@ fn splits_no_break(g: &[SGlyph], end: usize) -> bool {
|
||||
|
||||
/// Does kinsoku allow a line break after glyph `j`? Not after an opening bracket, nor before a
|
||||
/// closing one, a comma, a full stop, a small kana… (the character is pushed to the next line
|
||||
/// with the one before it).
|
||||
/// with the one before it). A line that breaks after spaces is drawn without them, so the
|
||||
/// character before them is the one that ends it: up to [`KINSOKU_SPACES_BACK`] spaces are looked
|
||||
/// past, and after a longer run of spaces the space itself counts.
|
||||
fn kinsoku_allows(g: &[SGlyph], j: usize, kinsoku: KinsokuRules<'_>) -> bool {
|
||||
g.get(j).is_some_and(|gl| kinsoku_between(gl.ch, g.get(j + 1).map(|n| n.ch), kinsoku))
|
||||
let Some(gl) = g.get(j) else { return false };
|
||||
let before = if gl.is_space() { g[..j].iter().rev().take(KINSOKU_SPACES_BACK).find(|h| !h.is_space()).map_or(gl.ch, |h| h.ch) } else { gl.ch };
|
||||
kinsoku_between(before, g.get(j + 1).map(|n| n.ch), kinsoku)
|
||||
}
|
||||
|
||||
/// The most spaces [`kinsoku_allows`] looks past for the character that ends a line, so a long run
|
||||
/// of spaces is not read again at each of its breaks.
|
||||
const KINSOKU_SPACES_BACK: usize = 64;
|
||||
|
||||
/// Kinsoku for a break between `before` and `after` (none: the end of the paragraph): Hard keeps
|
||||
/// every character of the set off the line start and end, Soft lets 々, ー and small kana start a
|
||||
/// line, a custom set keeps its own characters off them ([`KinsokuRules`]), and None allows the
|
||||
|
||||
@@ -1479,6 +1479,11 @@ fn a_custom_set_keeps_its_own_characters_off_line_ends() {
|
||||
let bracket = "一二三」四";
|
||||
assert_eq!(end(bracket, 35.0, Kinsoku::Hard.into()), 2, "Hard keeps 」 off the next line's start");
|
||||
assert_eq!(end(bracket, 35.0, breaking::KinsokuRules::Custom(&empty)), 3, "the empty set lets 」 start it");
|
||||
// A space after 三 is not drawn at the end of the line, so a break after it would still end
|
||||
// the line with 三.
|
||||
let spaced = "一二三 四";
|
||||
assert_eq!(end(spaced, 35.0, breaking::KinsokuRules::Custom(&finish)), 2, "三 does not end the line before a space");
|
||||
assert_eq!(end(spaced, 35.0, breaking::KinsokuRules::Custom(&empty)), 4);
|
||||
let ends = candidates(text, &full_width(text), None, breaking::KinsokuRules::Custom(&start)).iter().map(|b| b.end).collect::<Vec<_>>();
|
||||
assert_eq!(ends, [1, 2, 4, 5]);
|
||||
}
|
||||
@@ -1520,6 +1525,13 @@ fn a_custom_set_hangs_its_own_hanging_punctuation() {
|
||||
assert_eq!(end("一二三、四", breaking::KinsokuRules::Custom(&comma)), 4, "、 hangs");
|
||||
assert_eq!(end("一二三、四", breaking::KinsokuRules::Custom(&bang)), 3, "、 does not hang");
|
||||
assert_eq!(end("一二三!四", breaking::KinsokuRules::Custom(&bang)), 4, "! hangs");
|
||||
// In a wider line where ! fits, the line still ends after it.
|
||||
let text = "一二三!四";
|
||||
let wide = |b: Burasagari| {
|
||||
break_line(text, &full_width(text), 0, 40.0, &[], 0.0, None, b, breaking::KinsokuRules::Custom(&bang), PushOutOnly, false, false, &[], None).0
|
||||
};
|
||||
assert_eq!(wide(Burasagari::Standard), 4, "! fits and ends the line");
|
||||
assert_eq!(wide(Burasagari::None), 3, "without Burasagari ! does not hang, and no line ends after it");
|
||||
// A hanging character that starts an inseparable sequence kept with Bunri-Kinshi does not hang
|
||||
// the rest of the sequence with it: the sequence goes to the next line.
|
||||
let joined = custom_set("", "", "!", &["!あ"]);
|
||||
|
||||
@@ -6,7 +6,8 @@
|
||||
//! which runs `type.kinsokuSets.set` with every set as one undo step; Cancel leaves them as they were.
|
||||
//!
|
||||
//! Fields: `__doc` (the uid of the document the dialog opened on, which OK sets the sets of),
|
||||
//! `sets` (the sets as `type.kinsokuSets.set` takes them, each with the name it had in the
|
||||
//! `__opened` (the names of that document's sets when the dialog opened; OK keeps the sets the
|
||||
//! document gained since, after the dialog's own), `sets` (the sets as `type.kinsokuSets.set` takes them, each with the name it had in the
|
||||
//! document as `from`), `selected` (the index of the one picked), `name`, `cannotStart`,
|
||||
//! `cannotEnd`, `hanging` and `inseparable` (its name and categories as typed, the inseparable
|
||||
//! sequences separated by spaces), `base` (what New starts from), `code` (the code point Add
|
||||
@@ -34,6 +35,10 @@ pub const COMMAND: &str = vectorcraft_engine::cmd::kinsokusets::SET;
|
||||
/// refuses `__` fields).
|
||||
const DOC: &str = "__doc";
|
||||
|
||||
/// The field that holds the names of the document's sets when the dialog opened: OK keeps the
|
||||
/// sets the document gained since, which the dialog does not list.
|
||||
const OPENED: &str = "__opened";
|
||||
|
||||
/// The categories, as the command's keys, in the order the dialog shows them.
|
||||
const CATEGORIES: [&str; 4] = ["cannotStart", "cannotEnd", "hanging", "inseparable"];
|
||||
|
||||
@@ -44,24 +49,25 @@ pub(super) const SPEC: DialogSpec =
|
||||
pub fn open(app: &mut VectorcraftApp) -> Result<Value, String> {
|
||||
let st = app.session.active().ok_or("no document open")?;
|
||||
let uid = st.uid;
|
||||
let sets: Vec<Value> = st
|
||||
.doc
|
||||
.kinsoku_sets
|
||||
.iter()
|
||||
.map(|k| {
|
||||
let mut v = json!(k);
|
||||
if let Some(o) = v.as_object_mut() {
|
||||
o.insert("from".into(), json!(k.name));
|
||||
}
|
||||
v
|
||||
})
|
||||
.collect();
|
||||
let mut d = Dialog::new(KIND, json!({ DOC: uid, "sets": sets, "selected": 0, "base": "hard", "into": "cannotStart", "code": "" }));
|
||||
let sets: Vec<Value> = st.doc.kinsoku_sets.iter().map(listed).collect();
|
||||
let opened: Vec<&str> = st.doc.kinsoku_sets.iter().map(|k| k.name.as_str()).collect();
|
||||
let mut d =
|
||||
Dialog::new(KIND, json!({ DOC: uid, OPENED: opened, "sets": sets, "selected": 0, "base": "hard", "into": "cannotStart", "code": "" }));
|
||||
show_selected(&mut d);
|
||||
app.ui.dialog = Some(d);
|
||||
Ok(Value::Null)
|
||||
}
|
||||
|
||||
/// A set of the document as the dialog lists it: as `type.kinsokuSets.set` takes it, with its name
|
||||
/// in the document as `from`.
|
||||
fn listed(k: &KinsokuSet) -> Value {
|
||||
let mut v = json!(k);
|
||||
if let Some(o) = v.as_object_mut() {
|
||||
o.insert("from".into(), json!(k.name));
|
||||
}
|
||||
v
|
||||
}
|
||||
|
||||
/// The sets the dialog holds, as the command takes them.
|
||||
fn sets(d: &Dialog) -> Vec<Map<String, Value>> {
|
||||
d.fields.get("sets").and_then(Value::as_array).map(|a| a.iter().filter_map(|v| v.as_object().cloned()).collect()).unwrap_or_default()
|
||||
@@ -318,7 +324,21 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
};
|
||||
app.run("document.activate", json!({ "index": index }))?;
|
||||
}
|
||||
let r = app.run(COMMAND, json!({ "sets": list }));
|
||||
// A `sets` field that is not a list of sets goes to the command as it is, which refuses it.
|
||||
let raw = d.fields.get("sets").cloned().unwrap_or(Value::Null);
|
||||
let sets = if raw.as_array().is_some_and(|a| a.iter().all(Value::is_object)) {
|
||||
// A set the document gained while the dialog was open (a script, another command) is
|
||||
// kept, after the dialog's sets.
|
||||
let opened: Vec<&str> =
|
||||
d.fields.get(OPENED).and_then(Value::as_array).map(|a| a.iter().filter_map(Value::as_str).collect()).unwrap_or_default();
|
||||
if let Some(st) = app.session.active() {
|
||||
list.extend(st.doc.kinsoku_sets.iter().filter(|k| !opened.contains(&k.name.as_str())).filter_map(|k| listed(k).as_object().cloned()));
|
||||
}
|
||||
json!(list)
|
||||
} else {
|
||||
raw
|
||||
};
|
||||
let r = app.run(COMMAND, json!({ "sets": sets }));
|
||||
// A name two sets share, a reserved name or a category too long keeps the dialog open to be
|
||||
// changed, with its document active.
|
||||
if r.is_ok() {
|
||||
|
||||
@@ -244,6 +244,35 @@ fn ok_sets_the_document_the_dialog_opened_on() {
|
||||
assert!(app.session.documents().iter().all(|d| d.doc.kinsoku_sets.is_empty()));
|
||||
}
|
||||
|
||||
/// OK keeps a set the document gained while the dialog was open, after the dialog's sets, and a
|
||||
/// set deleted in the dialog stays deleted. A `sets` field that is not a list of sets keeps the
|
||||
/// dialog open and leaves the document's sets as they are.
|
||||
#[test]
|
||||
fn ok_keeps_sets_added_while_open_and_refuses_malformed_sets() {
|
||||
let mut app = app();
|
||||
app.run("type.kinsokuSets.set", json!({ "sets": [{"name": "First", "cannotStart": "ゝ"}] })).unwrap();
|
||||
open(&mut app).unwrap();
|
||||
let both = json!([{"name": "First", "cannotStart": "ゝ"}, {"name": "Later", "cannotStart": "ー"}]);
|
||||
app.run("type.kinsokuSets.set", json!({ "sets": both })).unwrap();
|
||||
dialog(&mut app).fields.insert("cannotStart".into(), json!("ゞ"));
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let sets = doc_sets(&app);
|
||||
let named = sets.iter().map(|k| (k.name.as_str(), k.cannot_start.clone())).collect::<Vec<_>>();
|
||||
assert_eq!(named, [("First", vec!['ゞ']), ("Later", vec!['ー'])]);
|
||||
open(&mut app).unwrap();
|
||||
press(&mut app, Action::Delete);
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
let names = |app: &VectorcraftApp| doc_sets(app).iter().map(|k| k.name.clone()).collect::<Vec<_>>();
|
||||
assert_eq!(names(&app), ["Later"]);
|
||||
for bad in [Value::Null, json!({}), json!("Later"), json!([1])] {
|
||||
open(&mut app).unwrap();
|
||||
dialog(&mut app).fields.insert("sets".into(), bad.clone());
|
||||
assert!(super::super::confirm(&mut app).is_err(), "{bad}");
|
||||
assert!(app.ui.dialog.is_some(), "{bad}");
|
||||
assert_eq!(names(&app), ["Later"], "{bad}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Add takes a code point as U+XXXX, u+xxxx or bare hexadecimal, and never a control or space
|
||||
/// character or a value that is no character.
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user