Home | History | Annotate | Download | only in render
      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_fxgedevice.h"
     13 #include "core/fxge/cfx_renderdevice.h"
     14 #include "core/fxge/fx_dib.h"
     15 #include "third_party/base/ptr_util.h"
     16 
     17 CPDF_DeviceBuffer::CPDF_DeviceBuffer()
     18     : m_pDevice(nullptr), m_pContext(nullptr), m_pObject(nullptr) {}
     19 
     20 CPDF_DeviceBuffer::~CPDF_DeviceBuffer() {}
     21 
     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 _FXM_PLATFORM_ != _FXM_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((FX_FLOAT)(max_dpi) / dpih, 1.0f);
     42     if (dpiv > max_dpi)
     43       m_Matrix.Scale(1.0f, (FX_FLOAT)(max_dpi) / (FX_FLOAT)dpiv);
     44   }
     45 #endif
     46   CFX_Matrix ctm = m_pDevice->GetCTM();
     47   m_Matrix.Concat(CFX_Matrix(FXSYS_fabs(ctm.a), 0, 0, FXSYS_fabs(ctm.d), 0, 0));
     48 
     49   CFX_FloatRect rect(*pRect);
     50   m_Matrix.TransformRect(rect);
     51 
     52   FX_RECT bitmap_rect = rect.GetOuterRect();
     53   m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
     54   m_pBitmap->Create(bitmap_rect.Width(), bitmap_rect.Height(), FXDIB_Argb);
     55   return true;
     56 }
     57 
     58 void CPDF_DeviceBuffer::OutputToDevice() {
     59   if (m_pDevice->GetDeviceCaps(FXDC_RENDER_CAPS) & FXRC_GET_BITS) {
     60     if (m_Matrix.a == 1.0f && m_Matrix.d == 1.0f) {
     61       m_pDevice->SetDIBits(m_pBitmap.get(), m_Rect.left, m_Rect.top);
     62     } else {
     63       m_pDevice->StretchDIBits(m_pBitmap.get(), m_Rect.left, m_Rect.top,
     64                                m_Rect.Width(), m_Rect.Height());
     65     }
     66     return;
     67   }
     68   CFX_DIBitmap buffer;
     69   m_pDevice->CreateCompatibleBitmap(&buffer, m_pBitmap->GetWidth(),
     70                                     m_pBitmap->GetHeight());
     71   m_pContext->GetBackground(&buffer, m_pObject, nullptr, &m_Matrix);
     72   buffer.CompositeBitmap(0, 0, buffer.GetWidth(), buffer.GetHeight(),
     73                          m_pBitmap.get(), 0, 0);
     74   m_pDevice->StretchDIBits(&buffer, m_Rect.left, m_Rect.top, m_Rect.Width(),
     75                            m_Rect.Height());
     76 }
     77