nuclear@21: #include <stdio.h>
nuclear@21: #include <stdlib.h>
nuclear@21: #include <string.h>
nuclear@21: #include "drawtext.h"
nuclear@21: 
nuclear@21: #define SUFFIX	"glyphmap"
nuclear@44: #define DEF_SIZE	12
nuclear@21: 
nuclear@21: struct coderange {
nuclear@21: 	int start, end;
nuclear@21: 	struct coderange *next;
nuclear@21: };
nuclear@21: 
nuclear@44: void print_usage(const char *argv0);
nuclear@21: int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend);
nuclear@21: 
nuclear@21: int main(int argc, char **argv)
nuclear@21: {
nuclear@44: 	int i, font_size = DEF_SIZE, suffix_len = strlen(SUFFIX);
nuclear@21: 	struct coderange *clist = 0;
nuclear@21: 
nuclear@21: 	for(i=1; i<argc; i++) {
nuclear@21: 		if(argv[i][0] == '-') {
nuclear@21: 			if(strcmp(argv[i], "-range") == 0) {
nuclear@21: 				struct coderange *node;
nuclear@21: 				int start, end;
nuclear@21: 
nuclear@34: 				if(sscanf(argv[++i], "%i-%i", &start, &end) != 2) {
nuclear@21: 					fprintf(stderr, "-range must be followed by a range of the form: START-END\n");
nuclear@21: 					return 1;
nuclear@21: 				}
nuclear@21: 
nuclear@21: 				if(!(node = malloc(sizeof *node))) {
nuclear@21: 					perror("failed to allocate memory");
nuclear@21: 					return 1;
nuclear@21: 				}
nuclear@21: 				node->start = start;
nuclear@21: 				node->end = end;
nuclear@21: 				node->next = clist;
nuclear@21: 				clist = node;
nuclear@21: 			} else if(strcmp(argv[i], "-size") == 0) {
nuclear@21: 				char *endp;
nuclear@21: 
nuclear@21: 				font_size = strtol(argv[++i], &endp, 10);
nuclear@21: 				if(endp == argv[i]) {
nuclear@21: 					fprintf(stderr, "-size must be followed by the font size\n");
nuclear@21: 					return 1;
nuclear@21: 				}
nuclear@21: 			} else {
nuclear@44: 				if(strcmp(argv[i], "-help") != 0 && strcmp(argv[i], "-h") != 0) {
nuclear@44: 					fprintf(stderr, "invalid option: %s\n", argv[i]);
nuclear@44: 				}
nuclear@44: 				print_usage(argv[0]);
nuclear@21: 				return 1;
nuclear@21: 			}
nuclear@21: 		} else {
nuclear@44: 			char *basename, *dotptr, *outfile, *lastslash;
nuclear@21: 			struct dtx_font *font;
nuclear@21: 
nuclear@21: 			if(!(font = dtx_open_font(argv[i], clist ? 0 : font_size))) {
nuclear@21: 				fprintf(stderr, "failed to open font file: %s\n", argv[i]);
nuclear@21: 				return -1;
nuclear@21: 			}
nuclear@21: 
nuclear@21: 			basename = alloca(strlen(argv[i]) + suffix_len + 1);
nuclear@21: 			strcpy(basename, argv[i]);
nuclear@21: 
nuclear@21: 			if((dotptr = strrchr(basename, '.'))) {
nuclear@21: 				*dotptr = 0;
nuclear@21: 			}
nuclear@44: 			if((lastslash = strrchr(basename, '/'))) {
nuclear@44: 				basename = lastslash + 1;
nuclear@44: 			}
nuclear@21: 
nuclear@21: 			outfile = alloca(strlen(basename) + 64);
nuclear@21: 
nuclear@21: 			if(clist) {
nuclear@21: 				while(clist) {
nuclear@21: 					struct coderange *r = clist;
nuclear@21: 					clist = clist->next;
nuclear@21: 
nuclear@34: 					sprintf(outfile, "%s_s%d_r%04x-%04x.%s", basename, font_size, r->start, r->end, SUFFIX);
nuclear@21: 					font2glyphmap(font, argv[i], outfile, font_size, r->start, r->end);
nuclear@21: 
nuclear@21: 					free(r);
nuclear@21: 				}
nuclear@21: 				clist = 0;
nuclear@21: 			} else {
nuclear@21: 				sprintf(outfile, "%s_s%d.%s", basename, font_size, SUFFIX);
nuclear@21: 				font2glyphmap(font, argv[i], outfile, font_size, -1, -1);
nuclear@21: 			}
nuclear@21: 		}
nuclear@21: 	}
nuclear@21: 
nuclear@21: 	return 0;
nuclear@21: }
nuclear@21: 
nuclear@44: void print_usage(const char *argv0)
nuclear@44: {
nuclear@44: 	printf("usage: %s [options] <font-1> [<font-2> ... <font-n>]\n", argv0);
nuclear@44: 	printf("options:\n");
nuclear@44: 	printf("  -size <pt>: point size (default: %d)\n", DEF_SIZE);
nuclear@44: 	printf("  -range <low>-<high>: unicode range (default: ascii)\n");
nuclear@44: 	printf("  -help: print usage information and exit\n");
nuclear@44: }
nuclear@44: 
nuclear@21: int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend)
nuclear@21: {
nuclear@21: 	struct dtx_glyphmap *gmap;
nuclear@21: 
nuclear@21: 	if(rstart != -1) {
nuclear@21: 		dtx_prepare_range(font, size, rstart, rend);
nuclear@21: 		if(!(gmap = dtx_get_font_glyphmap(font, size, rstart))) {
nuclear@21: 			fprintf(stderr, "failed to retrieve unicode glyphmap (code range: %d-%d)\n", rstart, rend);
nuclear@21: 			return -1;
nuclear@21: 		}
nuclear@21: 	} else {
nuclear@21: 		dtx_prepare(font, size);
nuclear@21: 		if(!(gmap = dtx_get_font_glyphmap(font, size, ' '))) {
nuclear@21: 			fprintf(stderr, "failed to retrieve ASCII glyphmap!\n");
nuclear@21: 			return -1;
nuclear@21: 		}
nuclear@21: 	}
nuclear@21: 
nuclear@21: 	if(dtx_save_glyphmap(outfname, gmap) == -1) {
nuclear@21: 		fprintf(stderr, "failed to save glyphmap to: %s\n", outfname);
nuclear@21: 		return -1;
nuclear@21: 	}
nuclear@21: 
nuclear@21: 	return 0;
nuclear@21: }