shithub: pdffs

Download patch

ref: 464b83a87fb9d1841de5f619db8a515161bbbbce
parent: 6d2612e1503c37990bba44b409e5402d9dbd5c19
author: Noam Preil <noam@pixelhero.dev>
date: Tue Jun 1 19:30:37 EDT 2021

buffered pagerender

--- a/main.c
+++ b/main.c
@@ -21,7 +21,7 @@
 	Page p;
 	pageinit(&p);
 	if(pagerender(&p, page))
-		fprint(1, "%s\n", p.text);
+		write(1, p.buf.b, p.buf.sz);
 	pagefree(&p);
 }
 
--- a/op.c
+++ b/op.c
@@ -298,7 +298,8 @@
 	int d = arrayget(p->stack, 0)->num.d / 20;
 	while(d > 0){
 		d -= 1;
-		fprint(1, "\n");
+		if(bufput(&p->buf, (uchar*)"\n", 1) == -1)
+			sysfatal("OOM");
 	}
 	USED(op, p);
 	return 0;
@@ -345,10 +346,13 @@
 	Object *x, *y;
 	x = arrayget(p->stack, 0);
 	y = arrayget(p->stack, 1);
-	if(y->num.d != 0)
-		fprint(1, "\n");
+	if(y->num.d != 0){
+		if(bufput(&p->buf, (uchar*)"\n", 1) == -1)
+			sysfatal("OOM");
+	}
 	else if(x->num.d < 50)
-		fprint(1, " ");
+		if(bufput(&p->buf, (uchar*)" ", 1) == -1)
+			sysfatal("OOM");
 	USED(op, p);
 	return 0;
 }
@@ -364,7 +368,8 @@
 tpmove0(Op *op, Page *p)
 {
 	USED(op, p);
-	fprint(1, "\n");
+	if(bufput(&p->buf, (uchar*)"\n", 1) == -1)
+		sysfatal("OOM");
 	return 0;
 }
 
@@ -383,10 +388,14 @@
 	int i;
 	for(i = 0; i < arraylen(arr); i += 1){
 		o = arrayget(arr, i);
-		if(o->type == Ostr)
-			fprint(1, "%s", o->str);
-		else if(o->num.d < -150)
-			fprint(1, " ");
+		if(o->type == Ostr){
+			if(bufput(&p->buf, (uchar*)o->str, o->len) == -1)
+				sysfatal("OOM");
+		}
+		else if(o->num.d < -150){
+			if(bufput(&p->buf, (uchar*)" ", 1) == -1)
+				sysfatal("OOM");
+		}
 	}
 	USED(op);
 	return 0;
@@ -905,7 +914,7 @@
 void
 pageinit(Page *page)
 {
-	page->text = nil;
+	bufinit(&page->buf, 0, 0);
 	// Stack is per-content-stream, so we don't create it here
 	page->stack = nil;
 }
@@ -913,7 +922,7 @@
 void
 pagefree(Page *p)
 {
-	free(p->text);
+	buffree(&p->buf);
 	pdfobjfree(p->stack);
 }
 
@@ -963,7 +972,8 @@
 			}
 		}
 	}
-	fprint(1, "\n");
+	if(bufput(&p->buf, (uchar*)"\n", 1) == -1)
+		sysfatal("OOM");
 	Sclose(s);
 }
 
@@ -978,5 +988,5 @@
 			pagerendercontent(p, arrayget(content, i));
 	else if(content->type != Onull)
 		pagerendercontent(p, content);
-	return 0;
+	return 1;
 }
--- a/pdf.h
+++ b/pdf.h
@@ -24,11 +24,6 @@
 typedef struct Xref Xref;
 typedef struct Page Page;
 
-struct Page {
-	Object *stack;
-	char *text;
-};
-
 struct Buffer {
 	uchar *b;
 	int ro;
@@ -35,6 +30,11 @@
 	int maxsz;
 	int sz;
 	int off;
+};
+
+struct Page {
+	Object *stack;
+	Buffer buf;
 };
 
 struct Filter {