summaryrefslogtreecommitdiffstats
path: root/cairo-renderer/src
diff options
context:
space:
mode:
authorMinijackson <minijackson@riseup.net>2023-02-03 19:59:43 +0100
committerMinijackson <minijackson@riseup.net>2023-02-03 19:59:43 +0100
commit82ffe3187a32bad4ecca0736882a23793a800822 (patch)
tree2d3ab9231b76dadcf2ab69b27053df6cc66bead1 /cairo-renderer/src
parentc8bb3e4a24ca1ca6ec1a57c9e8cd8655483b3eb0 (diff)
downloaddiaphragm-82ffe3187a32bad4ecca0736882a23793a800822.tar.gz
diaphragm-82ffe3187a32bad4ecca0736882a23793a800822.zip
add support for images, and some chores
- SVG files not yet supported - Remove dead commented code - Add inherent constraints for text, path, and images - Formatting
Diffstat (limited to 'cairo-renderer/src')
-rw-r--r--cairo-renderer/src/lib.rs43
1 files changed, 38 insertions, 5 deletions
diff --git a/cairo-renderer/src/lib.rs b/cairo-renderer/src/lib.rs
index 40d24d1..5a730f4 100644
--- a/cairo-renderer/src/lib.rs
+++ b/cairo-renderer/src/lib.rs
@@ -1,3 +1,10 @@
1use std::{
2 collections::HashMap,
3 path::{Path, PathBuf},
4};
5
6use gdk::{gdk_pixbuf::Pixbuf, prelude::GdkContextExt};
7
1use diaphragm_core::{ 8use diaphragm_core::{
2 styles::{DefinedDashStyle, Pattern}, 9 styles::{DefinedDashStyle, Pattern},
3 text::{DefinedFontDescription, FontDescription}, 10 text::{DefinedFontDescription, FontDescription},
@@ -6,6 +13,7 @@ use diaphragm_core::{
6 13
7pub struct CairoRenderer { 14pub struct CairoRenderer {
8 ctx: cairo::Context, 15 ctx: cairo::Context,
16 loaded_images: HashMap<PathBuf, Pixbuf>,
9} 17}
10 18
11impl CairoRenderer { 19impl CairoRenderer {
@@ -15,7 +23,17 @@ impl CairoRenderer {
15 let surface = cairo::SvgSurface::for_stream(1920., 1080., std::io::sink()).unwrap(); 23 let surface = cairo::SvgSurface::for_stream(1920., 1080., std::io::sink()).unwrap();
16 let ctx = cairo::Context::new(&surface).unwrap(); 24 let ctx = cairo::Context::new(&surface).unwrap();
17 25
18 Self { ctx } 26 Self {
27 ctx,
28 loaded_images: HashMap::new(),
29 }
30 }
31
32 fn get_image(&mut self, path: &Path) -> &Pixbuf {
33 let path = path.to_owned();
34 self.loaded_images
35 .entry(path.clone())
36 .or_insert_with(|| Pixbuf::from_file(path).unwrap())
19 } 37 }
20} 38}
21 39
@@ -102,9 +120,6 @@ impl Renderer for CairoRenderer {
102 extents.width() as f64 / TEST_ABSOLUTE_SIZE, 120 extents.width() as f64 / TEST_ABSOLUTE_SIZE,
103 extents.height() as f64 / TEST_ABSOLUTE_SIZE, 121 extents.height() as f64 / TEST_ABSOLUTE_SIZE,
104 ) 122 )
105
106 //let (width, height) = layout.get_pixel_size();
107 //(width as f64, height as f64)
108 } 123 }
109 124
110 fn show_text(&mut self, text: &str, font: &DefinedFontDescription) { 125 fn show_text(&mut self, text: &str, font: &DefinedFontDescription) {
@@ -115,7 +130,25 @@ impl Renderer for CairoRenderer {
115 layout.set_font_description(Some(&font_desc)); 130 layout.set_font_description(Some(&font_desc));
116 layout.set_markup(text); 131 layout.set_markup(text);
117 132
118 //self.ctx.set_font_size(dbg!(font.size));
119 pangocairo::show_layout(&self.ctx, &layout); 133 pangocairo::show_layout(&self.ctx, &layout);
120 } 134 }
135
136 fn show_image(&mut self, path: &Path, x: f64, y: f64, width: f64, height: f64) {
137 let image = self.get_image(path).clone();
138
139 self.ctx.save().unwrap();
140
141 self.ctx
142 .scale(width / image.width() as f64, height / image.height() as f64);
143
144 self.ctx.set_source_pixbuf(&image, x, y);
145 self.ctx.paint().unwrap();
146
147 self.ctx.restore().unwrap();
148 }
149
150 fn geometry_for_image(&mut self, path: &Path) -> (f64, f64) {
151 let image = self.get_image(path);
152 (image.width() as f64, image.height() as f64)
153 }
121} 154}