History and plug-in names: trim the fillers and blank padding at the ends too

trim_name drops white space, ZWNJ/ZWJ and the blank characters (Braille blank, Hangul fillers,
invisible operators, tag and other default-ignorable blanks) from both ends. A variation or
Mongolian free variation selector or CGJ that belongs to a kept character stays, and so do Hangul
filler jamo that complete a syllable. History names use the same trim as plug-in names.
This commit is contained in:
SBaksa committed 2026-11-08 22:40:53 +11:00
1 parent c2f85a590f
commit 28d2d9095d
4 files changed
+115 -7

No files matched your search

@@ -83,6 +83,15 @@ fn spaces_and_joiners_at_the_ends_are_trimmed_and_only_what_is_left_counts() {
(" \u{200C}Patch Tool ", "Patch Tool"),
("Love \u{2764}\u{FE0F}", "Love \u{2764}\u{FE0F}"),
(joined.as_str(), "Patch Tool"),
("Patch Tool\u{2800}", "Patch Tool"),
("\u{3164}\u{2800}Patch Tool\u{FFA0} \u{115F}", "Patch Tool"),
("Patch Tool\u{2065}\u{FFF0}", "Patch Tool"),
("\u{17B4}Patch Tool", "Patch Tool"),
("Patch Tool \u{FE0F}", "Patch Tool"),
("\u{FE0F}Patch Tool", "Patch Tool"),
("Patch \u{2764}\u{FE0F}\u{2800}", "Patch \u{2764}\u{FE0F}"),
("Patch \u{1100}\u{1160}", "Patch \u{1100}\u{1160}"),
("\u{115F}\u{1161} Patch", "\u{115F}\u{1161} Patch"),
];
for (name, stored) in cases.iter().copied().chain([(padded.as_str(), &padded[10..])]) {
s.execute("select.lasso", json!({"points": TRIANGLE, "historyName": name})).unwrap();
+5 -4
View File
@@ -638,13 +638,14 @@ fn lasso(s: &mut Session, p: &Value) -> Result<Value> {
}
/// The History step name for `historyName` `n`: refused when it has a control or line-break
/// character anywhere; otherwise without white space or zero-width (non-)joiners at its ends, so a
/// name that looks like "Patch Tool" is "Patch Tool", and then held to the plug-in menu-label rule.
/// character anywhere; otherwise without its invisible end padding (see
/// [`photocraft_plugins::manifest::trim_name`]), so a name that looks like "Patch Tool" is "Patch Tool",
/// and then held to the plug-in menu-label rule.
fn history_name(n: &str) -> Option<&str> {
if n.chars().any(|c| c.is_control() || matches!(c, '\u{2028}' | '\u{2029}')) {
return None;
}
let t = n.trim_matches(|c: char| c.is_whitespace() || matches!(c, '\u{200C}' | '\u{200D}'));
let t = photocraft_plugins::manifest::trim_name(n);
photocraft_plugins::manifest::printable_name(t).then_some(t)
}
@@ -778,7 +779,7 @@ pub fn specs() -> Vec<CommandSpec> {
"select.lasso",
"Lasso",
[],
r##"{"points":[[x,y],…],"mode":"replace|add|subtract|intersect"="replace","antiAlias":bool=true,"feather":px=0,"polygonal":bool=false,"historyName"?:text (1..64 characters with a visible one, no control, line-break or hidden format characters, spaces and joiners at the ends trimmed; the History step name when a tool other than the Lasso draws it, e.g. "Patch Tool"; default "Lasso")}"##,
r##"{"points":[[x,y],…],"mode":"replace|add|subtract|intersect"="replace","antiAlias":bool=true,"feather":px=0,"polygonal":bool=false,"historyName"?:text (1..64 characters with a visible one, no control, line-break or hidden format characters, invisible padding at the ends (spaces, joiners, fillers, blank Braille and other default-ignorable blanks, and selectors cut off by them) trimmed; the History step name when a tool other than the Lasso draws it, e.g. "Patch Tool"; default "Lasso")}"##,
has_doc,
lasso
),
+98 -3
View File
@@ -135,8 +135,9 @@ impl Manifest {
}
let text = std::str::from_utf8(bytes).map_err(|_| Error::Manifest("manifest is not UTF-8".into()))?;
let mut man: Manifest = serde_json::from_str(text).map_err(|e| Error::Manifest(format!("manifest: {e}")))?;
// The name is shown as stored, so white space and line breaks at its ends are dropped here.
man.name = man.name.trim().to_owned();
// The name is shown as stored, so the invisible padding at its ends (white space, line breaks,
// joiners, fillers) is dropped here.
man.name = trim_name(&man.name).to_owned();
man.validate()?;
Ok(man)
}
@@ -412,6 +413,77 @@ pub fn printable_name(name: &str) -> bool {
&& !name.chars().any(|c| c.is_control() || matches!(c, '\u{2028}' | '\u{2029}') || hidden_format(c))
}
/// `name` without what pads its ends invisibly: white space, the zero-width (non-)joiners and the
/// characters that draw nothing on their own (fillers, blank Braille, unassigned ignorables, stray
/// selectors), so a name that looks like "Tone" is "Tone". What belongs to a character the name
/// keeps stays: a variation selector after it (the emoji in "Love ❤️"), and a Hangul filler that
/// is part of a syllable block (U+1160 after a leading consonant, U+115F before a vowel).
pub fn trim_name(name: &str) -> &str {
let pad = |c: char| c.is_whitespace() || matches!(c, '\u{200C}' | '\u{200D}') || in_ranges(c, BLANK);
let mut start = name.len();
let mut chars = name.char_indices().peekable();
while let Some((i, c)) = chars.next() {
// The choseong filler opens a syllable before a vowel, and before the jungseong filler U+1160
// when a final consonant follows (U+115F U+1160 + a final consonant is how a lone final
// consonant is written; U+115F U+1160 alone draws nothing).
let opens_syllable = c == '\u{115F}'
&& chars.peek().is_some_and(|&(j, n)| {
vowel_jamo(n) || (n == '\u{1160}' && name.get(j + n.len_utf8()..).and_then(|r| r.chars().next()).is_some_and(trailing_jamo))
});
if !pad(c) || opens_syllable {
start = i;
break;
}
}
let s = name.get(start..).unwrap_or_default();
let mut end = s.len();
for (i, c) in s.char_indices().rev() {
if !pad(c) {
break;
}
end = i;
}
// Take back the mark that belongs to the last character kept: one per character, so a run of
// selectors after it is still padding.
let mut prev = s.get(..end).and_then(|h| h.chars().next_back());
for c in s.get(end..).unwrap_or_default().chars() {
let belongs = match prev {
Some(p) => attaches(c) || (c == '\u{1160}' && leading_jamo(p)),
None => false,
};
if !belongs {
break;
}
end += c.len_utf8();
prev = if attaches(c) { None } else { Some(c) };
}
s.get(..end).unwrap_or_default()
}
/// The marks among [`BLANK`] that modify the character before them: the combining grapheme joiner,
/// the Khmer inherent vowels, the Mongolian free variation selectors and the variation selectors.
fn attaches(c: char) -> bool {
matches!(
c,
'\u{34F}' | '\u{17B4}' | '\u{17B5}' | '\u{180B}'..='\u{180D}' | '\u{180F}' | '\u{FE00}'..='\u{FE0F}' | '\u{E0100}'..='\u{E01EF}'
)
}
/// A conjoining Hangul leading consonant (choseong), not the filler U+115F.
fn leading_jamo(c: char) -> bool {
matches!(c, '\u{1100}'..='\u{115E}' | '\u{A960}'..='\u{A97C}')
}
/// A conjoining Hangul final consonant (jongseong).
fn trailing_jamo(c: char) -> bool {
matches!(c, '\u{11A8}'..='\u{11FF}' | '\u{D7CB}'..='\u{D7FB}')
}
/// A conjoining Hangul vowel (jungseong) other than the vowel filler U+1160.
fn vowel_jamo(c: char) -> bool {
matches!(c, '\u{1161}'..='\u{11A7}' | '\u{D7B0}'..='\u{D7C6}')
}
/// A character that draws something on its own.
fn visible(c: char) -> bool {
!c.is_whitespace() && !c.is_control() && !in_ranges(c, FORMAT) && !in_ranges(c, BLANK)
@@ -661,7 +733,30 @@ mod tests {
}
// White space and line breaks at the ends are dropped from the stored name, so the menu never
// shows them, and padding does not count towards the limit.
for (padded, stored) in [("\nTone", "Tone"), ("Tone\r\n", "Tone"), ("\u{2028}Tone ", "Tone"), (" Tone… ", "Tone…")] {
for (padded, stored) in [
("\nTone", "Tone"),
("Tone\r\n", "Tone"),
("\u{2028}Tone ", "Tone"),
(" Tone… ", "Tone…"),
("Tone\u{2800}", "Tone"),
("\u{3164}\u{2800}Tone\u{FFA0}", "Tone"),
("\u{2764}\u{FE0F} Love\u{2800}", "\u{2764}\u{FE0F} Love"),
("Tone\u{2065}\u{FFF0}", "Tone"),
("\u{17B4}Tone", "Tone"),
// A selector right after the last kept character belongs to it and stays; one cut off by padding goes.
("Love \u{2764}\u{FE0F}\u{2800}", "Love \u{2764}\u{FE0F}"),
("Tone \u{FE0F}", "Tone"),
("\u{FE0F}Tone", "Tone"),
("Love \u{2764}\u{FE0F}\u{FE0F}\u{FE0F}", "Love \u{2764}\u{FE0F}"),
// Hangul filler jamo that complete a syllable are part of the name; the two fillers alone draw nothing.
("\u{115F}\u{1160}Tone", "Tone"),
("Tone \u{115F}\u{1160}", "Tone"),
("Tone \u{1100}\u{1160}", "Tone \u{1100}\u{1160}"),
("\u{115F}\u{1161} Tone", "\u{115F}\u{1161} Tone"),
("\u{115F}\u{1160}\u{11A8} Tone", "\u{115F}\u{1160}\u{11A8} Tone"),
// Khmer inherent vowels belong to the consonant before them.
("Tone \u{1780}\u{17B5}", "Tone \u{1780}\u{17B5}"),
] {
assert_eq!(with_name(padded).map(|m| m.name).ok().as_deref(), Some(stored), "{padded:?}");
}
assert_eq!(with_name(&format!("{}{}", "x".repeat(64), " ".repeat(1000))).map(|m| m.name.len()).ok(), Some(64));
+3
View File
@@ -100,6 +100,9 @@ exports. All integers are `i32` unless noted; pointers are offsets into the modu
- `name`: the menu label (1–64 printable characters). Any script works, but control characters and
Unicode format characters (bidi overrides and marks, zero-width spaces, soft hyphens) are rejected,
except the zero-width joiner and non-joiner, and at least one character must be visible.
Invisible padding at the ends (white space, joiners, Hangul fillers, blank Braille and other
default-ignorable blanks, and variation selectors cut off by them) is removed before these checks,
so it does not count towards the limit; marks that belong to a kept character stay.
`kind`: `"filter"` (the only kind in v1).
- `params`: in dialog order. Names are `[A-Za-z0-9_]`, not starting with `_`, not `id`/`layer`.
The host validates the user's values before calling: missing values take the default, numbers are