Skip to main content

typf_core/
context.rs

1//! The traveling container that carries data through pipeline stages
2
3use crate::{
4    traits::{Exporter, FontRef, Renderer, Shaper},
5    types::{RenderOutput, ShapingResult},
6    RenderParams, ShapingParams,
7};
8use std::sync::Arc;
9
10/// Everything a stage needs, nothing it doesn't
11///
12/// The context flows from stage to stage, accumulating the results
13/// of each transformation. Text becomes glyphs, glyphs become pixels,
14/// and pixels become files - all tracked here.
15pub struct PipelineContext {
16    // What we start with
17    text: String,
18    font_spec: String,
19
20    // Who does the work
21    shaper: Option<Arc<dyn Shaper>>,
22    renderer: Option<Arc<dyn Renderer>>,
23    exporter: Option<Arc<dyn Exporter>>,
24
25    // What emerges along the way
26    font: Option<Arc<dyn FontRef>>,
27    shaped: Option<ShapingResult>,
28    output: Option<RenderOutput>,
29    exported: Option<Vec<u8>>,
30
31    // How we want it done
32    shaping_params: ShapingParams,
33    render_params: RenderParams,
34}
35
36impl PipelineContext {
37    /// Start fresh with text and a font specification
38    pub fn new(text: String, font_spec: String) -> Self {
39        Self {
40            text,
41            font_spec,
42            shaper: None,
43            renderer: None,
44            exporter: None,
45            font: None,
46            shaped: None,
47            output: None,
48            exported: None,
49            shaping_params: ShapingParams::default(),
50            render_params: RenderParams::default(),
51        }
52    }
53
54    // Read what's inside
55
56    pub fn text(&self) -> &str {
57        &self.text
58    }
59
60    pub fn font_spec(&self) -> &str {
61        &self.font_spec
62    }
63
64    pub fn shaper(&self) -> Option<Arc<dyn Shaper>> {
65        self.shaper.clone()
66    }
67
68    pub fn renderer(&self) -> Option<Arc<dyn Renderer>> {
69        self.renderer.clone()
70    }
71
72    pub fn exporter(&self) -> Option<Arc<dyn Exporter>> {
73        self.exporter.clone()
74    }
75
76    pub fn font(&self) -> Option<Arc<dyn FontRef>> {
77        self.font.clone()
78    }
79
80    pub fn shaped(&self) -> Option<&ShapingResult> {
81        self.shaped.as_ref()
82    }
83
84    pub fn output(&self) -> Option<&RenderOutput> {
85        self.output.as_ref()
86    }
87
88    pub fn exported(&self) -> Option<&Vec<u8>> {
89        self.exported.as_ref()
90    }
91
92    pub fn shaping_params(&self) -> &ShapingParams {
93        &self.shaping_params
94    }
95
96    pub fn render_params(&self) -> &RenderParams {
97        &self.render_params
98    }
99
100    // Change what's inside
101
102    pub fn set_shaper(&mut self, shaper: Arc<dyn Shaper>) {
103        self.shaper = Some(shaper);
104    }
105
106    pub fn set_renderer(&mut self, renderer: Arc<dyn Renderer>) {
107        self.renderer = Some(renderer);
108    }
109
110    pub fn set_exporter(&mut self, exporter: Arc<dyn Exporter>) {
111        self.exporter = Some(exporter);
112    }
113
114    pub fn set_font(&mut self, font: Arc<dyn FontRef>) {
115        self.font = Some(font);
116    }
117
118    pub fn set_shaped(&mut self, shaped: ShapingResult) {
119        self.shaped = Some(shaped);
120    }
121
122    pub fn set_output(&mut self, output: RenderOutput) {
123        self.output = Some(output);
124    }
125
126    pub fn set_exported(&mut self, exported: Vec<u8>) {
127        self.exported = Some(exported);
128    }
129
130    pub fn set_shaping_params(&mut self, params: ShapingParams) {
131        self.shaping_params = params;
132    }
133
134    pub fn set_render_params(&mut self, params: RenderParams) {
135        self.render_params = params;
136    }
137}