Skip to content

Commit 9e3b489

Browse files
committed
feat(gui): can render the ofd to gui
1 parent ddfbb93 commit 9e3b489

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

src/lib.rs

+19-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use document::{Document, DocumentRes, PublicRes, Annotations, PageAnnot};
2020
use ofd::{Ofd, OfdNode};
2121
use page::Page;
2222
use render::Renderable;
23-
use types::ct;
23+
// use types::ct;
2424

2525
pub fn read_ofd(file_path: &str) -> Result<Ofd, Box<dyn Error>> {
2626
let file = File::open(file_path)?;
@@ -46,7 +46,7 @@ pub fn read_ofd(file_path: &str) -> Result<Ofd, Box<dyn Error>> {
4646
Ok(ofd_result)
4747
}
4848

49-
pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str) -> Result<(), Box<dyn Error>> {
49+
pub fn render_ofd_to_context(ofd: &mut Ofd, context: &mut cairo::Context) -> Result<(), Box<dyn Error>> {
5050
// create a new String to store the content of the DocRoot file.
5151
let mut content = String::new();
5252

@@ -128,17 +128,7 @@ pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str) -> Result<(), Box<dyn
128128
}
129129
}
130130

131-
132-
133-
// Create a cairo surface and context.
134-
let pybox = ct::PageArea::from(document.common_data.page_area.physical_box.clone()).to_pixel();
135-
let surface = cairo::ImageSurface::create(
136-
cairo::Format::ARgb32,
137-
pybox.width as i32,
138-
pybox.height as i32,
139-
)?;
140-
141-
let mut context = cairo::Context::new(&surface)?;
131+
// let _pybox = ct::PageArea::from(document.common_data.page_area.physical_box.clone()).to_pixel();
142132

143133
// draw page
144134
let pages = &document.pages.page;
@@ -160,10 +150,24 @@ pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str) -> Result<(), Box<dyn
160150
page_file.read_to_string(&mut content)?;
161151
}
162152
let page = Page::from_xml(&content)?;
163-
page.render(&mut context, ofd, &document)?;
153+
page.render(context, ofd, &document)?;
164154
}
165155

156+
Ok(())
157+
}
158+
159+
pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str, width: u32, height: u32) -> Result<(), Box<dyn Error>> {
160+
let surface = cairo::ImageSurface::create(
161+
cairo::Format::ARgb32,
162+
width as i32,
163+
height as i32,
164+
)?;
165+
166+
let mut context = cairo::Context::new(&surface)?;
167+
render_ofd_to_context(ofd, &mut context)?;
168+
166169
let mut file = File::create(output_path)?;
167170
surface.write_to_png(&mut file)?;
171+
168172
Ok(())
169-
}
173+
}

src/main.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
use rofd::*;
2-
31
use env_logger;
42

53
use gtk::{glib, prelude::*};
64

7-
// fn main() {
8-
// env_logger::init();
9-
// let mut ofd_node = read_ofd("learning/test.ofd").unwrap();
10-
// export_ofd_to_png(&mut ofd_node, "target/out.png").unwrap();
11-
// }
12-
135

146
mod widgets {
7+
use rofd::*;
158
use gtk::{gdk, glib, graphene, gsk, prelude::*, subclass::prelude::*};
169

1710
glib::wrapper! {
@@ -53,7 +46,13 @@ mod widgets {
5346
let fill_path = path_builder.to_path();
5447

5548
snapshot.push_fill(&fill_path, gsk::FillRule::Winding);
56-
snapshot.append_color(&fill_color, &bounds);
49+
50+
51+
let mut context = snapshot.append_cairo(&bounds);
52+
let mut ofd_node = read_ofd("learning/test.ofd").unwrap();
53+
render_ofd_to_context(&mut ofd_node, &mut context).unwrap();
54+
55+
5756
snapshot.pop();
5857
}
5958
}

tests/main_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ fn test_read_ofd() {
1010
fn test_export_ofd_to_png() {
1111
let mut ofd_node = read_ofd("./learning/test.ofd").unwrap();
1212
println!("ofd: {:#?}", ofd_node);
13-
export_ofd_to_png(&mut ofd_node, "target/out.png").unwrap();
13+
export_ofd_to_png(&mut ofd_node, "target/out.png", 400, 400).unwrap();
1414
}

0 commit comments

Comments
 (0)