Skip to content

Commit

Permalink
feat(gui): can render the ofd to gui
Browse files Browse the repository at this point in the history
  • Loading branch information
hualet committed Feb 6, 2025
1 parent ddfbb93 commit 9e3b489
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
34 changes: 19 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use document::{Document, DocumentRes, PublicRes, Annotations, PageAnnot};
use ofd::{Ofd, OfdNode};
use page::Page;
use render::Renderable;
use types::ct;
// use types::ct;

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

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

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



// Create a cairo surface and context.
let pybox = ct::PageArea::from(document.common_data.page_area.physical_box.clone()).to_pixel();
let surface = cairo::ImageSurface::create(
cairo::Format::ARgb32,
pybox.width as i32,
pybox.height as i32,
)?;

let mut context = cairo::Context::new(&surface)?;
// let _pybox = ct::PageArea::from(document.common_data.page_area.physical_box.clone()).to_pixel();

// draw page
let pages = &document.pages.page;
Expand All @@ -160,10 +150,24 @@ pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str) -> Result<(), Box<dyn
page_file.read_to_string(&mut content)?;
}
let page = Page::from_xml(&content)?;
page.render(&mut context, ofd, &document)?;
page.render(context, ofd, &document)?;
}

Ok(())
}

pub fn export_ofd_to_png(ofd: &mut Ofd, output_path: &str, width: u32, height: u32) -> Result<(), Box<dyn Error>> {
let surface = cairo::ImageSurface::create(
cairo::Format::ARgb32,
width as i32,
height as i32,
)?;

let mut context = cairo::Context::new(&surface)?;
render_ofd_to_context(ofd, &mut context)?;

let mut file = File::create(output_path)?;
surface.write_to_png(&mut file)?;

Ok(())
}
}
17 changes: 8 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
use rofd::*;

use env_logger;

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

// fn main() {
// env_logger::init();
// let mut ofd_node = read_ofd("learning/test.ofd").unwrap();
// export_ofd_to_png(&mut ofd_node, "target/out.png").unwrap();
// }


mod widgets {
use rofd::*;
use gtk::{gdk, glib, graphene, gsk, prelude::*, subclass::prelude::*};

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

snapshot.push_fill(&fill_path, gsk::FillRule::Winding);
snapshot.append_color(&fill_color, &bounds);


let mut context = snapshot.append_cairo(&bounds);
let mut ofd_node = read_ofd("learning/test.ofd").unwrap();
render_ofd_to_context(&mut ofd_node, &mut context).unwrap();


snapshot.pop();
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/main_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn test_read_ofd() {
fn test_export_ofd_to_png() {
let mut ofd_node = read_ofd("./learning/test.ofd").unwrap();
println!("ofd: {:#?}", ofd_node);
export_ofd_to_png(&mut ofd_node, "target/out.png").unwrap();
export_ofd_to_png(&mut ofd_node, "target/out.png", 400, 400).unwrap();
}

0 comments on commit 9e3b489

Please sign in to comment.