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 "core/fpdfapi/page/cpdf_pageobject.h"
10 #include "core/fpdfapi/render/cpdf_rendercontext.h"
11 #include "core/fpdfapi/render/cpdf_renderoptions.h"
12 #include "core/fxge/cfx_defaultrenderdevice.h"
13 #include "core/fxge/cfx_renderdevice.h"
14 #include "core/fxge/dib/cfx_dibitmap.h"
15 #include "core/fxge/fx_dib.h"
16 #include "third_party/base/ptr_util.h"
17
CPDF_DeviceBuffer()18 CPDF_DeviceBuffer::CPDF_DeviceBuffer() {}
19
~CPDF_DeviceBuffer()20 CPDF_DeviceBuffer::~CPDF_DeviceBuffer() {}
21
Initialize(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,FX_RECT * pRect,const CPDF_PageObject * pObj,int max_dpi)22 bool CPDF_DeviceBuffer::Initialize(CPDF_RenderContext* pContext,
23 CFX_RenderDevice* pDevice,
24 FX_RECT* pRect,
25 const CPDF_PageObject* pObj,
26 int max_dpi) {
27 m_pDevice = pDevice;
28 m_pContext = pContext;
29 m_Rect = *pRect;
30 m_pObject = pObj;
31 m_Matrix.Translate(-pRect->left, -pRect->top);
32 #if _FX_PLATFORM_ != _FX_PLATFORM_APPLE_
33 int horz_size = pDevice->GetDeviceCaps(FXDC_HORZ_SIZE);
34 int vert_size = pDevice->GetDeviceCaps(FXDC_VERT_SIZE);
35 if (horz_size && vert_size && max_dpi) {
36 int dpih =
37 pDevice->GetDeviceCaps(FXDC_PIXEL_WIDTH) * 254 / (horz_size * 10);
38 int dpiv =
39 pDevice->GetDeviceCaps(FXDC_PIXEL_HEIGHT) * 254 / (vert_size * 10);
40 if (dpih > max_dpi)
41 m_Matrix.Scale((float)(max_dpi) / dpih, 1.0f);
42 if (dpiv > max_dpi)
43 m_Matrix.Scale(1.0f, (float)(max_dpi) / (float)dpiv);
44 }
45 #endif
46 CFX_Matrix ctm = m_pDevice->GetCTM();
47 m_Matrix.Concat(CFX_Matrix(fabs(ctm.a), 0, 0, fabs(ctm.d), 0, 0));
48
49 FX_RECT bitmap_rect =
50 m_Matrix.TransformRect(CFX_FloatRect(*pRect)).GetOuterRect();
51 m_pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
52 m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(), FXDIB_Argb);
53 return true;
54 }
55
OutputToDevice()56 void CPDF_DeviceBuffer::OutputToDevice() {
57 if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
58 if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {
59 m_pDevice->SetDIBits(m_pBitmap, m_Rect.left, m_Rect.top);
60 } else {
61 m_pDevice->StretchDIBits(m_pBitmap, m_Rect.left, m_Rect.top,
62 m_Rect.Width(), m_Rect.Height());
63 }
64 return;
65 }
66 auto pBuffer = pdfium::MakeRetain<CFX_DIBitmap>();
67 m_pDevice->CreateCompatibleBitmap(pBuffer, m_pBitmap->GetWidth(),
68 m_pBitmap->GetHeight());
69 m_pContext->GetBackground(pBuffer, m_pObject.Get(), nullptr, &m_Matrix);
70 pBuffer->CompositeBitmap(0, 0, pBuffer->GetWidth(), pBuffer->GetHeight(),
71 m_pBitmap, 0, 0);
72 m_pDevice->StretchDIBits(pBuffer, m_Rect.left, m_Rect.top, m_Rect.Width(),
73 m_Rect.Height());
74 }
75