Compare commits

...

4 Commits

3 changed files with 66 additions and 133 deletions

View File

@@ -3,6 +3,7 @@ use std::{
collections::BTreeMap,
path::{Path, PathBuf},
rc::Rc,
time::Instant,
};
use async_channel::Sender;
@@ -31,6 +32,7 @@ impl PageCache {
pub fn cache_pages(&mut self, page_numbers: Vec<usize>) {
println!("Caching pages {:?}", page_numbers);
let begin_of_cashing = Instant::now();
for page_number in page_numbers {
if self.pages.contains_key(&page_number) {
continue;
@@ -44,7 +46,10 @@ impl PageCache {
}
}
}
println!("done caching");
println!(
"done caching in {}ms",
begin_of_cashing.elapsed().as_millis()
);
}
fn remove_most_distant_page(&mut self, current_page_number: usize) -> Result<(), ()> {

View File

@@ -1,140 +1,76 @@
use std::{rc::Rc, time::Instant};
use cairo::Context;
use poppler::Page;
use crate::ui::Ui;
use gtk::{prelude::*, DrawingArea};
use crate::ui::DocumentCanvas;
pub fn draw(ui: &mut Ui, area: &DrawingArea, context: &Context) {
pub fn draw(
document_canvas: &Option<DocumentCanvas>,
context: &Context,
area_width: i32,
area_height: i32,
) {
println!("Draw");
if ui.document_canvas.is_none() {
return;
}
let document_canvas = ui.document_canvas.as_ref().unwrap();
if let Some(document_canvas) = document_canvas {
let begin_of_drawing = Instant::now();
if document_canvas.num_pages.unwrap_or(0) > 1 {
let mut pages = Vec::new();
if let Some(page_left) = &document_canvas.left_page {
pages.push(Rc::clone(page_left));
}
if let Some(page_right) = &document_canvas.right_page {
pages.push(Rc::clone(page_right));
}
draw_pages(&pages, context, area_width, area_height);
}
if document_canvas.num_pages.unwrap_or(0) > 1 {
draw_two_pages(ui, area, context);
} else {
draw_single_page(ui, area, context);
println!(
"Finished drawing in {}ms",
begin_of_drawing.elapsed().as_millis()
);
document_canvas.cache_surrounding_pages();
}
println!("Finished drawing");
document_canvas.cache_surrounding_pages();
}
fn draw_two_pages(ui: &Ui, area: &DrawingArea, context: &Context) {
if ui.document_canvas.is_none() {
fn draw_pages(pages: &[Rc<Page>], context: &Context, area_width: i32, area_height: i32) {
if pages.is_empty() {
return;
}
let document_canvas = ui.document_canvas.as_ref().unwrap();
let area_width = area_width as f64;
let area_height = area_height as f64;
let page_left = document_canvas.left_page.as_ref();
let page_right = document_canvas.right_page.as_ref();
if page_left.is_none() || page_right.is_none() {
// TODO: show error message
return;
}
let page_left = page_left.unwrap();
let page_right = page_right.unwrap();
// Add white background
// context.set_source_rgba(1.0, 1.0, 1.0, 1.0);
// context.fill().unwrap();
// context.paint().unwrap();
let (w_left, h_left) = page_left.size();
let (w_right, h_right) = page_right.size();
let h_max = f64::max(h_left, h_right);
// Make sure both pages are rendered with the same height
let w_max = match h_left < h_right {
true => w_left * h_right / h_left + w_right,
false => w_left + w_right * h_left / h_right,
};
let h_scale = area.height() as f64 / h_max;
let w_scale = area.width() as f64 / w_max;
let scale = f64::min(h_scale, w_scale);
let h_page = h_max * scale;
let scale_left = h_page / h_left;
let scale_right = h_page / h_right;
// Total width if height of every page was 1
let total_width_normalized: f64 = pages
.iter()
.map(|page| page.size())
.map(|(w, h)| w / h)
.sum();
let height_to_scale_to = f64::min(area_width / total_width_normalized, area_height);
let total_width = total_width_normalized * height_to_scale_to;
context.set_source_rgba(1.0, 1.0, 1.0, 1.0);
context.translate(
(area_width - total_width) / 2.0,
(area_height - height_to_scale_to) / 2.0,
);
context.save().unwrap();
context.translate(
area.width() as f64 / 2.0 - w_left * scale_left,
area.height() as f64 / 2.0 - h_page / 2.0,
);
// Poppler sometimes crops white border, draw it manually
context.rectangle(0.0, 0.0, w_left * scale_left, h_page);
context.fill().unwrap();
context.scale(scale_left, scale_left);
page_left.render(context);
context.restore().unwrap();
context.translate(
area.width() as f64 / 2.0,
area.height() as f64 / 2.0 - h_page / 2.0,
);
// Poppler sometimes crops white border, draw it manually
context.rectangle(0.0, 0.0, w_right * scale_right, h_page);
context.fill().unwrap();
context.scale(scale_right, scale_right);
page_right.render(context);
for page in pages {
let (page_width, page_height) = page.size();
let scale = height_to_scale_to / page_height;
let scaled_width = page_width * scale;
let r = ui.drawing_context.paint();
match r {
Err(v) => println!("Error painting PDF: {v:?}"),
Ok(_v) => {}
// context.translate(total_width_of_rendered_pages, 0.0);
// Poppler sometimes crops white border, draw it manually
context.rectangle(0.0, 0.0, scaled_width, height_to_scale_to);
context.fill().unwrap();
context.scale(scale, scale);
page.render(context);
context.restore().unwrap();
context.translate(scaled_width, 0.0);
context.save().unwrap();
}
ui.drawing_context.show_page().unwrap();
}
fn draw_single_page(ui: &Ui, area: &DrawingArea, context: &Context) {
if ui.document_canvas.is_none() {
return;
}
let document_canvas = ui.document_canvas.as_ref().unwrap();
if document_canvas.left_page.is_none() {
// TODO: show error message
return;
}
let page = document_canvas.left_page.as_ref().unwrap();
let (w, h) = page.size();
let width_diff = area.width() as f64 / w;
let height_diff = area.height() as f64 / h;
if width_diff > height_diff {
context.translate(
(area.width() as f64 - w * height_diff) / 2.0,
(area.height() as f64 - h * height_diff) / 2.0,
);
context.scale(height_diff, height_diff);
} else {
context.translate(
(area.width() as f64 - w * width_diff) / 2.0,
(area.height() as f64 - h * width_diff) / 2.0,
);
context.scale(width_diff, width_diff);
}
// Poppler sometimes crops white border, draw it manually
context.set_source_rgba(1.0, 1.0, 1.0, 1.0);
context.rectangle(0.0, 0.0, w, h);
context.fill().unwrap();
page.render(context);
let r = ui.drawing_context.paint();
match r {
Err(v) => println!("Error painting PDF: {v:?}"),
Ok(_v) => {}
}
ui.drawing_context.show_page().unwrap();
}

View File

@@ -1,7 +1,6 @@
use std::{cell::RefCell, path::Path, rc::Rc};
use async_channel::Sender;
use cairo::{Context, Format, ImageSurface};
use gtk::{
glib, Application, ApplicationWindow, Box, Button, DrawingArea, FileChooserAction,
FileChooserDialog, HeaderBar, Label, Orientation, ResponseType,
@@ -20,7 +19,6 @@ pub struct Ui {
header_bar: gtk::HeaderBar,
page_indicator: gtk::Label,
drawing_area: gtk::DrawingArea,
pub drawing_context: cairo::Context,
pub document_canvas: Option<DocumentCanvas>,
}
@@ -162,11 +160,6 @@ fn process_left_click(ui: &mut Ui, x: f64, y: f64) {
update_page_status(ui);
}
fn create_drawing_context() -> Context {
let surface = ImageSurface::create(Format::Rgb24, 0, 0).unwrap();
Context::new(&surface).unwrap()
}
impl Ui {
pub fn build(app: &Application) -> Rc<RefCell<Ui>> {
println!("building ui");
@@ -191,7 +184,6 @@ impl Ui {
.hexpand(true)
.vexpand(true)
.build(),
drawing_context: create_drawing_context(),
document_canvas: None,
};
let ui = Rc::new(RefCell::new(ui));
@@ -217,8 +209,8 @@ impl Ui {
ui.borrow().drawing_area.add_controller(click_right);
ui.borrow().drawing_area.set_draw_func(
glib::clone!(@weak ui => move |area, context, _, _| {
draw::draw(&mut ui.borrow_mut(), area, context);
glib::clone!(@weak ui => move |_area, context, w, h| {
draw::draw(&ui.borrow().document_canvas, context, w, h);
}),
);