2005-06-12 Han-Wen Nienhuys <hanwen@xs4all.nl>
+ * 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
(c) 1997--2005 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
-
+
#include "main.hh"
#include <cassert>
/* 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);
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
#include "ttftool.h"
struct TableDirectoryEntry *
-readDirectory (int fd, struct OffsetTable *ot)
+readDirectory (FILE *fd, struct OffsetTable *ot)
{
unsigned n;
int i;
}
char **
-readNamingTable (int fd)
+readNamingTable (FILE *fd)
{
USHORT format;
USHORT nrecords;
char *data;
char **strings;
- position = surely_lseek (fd, 0, SEEK_CUR);
+ position = ftell (fd);
surely_read (fd, &format, sizeof (USHORT));
FIX_UH (format);
}
int
-readMaxpTable (int fd)
+readMaxpTable (FILE *fd)
{
struct
{
}
void
-readHeadTable (int fd, struct HeadTable *ht)
+readHeadTable (FILE *fd, struct HeadTable *ht)
{
surely_read (fd, ht, sizeof (struct HeadTable));
FIX_HeadTable (*ht);
}
int
-readPostTable (int fd, int nglyphs, struct PostTable *pt,
+readPostTable (FILE *fd, int nglyphs, struct PostTable *pt,
struct GlyphName **gt)
{
USHORT nglyphspost;
/*NOTREACHED*/}
void *
-readLocaTable (int fd, int nglyphs, int format)
+readLocaTable (FILE *fd, int nglyphs, int format)
{
int i;
switch (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++)
}
longHorMetric *
-readHmtxTable (int fd, int nummetrics)
+readHmtxTable (FILE *fd, int nummetrics)
{
longHorMetric *metrics;
int i;
}
struct HheaTable *
-readHheaTable (int fd)
+readHheaTable (FILE *fd)
{
struct HheaTable *hhea;
hhea = mymalloc (sizeof (struct HheaTable));
}
int
-readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
+readKernTable (FILE *fd, int **nkep, struct KernEntry0 ***kep)
{
struct KernTable kt;
struct KernSubTableHeader ksth;
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);
}
void
-printPSData (void *out, int fd)
+printPSData (void *out, FILE *fd)
{
static char xdigits[] = "0123456789ABCDEF";
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<");
--- /dev/null
+#ifdef TEST_TTFTOOL
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <assert.h>
+
+#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
#include "types.h"
#include "proto.h"
-#include "file-cookie.hh"
-
+
static void endianness_test (void);
static void usage (char *);
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;
endianness_test ();
- if ((fd = open (infile, O_RDONLY)) < 0)
+ if ((fd = fopen (infile, "rb")) == NULL)
{
syserror ("Error opening input file");
}
lily_cookie_fclose (out);
if (ttf_verbosity >= 1)
fprintf (stderr, "Done.\n");
- close (fd);
+ fclose (fd);
}
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;
}