libdrawtext

changeset 73:850335344428

font2glyphmap tool
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Apr 2014 03:33:39 +0300
parents 9fd4bf11cc1e
children 838d473cf6cc
files src/drawtext.h tools/font2glyphmap/Makefile tools/font2glyphmap/src/font2glyphmap.c
diffstat 3 files changed, 139 insertions(+), 1 deletions(-) [+]
line diff
     1.1 --- a/src/drawtext.h	Mon Mar 18 06:06:26 2013 +0200
     1.2 +++ b/src/drawtext.h	Tue Apr 15 03:33:39 2014 +0300
     1.3 @@ -82,7 +82,7 @@
     1.4  int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
     1.5  
     1.6  /* The following functions can be used even when the library is compiled without
     1.7 - * freetype support. (TODO)
     1.8 + * freetype support.
     1.9   */
    1.10  struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
    1.11  struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/tools/font2glyphmap/Makefile	Tue Apr 15 03:33:39 2014 +0300
     2.3 @@ -0,0 +1,24 @@
     2.4 +src = $(wildcard src/*.c)
     2.5 +obj = $(src:.c=.o)
     2.6 +bin = font2glyphmap
     2.7 +
     2.8 +CC = gcc
     2.9 +CFLAGS = -pedantic -Wall -g -I../../src -I/usr/local/include
    2.10 +LDFLAGS = -L. -L/usr/local/lib -ldrawtext
    2.11 +
    2.12 +ifeq ($(shell uname -s), Darwin)
    2.13 +	lib_so = libdrawtext.dylib
    2.14 +else
    2.15 +	lib_so = libdrawtext.so.0.0
    2.16 +endif
    2.17 +
    2.18 +$(bin): $(obj) $(lib_so) $(font)
    2.19 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.20 +
    2.21 +$(lib_so): ../../$(lib_so)
    2.22 +	rm -f $@
    2.23 +	ln -s $< $@
    2.24 +
    2.25 +.PHONY: clean
    2.26 +clean:
    2.27 +	rm -f $(obj) $(bin) $(lib_so)
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/tools/font2glyphmap/src/font2glyphmap.c	Tue Apr 15 03:33:39 2014 +0300
     3.3 @@ -0,0 +1,114 @@
     3.4 +#include <stdio.h>
     3.5 +#include <stdlib.h>
     3.6 +#include <string.h>
     3.7 +#include "drawtext.h"
     3.8 +
     3.9 +#define SUFFIX	"glyphmap"
    3.10 +
    3.11 +struct coderange {
    3.12 +	int start, end;
    3.13 +	struct coderange *next;
    3.14 +};
    3.15 +
    3.16 +int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend);
    3.17 +
    3.18 +int main(int argc, char **argv)
    3.19 +{
    3.20 +	int i, font_size = 12, suffix_len = strlen(SUFFIX);
    3.21 +	struct coderange *clist = 0;
    3.22 +
    3.23 +	for(i=1; i<argc; i++) {
    3.24 +		if(argv[i][0] == '-') {
    3.25 +			if(strcmp(argv[i], "-range") == 0) {
    3.26 +				struct coderange *node;
    3.27 +				int start, end;
    3.28 +
    3.29 +				if(sscanf(argv[++i], "%d-%d", &start, &end) != 2) {
    3.30 +					fprintf(stderr, "-range must be followed by a range of the form: START-END\n");
    3.31 +					return 1;
    3.32 +				}
    3.33 +
    3.34 +				if(!(node = malloc(sizeof *node))) {
    3.35 +					perror("failed to allocate memory");
    3.36 +					return 1;
    3.37 +				}
    3.38 +				node->start = start;
    3.39 +				node->end = end;
    3.40 +				node->next = clist;
    3.41 +				clist = node;
    3.42 +			} else if(strcmp(argv[i], "-size") == 0) {
    3.43 +				char *endp;
    3.44 +
    3.45 +				font_size = strtol(argv[++i], &endp, 10);
    3.46 +				if(endp == argv[i]) {
    3.47 +					fprintf(stderr, "-size must be followed by the font size\n");
    3.48 +					return 1;
    3.49 +				}
    3.50 +			} else {
    3.51 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
    3.52 +				return 1;
    3.53 +			}
    3.54 +		} else {
    3.55 +			char *basename, *dotptr, *outfile;
    3.56 +			struct dtx_font *font;
    3.57 +
    3.58 +			if(!(font = dtx_open_font(argv[i], clist ? 0 : font_size))) {
    3.59 +				fprintf(stderr, "failed to open font file: %s\n", argv[i]);
    3.60 +				return -1;
    3.61 +			}
    3.62 +
    3.63 +			basename = alloca(strlen(argv[i]) + suffix_len + 1);
    3.64 +			strcpy(basename, argv[i]);
    3.65 +
    3.66 +			if((dotptr = strrchr(basename, '.'))) {
    3.67 +				*dotptr = 0;
    3.68 +			}
    3.69 +
    3.70 +			outfile = alloca(strlen(basename) + 64);
    3.71 +
    3.72 +			if(clist) {
    3.73 +				while(clist) {
    3.74 +					struct coderange *r = clist;
    3.75 +					clist = clist->next;
    3.76 +
    3.77 +					sprintf(outfile, "%s_s%d_r%d-%d.%s", basename, font_size, r->start, r->end, SUFFIX);
    3.78 +					font2glyphmap(font, argv[i], outfile, font_size, r->start, r->end);
    3.79 +
    3.80 +					free(r);
    3.81 +				}
    3.82 +				clist = 0;
    3.83 +			} else {
    3.84 +				sprintf(outfile, "%s_s%d.%s", basename, font_size, SUFFIX);
    3.85 +				font2glyphmap(font, argv[i], outfile, font_size, -1, -1);
    3.86 +			}
    3.87 +		}
    3.88 +	}
    3.89 +
    3.90 +	return 0;
    3.91 +}
    3.92 +
    3.93 +int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend)
    3.94 +{
    3.95 +	struct dtx_glyphmap *gmap;
    3.96 +
    3.97 +	if(rstart != -1) {
    3.98 +		dtx_prepare_range(font, size, rstart, rend);
    3.99 +		if(!(gmap = dtx_get_font_glyphmap(font, size, rstart))) {
   3.100 +			fprintf(stderr, "failed to retrieve unicode glyphmap (code range: %d-%d)\n", rstart, rend);
   3.101 +			return -1;
   3.102 +		}
   3.103 +	} else {
   3.104 +		dtx_prepare(font, size);
   3.105 +		if(!(gmap = dtx_get_font_glyphmap(font, size, ' '))) {
   3.106 +			fprintf(stderr, "failed to retrieve ASCII glyphmap!\n");
   3.107 +			return -1;
   3.108 +		}
   3.109 +	}
   3.110 +
   3.111 +	if(dtx_save_glyphmap(outfname, gmap) == -1) {
   3.112 +		fprintf(stderr, "failed to save glyphmap to: %s\n", outfname);
   3.113 +		return -1;
   3.114 +	}
   3.115 +
   3.116 +	return 0;
   3.117 +}