diff --git a/Cargo.lock b/Cargo.lock index 9b4662232..f829f299c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1362,6 +1362,10 @@ dependencies = [ "vorbis_rs", ] +[[package]] +name = "craft-ber" +version = "0.1.0" + [[package]] name = "craft-captions" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 576efca6f..3c701ab6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ rust-version = "1.95" repository = "https://github.com/storytold/craft-libs" [workspace.dependencies] +craft-ber = { path = "crates/ber" } acadrust = "=0.6.6" log = "0.4" # internal: add each crate here as `craft- = { path = "crates/" }` diff --git a/crates/ber/Cargo.toml b/crates/ber/Cargo.toml new file mode 100644 index 000000000..fa3902e88 --- /dev/null +++ b/crates/ber/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "craft-ber" +description = "ASN.1 BER and DER (X.690): reading TLVs, BER to DER, and what an opaque signed CMS holds" +version.workspace = true +edition.workspace = true +license.workspace = true +rust-version.workspace = true +repository.workspace = true + +[lints] +workspace = true diff --git a/crates/ber/src/lib.rs b/crates/ber/src/lib.rs new file mode 100644 index 000000000..e97c1b0f5 --- /dev/null +++ b/crates/ber/src/lib.rs @@ -0,0 +1,432 @@ +// SPDX-FileCopyrightText: MailCraft contributors +// SPDX-License-Identifier: MIT OR Apache-2.0 +//! Reading the TLVs of ASN.1 BER and DER (X.690), and turning BER into DER where they differ in +//! what CMS writers use: indefinite lengths (which streaming signers write) and OCTET STRINGs in +//! pieces (constructed). What is DER already is kept byte for byte, an element with a definite +//! length inside an indefinite one too, so that the bytes a signature covers (signed attributes, +//! a certificate's body) are those the signer wrote. For S/MIME: `craft-smime` checks +//! signatures with it, and a message's reader takes out what an opaque signed one holds +//! ([`signed_content`]). +//! +//! The input is hostile: lengths are checked against what is there, nesting is bounded +//! ([`MAX_DEPTH`]), and the work is linear in the input (times the bounded depth). +#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)] + +/// CMS's id-signedData and id-data (RFC 5652 §5.1, §4), as OBJECT IDENTIFIER values. +const SIGNED_DATA: &[u8] = &[0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02]; +const DATA: &[u8] = &[0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01]; + +/// What the CMS `cms` holds when it is SignedData with its content inside, of type id-data: the +/// MIME entity an opaque signed message (`application/pkcs7-mime`, RFC 8551 §3.5) signed, for +/// reading as the message. Nothing is checked here (`craft-smime` checks it); `None` for any +/// other CMS (an Encrypted message's enveloped data) or bytes that aren't one. +pub fn signed_content(cms: &[u8]) -> Option> { + let der = to_der(cms)?; + let (info, rest) = read(&der)?; + if info.tag != 0x30 || !rest.is_empty() { + return None; + } + let info = children(info.value)?; + let [oid, explicit] = info.as_slice() else { return None }; + if oid.tag != 0x06 || oid.value != SIGNED_DATA || explicit.tag != 0xa0 { + return None; + } + let (signed, rest) = read(explicit.value)?; + if signed.tag != 0x30 || !rest.is_empty() { + return None; + } + let signed = children(signed.value)?; + let [version, digests, encap, tail @ ..] = signed.as_slice() else { return None }; + if version.tag != 0x02 || !matches!(version.value, [1 | 3 | 4 | 5]) || digests.tag != 0x31 || encap.tag != 0x30 { + return None; + } + if children(digests.value)?.iter().any(|algorithm| algorithm.tag != 0x30) { + return None; + } + // Certificates and revocation lists are optional, but ordered and present at most once. + let tail = if tail.first().is_some_and(|field| field.tag == 0xa0) { tail.get(1..)? } else { tail }; + let tail = if tail.first().is_some_and(|field| field.tag == 0xa1) { tail.get(1..)? } else { tail }; + let [signers] = tail else { return None }; + if signers.tag != 0x31 || children(signers.value)?.iter().any(|signer| signer.tag != 0x30) { + return None; + } + let encap = children(encap.value)?; + let [kind, content] = encap.as_slice() else { return None }; + if kind.tag != 0x06 || kind.value != DATA || content.tag != 0xa0 { + return None; + } + let (octets, rest) = read(content.value)?; + (octets.tag == 0x04 && rest.is_empty()).then(|| octets.value.to_vec()) +} + +/// How deep elements may nest. +pub const MAX_DEPTH: usize = 48; + +/// Maximum elements materialized or indexed in one parsing operation. This bounds the +/// allocation amplification from hostile, shallow input containing many empty TLVs. +pub const MAX_ELEMENTS: usize = 65_536; + +/// The universal tag of an OCTET STRING, and of one in pieces. +const OCTET_STRING: u8 = 0x04; +const OCTET_STRING_PIECES: u8 = 0x24; + +/// One element: its tag bytes, and its value (the content octets). +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Tlv<'a> { + /// The tag's first byte (class, constructed bit, number below 31). + pub tag: u8, + /// The whole element, tag and length included. + pub whole: &'a [u8], + /// Its content octets. + pub value: &'a [u8], +} + +/// The tag bytes at the start of `bytes`: one, or a high tag number in up to four more. +fn tag_len(bytes: &[u8]) -> Option { + let first = *bytes.first()?; + if first & 0x1f != 0x1f { + return Some(1); + } + // High tag number: base-128 bytes, the last without its top bit. + for (i, b) in bytes.iter().enumerate().skip(1).take(4) { + if b & 0x80 == 0 { + return i.checked_add(1); + } + } + None +} + +/// The length at the start of `bytes`: `(length, bytes it took)`, `None` for an indefinite one +/// (`Some((None, 1))`) is told apart by the caller. +fn length(bytes: &[u8]) -> Option<(Option, usize)> { + let first = *bytes.first()?; + if first < 0x80 { + return Some((Some(usize::from(first)), 1)); + } + if first == 0x80 { + return Some((None, 1)); + } + let n = usize::from(first & 0x7f); + if n > 4 { + return None; + } + let digits = bytes.get(1..n.checked_add(1)?)?; + let len = digits.iter().fold(0usize, |acc, d| (acc << 8) | usize::from(*d)); + Some((Some(len), n.checked_add(1)?)) +} + +/// The element at the start of `bytes` with a definite length, and what follows it. `None` for +/// an indefinite length, or one longer than what is there. +pub fn read(bytes: &[u8]) -> Option<(Tlv<'_>, &[u8])> { + let t = tag_len(bytes)?; + let tag = *bytes.first()?; + let (len, l) = length(bytes.get(t..)?)?; + let len = len?; + let start = t.checked_add(l)?; + let end = start.checked_add(len)?; + let value = bytes.get(start..end)?; + let whole = bytes.get(..end)?; + Some((Tlv { tag, whole, value }, bytes.get(end..)?)) +} + +/// The elements of `value`, each with a definite length; `None` when one can't be read. +pub fn children(mut value: &[u8]) -> Option>> { + let mut out = Vec::new(); + while !value.is_empty() { + if out.len() >= MAX_ELEMENTS { + return None; + } + let (tlv, rest) = read(value)?; + out.push(tlv); + value = rest; + } + Some(out) +} + +/// `bytes` as DER as far as CMS needs it: indefinite lengths made definite and OCTET STRINGs in +/// pieces joined into one, everything else as it was. One element is read, and must be all of +/// `bytes` (bar trailing zero bytes, which some writers pad with). `None` when it isn't BER. +pub fn to_der(bytes: &[u8]) -> Option> { + if let Some((_, rest)) = read(bytes) + && rest.iter().all(|b| *b == 0) + && der_shaped(bytes.get(..bytes.len().saturating_sub(rest.len()))?, 0) + { + return bytes.get(..bytes.len().saturating_sub(rest.len())).map(<[u8]>::to_vec); + } + let mut out = Vec::with_capacity(bytes.len()); + let used = element(bytes, 0, &mut out)?; + let rest = bytes.get(used..)?; + rest.iter().all(|b| *b == 0).then_some(out) +} + +/// Whether the elements of `bytes` all have definite lengths and no OCTET STRING is in pieces, +/// as deep as they go: nothing to change. +fn der_shaped(mut bytes: &[u8], depth: usize) -> bool { + if depth > MAX_DEPTH { + return false; + } + while !bytes.is_empty() { + let Some((tlv, rest)) = read(bytes) else { return false }; + if tlv.tag == OCTET_STRING_PIECES || (tlv.tag & 0x20 != 0 && !der_shaped(tlv.value, depth.saturating_add(1))) { + return false; + } + bytes = rest; + } + true +} + +/// What [`measure`] found of one element: how many bytes of the input it was, and how long its +/// content is as DER. +#[derive(Clone, Copy)] +struct Size { + used: usize, + content: usize, + /// It is DER-shaped already (a definite length, and nothing in it to change): copied as it + /// is, its lengths as the writer wrote them, since a signature may cover them. + verbatim: bool, +} + +/// The sizes of the constructed elements of the input, by where each starts in it: worked out +/// once, so that writing them is one pass with no copy of a level for each level above it. +type Sizes = std::collections::HashMap; + +/// The element at `at` in `input`: how many bytes it was, and its length as DER, header and all. +fn measure(input: &[u8], at: usize, depth: usize, sizes: &mut Sizes) -> Option<(usize, usize)> { + if depth > MAX_DEPTH || sizes.len() >= MAX_ELEMENTS { + return None; + } + let bytes = input.get(at..)?; + if let Some((tlv, _)) = read(bytes) + && der_shaped(tlv.whole, depth) + { + sizes.insert(at, Size { used: tlv.whole.len(), content: tlv.value.len(), verbatim: true }); + return Some((tlv.whole.len(), tlv.whole.len())); + } + let t = tag_len(bytes)?; + let tag = *bytes.first()?; + let (len, l) = length(bytes.get(t..)?)?; + let start = t.checked_add(l)?; + if tag & 0x20 == 0 { + let used = start.checked_add(len?)?; + bytes.get(..used)?; + return Some((used, used_der(t, used.checked_sub(start)?)?)); + } + // The children, one after another, to the end of a definite length or to the end mark. + let mut content = 0usize; + let mut pos = start; + let end = match len { + Some(len) => Some(start.checked_add(len)?), + None => None, + }; + loop { + match end { + Some(end) if pos >= end => break, + None if bytes.get(pos..)?.starts_with(&[0, 0]) => { + pos = pos.checked_add(2)?; + break; + } + _ => {} + } + // (The pieces of an OCTET STRING are OCTET STRINGs, in pieces themselves or not.) + if tag == OCTET_STRING_PIECES && !matches!(bytes.get(pos), Some(&OCTET_STRING | &OCTET_STRING_PIECES)) { + return None; + } + let (n, der) = measure(input, at.checked_add(pos)?, depth.checked_add(1)?, sizes)?; + if n == 0 { + return None; + } + // An OCTET STRING in pieces is one of their contents together. + content = content.checked_add(if tag == OCTET_STRING_PIECES { der_content(input, at.checked_add(pos)?, sizes)? } else { der })?; + pos = pos.checked_add(n)?; + } + if let Some(end) = end + && pos != end + { + return None; + } + if sizes.len() >= MAX_ELEMENTS { + return None; + } + sizes.insert(at, Size { used: pos, content, verbatim: false }); + let header_tag = if tag == OCTET_STRING_PIECES { 1 } else { t }; + Some((pos, used_der(header_tag, content)?)) +} + +/// The length of a DER element with a tag of `tag` bytes and `content` bytes of content. +fn used_der(tag: usize, content: usize) -> Option { + let mut l = Vec::new(); + put_length(&mut l, content); + tag.checked_add(l.len())?.checked_add(content) +} + +/// The DER content length of the (measured) element at `at`: a primitive's own. +fn der_content(input: &[u8], at: usize, sizes: &Sizes) -> Option { + if let Some(s) = sizes.get(&at) { + return Some(s.content); + } + let bytes = input.get(at..)?; + let t = tag_len(bytes)?; + let (len, _) = length(bytes.get(t..)?)?; + len +} + +/// Write the (measured) element at `at` in `input` to `out` as DER; how many bytes it was. +fn write(input: &[u8], at: usize, sizes: &Sizes, pieces: bool, out: &mut Vec) -> Option { + if let Some(s) = sizes.get(&at).filter(|s| s.verbatim && !pieces) { + out.extend_from_slice(input.get(at..at.checked_add(s.used)?)?); + return Some(s.used); + } + let bytes = input.get(at..)?; + let t = tag_len(bytes)?; + let tag_bytes = bytes.get(..t)?; + let tag = *tag_bytes.first()?; + let (len, l) = length(bytes.get(t..)?)?; + let start = t.checked_add(l)?; + if tag & 0x20 == 0 { + let used = start.checked_add(len?)?; + let value = bytes.get(start..used)?; + // (A piece of an OCTET STRING in pieces gives its bytes alone.) + if !pieces { + out.extend_from_slice(tag_bytes); + put_length(out, value.len()); + } + out.extend_from_slice(value); + return Some(used); + } + let size = *sizes.get(&at)?; + if tag == OCTET_STRING_PIECES { + if !pieces { + out.push(OCTET_STRING); + put_length(out, size.content); + } + } else { + out.extend_from_slice(tag_bytes); + put_length(out, size.content); + } + let inner_pieces = tag == OCTET_STRING_PIECES; + let mut pos = start; + while pos < size.used { + if bytes.get(pos..)?.starts_with(&[0, 0]) && len.is_none() { + break; + } + let n = write(input, at.checked_add(pos)?, sizes, inner_pieces, out)?; + pos = pos.checked_add(n)?; + } + Some(size.used) +} + +/// Write the element at the start of `bytes` to `out` as DER; how many bytes of `bytes` it was. +fn element(bytes: &[u8], depth: usize, out: &mut Vec) -> Option { + let mut sizes = Sizes::new(); + let (used, der) = measure(bytes, 0, depth, &mut sizes)?; + out.reserve(der); + write(bytes, 0, &sizes, false, out)?; + Some(used) +} + +/// A definite length in DER's shortest form. +fn put_length(out: &mut Vec, len: usize) { + if len < 0x80 { + out.push(len as u8); + return; + } + let bytes = len.to_be_bytes(); + let skip = bytes.iter().take_while(|b| **b == 0).count(); + let digits = bytes.get(skip..).unwrap_or_default(); + out.push(0x80 | digits.len() as u8); + out.extend_from_slice(digits); +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tlv(tag: u8, value: &[u8]) -> Vec { + let mut out = vec![tag]; + put_length(&mut out, value.len()); + out.extend_from_slice(value); + out + } + + fn cms(outer_tag: u8, signed_tag: u8, oid_tag: u8, long_oid: bool, trailing: bool) -> Vec { + let oid = |value: &[u8]| { + if long_oid { + let mut out = vec![oid_tag, 0x81, 9]; + out.extend_from_slice(value); + out + } else { + tlv(oid_tag, value) + } + }; + let mut encap = oid(DATA); + encap.extend(tlv(0xa0, &tlv(0x04, b"hello"))); + let mut signed = tlv(0x02, &[1]); + signed.extend(tlv(0x31, &[])); + signed.extend(tlv(0x30, &encap)); + signed.extend(tlv(0x31, &[])); + let mut signed = tlv(signed_tag, &signed); + if trailing { + signed.extend(tlv(0x05, &[])); + } + let mut info = oid(SIGNED_DATA); + info.extend(tlv(0xa0, &signed)); + tlv(outer_tag, &info) + } + + #[test] + fn extracts_content_with_der_and_ber_oid_lengths() { + for long_oid in [false, true] { + assert_eq!(signed_content(&cms(0x30, 0x30, 0x06, long_oid, false)), Some(b"hello".to_vec())); + } + } + + #[test] + fn rejects_wrong_cms_container_and_oid_tags() { + for (outer, signed, oid) in [(0x31, 0x30, 0x06), (0x30, 0x31, 0x06), (0x30, 0x30, 0x04)] { + assert_eq!(signed_content(&cms(outer, signed, oid, false, false)), None); + } + } + + #[test] + fn rejects_missing_mandatory_signed_data_fields() { + let mut encap = tlv(0x06, DATA); + encap.extend(tlv(0xa0, &tlv(0x04, b"hello"))); + for prefix in [vec![0x05, 0, 0x05, 0], vec![0x02, 1, 1, 0x31, 0]] { + let mut malformed = prefix; + malformed.extend(tlv(0x30, &encap)); + let mut info = tlv(0x06, SIGNED_DATA); + info.extend(tlv(0xa0, &tlv(0x30, &malformed))); + assert_eq!(signed_content(&tlv(0x30, &info)), None); + } + } + + #[test] + fn rejects_extra_signed_data_inside_explicit_container() { + assert_eq!(signed_content(&cms(0x30, 0x30, 0x06, false, true)), None); + } + + #[test] + fn rejects_wide_inputs_before_unbounded_metadata_allocation() { + let children_bytes = [0x05, 0].repeat(MAX_ELEMENTS + 1); + assert!(children(&children_bytes).is_none()); + let mut indefinite = vec![0x30, 0x80]; + indefinite.extend_from_slice(&children_bytes); + indefinite.extend_from_slice(&[0, 0]); + assert!(to_der(&indefinite).is_none()); + } + + #[test] + fn joins_indefinite_octet_string_pieces() { + assert_eq!(to_der(&[0x24, 0x80, 0x04, 1, b'a', 0x04, 1, b'b', 0, 0]), Some(vec![0x04, 2, b'a', b'b'])); + } + + #[test] + fn rejects_truncated_and_over_nested_input() { + assert_eq!(to_der(&[0x04, 2, b'a']), None); + let mut nested = tlv(0x04, &[]); + for _ in 0..=MAX_DEPTH { + nested = tlv(0x30, &nested); + } + assert_eq!(to_der(&nested), None); + } +} diff --git a/crates/xfl/src/document.rs b/crates/xfl/src/document.rs index d82d52f2d..1b1fc8dc8 100644 --- a/crates/xfl/src/document.rs +++ b/crates/xfl/src/document.rs @@ -475,6 +475,10 @@ mod tests { fn elements(&self) -> Self::Elements<'_> { self.children.iter() } + + fn text_content(&self) -> Cow<'_, str> { + Cow::Borrowed("") + } } fn element(name: &str, attributes: &[(&str, &str)], children: Vec) -> AppElement { diff --git a/crates/xfl/src/lib.rs b/crates/xfl/src/lib.rs index 9dd43fd5d..5c1bf12df 100644 --- a/crates/xfl/src/lib.rs +++ b/crates/xfl/src/lib.rs @@ -31,6 +31,8 @@ pub mod edges; pub mod media; /// Borrowed views of native XFL shape and style elements. pub mod shape; +/// Borrowed views of native XFL text-field and text-run schema. +pub mod text; /// Largest single entry inflated (bytes). pub const MAX_ENTRY: usize = 256 * 1024 * 1024; diff --git a/crates/xfl/src/shape.rs b/crates/xfl/src/shape.rs index ddd33fb8f..176c800d8 100644 --- a/crates/xfl/src/shape.rs +++ b/crates/xfl/src/shape.rs @@ -5,6 +5,7 @@ //! compatible borrowed element adapters implemented by consumers. use crate::edges::{self, Segment}; +use std::borrow::Cow; /// An element source used by the borrowed XFL schema views. /// @@ -27,6 +28,9 @@ pub trait XflElement { /// Direct child elements in document order. fn elements(&self) -> Self::Elements<'_>; + /// This element's direct character data, with XML references resolved. + fn text_content(&self) -> Cow<'_, str>; + /// The first direct child whose local name matches. fn child(&self, name: &str) -> Option<&Self> { self.elements().find(|element| element.local_name() == name) @@ -68,6 +72,10 @@ impl XflElement for craft_xml::El { fn elements(&self) -> Self::Elements<'_> { XmlElements { nodes: self.children.iter() } } + + fn text_content(&self) -> Cow<'_, str> { + self.text() + } } /// XFL matrix coefficients, in the native units stored in XML. @@ -580,6 +588,9 @@ mod tests { fn elements(&self) -> Self::Elements<'_> { self.children.iter() } + fn text_content(&self) -> Cow<'_, str> { + Cow::Borrowed("") + } } let app = AppElement { name: "x:DOMShape".into(), attrs: vec![("isDrawingObject".into(), "true".into())], children: Vec::new() }; assert_eq!(Shape::new(&app).unwrap().is_drawing_object(), Some(true)); diff --git a/crates/xfl/src/text.rs b/crates/xfl/src/text.rs new file mode 100644 index 000000000..8d883fc0c --- /dev/null +++ b/crates/xfl/src/text.rs @@ -0,0 +1,273 @@ +//! Borrowed views of native XFL text-field, run and style records. +//! +//! The views expose format attributes and direct text only. Text layout, font selection, +//! unsupported-style reporting and conversion into an application's text model stay with the +//! consumer. + +use crate::shape::{self, Color, XflElement}; +use std::borrow::Cow; + +/// A native static, dynamic or input text field. +#[derive(Clone, Copy, Debug)] +pub struct TextField<'a, E: XflElement> { + element: &'a E, + kind: TextFieldKind, +} + +/// Kind of native XFL text field. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TextFieldKind { + Static, + Dynamic, + Input, +} + +impl<'a, E: XflElement> TextField<'a, E> { + /// Views a recognized XFL text field. + pub fn new(element: &'a E) -> Option { + let kind = match element.local_name() { + "DOMStaticText" => TextFieldKind::Static, + "DOMDynamicText" => TextFieldKind::Dynamic, + "DOMInputText" => TextFieldKind::Input, + _ => return None, + }; + Some(Self { element, kind }) + } + + pub fn kind(&self) -> TextFieldKind { + self.kind + } + + /// Native field orientation (`horizontal`, `vertical`, etc.). + pub fn orientation(&self) -> Option<&'a str> { + self.element.attr("orientation") + } + + /// Native `autoExpand` flag, if present and parseable. + pub fn auto_expand(&self) -> Option { + boolean(self.element, "autoExpand") + } + + pub fn width(&self) -> Option { + number(self.element, "width") + } + + pub fn height(&self) -> Option { + number(self.element, "height") + } + + pub fn left(&self) -> Option { + number(self.element, "left") + } + + pub fn top(&self) -> Option { + number(self.element, "top") + } + + pub fn line_type(&self) -> Option<&'a str> { + self.element.attr("lineType") + } + + pub fn border(&self) -> Option { + boolean(self.element, "border") + } + + pub fn is_selectable(&self) -> Option { + boolean(self.element, "isSelectable") + } + + pub fn render_as_html(&self) -> Option { + boolean(self.element, "renderAsHTML") + } + + pub fn max_characters(&self) -> Option { + number(self.element, "maxCharacters") + } + + pub fn variable_name(&self) -> Option<&'a str> { + self.element.attr("variableName") + } + + /// Whether the native field has any filter records, which consumers may need to report as + /// unsupported while keeping the field. + pub fn has_filters(&self) -> bool { + self.element.child("filters").is_some_and(|wrapper| wrapper.elements().next().is_some()) + } + + /// Text runs in document order. Non-`DOMTextRun` children are ignored. + pub fn runs(&self) -> impl Iterator> + 'a { + self.element.child("textRuns").into_iter().flat_map(|wrapper| wrapper.elements_named("DOMTextRun")).map(|element| TextRun { element }) + } + + /// Number of element records inside the native `` wrapper, including unknown + /// records. This lets consumers retain their prior malformed-record and resource-limit + /// policy while parsing recognized runs through [`Self::runs`]. + pub fn run_entries_count(&self) -> usize { + self.element.child("textRuns").map_or(0, |wrapper| wrapper.elements().count()) + } +} + +/// One native text run and its optional style record. +#[derive(Clone, Copy, Debug)] +pub struct TextRun<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> TextRun<'a, E> { + /// Direct character data of this run's `` element. + pub fn characters(&self) -> Cow<'a, str> { + self.element.child("characters").map_or(Cow::Borrowed(""), XflElement::text_content) + } + + /// The native text attributes nested under ``. + pub fn style(&self) -> Option> { + self.element.child("textAttrs").and_then(|wrapper| wrapper.child("DOMTextAttrs")).map(|element| TextStyle { element }) + } +} + +/// Native `DOMTextAttrs` fields. Values are parsed from the wire schema but not laid out or +/// normalized for any application. +#[derive(Clone, Copy, Debug)] +pub struct TextStyle<'a, E: XflElement> { + element: &'a E, +} + +impl<'a, E: XflElement> TextStyle<'a, E> { + pub fn face(&self) -> Option<&'a str> { + self.element.attr("face") + } + + pub fn size(&self) -> Option { + number(self.element, "size") + } + + pub fn bold(&self) -> Option { + boolean(self.element, "bold") + } + + pub fn italic(&self) -> Option { + boolean(self.element, "italic") + } + + pub fn fill_color(&self) -> Option<&'a str> { + self.element.attr("fillColor") + } + + /// Parsed native fill color and alpha, with XFL's black/full-opacity defaults. + pub fn color(&self) -> Color { + shape::color(self.element, "fillColor", "alpha") + } + + pub fn alpha(&self) -> Option { + number(self.element, "alpha") + } + + pub fn alignment(&self) -> Option<&'a str> { + self.element.attr("alignment") + } + + pub fn indent(&self) -> Option { + number(self.element, "indent") + } + + pub fn character_position(&self) -> Option<&'a str> { + self.element.attr("characterPosition") + } + + pub fn letter_spacing(&self) -> Option { + number(self.element, "letterSpacing") + } + + pub fn line_spacing(&self) -> Option { + number(self.element, "lineSpacing") + } + + pub fn left_margin(&self) -> Option { + number(self.element, "leftMargin") + } + + pub fn right_margin(&self) -> Option { + number(self.element, "rightMargin") + } +} + +fn number(element: &impl XflElement, name: &str) -> Option { + element.attr(name)?.trim().parse::().ok().filter(|value| value.is_finite()) +} + +fn boolean(element: &impl XflElement, name: &str) -> Option { + element.attr(name).map(|value| value.trim() == "true") +} + +#[cfg(test)] +mod tests { + use super::*; + + fn parse(source: &[u8]) -> craft_xml::El { + craft_xml::parse(source).expect("well-formed test XML") + } + + #[test] + fn reads_prefixed_text_fields_runs_and_native_style_values() { + let root = parse( + br##"one & two"##, + ); + let field = TextField::new(&root).expect("dynamic text field"); + assert_eq!(field.kind(), TextFieldKind::Dynamic); + assert_eq!(field.orientation(), Some("horizontal")); + assert_eq!(field.auto_expand(), Some(true)); + assert_eq!((field.width(), field.height(), field.left(), field.top()), (Some(320.0), Some(40.0), Some(12.0), Some(3.0))); + assert_eq!(field.line_type(), Some("multiline")); + assert_eq!(field.border(), Some(true)); + assert_eq!(field.is_selectable(), Some(false)); + assert_eq!(field.render_as_html(), Some(false)); + assert_eq!(field.max_characters(), Some(45.0)); + assert_eq!(field.variable_name(), Some("message")); + assert!(field.has_filters()); + + let mut runs = field.runs(); + let first = runs.next().expect("first run"); + assert_eq!(first.characters(), "one & two"); + let style = first.style().expect("text style"); + assert_eq!(style.face(), Some("Arial")); + assert_eq!(style.size(), Some(24.0)); + assert_eq!(style.bold(), Some(true)); + assert_eq!(style.italic(), Some(false)); + assert_eq!(style.fill_color(), Some("#123456")); + assert_eq!(style.alpha(), Some(0.5)); + assert_eq!(style.alignment(), Some("center")); + assert_eq!(style.indent(), Some(3.0)); + assert_eq!(style.character_position(), Some("superscript")); + assert_eq!(style.letter_spacing(), Some(1.25)); + assert_eq!(style.line_spacing(), Some(2.0)); + assert_eq!(style.left_margin(), Some(4.0)); + assert_eq!(style.right_margin(), Some(5.0)); + + let second = runs.next().expect("second run"); + assert_eq!(second.characters(), "third"); + assert!(second.style().is_none()); + assert!(runs.next().is_none()); + } + + #[test] + fn recognizes_all_native_field_kinds_and_ignores_non_text_elements() { + for (name, expected) in + [("DOMStaticText", TextFieldKind::Static), ("DOMDynamicText", TextFieldKind::Dynamic), ("DOMInputText", TextFieldKind::Input)] + { + let xml = format!("<{name}/>"); + let root = craft_xml::parse(xml.as_bytes()).expect("test XML"); + assert_eq!(TextField::new(&root).map(|field| field.kind()), Some(expected)); + } + let root = parse(b""); + assert!(TextField::new(&root).is_none()); + } + + #[test] + fn malformed_and_non_finite_numbers_remain_absent() { + let root = parse(b""); + let field = TextField::new(&root).expect("text field"); + let style = field.runs().next().and_then(|run| run.style()).expect("style"); + assert_eq!(style.size(), None); + assert_eq!(style.letter_spacing(), None); + } +}