60 lines
1.8 KiB
Rust
60 lines
1.8 KiB
Rust
extern crate getopts;
|
|
extern crate image;
|
|
|
|
use std::io::Read;
|
|
use std::fs::File;
|
|
|
|
pub mod css;
|
|
pub mod display;
|
|
pub mod dom;
|
|
pub mod html;
|
|
pub mod layout;
|
|
pub mod styling;
|
|
|
|
fn read_source(filename: String) -> String {
|
|
let mut str = String::new();
|
|
File::open(filename).unwrap().read_to_string(&mut str).unwrap();
|
|
str
|
|
}
|
|
|
|
fn main() {
|
|
let mut opts = getopts::Options::new();
|
|
opts.optopt("h", "html", "HTML document", "FILENAME");
|
|
opts.optopt("o", "out", "PNG output", "FILENAME");
|
|
|
|
let matches = opts.parse(std::env::args().skip(1)).unwrap();
|
|
let str_arg = |flag: &str, default: &str| -> String {
|
|
matches.opt_str(flag).unwrap_or(default.to_string())
|
|
};
|
|
|
|
let initial_block = layout::Dim {
|
|
content: layout::Rect { x: 0.0, y: 0.0, width: 800.0, height: 600.0 },
|
|
padding: Default::default(),
|
|
border: Default::default(),
|
|
margin: Default::default(),
|
|
};
|
|
|
|
let html = read_source(str_arg("h", "examples/test.html"));
|
|
let node = html::parse(html);
|
|
let style = dom::find_style(&node).unwrap();
|
|
let styled_node = styling::style_tree(&node, &style);
|
|
|
|
let layout = layout::layout_tree(&styled_node, initial_block);
|
|
let canvas = display::paint(&layout, initial_block.content);
|
|
|
|
let filename = str_arg("o", "out.png");
|
|
let mut file = File::create(&filename).unwrap();
|
|
|
|
let (w, h) = (canvas.width as u32, canvas.height as u32);
|
|
let img = image::ImageBuffer::from_fn(w, h, move |x, y| {
|
|
let color = canvas.pixels[(y * w + x) as usize];
|
|
image::Pixel::from_channels(color.r, color.g, color.b, color.a)
|
|
});
|
|
|
|
let result = image::ImageRgba8(img).save(&mut file, image::PNG);
|
|
match result {
|
|
Ok(_) => println!("Saved output as {}", filename),
|
|
Err(_) => println!("Error saving output as {}", filename)
|
|
}
|
|
}
|