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