Skip to main content

folio_annot/
border.rs

1//! Annotation border styles.
2
3use folio_cos::PdfObject;
4use indexmap::IndexMap;
5
6/// Border style types.
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum BorderStyleType {
9    Solid,
10    Dashed,
11    Beveled,
12    Inset,
13    Underline,
14}
15
16/// Annotation border style.
17#[derive(Debug, Clone)]
18pub struct BorderStyle {
19    pub style: BorderStyleType,
20    pub width: f64,
21    pub dash_pattern: Vec<f64>,
22}
23
24impl BorderStyle {
25    /// Parse from a /BS dictionary.
26    pub fn from_dict(dict: &IndexMap<Vec<u8>, PdfObject>) -> Self {
27        let style = dict
28            .get(b"S".as_slice())
29            .and_then(|o| o.as_name())
30            .map(|n| match n {
31                b"S" => BorderStyleType::Solid,
32                b"D" => BorderStyleType::Dashed,
33                b"B" => BorderStyleType::Beveled,
34                b"I" => BorderStyleType::Inset,
35                b"U" => BorderStyleType::Underline,
36                _ => BorderStyleType::Solid,
37            })
38            .unwrap_or(BorderStyleType::Solid);
39
40        let width = dict
41            .get(b"W".as_slice())
42            .and_then(|o| o.as_f64())
43            .unwrap_or(1.0);
44
45        let dash_pattern = dict
46            .get(b"D".as_slice())
47            .and_then(|o| o.as_array())
48            .map(|a| a.iter().filter_map(|v| v.as_f64()).collect())
49            .unwrap_or_else(|| vec![3.0]);
50
51        Self {
52            style,
53            width,
54            dash_pattern,
55        }
56    }
57
58    /// Get the border style from an annotation dictionary.
59    pub fn from_annot_dict(dict: &IndexMap<Vec<u8>, PdfObject>) -> Option<Self> {
60        let bs = dict.get(b"BS".as_slice())?.as_dict()?;
61        Some(Self::from_dict(bs))
62    }
63}