1 #include "pdf-interpret-imp.h"
3 typedef struct pdf_buffer_state_s
12 put_hexstring(pdf_csi *csi, fz_output *out)
17 for (i = 0; i < csi->string_len; i++)
18 fz_printf(out, "%02x", csi->string[i]);
23 put_string(pdf_csi *csi, fz_output *out)
27 for (i=0; i < csi->string_len; i++)
28 if (csi->string[i] < 32 || csi->string[i] >= 127)
30 if (i < csi->string_len)
31 put_hexstring(csi, out);
35 for (i = 0; i < csi->string_len; i++)
37 char c = csi->string[i];
41 fz_printf(out, "\\(");
44 fz_printf(out, "\\)");
47 fz_printf(out, "\\\\");
50 fz_printf(out, "%c", csi->string[i]);
59 put_string_or_obj(pdf_csi *csi, fz_output *out)
64 pdf_output_obj(out, csi->obj, 1);
68 pdf_buffer_dquote(pdf_csi *csi, void *state_)
70 pdf_buffer_state *state = (pdf_buffer_state *)state_;
72 fz_printf(state->out, "%f %f ", csi->stack[0], csi->stack[1]);
73 put_string_or_obj(csi, state->out);
74 fz_printf(state->out, " \"\n");
78 pdf_buffer_squote(pdf_csi *csi, void *state_)
80 pdf_buffer_state *state = (pdf_buffer_state *)state_;
82 put_string_or_obj(csi, state->out);
83 fz_printf(state->out, " \'\n");
87 pdf_buffer_B(pdf_csi *csi, void *state_)
89 pdf_buffer_state *state = (pdf_buffer_state *)state_;
91 fz_printf(state->out, "B\n");
95 pdf_buffer_Bstar(pdf_csi *csi, void *state_)
97 pdf_buffer_state *state = (pdf_buffer_state *)state_;
99 fz_printf(state->out, "B*\n");
103 pdf_buffer_BDC(pdf_csi *csi, void *state_)
105 pdf_buffer_state *state = (pdf_buffer_state *)state_;
107 fz_printf(state->out, "/%s ", csi->name);
108 pdf_output_obj(state->out, csi->obj, 1);
109 fz_printf(state->out, " BDC\n");
113 pdf_buffer_BI(pdf_csi *csi, void *state_)
115 pdf_buffer_state *state = (pdf_buffer_state *)state_;
118 fz_compressed_buffer *cbuf;
123 fz_context *ctx = csi->doc->ctx;
125 if (csi->img == NULL)
127 cbuf = csi->img->buffer;
130 buffer = cbuf->buffer;
134 /* Tweak the /Filter entry in csi->obj to match the buffer params */
135 switch (cbuf->params.type)
142 match = "CCITTFaxDecode";
150 match = "RunLengthDecode";
154 match = "FlateDecode";
162 fz_warn(ctx, "Unsupported type (%d) of inline image", cbuf->params.type);
166 filter = pdf_dict_gets(csi->obj, "Filter");
168 filter = pdf_dict_gets(csi->obj, "F");
171 /* Remove any filter entry (e.g. Ascii85Decode) */
174 pdf_dict_dels(csi->obj, "Filter");
175 pdf_dict_dels(csi->obj, "F");
177 pdf_dict_dels(csi->obj, "DecodeParms");
178 pdf_dict_dels(csi->obj, "DP");
180 else if (pdf_is_array(filter))
182 int l = pdf_array_len(filter);
183 pdf_obj *o = (l == 0 ? NULL : pdf_array_get(filter, l-1));
184 const char *fil = pdf_to_name(o);
186 if (l == 0 || (strcmp(fil, match) && strcmp(fil, match2)))
188 fz_warn(ctx, "Unexpected Filter configuration in inline image");
191 pdf_dict_puts(csi->obj, "F", o);
193 o = pdf_dict_gets(csi->obj, "DecodeParms");
195 o = pdf_dict_gets(csi->obj, "DP");
198 o = pdf_array_get(o, l-1);
200 pdf_dict_puts(csi->obj, "DP", o);
202 pdf_dict_dels(csi->obj, "DP");
203 pdf_dict_dels(csi->obj, "DecodeParms");
208 /* It's a singleton. It must be correct */
211 fz_printf(state->out, "BI\n");
213 len = pdf_dict_len(csi->obj);
214 for (i = 0; i < len; i++)
216 pdf_output_obj(state->out, pdf_dict_get_key(csi->obj, i), 1);
217 pdf_output_obj(state->out, pdf_dict_get_val(csi->obj, i), 1);
219 fz_printf(state->out, "ID\n");
221 buffer = csi->img->buffer->buffer;
224 for (i = 0; i < len; i++)
226 fz_printf(state->out, "%c", data[i]);
229 fz_printf(state->out, "\nEI\n");
233 pdf_buffer_BMC(pdf_csi *csi, void *state_)
235 pdf_buffer_state *state = (pdf_buffer_state *)state_;
237 fz_printf(state->out, "/%s BMC\n", csi->name);
241 pdf_buffer_BT(pdf_csi *csi, void *state_)
243 pdf_buffer_state *state = (pdf_buffer_state *)state_;
245 fz_printf(state->out, "BT\n");
249 pdf_buffer_BX(pdf_csi *csi, void *state_)
251 pdf_buffer_state *state = (pdf_buffer_state *)state_;
253 fz_printf(state->out, "BX\n");
257 pdf_buffer_CS(pdf_csi *csi, void *state_)
259 pdf_buffer_state *state = (pdf_buffer_state *)state_;
261 fz_printf(state->out, "/%s CS\n", csi->name);
265 pdf_buffer_DP(pdf_csi *csi, void *state_)
267 pdf_buffer_state *state = (pdf_buffer_state *)state_;
269 fz_printf(state->out, "/%s ", csi->name);
270 pdf_output_obj(state->out, csi->obj, 1);
271 fz_printf(state->out, " DP\n");
275 pdf_buffer_EMC(pdf_csi *csi, void *state_)
277 pdf_buffer_state *state = (pdf_buffer_state *)state_;
279 fz_printf(state->out, "EMC\n");
283 pdf_buffer_ET(pdf_csi *csi, void *state_)
285 pdf_buffer_state *state = (pdf_buffer_state *)state_;
287 fz_printf(state->out, "ET\n");
291 pdf_buffer_EX(pdf_csi *csi, void *state_)
293 pdf_buffer_state *state = (pdf_buffer_state *)state_;
295 fz_printf(state->out, "EX\n");
299 pdf_buffer_F(pdf_csi *csi, void *state_)
301 pdf_buffer_state *state = (pdf_buffer_state *)state_;
303 fz_printf(state->out, "F\n");
307 pdf_buffer_G(pdf_csi *csi, void *state_)
309 pdf_buffer_state *state = (pdf_buffer_state *)state_;
311 fz_printf(state->out, "%f G\n", csi->stack[0]);
315 pdf_buffer_J(pdf_csi *csi, void *state_)
317 pdf_buffer_state *state = (pdf_buffer_state *)state_;
319 fz_printf(state->out, "%d J\n", (int)csi->stack[0]);
323 pdf_buffer_K(pdf_csi *csi, void *state_)
325 pdf_buffer_state *state = (pdf_buffer_state *)state_;
327 fz_printf(state->out, "%f %f %f %f K\n", csi->stack[0],
328 csi->stack[1], csi->stack[2], csi->stack[3]);
332 pdf_buffer_M(pdf_csi *csi, void *state_)
334 pdf_buffer_state *state = (pdf_buffer_state *)state_;
336 fz_printf(state->out, "%f M\n", csi->stack[0]);
340 pdf_buffer_MP(pdf_csi *csi, void *state_)
342 pdf_buffer_state *state = (pdf_buffer_state *)state_;
344 fz_printf(state->out, "/%s MP\n", csi->name);
348 pdf_buffer_Q(pdf_csi *csi, void *state_)
350 pdf_buffer_state *state = (pdf_buffer_state *)state_;
352 fz_printf(state->out, "Q\n");
356 pdf_buffer_RG(pdf_csi *csi, void *state_)
358 pdf_buffer_state *state = (pdf_buffer_state *)state_;
360 fz_printf(state->out, "%f %f %f RG\n", csi->stack[0], csi->stack[1], csi->stack[2]);
364 pdf_buffer_S(pdf_csi *csi, void *state_)
366 pdf_buffer_state *state = (pdf_buffer_state *)state_;
368 fz_printf(state->out, "S\n");
372 pdf_buffer_SC(pdf_csi *csi, void *state_)
374 pdf_buffer_state *state = (pdf_buffer_state *)state_;
377 for (i = 0; i < csi->top; i++)
378 fz_printf(state->out, "%f ", csi->stack[i]);
379 fz_printf(state->out, "SC\n");
383 pdf_buffer_SCN(pdf_csi *csi, void *state_)
385 pdf_buffer_state *state = (pdf_buffer_state *)state_;
388 for (i = 0; i < csi->top; i++)
389 fz_printf(state->out, "%f ", csi->stack[i]);
391 fz_printf(state->out, "/%s ", csi->name);
392 fz_printf(state->out, "SCN\n");
396 pdf_buffer_Tstar(pdf_csi *csi, void *state_)
398 pdf_buffer_state *state = (pdf_buffer_state *)state_;
400 fz_printf(state->out, "T*\n");
404 pdf_buffer_TD(pdf_csi *csi, void *state_)
406 pdf_buffer_state *state = (pdf_buffer_state *)state_;
408 fz_printf(state->out, "%f %f TD\n", csi->stack[0], csi->stack[1]);
412 pdf_buffer_TJ(pdf_csi *csi, void *state_)
414 pdf_buffer_state *state = (pdf_buffer_state *)state_;
416 pdf_output_obj(state->out, csi->obj, 1);
417 fz_printf(state->out, " TJ\n");
421 pdf_buffer_TL(pdf_csi *csi, void *state_)
423 pdf_buffer_state *state = (pdf_buffer_state *)state_;
425 fz_printf(state->out, "%f TL\n", csi->stack[0]);
429 pdf_buffer_Tc(pdf_csi *csi, void *state_)
431 pdf_buffer_state *state = (pdf_buffer_state *)state_;
433 fz_printf(state->out, "%f Tc\n", csi->stack[0]);
437 pdf_buffer_Td(pdf_csi *csi, void *state_)
439 pdf_buffer_state *state = (pdf_buffer_state *)state_;
441 fz_printf(state->out, "%f %f Td\n", csi->stack[0], csi->stack[1]);
445 pdf_buffer_Tj(pdf_csi *csi, void *state_)
447 pdf_buffer_state *state = (pdf_buffer_state *)state_;
449 put_string_or_obj(csi, state->out);
450 fz_printf(state->out, " Tj\n");
454 pdf_buffer_Tm(pdf_csi *csi, void *state_)
456 pdf_buffer_state *state = (pdf_buffer_state *)state_;
458 fz_printf(state->out, "%f %f %f %f %f %f Tm\n",
459 csi->stack[0], csi->stack[1], csi->stack[2],
460 csi->stack[3], csi->stack[4], csi->stack[5]);
464 pdf_buffer_Tr(pdf_csi *csi, void *state_)
466 pdf_buffer_state *state = (pdf_buffer_state *)state_;
468 fz_printf(state->out, "%f Tr\n", csi->stack[0]);
472 pdf_buffer_Ts(pdf_csi *csi, void *state_)
474 pdf_buffer_state *state = (pdf_buffer_state *)state_;
476 fz_printf(state->out, "%f Ts\n", csi->stack[0]);
480 pdf_buffer_Tw(pdf_csi *csi, void *state_)
482 pdf_buffer_state *state = (pdf_buffer_state *)state_;
484 fz_printf(state->out, "%f Tw\n", csi->stack[0]);
488 pdf_buffer_Tz(pdf_csi *csi, void *state_)
490 pdf_buffer_state *state = (pdf_buffer_state *)state_;
492 fz_printf(state->out, "%f Tz\n", csi->stack[0]);
496 pdf_buffer_W(pdf_csi *csi, void *state_)
498 pdf_buffer_state *state = (pdf_buffer_state *)state_;
500 fz_printf(state->out, "W\n");
504 pdf_buffer_Wstar(pdf_csi *csi, void *state_)
506 pdf_buffer_state *state = (pdf_buffer_state *)state_;
508 fz_printf(state->out, "W*\n");
512 pdf_buffer_b(pdf_csi *csi, void *state_)
514 pdf_buffer_state *state = (pdf_buffer_state *)state_;
516 fz_printf(state->out, "b\n");
520 pdf_buffer_bstar(pdf_csi *csi, void *state_)
522 pdf_buffer_state *state = (pdf_buffer_state *)state_;
524 fz_printf(state->out, "b*\n");
528 pdf_buffer_c(pdf_csi *csi, void *state_)
530 pdf_buffer_state *state = (pdf_buffer_state *)state_;
532 fz_printf(state->out, "%f %f %f %f %f %f c\n",
533 csi->stack[0], csi->stack[1], csi->stack[2],
534 csi->stack[3], csi->stack[4], csi->stack[5]);
538 pdf_buffer_cm(pdf_csi *csi, void *state_)
540 pdf_buffer_state *state = (pdf_buffer_state *)state_;
542 fz_printf(state->out, "%f %f %f %f %f %f cm\n",
543 csi->stack[0], csi->stack[1], csi->stack[2],
544 csi->stack[3], csi->stack[4], csi->stack[5]);
548 pdf_buffer_cs(pdf_csi *csi, void *state_)
550 pdf_buffer_state *state = (pdf_buffer_state *)state_;
552 fz_printf(state->out, "/%s cs\n", csi->name);
556 pdf_buffer_d(pdf_csi *csi, void *state_)
558 pdf_buffer_state *state = (pdf_buffer_state *)state_;
560 pdf_output_obj(state->out, csi->obj, 1);
561 fz_printf(state->out, " %f d\n", csi->stack[0]);
565 pdf_buffer_d0(pdf_csi *csi, void *state_)
567 pdf_buffer_state *state = (pdf_buffer_state *)state_;
569 fz_printf(state->out, "%f %f d0\n", csi->stack[0], csi->stack[1]);
573 pdf_buffer_d1(pdf_csi *csi, void *state_)
575 pdf_buffer_state *state = (pdf_buffer_state *)state_;
577 fz_printf(state->out, "%f %f %f %f %f %f d1\n",
578 csi->stack[0], csi->stack[1], csi->stack[2],
579 csi->stack[3], csi->stack[4], csi->stack[5]);
583 pdf_buffer_f(pdf_csi *csi, void *state_)
585 pdf_buffer_state *state = (pdf_buffer_state *)state_;
587 fz_printf(state->out, "f\n");
591 pdf_buffer_fstar(pdf_csi *csi, void *state_)
593 pdf_buffer_state *state = (pdf_buffer_state *)state_;
595 fz_printf(state->out, "f*\n");
599 pdf_buffer_g(pdf_csi *csi, void *state_)
601 pdf_buffer_state *state = (pdf_buffer_state *)state_;
603 fz_printf(state->out, "%f g\n", csi->stack[0]);
607 pdf_buffer_h(pdf_csi *csi, void *state_)
609 pdf_buffer_state *state = (pdf_buffer_state *)state_;
611 fz_printf(state->out, "h\n");
615 pdf_buffer_i(pdf_csi *csi, void *state_)
617 pdf_buffer_state *state = (pdf_buffer_state *)state_;
619 fz_printf(state->out, "%f i\n", csi->stack[0]);
623 pdf_buffer_j(pdf_csi *csi, void *state_)
625 pdf_buffer_state *state = (pdf_buffer_state *)state_;
627 fz_printf(state->out, "%d j\n", (int)csi->stack[0]);
631 pdf_buffer_k(pdf_csi *csi, void *state_)
633 pdf_buffer_state *state = (pdf_buffer_state *)state_;
635 fz_printf(state->out, "%f %f %f %f k\n", csi->stack[0],
636 csi->stack[1], csi->stack[2], csi->stack[3]);
640 pdf_buffer_l(pdf_csi *csi, void *state_)
642 pdf_buffer_state *state = (pdf_buffer_state *)state_;
644 fz_printf(state->out, "%f %f l\n", csi->stack[0], csi->stack[1]);
648 pdf_buffer_m(pdf_csi *csi, void *state_)
650 pdf_buffer_state *state = (pdf_buffer_state *)state_;
652 fz_printf(state->out, "%f %f m\n", csi->stack[0], csi->stack[1]);
656 pdf_buffer_n(pdf_csi *csi, void *state_)
658 pdf_buffer_state *state = (pdf_buffer_state *)state_;
660 fz_printf(state->out, "n\n");
664 pdf_buffer_q(pdf_csi *csi, void *state_)
666 pdf_buffer_state *state = (pdf_buffer_state *)state_;
668 fz_printf(state->out, "q\n");
672 pdf_buffer_re(pdf_csi *csi, void *state_)
674 pdf_buffer_state *state = (pdf_buffer_state *)state_;
676 fz_printf(state->out, "%f %f %f %f re\n", csi->stack[0],
677 csi->stack[1], csi->stack[2], csi->stack[3]);
681 pdf_buffer_rg(pdf_csi *csi, void *state_)
683 pdf_buffer_state *state = (pdf_buffer_state *)state_;
685 fz_printf(state->out, "%f %f %f rg\n",
686 csi->stack[0], csi->stack[1], csi->stack[2]);
690 pdf_buffer_ri(pdf_csi *csi, void *state_)
692 pdf_buffer_state *state = (pdf_buffer_state *)state_;
694 fz_printf(state->out, "/%s ri\n", csi->name);
698 pdf_buffer_s(pdf_csi *csi, void *state_)
700 pdf_buffer_state *state = (pdf_buffer_state *)state_;
702 fz_printf(state->out, "s\n");
706 pdf_buffer_sc(pdf_csi *csi, void *state_)
708 pdf_buffer_state *state = (pdf_buffer_state *)state_;
711 for (i = 0; i < csi->top; i++)
712 fz_printf(state->out, "%f ", csi->stack[i]);
713 fz_printf(state->out, "sc\n");
717 pdf_buffer_scn(pdf_csi *csi, void *state_)
719 pdf_buffer_state *state = (pdf_buffer_state *)state_;
722 for (i = 0; i < csi->top; i++)
723 fz_printf(state->out, "%f ", csi->stack[i]);
725 fz_printf(state->out, "/%s ", csi->name);
726 fz_printf(state->out, "scn\n");
730 pdf_buffer_v(pdf_csi *csi, void *state_)
732 pdf_buffer_state *state = (pdf_buffer_state *)state_;
734 fz_printf(state->out, "%f %f %f %f v\n", csi->stack[0],
735 csi->stack[1], csi->stack[2], csi->stack[3]);
739 pdf_buffer_w(pdf_csi *csi, void *state_)
741 pdf_buffer_state *state = (pdf_buffer_state *)state_;
743 fz_printf(state->out, "%f w\n", csi->stack[0]);
747 pdf_buffer_y(pdf_csi *csi, void *state_)
749 pdf_buffer_state *state = (pdf_buffer_state *)state_;
751 fz_printf(state->out, "%f %f %f %f y\n", csi->stack[0],
752 csi->stack[1], csi->stack[2], csi->stack[3]);
756 pdf_buffer_Do(pdf_csi *csi, void *state_)
758 pdf_buffer_state *state = (pdf_buffer_state *)state_;
760 fz_printf(state->out, "/%s Do\n", csi->name);
764 pdf_buffer_Tf(pdf_csi *csi, void *state_)
766 pdf_buffer_state *state = (pdf_buffer_state *)state_;
768 fz_printf(state->out, "/%s %f Tf\n", csi->name, csi->stack[0]);
772 pdf_buffer_gs(pdf_csi *csi, void *state_)
774 pdf_buffer_state *state = (pdf_buffer_state *)state_;
776 fz_printf(state->out, "/%s gs\n", csi->name);
780 pdf_buffer_sh(pdf_csi *csi, void *state_)
782 pdf_buffer_state *state = (pdf_buffer_state *)state_;
784 fz_printf(state->out, "/%s sh\n", csi->name);
788 free_processor_buffer(pdf_csi *csi, void *state_)
790 pdf_buffer_state *state = (pdf_buffer_state *)state_;
791 fz_context *ctx = state->ctx;
793 fz_close_output(state->out);
798 process_annot(pdf_csi *csi, void *state, pdf_obj *resources, pdf_annot *annot)
800 fz_context *ctx = csi->doc->ctx;
801 pdf_xobject *xobj = annot->ap;
803 /* Avoid infinite recursion */
804 if (xobj == NULL || pdf_mark_obj(xobj->me))
810 resources = xobj->resources;
812 pdf_process_contents_object(csi, resources, xobj->contents);
816 pdf_unmark_obj(xobj->me);
825 process_stream(pdf_csi *csi, void *state, pdf_lexbuf *buf)
827 pdf_process_stream(csi, buf);
831 process_contents(pdf_csi *csi, void *state, pdf_obj *resources, pdf_obj *contents)
833 pdf_process_contents_object(csi, resources, contents);
836 static const pdf_processor pdf_processor_buffer =
910 free_processor_buffer
917 pdf_process *pdf_process_buffer(pdf_process *process, fz_context *ctx, fz_buffer *buffer)
919 fz_output *out = fz_new_output_with_buffer(ctx, buffer);
920 pdf_buffer_state *p = NULL;
926 p = fz_malloc_struct(ctx, pdf_buffer_state);
933 fz_close_output(out);
938 process->processor = &pdf_processor_buffer;