Skip to content

Commit 55b7bb6

Browse files
committed
Initial
1 parent d927d1a commit 55b7bb6

File tree

4 files changed

+68
-39
lines changed

4 files changed

+68
-39
lines changed

.gitignore

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/target
2+
**/*.rs.bk
3+
Cargo.lock
4+
.*.sw*
5+
backup/*
6+
**/Cargo.lock
7+
**/target
8+
examples/wasm-demo/www/pkg
9+
examples/.ipynb_checkpoints/
10+
tarpaulin-report.html
11+
.vscode/*

Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "plotters-cairo"
3+
version = "0.1.0"
4+
authors = ["Hao Hou <[email protected]>"]
5+
edition = "2018"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
plotters-backend = "0.0.*"
11+
12+
[dependencies.cairo-rs]
13+
version = "0.8.0"
14+
features = ["ps"]
15+
16+
[dev-dependencies]
17+
plotters = { path = "../plotters", default_features = false}
18+

src/backend.rs

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use cairo::{Context as CairoContext, FontSlant, FontWeight, Status as CairoStatus};
22

33
#[allow(unused_imports)]
4-
use crate::drawing::backend::{BackendCoord, BackendStyle, DrawingBackend, DrawingErrorKind};
5-
use crate::style::text_anchor::{HPos, VPos};
6-
#[allow(unused_imports)]
7-
use crate::style::{Color, FontDesc, FontStyle, FontTransform, RGBAColor, TextStyle};
4+
use plotters_backend::{BackendCoord, BackendStyle, DrawingBackend, DrawingErrorKind, FontStyle, FontTransform, BackendColor, BackendTextStyle};
5+
use plotters_backend::text_anchor::{HPos, VPos};
86

97
/// The drawing backend that is backed with a Cairo context
108
pub struct CairoBackend<'a> {
@@ -50,13 +48,13 @@ impl<'a> CairoBackend<'a> {
5048
}
5149
}
5250

53-
fn set_color(&self, color: &RGBAColor) -> Result<(), DrawingErrorKind<CairoError>> {
51+
fn set_color(&self, color: &BackendColor) -> Result<(), DrawingErrorKind<CairoError>> {
5452
self.call_cairo(|c| {
5553
c.set_source_rgba(
56-
f64::from(color.rgb().0) / 255.0,
57-
f64::from(color.rgb().1) / 255.0,
58-
f64::from(color.rgb().2) / 255.0,
59-
color.alpha(),
54+
f64::from(color.rgb.0) / 255.0,
55+
f64::from(color.rgb.1) / 255.0,
56+
f64::from(color.rgb.2) / 255.0,
57+
color.alpha,
6058
)
6159
})
6260
}
@@ -65,21 +63,21 @@ impl<'a> CairoBackend<'a> {
6563
self.call_cairo(|c| c.set_line_width(f64::from(width)))
6664
}
6765

68-
fn set_font<'b>(&self, font: &FontDesc<'b>) -> Result<(), DrawingErrorKind<CairoError>> {
69-
let actual_size = font.get_size();
66+
fn set_font<S: BackendTextStyle>(&self, font: &S) -> Result<(), DrawingErrorKind<CairoError>> {
67+
let actual_size = font.size();
7068
self.call_cairo(|c| {
71-
match font.get_style() {
69+
match font.style() {
7270
FontStyle::Normal => {
73-
c.select_font_face(font.get_name(), FontSlant::Normal, FontWeight::Normal)
71+
c.select_font_face(font.family().as_str(), FontSlant::Normal, FontWeight::Normal)
7472
}
7573
FontStyle::Bold => {
76-
c.select_font_face(font.get_name(), FontSlant::Normal, FontWeight::Bold)
74+
c.select_font_face(font.family().as_str(), FontSlant::Normal, FontWeight::Bold)
7775
}
7876
FontStyle::Oblique => {
79-
c.select_font_face(font.get_name(), FontSlant::Oblique, FontWeight::Normal)
77+
c.select_font_face(font.family().as_str(), FontSlant::Oblique, FontWeight::Normal)
8078
}
8179
FontStyle::Italic => {
82-
c.select_font_face(font.get_name(), FontSlant::Italic, FontWeight::Normal)
80+
c.select_font_face(font.family().as_str(), FontSlant::Italic, FontWeight::Normal)
8381
}
8482
};
8583
c.set_font_size(actual_size);
@@ -125,15 +123,15 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
125123
fn draw_pixel(
126124
&mut self,
127125
point: BackendCoord,
128-
color: &RGBAColor,
126+
color: BackendColor,
129127
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
130128
self.call_cairo(|c| {
131129
c.rectangle(f64::from(point.0), f64::from(point.1), 1.0, 1.0);
132130
c.set_source_rgba(
133-
f64::from(color.rgb().0) / 255.0,
134-
f64::from(color.rgb().1) / 255.0,
135-
f64::from(color.rgb().2) / 255.0,
136-
color.alpha(),
131+
f64::from(color.rgb.0) / 255.0,
132+
f64::from(color.rgb.1) / 255.0,
133+
f64::from(color.rgb.2) / 255.0,
134+
color.alpha,
137135
);
138136
c.fill();
139137
})
@@ -145,7 +143,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
145143
to: BackendCoord,
146144
style: &S,
147145
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
148-
self.set_color(&style.as_color())?;
146+
self.set_color(&style.color())?;
149147
self.set_stroke_width(style.stroke_width())?;
150148

151149
self.call_cairo(|c| {
@@ -162,7 +160,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
162160
style: &S,
163161
fill: bool,
164162
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
165-
self.set_color(&style.as_color())?;
163+
self.set_color(&style.color())?;
166164
self.set_stroke_width(style.stroke_width())?;
167165

168166
self.call_cairo(|c| {
@@ -185,7 +183,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
185183
path: I,
186184
style: &S,
187185
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
188-
self.set_color(&style.as_color())?;
186+
self.set_color(&style.color())?;
189187
self.set_stroke_width(style.stroke_width())?;
190188

191189
let mut path = path.into_iter();
@@ -205,7 +203,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
205203
path: I,
206204
style: &S,
207205
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
208-
self.set_color(&style.as_color())?;
206+
self.set_color(&style.color())?;
209207
self.set_stroke_width(style.stroke_width())?;
210208

211209
let mut path = path.into_iter();
@@ -233,7 +231,7 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
233231
style: &S,
234232
fill: bool,
235233
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
236-
self.set_color(&style.as_color())?;
234+
self.set_color(&style.color())?;
237235
self.set_stroke_width(style.stroke_width())?;
238236

239237
self.call_cairo(|c| {
@@ -254,29 +252,28 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
254252
})
255253
}
256254

257-
fn estimate_text_size<'b>(
255+
fn estimate_text_size<S: BackendTextStyle>(
258256
&self,
259257
text: &str,
260-
font: &FontDesc<'b>,
258+
font: &S,
261259
) -> Result<(u32, u32), DrawingErrorKind<Self::ErrorType>> {
262-
self.set_font(&font)?;
260+
self.set_font(font)?;
263261
self.call_cairo(|c| {
264262
let extents = c.text_extents(text);
265263
(extents.width as u32, extents.height as u32)
266264
})
267265
}
268266

269-
fn draw_text(
267+
fn draw_text<S: BackendTextStyle>(
270268
&mut self,
271269
text: &str,
272-
style: &TextStyle,
270+
style: &S,
273271
pos: BackendCoord,
274272
) -> Result<(), DrawingErrorKind<Self::ErrorType>> {
275-
let font = &style.font;
276-
let color = &style.color;
273+
let color = style.color();
277274
let (mut x, mut y) = (pos.0, pos.1);
278275

279-
let degree = match font.get_transform() {
276+
let degree = match style.transform() {
280277
FontTransform::None => 0.0,
281278
FontTransform::Rotate90 => 90.0,
282279
FontTransform::Rotate180 => 180.0,
@@ -294,17 +291,17 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
294291
y = 0;
295292
}
296293

297-
self.set_font(&font)?;
294+
self.set_font(style)?;
298295
self.set_color(&color)?;
299296

300297
self.call_cairo(|c| {
301298
let extents = c.text_extents(text);
302-
let dx = match style.pos.h_pos {
299+
let dx = match style.anchor().h_pos {
303300
HPos::Left => 0.0,
304301
HPos::Right => -extents.width,
305302
HPos::Center => -extents.width / 2.0,
306303
};
307-
let dy = match style.pos.v_pos {
304+
let dy = match style.anchor().v_pos {
308305
VPos::Top => extents.height,
309306
VPos::Center => extents.height / 2.0,
310307
VPos::Bottom => 0.0,
@@ -324,8 +321,8 @@ impl<'a> DrawingBackend for CairoBackend<'a> {
324321
#[cfg(test)]
325322
mod test {
326323
use super::*;
327-
use crate::prelude::*;
328-
use crate::style::text_anchor::{HPos, Pos, VPos};
324+
use plotters::prelude::*;
325+
use plotters_backend::text_anchor::{HPos, Pos, VPos};
329326
use std::fs;
330327
use std::path::Path;
331328

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mod backend;
2+
3+
pub use backend::CairoBackend;

0 commit comments

Comments
 (0)