• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The PDFium Authors
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 <utility>
10 
11 #include "build/build_config.h"
12 #include "core/fpdfapi/page/cpdf_pageobject.h"
13 #include "core/fpdfapi/parser/cpdf_dictionary.h"
14 #include "core/fpdfapi/render/cpdf_renderoptions.h"
15 #include "core/fxge/cfx_defaultrenderdevice.h"
16 #include "core/fxge/cfx_renderdevice.h"
17 #include "core/fxge/dib/cfx_dibitmap.h"
18 #include "core/fxge/dib/fx_dib.h"
19 
20 #if BUILDFLAG(IS_WIN)
21 #include "core/fpdfapi/render/cpdf_rendercontext.h"
22 #else
23 #include "core/fxcrt/notreached.h"
24 #endif
25 
26 namespace {
27 
28 #if BUILDFLAG(IS_WIN)
29 constexpr bool kScaleDeviceBuffer = true;
30 #else
31 constexpr bool kScaleDeviceBuffer = false;
32 #endif
33 
34 }  // namespace
35 
36 // static
CalculateMatrix(CFX_RenderDevice * pDevice,const FX_RECT & rect,int max_dpi,bool scale)37 CFX_Matrix CPDF_DeviceBuffer::CalculateMatrix(CFX_RenderDevice* pDevice,
38                                               const FX_RECT& rect,
39                                               int max_dpi,
40                                               bool scale) {
41   CFX_Matrix matrix;
42   matrix.Translate(-rect.left, -rect.top);
43   if (scale) {
44     int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);
45     int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);
46     if (horz_size && vert_size && max_dpi) {
47       int dpih =
48           pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);
49       int dpiv =
50           pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);
51       if (dpih > max_dpi)
52         matrix.Scale(static_cast<float>(max_dpi) / dpih, 1.0f);
53       if (dpiv > max_dpi)
54         matrix.Scale(1.0f, static_cast<float>(max_dpi) / dpiv);
55     }
56   }
57   return matrix;
58 }
59 
CPDF_DeviceBuffer(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const FX_RECT & rect,const CPDF_PageObject * pObj,int max_dpi)60 CPDF_DeviceBuffer::CPDF_DeviceBuffer(CPDF_RenderContext* pContext,
61                                      CFX_RenderDevice* pDevice,
62                                      const FX_RECT& rect,
63                                      const CPDF_PageObject* pObj,
64                                      int max_dpi)
65     : m_pDevice(pDevice),
66 #if BUILDFLAG(IS_WIN)
67       m_pContext(pContext),
68 #endif
69       m_pObject(pObj),
70       m_pBitmap(pdfium::MakeRetain<CFX_DIBitmap>()),
71       m_Rect(rect),
72       m_Matrix(CalculateMatrix(pDevice, rect, max_dpi, kScaleDeviceBuffer)) {
73 }
74 
75 CPDF_DeviceBuffer::~CPDF_DeviceBuffer() = default;
76 
Initialize()77 RetainPtr<CFX_DIBitmap> CPDF_DeviceBuffer::Initialize() {
78   FX_RECT bitmap_rect =
79       m_Matrix.TransformRect(CFX_FloatRect(m_Rect)).GetOuterRect();
80   // TODO(crbug.com/355630557): Consider adding support for
81   // `FXDIB_Format::kBgraPremul`
82   if (!m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(),
83                          FXDIB_Format::kBgra)) {
84     return nullptr;
85   }
86   return m_pBitmap;
87 }
88 
OutputToDevice()89 void CPDF_DeviceBuffer::OutputToDevice() {
90   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
91     if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {
92       m_pDevice->SetDIBits(m_pBitmap, m_Rect.left, m_Rect.top);
93       return;
94     }
95 
96 #if BUILDFLAG(IS_WIN)
97     m_pDevice->StretchDIBits(m_pBitmap, m_Rect.left, m_Rect.top, m_Rect.Width(),
98                              m_Rect.Height());
99     return;
100 #else
101     NOTREACHED_NORETURN();
102 #endif
103   }
104 
105 #if BUILDFLAG(IS_WIN)
106   auto buffer = pdfium::MakeRetain<CFX_DIBitmap>();
107   if (!m_pDevice->CreateCompatibleBitmap(buffer, m_pBitmap->GetWidth(),
108                                          m_pBitmap->GetHeight())) {
109     return;
110   }
111   m_pContext->GetBackgroundToBitmap(buffer, m_pObject, m_Matrix);
112   buffer->CompositeBitmap(0, 0, buffer->GetWidth(), buffer->GetHeight(),
113                           m_pBitmap, 0, 0, BlendMode::kNormal, nullptr, false);
114   m_pDevice->StretchDIBits(std::move(buffer), m_Rect.left, m_Rect.top,
115                            m_Rect.Width(), m_Rect.Height());
116 #else
117   NOTREACHED_NORETURN();
118 #endif
119 }
120