pdfio/pdfio-page.c

124 lines
2.8 KiB
C
Raw Permalink Normal View History

2021-04-10 14:00:52 +02:00
//
// PDF page functions for PDFio.
2021-04-10 14:00:52 +02:00
//
// Copyright © 2021-2022 by Michael R Sweet.
2021-04-10 14:00:52 +02:00
//
// Licensed under Apache License v2.0. See the file "LICENSE" for more
// information.
//
#include "pdfio-private.h"
2021-04-17 02:41:46 +02:00
//
// Local functions...
//
static _pdfio_value_t *get_contents(pdfio_obj_t *page);
2021-04-17 02:41:46 +02:00
//
2021-04-28 03:22:34 +02:00
// 'pdfioPageCopy()' - Copy a page to a PDF file.
2021-04-17 02:41:46 +02:00
//
bool // O - `true` on success, `false` on failure
2021-04-28 03:22:34 +02:00
pdfioPageCopy(pdfio_file_t *pdf, // I - PDF file
pdfio_obj_t *srcpage) // I - Source page
2021-04-17 02:41:46 +02:00
{
pdfio_obj_t *dstpage; // Destination page object
2021-04-28 03:22:34 +02:00
PDFIO_DEBUG("pdfioPageCopy(pdf=%p, srcpage=%p(%p))\n", pdf, srcpage, srcpage ? srcpage->pdf : NULL);
// Range check input
if (!pdf || !srcpage || srcpage->value.type != PDFIO_VALTYPE_DICT)
{
if (pdf)
{
if (!srcpage)
_pdfioFileError(pdf, "NULL page object specified.");
else
_pdfioFileError(pdf, "Object is not a page.");
}
return (false);
}
// Copy the page object and add it to the pages array...
if ((dstpage = pdfioObjCopy(pdf, srcpage)) == NULL)
return (false);
else
return (_pdfioFileAddPage(pdf, dstpage));
2021-04-17 02:41:46 +02:00
}
//
// 'pdfioPageGetNumStreams()' - Get the number of content streams for a page object.
//
size_t // O - Number of streams
pdfioPageGetNumStreams(
pdfio_obj_t *page) // I - Page object
{
_pdfio_value_t *contents = get_contents(page);
// Contents value
if (!contents)
return (0);
else if (contents->type == PDFIO_VALTYPE_ARRAY)
return (pdfioArrayGetSize(contents->value.array));
else
return (1);
}
//
// 'pdfioPageOpenStream()' - Open a content stream for a page.
//
pdfio_stream_t * // O - Stream
pdfioPageOpenStream(
pdfio_obj_t *page, // I - Page object
size_t n, // I - Stream index (0-based)
bool decode) // I - `true` to decode/decompress stream
{
_pdfio_value_t *contents = get_contents(page);
// Contents value
if (!contents)
return (NULL);
else if (contents->type == PDFIO_VALTYPE_ARRAY && n < pdfioArrayGetSize(contents->value.array))
return (pdfioObjOpenStream(pdfioArrayGetObj(contents->value.array, n), decode));
else if (n)
return (NULL);
else
return (pdfioObjOpenStream(pdfioFileFindObj(page->pdf, contents->value.indirect.number), decode));
}
//
// 'get_contents()' - Get a page's Contents value.
//
static _pdfio_value_t * // O - Value or NULL on error
get_contents(pdfio_obj_t *page) // I - Page object
{
// Range check input...
if (!page)
return (NULL);
// Load the page object as needed...
if (page->value.type == PDFIO_VALTYPE_NONE)
{
if (!_pdfioObjLoad(page))
return (NULL);
}
if (page->value.type != PDFIO_VALTYPE_DICT)
return (NULL);
return (_pdfioDictGetValue(page->value.value.dict, "Contents"));
}