From 95b66f1511522912fce75b9908c10e990234e4b3 Mon Sep 17 00:00:00 2001 From: Benjamin Adler Date: Fri, 29 Oct 2010 01:17:19 +0200 Subject: [PATCH] Added -crop option to pdftops and pdftoppm, extended pageBox support in poppler-core. --- cpp/poppler-page.cpp | 4 +- poppler/OutputDev.h | 4 +- poppler/PDFDoc.cc | 12 ++-- poppler/PDFDoc.h | 6 +- poppler/PSOutputDev.cc | 10 ++-- poppler/PSOutputDev.h | 4 +- poppler/Page.cc | 99 ++++++++++++++++++++++++++++++--------- poppler/Page.h | 18 +++++-- poppler/SplashOutputDev.cc | 10 ---- qt4/src/poppler-page.cc | 12 ++-- qt4/src/poppler-ps-converter.cc | 2 +- test/perf-test.cc | 13 ++--- utils/HtmlOutputDev.h | 2 +- utils/pdfimages.cc | 2 +- utils/pdftoabw.cc | 2 +- utils/pdftohtml.cc | 6 +- utils/pdftoppm.cc | 39 ++++++++++++++-- utils/pdftops.cc | 31 +++++++++++- utils/pdftotext.cc | 6 +- 19 files changed, 196 insertions(+), 86 deletions(-) diff --git a/cpp/poppler-page.cpp b/cpp/poppler-page.cpp index 4e2f730..52dad82 100644 --- a/cpp/poppler-page.cpp +++ b/cpp/poppler-page.cpp @@ -209,7 +209,7 @@ bool page::search(const ustring &text, rectf &r, search_direction_enum direction double rect_bottom = r.bottom(); TextOutputDev td(NULL, gTrue, gFalse, gFalse); - d->doc->doc->displayPage(&td, d->index + 1, 72, 72, rotation_value, false, true, false); + d->doc->doc->displayPage(&td, d->index + 1, 72, 72, rotation_value, Page::CropBox, false); TextPage *text_page = td.takeText(); switch (direction) { @@ -268,7 +268,7 @@ ustring page::text(const rectf &r, text_layout_enum layout_mode) const std::auto_ptr s; const GBool use_raw_order = (layout_mode == raw_order_layout); TextOutputDev td(0, gFalse, use_raw_order, gFalse); - d->doc->doc->displayPage(&td, d->index + 1, 72, 72, 0, false, true, false); + d->doc->doc->displayPage(&td, d->index + 1, 72, 72, 0, Page::CropBox, false); if (r.is_empty()) { const PDFRectangle *rect = d->page->getCropBox(); s.reset(td.getText(rect->x1, rect->y1, rect->x2, rect->y2)); diff --git a/poppler/OutputDev.h b/poppler/OutputDev.h index cdc74cf..867bafd 100644 --- a/poppler/OutputDev.h +++ b/poppler/OutputDev.h @@ -37,6 +37,7 @@ #include "goo/gtypes.h" #include "CharTypes.h" #include "Object.h" +#include "Page.h" class Dict; class GooHash; @@ -52,7 +53,6 @@ class Stream; class Links; class Link; class Catalog; -class Page; class Function; //------------------------------------------------------------------------ @@ -122,7 +122,7 @@ public: // OutputDev will use some alternate means to display the page // before returning false. virtual GBool checkPageSlice(Page *page, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, Page::PageBox cropBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog * catalog, GBool (* abortCheckCbk)(void *data) = NULL, diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 8155250..6ae0855 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -376,7 +376,7 @@ GBool PDFDoc::checkEncryption(GooString *ownerPassword, GooString *userPassword) void PDFDoc::displayPage(OutputDev *out, int page, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + Page::PageBox cropBox, GBool printing, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), @@ -386,14 +386,14 @@ void PDFDoc::displayPage(OutputDev *out, int page, } if (catalog->getPage(page)) catalog->getPage(page)->display(out, hDPI, vDPI, - rotate, useMediaBox, crop, printing, catalog, + rotate, cropBox, printing, catalog, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + Page::PageBox cropBox, GBool printing, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), @@ -401,7 +401,7 @@ void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage, int page; for (page = firstPage; page <= lastPage; ++page) { - displayPage(out, page, hDPI, vDPI, rotate, useMediaBox, crop, printing, + displayPage(out, page, hDPI, vDPI, rotate, cropBox, printing, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } @@ -409,7 +409,7 @@ void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage, void PDFDoc::displayPageSlice(OutputDev *out, int page, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + Page::PageBox pageBox, GBool printing, int sliceX, int sliceY, int sliceW, int sliceH, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, @@ -417,7 +417,7 @@ void PDFDoc::displayPageSlice(OutputDev *out, int page, void *annotDisplayDecideCbkData) { if (catalog->getPage(page)) catalog->getPage(page)->displaySlice(out, hDPI, vDPI, - rotate, useMediaBox, crop, + rotate, pageBox, sliceX, sliceY, sliceW, sliceH, printing, catalog, abortCheckCbk, abortCheckCbkData, diff --git a/poppler/PDFDoc.h b/poppler/PDFDoc.h index 8fa2dcf..e4c807f 100644 --- a/poppler/PDFDoc.h +++ b/poppler/PDFDoc.h @@ -127,7 +127,7 @@ public: // Display a page. void displayPage(OutputDev *out, int page, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, GBool printing, GBool (*abortCheckCbk)(void *data) = NULL, void *abortCheckCbkData = NULL, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL, @@ -136,7 +136,7 @@ public: // Display a range of pages. void displayPages(OutputDev *out, int firstPage, int lastPage, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, GBool printing, GBool (*abortCheckCbk)(void *data) = NULL, void *abortCheckCbkData = NULL, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data) = NULL, @@ -145,7 +145,7 @@ public: // Display part of a page. void displayPageSlice(OutputDev *out, int page, double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool crop, GBool printing, + /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, GBool printing, int sliceX, int sliceY, int sliceW, int sliceH, GBool (*abortCheckCbk)(void *data) = NULL, void *abortCheckCbkData = NULL, diff --git a/poppler/PSOutputDev.cc b/poppler/PSOutputDev.cc index f7e4b8c..ae0c518 100644 --- a/poppler/PSOutputDev.cc +++ b/poppler/PSOutputDev.cc @@ -2941,7 +2941,7 @@ void PSOutputDev::setupForm(Ref id, Object *strObj) { } GBool PSOutputDev::checkPageSlice(Page *page, double /*hDPI*/, double /*vDPI*/, - int rotateA, GBool useMediaBox, GBool crop, + int rotateA, /*GBool useMediaBox, GBool crop*/ Page::PageBox cropBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, @@ -2964,7 +2964,7 @@ GBool PSOutputDev::checkPageSlice(Page *page, double /*hDPI*/, double /*vDPI*/, if (!forceRasterize) { scan = new PreScanOutputDev(); - page->displaySlice(scan, 72, 72, rotateA, useMediaBox, crop, + page->displaySlice(scan, 72, 72, rotateA, cropBox, sliceX, sliceY, sliceW, sliceH, printing, catalog, abortCheckCbk, abortCheckCbkData); rasterize = scan->usesTransparency(); @@ -2998,13 +2998,13 @@ GBool PSOutputDev::checkPageSlice(Page *page, double /*hDPI*/, double /*vDPI*/, } splashOut->startDoc(xref); page->displaySlice(splashOut, splashDPI, splashDPI, rotateA, - useMediaBox, crop, + cropBox, sliceX, sliceY, sliceW, sliceH, printing, catalog, abortCheckCbk, abortCheckCbkData); // start the PS page - page->makeBox(splashDPI, splashDPI, rotateA, useMediaBox, gFalse, - sliceX, sliceY, sliceW, sliceH, &box, &crop); + page->makeBox(splashDPI, splashDPI, rotateA, cropBox, gFalse, + sliceX, sliceY, sliceW, sliceH, &box/*, &crop*/); rotateA += page->getRotate(); if (rotateA >= 360) { rotateA -= 360; diff --git a/poppler/PSOutputDev.h b/poppler/PSOutputDev.h index 2ac7b13..955f6b7 100644 --- a/poppler/PSOutputDev.h +++ b/poppler/PSOutputDev.h @@ -161,7 +161,9 @@ public: // OutputDev will use some alternate means to display the page // before returning false. virtual GBool checkPageSlice(Page *page, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, + //GBool useMediaBox, GBool crop, + Page::PageBox cropBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data) = NULL, diff --git a/poppler/Page.cc b/poppler/Page.cc index caf233f..0a040be 100644 --- a/poppler/Page.cc +++ b/poppler/Page.cc @@ -400,26 +400,29 @@ Links *Page::getLinks(Catalog *catalog) { } void Page::display(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, Page::PageBox pageBox, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData) { - displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop, -1, -1, -1, -1, printing, catalog, + displaySlice(out, hDPI, vDPI, rotate, pageBox, -1, -1, -1, -1, printing, catalog, abortCheckCbk, abortCheckCbkData, annotDisplayDecideCbk, annotDisplayDecideCbkData); } Gfx *Page::createGfx(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, + // GBool useMediaBox, GBool crop, + Page::PageBox pageBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data), void *abortCheckCbkData, GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data), void *annotDisplayDecideCbkData) { - PDFRectangle *mediaBox, *cropBox; + + // A PDFRectangle matching the pageBox that we want to crop to. PDFRectangle box; Gfx *gfx; @@ -430,28 +433,50 @@ Gfx *Page::createGfx(OutputDev *out, double hDPI, double vDPI, rotate += 360; } - makeBox(hDPI, vDPI, rotate, useMediaBox, out->upsideDown(), - sliceX, sliceY, sliceW, sliceH, &box, &crop); - cropBox = getCropBox(); - mediaBox = getMediaBox(); + PDFRectangle *cropBox = getCropBox(); + PDFRectangle *mediaBox = getMediaBox(); + PDFRectangle *bleedBox = getBleedBox(); + PDFRectangle *trimBox = getTrimBox(); + PDFRectangle *artBox = getArtBox(); + + makeBox(hDPI, vDPI, rotate, pageBox, out->upsideDown(), sliceX, sliceY, sliceW, sliceH, &box); if (globalParams->getPrintCommands()) { printf("***** MediaBox = ll:%g,%g ur:%g,%g\n", mediaBox->x1, mediaBox->y1, mediaBox->x2, mediaBox->y2); printf("***** CropBox = ll:%g,%g ur:%g,%g\n", cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2); + printf("***** BleedBox = ll:%g,%g ur:%g,%g\n", + bleedBox->x1, bleedBox->y1, bleedBox->x2, bleedBox->y2); + printf("***** TrimBox = ll:%g,%g ur:%g,%g\n", + trimBox->x1, trimBox->y1, trimBox->x2, trimBox->y2); + printf("***** ArtBox = ll:%g,%g ur:%g,%g\n", + artBox->x1, artBox->y1, artBox->x2, artBox->y2); printf("***** Rotate = %d\n", attrs->getRotate()); } + PDFRectangle *pdfRectangleToUseForCropping = NULL; + if (pageBox == Page::MediaBox) { + pdfRectangleToUseForCropping = NULL; + } else if (pageBox == Page::CropBox) { + pdfRectangleToUseForCropping = cropBox; + } else if (pageBox == Page::BleedBox) { + pdfRectangleToUseForCropping = bleedBox; + } else if (pageBox == Page::TrimBox) { + pdfRectangleToUseForCropping = trimBox; + } else if (pageBox == Page::ArtBox) { + pdfRectangleToUseForCropping = artBox; + } + gfx = new Gfx(xref, out, num, attrs->getResourceDict(), catalog, - hDPI, vDPI, &box, crop ? cropBox : (PDFRectangle *)NULL, + hDPI, vDPI, &box, pdfRectangleToUseForCropping, rotate, abortCheckCbk, abortCheckCbkData); return gfx; } void Page::displaySlice(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, Page::PageBox pageBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data), @@ -463,14 +488,14 @@ void Page::displaySlice(OutputDev *out, double hDPI, double vDPI, Annots *annotList; int i; - if (!out->checkPageSlice(this, hDPI, vDPI, rotate, useMediaBox, crop, + if (!out->checkPageSlice(this, hDPI, vDPI, rotate, pageBox, sliceX, sliceY, sliceW, sliceH, printing, catalog, abortCheckCbk, abortCheckCbkData)) { return; } - gfx = createGfx(out, hDPI, vDPI, rotate, useMediaBox, crop, + gfx = createGfx(out, hDPI, vDPI, rotate, pageBox, sliceX, sliceY, sliceW, sliceH, printing, catalog, abortCheckCbk, abortCheckCbkData, @@ -624,16 +649,14 @@ GBool Page::loadThumb(unsigned char **data_out, } void Page::makeBox(double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool upsideDown, + Page::PageBox pageBox, GBool upsideDown, double sliceX, double sliceY, double sliceW, double sliceH, - PDFRectangle *box, GBool *crop) { - PDFRectangle *mediaBox, *cropBox, *baseBox; + PDFRectangle *box) { double kx, ky; - mediaBox = getMediaBox(); - cropBox = getCropBox(); + PDFRectangle *baseBox = getPDFRectangleOfPageBox(pageBox); + if (sliceW >= 0 && sliceH >= 0) { - baseBox = useMediaBox ? mediaBox : cropBox; kx = 72.0 / hDPI; ky = 72.0 / vDPI; if (rotate == 90) { @@ -677,11 +700,43 @@ void Page::makeBox(double hDPI, double vDPI, int rotate, box->y2 = baseBox->y1 + ky * (sliceY + sliceH); } } - } else if (useMediaBox) { - *box = *mediaBox; } else { - *box = *cropBox; - *crop = gFalse; + *box = *baseBox; + } +} + +Page::PageBox Page::getPageBoxFromString(const char *pageBoxName, GBool* success) +{ + *success = true; + + // Set up pageBox as desired by user, possible values are in Page::pageBox enum. + if (!strcasecmp(pageBoxName, "MediaBox")) + return Page::MediaBox; + if (!strcasecmp(pageBoxName, "CropBox")) + return Page::CropBox; + if (!strcasecmp(pageBoxName, "BleedBox")) + return Page::BleedBox; + if (!strcasecmp(pageBoxName, "TrimBox")) + return Page::TrimBox; + if (!strcasecmp(pageBoxName, "ArtBox")) + return Page::ArtBox; + + *success = false; + return Page::MediaBox; +} + +PDFRectangle* Page::getPDFRectangleOfPageBox(const Page::PageBox pageBox) +{ + if (pageBox == Page::MediaBox) { + return getMediaBox(); + } else if (pageBox == Page::CropBox) { + return getCropBox(); + } else if (pageBox == Page::BleedBox) { + return getBleedBox(); + } else if (pageBox == Page::TrimBox) { + return getTrimBox(); + } else { + return getArtBox(); } } diff --git a/poppler/Page.h b/poppler/Page.h index 9d892b1..3247723 100644 --- a/poppler/Page.h +++ b/poppler/Page.h @@ -134,6 +134,14 @@ public: // Is page valid? GBool isOk() { return ok; } + // Available page-boxes, used for cropping. + enum PageBox { MediaBox, CropBox, BleedBox, TrimBox, ArtBox }; + + // Convert a string (MediaBox,CropBox,BleedBox,...) to the corresponding enum-value + static Page::PageBox getPageBoxFromString(const char *pageBoxName, GBool* success); + + PDFRectangle* getPDFRectangleOfPageBox(const Page::PageBox pageBox); + // Get page parameters. int getNum() { return num; } PDFRectangle *getMediaBox() { return attrs->getMediaBox(); } @@ -194,7 +202,7 @@ public: Object *getActions(Object *obj) { return actions.fetch(xref, obj); } Gfx *createGfx(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data), @@ -204,7 +212,7 @@ public: // Display a page. void display(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data) = NULL, void *abortCheckCbkData = NULL, @@ -213,7 +221,7 @@ public: // Display part of a page. void displaySlice(OutputDev *out, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, /*GBool useMediaBox, GBool crop*/Page::PageBox cropBox, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog *catalog, GBool (*abortCheckCbk)(void *data) = NULL, @@ -224,9 +232,9 @@ public: void display(Gfx *gfx); void makeBox(double hDPI, double vDPI, int rotate, - GBool useMediaBox, GBool upsideDown, + Page::PageBox cropBox, GBool upsideDown, double sliceX, double sliceY, double sliceW, double sliceH, - PDFRectangle *box, GBool *crop); + PDFRectangle *box); void processLinks(OutputDev *out, Catalog *catalog); diff --git a/qt4/src/poppler-page.cc b/qt4/src/poppler-page.cc index 293d09b..57bde7e 100644 --- a/qt4/src/poppler-page.cc +++ b/qt4/src/poppler-page.cc @@ -34,6 +34,7 @@ #include #include +#include #include #include #include @@ -212,7 +213,7 @@ QImage Page::renderToImage(double xres, double yres, int x, int y, int w, int h, SplashOutputDev *splash_output = static_cast(m_page->parentDoc->getOutputDev()); m_page->parentDoc->doc->displayPageSlice(splash_output, m_page->index + 1, xres, yres, - rotation, false, true, false, x, y, w, h); + rotation, ::Page::CropBox/*false, true*/, false, x, y, w, h); SplashBitmap *bitmap = splash_output->getBitmap(); int bw = bitmap->getWidth(); @@ -262,8 +263,7 @@ QImage Page::renderToImage(double xres, double yres, int x, int y, int w, int h, xres, yres, rotation, - false, - true, + ::Page::CropBox, false, x, y, @@ -306,7 +306,7 @@ QString Page::text(const QRectF &r, TextLayout textLayout) const const GBool rawOrder = textLayout == RawOrderLayout; output_dev = new TextOutputDev(0, gFalse, rawOrder, gFalse); m_page->parentDoc->doc->displayPageSlice(output_dev, m_page->index + 1, 72, 72, - 0, false, true, false, -1, -1, -1, -1); + 0, ::Page::CropBox, false, -1, -1, -1, -1); if (r.isNull()) { rect = m_page->page->getCropBox(); @@ -346,7 +346,7 @@ bool Page::search(const QString &text, double &sLeft, double &sTop, double &sRig // fetch ourselves a textpage TextOutputDev td(NULL, gTrue, gFalse, gFalse); - m_page->parentDoc->doc->displayPage( &td, m_page->index + 1, 72, 72, rotation, false, true, false ); + m_page->parentDoc->doc->displayPage( &td, m_page->index + 1, 72, 72, rotation, ::Page::CropBox, false ); TextPage *textPage=td.takeText(); if (direction == FromTop) @@ -393,7 +393,7 @@ QList Page::textList(Rotation rotate) const int rotation = (int)rotate * 90; m_page->parentDoc->doc->displayPageSlice(output_dev, m_page->index + 1, 72, 72, - rotation, false, false, false, -1, -1, -1, -1); + rotation, ::Page::MediaBox, false, -1, -1, -1, -1); TextWordList *word_list = output_dev->makeWordList(); diff --git a/qt4/src/poppler-ps-converter.cc b/qt4/src/poppler-ps-converter.cc index f39673d..94dd4ae 100644 --- a/qt4/src/poppler-ps-converter.cc +++ b/qt4/src/poppler-ps-converter.cc @@ -231,7 +231,7 @@ bool PSConverter::convert() GBool isPrinting = (d->opts & Printing) ? gTrue : gFalse; foreach(int page, d->pageList) { - d->document->doc->displayPage(psOut, page, d->hDPI, d->vDPI, d->rotate, gFalse, gTrue, isPrinting); + d->document->doc->displayPage(psOut, page, d->hDPI, d->vDPI, d->rotate, ::Page::CropBox, isPrinting); if (d->pageConvertedCallback) (*d->pageConvertedCallback)(page, d->pageConvertedPayload); } diff --git a/test/perf-test.cc b/test/perf-test.cc index 6d6961e..e2459d4 100644 --- a/test/perf-test.cc +++ b/test/perf-test.cc @@ -443,11 +443,10 @@ SplashBitmap *PdfEnginePoppler::renderBitmap(int pageNo, double zoomReal, int ro double hDPI = (double)PDF_FILE_DPI * zoomReal * 0.01; double vDPI = (double)PDF_FILE_DPI * zoomReal * 0.01; - GBool useMediaBox = gFalse; - GBool crop = gTrue; +// GBool useMediaBox = gFalse; +// GBool crop = gTrue; GBool doLinks = gTrue; - _pdfDoc->displayPage(_outputDev, pageNo, hDPI, vDPI, rotation, useMediaBox, - crop, doLinks, NULL, NULL); + _pdfDoc->displayPage(_outputDev, pageNo, hDPI, vDPI, rotation, Page::CropBox, doLinks, NULL, NULL); SplashBitmap* bmp = _outputDev->takeBitmap(); return bmp; @@ -871,10 +870,10 @@ static void RenderPdfAsText(const char *fileName) msTimer.start(); int rotate = 0; - GBool useMediaBox = gFalse; - GBool crop = gTrue; +// GBool useMediaBox = gFalse; +// GBool crop = gTrue; GBool doLinks = gFalse; - pdfDoc->displayPage(textOut, curPage, 72, 72, rotate, useMediaBox, crop, doLinks); + pdfDoc->displayPage(textOut, curPage, 72, 72, rotate, Page::CropBox, doLinks); txt = textOut->getText(0.0, 0.0, 10000.0, 10000.0); msTimer.stop(); timeInMs = msTimer.getElapsed(); diff --git a/utils/HtmlOutputDev.h b/utils/HtmlOutputDev.h index 24ccfd1..55cc7b4 100644 --- a/utils/HtmlOutputDev.h +++ b/utils/HtmlOutputDev.h @@ -250,7 +250,7 @@ public: //----- initialization and control virtual GBool checkPageSlice(Page *page, double hDPI, double vDPI, - int rotate, GBool useMediaBox, GBool crop, + int rotate, Page::PageBox cropBox/*GBool useMediaBox, GBool crop*/, int sliceX, int sliceY, int sliceW, int sliceH, GBool printing, Catalog * catalogA, GBool (* abortCheckCbk)(void *data) = NULL, diff --git a/utils/pdfimages.cc b/utils/pdfimages.cc index ffa7991..7fba863 100644 --- a/utils/pdfimages.cc +++ b/utils/pdfimages.cc @@ -160,7 +160,7 @@ int main(int argc, char *argv[]) { imgOut = new ImageOutputDev(imgRoot, dumpJPEG); if (imgOut->isOk()) { doc->displayPages(imgOut, firstPage, lastPage, 72, 72, 0, - gTrue, gFalse, gFalse); + Page::MediaBox, gFalse); } delete imgOut; diff --git a/utils/pdftoabw.cc b/utils/pdftoabw.cc index 5a53281..70c52ee 100644 --- a/utils/pdftoabw.cc +++ b/utils/pdftoabw.cc @@ -173,7 +173,7 @@ int main(int argc, char *argv[]) { if (abwOut->isOk()) { - doc->displayPages(abwOut, firstPage, lastPage, 72, 72, 0, gTrue, gFalse, gFalse); + doc->displayPages(abwOut, firstPage, lastPage, 72, 72, 0, Page::MediaBox, gFalse); abwOut->createABW(); } diff --git a/utils/pdftohtml.cc b/utils/pdftohtml.cc index 5323b6e..528aa5f 100644 --- a/utils/pdftohtml.cc +++ b/utils/pdftohtml.cc @@ -402,7 +402,7 @@ int main(int argc, char *argv[]) { if (htmlOut->isOk()) { doc->displayPages(htmlOut, firstPage, lastPage, 72, 72, 0, - gTrue, gFalse, gFalse); + Page::MediaBox, gFalse); if (!xml) { htmlOut->dumpDocOutline(doc->getCatalog()); @@ -434,7 +434,7 @@ int main(int argc, char *argv[]) { doc->displayPage(splashOut, pg, resolution, resolution, - 0, gTrue, gFalse, gFalse); + 0, Page::MediaBox, gFalse); SplashBitmap *bitmap = splashOut->getBitmap(); imgFileName = GooString::format("{0:s}{1:03d}.{2:s}", @@ -461,7 +461,7 @@ int main(int argc, char *argv[]) { doc->getCatalog(), NULL, firstPage, lastPage, psModePS, w, h); psOut->setDisplayText(gFalse); doc->displayPages(psOut, firstPage, lastPage, 72, 72, 0, - gTrue, gFalse, gFalse); + Page::MediaBox, gFalse); delete psOut; /*sprintf(buf, "%s -sDEVICE=png16m -dBATCH -dNOPROMPT -dNOPAUSE -r%d -sOutputFile=%s%%03d.png -g%dx%d -q %s", GHOSTSCRIPT, resolution, htmlFileName->getCString(), w, h, diff --git a/utils/pdftoppm.cc b/utils/pdftoppm.cc index c4cc43b..ad0665b 100644 --- a/utils/pdftoppm.cc +++ b/utils/pdftoppm.cc @@ -65,6 +65,7 @@ static int w = 0; static int h = 0; static int sz = 0; static GBool useCropBox = gFalse; +static char pageBoxString[33] = "\001"; static GBool mono = gFalse; static GBool gray = gFalse; static GBool png = gFalse; @@ -112,7 +113,9 @@ static const ArgDesc argDesc[] = { {"-sz", argInt, &sz, 0, "size of crop square in pixels (sets W and H)"}, {"-cropbox",argFlag, &useCropBox, 0, - "use the crop box rather than media box"}, + "use the crop box rather than media box. Deprecated, use -crop"}, + {"-crop", argString, pageBoxString, sizeof(pageBoxString), + "Crop using MediaBox(default)/CropBox/BleedBox/TrimBox/ArtBox"}, {"-mono", argFlag, &mono, 0, "generate a monochrome PBM file"}, @@ -158,6 +161,7 @@ static const ArgDesc argDesc[] = { static void savePageSlice(PDFDoc *doc, SplashOutputDev *splashOut, + Page::PageBox pageBox, int pg, int x, int y, int w, int h, double pg_w, double pg_h, char *ppmFile) { @@ -165,10 +169,15 @@ static void savePageSlice(PDFDoc *doc, if (h == 0) h = (int)ceil(pg_h); w = (x+w > pg_w ? (int)ceil(pg_w-x) : w); h = (y+h > pg_h ? (int)ceil(pg_h-y) : h); + + // For backwards-compatibility with -cropbox + if(useCropBox) pageBox = Page::CropBox; + doc->displayPageSlice(splashOut, pg, x_resolution, y_resolution, 0, - !useCropBox, gFalse, gFalse, + pageBox, + gFalse, x, y, w, h ); @@ -222,6 +231,10 @@ int main(int argc, char *argv[]) { int pg, pg_num_len; double pg_w, pg_h, tmp; + // This holds the PageBox to be used for cropping, as selected by the user using + // the -crop parameter. MediaBox is the default. + Page::PageBox pageBox = Page::MediaBox; + exitCode = 99; // parse args @@ -307,6 +320,24 @@ int main(int argc, char *argv[]) { if (lastPage < 1 || lastPage > doc->getNumPages()) lastPage = doc->getNumPages(); + // Set up pageBox as desired by user. + if (pageBoxString[0] != '\001') { + + if(useCropBox) { + error(-1, "Please use either the old -nocrop option, or the new -crop option, not both."); + goto err1; + } + + GBool success; + + pageBox = Page::getPageBoxFromString(pageBoxString, &success); + + if(!success) { + error(-1, "Please specify a valid -crop option, \"%s\" not recognized.", pageBoxString); + goto err1; + } + } + // write PPM files paperColor[0] = 255; paperColor[1] = 255; @@ -351,9 +382,9 @@ int main(int argc, char *argv[]) { snprintf(ppmFile, PPM_FILE_SZ, "%.*s-%0*d.%s", PPM_FILE_SZ - 32, ppmRoot, pg_num_len, pg, png ? "png" : jpeg ? "jpg" : mono ? "pbm" : gray ? "pgm" : "ppm"); - savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, ppmFile); + savePageSlice(doc, splashOut, pageBox, pg, x, y, w, h, pg_w, pg_h, ppmFile); } else { - savePageSlice(doc, splashOut, pg, x, y, w, h, pg_w, pg_h, NULL); + savePageSlice(doc, splashOut, pageBox, pg, x, y, w, h, pg_w, pg_h, NULL); } } delete splashOut; diff --git a/utils/pdftops.cc b/utils/pdftops.cc index 0bc43a1..961b62c 100644 --- a/utils/pdftops.cc +++ b/utils/pdftops.cc @@ -94,6 +94,7 @@ static char paperSize[15] = ""; static int paperWidth = -1; static int paperHeight = -1; static GBool noCrop = gFalse; +static char pageBoxString[33] = "\001"; static GBool expand = gFalse; static GBool noShrink = gFalse; static GBool noCenter = gFalse; @@ -150,7 +151,9 @@ static const ArgDesc argDesc[] = { {"-paperh", argInt, &paperHeight, 0, "paper height, in points"}, {"-nocrop", argFlag, &noCrop, 0, - "don't crop pages to CropBox"}, + "don't crop pages to CropBox. Deprecated, use -crop"}, + {"-crop", argString, pageBoxString, sizeof(pageBoxString), + "Crop using MediaBox(default)/CropBox/BleedBox/TrimBox/ArtBox"}, {"-expand", argFlag, &expand, 0, "expand pages smaller than the paper size"}, {"-noshrink", argFlag, &noShrink, 0, @@ -185,6 +188,11 @@ int main(int argc, char *argv[]) { PSLevel level; PSOutMode mode; GooString *ownerPW, *userPW; + + // This holds the PageBox to be used for cropping, as selected by the user using + // the -crop parameter. MediaBox is the default. + Page::PageBox pageBox = Page::MediaBox; + PSOutputDev *psOut; GBool ok; char *p; @@ -358,6 +366,24 @@ int main(int argc, char *argv[]) { goto err2; } + // Set up pageBox as desired by user. + if (pageBoxString[0] != '\001') { + + if(noCrop) { + error(-1, "Please use either the old -nocrop option, or the new -crop option, not both."); + goto err2; + } + + GBool success; + + pageBox = Page::getPageBoxFromString(pageBoxString, &success); + + if(!success) { + error(-1, "Please specify a valid -crop option, \"%s\" not recognized.", pageBoxString); + goto err2; + } + } + // write PostScript file psOut = new PSOutputDev(psFileName->getCString(), doc->getXRef(), doc->getCatalog(), NULL, firstPage, lastPage, mode, @@ -365,8 +391,7 @@ int main(int argc, char *argv[]) { paperHeight, duplex); if (psOut->isOk()) { - doc->displayPages(psOut, firstPage, lastPage, 72, 72, - 0, noCrop, !noCrop, gTrue); + doc->displayPages(psOut, firstPage, lastPage, 72, 72, 0, pageBox, gTrue); } else { delete psOut; exitCode = 2; diff --git a/utils/pdftotext.cc b/utils/pdftotext.cc index b189e84..46238ec 100644 --- a/utils/pdftotext.cc +++ b/utils/pdftotext.cc @@ -339,7 +339,7 @@ int main(int argc, char *argv[]) { fprintf(f, "\n"); for (int page = firstPage; page <= lastPage; ++page) { fprintf(f, " \n",doc->getPageCropWidth(page), doc->getPageCropHeight(page)); - doc->displayPage(textOut, page, resolution, resolution, 0, gTrue, gFalse, gFalse); + doc->displayPage(textOut, page, resolution, resolution, 0, Page::MediaBox, gFalse); TextWordList *wordlist = textOut->makeWordList(); const int word_length = wordlist != NULL ? wordlist->getLength() : 0; TextWord *word; @@ -366,12 +366,12 @@ int main(int argc, char *argv[]) { if (textOut->isOk()) { if ((w==0) && (h==0) && (x==0) && (y==0)) { doc->displayPages(textOut, firstPage, lastPage, resolution, resolution, 0, - gTrue, gFalse, gFalse); + Page::MediaBox, gFalse); } else { for (int page = firstPage; page <= lastPage; ++page) { doc->displayPageSlice(textOut, page, resolution, resolution, 0, - gTrue, gFalse, gFalse, + Page::MediaBox, gFalse, x, y, w, h); } } -- 1.6.3.3