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