From a926cb9248e232a03c1eb051c53db4c3133236de Mon Sep 17 00:00:00 2001 From: Johannes Hofmann Date: Fri, 9 Jul 2004 15:22:55 +0000 Subject: major restructuring... major restructuring... --- src/PSEditWidget.H | 20 ++-- src/PSEditWidget.cxx | 254 +++++++-------------------------------------------- src/PSEditor.H | 48 ++++++++++ src/PSEditor.cxx | 154 +++++++++++++++++++++++++++++++ src/Postscript.cxx | 60 +++++++++++- src/flpsed.cxx | 63 +------------ 6 files changed, 302 insertions(+), 297 deletions(-) create mode 100644 src/PSEditor.H create mode 100644 src/PSEditor.cxx (limited to 'src') diff --git a/src/PSEditWidget.H b/src/PSEditWidget.H index bf2592c..04f72dd 100644 --- a/src/PSEditWidget.H +++ b/src/PSEditWidget.H @@ -1,5 +1,5 @@ // -// "$Id: PSEditWidget.H,v 1.5 2004/06/25 17:25:17 hofmann Exp $" +// "$Id: PSEditWidget.H,v 1.6 2004/07/09 17:22:55 hofmann Exp $" // // X11 header file for the Fast Light Tool Kit (FLTK). // @@ -30,12 +30,9 @@ class PSText; class PSEditWidget : public GsWidget { private: - int mark_x, mark_y; PSText **text; int max_pages; - int tmp_fd; int cur_size; - int mod, loaded; protected: void clear_text(); @@ -56,15 +53,12 @@ public: int ps_to_display_y(int y1); int ps_x(int x1); int ps_y(int y1); - int load(char *f); int reload(); - void to_ps(FILE *f, int p); - int save(const char* savefile); void set_cur_size(int s); void set_size(int s); int get_size(); - int modified(); - int file_loaded(); + int get_max_pages(); + PSText * get_text(int p); }; @@ -85,9 +79,13 @@ public: void move(int x1, int y1); void append(PSText *g); PSText *get_match(int x1, int y1); + char *get_text(); + int get_size(); + PSText *get_next(); + Fl_Color get_color(); + int get_x(); + int get_y(); void draw(int off_x,int off_y); - void string_to_ps(FILE *f, char *s); - void to_ps(FILE *f); }; #endif diff --git a/src/PSEditWidget.cxx b/src/PSEditWidget.cxx index ba7cc54..013a94e 100644 --- a/src/PSEditWidget.cxx +++ b/src/PSEditWidget.cxx @@ -1,5 +1,5 @@ // -// "$Id: PSEditWidget.cxx,v 1.13 2004/07/07 17:17:54 hofmann Exp $" +// "$Id: PSEditWidget.cxx,v 1.14 2004/07/09 17:22:55 hofmann Exp $" // // PSEditWidget routines. // @@ -37,53 +37,6 @@ #include "PSEditWidget.H" -#define PS_POS_FORMAT "newpath %d %d moveto %% PSEditWidget\n" -#define PS_TEXT_FORMAT "(%s) show %% PSEditWidget\n" -#define PS_FONT_SIZE_FORMAT "/HelveticaNeue-Roman findfont %d scalefont setfont %% PSEditWidget\n" -#define PS_GLYPH_FORMAT "/%s glyphshow %% PSEditWidget\n" - -static struct { - const char *glyph; - const char *c; -} glyph_char[] = { - {"adieresis", "ä"}, - {"Adieresis", "Ä"}, - {"odieresis", "ö"}, - {"Odieresis", "Ö"}, - {"udieresis", "ü"}, - {"Udieresis", "Ü"}, - {"germandbls", "ß"}, - {"parenleft", "("}, - {"parenright", ")"}, - {"backslash", "\\"}, - {NULL, NULL}}; - -static const char * glyph_to_char(char *glyph) { - int i=0; - - while(glyph_char[i].glyph != NULL) { - if (strcmp(glyph_char[i].glyph, glyph) == 0) { - return glyph_char[i].c; - } - i++; - } - - return NULL; -} - -static const char * char_to_glyph(char *c) { - int i=0; - - while(glyph_char[i].glyph != NULL) { - if (strncmp(glyph_char[i].c, c, 1) == 0) { - return glyph_char[i].glyph; - } - i++; - } - - return NULL; -} - void PSEditWidget::clear_text() { cur_text = NULL; for (int i = 0; i < max_pages; i++) { @@ -109,8 +62,6 @@ PSEditWidget::PSEditWidget(int X,int Y,int W, int H) : GsWidget(X, Y, W, H) { } cur_text = NULL; cur_size = 12; - mod = 0; - loaded = 0; } int PSEditWidget::next() { @@ -133,7 +84,6 @@ void PSEditWidget::new_text(int x1, int y1, const char *s, int p) { text[p] = cur_text; } redraw(); - mod = 1; } void PSEditWidget::new_text(int x1, int y1, const char *s) { @@ -154,7 +104,6 @@ int PSEditWidget::set_cur_text(int x1, int y1) { void PSEditWidget::append_text(const char *s) { if (cur_text && s) { cur_text->append_text(s); - mod = 1; redraw(); } } @@ -162,7 +111,6 @@ void PSEditWidget::append_text(const char *s) { void PSEditWidget::move(int x1, int y1) { if (cur_text) { cur_text->move(x1, y1); - mod = 1; redraw(); } } @@ -170,7 +118,6 @@ void PSEditWidget::move(int x1, int y1) { void PSEditWidget::rm_char() { if (cur_text) { cur_text->rm_char(); - mod = 1; redraw(); } } @@ -196,141 +143,6 @@ int PSEditWidget::reload() { return GsWidget::reload(); } -int PSEditWidget::load(char *f) { - FILE *fp = fopen(f, "r"); - char tmpname[256]; - char linebuf[1024]; - int p = 1; - int x1, y1; - char *s, *e, glyph[1024]; - int size, ret; - - strncpy(tmpname, "/tmp/PSEditWidgetXXXXXX", 256); - tmp_fd = mkstemp(tmpname); - if (tmp_fd < 0) { - fprintf(stderr, "Could not create temporary file (errno %d).\n", errno); - return 1; - } - unlink(tmpname); - - clear_text(); - - while (fgets(linebuf, 1024, fp) != NULL) { - - if (strcmp(linebuf, "showpage\n") == 0) { - p++; - } - - if (strstr(linebuf, "% PSEditWidget")) { - if (sscanf(linebuf, PS_FONT_SIZE_FORMAT, &size) == 1) { - set_cur_size(size); - } else if (sscanf(linebuf, PS_POS_FORMAT, &x1, &y1) == 2) { - new_text(ps_to_display_x(x1), ps_to_display_y(y1), "", p); - } else if (sscanf(linebuf, PS_GLYPH_FORMAT, glyph) == 1) { - fprintf(stderr, "GLYPH %s\n", glyph); - append_text(glyph_to_char(glyph)); - } else if ((s = strchr(linebuf, '(')) && - (e = strrchr(linebuf, ')'))) { - *e = '\0'; - s++; - append_text(s); - } - } else { - ret = write(tmp_fd, linebuf, strlen(linebuf)); - if (ret != strlen(linebuf)) { - fprintf(stderr, "Error while writing to temporary file\n"); - } - } - } - fclose(fp); - lseek(tmp_fd, 0L, SEEK_SET); - - mod = 0; - loaded = 1; - return GsWidget::load(tmp_fd); -} - -void PSEditWidget::to_ps(FILE *f, int p) { - if (!text[p]) { - return; - } - fprintf(f, "dup %d eq {\n", p); - text[p]->to_ps(f); - fprintf(f, "} if\n"); -} - -#define PS_HEADER_L2 "%% Begin PSEditWidget\n" \ -"/PSEditWidgetPageCount 0 def\n" \ -"<< /EndPage {\n" \ -"pop\n" \ -"PSEditWidgetPageCount 0 eq { %% if PSEditWidgetPageCount is undefined,\n" \ -"1 add %% use showpage counter instead.\n" \ -"} {\n" \ -"PSEditWidgetPageCount\n" \ -"} ifelse\n" - - -#define PS_TRAILER_L2 "true } >> setpagedevice\n" \ -"%% End PSEditWidget\n" - -#define PS_HEADER_L1 "%% Begin PSEditWidget\n" \ -"/PSEditWidgetPageCount 0 def\n" \ -"/PSEditWidgetPC 0 def\n" \ -"/PSEditWidgetshowpage /showpage load def\n" \ -"/showpage {\n" \ -"PSEditWidgetPageCount 0 eq { %% if PSEditWidgetPageCount is undefined,\n" \ -"/PSEditWidgetPC PSEditWidgetPC 1 add def PSEditWidgetPC\n" \ -"} {\n" \ -"PSEditWidgetPageCount\n" \ -"} ifelse\n" - -#define PS_TRAILER_L1 "PSEditWidgetshowpage} def\n" \ -"%% End PSEditWidget\n" - -int PSEditWidget::save(const char* savefile) { - if (!file_loaded()) { - return 1; - } - FILE *fp = fdopen(tmp_fd, "r"); - rewind(fp); - FILE *sfp = fopen(savefile, "w"); - char linebuf[1024]; - int done=0, page = 1; - - while (fgets(linebuf, 1024, fp) != NULL) { - if (!done && strncmp(linebuf, "%%EndSetup", 10) == 0) { - done++; - - fprintf(sfp, PS_HEADER_L1); - - for (int i=1;i= max_pages) { + return 0; + } else { + return text[p]; + } +} + PSText::PSText(PSEditWidget *g, int x1, int y1, const char *s1, int size1) { @@ -429,42 +253,26 @@ void PSText::draw(int off_x,int off_y) { } } -void PSText::string_to_ps(FILE *f, char *s) { - const char *glyph; - - if (strlen(s) == 0) { - return; - } else if ((glyph = char_to_glyph(s)) != NULL) { - fprintf(f, PS_GLYPH_FORMAT, glyph); - string_to_ps(f, &(s[1])); - return; - } else { - for(int i=0; ips_x(x), gsew->ps_y(y)); - string_to_ps(f, s); - } - - if (next) { - next->to_ps(f); - } + +int PSText::get_size() { + return size; } +Fl_Color PSText::get_color() { + return c; +} +PSText* PSText::get_next() { + return next; +} +int PSText::get_x() { + return x; +} + +int PSText::get_y() { + return y; +} diff --git a/src/PSEditor.H b/src/PSEditor.H new file mode 100644 index 0000000..3c6c8f5 --- /dev/null +++ b/src/PSEditor.H @@ -0,0 +1,48 @@ +// +// "$Id: PSEditor.H,v 1.1 2004/07/09 17:22:55 hofmann Exp $" +// +// X11 header file for the Fast Light Tool Kit (FLTK). +// +// Copyright 2004 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 PSEDITOR_H +#define PSEDITOR_H + +#include "PSEditWidget.H" + +class PSEditor : public PSEditWidget { + private: + int mark_x, mark_y; + int loaded; + int mod; + int tmp_fd; + + int handle(int event); + +public: + PSEditor(int X,int Y,int W, int H); + int load(char *f); + int save(const char* savefile); + int modified(); + int file_loaded(); +}; + + + +#endif diff --git a/src/PSEditor.cxx b/src/PSEditor.cxx new file mode 100644 index 0000000..db501fd --- /dev/null +++ b/src/PSEditor.cxx @@ -0,0 +1,154 @@ +// +// "$Id: PSEditor.cxx,v 1.1 2004/07/09 17:22:55 hofmann Exp $" +// +// PSEditor routines. +// +// Copyright 2004 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 +#include +#include "PSEditor.H" +#include "Postscript.H" + +PSEditor::PSEditor(int X,int Y,int W, int H) : PSEditWidget(X, Y, W, H) { + loaded = 0; + mod = 0; +} + +int PSEditor::handle(int event) { + switch(event) { + case FL_PUSH: + if (!file_loaded()) { + fl_beep(); + return 0; + } + + mark_x = Fl::event_x()-x(); + mark_y = Fl::event_y()-y(); + + if (!set_cur_text(mark_x, mark_y) == 0) { + new_text(mark_x, mark_y, ""); + mod++; + } + + Fl::focus(this); + return 1; + case FL_DRAG: + move(Fl::event_x()-x(), Fl::event_y()-y()); + mod++; + return 1; + case FL_KEYBOARD: + { + int del; + int key = Fl::event_key(); + if (key == FL_BackSpace) { + rm_char(); + mod++; + } else if (Fl::compose(del)) { + if (del > 0) { + for (int i=0; iparse(linebuf); + if (ret1 == 0 || ret1 == 2) { + ret2 = p2->parse(linebuf); + } + + if (!ret1 && !ret2) { + ret = write(tmp_fd, linebuf, strlen(linebuf)); + if (ret != strlen(linebuf)) { + fprintf(stderr, "Error while writing to temporary file\n"); + } + } + } + fclose(fp); + lseek(tmp_fd, 0L, SEEK_SET); + + delete(p1); + delete(p2); + + mod = 0; + loaded = 1; + return GsWidget::load(tmp_fd); +} + +int PSEditor::save(const char* savefile) { + if (!file_loaded()) { + return 1; + } + FILE *fp = fdopen(tmp_fd, "r"); + rewind(fp); + FILE *sfp = fopen(savefile, "w"); + PSWriter *pw = new PSLevel1Writer(this); + + pw->write(fp, sfp); + + delete(pw); + + fclose(sfp); + mod = 0; + return 0; +} diff --git a/src/Postscript.cxx b/src/Postscript.cxx index 90d7b8c..969e71e 100644 --- a/src/Postscript.cxx +++ b/src/Postscript.cxx @@ -1,7 +1,7 @@ // -// "$Id: Postscript.cxx,v 1.1 2004/07/09 16:13:35 hofmann Exp $" +// "$Id: Postscript.cxx,v 1.2 2004/07/09 17:22:55 hofmann Exp $" // -// PSEditWidget routines. +// Postscript handling routines. // // Copyright 2004 by Johannes Hofmann // @@ -22,6 +22,7 @@ // #include "Postscript.H" + #define PS_POS_FORMAT "newpath %d %d moveto %% PSEditWidget\n" #define PS_TEXT_FORMAT "(%s) show %% PSEditWidget\n" #define PS_FONT_SIZE_FORMAT "/HelveticaNeue-Roman findfont %d scalefont setfont %% PSEditWidget\n" @@ -79,6 +80,10 @@ PSParser::PSParser(PSEditWidget *p) { pse = p; } +int PSParser::parse(char *line) { + return 0; +} + PSParser_1::PSParser_1(PSEditWidget *p) : PSParser(p) { page = 1; } @@ -210,3 +215,54 @@ int PSWriter::write_text(FILE *out, PSText *t) { return 0; } + +char * PSWriter::ps_header() { + return ""; +} + +char * PSWriter::ps_trailer() { + return ""; +} + +PSLevel1Writer::PSLevel1Writer(PSEditWidget *p) : PSWriter(p) {}; + +char * PSLevel1Writer::ps_header() { + return \ + "%% Begin PSEditWidget\n" \ + "/PSEditWidgetPageCount 0 def\n" \ + "/PSEditWidgetPC 0 def\n" \ + "/PSEditWidgetshowpage /showpage load def\n" \ + "/showpage {\n" \ + "PSEditWidgetPageCount 0 eq { \n" \ + "/PSEditWidgetPC PSEditWidgetPC 1 add def PSEditWidgetPC\n" \ + "} {\n" \ + "PSEditWidgetPageCount\n" \ + "} ifelse\n"; +} + +char * PSLevel1Writer::ps_trailer() { + return "PSEditWidgetshowpage} def\n" \ + "%% End PSEditWidget\n"; +} + + +PSLevel2Writer::PSLevel2Writer(PSEditWidget *p) : PSWriter(p) {}; + +char * PSLevel2Writer::ps_header() { + return \ + "%% Begin PSEditWidget\n" \ + "/PSEditWidgetPageCount 0 def\n" \ + "<< /EndPage {\n" \ + "pop\n" \ + "PSEditWidgetPageCount 0 eq { \n" \ + "1 add %% use showpage counter instead.\n" \ + "} {\n" \ + "PSEditWidgetPageCount\n" \ + "} ifelse\n"; + +} + +char * PSLevel2Writer::ps_trailer() { + return "true } >> setpagedevice\n" \ + "%% End PSEditWidget\n"; +} diff --git a/src/flpsed.cxx b/src/flpsed.cxx index 6506185..a0575bf 100644 --- a/src/flpsed.cxx +++ b/src/flpsed.cxx @@ -1,5 +1,5 @@ // -// "$Id: flpsed.cxx,v 1.11 2004/06/29 17:26:20 hofmann Exp $" +// "$Id: flpsed.cxx,v 1.12 2004/07/09 17:22:55 hofmann Exp $" // // flpsed program. // @@ -40,66 +40,7 @@ #include #include -#include "PSEditWidget.H" - -class PSEditor : public PSEditWidget { - int mark_x, mark_y; - - int handle(int event) { - switch(event) { - case FL_PUSH: - if (!file_loaded()) { - fl_beep(); - return 0; - } - - mark_x = Fl::event_x()-x(); - mark_y = Fl::event_y()-y(); - - if (!set_cur_text(mark_x, mark_y) == 0) { - new_text(mark_x, mark_y, ""); - } - - Fl::focus(this); - return 1; - case FL_DRAG: - move(Fl::event_x()-x(), Fl::event_y()-y()); - return 1; - case FL_KEYBOARD: - { - int del; - int key = Fl::event_key(); - if (key == FL_BackSpace) { - rm_char(); - } else if (Fl::compose(del)) { - if (del > 0) { - for (int i=0; i