blob: 1933c884859f43dc7f5a5919a84f4bd196b0de5c [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
dsinclair39c62fd2016-09-29 12:49:17 -07009#include "core/fpdfapi/cpdf_pagerendercontext.h"
dsinclair41872fa2016-10-04 11:29:35 -070010#include "core/fpdfapi/page/cpdf_page.h"
dsinclair69d9c682016-10-04 12:18:35 -070011#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
dsinclaira52ab742016-09-29 13:59:29 -070012#include "core/fxcrt/fx_memory.h"
dsinclair74a34fc2016-09-29 16:41:42 -070013#include "core/fxge/cfx_fxgedevice.h"
14#include "core/fxge/cfx_renderdevice.h"
dsinclair114e46a2016-09-29 17:18:21 -070015#include "fpdfsdk/fsdk_define.h"
16#include "fpdfsdk/fsdk_pauseadapter.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080017#include "public/fpdfview.h"
tsepez36eb4bd2016-10-03 15:24:27 -070018#include "third_party/base/ptr_util.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070019
Lei Zhang8fec3e42015-11-04 15:32:02 -080020// These checks are here because core/ and public/ cannot depend on each other.
21static_assert(CPDF_ProgressiveRenderer::Ready == FPDF_RENDER_READER,
22 "CPDF_ProgressiveRenderer::Ready value mismatch");
23static_assert(CPDF_ProgressiveRenderer::ToBeContinued ==
24 FPDF_RENDER_TOBECOUNTINUED,
25 "CPDF_ProgressiveRenderer::ToBeContinued value mismatch");
26static_assert(CPDF_ProgressiveRenderer::Done == FPDF_RENDER_DONE,
27 "CPDF_ProgressiveRenderer::Done value mismatch");
28static_assert(CPDF_ProgressiveRenderer::Failed == FPDF_RENDER_FAILED,
29 "CPDF_ProgressiveRenderer::Failed value mismatch");
30
Nico Weber9d8ec5a2015-08-04 13:00:21 -070031DLLEXPORT int STDCALL FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
32 FPDF_PAGE page,
33 int start_x,
34 int start_y,
35 int size_x,
36 int size_y,
37 int rotate,
38 int flags,
39 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070040 if (!bitmap || !pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070041 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070042
Tom Sepezdb0be962015-10-16 14:00:21 -070043 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070044 if (!pPage)
45 return FPDF_RENDER_FAILED;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070046
weili9f515bc2016-07-24 08:08:24 -070047 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
tsepez36eb4bd2016-10-03 15:24:27 -070048 pPage->SetRenderContext(pdfium::WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -070049 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -070050 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -070051 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
52 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070053
tsepez1e2c5572016-05-25 14:58:09 -070054 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
56 rotate, flags, FALSE, &IPauseAdapter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070057
Nico Weber9d8ec5a2015-08-04 13:00:21 -070058 if (pContext->m_pRenderer) {
Tom Sepezb3b67622015-10-19 16:20:03 -070059 return CPDF_ProgressiveRenderer::ToFPDFStatus(
60 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070061 }
62 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070063}
64
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
66 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070067 if (!pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070068 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070069
Tom Sepezdb0be962015-10-16 14:00:21 -070070 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 if (!pPage)
72 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070073
weili9f515bc2016-07-24 08:08:24 -070074 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 if (pContext && pContext->m_pRenderer) {
76 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
77 pContext->m_pRenderer->Continue(&IPauseAdapter);
Tom Sepezb3b67622015-10-19 16:20:03 -070078 return CPDF_ProgressiveRenderer::ToFPDFStatus(
79 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 }
81 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082}
83
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084DLLEXPORT void STDCALL FPDF_RenderPage_Close(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -070085 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070086 if (!pPage)
87 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070088
weili9f515bc2016-07-24 08:08:24 -070089 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Tom Sepezdfbf8e72015-10-14 14:17:26 -070090 if (!pContext)
91 return;
92
thestig41846a02016-05-26 10:45:30 -070093 pContext->m_pDevice->RestoreState(false);
weili9f515bc2016-07-24 08:08:24 -070094 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070095}