blob: fc6ceab38e9f66b87783975802ae29b656237e33 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// Copyright 2014 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Lei Zhangb4e7f302015-11-06 15:52:32 -08007#include "public/fpdf_progressive.h"
8
Tom Sepezf0799fe2017-03-28 09:31:32 -07009#include <utility>
10
dsinclair39c62fd2016-09-29 12:49:17 -070011#include "core/fpdfapi/cpdf_pagerendercontext.h"
dsinclair41872fa2016-10-04 11:29:35 -070012#include "core/fpdfapi/page/cpdf_page.h"
dsinclair69d9c682016-10-04 12:18:35 -070013#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
dsinclaira52ab742016-09-29 13:59:29 -070014#include "core/fxcrt/fx_memory.h"
Dan Sinclaira5085d42017-05-11 16:26:50 -040015#include "core/fxge/cfx_defaultrenderdevice.h"
dsinclair74a34fc2016-09-29 16:41:42 -070016#include "core/fxge/cfx_renderdevice.h"
dsinclair114e46a2016-09-29 17:18:21 -070017#include "fpdfsdk/fsdk_define.h"
18#include "fpdfsdk/fsdk_pauseadapter.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080019#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070020#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070021
Lei Zhang8fec3e42015-11-04 15:32:02 -080022// These checks are here because core/ and public/ cannot depend on each other.
23static_assert(CPDF_ProgressiveRenderer::Ready == FPDF_RENDER_READER,
24 "CPDF_ProgressiveRenderer::Ready value mismatch");
25static_assert(CPDF_ProgressiveRenderer::ToBeContinued ==
26 FPDF_RENDER_TOBECOUNTINUED,
27 "CPDF_ProgressiveRenderer::ToBeContinued value mismatch");
28static_assert(CPDF_ProgressiveRenderer::Done == FPDF_RENDER_DONE,
29 "CPDF_ProgressiveRenderer::Done value mismatch");
30static_assert(CPDF_ProgressiveRenderer::Failed == FPDF_RENDER_FAILED,
31 "CPDF_ProgressiveRenderer::Failed value mismatch");
32
Nico Weber9d8ec5a2015-08-04 13:00:21 -070033DLLEXPORT int STDCALL FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
34 FPDF_PAGE page,
35 int start_x,
36 int start_y,
37 int size_x,
38 int size_y,
39 int rotate,
40 int flags,
41 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070042 if (!bitmap || !pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070043 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070044
Tom Sepezdb0be962015-10-16 14:00:21 -070045 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 if (!pPage)
47 return FPDF_RENDER_FAILED;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070048
Tom Sepezf0799fe2017-03-28 09:31:32 -070049 auto pOwnedContext = pdfium::MakeUnique<CPDF_PageRenderContext>();
50 CPDF_PageRenderContext* pContext = pOwnedContext.get();
51 pPage->SetRenderContext(std::move(pOwnedContext));
52
53 CFX_RetainPtr<CFX_DIBitmap> pBitmap(CFXBitmapFromFPDFBitmap(bitmap));
Dan Sinclaira5085d42017-05-11 16:26:50 -040054 auto pOwnedDevice = pdfium::MakeUnique<CFX_DefaultRenderDevice>();
55 CFX_DefaultRenderDevice* pDevice = pOwnedDevice.get();
Tom Sepezf0799fe2017-03-28 09:31:32 -070056 pContext->m_pDevice = std::move(pOwnedDevice);
thestigbefa4502016-05-26 20:15:19 -070057 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070058
tsepez1e2c5572016-05-25 14:58:09 -070059 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070060 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
tsepez4cf55152016-11-02 14:37:54 -070061 rotate, flags, false, &IPauseAdapter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070062
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063 if (pContext->m_pRenderer) {
Tom Sepezb3b67622015-10-19 16:20:03 -070064 return CPDF_ProgressiveRenderer::ToFPDFStatus(
65 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 }
67 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070068}
69
Nico Weber9d8ec5a2015-08-04 13:00:21 -070070DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
71 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070072 if (!pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070074
Tom Sepezdb0be962015-10-16 14:00:21 -070075 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070076 if (!pPage)
77 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070078
weili9f515bc2016-07-24 08:08:24 -070079 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 if (pContext && pContext->m_pRenderer) {
81 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
82 pContext->m_pRenderer->Continue(&IPauseAdapter);
Tom Sepezb3b67622015-10-19 16:20:03 -070083 return CPDF_ProgressiveRenderer::ToFPDFStatus(
84 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 }
86 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070087}
88
Nico Weber9d8ec5a2015-08-04 13:00:21 -070089DLLEXPORT void STDCALL FPDF_RenderPage_Close(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -070090 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091 if (!pPage)
92 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093
weili9f515bc2016-07-24 08:08:24 -070094 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Tom Sepezdfbf8e72015-10-14 14:17:26 -070095 if (!pContext)
96 return;
97
thestig41846a02016-05-26 10:45:30 -070098 pContext->m_pDevice->RestoreState(false);
tsepeze5cb0b12016-10-26 15:06:11 -070099 pPage->SetRenderContext(nullptr);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700100}