libdrawtext
view tools/font2glyphmap/src/font2glyphmap.c @ 86:69e1ccbda826
fixed the glyphmap buffer size bug
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 17 Apr 2014 10:43:03 +0300 |
parents | c750059a1258 |
children | 35675c3e16b4 |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "drawtext.h"
6 #define SUFFIX "glyphmap"
8 struct coderange {
9 int start, end;
10 struct coderange *next;
11 };
13 int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend);
15 int main(int argc, char **argv)
16 {
17 int i, font_size = 12, suffix_len = strlen(SUFFIX);
18 struct coderange *clist = 0;
20 for(i=1; i<argc; i++) {
21 if(argv[i][0] == '-') {
22 if(strcmp(argv[i], "-range") == 0) {
23 struct coderange *node;
24 int start, end;
26 if(sscanf(argv[++i], "%i-%i", &start, &end) != 2) {
27 fprintf(stderr, "-range must be followed by a range of the form: START-END\n");
28 return 1;
29 }
31 if(!(node = malloc(sizeof *node))) {
32 perror("failed to allocate memory");
33 return 1;
34 }
35 node->start = start;
36 node->end = end;
37 node->next = clist;
38 clist = node;
39 } else if(strcmp(argv[i], "-size") == 0) {
40 char *endp;
42 font_size = strtol(argv[++i], &endp, 10);
43 if(endp == argv[i]) {
44 fprintf(stderr, "-size must be followed by the font size\n");
45 return 1;
46 }
47 } else {
48 fprintf(stderr, "invalid option: %s\n", argv[i]);
49 return 1;
50 }
51 } else {
52 char *basename, *dotptr, *outfile;
53 struct dtx_font *font;
55 if(!(font = dtx_open_font(argv[i], clist ? 0 : font_size))) {
56 fprintf(stderr, "failed to open font file: %s\n", argv[i]);
57 return -1;
58 }
60 basename = alloca(strlen(argv[i]) + suffix_len + 1);
61 strcpy(basename, argv[i]);
63 if((dotptr = strrchr(basename, '.'))) {
64 *dotptr = 0;
65 }
67 outfile = alloca(strlen(basename) + 64);
69 if(clist) {
70 while(clist) {
71 struct coderange *r = clist;
72 clist = clist->next;
74 sprintf(outfile, "%s_s%d_r%04x-%04x.%s", basename, font_size, r->start, r->end, SUFFIX);
75 font2glyphmap(font, argv[i], outfile, font_size, r->start, r->end);
77 free(r);
78 }
79 clist = 0;
80 } else {
81 sprintf(outfile, "%s_s%d.%s", basename, font_size, SUFFIX);
82 font2glyphmap(font, argv[i], outfile, font_size, -1, -1);
83 }
84 }
85 }
87 return 0;
88 }
90 int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend)
91 {
92 struct dtx_glyphmap *gmap;
94 if(rstart != -1) {
95 dtx_prepare_range(font, size, rstart, rend);
96 if(!(gmap = dtx_get_font_glyphmap(font, size, rstart))) {
97 fprintf(stderr, "failed to retrieve unicode glyphmap (code range: %d-%d)\n", rstart, rend);
98 return -1;
99 }
100 } else {
101 dtx_prepare(font, size);
102 if(!(gmap = dtx_get_font_glyphmap(font, size, ' '))) {
103 fprintf(stderr, "failed to retrieve ASCII glyphmap!\n");
104 return -1;
105 }
106 }
108 if(dtx_save_glyphmap(outfname, gmap) == -1) {
109 fprintf(stderr, "failed to save glyphmap to: %s\n", outfname);
110 return -1;
111 }
113 return 0;
114 }