Merge pull request #10204 from dotCooCoo/fix/hyphenation-zone-every-line

Paragraph Style Options: Hyphenation Zone is disabled with an every-line composer
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-03 07:20:58 -08:00
commit d745ca5090
3 files changed
+134 -8

No files matched your search

@@ -42,6 +42,9 @@ const DISTANCES: [&str; 5] = ["left_indent", "right_indent", "first_line_indent"
/// The range of the indents, the space before and after, and the Hyphenation Zone (pt), as
/// `text.setFormat` keeps them.
const MAX_DISTANCE: f64 = 1296.0;
/// The composers that take a Hyphenation Zone. With Composition unset, the style follows the
/// paragraph's composer and the zone stays available.
const SINGLE_LINE_COMPOSERS: [&str; 2] = ["singleLine", "standardSingleLine"];
/// The whole-number fields of Hyphenation: (field, `hyphenation` key, range).
const COUNTS: [(&str, &str, (f64, f64)); 4] = [
("words_longer_than", "words_longer_than", (1.0, 25.0)),
@@ -309,6 +312,28 @@ pub(crate) fn edited_attrs(fields: &DialogFields, opening: &Opening) -> Result<M
let original = &opening.attrs;
let unit = opening.unit;
let original_fields = canonical_map(fields_from_attrs(original, unit));
// A disabled field with an invalid value keeps the style's value (none after a reset of
// Hyphenation), so the field cannot block OK.
let blocked: Vec<&str> = HYPHENATION_FIELDS
.into_iter()
.filter(|key| !field_enabled(&d, key) && d.get(key) != original_fields.get(*key) && !valid(&d, key, unit))
.collect();
let normalized;
let d = if blocked.is_empty() {
d
} else {
let mut kept = fields.clone();
for key in blocked {
match original_fields.get(key).and_then(Value::as_str).filter(|_| !d.reset("hyphenation")) {
Some(stored) => kept.set(key, stored),
None => {
kept.remove(key);
}
}
}
normalized = kept;
Values::new(&normalized)
};
let mut attrs = original.clone();
let unchanged = |key: &str, tab: &str| !d.reset(tab) && d.get(key) == original_fields.get(key);
let keep_or = |attrs: &mut Map<String, Value>, key: &str, unchanged: bool, value: Option<Value>| {
@@ -678,13 +703,45 @@ fn composition_rows() -> Vec<PropertyRow> {
rows(vec![select("composer", tl!("Composition"), &composers), tri_state("roman_hanging", tl!("Roman Hanging Punctuation"))])
}
// PLAN: Hyphenation Zone is gated on Hyphenation alone; the Single-line Composer condition needs a gate on two keys.
fn hyphenation_rows() -> Vec<PropertyRow> {
/// Whether Hyphenation Zone needs Composition unset or a single-line composer to take input.
fn needs_single_line(key: &str) -> bool {
key == "hyphenation_zone"
}
/// Whether field `key` takes input: the Hyphenation options while Hyphenation is on, and
/// Hyphenation Zone only while Composition is also unset or a single-line composer. Every other
/// field takes input.
fn field_enabled(d: &Values, key: &str) -> bool {
if !HYPHENATION_FIELDS.contains(&key) {
return true;
}
d.get("hyphenate").and_then(Value::as_bool) == Some(true)
&& (!needs_single_line(key)
|| d.get("composer").and_then(Value::as_str).is_none_or(|composer| composer.is_empty() || SINGLE_LINE_COMPOSERS.contains(&composer)))
}
/// Whether the text of Hyphenation field `key` is a value the field accepts.
fn valid(d: &Values, key: &str, unit: Unit) -> bool {
match COUNTS.iter().find(|(field, _, _)| *field == key) {
Some((field, _, (lo, hi))) => count(d, field, *lo, *hi).is_ok(),
None if key == "hyphenation_zone" => distance(d, key, unit, 0.0, MAX_DISTANCE).is_ok(),
None => true,
}
}
/// The Hyphenation options take input as [`field_enabled`] says: Hyphenation Zone applies only
/// with a single-line composer, so it is disabled while Composition holds another one.
fn hyphenation_rows(fields: &DialogFields) -> Vec<PropertyRow> {
let on = FieldValue::Bool(true);
let composer_set = fields.text("composer").is_some_and(|composer| !composer.is_empty());
let gate = |key: &str, param: Param| {
let param = param.enabled_when("hyphenate", on.clone());
if needs_single_line(key) && composer_set { param.and_enabled_when_in("composer", SINGLE_LINE_COMPOSERS) } else { param }
};
let mut params = vec![tri_state("hyphenate", tl!("Hyphenation"))];
params.extend(COUNTS.iter().map(|(key, _, _)| text(*key, label_of(key)).enabled_when("hyphenate", on.clone())));
params.push(text("hyphenation_zone", label_of("hyphenation_zone")).enabled_when("hyphenate", on.clone()));
params.push(tri_state("hyphenate_capitalized", tl!("Hyphenate Capitalized Words")).enabled_when("hyphenate", on));
params.extend(COUNTS.iter().map(|(key, _, _)| gate(key, text(*key, label_of(key)))));
params.push(gate("hyphenation_zone", text("hyphenation_zone", label_of("hyphenation_zone"))));
params.push(gate("hyphenate_capitalized", tri_state("hyphenate_capitalized", tl!("Hyphenate Capitalized Words"))));
rows(params)
}
@@ -737,7 +794,7 @@ pub(crate) fn tabs(fields: &DialogFields, opening: &Opening) -> Vec<PropertiesTa
basic_rows(),
tabs_rows(fields, opening),
composition_rows(),
hyphenation_rows(),
hyphenation_rows(fields),
justification_rows(),
east_asian_rows(),
];
@@ -200,10 +200,14 @@ fn invalid_values_keep_the_dialog_open() {
("word_min", "120"),
] {
open(&mut app, "Body");
// The hyphenation rows take input while Hyphenation is on.
// The hyphenation rows take input while Hyphenation is on, and Hyphenation Zone also with a
// single-line composer.
if HYPHENATION_COUNTS.contains(&key) {
set(&app, "hyphenate", json!(true));
}
if key == "hyphenation_zone" {
set(&app, "composer", json!("singleLine"));
}
set(&app, key, json!(value));
assert!(!ok(&mut app), "{key} = {value}");
assert!(is_open(&app), "{key} = {value}");
@@ -221,6 +225,70 @@ fn invalid_values_keep_the_dialog_open() {
assert_eq!(def(&app, "Body")["justification"]["auto_leading"], json!(150.0));
}
/// Hyphenation Zone takes input only while Hyphenation is on and Composition is unset or a
/// single-line composer.
#[test]
fn the_hyphenation_zone_is_disabled_for_every_line_composers_or_with_hyphenation_off() {
let mut app = app();
app.run("paraStyle.new", json!({"name": "Body", "attrs": {}})).unwrap();
for composer in [None, Some("singleLine"), Some("standardSingleLine"), Some("everyLine"), Some("standardEveryLine")] {
for hyphenate in [false, true] {
open(&mut app, "Body");
if let Some(composer) = composer {
set(&app, "composer", json!(composer));
}
set(&app, "hyphenate", json!(hyphenate));
let ctx = app.ui_ctx.clone().unwrap();
let changed = with_dialog(&app, |host, id| host.set_json_field(&ctx, id, "hyphenation_zone", json!("18")));
let takes_zone = matches!(composer, None | Some("singleLine" | "standardSingleLine"));
assert_eq!(changed.is_ok(), takes_zone && hyphenate, "composer {composer:?}, hyphenation {hyphenate}: {changed:?}");
cancel(&mut app);
}
}
}
/// An invalid zone left in the field does not block OK once an every-line composer disables it,
/// and the style keeps its stored zone.
#[test]
fn an_invalid_zone_does_not_block_a_change_to_an_every_line_composer() {
let mut app = app();
let attrs = json!({"composer": "singleLine", "hyphenate": true, "hyphenation": {"zone": 18.0}});
app.run("paraStyle.new", json!({"name": "Body", "attrs": attrs})).unwrap();
open(&mut app, "Body");
set(&app, "hyphenation_zone", json!("-1"));
assert!(!ok(&mut app), "the zone takes input and its value is invalid");
set(&app, "composer", json!("standardEveryLine"));
pump(&mut app);
assert!(ok(&mut app), "the disabled zone does not block OK");
let attrs = def(&app, "Body");
assert_eq!(attrs["composer"], json!("standardEveryLine"));
assert_eq!(attrs["hyphenation"]["zone"], json!(18.0));
}
/// An invalid value left in a Hyphenation option does not block OK once Hyphenation is turned off,
/// and the style keeps its stored options.
#[test]
fn invalid_hyphenation_options_do_not_block_turning_hyphenation_off() {
let mut app = app();
let hyphenation = json!({"words_longer_than": 5, "after_first": 3, "before_last": 3, "hyphen_limit": 2, "zone": 18.0, "capitalized": false});
for (key, value) in
[("words_longer_than", "26"), ("after_first", "16"), ("before_last", "1.5"), ("hyphen_limit", "-1"), ("hyphenation_zone", "-1")]
{
let attrs = json!({"composer": "singleLine", "hyphenate": true, "hyphenation": hyphenation});
app.run("paraStyle.new", json!({"name": key, "attrs": attrs})).unwrap();
let stored = def(&app, key);
open(&mut app, key);
set(&app, key, json!(value));
assert!(!ok(&mut app), "{key} = {value} takes input and is invalid");
set(&app, "hyphenate", json!(false));
pump(&mut app);
assert!(ok(&mut app), "{key} = {value} is disabled and does not block OK");
let attrs = def(&app, key);
assert_eq!(attrs["hyphenate"], json!(false), "{key}");
assert_eq!(attrs["hyphenation"], stored["hyphenation"], "{key} keeps the stored options");
}
}
/// Reset Panel clears only the section shown, and General has none.
#[test]
fn reset_panel_clears_only_its_section() {
+2 -1
View File
@@ -15868,7 +15868,8 @@ The `paraStyleOptions` dialog exposes `name`, `section` (`general`, `basic`, `ta
- Composition: `composer` (`singleLine`, `everyLine`, `standardSingleLine` or `standardEveryLine`) and the `roman_hanging` checkbox.
- Hyphenation: the `hyphenate` checkbox, `words_longer_than`, `after_first`, `before_last`, `hyphen_limit`,
`hyphenation_zone` and the `hyphenate_capitalized` checkbox. The options are inactive while Hyphenation is off, and
Hyphenation Zone is inactive with the Every-line Composer.
Hyphenation Zone is also inactive while `composer` is `everyLine` or `standardEveryLine`. An invalid value left in an
inactive option does not block OK, and the style keeps that option's stored value.
- Justification: `word_min`, `word_desired`, `word_max`, `letter_min`, `letter_desired`, `letter_max`, `glyph_min`,
`glyph_desired` and `glyph_max` (percent), `auto_leading` (percent) and `single_word` (`fullJustify`,
`alignLeft`, `alignCenter` or `alignRight`).