xref: /aosp_15_r20/external/pdfium/fpdfsdk/cpdfsdk_renderpage.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
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 "core/fpdfapi/page/cpdf_pageimagecache.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::kPrint : CPDF_OCContext::kView;
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->GetMutablePageResources(),
63       pPage->GetPageImageCache());
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         (flags & FPDF_PRINTING) ||
73         pContext->m_pDevice->GetDeviceType() != DeviceType::kDisplay;
74 
75     // TODO(https://crbug.com/pdfium/993) - maybe pass true here.
76     const bool bShowWidget = false;
77     pList->DisplayAnnots(pPage, pContext->m_pDevice.get(),
78                          pContext->m_pContext.get(), bPrinting, matrix,
79                          bShowWidget);
80   }
81 
82   pContext->m_pRenderer = std::make_unique<CPDF_ProgressiveRenderer>(
83       pContext->m_pContext.get(), pContext->m_pDevice.get(),
84       pContext->m_pOptions.get());
85   pContext->m_pRenderer->Start(pause);
86   if (need_to_restore)
87     pContext->m_pDevice->RestoreState(false);
88 }
89 
90 }  // namespace
91 
CPDFSDK_RenderPage(CPDF_PageRenderContext * pContext,CPDF_Page * pPage,const CFX_Matrix & matrix,const FX_RECT & clipping_rect,int flags,const FPDF_COLORSCHEME * color_scheme)92 void CPDFSDK_RenderPage(CPDF_PageRenderContext* pContext,
93                         CPDF_Page* pPage,
94                         const CFX_Matrix& matrix,
95                         const FX_RECT& clipping_rect,
96                         int flags,
97                         const FPDF_COLORSCHEME* color_scheme) {
98   RenderPageImpl(pContext, pPage, matrix, clipping_rect, flags, color_scheme,
99                  /*need_to_restore=*/true, /*pause=*/nullptr);
100 }
101 
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)102 void CPDFSDK_RenderPageWithContext(CPDF_PageRenderContext* pContext,
103                                    CPDF_Page* pPage,
104                                    int start_x,
105                                    int start_y,
106                                    int size_x,
107                                    int size_y,
108                                    int rotate,
109                                    int flags,
110                                    const FPDF_COLORSCHEME* color_scheme,
111                                    bool need_to_restore,
112                                    CPDFSDK_PauseAdapter* pause) {
113   const FX_RECT rect(start_x, start_y, start_x + size_x, start_y + size_y);
114   RenderPageImpl(pContext, pPage, pPage->GetDisplayMatrix(rect, rotate), rect,
115                  flags, color_scheme, need_to_restore, pause);
116 }
117