• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 
7 #include "core/fpdfapi/render/cpdf_scaledrenderbuffer.h"
8 
9 #include "core/fpdfapi/render/cpdf_devicebuffer.h"
10 #include "core/fpdfapi/render/cpdf_rendercontext.h"
11 #include "core/fxge/cfx_defaultrenderdevice.h"
12 #include "core/fxge/dib/cfx_dibitmap.h"
13 #include "third_party/base/ptr_util.h"
14 
15 namespace {
16 
17 constexpr size_t kImageSizeLimitBytes = 30 * 1024 * 1024;
18 
19 }  // namespace
20 
CPDF_ScaledRenderBuffer()21 CPDF_ScaledRenderBuffer::CPDF_ScaledRenderBuffer() {}
22 
~CPDF_ScaledRenderBuffer()23 CPDF_ScaledRenderBuffer::~CPDF_ScaledRenderBuffer() {}
24 
Initialize(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const FX_RECT & rect,const CPDF_PageObject * pObj,const CPDF_RenderOptions * pOptions,int max_dpi)25 bool CPDF_ScaledRenderBuffer::Initialize(CPDF_RenderContext* pContext,
26                                          CFX_RenderDevice* pDevice,
27                                          const FX_RECT& rect,
28                                          const CPDF_PageObject* pObj,
29                                          const CPDF_RenderOptions* pOptions,
30                                          int max_dpi) {
31   m_pDevice = pDevice;
32   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS)
33     return true;
34 
35   m_pContext = pContext;
36   m_Rect = rect;
37   m_pObject = pObj;
38   m_Matrix = CPDF_DeviceBuffer::CalculateMatrix(pDevice, rect, max_dpi,
39                                                 /*scale=*/true);
40   m_pBitmapDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
41   bool bIsAlpha =
42       !!(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_ALPHA_OUTPUT);
43   FXDIB_Format dibFormat = bIsAlpha ? FXDIB_Argb : FXDIB_Rgb;
44   while (1) {
45     FX_RECT bitmap_rect =
46         m_Matrix.TransformRect(CFX_FloatRect(rect)).GetOuterRect();
47     int32_t width = bitmap_rect.Width();
48     int32_t height = bitmap_rect.Height();
49     // Set to 0 to make CalculatePitchAndSize() calculate it.
50     uint32_t pitch = 0;
51     uint32_t size;
52     if (!CFX_DIBitmap::CalculatePitchAndSize(width, height, dibFormat, &pitch,
53                                              &size)) {
54       return false;
55     }
56 
57     if (size <= kImageSizeLimitBytes &&
58         m_pBitmapDevice->Create(width, height, dibFormat, nullptr)) {
59       break;
60     }
61     m_Matrix.Scale(0.5f, 0.5f);
62   }
63   m_pContext->GetBackground(m_pBitmapDevice->GetBitmap(), m_pObject.Get(),
64                             pOptions, m_Matrix);
65   return true;
66 }
67 
GetDevice() const68 CFX_RenderDevice* CPDF_ScaledRenderBuffer::GetDevice() const {
69   return m_pBitmapDevice ? m_pBitmapDevice.get() : m_pDevice.Get();
70 }
71 
OutputToDevice()72 void CPDF_ScaledRenderBuffer::OutputToDevice() {
73   if (m_pBitmapDevice) {
74     m_pDevice->StretchDIBits(m_pBitmapDevice->GetBitmap(), m_Rect.left,
75                              m_Rect.top, m_Rect.Width(), m_Rect.Height());
76   }
77 }
78