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_page.h"
10 #include "core/fpdfapi/page/cpdf_pageobject.h"
11 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
12 #include "core/fpdfapi/parser/cpdf_dictionary.h"
13 #include "core/fpdfapi/parser/cpdf_document.h"
14 #include "core/fpdfapi/render/cpdf_pagerendercache.h"
15 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
16 #include "core/fpdfapi/render/cpdf_renderoptions.h"
17 #include "core/fpdfapi/render/cpdf_renderstatus.h"
18 #include "core/fpdfapi/render/cpdf_textrenderer.h"
19 #include "core/fxge/cfx_renderdevice.h"
20 #include "core/fxge/fx_dib.h"
21
CPDF_RenderContext(CPDF_Page * pPage)22 CPDF_RenderContext::CPDF_RenderContext(CPDF_Page* pPage)
23 : m_pDocument(pPage->m_pDocument),
24 m_pPageResources(pPage->m_pPageResources),
25 m_pPageCache(pPage->GetRenderCache()) {}
26
CPDF_RenderContext(CPDF_Document * pDoc,CPDF_PageRenderCache * pPageCache)27 CPDF_RenderContext::CPDF_RenderContext(CPDF_Document* pDoc,
28 CPDF_PageRenderCache* pPageCache)
29 : m_pDocument(pDoc), m_pPageResources(nullptr), m_pPageCache(pPageCache) {}
30
~CPDF_RenderContext()31 CPDF_RenderContext::~CPDF_RenderContext() {}
32
GetBackground(CFX_DIBitmap * pBuffer,const CPDF_PageObject * pObj,const CPDF_RenderOptions * pOptions,CFX_Matrix * pFinalMatrix)33 void CPDF_RenderContext::GetBackground(CFX_DIBitmap* pBuffer,
34 const CPDF_PageObject* pObj,
35 const CPDF_RenderOptions* pOptions,
36 CFX_Matrix* pFinalMatrix) {
37 CFX_FxgeDevice device;
38 device.Attach(pBuffer, false, nullptr, false);
39
40 FX_RECT rect(0, 0, device.GetWidth(), device.GetHeight());
41 device.FillRect(&rect, 0xffffffff);
42 Render(&device, pObj, pOptions, pFinalMatrix);
43 }
44
AppendLayer(CPDF_PageObjectHolder * pObjectHolder,const CFX_Matrix * pObject2Device)45 void CPDF_RenderContext::AppendLayer(CPDF_PageObjectHolder* pObjectHolder,
46 const CFX_Matrix* pObject2Device) {
47 m_Layers.emplace_back();
48 m_Layers.back().m_pObjectHolder = pObjectHolder;
49 if (pObject2Device)
50 m_Layers.back().m_Matrix = *pObject2Device;
51 else
52 m_Layers.back().m_Matrix.SetIdentity();
53 }
54
Render(CFX_RenderDevice * pDevice,const CPDF_RenderOptions * pOptions,const CFX_Matrix * pLastMatrix)55 void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice,
56 const CPDF_RenderOptions* pOptions,
57 const CFX_Matrix* pLastMatrix) {
58 Render(pDevice, nullptr, pOptions, pLastMatrix);
59 }
60
Render(CFX_RenderDevice * pDevice,const CPDF_PageObject * pStopObj,const CPDF_RenderOptions * pOptions,const CFX_Matrix * pLastMatrix)61 void CPDF_RenderContext::Render(CFX_RenderDevice* pDevice,
62 const CPDF_PageObject* pStopObj,
63 const CPDF_RenderOptions* pOptions,
64 const CFX_Matrix* pLastMatrix) {
65 for (auto& layer : m_Layers) {
66 pDevice->SaveState();
67 if (pLastMatrix) {
68 CFX_Matrix FinalMatrix = layer.m_Matrix;
69 FinalMatrix.Concat(*pLastMatrix);
70 CPDF_RenderStatus status;
71 status.Initialize(this, pDevice, pLastMatrix, pStopObj, nullptr, nullptr,
72 pOptions, layer.m_pObjectHolder->m_Transparency, false,
73 nullptr);
74 status.RenderObjectList(layer.m_pObjectHolder, &FinalMatrix);
75 if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE)
76 m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);
77 if (status.m_bStopped) {
78 pDevice->RestoreState(false);
79 break;
80 }
81 } else {
82 CPDF_RenderStatus status;
83 status.Initialize(this, pDevice, nullptr, pStopObj, nullptr, nullptr,
84 pOptions, layer.m_pObjectHolder->m_Transparency, false,
85 nullptr);
86 status.RenderObjectList(layer.m_pObjectHolder, &layer.m_Matrix);
87 if (status.m_Options.m_Flags & RENDER_LIMITEDIMAGECACHE)
88 m_pPageCache->CacheOptimization(status.m_Options.m_dwLimitCacheSize);
89 if (status.m_bStopped) {
90 pDevice->RestoreState(false);
91 break;
92 }
93 }
94 pDevice->RestoreState(false);
95 }
96 }
97