+static void
+pdf_obj(struct termp *p, size_t obj)
+{
+
+ assert(obj > 0);
+
+ if ((obj - 1) >= p->engine.ps.pdfobjsz) {
+ p->engine.ps.pdfobjsz = obj + 128;
+ p->engine.ps.pdfobjs = realloc
+ (p->engine.ps.pdfobjs,
+ p->engine.ps.pdfobjsz * sizeof(size_t));
+ if (NULL == p->engine.ps.pdfobjs) {
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ p->engine.ps.pdfobjs[(int)obj - 1] = p->engine.ps.pdfbytes;
+ ps_printf(p, "%zu 0 obj\n", obj);
+}
+
+
+static void
+ps_closepage(struct termp *p)
+{
+ int i;
+ size_t len, base;
+
+ /*
+ * Close out a page that we've already flushed to output. In
+ * PostScript, we simply note that the page must be showed. In
+ * PDF, we must now create the Length, Resource, and Page node
+ * for the page contents.
+ */
+
+ assert(p->engine.ps.psmarg && p->engine.ps.psmarg[0]);
+ ps_printf(p, "%s", p->engine.ps.psmarg);
+
+ if (TERMTYPE_PS != p->type) {
+ ps_printf(p, "ET\n");
+
+ len = p->engine.ps.pdfbytes - p->engine.ps.pdflastpg;
+ base = p->engine.ps.pages * 4 + p->engine.ps.pdfbody;
+
+ ps_printf(p, "endstream\nendobj\n");
+
+ /* Length of content. */
+ pdf_obj(p, base + 1);
+ ps_printf(p, "%zu\nendobj\n", len);
+
+ /* Resource for content. */
+ pdf_obj(p, base + 2);
+ ps_printf(p, "<<\n/ProcSet [/PDF /Text]\n");
+ ps_printf(p, "/Font <<\n");
+ for (i = 0; i < (int)TERMFONT__MAX; i++)
+ ps_printf(p, "/F%d %d 0 R\n", i, 3 + i);
+ ps_printf(p, ">>\n>>\n");
+
+ /* Page node. */
+ pdf_obj(p, base + 3);
+ ps_printf(p, "<<\n");
+ ps_printf(p, "/Type /Page\n");
+ ps_printf(p, "/Parent 2 0 R\n");
+ ps_printf(p, "/Resources %zu 0 R\n", base + 2);
+ ps_printf(p, "/Contents %zu 0 R\n", base);
+ ps_printf(p, ">>\nendobj\n");
+ } else
+ ps_printf(p, "showpage\n");
+
+ p->engine.ps.pages++;
+ p->engine.ps.psrow = p->engine.ps.top;
+ assert( ! (PS_NEWPAGE & p->engine.ps.flags));
+ p->engine.ps.flags |= PS_NEWPAGE;
+}
+
+