Skip to main content

latexml_post/
picture_images.rs

1//! Picture image generation processor.
2//!
3//! Port of `LaTeXML::Post::PictureImages`.
4//! Extends LaTeXImages to generate images for ltx:picture elements.
5
6use libxml::tree::Node;
7
8use crate::{
9  document::PostDocument,
10  processor::{ProcessResult, Processor},
11};
12
13/// PictureImages post-processor.
14///
15/// Port of `LaTeXML::Post::PictureImages`.
16pub struct PictureImages {
17  name:               String,
18  resource_directory: String,
19  resource_prefix:    String,
20  use_dvipng:         bool,
21  empty_only:         bool,
22}
23
24impl PictureImages {
25  pub fn new(empty_only: bool) -> Self {
26    PictureImages {
27      name: "PictureImages".to_string(),
28      resource_directory: "pic".to_string(),
29      resource_prefix: "pic".to_string(),
30      use_dvipng: false,
31      empty_only,
32    }
33  }
34
35  /// Extract the TeX string for a picture node.
36  ///
37  /// Port of `PictureImages::extractTeX`.
38  fn extract_tex(&self, node: &Node) -> Option<String> {
39    let mut tex = node.get_attribute("tex").unwrap_or_default();
40    tex = tex.replace('\n', "");
41
42    if let Some(u) = node.get_attribute("unitlength") {
43      tex = format!("\\setlength{{\\unitlength}}{{{}}}{}", u, tex);
44    }
45    if let Some(s) = node.get_attribute("scale") {
46      tex = format!("\\scalebox{{{}}}{{{}}}", s, tex);
47    }
48
49    Some(format!("\\beginPICTURE {}\\endPICTURE", tex))
50  }
51}
52
53impl Processor for PictureImages {
54  fn get_name(&self) -> &str { &self.name }
55
56  fn to_process(&self, doc: &PostDocument) -> Vec<Node> {
57    let nodes = doc.findnodes("//ltx:picture");
58    if self.empty_only {
59      nodes
60        .into_iter()
61        .filter(|n| n.get_first_child().is_none())
62        .collect()
63    } else {
64      nodes
65    }
66  }
67
68  fn resource_directory(&self) -> Option<&str> { Some(&self.resource_directory) }
69
70  fn resource_prefix(&self) -> Option<&str> { Some(&self.resource_prefix) }
71
72  fn process(&mut self, doc: PostDocument, nodes: Vec<Node>) -> ProcessResult {
73    Info!(
74      "picture_images",
75      "generate",
76      "PictureImages: would generate {} picture images",
77      nodes.len()
78    );
79    // NOTE: delegates to LaTeXImages::generateImages (requires latex + dvipng)
80    Ok(vec![doc])
81  }
82}