ironpress 1.4.4

Pure Rust HTML/CSS/Markdown to PDF converter with layout engine, LaTeX math, tables, images, custom fonts, and streaming output. No browser, no system dependencies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use super::*;
use crate::render::pdf_syntax::format_pdf_number_fixed;

fn format_function_scalar(value: f32) -> String {
    format_pdf_number_fixed(f64::from(value), 8)
}

#[derive(Debug, Clone, Copy)]
struct PdfFunctionColor {
    exact: [f32; 3],
    serialized: PdfRgb,
}

impl From<(f32, f32, f32)> for PdfFunctionColor {
    fn from(color: (f32, f32, f32)) -> Self {
        Self {
            exact: [color.0, color.1, color.2],
            serialized: color.into(),
        }
    }
}

impl PdfFunctionColor {
    fn exact_channel(self, channel: usize) -> Option<f32> {
        self.exact.get(channel).copied()
    }

    fn serialized_channel(self, channel: usize) -> Option<f32> {
        self.serialized.components().get(channel).copied()
    }
}

#[derive(Debug, Clone, Copy)]
struct PdfFunctionStopGroup {
    position: f32,
    incoming: PdfFunctionColor,
    outgoing: PdfFunctionColor,
}

impl PdfFunctionStopGroup {
    fn segment(self, end: Self, channel: usize) -> Option<String> {
        let from = self.outgoing.exact_channel(channel)?;
        let to = end.incoming.exact_channel(channel)?;
        let base = self.outgoing.serialized_channel(channel)?;
        if from == to {
            return Some(format!("pop {}", format_pdf_number(base)));
        }
        let length = end.position - self.position;
        (length.is_finite() && length > 0.0).then(|| {
            let slope = (to - from) / length;
            format!(
                "{} sub {} mul {} add",
                format_function_scalar(self.position),
                format_function_scalar(slope),
                format_pdf_number(base),
            )
        })
    }
}

#[derive(Debug)]
pub(super) struct PdfFunctionStopSequence {
    groups: Vec<PdfFunctionStopGroup>,
    opacity: f32,
}

impl PdfFunctionStopSequence {
    pub(super) fn from_resolved(resolved: &ResolvedGradientRamp) -> Option<Self> {
        let opacity = resolved.uniform_opacity()?;
        let stops = pdf_backend_gradient_stops(resolved)?;
        let mut groups: Vec<PdfFunctionStopGroup> = Vec::with_capacity(stops.len());
        for stop in stops {
            let color = PdfFunctionColor::from(stop.color);
            if let Some(group) = groups.last_mut()
                && group.position == stop.position
            {
                group.outgoing = color;
                continue;
            }
            groups.push(PdfFunctionStopGroup {
                position: stop.position,
                incoming: color,
                outgoing: color,
            });
        }
        (!groups.is_empty()).then_some(Self { groups, opacity })
    }

    /// Convert a resolved ramp into an opaque grayscale coverage function.
    ///
    /// A CSS gradient used as an alpha mask contributes its interpolated alpha,
    /// not its visible colour. Keeping that coverage in the function's equal
    /// RGB channels lets a PDF luminosity group represent it without a raster
    /// fallback or an intermediate alpha image.
    pub(super) fn from_resolved_alpha(resolved: &ResolvedGradientRamp) -> Option<Self> {
        let mut groups: Vec<PdfFunctionStopGroup> = Vec::with_capacity(resolved.stops().len());
        for stop in resolved.stops() {
            let alpha = stop.color.color.to_f32_rgba().3.clamp(0.0, 1.0);
            let color = PdfFunctionColor::from((alpha, alpha, alpha));
            if let Some(group) = groups.last_mut()
                && group.position == stop.position
            {
                group.outgoing = color;
                continue;
            }
            groups.push(PdfFunctionStopGroup {
                position: stop.position,
                incoming: color,
                outgoing: color,
            });
        }
        (!groups.is_empty()).then_some(Self {
            groups,
            opacity: 1.0,
        })
    }

    pub(super) fn normalized(mut self, span: GradientParameterSpan) -> Option<Self> {
        let length = span.end - span.start;
        if !length.is_finite() || length <= 0.0 {
            return None;
        }
        for group in &mut self.groups {
            group.position = (group.position - span.start) / length;
        }
        Some(self)
    }

    pub(super) const fn opacity(&self) -> f32 {
        self.opacity
    }

    pub(super) fn span(&self) -> Option<GradientParameterSpan> {
        Some(GradientParameterSpan {
            start: self.groups.first()?.position,
            end: self.groups.last()?.position,
        })
    }

    pub(super) fn selector(&self, channel: usize) -> Option<String> {
        let first = *self.groups.first()?;
        let last = *self.groups.last()?;
        if self.groups.len() == 1 {
            return Some(format!(
                "dup {} lt {{pop {}}} {{pop {}}} ifelse",
                format_function_scalar(first.position),
                format_pdf_number(first.incoming.serialized_channel(channel)?),
                format_pdf_number(first.outgoing.serialized_channel(channel)?),
            ));
        }

        let mut code = format!(
            "dup {} lt {{pop {}}} {{",
            format_function_scalar(first.position),
            format_pdf_number(first.incoming.serialized_channel(channel)?),
        );
        for pair in self.groups.windows(2) {
            let [start, end] = pair else {
                return None;
            };
            code.push_str(&format!(
                "dup {} le {{{}}} {{",
                format_function_scalar(end.position),
                start.segment(*end, channel)?,
            ));
        }
        code.push_str(&format!(
            "pop {}",
            format_pdf_number(last.outgoing.serialized_channel(channel)?),
        ));
        for _ in self.groups.windows(2) {
            code.push_str("} ifelse");
        }
        code.push_str("} ifelse");
        Some(code)
    }

    pub(super) fn calculator(&self, parameter: &str) -> Option<String> {
        Some(format!(
            "{{{parameter} dup {} exch dup {} exch {}}}",
            self.selector(0)?,
            self.selector(1)?,
            self.selector(2)?,
        ))
    }
}

#[derive(Debug)]
pub(super) struct LinearFunctionGradient {
    stops: PdfFunctionStopSequence,
    span: GradientParameterSpan,
    repeating: bool,
}

#[derive(Debug)]
pub(super) struct RadialFunctionGradient {
    stops: PdfFunctionStopSequence,
    span: GradientParameterSpan,
}

impl RadialFunctionGradient {
    pub(super) fn period(&self) -> f32 {
        self.span.length()
    }

    pub(super) fn calculator(&self) -> Option<String> {
        let period = self.span.end - self.span.start;
        let cycle_start = self.span.start / period;
        let radius = if cycle_start == 0.0 {
            "dup mul exch dup mul add sqrt".to_owned()
        } else {
            format!(
                "dup mul exch dup mul add sqrt {} sub",
                format_function_scalar(cycle_start),
            )
        };
        self.stops
            .calculator(&format!("{radius} dup truncate sub dup 0 le {{1 add}} if"))
    }
}

impl LinearFunctionGradient {
    #[cfg(test)]
    pub(super) fn selector(&self, channel: usize) -> Option<String> {
        self.stops.selector(channel)
    }

    fn calculator(&self) -> Option<String> {
        let parameter = if self.repeating {
            "pop dup truncate sub dup 0 le {1 add} if"
        } else {
            "pop"
        };
        self.stops.calculator(parameter)
    }
}

pub(super) fn linear_function_gradient(
    ramp: &GradientRamp,
    basis: f32,
) -> Option<LinearFunctionGradient> {
    let resolved = ramp.resolve(basis)?;
    if resolved.uniform_opacity()? != 1.0 {
        return None;
    }

    let repeating = resolved.repeat().is_repeating();
    let has_hard_stop = resolved.stops().windows(2).any(|pair| {
        pair[0].position == pair[1].position && pair[0].color.color != pair[1].color.color
    });
    if !repeating && !has_hard_stop {
        return None;
    }

    let first = resolved.stops().first()?.position;
    let last = resolved.stops().last()?.position;
    let span = if repeating {
        GradientParameterSpan {
            start: first,
            end: last,
        }
    } else {
        GradientParameterSpan {
            start: first.min(0.0),
            end: last.max(1.0),
        }
    };
    let length = span.length();
    if !length.is_finite() || length <= 0.0 {
        return None;
    }

    let stops = PdfFunctionStopSequence::from_resolved(&resolved)?.normalized(span)?;
    Some(LinearFunctionGradient {
        stops,
        span,
        repeating,
    })
}

fn radial_function_gradient(ramp: &GradientRamp, basis: f32) -> Option<RadialFunctionGradient> {
    let resolved = ramp.resolve(basis)?;
    if !resolved.repeat().is_repeating() || resolved.uniform_opacity()? != 1.0 {
        return None;
    }
    let span = GradientParameterSpan {
        start: resolved.stops().first()?.position,
        end: resolved.stops().last()?.position,
    };
    let period = span.length();
    if !period.is_finite() || period <= 0.0 {
        return None;
    }
    Some(RadialFunctionGradient {
        stops: PdfFunctionStopSequence::from_resolved(&resolved)?.normalized(span)?,
        span,
    })
}

/// Build the repeating function used by an alpha-interpreted radial CSS mask.
///
/// `mask-mode: alpha` and `match-source` both use a CSS gradient's alpha
/// channel. Luminance mode stays on the general path because its coverage is
/// derived from interpolated colour instead.
pub(super) fn radial_alpha_function_gradient(
    ramp: &GradientRamp,
    basis: f32,
) -> Option<RadialFunctionGradient> {
    let resolved = ramp.resolve(basis)?;
    if !resolved.repeat().is_repeating() {
        return None;
    }
    let span = GradientParameterSpan {
        start: resolved.stops().first()?.position,
        end: resolved.stops().last()?.position,
    };
    if !span.length().is_finite() || span.length() <= 0.0 {
        return None;
    }
    Some(RadialFunctionGradient {
        stops: PdfFunctionStopSequence::from_resolved_alpha(&resolved)?.normalized(span)?,
        span,
    })
}

pub(super) fn render_linear_function_gradient(
    content: &mut String,
    gradient: &LinearGradient,
    tile: PdfRect,
    content_transform: PageContentTransform,
    pdf_writer: &mut PdfWriter,
) -> bool {
    if content_transform.is_identity() && !pdf_writer.page_content_transform.is_identity() {
        return false;
    }
    let basis = linear_gradient_line_length(gradient.angle, tile.width, tile.height);
    let Some(function) = linear_function_gradient(&gradient.ramp, basis) else {
        return false;
    };
    let span_length = basis * (function.span.end - function.span.start);
    if !span_length.is_finite() || span_length <= 0.0 {
        return false;
    }

    let (sin, cos) = sin_cos_degrees(gradient.angle);
    let direction = PdfVector::new(sin, cos);
    let perpendicular = PdfVector::new(cos, -sin);
    let center = PdfPoint::new(
        tile.left + tile.width / 2.0,
        tile.bottom + tile.height / 2.0,
    );
    let nominal_start = center - direction * (basis / 2.0);
    let span_start = nominal_start + direction * (basis * function.span.start);
    let page = pdf_writer
        .page_content_transform
        .page_bounds()
        .unwrap_or(tile);
    let page_anchor = PdfPoint::new(page.left, page.top());
    let perpendicular_offset = (page_anchor - span_start).dot(perpendicular);
    let transform = pdf_writer.paint_matrix(PdfMatrix::new(
        direction * span_length,
        perpendicular * span_length,
        span_start + perpendicular * perpendicular_offset,
    ));
    let Some(inverse) = transform.inverse() else {
        return false;
    };
    let domain = page.transformed_bounds(inverse);
    let Some(calculator) = function.calculator() else {
        return false;
    };
    let Some(pattern) = PdfFunctionPattern::new(transform, domain, calculator) else {
        return false;
    };
    let name = pdf_writer.add_function_pattern(pattern);
    if content_transform.is_identity() {
        paint_shading_pattern(content, &name, tile);
    } else if paint_css_page_pattern(content, content_transform, &name, tile).is_none() {
        return false;
    }
    true
}

pub(super) fn render_radial_function_gradient(
    content: &mut String,
    gradient: &RadialGradient,
    geometry: RadialGradientGeometry,
    tile: PdfRect,
    content_transform: PageContentTransform,
    pdf_writer: &mut PdfWriter,
) -> bool {
    if content_transform.is_identity() && !pdf_writer.page_content_transform.is_identity() {
        return false;
    }
    let Some(function) = radial_function_gradient(&gradient.ramp, geometry.stop_basis()) else {
        return false;
    };
    let period = function.period();
    let period_radii = geometry.point_radii() * period;
    if !period_radii.is_positive() {
        return false;
    }
    let transform = pdf_writer.paint_matrix(PdfMatrix::new(
        PdfVector::new(period_radii.x, 0.0),
        PdfVector::new(0.0, -period_radii.y),
        geometry.page_center(tile),
    ));
    let page = pdf_writer
        .page_content_transform
        .page_bounds()
        .unwrap_or(tile);
    let Some(inverse) = transform.inverse() else {
        return false;
    };
    let domain = page.transformed_bounds(inverse);
    let Some(calculator) = function.calculator() else {
        return false;
    };
    let Some(pattern) = PdfFunctionPattern::new(transform, domain, calculator) else {
        return false;
    };
    let name = pdf_writer.add_function_pattern(pattern);
    if content_transform.is_identity() {
        paint_shading_pattern(content, &name, tile);
    } else if paint_css_page_pattern(content, content_transform, &name, tile).is_none() {
        return false;
    }
    true
}