From 93b85ebaf9047a55505cc7b2c302dce42cc49d59 Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 12 Jun 2005 16:33:21 +0000 Subject: [PATCH] * ttftool/util.c (surely_lseek): use stdio FILE's for I/O * ttftool/test.c: new file. If compiled with -DTEST_TTFTOOL, create a ttf2ps binary. --- ChangeLog | 5 +++++ lily/main.cc | 2 +- ttftool/include/proto.h | 36 +++++++++++++++++++++-------------- ttftool/parse.c | 24 +++++++++++------------ ttftool/ps.c | 6 +++--- ttftool/test.c | 24 +++++++++++++++++++++++ ttftool/ttfps.c | 10 +++++----- ttftool/util.c | 42 ++++++++++++++++++++--------------------- 8 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 ttftool/test.c diff --git a/ChangeLog b/ChangeLog index 7800f284b8..632d44d2f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,10 @@ 2005-06-12 Han-Wen Nienhuys + * ttftool/util.c (surely_lseek): use stdio FILE's for I/O + + * ttftool/test.c: new file. If compiled with -DTEST_TTFTOOL, + create a ttf2ps binary. + * scm/ps-to-png.scm (make-ps-images): fix rename-page-1 functionality. * scm/backend-library.scm (postscript->png): set rename-page-1 to diff --git a/lily/main.cc b/lily/main.cc index 6ebaae1826..02d399aefe 100644 --- a/lily/main.cc +++ b/lily/main.cc @@ -5,7 +5,7 @@ (c) 1997--2005 Han-Wen Nienhuys */ - + #include "main.hh" #include diff --git a/ttftool/include/proto.h b/ttftool/include/proto.h index a01f1e0651..d879f947c8 100644 --- a/ttftool/include/proto.h +++ b/ttftool/include/proto.h @@ -1,24 +1,24 @@ /* Copyright (c) 1997-1998 by Juliusz Chroboczek */ -struct TableDirectoryEntry *readDirectory (int fd, struct OffsetTable *ot); -char **readNamingTable (int fd); -void readHeadTable (int fd, struct HeadTable *ht); -int readPostTable (int fd, int nglyphs, +struct TableDirectoryEntry *readDirectory (FILE *fd, struct OffsetTable *ot); +char **readNamingTable (FILE *fd); +void readHeadTable (FILE *fd, struct HeadTable *ht); +int readPostTable (FILE *fd, int nglyphs, struct PostTable *pt, struct GlyphName **gnt); -int readMaxpTable (int fd); -void *readLocaTable (int fd, int nglyphs, int format); -struct Box *readGlyfTable (int fd, int nglyphs, int format, void *loca); -longHorMetric *readHmtxTable (int fd, int nummetrics); -struct HheaTable *readHheaTable (int fd); -int readKernTable (int fd, int **nke, struct KernEntry0 ***ke); +int readMaxpTable (FILE *fd); +void *readLocaTable (FILE *fd, int nglyphs, int format); +struct Box *readGlyfTable (FILE *fd, int nglyphs, int format, void *loca); +longHorMetric *readHmtxTable (FILE *fd, int nummetrics); +struct HheaTable *readHheaTable (FILE *fd); +int readKernTable (FILE *fd, int **nke, struct KernEntry0 ***ke); void printPSFont (void * out, struct HeadTable *ht, char **strings, int nglyphs, int postType, - struct PostTable *pt, struct GlyphName *gnt, int fd); + struct PostTable *pt, struct GlyphName *gnt, FILE *fd); void printPSHeader (void * out, struct HeadTable *ht, char **strings, struct PostTable *pt); -void printPSData (void * out, int fd); +void printPSData (void * out, FILE *fd); void printPSTrailer (void * out, int nglyphs, int postType, struct GlyphName *gnt); @@ -50,11 +50,19 @@ void *mycalloc (size_t nelem, size_t elsize); void *myrealloc (void *ptr, size_t size); void ttf_error (char *string); void syserror (char *string); -ssize_t surely_read (int fildes, void *buf, size_t nbyte); +ssize_t surely_read (FILE *fildes, void *buf, size_t nbyte); char *unistrncpy (char *dst, char *str, size_t length); void fputpss (char *s, void * stream); -off_t surely_lseek (int fildes, off_t offset, int whence); +void surely_lseek (FILE *fildes, off_t offset, int whence); unsigned hash (char *string); struct hashtable *make_hashtable (int size); int puthash (struct hashtable *t, char *key, int value); int gethash (struct hashtable *t, char *key); + +#ifdef TEST_TTFTOOL +#define lily_cookie_fclose fclose +#define lily_cookie_fprintf fprintf +#define lily_cookie_putc fputc +#else +#include "file-cookie.hh" +#endif diff --git a/ttftool/parse.c b/ttftool/parse.c index 95a605ee43..d198475c4b 100644 --- a/ttftool/parse.c +++ b/ttftool/parse.c @@ -11,7 +11,7 @@ #include "ttftool.h" struct TableDirectoryEntry * -readDirectory (int fd, struct OffsetTable *ot) +readDirectory (FILE *fd, struct OffsetTable *ot) { unsigned n; int i; @@ -32,7 +32,7 @@ readDirectory (int fd, struct OffsetTable *ot) } char ** -readNamingTable (int fd) +readNamingTable (FILE *fd) { USHORT format; USHORT nrecords; @@ -43,7 +43,7 @@ readNamingTable (int fd) char *data; char **strings; - position = surely_lseek (fd, 0, SEEK_CUR); + position = ftell (fd); surely_read (fd, &format, sizeof (USHORT)); FIX_UH (format); @@ -114,7 +114,7 @@ readNamingTable (int fd) } int -readMaxpTable (int fd) +readMaxpTable (FILE *fd) { struct { @@ -131,7 +131,7 @@ readMaxpTable (int fd) } void -readHeadTable (int fd, struct HeadTable *ht) +readHeadTable (FILE *fd, struct HeadTable *ht) { surely_read (fd, ht, sizeof (struct HeadTable)); FIX_HeadTable (*ht); @@ -149,7 +149,7 @@ readHeadTable (int fd, struct HeadTable *ht) } int -readPostTable (int fd, int nglyphs, struct PostTable *pt, +readPostTable (FILE *fd, int nglyphs, struct PostTable *pt, struct GlyphName **gt) { USHORT nglyphspost; @@ -226,7 +226,7 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt, /*NOTREACHED*/} void * -readLocaTable (int fd, int nglyphs, int format) +readLocaTable (FILE *fd, int nglyphs, int format) { int i; switch (format) @@ -255,13 +255,13 @@ readLocaTable (int fd, int nglyphs, int format) /*NOTREACHED*/} struct Box * -readGlyfTable (int fd, int nglyphs, int format, void *loca) +readGlyfTable (FILE *fd, int nglyphs, int format, void *loca) { int i; struct Box *bbox; off_t base, offset; - base = surely_lseek (fd, 0, SEEK_CUR); + base = ftell (fd); bbox = mymalloc (nglyphs * sizeof (struct Box)); for (i = 0; i < nglyphs; i++) @@ -278,7 +278,7 @@ readGlyfTable (int fd, int nglyphs, int format, void *loca) } longHorMetric * -readHmtxTable (int fd, int nummetrics) +readHmtxTable (FILE *fd, int nummetrics) { longHorMetric *metrics; int i; @@ -293,7 +293,7 @@ readHmtxTable (int fd, int nummetrics) } struct HheaTable * -readHheaTable (int fd) +readHheaTable (FILE *fd) { struct HheaTable *hhea; hhea = mymalloc (sizeof (struct HheaTable)); @@ -308,7 +308,7 @@ readHheaTable (int fd) } int -readKernTable (int fd, int **nkep, struct KernEntry0 ***kep) +readKernTable (FILE *fd, int **nkep, struct KernEntry0 ***kep) { struct KernTable kt; struct KernSubTableHeader ksth; diff --git a/ttftool/ps.c b/ttftool/ps.c index d672f3b5f7..94104d7bdc 100644 --- a/ttftool/ps.c +++ b/ttftool/ps.c @@ -25,7 +25,7 @@ void printPSFont (void *out, struct HeadTable *ht, char **strings, int nglyphs, int postType, - struct PostTable *pt, struct GlyphName *gnt, int fd) + struct PostTable *pt, struct GlyphName *gnt, FILE *fd) { printPSHeader (out, ht, strings, pt); printPSData (out, fd); @@ -81,7 +81,7 @@ printPSHeader (void *out, struct HeadTable *ht, } void -printPSData (void *out, int fd) +printPSData (void *out, FILE *fd) { static char xdigits[] = "0123456789ABCDEF"; @@ -95,7 +95,7 @@ printPSData (void *out, int fd) lily_cookie_fprintf (out, "/sfnts ["); for (;;) { - i = read (fd, buffer, CHUNKSIZE); + i = fread (buffer, 1, CHUNKSIZE, fd); if (i == 0) break; lily_cookie_fprintf (out, "\n<"); diff --git a/ttftool/test.c b/ttftool/test.c new file mode 100644 index 0000000000..76c18f5eb1 --- /dev/null +++ b/ttftool/test.c @@ -0,0 +1,24 @@ +#ifdef TEST_TTFTOOL + +#include +#include +#include + +#include "ttftool.h" + + +main (int argc, char **argv) +{ + FILE *in, *out; + if (argc != 3) + { + fprintf (stderr, "test foo.ttf bar.pfa"); + exit (2); + } + + out = fopen (argv[2], "w"); + assert(out); + create_type42 (argv[1], (void*) out); +} + +#endif diff --git a/ttftool/ttfps.c b/ttftool/ttfps.c index 8931949574..2be555f5f0 100644 --- a/ttftool/ttfps.c +++ b/ttftool/ttfps.c @@ -8,8 +8,7 @@ #include "types.h" #include "proto.h" -#include "file-cookie.hh" - + static void endianness_test (void); static void usage (char *); @@ -18,7 +17,8 @@ int ttf_verbosity = 0; void create_type42 (const char *infile, void *out) { - int fd, i; + FILE *fd = 0; + int i; struct OffsetTable ot; struct HeadTable *ht; struct PostTable *pt; @@ -41,7 +41,7 @@ create_type42 (const char *infile, void *out) endianness_test (); - if ((fd = open (infile, O_RDONLY)) < 0) + if ((fd = fopen (infile, "rb")) == NULL) { syserror ("Error opening input file"); } @@ -122,7 +122,7 @@ create_type42 (const char *infile, void *out) lily_cookie_fclose (out); if (ttf_verbosity >= 1) fprintf (stderr, "Done.\n"); - close (fd); + fclose (fd); } diff --git a/ttftool/util.c b/ttftool/util.c index ce794ba118..12f29da1f9 100644 --- a/ttftool/util.c +++ b/ttftool/util.c @@ -58,41 +58,39 @@ myrealloc (void *ptr, size_t size) return p; } -off_t -surely_lseek (int fildes, off_t offset, int whence) +void +show_fpos (int fd) +{ + off_t here = lseek (fd, 0, SEEK_CUR); + off_t end = lseek (fd, 0, SEEK_END); + fprintf (stderr, "here %d end %d", here, end); + lseek (fd, here, SEEK_SET); +} + +void +surely_lseek (FILE *fildes, off_t offset, int whence) { + if (ttf_verbosity >= 3) + fprintf (stderr, "Seeking to %d %d\n", whence, offset); + off_t result; - if ((result = lseek (fildes, offset, whence)) < 0) + if ((result = fseek (fildes, (long) offset, whence)) < 0) { char s[100]; - sprintf (s, "Cannot seek to %d %ld", whence, offset); + sprintf (s, "Cannot seek"); syserror (s); } - return result; } ssize_t -surely_read (int fildes, void *buf, size_t nbyte) +surely_read (FILE *fildes, void *buf, size_t nbyte) { if (ttf_verbosity >= 3) fprintf (stderr, "Reading %d bytes\n", nbyte); + if (nbyte == 0) + return 0; - ssize_t n; - void *bufptr = buf; - while (nbyte > 0 - && (n = read (fildes, bufptr, nbyte)) > 0) - { - bufptr += n; - nbyte -= n; - } - - if (n < 0 || nbyte > 0) - { - char s[100]; - sprintf (s, "error during read(), n = %d, nbyte = %d", n, nbyte); - syserror (s); - } - + int items = fread (buf, nbyte, 1, fildes); return nbyte; } -- 2.39.5