Skip to content

Commit

Permalink
Only allow images to exist on a single page
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreRenaud committed Sep 26, 2023
1 parent 0fc49ef commit 6e439bf
Showing 1 changed file with 27 additions and 13 deletions.
40 changes: 27 additions & 13 deletions pdfgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,10 @@ struct pdf_object {
struct pdf_object *parent;
struct flexarray children;
} bookmark;
struct dstr stream;
struct {
struct pdf_object *page;
struct dstr stream;
} stream;
struct {
float width;
float height;
Expand Down Expand Up @@ -640,7 +643,7 @@ static void pdf_object_destroy(struct pdf_object *object)
switch (object->type) {
case OBJ_stream:
case OBJ_image:
dstr_free(&object->stream);
dstr_free(&object->stream.stream);
break;
case OBJ_page:
flexarray_clear(&object->page.children);
Expand Down Expand Up @@ -926,7 +929,8 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
switch (object->type) {
case OBJ_stream:
case OBJ_image: {
fwrite(dstr_data(&object->stream), dstr_len(&object->stream), 1, fp);
fwrite(dstr_data(&object->stream.stream),
dstr_len(&object->stream.stream), 1, fp);
break;
}
case OBJ_info: {
Expand All @@ -951,7 +955,7 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)

case OBJ_page: {
struct pdf_object *pages = pdf_find_first_object(pdf, OBJ_pages);
struct pdf_object *image = pdf_find_first_object(pdf, OBJ_image);
bool printed_xobjects = false;

fprintf(fp,
"<<\r\n"
Expand All @@ -975,13 +979,19 @@ static int pdf_save_object(struct pdf_doc *pdf, FILE *fp, int index)
}
fprintf(fp, " >>\r\n");

if (image) {
fprintf(fp, " /XObject <<");
for (; image; image = image->next)
for (struct pdf_object *image = pdf_find_first_object(pdf, OBJ_image);
image; image = image->next) {
if (image->stream.page == object) {
if (!printed_xobjects) {
fprintf(fp, " /XObject <<");
printed_xobjects = true;
}
fprintf(fp, " /Image%d %d 0 R ", image->index,
image->index);
fprintf(fp, " >>\r\n");
}
}
if (printed_xobjects)
fprintf(fp, " >>\r\n");
fprintf(fp, " >>\r\n");

fprintf(fp, " /Contents [\r\n");
Expand Down Expand Up @@ -1258,9 +1268,9 @@ static int pdf_add_stream(struct pdf_doc *pdf, struct pdf_object *page,
if (!obj)
return pdf->errval;

dstr_printf(&obj->stream, "<< /Length %zu >>stream\r\n", len);
dstr_append_data(&obj->stream, buffer, len);
dstr_append(&obj->stream, "\r\nendstream\r\n");
dstr_printf(&obj->stream.stream, "<< /Length %zu >>stream\r\n", len);
dstr_append_data(&obj->stream.stream, buffer, len);
dstr_append(&obj->stream.stream, "\r\nendstream\r\n");

return flexarray_append(&page->page.children, obj);
}
Expand Down Expand Up @@ -3206,7 +3216,7 @@ static pdf_object *pdf_add_raw_grayscale8(struct pdf_doc *pdf,
dstr_free(&str);
return NULL;
}
obj->stream = str;
obj->stream.stream = str;

return obj;
}
Expand Down Expand Up @@ -3249,7 +3259,7 @@ static struct pdf_object *pdf_add_raw_rgb24(struct pdf_doc *pdf,
dstr_free(&str);
return NULL;
}
obj->stream = str;
obj->stream.stream = str;

return obj;
}
Expand Down Expand Up @@ -3374,6 +3384,10 @@ static int pdf_add_image(struct pdf_doc *pdf, struct pdf_object *page,
{
int ret;
struct dstr str = INIT_DSTR;
if (image->stream.page != NULL) {
return pdf_set_err(pdf, -EEXIST, "image already on a page");
}
image->stream.page = page;

dstr_append(&str, "q ");
dstr_printf(&str, "%f 0 0 %f %f %f cm ", width, height, x, y);
Expand Down

0 comments on commit 6e439bf

Please sign in to comment.