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_rendercontext.h"
8
9 #include <utility>
10
11 #include "build/build_config.h"
12 #include "core/fpdfapi/page/cpdf_pageimagecache.h"
13 #include "core/fpdfapi/page/cpdf_pageobject.h"
14 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
15 #include "core/fpdfapi/parser/cpdf_dictionary.h"
16 #include "core/fpdfapi/parser/cpdf_document.h"
17 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
18 #include "core/fpdfapi/render/cpdf_renderoptions.h"
19 #include "core/fpdfapi/render/cpdf_renderstatus.h"
20 #include "core/fpdfapi/render/cpdf_textrenderer.h"
21 #include "core/fxcrt/check.h"
22 #include "core/fxge/cfx_defaultrenderdevice.h"
23 #include "core/fxge/cfx_renderdevice.h"
24 #include "core/fxge/dib/cfx_dibitmap.h"
25 #include "core/fxge/dib/fx_dib.h"
26
CPDF_RenderContext(CPDF_Document * pDoc,RetainPtr<CPDF_Dictionary> pPageResources,CPDF_PageImageCache * pPageCache)27 CPDF_RenderContext::CPDF_RenderContext(
28 CPDF_Document* pDoc,
29 RetainPtr<CPDF_Dictionary> pPageResources,
30 CPDF_PageImageCache* pPageCache)
31 : m_pDocument(pDoc),
32 m_pPageResources(std::move(pPageResources)),
33 m_pPageCache(pPageCache) {}
34
35 CPDF_RenderContext::~CPDF_RenderContext() = default;
36
GetBackgroundToDevice(CFX_RenderDevice * device,const CPDF_PageObject * object,const CPDF_RenderOptions * options,const CFX_Matrix & matrix)37 void CPDF_RenderContext::GetBackgroundToDevice(
38 CFX_RenderDevice* device,
39 const CPDF_PageObject* object,
40 const CPDF_RenderOptions* options,
41 const CFX_Matrix& matrix) {
42 device->FillRect(FX_RECT(0, 0, device->GetWidth(), device->GetHeight()),
43 0xffffffff);
44 Render(device, object, options, &matrix);
45 }
46
47 #if BUILDFLAG(IS_WIN)
GetBackgroundToBitmap(RetainPtr<CFX_DIBitmap> bitmap,const CPDF_PageObject * object,const CFX_Matrix & matrix)48 void CPDF_RenderContext::GetBackgroundToBitmap(RetainPtr<CFX_DIBitmap> bitmap,
49 const CPDF_PageObject* object,
50 const CFX_Matrix& matrix) {
51 CFX_DefaultRenderDevice device;
52 device.Attach(std::move(bitmap));
53 GetBackgroundToDevice(&device, object, /*options=*/nullptr, matrix);
54 }
55 #endif
56
AppendLayer(CPDF_PageObjectHolder * pObjectHolder,const CFX_Matrix & mtObject2Device)57 void CPDF_RenderContext::AppendLayer(CPDF_PageObjectHolder* pObjectHolder,
58 const CFX_Matrix& mtObject2Device) {
59 m_Layers.emplace_back(pObjectHolder, mtObject2Device);
60 }
61
Render(CFX_RenderDevice * pDevice,const CPDF_PageObject * pStopObj,const CPDF_RenderOptions * pOptions,const CFX_Matrix * pLastMatrix)62 void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice,
63 const CPDF_PageObject* pStopObj,
64 const CPDF_RenderOptions* pOptions,
65 const CFX_Matrix* pLastMatrix) {
66 for (auto& layer : m_Layers) {
67 CFX_RenderDevice::StateRestorer restorer(pDevice);
68 CPDF_RenderStatus status(this, pDevice);
69 if (pOptions)
70 status.SetOptions(*pOptions);
71 status.SetStopObject(pStopObj);
72 status.SetTransparency(layer.GetObjectHolder()->GetTransparency());
73 CFX_Matrix final_matrix = layer.GetMatrix();
74 if (pLastMatrix) {
75 final_matrix *= *pLastMatrix;
76 status.SetDeviceMatrix(*pLastMatrix);
77 }
78 status.Initialize(nullptr, nullptr);
79 status.RenderObjectList(layer.GetObjectHolder(), final_matrix);
80 if (status.GetRenderOptions().GetOptions().bLimitedImageCache) {
81 m_pPageCache->CacheOptimization(
82 status.GetRenderOptions().GetCacheSizeLimit());
83 }
84 if (status.IsStopped())
85 break;
86 }
87 }
88
Layer(CPDF_PageObjectHolder * pHolder,const CFX_Matrix & matrix)89 CPDF_RenderContext::Layer::Layer(CPDF_PageObjectHolder* pHolder,
90 const CFX_Matrix& matrix)
91 : m_pObjectHolder(pHolder), m_Matrix(matrix) {}
92
93 CPDF_RenderContext::Layer::Layer(const Layer& that) = default;
94
95 CPDF_RenderContext::Layer::~Layer() = default;
96