libharu/
lib.rs

1//! Rust binding of libharu PDF library.
2
3#![warn(missing_docs)]
4mod document;
5mod page;
6mod outline;
7mod destination;
8mod encoder;
9mod error;
10mod context;
11mod image;
12
13/// prelude
14pub mod prelude;
15
16/// Floating-point type used in libharu.
17pub type Real = libharu_sys::HPDF_REAL;
18
19/// RGB color type.
20#[derive(Debug, Clone)]
21pub struct Color {
22    /// red (0.0 ~ 1.0)
23    pub red: Real,
24
25    /// green (0.0 ~ 1.0)
26    pub green: Real,
27
28    /// blue (0.0 ~ 1.0)
29    pub blue: Real,
30}
31
32impl Copy for Color {}
33
34impl From<(Real, Real, Real)> for Color {
35    fn from(v: (Real, Real, Real)) -> Self {
36        Self { red: v.0, green: v.1, blue: v.2 }
37    }
38}
39
40/// CMYK color type
41#[derive(Debug, Clone)]
42pub struct CmykColor {
43    /// cyan (0.0 ~ 1.0)
44    pub cyan: Real,
45
46    /// magenta (0.0 ~ 1.0)
47    pub magenta: Real,
48    
49    /// yellow (0.0 ~ 1.0)
50    pub yellow: Real,
51    
52    /// keyplate (0.0 ~ 1.0)
53    pub keyplate: Real,
54}
55
56impl Copy for CmykColor {}
57
58impl From<(Real, Real, Real, Real)> for CmykColor {
59    fn from(v: (Real, Real, Real, Real)) -> Self {
60        Self { cyan: v.0, magenta: v.1, yellow: v.2, keyplate: v.3 }
61    }
62}
63/// Point
64#[derive(Debug, Clone)]
65pub struct Point {
66    /// x
67    pub x: Real,
68
69    /// y
70    pub y: Real,
71}
72
73impl Copy for Point {}
74
75impl From<(Real, Real)> for Point {
76    fn from(v: (Real, Real)) -> Self {
77        Self { x: v.0, y: v.1 }
78    }
79}
80
81/// Rect
82#[derive(Debug, Clone)]
83pub struct Rect {
84    /// Left position
85    pub left: Real,
86
87    /// Top position
88    pub top: Real,
89    
90    /// Right position
91    pub right: Real,
92
93    /// Bottom position
94    pub bottom: Real,
95}
96
97impl Copy for Rect {}
98
99impl From<(Real, Real, Real, Real)> for Rect {
100    fn from(v: (Real, Real, Real, Real)) -> Self {
101        Self { left: v.0, top: v.1, right: v.2, bottom: v.3 }
102    }
103}
104
105/// Font handle type.
106pub struct Font<'a> {
107    font: libharu_sys::HPDF_Font,
108    _doc: &'a prelude::Document,
109}
110
111impl<'a> Font<'a> {
112    pub(crate) fn new(_doc: &'a prelude::Document, font: libharu_sys::HPDF_Font) -> Self {
113        Self { font, _doc }
114    }
115
116    #[inline]
117    pub(crate) fn handle(&self) -> libharu_sys::HPDF_Font {
118        self.font
119    }
120
121    /// Get the name of the font.
122    pub fn name(&self) -> anyhow::Result<&str> {
123        unsafe {
124            let name = libharu_sys::HPDF_Font_GetFontName(self.handle());
125
126            let s = std::ffi::CStr::from_ptr(name).to_str()?;
127
128            Ok(s)
129        }
130    }
131}