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
#![allow(clippy::identity_op)]
use cairo::{Context, Format, ImageSurface};
#[cfg(feature = "png")]
use png::{ColorType, Encoder};
#[cfg(feature = "png")]
use std::fs::File;
#[cfg(feature = "png")]
use std::io::BufWriter;
use std::marker::PhantomData;
use std::path::Path;
use piet::ImageFormat;
#[doc(hidden)]
pub use piet_cairo::*;
pub type Piet<'a> = CairoRenderContext<'a>;
pub type Brush = piet_cairo::Brush;
pub type PietText<'a> = CairoText<'a>;
pub type PietFont = CairoFont;
pub type PietFontBuilder<'a> = CairoFontBuilder;
pub type PietTextLayout = CairoTextLayout;
pub type PietTextLayoutBuilder<'a> = CairoTextLayoutBuilder;
pub type Image = ImageSurface;
pub struct Device;
pub struct BitmapTarget<'a> {
surface: ImageSurface,
cr: Context,
phantom: PhantomData<&'a ()>,
}
impl Device {
pub fn new() -> Result<Device, piet::Error> {
Ok(Device)
}
pub fn bitmap_target(
&mut self,
width: usize,
height: usize,
pix_scale: f64,
) -> Result<BitmapTarget, piet::Error> {
let surface = ImageSurface::create(Format::ARgb32, width as i32, height as i32).unwrap();
let cr = Context::new(&surface);
cr.scale(pix_scale, pix_scale);
let phantom = Default::default();
Ok(BitmapTarget {
surface,
cr,
phantom,
})
}
}
impl<'a> BitmapTarget<'a> {
pub fn render_context(&mut self) -> CairoRenderContext {
CairoRenderContext::new(&mut self.cr)
}
pub fn into_raw_pixels(mut self, fmt: ImageFormat) -> Result<Vec<u8>, piet::Error> {
if fmt != ImageFormat::RgbaPremul {
return Err(piet::Error::NotSupported);
}
std::mem::drop(self.cr);
self.surface.flush();
let stride = self.surface.get_stride() as usize;
let width = self.surface.get_width() as usize;
let height = self.surface.get_height() as usize;
let mut raw_data = vec![0; width * height * 4];
let buf = self
.surface
.get_data()
.map_err(Into::<Box<dyn std::error::Error>>::into)?;
for y in 0..height {
let src_off = y * stride;
let dst_off = y * width * 4;
for x in 0..width {
raw_data[dst_off + x * 4 + 0] = buf[src_off + x * 4 + 2];
raw_data[dst_off + x * 4 + 1] = buf[src_off + x * 4 + 1];
raw_data[dst_off + x * 4 + 2] = buf[src_off + x * 4 + 0];
raw_data[dst_off + x * 4 + 3] = buf[src_off + x * 4 + 3];
}
}
Ok(raw_data)
}
#[cfg(feature = "png")]
pub fn save_to_file<P: AsRef<Path>>(self, path: P) -> Result<(), piet::Error> {
let height = self.surface.get_height();
let width = self.surface.get_width();
let image = self.into_raw_pixels(ImageFormat::RgbaPremul)?;
let file = BufWriter::new(File::create(path).map_err(|e| Into::<Box<_>>::into(e))?);
let mut encoder = Encoder::new(file, width as u32, height as u32);
encoder.set_color(ColorType::RGBA);
encoder
.write_header()
.map_err(|e| Into::<Box<_>>::into(e))?
.write_image_data(&image)
.map_err(|e| Into::<Box<_>>::into(e))?;
Ok(())
}
#[cfg(not(feature = "png"))]
pub fn save_to_file<P: AsRef<Path>>(self, _path: P) -> Result<(), piet::Error> {
Err(piet::Error::MissingFeature)
}
}