summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohannes Hofmann <Johannes.Hofmann@gmx.de>2006-02-04 15:35:41 +0100
committerJohannes Hofmann <Johannes.Hofmann@gmx.de>2006-02-04 15:35:41 +0100
commit13278eaedfccf32ea897ef187d82d047af903557 (patch)
treedb07bb5f21d1967169aba8cd0e3e0777798250d7 /src
parent9d93fd766028f7930c9a6b6f15094ebccc36411f (diff)
initial version of load_page()
Diffstat (limited to 'src')
-rw-r--r--src/GsWidget.H10
-rw-r--r--src/GsWidget.cxx144
-rw-r--r--src/Makefile.am2
-rw-r--r--src/PostscriptDSC.H45
-rw-r--r--src/PostscriptDSC.cxx142
-rw-r--r--src/flpsed.cxx2
6 files changed, 323 insertions, 22 deletions
diff --git a/src/GsWidget.H b/src/GsWidget.H
index 99ef08a..0735607 100644
--- a/src/GsWidget.H
+++ b/src/GsWidget.H
@@ -34,8 +34,10 @@
#include <FL/Fl_Window.H>
#include <FL/x.H>
+#include "PostscriptDSC.H"
class GsWidget : public Fl_Widget {
+ PostscriptDSC *dsc;
int gs_win;
Atom atoms[5];
pid_t gs_pid;
@@ -61,6 +63,8 @@ public:
int load(int fd);
+ int load_page(int p);
+
int reload();
int next();
@@ -73,9 +77,15 @@ public:
private:
void setProps();
+
+ void exec_gs();
void kill_gs();
+ int fd_copy(int to, int from, size_t len);
+
+ int feed_page(int p);
+
bool gs_active();
};
#endif
diff --git a/src/GsWidget.cxx b/src/GsWidget.cxx
index dc134d2..14d77f9 100644
--- a/src/GsWidget.cxx
+++ b/src/GsWidget.cxx
@@ -33,7 +33,9 @@
#include <FL/fl_draw.H>
#include "GsWidget.H"
+#include "PostscriptDSC.H"
+#define MIN(A,B) ((A)<(B)?(A):(B))
void GsWidget::draw() {
if (!offscreen) {
@@ -111,6 +113,7 @@ GsWidget::GsWidget(int X,int Y,int W, int H) : Fl_Widget(X, Y, W, H) {
initial_height = H;
in_fd = -1;
reload_needed = 0;
+ dsc = new PostscriptDSC();
}
GsWidget::~GsWidget() {
@@ -130,12 +133,25 @@ int GsWidget::load(char *f) {
}
int GsWidget::load(int fd) {
+ pid_t pid;
+
if (in_fd >= 0 && fd != in_fd) {
close (in_fd);
}
in_fd = fd;
+ if (dsc->parse(in_fd) == 0) {
+ int bb_x, bb_y, bb_w, bb_h;
+
+ dsc->print();
+ dsc->get_bounding_box(&bb_x, &bb_y, &bb_w, &bb_h);
+ paper_x = bb_w;
+ paper_y = bb_h;
+ }
+
+ lseek(in_fd, 0L, SEEK_SET);
+
fl_cursor(FL_CURSOR_WAIT);
kill_gs();
@@ -146,30 +162,10 @@ int GsWidget::load(int fd) {
setProps();
- pid_t pid = fork();
+ pid = fork();
if (pid == (pid_t) 0) {
- char *argv[16];
- char gvenv[256];
- int d_null = open("/dev/null", O_WRONLY);
-
- dup2(d_null, STDOUT_FILENO);
- close(d_null);
dup2(in_fd, STDIN_FILENO);
- snprintf(gvenv, 256, "%d %d", (int) fl_xid(window()), (int) offscreen);
-
- setenv("GHOSTVIEW", gvenv, 1);
- argv[0] = "gs";
- argv[1] = "-dSAFER";
- argv[2] = "-dQUIET";
- argv[3] = "-sDEVICE=x11alpha";
- argv[4] = "-dNOPLATFONTS";
- argv[5] = "-";
- argv[6] = NULL;
- execvp(argv[0], argv);
- perror("exec");
- fprintf(stderr, "Please install ghostscript and make sure 'gs' "
- "is in the PATH.\n");
- exit(1);
+ exec_gs();
} else {
gs_pid = pid;
page = 0;
@@ -178,6 +174,110 @@ int GsWidget::load(int fd) {
return 0;
}
+int
+GsWidget::load_page(int p) {
+ pid_t pid;
+
+ if (p < 1 || p > dsc->get_pages()) {
+ fprintf(stderr, "Page %d not found in document\n", p);
+ return 1;
+ }
+
+ fl_cursor(FL_CURSOR_WAIT);
+ kill_gs();
+
+ setProps();
+
+ pid = fork();
+
+ if (pid == (pid_t) 0) {
+ feed_page(p);
+ exit(0);
+ } else {
+ gs_pid = pid;
+ }
+}
+
+int GsWidget::feed_page(int p) {
+ pid_t pid;
+ int pdes[2];
+
+ if (pipe(pdes) < 0) {
+ return 1;
+ }
+
+ pid = fork();
+
+ if (pid == (pid_t) 0) {
+ close(pdes[1]);
+ dup2(pdes[0], STDIN_FILENO);
+ exec_gs();
+ } else {
+ size_t len;
+
+ close(pdes[0]);
+
+ lseek(in_fd, 0L, SEEK_SET);
+ len = dsc->get_setup_len();
+ if (fd_copy(pdes[1], in_fd, len) != 0) {
+ exit(1);
+ }
+
+ lseek(in_fd, dsc->get_page_off(p), SEEK_SET);
+ len = dsc->get_page_len(p);
+ if (fd_copy(pdes[1], in_fd, len) != 0) {
+ exit(1);
+ }
+
+ }
+
+}
+
+int GsWidget::fd_copy(int to, int from, size_t len) {
+ size_t n, r;
+ char buf[1024];
+
+ n = 0;
+ while(len > 0) {
+ r = read(from, buf, MIN(sizeof(buf), len));
+
+ write(STDOUT_FILENO, buf, r);
+ if (r < 0) {
+ perror("read");
+ return 1;
+ }
+ write(to, buf, r);
+ len -= r;
+ }
+
+ return 0;
+}
+
+void GsWidget::exec_gs() {
+ char *argv[16];
+ char gvenv[256];
+ int d_null = open("/dev/null", O_WRONLY);
+
+ dup2(d_null, STDOUT_FILENO);
+ close(d_null);
+
+ snprintf(gvenv, 256, "%d %d", (int) fl_xid(window()), (int) offscreen);
+
+ setenv("GHOSTVIEW", gvenv, 1);
+ argv[0] = "gs";
+ argv[1] = "-dSAFER";
+ argv[2] = "-dQUIET";
+ argv[3] = "-sDEVICE=x11alpha";
+ argv[4] = "-dNOPLATFONTS";
+ argv[5] = "-";
+ argv[6] = NULL;
+ execvp(argv[0], argv);
+ perror("exec");
+ fprintf(stderr, "Please install ghostscript and make sure 'gs' "
+ "is in the PATH.\n");
+ exit(1);
+}
+
int GsWidget::reload() {
int ret;
diff --git a/src/Makefile.am b/src/Makefile.am
index 62c767c..8a0e366 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -7,6 +7,7 @@ flpsed_SOURCES = \
PSEditText.cxx \
PSEditModel.cxx \
Postscript.cxx \
+ PostscriptDSC.cxx \
flpsed.cxx \
util.c
@@ -15,6 +16,7 @@ noinst_HEADERS = \
PSEditModel.H \
PSEditText.H \
Postscript.H \
+ PostscriptDSC.H \
PSEditor.H \
PSEditWidget.H \
util.h
diff --git a/src/PostscriptDSC.H b/src/PostscriptDSC.H
new file mode 100644
index 0000000..6c5823f
--- /dev/null
+++ b/src/PostscriptDSC.H
@@ -0,0 +1,45 @@
+//
+// Copyright 2006 by Johannes Hofmann
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU Library General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+//
+
+#ifndef POSTSCRIPTDSC_H
+#define POSTSCRIPTDSC_H
+
+class PostscriptDSC {
+ private:
+ int bb_x, bb_y, bb_w, bb_h;
+ size_t setup_len;
+ int pages;
+
+ size_t *page_off;
+ size_t *page_len;
+
+ public:
+ PostscriptDSC();
+ ~PostscriptDSC();
+
+ int parse(int fd);
+ int get_pages();
+ void get_bounding_box(int *x, int *y, int *w, int *h);
+ size_t get_setup_len();
+ size_t get_page_off(int p);
+ size_t get_page_len(int p);
+ void print();
+};
+
+#endif
diff --git a/src/PostscriptDSC.cxx b/src/PostscriptDSC.cxx
new file mode 100644
index 0000000..bd47837
--- /dev/null
+++ b/src/PostscriptDSC.cxx
@@ -0,0 +1,142 @@
+//
+// Postscript Document Structure Convention handling routines.
+//
+// Copyright 2006 by Johannes Hofmann
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Library General Public
+// License as published by the Free Software Foundation; either
+// version 2 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Library General Public License for more details.
+//
+// You should have received a copy of the GNU Library General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+// USA.
+//
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include "PostscriptDSC.H"
+
+PostscriptDSC::PostscriptDSC() {
+ bb_x = 0;
+ bb_y = 0;
+ bb_w = 594; // A4
+ bb_h = 841; //
+ setup_len = 0;
+ pages = 0;
+ page_off = NULL;
+ page_len = NULL;
+}
+
+int
+PostscriptDSC::parse(int fd) {
+ FILE *fp;
+ char linebuf[1024];
+ int x, y, w, h;
+ int p1, p2, ps;
+ int i = 0;
+
+ bb_x = 0;
+ bb_y = 0;
+ bb_w = 594; // A4
+ bb_h = 841; //
+ setup_len = 0;
+ pages = 0;
+ page_off = NULL;
+ page_len = NULL;
+
+ fp = fdopen(fd, "r");
+ if (!fp) {
+ perror("fdopen");
+ return 1;
+ }
+
+ while (fgets(linebuf, sizeof(linebuf), fp) != NULL) {
+ if (sscanf(linebuf, "%%%%BoundingBox: %d %d %d %d", &x, &y, &w, &h) == 4) {
+ bb_x = x;
+ bb_y = y;
+ bb_w = w;
+ bb_h = h;
+ } else if (strncmp(linebuf, "%%EndSetup", strlen("%%EndSetup")) == 0) {
+ setup_len = ftello(fp);
+ } else if (sscanf(linebuf, "%%%%Pages: %d", &ps) == 1) {
+ if (pages != 0) {
+ fprintf(stderr, "Multiple Pages tags found\n");
+ free(page_off);
+ free(page_len);
+ return 1;
+ }
+
+ pages = ps;
+ page_off = (size_t*) malloc(sizeof(size_t) * (pages + 1));
+ memset(page_off, 0, sizeof(size_t) * (pages + 1));
+ page_len = (size_t*) malloc(sizeof(size_t) * (pages + 1));
+ memset(page_len, 0, sizeof(size_t) * (pages + 1));
+ } else if (sscanf(linebuf, "%%%%Page: %d %d", &p1, &p2) == 2) {
+ if (p2 > pages) {
+ fprintf(stderr, "Page %d > Pages (%d)\n", p1, pages);
+ return 1;
+ }
+ if (page_off[p1] != NULL) {
+ fprintf(stderr, "Page %d already defined\n", p1);
+ return 1;
+ }
+ if (p1 > 1 && page_off[p1 - 1] == NULL) {
+ fprintf(stderr, "Page %d not yet defined\n", p1 - 1);
+ return 1;
+ }
+
+ page_off[p1] = ftello(fp);
+ if (p1 > 1) {
+ page_len[p1 - 1] = page_off[p1] - page_off[p1 - 1];
+ }
+ }
+ }
+}
+
+void
+PostscriptDSC::get_bounding_box(int *x, int *y, int *w, int *h) {
+ *x = bb_x;
+ *y = bb_y;
+ *w = bb_w;
+ *h = bb_h;
+}
+
+int
+PostscriptDSC::get_pages() {
+ return pages;
+}
+
+size_t
+PostscriptDSC::get_setup_len() {
+ return setup_len;
+}
+
+size_t
+PostscriptDSC::get_page_off(int p) {
+ return page_off[p];
+}
+
+size_t
+PostscriptDSC::get_page_len(int p) {
+ return page_len[p];
+}
+
+void
+PostscriptDSC::print() {
+ int i;
+
+ printf("x %d, y %d, w %d, h %d\n", bb_x, bb_y, bb_w, bb_h);
+ printf("setup_len %d\n", setup_len);
+ for (i=0; i<pages; i++) {
+ printf("p %d, off %d, len %d\n", i, page_off[i], page_len[i]);
+ }
+}
+
diff --git a/src/flpsed.cxx b/src/flpsed.cxx
index dae4677..a341fc2 100644
--- a/src/flpsed.cxx
+++ b/src/flpsed.cxx
@@ -82,6 +82,8 @@ void open_cb() {
char *file = fl_file_chooser("Open File?", "*.ps", filename);
if(file != NULL) {
psed_p->load(file);
+
+ psed_p->load_page(2);
}
}