• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The PDFium Authors
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 "build/build_config.h"
13 #include "core/fpdfapi/page/cpdf_pageimagecache.h"
14 #include "core/fpdfapi/render/cpdf_pagerendercontext.h"
15 #include "core/fpdfapi/render/cpdf_progressiverenderer.h"
16 #include "core/fpdfapi/render/cpdf_renderoptions.h"
17 #include "core/fpdfdoc/cpdf_annotlist.h"
18 #include "core/fxge/cfx_renderdevice.h"
19 #include "fpdfsdk/cpdfsdk_helpers.h"
20 #include "fpdfsdk/cpdfsdk_pauseadapter.h"
21 
22 namespace {
23 
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)24 void RenderPageImpl(CPDF_PageRenderContext* pContext,
25                     CPDF_Page* pPage,
26                     const CFX_Matrix& matrix,
27                     const FX_RECT& clipping_rect,
28                     int flags,
29                     const FPDF_COLORSCHEME* color_scheme,
30                     bool need_to_restore,
31                     CPDFSDK_PauseAdapter* pause) {
32   if (!pContext->m_pOptions)
33     pContext->m_pOptions = std::make_unique<CPDF_RenderOptions>();
34 
35   auto& options = pContext->m_pOptions->GetOptions();
36   options.bClearType = !!(flags & FPDF_LCD_TEXT);
37   options.bNoNativeText = !!(flags & FPDF_NO_NATIVETEXT);
38   options.bLimitedImageCache = !!(flags & FPDF_RENDER_LIMITEDIMAGECACHE);
39   options.bForceHalftone = !!(flags & FPDF_RENDER_FORCEHALFTONE);
40   options.bNoTextSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHTEXT);
41   options.bNoImageSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHIMAGE);
42   options.bNoPathSmooth = !!(flags & FPDF_RENDER_NO_SMOOTHPATH);
43 
44   // Grayscale output
45   if (flags & FPDF_GRAYSCALE)
46     pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kGray);
47 
48   if (color_scheme) {
49     pContext->m_pOptions->SetColorMode(CPDF_RenderOptions::kForcedColor);
50     SetColorFromScheme(color_scheme, pContext->m_pOptions.get());
51     options.bConvertFillToStroke = !!(flags & FPDF_CONVERT_FILL_TO_STROKE);
52   }
53 
54   const CPDF_OCContext::UsageType usage =
55       (flags & FPDF_PRINTING) ? CPDF_OCContext::kPrint : CPDF_OCContext::kView;
56   pContext->m_pOptions->SetOCContext(
57       pdfium::MakeRetain<CPDF_OCContext>(pPage->GetDocument(), usage));
58 
59   pContext->m_pDevice->SaveState();
60   pContext->m_pDevice->SetBaseClip(clipping_rect);
61   pContext->m_pDevice->SetClip_Rect(clipping_rect);
62   pContext->m_pContext = std::make_unique<CPDF_RenderContext>(
63       pPage->GetDocument(), pPage->GetMutablePageResources(),
64       pPage->GetPageImageCache());
65 
66   pContext->m_pContext->AppendLayer(pPage, matrix);
67 
68   if (flags & FPDF_ANNOT) {
69     auto pOwnedList = std::make_unique<CPDF_AnnotList>(pPage);
70     CPDF_AnnotList* pList = pOwnedList.get();
71     pContext->m_pAnnots = std::move(pOwnedList);
72     bool is_printing = (flags & FPDF_PRINTING);
73 #if BUILDFLAG(IS_WIN)
74     is_printing |= pContext->m_pDevice->GetDeviceType() == DeviceType::kPrinter;
75 #endif
76 
77     // TODO(https://crbug.com/pdfium/993) - maybe pass true here.
78     const bool bShowWidget = false;
79     pList->DisplayAnnots(pContext->m_pContext.get(), is_printing, matrix,
80                          bShowWidget);
81   }
82 
83   pContext->m_pRenderer = std::make_unique<CPDF_ProgressiveRenderer>(
84       pContext->m_pContext.get(), pContext->m_pDevice.get(),
85       pContext->m_pOptions.get());
86   pContext->m_pRenderer->Start(pause);
87   if (need_to_restore)
88     pContext->m_pDevice->RestoreState(false);
89 }
90 
91 }  // namespace
92 
CPDFSDK_RenderPage(CPDF_PageRenderContext * pContext,CPDF_Page * pPage,const CFX_Matrix & matrix,const FX_RECT & clipping_rect,int flags,const FPDF_COLORSCHEME * color_scheme)93 void CPDFSDK_RenderPage(CPDF_PageRenderContext* pContext,
94                         CPDF_Page* pPage,
95                         const CFX_Matrix& matrix,
96                         const FX_RECT& clipping_rect,
97                         int flags,
98                         const FPDF_COLORSCHEME* color_scheme) {
99   RenderPageImpl(pContext, pPage, matrix, clipping_rect, flags, color_scheme,
100                  /*need_to_restore=*/true, /*pause=*/nullptr);
101 }
102 
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)103 void CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext* pContext,
104                                    CPDF_Page* pPage,
105                                    int start_x,
106                                    int start_y,
107                                    int size_x,
108                                    int size_y,
109                                    int rotate,
110                                    int flags,
111                                    const FPDF_COLORSCHEME* color_scheme,
112                                    bool need_to_restore,
113                                    CPDFSDK_PauseAdapter* pause) {
114   const FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
115   RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(rect, rotate), rect,
116                  flags, color_scheme, need_to_restore, pause);
117 }
118