blob: 2912e8b6c388dd1f6345c46e2821c27d9994b1a6 [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
Dan Sinclair455a4192016-03-16 09:48:56 -04009#include "core/fpdfapi/fpdf_page/include/cpdf_page.h"
Dan Sinclairaa403d32016-03-15 14:57:22 -040010#include "core/fpdfapi/fpdf_render/include/cpdf_progressiverenderer.h"
weili9f515bc2016-07-24 08:08:24 -070011#include "core/fpdfapi/include/cpdf_pagerendercontext.h"
12#include "core/fxcrt/include/fx_memory.h"
thestig25fa42f2016-05-25 21:39:46 -070013#include "core/fxge/include/fx_ge.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080014#include "fpdfsdk/include/fsdk_define.h"
weili9f515bc2016-07-24 08:08:24 -070015#include "fpdfsdk/include/fsdk_pauseadapter.h"
Lei Zhangb4e7f302015-11-06 15:52:32 -080016#include "public/fpdfview.h"
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070017
Lei Zhang8fec3e42015-11-04 15:32:02 -080018// These checks are here because core/ and public/ cannot depend on each other.
19static_assert(CPDF_ProgressiveRenderer::Ready == FPDF_RENDER_READER,
20 "CPDF_ProgressiveRenderer::Ready value mismatch");
21static_assert(CPDF_ProgressiveRenderer::ToBeContinued ==
22 FPDF_RENDER_TOBECOUNTINUED,
23 "CPDF_ProgressiveRenderer::ToBeContinued value mismatch");
24static_assert(CPDF_ProgressiveRenderer::Done == FPDF_RENDER_DONE,
25 "CPDF_ProgressiveRenderer::Done value mismatch");
26static_assert(CPDF_ProgressiveRenderer::Failed == FPDF_RENDER_FAILED,
27 "CPDF_ProgressiveRenderer::Failed value mismatch");
28
Nico Weber9d8ec5a2015-08-04 13:00:21 -070029DLLEXPORT int STDCALL FPDF_RenderPageBitmap_Start(FPDF_BITMAP bitmap,
30 FPDF_PAGE page,
31 int start_x,
32 int start_y,
33 int size_x,
34 int size_y,
35 int rotate,
36 int flags,
37 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070038 if (!bitmap || !pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070039 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070040
Tom Sepezdb0be962015-10-16 14:00:21 -070041 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070042 if (!pPage)
43 return FPDF_RENDER_FAILED;
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070044
weili9f515bc2016-07-24 08:08:24 -070045 CPDF_PageRenderContext* pContext = new CPDF_PageRenderContext;
46 pPage->SetRenderContext(WrapUnique(pContext));
thestigbefa4502016-05-26 20:15:19 -070047 CFX_FxgeDevice* pDevice = new CFX_FxgeDevice;
weili9f515bc2016-07-24 08:08:24 -070048 pContext->m_pDevice.reset(pDevice);
thestigbefa4502016-05-26 20:15:19 -070049 CFX_DIBitmap* pBitmap = CFXBitmapFromFPDFBitmap(bitmap);
50 pDevice->Attach(pBitmap, !!(flags & FPDF_REVERSE_BYTE_ORDER), nullptr, false);
Lei Zhanga6d9f0e2015-06-13 00:48:38 -070051
tsepez1e2c5572016-05-25 14:58:09 -070052 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070053 FPDF_RenderPage_Retail(pContext, page, start_x, start_y, size_x, size_y,
54 rotate, flags, FALSE, &IPauseAdapter);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070055
Nico Weber9d8ec5a2015-08-04 13:00:21 -070056 if (pContext->m_pRenderer) {
Tom Sepezb3b67622015-10-19 16:20:03 -070057 return CPDF_ProgressiveRenderer::ToFPDFStatus(
58 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070059 }
60 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070061}
62
Nico Weber9d8ec5a2015-08-04 13:00:21 -070063DLLEXPORT int STDCALL FPDF_RenderPage_Continue(FPDF_PAGE page,
64 IFSDK_PAUSE* pause) {
Tom Sepezdb0be962015-10-16 14:00:21 -070065 if (!pause || pause->version != 1)
Nico Weber9d8ec5a2015-08-04 13:00:21 -070066 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070067
Tom Sepezdb0be962015-10-16 14:00:21 -070068 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070069 if (!pPage)
70 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070071
weili9f515bc2016-07-24 08:08:24 -070072 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -070073 if (pContext && pContext->m_pRenderer) {
74 IFSDK_PAUSE_Adapter IPauseAdapter(pause);
75 pContext->m_pRenderer->Continue(&IPauseAdapter);
Tom Sepezb3b67622015-10-19 16:20:03 -070076 return CPDF_ProgressiveRenderer::ToFPDFStatus(
77 pContext->m_pRenderer->GetStatus());
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 }
79 return FPDF_RENDER_FAILED;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070080}
81
Nico Weber9d8ec5a2015-08-04 13:00:21 -070082DLLEXPORT void STDCALL FPDF_RenderPage_Close(FPDF_PAGE page) {
Tom Sepezdb0be962015-10-16 14:00:21 -070083 CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070084 if (!pPage)
85 return;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070086
weili9f515bc2016-07-24 08:08:24 -070087 CPDF_PageRenderContext* pContext = pPage->GetRenderContext();
Tom Sepezdfbf8e72015-10-14 14:17:26 -070088 if (!pContext)
89 return;
90
thestig41846a02016-05-26 10:45:30 -070091 pContext->m_pDevice->RestoreState(false);
weili9f515bc2016-07-24 08:08:24 -070092 pPage->SetRenderContext(std::unique_ptr<CPDF_PageRenderContext>());
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070093}