• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_progressiverenderer.h"
8 
9 #include "core/fpdfapi/page/cpdf_image.h"
10 #include "core/fpdfapi/page/cpdf_imageobject.h"
11 #include "core/fpdfapi/page/cpdf_pageobject.h"
12 #include "core/fpdfapi/page/cpdf_pageobjectholder.h"
13 #include "core/fpdfapi/render/cpdf_pagerendercache.h"
14 #include "core/fpdfapi/render/cpdf_renderoptions.h"
15 #include "core/fpdfapi/render/cpdf_renderstatus.h"
16 #include "core/fxcrt/pauseindicator_iface.h"
17 #include "core/fxge/cfx_renderdevice.h"
18 #include "third_party/base/ptr_util.h"
19 
CPDF_ProgressiveRenderer(CPDF_RenderContext * pContext,CFX_RenderDevice * pDevice,const CPDF_RenderOptions * pOptions)20 CPDF_ProgressiveRenderer::CPDF_ProgressiveRenderer(
21     CPDF_RenderContext* pContext,
22     CFX_RenderDevice* pDevice,
23     const CPDF_RenderOptions* pOptions)
24     : m_pContext(pContext), m_pDevice(pDevice), m_pOptions(pOptions) {}
25 
~CPDF_ProgressiveRenderer()26 CPDF_ProgressiveRenderer::~CPDF_ProgressiveRenderer() {
27   if (m_pRenderStatus) {
28     m_pRenderStatus.reset();  // Release first.
29     m_pDevice->RestoreState(false);
30   }
31 }
32 
Start(PauseIndicatorIface * pPause)33 void CPDF_ProgressiveRenderer::Start(PauseIndicatorIface* pPause) {
34   if (!m_pContext || !m_pDevice || m_Status != kReady) {
35     m_Status = kFailed;
36     return;
37   }
38   m_Status = kToBeContinued;
39   Continue(pPause);
40 }
41 
Continue(PauseIndicatorIface * pPause)42 void CPDF_ProgressiveRenderer::Continue(PauseIndicatorIface* pPause) {
43   while (m_Status == kToBeContinued) {
44     if (!m_pCurrentLayer) {
45       if (m_LayerIndex >= m_pContext->CountLayers()) {
46         m_Status = kDone;
47         return;
48       }
49       m_pCurrentLayer = m_pContext->GetLayer(m_LayerIndex);
50       m_LastObjectRendered = m_pCurrentLayer->m_pObjectHolder->end();
51       m_pRenderStatus = pdfium::MakeUnique<CPDF_RenderStatus>(m_pContext.Get(),
52                                                               m_pDevice.Get());
53       if (m_pOptions)
54         m_pRenderStatus->SetOptions(*m_pOptions);
55       m_pRenderStatus->SetTransparency(
56           m_pCurrentLayer->m_pObjectHolder->GetTransparency());
57       m_pRenderStatus->Initialize(nullptr, nullptr);
58       m_pDevice->SaveState();
59       m_ClipRect = m_pCurrentLayer->m_Matrix.GetInverse().TransformRect(
60           CFX_FloatRect(m_pDevice->GetClipBox()));
61     }
62     CPDF_PageObjectHolder::const_iterator iter;
63     CPDF_PageObjectHolder::const_iterator iterEnd =
64         m_pCurrentLayer->m_pObjectHolder->end();
65     if (m_LastObjectRendered != iterEnd) {
66       iter = m_LastObjectRendered;
67       ++iter;
68     } else {
69       iter = m_pCurrentLayer->m_pObjectHolder->begin();
70     }
71     int nObjsToGo = kStepLimit;
72     bool is_mask = false;
73     while (iter != iterEnd) {
74       CPDF_PageObject* pCurObj = iter->get();
75       if (pCurObj && pCurObj->GetRect().left <= m_ClipRect.right &&
76           pCurObj->GetRect().right >= m_ClipRect.left &&
77           pCurObj->GetRect().bottom <= m_ClipRect.top &&
78           pCurObj->GetRect().top >= m_ClipRect.bottom) {
79         if (m_pOptions->GetOptions().bBreakForMasks && pCurObj->IsImage() &&
80             pCurObj->AsImage()->GetImage()->IsMask()) {
81           if (m_pDevice->GetDeviceType() == DeviceType::kPrinter) {
82             m_LastObjectRendered = iter;
83             m_pRenderStatus->ProcessClipPath(pCurObj->m_ClipPath,
84                                              m_pCurrentLayer->m_Matrix);
85             return;
86           }
87           is_mask = true;
88         }
89         if (m_pRenderStatus->ContinueSingleObject(
90                 pCurObj, m_pCurrentLayer->m_Matrix, pPause)) {
91           return;
92         }
93         if (pCurObj->IsImage() && m_pRenderStatus->GetRenderOptions()
94                                       .GetOptions()
95                                       .bLimitedImageCache) {
96           m_pContext->GetPageCache()->CacheOptimization(
97               m_pRenderStatus->GetRenderOptions().GetCacheSizeLimit());
98         }
99         if (pCurObj->IsForm() || pCurObj->IsShading())
100           nObjsToGo = 0;
101         else
102           --nObjsToGo;
103       }
104       m_LastObjectRendered = iter;
105       if (nObjsToGo == 0) {
106         if (pPause && pPause->NeedToPauseNow())
107           return;
108         nObjsToGo = kStepLimit;
109       }
110       ++iter;
111       if (is_mask && iter != iterEnd)
112         return;
113     }
114     if (m_pCurrentLayer->m_pObjectHolder->GetParseState() ==
115         CPDF_PageObjectHolder::ParseState::kParsed) {
116       m_pRenderStatus.reset();
117       m_pDevice->RestoreState(false);
118       m_pCurrentLayer = nullptr;
119       m_LayerIndex++;
120       if (is_mask || (pPause && pPause->NeedToPauseNow()))
121         return;
122     } else if (is_mask) {
123       return;
124     } else {
125       m_pCurrentLayer->m_pObjectHolder->ContinueParse(pPause);
126       if (m_pCurrentLayer->m_pObjectHolder->GetParseState() !=
127           CPDF_PageObjectHolder::ParseState::kParsed) {
128         return;
129       }
130     }
131   }
132 }
133