Skip to main content

rpdfium_render/
stroke.rs

1// Derived from PDFium's core/fxge/cfx_renderdevice.cpp stroke handling
2// Original: Copyright 2014 The PDFium Authors
3// Licensed under BSD-3-Clause / Apache-2.0
4// See pdfium-upstream/LICENSE for the original license.
5
6//! Stroke style conversion from PDF path styles.
7
8use rpdfium_graphics::{DashPattern, LineCapStyle, LineJoinStyle, PathStyle};
9
10/// Stroke style parameters for rendering.
11#[derive(Debug, Clone, PartialEq)]
12pub struct StrokeStyle {
13    /// Line width in user space units.
14    pub width: f32,
15    /// Line cap style.
16    pub line_cap: LineCapStyle,
17    /// Line join style.
18    pub line_join: LineJoinStyle,
19    /// Miter limit for miter joins.
20    pub miter_limit: f32,
21    /// Dash pattern, or `None` for solid lines.
22    pub dash: Option<DashPattern>,
23}
24
25impl StrokeStyle {
26    /// Create a stroke style from a PDF path style.
27    pub fn from_path_style(style: &PathStyle) -> Self {
28        Self {
29            width: style.line_width,
30            line_cap: style.line_cap,
31            line_join: style.line_join,
32            miter_limit: style.miter_limit,
33            dash: style.dash.clone(),
34        }
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_from_default_path_style() {
44        let ps = PathStyle::default();
45        let ss = StrokeStyle::from_path_style(&ps);
46        assert_eq!(ss.width, 1.0);
47        assert_eq!(ss.line_cap, LineCapStyle::Butt);
48        assert_eq!(ss.line_join, LineJoinStyle::Miter);
49        assert_eq!(ss.miter_limit, 10.0);
50        assert!(ss.dash.is_none());
51    }
52
53    #[test]
54    fn test_from_custom_path_style() {
55        let ps = PathStyle {
56            line_width: 3.0,
57            line_cap: LineCapStyle::Round,
58            line_join: LineJoinStyle::Bevel,
59            miter_limit: 5.0,
60            dash: Some(DashPattern {
61                array: vec![3.0, 2.0],
62                phase: 1.0,
63            }),
64            ..PathStyle::default()
65        };
66        let ss = StrokeStyle::from_path_style(&ps);
67        assert_eq!(ss.width, 3.0);
68        assert_eq!(ss.line_cap, LineCapStyle::Round);
69        assert_eq!(ss.line_join, LineJoinStyle::Bevel);
70        assert_eq!(ss.miter_limit, 5.0);
71        let dash = ss.dash.unwrap();
72        assert_eq!(dash.array, vec![3.0, 2.0]);
73        assert_eq!(dash.phase, 1.0);
74    }
75}