mod behaviour;
mod read;
mod tabs;
use crate::annot::AnnotFlags;
use crate::geometry::UserSpaceRect;
use crate::id::{AnnotId, FieldId, PageId};
pub use behaviour::{
Behaviour, CalculateFunction, DatePattern, FieldBehaviour, FieldTrigger, FormScript, FormValues, InvalidValue,
NegativeStyle, NumberFormat, PercentFormat, RangeValidate, ScriptLocation, ScriptState, SeparatorStyle, ShownValue,
SimpleCalculate, SpecialFormat,
};
pub use tabs::{TabOrder, Tabs};
#[derive(Debug, Clone, PartialEq, Default)]
#[non_exhaustive]
pub struct Form {
pub fields: Vec<TerminalField>,
pub orphan_widgets: Vec<OrphanWidget>,
pub widgetless: Vec<FieldId>,
pub parent_mismatches: Vec<FieldId>,
pub untyped: Vec<FieldId>,
pub need_appearances: bool,
pub has_xfa: bool,
}
impl Form {
#[must_use]
pub fn field(&self, id: &FieldId) -> Option<&TerminalField> {
self.fields.iter().find(|f| &f.id == id || f.shared.contains(id))
}
#[must_use]
pub fn field_named(&self, name: &str) -> Option<&TerminalField> {
self.fields.iter().find(|f| f.name == name)
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct TerminalField {
pub id: FieldId,
pub shared: Vec<FieldId>,
pub name: String,
pub tooltip: Option<String>,
pub kind: FieldKind,
pub flags: FieldFlags,
pub value: Option<FieldValue>,
pub default_value: Option<FieldValue>,
pub default_appearance: Option<String>,
pub quadding: u8,
pub max_len: Option<u32>,
pub options: Vec<ChoiceOption>,
pub signed: bool,
pub widgets: Vec<Widget>,
}
impl TerminalField {
#[must_use]
pub const fn read_only(&self) -> bool {
self.flags.has(FieldFlags::READ_ONLY)
}
#[must_use]
pub const fn required(&self) -> bool {
self.flags.has(FieldFlags::REQUIRED)
}
#[must_use]
pub const fn takes_value(&self) -> bool {
!matches!(self.kind, FieldKind::PushButton | FieldKind::Signature)
}
#[must_use]
pub fn state_exporting(&self, export: &str) -> Option<&str> {
let index = self.options.iter().position(|o| o.export == export)?;
self.widgets.get(index)?.on_state.as_deref()
}
#[must_use]
pub fn export_of_state(&self, state: &str) -> Option<&str> {
let index = self.widgets.iter().position(|w| w.on_state.as_deref() == Some(state))?;
Some(self.options.get(index)?.export.as_str())
}
#[must_use]
pub fn on_states(&self) -> Vec<&str> {
let mut out: Vec<&str> = Vec::new();
for state in self.widgets.iter().filter_map(|w| w.on_state.as_deref()) {
if !out.contains(&state) {
out.push(state);
}
}
out
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FieldKind {
Text,
CheckBox,
RadioGroup,
PushButton,
ComboBox,
ListBox,
Signature,
}
impl FieldKind {
#[must_use]
pub(crate) fn of(ft: &[u8], flags: FieldFlags) -> Option<Self> {
Some(match ft {
b"Tx" => Self::Text,
b"Btn" if flags.has(FieldFlags::PUSHBUTTON) => Self::PushButton,
b"Btn" if flags.has(FieldFlags::RADIO) => Self::RadioGroup,
b"Btn" => Self::CheckBox,
b"Ch" if flags.has(FieldFlags::COMBO) => Self::ComboBox,
b"Ch" => Self::ListBox,
b"Sig" => Self::Signature,
_ => return None,
})
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Text => "text",
Self::CheckBox => "checkBox",
Self::RadioGroup => "radioGroup",
Self::PushButton => "pushButton",
Self::ComboBox => "comboBox",
Self::ListBox => "listBox",
Self::Signature => "signature",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct FieldFlags(pub u32);
impl FieldFlags {
pub const READ_ONLY: u32 = 1;
pub const REQUIRED: u32 = 1 << 1;
pub const NO_EXPORT: u32 = 1 << 2;
pub const MULTILINE: u32 = 1 << 12;
pub const PASSWORD: u32 = 1 << 13;
pub const NO_TOGGLE_TO_OFF: u32 = 1 << 14;
pub const RADIO: u32 = 1 << 15;
pub const PUSHBUTTON: u32 = 1 << 16;
pub const COMBO: u32 = 1 << 17;
pub const EDIT: u32 = 1 << 18;
pub const SORT: u32 = 1 << 19;
pub const FILE_SELECT: u32 = 1 << 20;
pub const MULTI_SELECT: u32 = 1 << 21;
pub const DO_NOT_SPELL_CHECK: u32 = 1 << 22;
pub const DO_NOT_SCROLL: u32 = 1 << 23;
pub const COMB: u32 = 1 << 24;
pub const RADIOS_IN_UNISON: u32 = 1 << 25;
pub const COMMIT_ON_SEL_CHANGE: u32 = 1 << 26;
#[must_use]
pub const fn has(self, flag: u32) -> bool {
self.0 & flag != 0
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum FieldValue {
Text(String),
Button(Option<String>),
Choice(Vec<String>),
Clear,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct ChoiceOption {
pub export: String,
pub display: String,
}
impl ChoiceOption {
#[must_use]
pub const fn new(export: String, display: String) -> Self {
Self { export, display }
}
}
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Widget {
pub id: AnnotId,
pub page: Option<PageId>,
pub rect: Option<UserSpaceRect>,
pub flags: AnnotFlags,
pub on_state: Option<String>,
pub state: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct OrphanWidget {
pub widget: AnnotId,
pub page: PageId,
pub field: Option<FieldId>,
}
#[cfg(test)]
mod tests {
use laminapdf_cos::ObjRef;
use super::*;
use crate::AnnotId;
fn sizes() -> TerminalField {
let widget = |num, state: &str| Widget {
id: AnnotId::object(ObjRef::new(num, 0)),
page: None,
rect: None,
flags: AnnotFlags::default(),
on_state: Some(state.to_owned()),
state: None,
};
TerminalField {
id: FieldId::object(ObjRef::new(10, 0)),
shared: Vec::new(),
name: "size".to_owned(),
tooltip: None,
kind: FieldKind::RadioGroup,
flags: FieldFlags(FieldFlags::RADIO),
value: None,
default_value: None,
default_appearance: None,
quadding: 0,
max_len: None,
options: ["Small", "Large"]
.map(|e| ChoiceOption::new(e.to_owned(), e.to_owned()))
.to_vec(),
signed: false,
widgets: vec![widget(11, "1"), widget(12, "0")],
}
}
#[test]
fn export_of_state_should_take_the_export_at_the_widgets_index_not_the_states_name() {
let field = sizes();
assert_eq!(
[field.export_of_state("1"), field.export_of_state("0")],
[Some("Small"), Some("Large")]
);
}
#[test]
fn state_exporting_should_be_the_inverse_of_export_of_state() {
let field = sizes();
assert_eq!(
["Small", "Large"].map(|e| field.state_exporting(e)),
[Some("1"), Some("0")]
);
}
}