#include "public/fpdfview.h"
#include <memory>
#include <utility>
#include <vector>
#include "build/build_config.h"
#include "core/fpdfapi/page/cpdf_docpagedata.h"
#include "core/fpdfapi/page/cpdf_occontext.h"
#include "core/fpdfapi/page/cpdf_page.h"
#include "core/fpdfapi/page/cpdf_pageimagecache.h"
#include "core/fpdfapi/page/cpdf_pagemodule.h"
#include "core/fpdfapi/parser/cpdf_array.h"
#include "core/fpdfapi/parser/cpdf_dictionary.h"
#include "core/fpdfapi/parser/cpdf_document.h"
#include "core/fpdfapi/parser/cpdf_name.h"
#include "core/fpdfapi/parser/cpdf_parser.h"
#include "core/fpdfapi/parser/cpdf_stream.h"
#include "core/fpdfapi/parser/cpdf_string.h"
#include "core/fpdfapi/parser/fpdf_parser_decode.h"
#include "core/fpdfapi/render/cpdf_docrenderdata.h"
#include "core/fpdfapi/render/cpdf_pagerendercontext.h"
#include "core/fpdfapi/render/cpdf_rendercontext.h"
#include "core/fpdfapi/render/cpdf_renderoptions.h"
#include "core/fpdfdoc/cpdf_nametree.h"
#include "core/fpdfdoc/cpdf_viewerpreferences.h"
#include "core/fxcrt/cfx_read_only_span_stream.h"
#include "core/fxcrt/cfx_timer.h"
#include "core/fxcrt/check_op.h"
#include "core/fxcrt/compiler_specific.h"
#include "core/fxcrt/fx_memcpy_wrappers.h"
#include "core/fxcrt/fx_safe_types.h"
#include "core/fxcrt/fx_stream.h"
#include "core/fxcrt/fx_system.h"
#include "core/fxcrt/numerics/safe_conversions.h"
#include "core/fxcrt/ptr_util.h"
#include "core/fxcrt/span.h"
#include "core/fxcrt/stl_util.h"
#include "core/fxcrt/unowned_ptr.h"
#include "core/fxge/cfx_defaultrenderdevice.h"
#include "core/fxge/cfx_gemodule.h"
#include "core/fxge/cfx_glyphcache.h"
#include "core/fxge/cfx_renderdevice.h"
#include "core/fxge/dib/cfx_dibitmap.h"
#include "fpdfsdk/cpdfsdk_customaccess.h"
#include "fpdfsdk/cpdfsdk_formfillenvironment.h"
#include "fpdfsdk/cpdfsdk_helpers.h"
#include "fpdfsdk/cpdfsdk_pageview.h"
#include "fpdfsdk/cpdfsdk_renderpage.h"
#include "fxjs/ijs_runtime.h"
#include "public/fpdf_formfill.h"
#ifdef PDF_ENABLE_V8
#include "fxjs/cfx_v8_array_buffer_allocator.h"
#endif
#ifdef PDF_ENABLE_XFA
#include "fpdfsdk/fpdfxfa/cpdfxfa_context.h"
#include "fpdfsdk/fpdfxfa/cpdfxfa_page.h"
#endif
#if BUILDFLAG(IS_WIN)
#include "core/fpdfapi/render/cpdf_progressiverenderer.h"
#include "core/fpdfapi/render/cpdf_windowsrenderdevice.h"
#include "public/fpdf_edit.h"
#if defined(PDF_USE_SKIA)
class SkCanvas;
#endif
static_assert(static_cast<int>(WindowsPrintMode::kEmf) == FPDF_PRINTMODE_EMF,
"WindowsPrintMode::kEmf value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kTextOnly) ==
FPDF_PRINTMODE_TEXTONLY,
"WindowsPrintMode::kTextOnly value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kPostScript2) ==
FPDF_PRINTMODE_POSTSCRIPT2,
"WindowsPrintMode::kPostScript2 value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kPostScript3) ==
FPDF_PRINTMODE_POSTSCRIPT3,
"WindowsPrintMode::kPostScript3 value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kPostScript2PassThrough) ==
FPDF_PRINTMODE_POSTSCRIPT2_PASSTHROUGH,
"WindowsPrintMode::kPostScript2PassThrough value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kPostScript3PassThrough) ==
FPDF_PRINTMODE_POSTSCRIPT3_PASSTHROUGH,
"WindowsPrintMode::kPostScript3PassThrough value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kEmfImageMasks) ==
FPDF_PRINTMODE_EMF_IMAGE_MASKS,
"WindowsPrintMode::kEmfImageMasks value mismatch");
static_assert(static_cast<int>(WindowsPrintMode::kPostScript3Type42) ==
FPDF_PRINTMODE_POSTSCRIPT3_TYPE42,
"WindowsPrintMode::kPostScript3Type42 value mismatch");
static_assert(
static_cast<int>(WindowsPrintMode::kPostScript3Type42PassThrough) ==
FPDF_PRINTMODE_POSTSCRIPT3_TYPE42_PASSTHROUGH,
"WindowsPrintMode::kPostScript3Type42PassThrough value mismatch");
#endif
#if defined(PDF_USE_SKIA)
static_assert …;
static_assert …;
#endif
namespace {
bool g_bLibraryInitialized = …;
void SetRendererType(FPDF_RENDERER_TYPE public_type) { … }
void ResetRendererType() { … }
RetainPtr<const CPDF_Object> GetXFAEntryFromDocument(const CPDF_Document* doc) { … }
struct XFAPacket { … };
std::vector<XFAPacket> GetXFAPackets(RetainPtr<const CPDF_Object> xfa_object) { … }
FPDF_DOCUMENT LoadDocumentImpl(RetainPtr<IFX_SeekableReadStream> pFileAccess,
FPDF_BYTESTRING password) { … }
}
FPDF_EXPORT void FPDF_CALLCONV FPDF_InitLibrary() { … }
FPDF_EXPORT void FPDF_CALLCONV
FPDF_InitLibraryWithConfig(const FPDF_LIBRARY_CONFIG* config) { … }
FPDF_EXPORT void FPDF_CALLCONV FPDF_DestroyLibrary() { … }
FPDF_EXPORT void FPDF_CALLCONV FPDF_SetSandBoxPolicy(FPDF_DWORD policy,
FPDF_BOOL enable) { … }
#if BUILDFLAG(IS_WIN)
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_SetPrintMode(int mode) {
if (mode < FPDF_PRINTMODE_EMF ||
mode > FPDF_PRINTMODE_POSTSCRIPT3_TYPE42_PASSTHROUGH) {
return FALSE;
}
g_pdfium_print_mode = static_cast<WindowsPrintMode>(mode);
return TRUE;
}
#endif
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
FPDF_LoadDocument(FPDF_STRING file_path, FPDF_BYTESTRING password) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetFormType(FPDF_DOCUMENT document) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_LoadXFA(FPDF_DOCUMENT document) { … }
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
FPDF_LoadMemDocument(const void* data_buf, int size, FPDF_BYTESTRING password) { … }
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
FPDF_LoadMemDocument64(const void* data_buf,
size_t size,
FPDF_BYTESTRING password) { … }
FPDF_EXPORT FPDF_DOCUMENT FPDF_CALLCONV
FPDF_LoadCustomDocument(FPDF_FILEACCESS* pFileAccess,
FPDF_BYTESTRING password) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetFileVersion(FPDF_DOCUMENT doc,
int* fileVersion) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDF_DocumentHasValidCrossReferenceTable(FPDF_DOCUMENT document) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDF_GetDocPermissions(FPDF_DOCUMENT document) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDF_GetDocUserPermissions(FPDF_DOCUMENT document) { … }
FPDF_EXPORT int FPDF_CALLCONV
FPDF_GetSecurityHandlerRevision(FPDF_DOCUMENT document) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageCount(FPDF_DOCUMENT document) { … }
FPDF_EXPORT FPDF_PAGE FPDF_CALLCONV FPDF_LoadPage(FPDF_DOCUMENT document,
int page_index) { … }
FPDF_EXPORT float FPDF_CALLCONV FPDF_GetPageWidthF(FPDF_PAGE page) { … }
FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageWidth(FPDF_PAGE page) { … }
FPDF_EXPORT float FPDF_CALLCONV FPDF_GetPageHeightF(FPDF_PAGE page) { … }
FPDF_EXPORT double FPDF_CALLCONV FPDF_GetPageHeight(FPDF_PAGE page) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_GetPageBoundingBox(FPDF_PAGE page,
FS_RECTF* rect) { … }
#if BUILDFLAG(IS_WIN)
namespace {
constexpr float kEpsilonSize = 0.01f;
bool IsPageTooSmall(const CPDF_Page* page) {
const CFX_SizeF& page_size = page->GetPageSize();
return page_size.width < kEpsilonSize || page_size.height < kEpsilonSize;
}
bool IsScalingTooSmall(const CFX_Matrix& matrix) {
float horizontal;
float vertical;
if (matrix.a == 0.0f && matrix.d == 0.0f) {
horizontal = matrix.b;
vertical = matrix.c;
} else {
horizontal = matrix.a;
vertical = matrix.d;
}
return fabsf(horizontal) < kEpsilonSize || fabsf(vertical) < kEpsilonSize;
}
RetainPtr<CFX_DIBitmap> GetMaskBitmap(CPDF_Page* pPage,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
RetainPtr<const CFX_DIBitmap> source,
const CFX_FloatRect& mask_box,
FX_RECT* bitmap_area) {
if (IsPageTooSmall(pPage))
return nullptr;
FX_RECT page_rect(start_x, start_y, start_x + size_x, start_y + size_y);
CFX_Matrix matrix = pPage->GetDisplayMatrix(page_rect, rotate);
if (IsScalingTooSmall(matrix))
return nullptr;
*bitmap_area = matrix.TransformRect(mask_box).GetOuterRect();
if (bitmap_area->IsEmpty())
return nullptr;
RetainPtr<CFX_DIBitmap> pDst = pdfium::MakeRetain<CFX_DIBitmap>();
if (!pDst->Create(bitmap_area->Width(), bitmap_area->Height(),
FXDIB_Format::kBgra)) {
return nullptr;
}
pDst->Clear(0x00ffffff);
pDst->TransferBitmap(bitmap_area->Width(), bitmap_area->Height(),
std::move(source), bitmap_area->left, bitmap_area->top);
return pDst;
}
void RenderBitmap(CFX_RenderDevice* device,
RetainPtr<const CFX_DIBitmap> source,
const FX_RECT& mask_area) {
int size_x_bm = mask_area.Width();
int size_y_bm = mask_area.Height();
if (size_x_bm == 0 || size_y_bm == 0)
return;
RetainPtr<CFX_DIBitmap> dest = pdfium::MakeRetain<CFX_DIBitmap>();
if (!dest->Create(size_x_bm, size_y_bm, FXDIB_Format::kBgrx)) {
return;
}
dest->Clear(0xffffffff);
dest->CompositeBitmap(0, 0, size_x_bm, size_y_bm, std::move(source), 0, 0,
BlendMode::kNormal, nullptr, false);
if (device->GetDeviceType() == DeviceType::kPrinter) {
device->StretchDIBits(std::move(dest), mask_area.left, mask_area.top,
size_x_bm, size_y_bm);
} else {
device->SetDIBits(std::move(dest), mask_area.left, mask_area.top);
}
}
}
FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPage(HDC dc,
FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int flags) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!pPage)
return;
auto owned_context = std::make_unique<CPDF_PageRenderContext>();
CPDF_PageRenderContext* context = owned_context.get();
CPDF_Page::RenderContextClearer clearer(pPage);
pPage->SetRenderContext(std::move(owned_context));
const bool bEnableImageMasks =
g_pdfium_print_mode == WindowsPrintMode::kEmfImageMasks;
const bool bNewBitmap = pPage->BackgroundAlphaNeeded() ||
(pPage->HasImageMask() && !bEnableImageMasks) ||
pPage->GetMaskBoundingBoxes().size() > 100;
const bool bHasMask = pPage->HasImageMask() && !bNewBitmap;
auto* render_data = CPDF_DocRenderData::FromDocument(pPage->GetDocument());
if (!bNewBitmap && !bHasMask) {
context->m_pDevice = std::make_unique<CPDF_WindowsRenderDevice>(
dc, render_data->GetPSFontTracker());
CPDFSDK_RenderPageWithContext(context, pPage, start_x, start_y, size_x,
size_y, rotate, flags,
nullptr,
true, nullptr);
return;
}
RetainPtr<CFX_DIBitmap> pBitmap = pdfium::MakeRetain<CFX_DIBitmap>();
CHECK(pBitmap->Create(size_x, size_y, FXDIB_Format::kBgra));
if (!CFX_DefaultRenderDevice::UseSkiaRenderer()) {
pBitmap->Clear(0x00ffffff);
}
auto device = std::make_unique<CFX_DefaultRenderDevice>();
device->Attach(pBitmap);
context->m_pDevice = std::move(device);
if (bHasMask) {
context->m_pOptions = std::make_unique<CPDF_RenderOptions>();
context->m_pOptions->GetOptions().bBreakForMasks = true;
}
CPDFSDK_RenderPageWithContext(context, pPage, start_x, start_y, size_x,
size_y, rotate, flags, nullptr,
true,
nullptr);
if (!bHasMask) {
CPDF_WindowsRenderDevice win_dc(dc, render_data->GetPSFontTracker());
bool bitsStretched = false;
if (win_dc.GetDeviceType() == DeviceType::kPrinter) {
auto dest_bitmap = pdfium::MakeRetain<CFX_DIBitmap>();
if (dest_bitmap->Create(size_x, size_y, FXDIB_Format::kBgrx)) {
fxcrt::Fill(dest_bitmap->GetWritableBuffer().first(pBitmap->GetPitch() *
size_y),
-1);
dest_bitmap->CompositeBitmap(0, 0, size_x, size_y, pBitmap, 0, 0,
BlendMode::kNormal, nullptr, false);
win_dc.StretchDIBits(std::move(dest_bitmap), 0, 0, size_x, size_y);
bitsStretched = true;
}
}
if (!bitsStretched)
win_dc.SetDIBits(std::move(pBitmap), 0, 0);
return;
}
const std::vector<CFX_FloatRect>& mask_boxes = pPage->GetMaskBoundingBoxes();
std::vector<FX_RECT> bitmap_areas(mask_boxes.size());
std::vector<RetainPtr<CFX_DIBitmap>> bitmaps(mask_boxes.size());
for (size_t i = 0; i < mask_boxes.size(); i++) {
bitmaps[i] = GetMaskBitmap(pPage, start_x, start_y, size_x, size_y, rotate,
pBitmap, mask_boxes[i], &bitmap_areas[i]);
context->m_pRenderer->Continue(nullptr);
}
pPage->ClearRenderContext();
owned_context = std::make_unique<CPDF_PageRenderContext>();
context = owned_context.get();
pPage->SetRenderContext(std::move(owned_context));
context->m_pDevice = std::make_unique<CPDF_WindowsRenderDevice>(
dc, render_data->GetPSFontTracker());
context->m_pOptions = std::make_unique<CPDF_RenderOptions>();
context->m_pOptions->GetOptions().bBreakForMasks = true;
CPDFSDK_RenderPageWithContext(context, pPage, start_x, start_y, size_x,
size_y, rotate, flags, nullptr,
true,
nullptr);
for (size_t i = 0; i < mask_boxes.size(); i++) {
if (bitmaps[i]) {
RenderBitmap(context->m_pDevice.get(), std::move(bitmaps[i]),
bitmap_areas[i]);
}
context->m_pRenderer->Continue(nullptr);
}
}
#endif
FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageBitmap(FPDF_BITMAP bitmap,
FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int flags) { … }
FPDF_EXPORT void FPDF_CALLCONV
FPDF_RenderPageBitmapWithMatrix(FPDF_BITMAP bitmap,
FPDF_PAGE page,
const FS_MATRIX* matrix,
const FS_RECTF* clipping,
int flags) { … }
#if defined(PDF_USE_SKIA)
FPDF_EXPORT void FPDF_CALLCONV FPDF_RenderPageSkia(FPDF_SKIA_CANVAS canvas,
FPDF_PAGE page,
int size_x,
int size_y) { … }
#endif
FPDF_EXPORT void FPDF_CALLCONV FPDF_ClosePage(FPDF_PAGE page) { … }
FPDF_EXPORT void FPDF_CALLCONV FPDF_CloseDocument(FPDF_DOCUMENT document) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV FPDF_GetLastError() { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_DeviceToPage(FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
int device_x,
int device_y,
double* page_x,
double* page_y) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDF_PageToDevice(FPDF_PAGE page,
int start_x,
int start_y,
int size_x,
int size_y,
int rotate,
double page_x,
double page_y,
int* device_x,
int* device_y) { … }
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_Create(int width,
int height,
int alpha) { … }
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV FPDFBitmap_CreateEx(int width,
int height,
int format,
void* first_scan,
int stride) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetFormat(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFBitmap_FillRect(FPDF_BITMAP bitmap,
int left,
int top,
int width,
int height,
FPDF_DWORD color) { … }
FPDF_EXPORT void* FPDF_CALLCONV FPDFBitmap_GetBuffer(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetWidth(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetHeight(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDFBitmap_GetStride(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT void FPDF_CALLCONV FPDFBitmap_Destroy(FPDF_BITMAP bitmap) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDF_GetPageSizeByIndexF(FPDF_DOCUMENT document,
int page_index,
FS_SIZEF* size) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetPageSizeByIndex(FPDF_DOCUMENT document,
int page_index,
double* width,
double* height) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDF_VIEWERREF_GetPrintScaling(FPDF_DOCUMENT document) { … }
FPDF_EXPORT int FPDF_CALLCONV
FPDF_VIEWERREF_GetNumCopies(FPDF_DOCUMENT document) { … }
FPDF_EXPORT FPDF_PAGERANGE FPDF_CALLCONV
FPDF_VIEWERREF_GetPrintPageRange(FPDF_DOCUMENT document) { … }
FPDF_EXPORT size_t FPDF_CALLCONV
FPDF_VIEWERREF_GetPrintPageRangeCount(FPDF_PAGERANGE pagerange) { … }
FPDF_EXPORT int FPDF_CALLCONV
FPDF_VIEWERREF_GetPrintPageRangeElement(FPDF_PAGERANGE pagerange,
size_t index) { … }
FPDF_EXPORT FPDF_DUPLEXTYPE FPDF_CALLCONV
FPDF_VIEWERREF_GetDuplex(FPDF_DOCUMENT document) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDF_VIEWERREF_GetName(FPDF_DOCUMENT document,
FPDF_BYTESTRING key,
char* buffer,
unsigned long length) { … }
FPDF_EXPORT FPDF_DWORD FPDF_CALLCONV
FPDF_CountNamedDests(FPDF_DOCUMENT document) { … }
FPDF_EXPORT FPDF_DEST FPDF_CALLCONV
FPDF_GetNamedDestByName(FPDF_DOCUMENT document, FPDF_BYTESTRING name) { … }
#ifdef PDF_ENABLE_V8
FPDF_EXPORT const char* FPDF_CALLCONV FPDF_GetRecommendedV8Flags() { … }
FPDF_EXPORT void* FPDF_CALLCONV FPDF_GetArrayBufferAllocatorSharedInstance() { … }
#endif
#ifdef PDF_ENABLE_XFA
FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Init(FPDF_BSTR* bstr) { … }
FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Set(FPDF_BSTR* bstr,
const char* cstr,
int length) { … }
FPDF_EXPORT FPDF_RESULT FPDF_CALLCONV FPDF_BStr_Clear(FPDF_BSTR* bstr) { … }
#endif
FPDF_EXPORT FPDF_DEST FPDF_CALLCONV FPDF_GetNamedDest(FPDF_DOCUMENT document,
int index,
void* buffer,
long* buflen) { … }
FPDF_EXPORT int FPDF_CALLCONV FPDF_GetXFAPacketCount(FPDF_DOCUMENT document) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDF_GetXFAPacketName(FPDF_DOCUMENT document,
int index,
void* buffer,
unsigned long buflen) { … }
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDF_GetXFAPacketContent(FPDF_DOCUMENT document,
int index,
void* buffer,
unsigned long buflen,
unsigned long* out_buflen) { … }
FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDF_GetTrailerEnds(FPDF_DOCUMENT document,
unsigned int* buffer,
unsigned long length) { … }