libdrawtext

diff tools/font2glyphmap/src/font2glyphmap.c @ 21:c750059a1258

font2glyphmap tool
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 15 Apr 2014 03:33:39 +0300
parents
children 49a190d62271
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/font2glyphmap/src/font2glyphmap.c	Tue Apr 15 03:33:39 2014 +0300
     1.3 @@ -0,0 +1,114 @@
     1.4 +#include <stdio.h>
     1.5 +#include <stdlib.h>
     1.6 +#include <string.h>
     1.7 +#include "drawtext.h"
     1.8 +
     1.9 +#define SUFFIX	"glyphmap"
    1.10 +
    1.11 +struct coderange {
    1.12 +	int start, end;
    1.13 +	struct coderange *next;
    1.14 +};
    1.15 +
    1.16 +int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend);
    1.17 +
    1.18 +int main(int argc, char **argv)
    1.19 +{
    1.20 +	int i, font_size = 12, suffix_len = strlen(SUFFIX);
    1.21 +	struct coderange *clist = 0;
    1.22 +
    1.23 +	for(i=1; i<argc; i++) {
    1.24 +		if(argv[i][0] == '-') {
    1.25 +			if(strcmp(argv[i], "-range") == 0) {
    1.26 +				struct coderange *node;
    1.27 +				int start, end;
    1.28 +
    1.29 +				if(sscanf(argv[++i], "%d-%d", &start, &end) != 2) {
    1.30 +					fprintf(stderr, "-range must be followed by a range of the form: START-END\n");
    1.31 +					return 1;
    1.32 +				}
    1.33 +
    1.34 +				if(!(node = malloc(sizeof *node))) {
    1.35 +					perror("failed to allocate memory");
    1.36 +					return 1;
    1.37 +				}
    1.38 +				node->start = start;
    1.39 +				node->end = end;
    1.40 +				node->next = clist;
    1.41 +				clist = node;
    1.42 +			} else if(strcmp(argv[i], "-size") == 0) {
    1.43 +				char *endp;
    1.44 +
    1.45 +				font_size = strtol(argv[++i], &endp, 10);
    1.46 +				if(endp == argv[i]) {
    1.47 +					fprintf(stderr, "-size must be followed by the font size\n");
    1.48 +					return 1;
    1.49 +				}
    1.50 +			} else {
    1.51 +				fprintf(stderr, "invalid option: %s\n", argv[i]);
    1.52 +				return 1;
    1.53 +			}
    1.54 +		} else {
    1.55 +			char *basename, *dotptr, *outfile;
    1.56 +			struct dtx_font *font;
    1.57 +
    1.58 +			if(!(font = dtx_open_font(argv[i], clist ? 0 : font_size))) {
    1.59 +				fprintf(stderr, "failed to open font file: %s\n", argv[i]);
    1.60 +				return -1;
    1.61 +			}
    1.62 +
    1.63 +			basename = alloca(strlen(argv[i]) + suffix_len + 1);
    1.64 +			strcpy(basename, argv[i]);
    1.65 +
    1.66 +			if((dotptr = strrchr(basename, '.'))) {
    1.67 +				*dotptr = 0;
    1.68 +			}
    1.69 +
    1.70 +			outfile = alloca(strlen(basename) + 64);
    1.71 +
    1.72 +			if(clist) {
    1.73 +				while(clist) {
    1.74 +					struct coderange *r = clist;
    1.75 +					clist = clist->next;
    1.76 +
    1.77 +					sprintf(outfile, "%s_s%d_r%d-%d.%s", basename, font_size, r->start, r->end, SUFFIX);
    1.78 +					font2glyphmap(font, argv[i], outfile, font_size, r->start, r->end);
    1.79 +
    1.80 +					free(r);
    1.81 +				}
    1.82 +				clist = 0;
    1.83 +			} else {
    1.84 +				sprintf(outfile, "%s_s%d.%s", basename, font_size, SUFFIX);
    1.85 +				font2glyphmap(font, argv[i], outfile, font_size, -1, -1);
    1.86 +			}
    1.87 +		}
    1.88 +	}
    1.89 +
    1.90 +	return 0;
    1.91 +}
    1.92 +
    1.93 +int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend)
    1.94 +{
    1.95 +	struct dtx_glyphmap *gmap;
    1.96 +
    1.97 +	if(rstart != -1) {
    1.98 +		dtx_prepare_range(font, size, rstart, rend);
    1.99 +		if(!(gmap = dtx_get_font_glyphmap(font, size, rstart))) {
   1.100 +			fprintf(stderr, "failed to retrieve unicode glyphmap (code range: %d-%d)\n", rstart, rend);
   1.101 +			return -1;
   1.102 +		}
   1.103 +	} else {
   1.104 +		dtx_prepare(font, size);
   1.105 +		if(!(gmap = dtx_get_font_glyphmap(font, size, ' '))) {
   1.106 +			fprintf(stderr, "failed to retrieve ASCII glyphmap!\n");
   1.107 +			return -1;
   1.108 +		}
   1.109 +	}
   1.110 +
   1.111 +	if(dtx_save_glyphmap(outfname, gmap) == -1) {
   1.112 +		fprintf(stderr, "failed to save glyphmap to: %s\n", outfname);
   1.113 +		return -1;
   1.114 +	}
   1.115 +
   1.116 +	return 0;
   1.117 +}