ref: 6030d0f343fb59db82e6788b67697dd5c75a149e
parent: 319752248ebdda696cc6cf81ddc058496575ba18
author: glenda <glenda@9front.local>
date: Wed Mar 30 14:52:06 EDT 2022
render pages to images
--- a/main.c
+++ b/main.c
@@ -18,21 +18,25 @@
}
static int
-dumppage(Object *page)
+dumppage(Object *page, int text)
{
int ret;
Page p;
pageinit(&p, page);
ret = pagerender(&p);
- if(ret)
- if(p.buf.sz != 0)
- fprint(1, "%s", (char*)p.buf.b);
+ if(ret){
+ if(text){
+ if(p.buf.sz != 0)
+ fprint(1, "%s", (char*)p.buf.b);
+ }else
+ writememimage(1, p.image);
+ }
pagefree(&p);
return ret;
}
static int
-dumppages(Object *pages)
+dumppages(Object *pages, int text)
{
Object *page, *kids, *type;
int i, count;
@@ -44,13 +48,14 @@
type = dictget(page, "Type");
// MUST be a name.
if(strcmp(type->name, "Pages") == 0){
- if(!dumppages(page))
+ if(!dumppages(page, text))
return 0;
}
else if(strcmp(type->name, "Page") == 0){
- if(!dumppage(page))
+ if(!dumppage(page, text))
return 0;
- print("\n");
+ if(text)
+ print("\n");
}
else
sysfatal("Unexpected page node type '%s'", type->name);
@@ -72,6 +77,8 @@
inflateinit();
memimageinit();
threadwaitchan();
+ if(memimageinit() != 0)
+ sysfatal("failed to initialize graphics: %r");
ARGBEGIN{
default:
@@ -103,12 +110,16 @@
Sclose(s);
v = nil;
break;
+ }else if(argv[i][0] == '!' && argv[i][1] == 0 && v->type == Odict && strcmp(dictget(v, "Type")->name, "Page") == 0){
+ dumppage(v, 0);
+ nodump = 1;
+ break;
}else if(argv[i][0] == '"' && argv[i][1] == 0 && v->type == Odict && strcmp(dictget(v, "Type")->name, "Page") == 0){
- dumppage(v);
+ dumppage(v, 1);
nodump = 1;
break;
}else if(argv[i][0] == '"' && argv[i][1] == 0 && v->type == Odict && strcmp(dictget(v, "Type")->name, "Pages") == 0){
- dumppages(v);
+ dumppages(v, 1);
nodump = 1;
break;
}else if(argv[i][0] == '*' && argv[i][1] == 0 && v->type == Odict){
@@ -136,6 +147,7 @@
fprint(2, "%r\n");
else if(v != nil)
print("%O\n", v);
+
/*
if((v = dictget(pdf->info, "Creator")) != nil)
fprint(2, "creator: %s\n", v->str);
--- a/op.c
+++ b/op.c
@@ -1,5 +1,7 @@
#include <u.h>
#include <libc.h>
+#include <draw.h>
+#include <memdraw.h>
#include "pdf.h"
enum {
@@ -618,8 +620,14 @@
return 0;
}
}
- if(!writepatched(p, c) || !tmove(p, tx, 0, 0))
- return 0;
+ usize oend = p->buf.sz;
+ if(!writepatched(p, c)){
+ werrstr("tchar: failed to write character: %r");
+ return 0;
+ }
+ if(p->buf.b != nil)
+ memimagestring(p->image, Pt(Trm[4], 1100 - Trm[5]), memblack, Pt(0,0), getmemdefont(), (char*)p->buf.b + oend);
+ tmove(p, tx, 0, 0);
p->TS.x = Trm[4] + tx;
p->TS.y = Trm[5];
return 1;
@@ -1325,6 +1333,12 @@
}
p->GSactive = p->GS;
gsinit(p, p->GS);
+ if((p->image = allocmemimage(Rect(0,0,850,1100), strtochan("r8g8b8"))) == nil){
+ werrstr("unable to allocate image: %r");
+ pagegsclean(p);
+ return 0;
+ }
+ memfillcolor(p->image, 0xFFFFFFFF);
content = dictget(p->obj, "Contents");
if(content->type == Oarray){
for(i = 0; i < arraylen(content); i += 1)
--- a/pdf.h
+++ b/pdf.h
@@ -151,6 +151,7 @@
Object *obj;
Object *stack;
Buffer buf;
+ void *image;
/* The graphical state stack. GSactive is always a shortcut for the top of the stack, GS[nGS - 1] */
struct{
GS *GS;