From 21696db95728a191f41a2209755a8dc6a206877e Mon Sep 17 00:00:00 2001 From: erg Date: Tue, 8 Jul 2008 21:51:27 +0000 Subject: [PATCH] Fix to allow postscript convention using \r as EOL. --- lib/common/psusershape.c | 47 +++++++++++++++++++++++++--------------- lib/gvc/gvusershape.c | 14 +++++++++++- 2 files changed, 42 insertions(+), 19 deletions(-) diff --git a/lib/common/psusershape.c b/lib/common/psusershape.c index bb4560b62..82bae27ff 100644 --- a/lib/common/psusershape.c +++ b/lib/common/psusershape.c @@ -130,27 +130,38 @@ void epsf_free(node_t * n) #ifdef FILTER_EPSF /* this removes EPSF DSC comments that, when nested in another * document, cause errors in Ghostview and other Postscript - * processors (although legal according to the Adobe EPSF spec). */ + * processors (although legal according to the Adobe EPSF spec). + * + * N.B. PostScript lines can end with \n, \r or \r\n. + */ void epsf_emit_body(usershape_t *us, FILE *of) { - char *p; - p = us->data; - while (*p) { - /* skip %%EOF lines */ - if ((p[0] == '%') && (p[1] == '%') - && (!strncasecmp(&p[2], "EOF", 3) - || !strncasecmp(&p[2], "BEGIN", 5) - || !strncasecmp(&p[2], "END", 3) - || !strncasecmp(&p[2], "TRAILER", 7) - )) { - /* check for *p since last line might not end in '\n' */ - while (*p && (*p++ != '\n')); - continue; - } - do { - fputc(*p, of); - } while (*p++ != '\n'); + char *p; + char c; + p = us->data; + while (*p) { + /* skip %%EOF lines */ + if ((p[0] == '%') && (p[1] == '%') + && (!strncasecmp(&p[2], "EOF", 3) + || !strncasecmp(&p[2], "BEGIN", 5) + || !strncasecmp(&p[2], "END", 3) + || !strncasecmp(&p[2], "TRAILER", 7) + )) { + /* check for *p since last line might not end in '\n' */ + while ((c = *p) && (c != '\r') && (c != '\n')) p++; + if ((*p == '\r') && (*(p+1) == '\n')) p += 2; + else if (*p) p++; + continue; } + /* output line */ + while ((c = *p) && (c != '\r') && (c != '\n')) { + fputc(c, of); + p++; + } + if ((*p == '\r') && (*(p+1) == '\n')) p += 2; + else if (*p) p++; + fputc('\n', of); + } } #else void epsf_emit_body(usershape_t *us, FILE *of) diff --git a/lib/gvc/gvusershape.c b/lib/gvc/gvusershape.c index c69fdddd4..6a3542e9c 100644 --- a/lib/gvc/gvusershape.c +++ b/lib/gvc/gvusershape.c @@ -291,12 +291,24 @@ static void ps_size (usershape_t *us) char line[BUFSIZ]; boolean saw_bb; int lx, ly, ux, uy; + char* linep; + char* s; us->dpi = POINTS_PER_INCH; fseek(us->f, 0, SEEK_SET); saw_bb = FALSE; while (fgets(line, sizeof(line), us->f)) { - if (sscanf (line, "%%%%BoundingBox: %d %d %d %d", &lx, &ly, &ux, &uy) == 4) { + /* PostScript accepts \r as EOL, so using fgets () and looking for a + * bounding box comment at the beginning doesn't work in this case. + * As a heuristic, we first search for a bounding box comment in line. + * This obviously fails if not all of the numbers make it into the + * current buffer. This shouldn't be a problem, as the comment is + * typically near the beginning, and so should be read within the first + * BUFSIZ bytes (even on Windows where this is 512). + */ + if (!(linep = strstr (line, "%%BoundingBox:"))) + continue; + if (sscanf (linep, "%%%%BoundingBox: %d %d %d %d", &lx, &ly, &ux, &uy) == 4) { saw_bb = TRUE; break; } -- 2.40.0