mirror of
https://github.com/storytold/pdfcraft.git
synced 2026-11-17 00:04:18 +00:00
Merge upstream/main into fix/xfa-open-scripts-complete
Main's DataOp::Value now replaces a multi-select list's value-group picks in the packet and in the data copy, so the reset writes through it and the separate DataOp::Reset (and Want::picks) is dropped. The reset keeps the PR's indexed targets and its pass over fields without widgets, beside main's floating-field pass (which the unshown pass leaves embedded fields to), and finish() lays out again for either an owed relayout or a changed embedded value. Parity notes and evidence take both sides. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Pq7HyCHD1R3rywQBPCHXZA
This commit is contained in:
commit
f996aef12f
654 files changed
+99220
-5792
No files matched your search
@@ -163,6 +163,11 @@ pub fn recalculate_with(doc: &mut Document, scripts: &mut dyn Scripts) -> Result
|
||||
let current = f.value.first().cloned().unwrap_or_default();
|
||||
let v = if let Some(js) = &f.actions.scripts.calculate {
|
||||
let r = scripts.run(FieldEvent::Calculate, js, &f, ¤t, &all);
|
||||
// A script may set its own field through the field object (`getField("Total").value
|
||||
// = …`) rather than through `event.value`. `apply_changes` leaves the event's own
|
||||
// field to the event, so that value is the result unless the script also assigned
|
||||
// `event.value`, even the value it already had.
|
||||
let own = r.changes.iter().find(|c| c.name == f.name).and_then(|c| c.value.as_ref()).map(|v| v.first().cloned().unwrap_or_default());
|
||||
scripting::apply_changes(doc, &r.changes, &f.name)?;
|
||||
if !r.changes.is_empty() {
|
||||
all = fields(doc);
|
||||
@@ -170,13 +175,37 @@ pub fn recalculate_with(doc: &mut Document, scripts: &mut dyn Scripts) -> Result
|
||||
if !r.rc {
|
||||
continue;
|
||||
}
|
||||
r.value
|
||||
match own {
|
||||
Some(v) if !r.value_set => v,
|
||||
_ => r.value,
|
||||
}
|
||||
} else {
|
||||
let Some(v) = af::calculate(&f.actions.calculate, &lookup) else { continue };
|
||||
v
|
||||
};
|
||||
// A combo box result naming an option, by export value or display text, is stored as that
|
||||
// option's export value (the first element of an /Opt pair, ISO 32000-2 Table 234), as
|
||||
// `write_value` stores a choice, so /V, /I and exported form data agree.
|
||||
let combo = f.kind == FieldKind::Combo;
|
||||
let matched = if combo { option_named(&f, &v) } else { None };
|
||||
let v = match matched.and_then(|i| f.options.get(i)) {
|
||||
Some((export, _)) => export.clone(),
|
||||
None => v,
|
||||
};
|
||||
if current != v && matches!(f.kind, FieldKind::Text | FieldKind::Combo) {
|
||||
doc.update_dict(f.obj, |d| d.set(b"V".to_vec(), PdfString::text(&v)))?;
|
||||
// A combo box's /I (its option indices, ISO 32000-2 Table 234) follows the value, as
|
||||
// `write_value` keeps it, so the selection kept never disagrees with the value.
|
||||
let picked = matched.and_then(|i| i64::try_from(i).ok());
|
||||
doc.update_dict(f.obj, |d| {
|
||||
d.set(b"V".to_vec(), PdfString::text(&v));
|
||||
match picked {
|
||||
Some(i) if combo => d.set(b"I".to_vec(), Object::Array(vec![Object::Int(i)])),
|
||||
_ if combo => {
|
||||
d.remove(b"I");
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})?;
|
||||
redraw(doc, &f, std::slice::from_ref(&v), scripts)?;
|
||||
if let Some(target) = all.iter_mut().find(|x| x.obj == obj) {
|
||||
target.value = vec![v];
|
||||
@@ -187,6 +216,12 @@ pub fn recalculate_with(doc: &mut Document, scripts: &mut dyn Scripts) -> Result
|
||||
Ok(changed)
|
||||
}
|
||||
|
||||
/// The index of the choice option `v` names: the option whose export value it is, else the first
|
||||
/// whose display text it is, so a display text never shadows a later option's export value.
|
||||
fn option_named(f: &Field, v: &str) -> Option<usize> {
|
||||
f.options.iter().position(|(e, _)| e == v).or_else(|| f.options.iter().position(|(_, d)| d == v))
|
||||
}
|
||||
|
||||
/// The fields whose calculation differs between two readings of a form, matched by field object
|
||||
/// and named as in `after`: the formulas an edit rewrote, such as a rename's.
|
||||
pub fn calculations_changed(before: &[Field], after: &[Field]) -> Vec<String> {
|
||||
@@ -206,6 +241,11 @@ pub(crate) fn write_value(doc: &mut Document, f: &Field, value: &FieldValue, scr
|
||||
{
|
||||
return invalid(format!("{:?} takes at most {max} characters", f.name));
|
||||
}
|
||||
// Thai this build can't draw is refused (before any script runs), rather than stored
|
||||
// and drawn as `?`.
|
||||
if let Some(why) = appearance::thai_refusal(doc, f, t) {
|
||||
return invalid(format!("{:?} can't take this text: {why}", f.name));
|
||||
}
|
||||
// Keystroke (on commit) and Validate, as Acrobat runs them before accepting a value.
|
||||
let t = &af::keystroke(&f.actions.format, &f.name, t).map_err(FormError::Format)?;
|
||||
af::validate(&f.actions.validate, t).map_err(FormError::Invalid)?;
|
||||
@@ -275,7 +315,7 @@ pub(crate) fn write_value(doc: &mut Document, f: &Field, value: &FieldValue, scr
|
||||
// Accept export values or display texts; free text only in editable combo boxes.
|
||||
let mut exports = Vec::new();
|
||||
for v in &vals {
|
||||
match f.options.iter().find(|(e, d)| e == v || d == v) {
|
||||
match option_named(f, v).and_then(|i| f.options.get(i)) {
|
||||
Some((e, _)) => exports.push(e.clone()),
|
||||
None if f.kind == FieldKind::Combo && f.has(flags::EDIT) => exports.push(v.clone()),
|
||||
None if f.options.is_empty() => exports.push(v.clone()),
|
||||
@@ -426,12 +466,29 @@ fn redraw(doc: &mut Document, f: &Field, values: &[String], scripts: &mut dyn Sc
|
||||
};
|
||||
let in_place = f.kind == FieldKind::Text && f.has(flags::PASSWORD);
|
||||
let mut undrawn = false;
|
||||
// The Format script again with English month and weekday names, once, for widgets whose
|
||||
// fonts can't draw the date language's (#5303).
|
||||
let mut english: Option<Option<String>> = None;
|
||||
for w in &f.widgets {
|
||||
let (stream, missing) = match &shown {
|
||||
Some(s) => appearance::field_appearance_drawn(doc, f, w, std::slice::from_ref(s), false),
|
||||
let (mut stream, missing) = match &shown {
|
||||
Some(s) => {
|
||||
let drawn = appearance::field_appearance_drawn(doc, f, w, std::slice::from_ref(s), false);
|
||||
// Taken when the English run happened and draws whole, even where it is the
|
||||
// stored value ("18-Oct-26" printed again from itself).
|
||||
let again = match (&drawn.1, values) {
|
||||
(Some(_), [one]) => english.get_or_insert_with(|| scripting::formatted_in_english(doc, f, one, scripts)).as_ref(),
|
||||
_ => None,
|
||||
};
|
||||
match again.map(|e| appearance::field_appearance_drawn(doc, f, w, std::slice::from_ref(e), false)) {
|
||||
Some(english) if english.1.is_none() => english,
|
||||
_ => drawn,
|
||||
}
|
||||
}
|
||||
None => appearance::field_appearance_drawn(doc, f, w, values, true),
|
||||
};
|
||||
undrawn |= missing.is_some();
|
||||
// Thai glyphs are streams of their own.
|
||||
pdfcraft_fonts::shaped_text::store_streams(doc, &mut stream.dict);
|
||||
let existing_ap = in_place
|
||||
.then(|| {
|
||||
doc.get(w.obj)
|
||||
|
||||
Reference in new issue
Block a user