Fix some issues with the core file writing code.

Add test images to the unit tests.
This commit is contained in:
Michael R Sweet 2021-05-28 10:41:21 -04:00
parent 9cf024c1ce
commit fb853dadda
No known key found for this signature in database
GPG Key ID: 999559A027815955
7 changed files with 103 additions and 15 deletions

View File

@ -16,7 +16,7 @@ ARFLAGS = cr
CC = cc CC = cc
CFLAGS = CFLAGS =
CODESIGN_IDENTITY = Developer ID CODESIGN_IDENTITY = Developer ID
COMMONFLAGS = -Os -g COMMONFLAGS = -g
CPPFLAGS = CPPFLAGS =
DESTDIR = $(DSTROOT) DESTDIR = $(DSTROOT)
DSO = cc DSO = cc

View File

@ -827,11 +827,35 @@ pdfioPageDictAddImage(
const char *name, // I - Image name const char *name, // I - Image name
pdfio_obj_t *obj) // I - Image object pdfio_obj_t *obj) // I - Image object
{ {
(void)dict; pdfio_dict_t *resources; // Resource dictionary
(void)name; pdfio_dict_t *xobject; // XObject dictionary
(void)obj;
return (false);
// Range check input...
if (!dict || !name || !obj)
return (false);
// Get the images dictionary...
if ((resources = pdfioDictGetDict(dict, "Resources")) == NULL)
{
if ((resources = pdfioDictCreate(dict->pdf)) == NULL)
return (false);
if (!pdfioDictSetDict(dict, "Resources", resources))
return (false);
}
if ((xobject = pdfioDictGetDict(resources, "XObject")) == NULL)
{
if ((xobject = pdfioDictCreate(dict->pdf)) == NULL)
return (false);
if (!pdfioDictSetDict(resources, "XObject", xobject))
return (false);
}
// Now set the image reference in the images resource dictionary and return...
return (pdfioDictSetObject(xobject, name, obj));
} }

View File

@ -801,7 +801,7 @@ _pdfioDictWrite(pdfio_dict_t *dict, // I - Dictionary
if (length && !strcmp(pair->key, "Length") && pair->value.type == PDFIO_VALTYPE_NUMBER && pair->value.value.number <= 0.0f) if (length && !strcmp(pair->key, "Length") && pair->value.type == PDFIO_VALTYPE_NUMBER && pair->value.value.number <= 0.0f)
{ {
// Writing an object dictionary with an undefined length // Writing an object dictionary with an undefined length
*length = _pdfioFileTell(pdf); *length = _pdfioFileTell(pdf) + 1;
if (!_pdfioFilePuts(pdf, " 9999999999")) if (!_pdfioFilePuts(pdf, " 9999999999"))
return (false); return (false);
} }

View File

@ -180,6 +180,8 @@ pdfioFileCreate(
pdf->mode = _PDFIO_MODE_WRITE; pdf->mode = _PDFIO_MODE_WRITE;
pdf->error_cb = error_cb; pdf->error_cb = error_cb;
pdf->error_data = error_data; pdf->error_data = error_data;
pdf->bufptr = pdf->buffer;
pdf->bufend = pdf->buffer + sizeof(pdf->buffer);
if (media_box) if (media_box)
{ {
@ -348,7 +350,7 @@ pdfioFileCreatePage(pdfio_file_t *pdf, // I - PDF file
// Create a contents object to hold the contents of the page... // Create a contents object to hold the contents of the page...
contents_dict = pdfioDictCreate(pdf); contents_dict = pdfioDictCreate(pdf);
pdfioDictSetName(contents_dict, "Filter", "FlateDecode"); // pdfioDictSetName(contents_dict, "Filter", "FlateDecode");
contents = pdfioFileCreateObject(pdf, contents_dict); contents = pdfioFileCreateObject(pdf, contents_dict);
@ -375,7 +377,8 @@ pdfioFileCreatePage(pdfio_file_t *pdf, // I - PDF file
pdf->pages[pdf->num_pages ++] = page; pdf->pages[pdf->num_pages ++] = page;
// Create the contents stream... // Create the contents stream...
return (pdfioObjCreateStream(contents, PDFIO_FILTER_FLATE)); // return (pdfioObjCreateStream(contents, PDFIO_FILTER_FLATE));
return (pdfioObjCreateStream(contents, PDFIO_FILTER_NONE));
} }

BIN
testfiles/color.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 116 KiB

BIN
testfiles/gray.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 131 KiB

View File

@ -12,6 +12,7 @@
// //
#include "pdfio-private.h" #include "pdfio-private.h"
#include "pdfio-content.h"
// //
@ -23,7 +24,7 @@ static int do_unit_tests(void);
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error); static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
static ssize_t token_consume_cb(const char **s, size_t bytes); static ssize_t token_consume_cb(const char **s, size_t bytes);
static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes); static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes);
static int write_page(pdfio_file_t *pdf, int number); static int write_page(pdfio_file_t *pdf, int number, pdfio_obj_t *image);
// //
@ -153,6 +154,8 @@ do_unit_tests(void)
_pdfio_token_t tb; // Token buffer _pdfio_token_t tb; // Token buffer
const char *s; // String buffer const char *s; // String buffer
_pdfio_value_t value; // Value _pdfio_value_t value; // Value
pdfio_obj_t *color_jpg, // color.jpg image
*gray_jpg; // gray.jpg image
static const char *complex_dict = // Complex dictionary value static const char *complex_dict = // Complex dictionary value
"<</Annots 5457 0 R/Contents 5469 0 R/CropBox[0 0 595.4 842]/Group 725 0 R" "<</Annots 5457 0 R/Contents 5469 0 R/CropBox[0 0 595.4 842]/Group 725 0 R"
"/MediaBox[0 0 595.4 842]/Parent 23513 0 R/Resources<</ColorSpace<<" "/MediaBox[0 0 595.4 842]/Parent 23513 0 R/Resources<</ColorSpace<<"
@ -166,6 +169,8 @@ do_unit_tests(void)
"/Tabs/S/Type/Page>>"; "/Tabs/S/Type/Page>>";
setbuf(stdout, NULL);
// First open the test PDF file... // First open the test PDF file...
fputs("pdfioFileOpen(\"testfiles/testpdfio.pdf\"): ", stdout); fputs("pdfioFileOpen(\"testfiles/testpdfio.pdf\"): ", stdout);
if ((pdf = pdfioFileOpen("testfiles/testpdfio.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL) if ((pdf = pdfioFileOpen("testfiles/testpdfio.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL)
@ -195,9 +200,21 @@ do_unit_tests(void)
return (1); return (1);
// Create a new PDF file... // Create a new PDF file...
snprintf(filename, sizeof(filename), "testpdfio-%d.pdf", (int)getpid()); fputs("pdfioFileCreate(\"testpdfio-out.pdf\", ...): ", stdout);
printf("pdfioFileCreate(\"%s\", ...): ", filename); if ((pdf = pdfioFileCreate("testpdfio-out.pdf", NULL, NULL, NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL)
if ((pdf = pdfioFileCreate(filename, NULL, NULL, NULL, (pdfio_error_cb_t)error_cb, &error)) != NULL) puts("PASS");
else
return (1);
// Create some image objects...
fputs("pdfioFileCreateImageObject(\"testfiles/color.jpg\"): ", stdout);
if ((color_jpg = pdfioFileCreateImageObject(pdf, "testfiles/color.jpg", true)) != NULL)
puts("PASS");
else
return (1);
fputs("pdfioFileCreateImageObject(\"testfiles/gray.jpg\"): ", stdout);
if ((gray_jpg = pdfioFileCreateImageObject(pdf, "testfiles/gray.jpg", true)) != NULL)
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -205,7 +222,7 @@ do_unit_tests(void)
// Write a few pages... // Write a few pages...
for (i = 1; i < 18; i ++) for (i = 1; i < 18; i ++)
{ {
if (write_page(pdf, i)) if (write_page(pdf, i, (i & 1) ? color_jpg : gray_jpg))
return (1); return (1);
} }
@ -298,15 +315,29 @@ token_peek_cb(const char **s, // IO - Test string
static int // O - 1 on failure, 0 on success static int // O - 1 on failure, 0 on success
write_page(pdfio_file_t *pdf, // I - PDF file write_page(pdfio_file_t *pdf, // I - PDF file
int number) // I - Page number int number, // I - Page number
pdfio_obj_t *image) // I - Image to draw
{ {
// TODO: Add font object support... // TODO: Add font object support...
pdfio_dict_t *dict; // Page dictionary
pdfio_stream_t *st; // Page contents stream pdfio_stream_t *st; // Page contents stream
fputs("pdfioDictCreate: ", stdout);
if ((dict = pdfioDictCreate(pdf)) != NULL)
puts("PASS");
else
return (1);
fputs("pdfioPageDictAddImage: ", stdout);
if (pdfioPageDictAddImage(dict, "IM1", image))
puts("PASS");
else
return (1);
printf("pdfioFileCreatePage(%d): ", number); printf("pdfioFileCreatePage(%d): ", number);
if ((st = pdfioFileCreatePage(pdf, NULL)) != NULL) if ((st = pdfioFileCreatePage(pdf, dict)) != NULL)
puts("PASS"); puts("PASS");
else else
return (1); return (1);
@ -319,6 +350,36 @@ write_page(pdfio_file_t *pdf, // I - PDF file
else else
return (1); return (1);
fputs("pdfioContentSave(): ", stdout);
if (pdfioContentSave(st))
puts("PASS");
else
return (1);
fputs("pdfioContentMatrixScale(72.0, 72.0): ", stdout);
if (pdfioContentMatrixScale(st, 72.0f, 72.0f))
puts("PASS");
else
return (1);
// fputs("pdfioContentTranslate(144.0, 144.0): ", stdout);
// if (pdfioContentMatrixTranslate(st, 144.0f, 144.0f))
// puts("PASS");
// else
// return (1);
fputs("pdfioContentDrawImage(\"IM1\"): ", stdout);
if (pdfioContentDrawImage(st, "IM1"))
puts("PASS");
else
return (1);
fputs("pdfioContentRestore(): ", stdout);
if (pdfioContentRestore(st))
puts("PASS");
else
return (1);
fputs("pdfioStreamClose: ", stdout); fputs("pdfioStreamClose: ", stdout);
if (pdfioStreamClose(st)) if (pdfioStreamClose(st))
puts("PASS"); puts("PASS");