diff options
author | Jouke Witteveen <j.witteveen@gmail.com> | 2019-08-02 14:03:15 +0200 |
---|---|---|
committer | Jouke Witteveen <j.witteveen@gmail.com> | 2019-12-25 13:20:57 +0100 |
commit | bdcb892975de167b3d4258859234f0abe375f236 (patch) | |
tree | 56916f5051fad01f63fab8f2a403b8cc0b8422ef | |
parent | 0006f577898129c0c5d5e0996026641605705e08 (diff) |
Accept a BDF font on stdin
This makes it easier to transition from bdftopcf.
Although an OTB file could include fonts from multiple BDF sources, that
functionality is not currently implemented for fonts read from stdin.
Signed-off-by: Jouke Witteveen <j.witteveen@gmail.com>
-rw-r--r-- | fonttosfnt.c | 8 | ||||
-rw-r--r-- | man/fonttosfnt.man | 6 | ||||
-rw-r--r-- | read.c | 62 |
3 files changed, 68 insertions, 8 deletions
diff --git a/fonttosfnt.c b/fonttosfnt.c index 90d3d95..062ef3e 100644 --- a/fonttosfnt.c +++ b/fonttosfnt.c @@ -40,7 +40,7 @@ usage(void) fprintf(stderr, "Usage:\n"); fprintf(stderr, "fonttosfnt [ -v ] [ -c ] [ -b ] [ -r ] [ -g n ] [ -m n ] -o font.otb " - "[ -- ] font ...\n"); + "[ -- ] [ font ] ...\n"); } int @@ -106,7 +106,11 @@ main(int argc, char **argv) font = makeFont(); - while(i < argc) { + if(i == argc) { + rc = readFile(NULL, font); + if(rc != 0) + exit(1); + } else while(i < argc) { rc = readFile(argv[i], font); if(rc != 0) exit(1); diff --git a/man/fonttosfnt.man b/man/fonttosfnt.man index 6b1fd71..473b87c 100644 --- a/man/fonttosfnt.man +++ b/man/fonttosfnt.man @@ -11,11 +11,13 @@ fonttosfnt \- Wrap a bitmap font in a sfnt (OpenType) wrapper .I file.otb [ .B \-\- -] -.IR font ... +] [ +.IR font +] ... .SH DESCRIPTION Wrap a bitmap font or a set of bitmap fonts in a sfnt (TrueType or OpenType) wrapper. +If no font is specified, a BDF font is read from standard input (stdin). .SH OPTIONS .TP .B \-v @@ -27,6 +27,7 @@ THE SOFTWARE. #include <ft2build.h> #include FT_FREETYPE_H +#include FT_MODULE_H #include FT_BDF_H #include "X11/Xos.h" #include "fonttosfnt.h" @@ -36,6 +37,8 @@ THE SOFTWARE. #define CEIL2(x, y) (FLOOR2((x) + (y) - 1, (y))) #define FT_Pos_DOWN(x) (FLOOR2((x),64)) #define FT_Pos_UP(x) (CEIL2((x), 64)) +#define MIN(x, y) (((x) <= (y)) ? (x) : (y)) +#define STREAM_FILE(stream) ((FILE*)stream->descriptor.pointer) static int ft_inited = 0; static FT_Library ft_library; @@ -55,11 +58,45 @@ FT_Ensure_Inited(void) return 0; } +static unsigned long +forwardRead(FT_Stream stream, unsigned long offset, unsigned char *buffer, + unsigned long count) { + unsigned char skip_buffer[BUFSIZ]; + unsigned long skip_count; + FILE *file = STREAM_FILE(stream); + + /* We may be asked to skip forward, but by not doing so we increase our + chance of survival. */ + if(count == 0) + return ferror(file) == 0 ? 0 : 1; + + if(offset < stream->pos) { + fprintf(stderr, "Cannot move backward in input stream.\n"); + return 0; + } + while((skip_count = MIN(BUFSIZ, offset - stream->pos))) { + if(fread(skip_buffer, sizeof(*skip_buffer), skip_count, file) < + skip_count) + return 0; + stream->pos += sizeof(*skip_buffer) * skip_count; + } + + return (unsigned long)fread(buffer, sizeof(*buffer), count, file); +} + +static void +streamClose(FT_Stream stream) { + fclose(STREAM_FILE(stream)); + stream->descriptor.pointer = NULL; + stream->size = 0; +} + int readFile(char *filename, FontPtr font) { int j, k, index; int rc; + FT_Open_Args input = { 0 }; FT_Face face; StrikePtr strike; BitmapPtr bitmap; @@ -74,9 +111,25 @@ readFile(char *filename, FontPtr font) if(rc != 0) return rc; - rc = FT_New_Face(ft_library, filename, 0, &face); + if(filename != NULL) { + input.pathname = filename; + input.flags = FT_OPEN_PATHNAME; + } else { + input.flags = FT_OPEN_STREAM | FT_OPEN_DRIVER; + input.driver = FT_Get_Module(ft_library, "bdf"); + input.stream = calloc(1, sizeof(FT_StreamRec)); + if(input.stream == NULL) + return -1; + + input.stream->size = 0x7FFFFFFF; + input.stream->descriptor.pointer = stdin; + input.stream->read = forwardRead; + input.stream->close = streamClose; + } + rc = FT_Open_Face(ft_library, &input, 0, &face); if(rc != 0) { - fprintf(stderr, "Couldn't open face %s.\n", filename); + fprintf(stderr, "Couldn't open face %s.\n", + filename ? filename : "<stdin>"); return -1; } @@ -110,8 +163,8 @@ readFile(char *filename, FontPtr font) if(verbose_flag) { fprintf(stderr, "%s %s %s: %d sizes%s\n", - filename, face->family_name, face->style_name, - face->num_fixed_sizes, + filename ? filename : "<stdin>", + face->family_name, face->style_name, face->num_fixed_sizes, symbol ? " (symbol)" : ""); } @@ -309,6 +362,7 @@ readFile(char *filename, FontPtr font) } FT_Done_Face(face); + free(input.stream); j = 0; for(int i = 0; i < FONT_CODES; i++) { |