summaryrefslogtreecommitdiff
path: root/fpdfsdk/fpdfview.cpp
diff options
context:
space:
mode:
authorrbpotter <rbpotter@chromium.org>2017-04-17 09:10:21 -0700
committerChromium commit bot <commit-bot@chromium.org>2017-04-18 00:26:02 +0000
commitcec01808e8b155f598b2a9ea527736ce774e76a2 (patch)
tree9614ce31345bb73e9d7c1a438c10bb83776599c3 /fpdfsdk/fpdfview.cpp
parent080dfd89269a95713d31d66829016d4885afe76b (diff)
downloadpdfium-cec01808e8b155f598b2a9ea527736ce774e76a2.tar.xz
Render image masks individually instead of rendering the entire page as
a bitmap in order to reduce spool sizes. BUG=682156, 674771 Change-Id: Ic743ba729a7b9609be18fb620a5eb6bc440aed6a Reviewed-on: https://pdfium-review.googlesource.com/2916 Commit-Queue: Lei Zhang <thestig@chromium.org> Reviewed-by: Lei Zhang <thestig@chromium.org>
Diffstat (limited to 'fpdfsdk/fpdfview.cpp')
-rw-r--r--fpdfsdk/fpdfview.cpp207
1 files changed, 198 insertions, 9 deletions
diff --git a/fpdfsdk/fpdfview.cpp b/fpdfsdk/fpdfview.cpp
index 5010c17ceb..6f32473985 100644
--- a/fpdfsdk/fpdfview.cpp
+++ b/fpdfsdk/fpdfview.cpp
@@ -8,10 +8,14 @@
#include <memory>
#include <utility>
+#include <vector>
#include "core/fpdfapi/cpdf_modulemgr.h"
#include "core/fpdfapi/cpdf_pagerendercontext.h"
+#include "core/fpdfapi/page/cpdf_image.h"
+#include "core/fpdfapi/page/cpdf_imageobject.h"
#include "core/fpdfapi/page/cpdf_page.h"
+#include "core/fpdfapi/page/cpdf_pageobject.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
@@ -687,6 +691,146 @@ DLLEXPORT double STDCALL FPDF_GetPageHeight(FPDF_PAGE page) {
}
#if defined(_WIN32)
+namespace {
+
+const double kEpsilonSize = 0.01f;
+
+void GetScaling(CPDF_Page* pPage,
+ int size_x,
+ int size_y,
+ int rotate,
+ double* scale_x,
+ double* scale_y) {
+ ASSERT(pPage);
+ ASSERT(scale_x);
+ ASSERT(scale_y);
+ double page_width = pPage->GetPageWidth();
+ double page_height = pPage->GetPageHeight();
+ if (page_width < kEpsilonSize || page_height < kEpsilonSize)
+ return;
+
+ if (rotate % 2 == 0) {
+ *scale_x = size_x / page_width;
+ *scale_y = size_y / page_height;
+ } else {
+ *scale_x = size_y / page_width;
+ *scale_y = size_x / page_height;
+ }
+}
+
+FX_RECT GetMaskDimensionsAndOffsets(CPDF_Page* pPage,
+ int start_x,
+ int start_y,
+ int size_x,
+ int size_y,
+ int rotate,
+ const CFX_FloatRect& mask_box) {
+ double scale_x = 0.0f;
+ double scale_y = 0.0f;
+ GetScaling(pPage, size_x, size_y, rotate, &scale_x, &scale_y);
+ if (scale_x < kEpsilonSize || scale_y < kEpsilonSize)
+ return FX_RECT();
+
+ // Compute sizes in page points. Round down to catch the entire bitmap.
+ int start_x_bm = static_cast<int>(mask_box.left * scale_x);
+ int start_y_bm = static_cast<int>(mask_box.bottom * scale_y);
+ int size_x_bm = static_cast<int>(mask_box.right * scale_x + 1.0f) -
+ static_cast<int>(mask_box.left * scale_x);
+ int size_y_bm = static_cast<int>(mask_box.top * scale_y + 1.0f) -
+ static_cast<int>(mask_box.bottom * scale_y);
+
+ // Get page rotation
+ int page_rotation = 0;
+ CPDF_Dictionary* pDict = pPage->m_pFormDict;
+ while (pDict) {
+ if (pDict->KeyExist("Rotate")) {
+ CPDF_Object* pRotateObj = pDict->GetObjectFor("Rotate")->GetDirect();
+ page_rotation = pRotateObj ? pRotateObj->GetInteger() / 90 : 0;
+ break;
+ }
+ if (!pDict->KeyExist("Parent"))
+ break;
+
+ pDict = ToDictionary(pDict->GetObjectFor("Parent")->GetDirect());
+ }
+
+ // Compute offsets
+ int offset_x = 0;
+ int offset_y = 0;
+ switch ((rotate + page_rotation) % 4) {
+ case 0:
+ offset_x = start_x_bm + start_x;
+ offset_y = start_y + size_y - size_y_bm - start_y_bm;
+ break;
+ case 1:
+ offset_x = start_y_bm + start_x;
+ offset_y = start_x_bm + start_y;
+ break;
+ case 2:
+ offset_x = start_x + size_x - size_x_bm - start_x_bm;
+ offset_y = start_y_bm + start_y;
+ break;
+ case 3:
+ offset_x = start_x + size_x - size_x_bm - start_y_bm;
+ offset_y = start_y + size_y - size_y_bm - start_x_bm;
+ break;
+ }
+ return FX_RECT(offset_x, offset_y, offset_x + size_x_bm,
+ offset_y + size_y_bm);
+}
+
+// Get a bitmap of just the mask section defined by |mask_box| from a full page
+// bitmap |pBitmap|.
+CFX_RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage,
+ int start_x,
+ int start_y,
+ int size_x,
+ int size_y,
+ int rotate,
+ CFX_RetainPtr<CFX_DIBitmap>& pSrc,
+ const CFX_FloatRect& mask_box,
+ FX_RECT* bitmap_area) {
+ ASSERT(bitmap_area);
+ *bitmap_area = GetMaskDimensionsAndOffsets(pPage, start_x, start_y, size_x,
+ size_y, rotate, mask_box);
+ if (bitmap_area->IsEmpty())
+ return nullptr;
+
+ // Create a new bitmap to transfer part of the page bitmap to.
+ CFX_RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
+ pDst->Create(bitmap_area->Width(), bitmap_area->Height(), FXDIB_Argb);
+ pDst->Clear(0x00ffffff);
+ pDst->TransferBitmap(0, 0, bitmap_area->Width(), bitmap_area->Height(), pSrc,
+ bitmap_area->left, bitmap_area->top);
+ return pDst;
+}
+
+void RenderBitmap(CFX_RenderDevice* device,
+ const CFX_RetainPtr<CFX_DIBitmap>& pSrc,
+ const FX_RECT& mask_area) {
+ int size_x_bm = mask_area.Width();
+ int size_y_bm = mask_area.Height();
+ if (size_x_bm == 0 || size_y_bm == 0)
+ return;
+
+ // Create a new bitmap from the old one
+ CFX_RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
+ pDst->Create(size_x_bm, size_y_bm, FXDIB_Rgb32);
+ pDst->Clear(0xffffffff);
+ pDst->CompositeBitmap(0, 0, size_x_bm, size_y_bm, pSrc, 0, 0,
+ FXDIB_BLEND_NORMAL, nullptr, false);
+
+ if (device->GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
+ device->StretchDIBitsWithFlagsAndBlend(pDst, mask_area.left, mask_area.top,
+ size_x_bm, size_y_bm, 0,
+ FXDIB_BLEND_NORMAL);
+ } else {
+ device->SetDIBits(pDst, mask_area.left, mask_area.top);
+ }
+}
+
+} // namespace
+
DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
FPDF_PAGE page,
int start_x,
@@ -698,30 +842,74 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return;
-
- CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
- pPage->SetRenderContext(pdfium::WrapUnique(pContext));
+ pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
+ CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
CFX_RetainPtr<CFX_DIBitmap> pBitmap;
- // TODO: This results in unnecessary rasterization of some PDFs due to
- // HasImageMask() returning true. If any image on the page is a mask, the
- // entire page gets rasterized and the spool size gets huge.
+ // Don't render the full page to bitmap for a mask unless there are a lot
+ // of masks. Full page bitmaps result in large spool sizes, so they should
+ // only be used when necessary. For large numbers of masks, rendering each
+ // individually is inefficient and unlikely to significantly improve spool
+ // size.
const bool bNewBitmap =
- pPage->BackgroundAlphaNeeded() || pPage->HasImageMask();
- if (bNewBitmap) {
+ pPage->BackgroundAlphaNeeded() ||
+ (pPage->HasImageMask() && pPage->GetMaskBoundingBoxes().size() > 100);
+ const bool bHasMask = pPage->HasImageMask() && !bNewBitmap;
+ if (bNewBitmap || bHasMask) {
pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
pBitmap->Create(size_x, size_y, FXDIB_Argb);
pBitmap->Clear(0x00ffffff);
CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
pContext->m_pDevice = pdfium::WrapUnique(pDevice);
pDevice->Attach(pBitmap, false, nullptr, false);
+ if (bHasMask) {
+ pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
+ pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS;
+ }
} else {
pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
}
+
FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
rotate, flags, true, nullptr);
- if (bNewBitmap) {
+ if (bHasMask) {
+ // Finish rendering the page to bitmap and copy the correct segments
+ // of the page to individual image mask bitmaps.
+ const std::vector<CFX_FloatRect>& mask_boxes =
+ pPage->GetMaskBoundingBoxes();
+ std::vector<FX_RECT> bitmap_areas(mask_boxes.size());
+ std::vector<CFX_RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size());
+ for (size_t i = 0; i < mask_boxes.size(); i++) {
+ bitmaps[i] =
+ GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate,
+ pBitmap, mask_boxes[i], &bitmap_areas[i]);
+ pContext->m_pRenderer->Continue(nullptr);
+ }
+
+ // Reset rendering context
+ pPage->SetRenderContext(nullptr);
+
+ // Begin rendering to the printer. Add flag to indicate the renderer should
+ // pause after each image mask.
+ pPage->SetRenderContext(pdfium::MakeUnique<CPDF_PageRenderContext>());
+ pContext = pPage->GetRenderContext();
+ pContext->m_pDevice = pdfium::MakeUnique<CFX_WindowsDevice>(dc);
+ pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
+ pContext->m_pOptions->m_Flags |= RENDER_BREAKFORMASKS;
+ FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
+ rotate, flags, true, nullptr);
+
+ // Render masks
+ for (size_t i = 0; i < mask_boxes.size(); i++) {
+ // Render the bitmap for the mask and free the bitmap.
+ if (bitmaps[i]) { // will be null if mask has zero area
+ RenderBitmap(pContext->m_pDevice.get(), bitmaps[i], bitmap_areas[i]);
+ }
+ // Render the next portion of page.
+ pContext->m_pRenderer->Continue(nullptr);
+ }
+ } else if (bNewBitmap) {
CFX_WindowsDevice WinDC(dc);
if (WinDC.GetDeviceCaps(FXDC_DEVICE_CLASS) == FXDC_PRINTER) {
auto pDst = pdfium::MakeRetain<CFX_DIBitmap>();
@@ -735,6 +923,7 @@ DLLEXPORT void STDCALL FPDF_RenderPage(HDC dc,
WinDC.SetDIBits(pBitmap, 0, 0);
}
}
+
pPage->SetRenderContext(nullptr);
}
#endif // defined(_WIN32)