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 
14 namespace {
15 
16 constexpr size_t kImageSizeLimitBytes = 30 * 1024 * 1024;
17 
18 }  // namespace
19 
20 CPDF_ScaledRenderBuffer::CPDF_ScaledRenderBuffer() = default;
21 
22 CPDF_ScaledRenderBuffer::~CPDF_ScaledRenderBuffer() = default;
23 
Initialize(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const FX_RECT & rect,const CPDF_PageObject * pObj,const CPDF_RenderOptions * pOptions,int max_dpi)24 bool CPDF_ScaledRenderBuffer::Initialize(CPDF_RenderContext* pContext,
25                                          CFX_RenderDevice* pDevice,
26                                          const FX_RECT& rect,
27                                          const CPDF_PageObject* pObj,
28                                          const CPDF_RenderOptions* pOptions,
29                                          int max_dpi) {
30   m_pDevice = pDevice;
31   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS)
32     return true;
33 
34   m_pContext = pContext;
35   m_Rect = rect;
36   m_pObject = pObj;
37   m_Matrix = CPDF_DeviceBuffer::CalculateMatrix(pDevice, rect, max_dpi,
38                                                 /*scale=*/true);
39   m_pBitmapDevice = std::make_unique<CFX_DefaultRenderDevice>();
40   bool bIsAlpha =
41       !!(m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_ALPHA_OUTPUT);
42   FXDIB_Format dibFormat = bIsAlpha ? FXDIB_Format::kArgb : FXDIB_Format::kRgb;
43   while (1) {
44     FX_RECT bitmap_rect =
45         m_Matrix.TransformRect(CFX_FloatRect(rect)).GetOuterRect();
46     int32_t width = bitmap_rect.Width();
47     int32_t height = bitmap_rect.Height();
48     // Set to 0 to make CalculatePitchAndSize() calculate it.
49     constexpr uint32_t kNoPitch = 0;
50     Optional<CFX_DIBitmap::PitchAndSize> pitch_size =
51         CFX_DIBitmap::CalculatePitchAndSize(width, height, dibFormat, kNoPitch);
52     if (!pitch_size.has_value())
53       return false;
54 
55     if (pitch_size.value().size <= kImageSizeLimitBytes &&
56         m_pBitmapDevice->Create(width, height, dibFormat, nullptr)) {
57       break;
58     }
59     m_Matrix.Scale(0.5f, 0.5f);
60   }
61   m_pContext->GetBackground(m_pBitmapDevice->GetBitmap(), m_pObject.Get(),
62                             pOptions, m_Matrix);
63   return true;
64 }
65 
GetDevice() const66 CFX_RenderDevice* CPDF_ScaledRenderBuffer::GetDevice() const {
67   return m_pBitmapDevice ? m_pBitmapDevice.get() : m_pDevice.Get();
68 }
69 
OutputToDevice()70 void CPDF_ScaledRenderBuffer::OutputToDevice() {
71   if (m_pBitmapDevice) {
72     m_pDevice->StretchDIBits(m_pBitmapDevice->GetBitmap(), m_Rect.left,
73                              m_Rect.top, m_Rect.Width(), m_Rect.Height());
74   }
75 }
76