• 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_devicebuffer.h"
8 
9 #include "build/build_config.h"
10 #include "core/fpdfapi/page/cpdf_pageobject.h"
11 #include "core/fpdfapi/render/cpdf_rendercontext.h"
12 #include "core/fpdfapi/render/cpdf_renderoptions.h"
13 #include "core/fxge/cfx_defaultrenderdevice.h"
14 #include "core/fxge/cfx_renderdevice.h"
15 #include "core/fxge/dib/cfx_dibitmap.h"
16 #include "core/fxge/fx_dib.h"
17 
18 namespace {
19 
20 #if defined(OS_MACOSX)
21 constexpr bool kScaleDeviceBuffer = false;
22 #else
23 constexpr bool kScaleDeviceBuffer = true;
24 #endif
25 
26 }  // namespace
27 
28 // static
CalculateMatrix(CFX_RenderDevice * pDevice,const FX_RECT & rect,int max_dpi,bool scale)29 CFX_Matrix CPDF_DeviceBuffer::CalculateMatrix(CFX_RenderDevice* pDevice,
30                                               const FX_RECT& rect,
31                                               int max_dpi,
32                                               bool scale) {
33   CFX_Matrix matrix;
34   matrix.Translate(-rect.left, -rect.top);
35   if (scale) {
36     int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);
37     int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);
38     if (horz_size && vert_size && max_dpi) {
39       int dpih =
40           pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);
41       int dpiv =
42           pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);
43       if (dpih > max_dpi)
44         matrix.Scale(static_cast<float>(max_dpi) / dpih, 1.0f);
45       if (dpiv > max_dpi)
46         matrix.Scale(1.0f, static_cast<float>(max_dpi) / dpiv);
47     }
48   }
49   return matrix;
50 }
51 
CPDF_DeviceBuffer(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const FX_RECT & rect,const CPDF_PageObject * pObj,int max_dpi)52 CPDF_DeviceBuffer::CPDF_DeviceBuffer(CPDF_RenderContext* pContext,
53                                      CFX_RenderDevice* pDevice,
54                                      const FX_RECT& rect,
55                                      const CPDF_PageObject* pObj,
56                                      int max_dpi)
57     : m_pDevice(pDevice),
58       m_pContext(pContext),
59       m_pObject(pObj),
60       m_pBitmap(pdfium::MakeRetain<CFX_DIBitmap>()),
61       m_Rect(rect),
62       m_Matrix(CalculateMatrix(pDevice, rect, max_dpi, kScaleDeviceBuffer)) {}
63 
64 CPDF_DeviceBuffer::~CPDF_DeviceBuffer() = default;
65 
Initialize()66 bool CPDF_DeviceBuffer::Initialize() {
67   FX_RECT bitmap_rect =
68       m_Matrix.TransformRect(CFX_FloatRect(m_Rect)).GetOuterRect();
69   return m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(),
70                            FXDIB_Argb);
71 }
72 
OutputToDevice()73 void CPDF_DeviceBuffer::OutputToDevice() {
74   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
75     if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {
76       m_pDevice->SetDIBits(m_pBitmap, m_Rect.left, m_Rect.top);
77     } else {
78       m_pDevice->StretchDIBits(m_pBitmap, m_Rect.left, m_Rect.top,
79                                m_Rect.Width(), m_Rect.Height());
80     }
81     return;
82   }
83   auto pBuffer = pdfium::MakeRetain<CFX_DIBitmap>();
84   m_pDevice->CreateCompatibleBitmap(pBuffer, m_pBitmap->GetWidth(),
85                                     m_pBitmap->GetHeight());
86   m_pContext->GetBackground(pBuffer, m_pObject.Get(), nullptr, m_Matrix);
87   pBuffer->CompositeBitmap(0, 0, pBuffer->GetWidth(), pBuffer->GetHeight(),
88                            m_pBitmap, 0, 0, BlendMode::kNormal, nullptr, false);
89   m_pDevice->StretchDIBits(pBuffer, m_Rect.left, m_Rect.top, m_Rect.Width(),
90                            m_Rect.Height());
91 }
92