Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added auto-crop support for PDF sources. #131

Merged
merged 1 commit into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/pdf-image-source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,23 @@ bool PDFImageSource::LoadAndScale(const DisplayOptions &opts, int frame_offset,
const int max_display_page =
(frame_count < 0) ? page_count
: std::min(page_count, start_page + frame_count);
PopplerRectangle bounding_box;
for (int page_num = start_page; page_num < max_display_page; ++page_num) {
PopplerPage *const page = poppler_document_get_page(document, page_num);
if (page == nullptr) {
success = false;
break;
}

poppler_page_get_size(page, &orig_width_, &orig_height_);
bounding_box = PopplerRectangle{
.x1 = 0, .y1 = 0, .x2 = orig_width_, .y2 = orig_height_};
#if POPPLER_CHECK_VERSION(0, 88, 0)
if (opts.auto_crop) {
poppler_page_get_bounding_box(page, &bounding_box);
orig_width_ = bounding_box.x2 - bounding_box.x1;
orig_height_ = bounding_box.y2 - bounding_box.y1;
}
#endif
int target_width;
int target_height;
CalcScaleToFitDisplay(orig_width_, orig_height_, opts, false,
Expand All @@ -77,9 +86,11 @@ bool PDFImageSource::LoadAndScale(const DisplayOptions &opts, int frame_offset,
cairo_surface_t *surface = cairo_image_surface_create_for_data(
(uint8_t *)image->begin(), kCairoFormat, render_width,
render_height, stride);

cairo_t *cr = cairo_create(surface);
cairo_scale(cr, 1.0 * render_width / orig_width_,
1.0 * render_height / orig_height_);
cairo_translate(cr, -bounding_box.x1, -bounding_box.y1);
cairo_save(cr);

// Fill background with page color.
Expand Down
6 changes: 3 additions & 3 deletions src/timg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,11 @@ static int PrintVersion(FILE *stream) {
#ifdef WITH_TIMG_STB
fprintf(stream,
"STB image loading; STB resize v"
#ifdef STB_RESIZE_VERSION2
# ifdef STB_RESIZE_VERSION2
"2"
#else
# else
"1"
#endif
# endif
# ifdef WITH_TIMG_GRPAPHICSMAGICK
// If we have graphics magic, that will take images first,
// so STB will only really be called as fallback.
Expand Down