1 // Copyright 2020 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 "fpdfsdk/cpdfsdk_renderpage.h"
8
9 #include <utility>
10
11 #include "core/fpdfapi/render/cpdf_pagerendercache.h"
12 #include "core/fpdfapi/render/cpdf_pagerendercontext.h"
13 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
14 #include "core/fpdfapi/render/cpdf_renderoptions.h"
15 #include "core/fpdfdoc/cpdf_annotlist.h"
16 #include "core/fxge/cfx_renderdevice.h"
17 #include "fpdfsdk/cpdfsdk_pauseadapter.h"
18 #include "public/fpdfview.h"
19 #include "third_party/base/ptr_util.h"
20
21 namespace {
22
RenderPageImpl(CPDF_PageRenderContext * pContext,CPDF_Page * pPage,const CFX_Matrix & matrix,const FX_RECT & clipping_rect,int flags,bool need_to_restore,CPDFSDK_PauseAdapter * pause)23 void RenderPageImpl(CPDF_PageRenderContext* pContext,
24 CPDF_Page* pPage,
25 const CFX_Matrix& matrix,
26 const FX_RECT& clipping_rect,
27 int flags,
28 bool need_to_restore,
29 CPDFSDK_PauseAdapter* pause) {
30 if (!pContext->m_pOptions)
31 pContext->m_pOptions = pdfium::MakeUnique<CPDF_RenderOptions>();
32
33 auto& options = pContext->m_pOptions->GetOptions();
34 options.bClearType = !!(flags & FPDF_LCD_TEXT);
35 options.bNoNativeText = !!(flags & FPDF_NO_NATIVETEXT);
36 options.bLimitedImageCache = !!(flags & FPDF_RENDER_LIMITEDIMAGECACHE);
37 options.bForceHalftone = !!(flags & FPDF_RENDER_FORCEHALFTONE);
38 options.bNoTextSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHTEXT);
39 options.bNoImageSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHIMAGE);
40 options.bNoPathSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHPATH);
41
42 // Grayscale output
43 if (flags & FPDF_GRAYSCALE)
44 pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kGray);
45
46 const CPDF_OCContext::UsageType usage =
47 (flags & FPDF_PRINTING) ? CPDF_OCContext::Print : CPDF_OCContext::View;
48 pContext->m_pOptions->SetOCContext(
49 pdfium::MakeRetain<CPDF_OCContext>(pPage->GetDocument(), usage));
50
51 pContext->m_pDevice->SaveState();
52 pContext->m_pDevice->SetBaseClip(clipping_rect);
53 pContext->m_pDevice->SetClip_Rect(clipping_rect);
54 pContext->m_pContext = pdfium::MakeUnique<CPDF_RenderContext>(
55 pPage->GetDocument(), pPage->m_pPageResources.Get(),
56 static_cast<CPDF_PageRenderCache*>(pPage->GetRenderCache()));
57
58 pContext->m_pContext->AppendLayer(pPage, &matrix);
59
60 if (flags & FPDF_ANNOT) {
61 auto pOwnedList = pdfium::MakeUnique<CPDF_AnnotList>(pPage);
62 CPDF_AnnotList* pList = pOwnedList.get();
63 pContext->m_pAnnots = std::move(pOwnedList);
64 bool bPrinting =
65 pContext->m_pDevice->GetDeviceType() != DeviceType::kDisplay;
66 pList->DisplayAnnots(pPage, pContext->m_pContext.get(), bPrinting, &matrix,
67 false, nullptr);
68 }
69
70 pContext->m_pRenderer = pdfium::MakeUnique<CPDF_ProgressiveRenderer>(
71 pContext->m_pContext.get(), pContext->m_pDevice.get(),
72 pContext->m_pOptions.get());
73 pContext->m_pRenderer->Start(pause);
74 if (need_to_restore)
75 pContext->m_pDevice->RestoreState(false);
76 }
77
78 } // namespace
79
CPDFSDK_RenderPage(CPDF_PageRenderContext * pContext,CPDF_Page * pPage,const CFX_Matrix & matrix,const FX_RECT & clipping_rect,int flags)80 void CPDFSDK_RenderPage(CPDF_PageRenderContext* pContext,
81 CPDF_Page* pPage,
82 const CFX_Matrix& matrix,
83 const FX_RECT& clipping_rect,
84 int flags) {
85 RenderPageImpl(pContext, pPage, matrix, clipping_rect, flags,
86 /*need_to_restore=*/true, /*pause=*/nullptr);
87 }
88
CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext * pContext,CPDF_Page * pPage,int start_x,int start_y,int size_x,int size_y,int rotate,int flags,bool need_to_restore,CPDFSDK_PauseAdapter * pause)89 void CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext* pContext,
90 CPDF_Page* pPage,
91 int start_x,
92 int start_y,
93 int size_x,
94 int size_y,
95 int rotate,
96 int flags,
97 bool need_to_restore,
98 CPDFSDK_PauseAdapter* pause) {
99 const FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
100 RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(rect, rotate), rect,
101 flags, need_to_restore, pause);
102 }
103