libdrawtext

view tools/font2glyphmap/src/font2glyphmap.c @ 96:6089fcfbd5ea

improvements in font2glyphmap
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 07 Aug 2014 07:02:41 +0300
parents 49a190d62271
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include "drawtext.h"
6 #define SUFFIX "glyphmap"
7 #define DEF_SIZE 12
9 struct coderange {
10 int start, end;
11 struct coderange *next;
12 };
14 void print_usage(const char *argv0);
15 int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend);
17 int main(int argc, char **argv)
18 {
19 int i, font_size = DEF_SIZE, suffix_len = strlen(SUFFIX);
20 struct coderange *clist = 0;
22 for(i=1; i<argc; i++) {
23 if(argv[i][0] == '-') {
24 if(strcmp(argv[i], "-range") == 0) {
25 struct coderange *node;
26 int start, end;
28 if(sscanf(argv[++i], "%i-%i", &start, &end) != 2) {
29 fprintf(stderr, "-range must be followed by a range of the form: START-END\n");
30 return 1;
31 }
33 if(!(node = malloc(sizeof *node))) {
34 perror("failed to allocate memory");
35 return 1;
36 }
37 node->start = start;
38 node->end = end;
39 node->next = clist;
40 clist = node;
41 } else if(strcmp(argv[i], "-size") == 0) {
42 char *endp;
44 font_size = strtol(argv[++i], &endp, 10);
45 if(endp == argv[i]) {
46 fprintf(stderr, "-size must be followed by the font size\n");
47 return 1;
48 }
49 } else {
50 if(strcmp(argv[i], "-help") != 0 && strcmp(argv[i], "-h") != 0) {
51 fprintf(stderr, "invalid option: %s\n", argv[i]);
52 }
53 print_usage(argv[0]);
54 return 1;
55 }
56 } else {
57 char *basename, *dotptr, *outfile, *lastslash;
58 struct dtx_font *font;
60 if(!(font = dtx_open_font(argv[i], clist ? 0 : font_size))) {
61 fprintf(stderr, "failed to open font file: %s\n", argv[i]);
62 return -1;
63 }
65 basename = alloca(strlen(argv[i]) + suffix_len + 1);
66 strcpy(basename, argv[i]);
68 if((dotptr = strrchr(basename, '.'))) {
69 *dotptr = 0;
70 }
71 if((lastslash = strrchr(basename, '/'))) {
72 basename = lastslash + 1;
73 }
75 outfile = alloca(strlen(basename) + 64);
77 if(clist) {
78 while(clist) {
79 struct coderange *r = clist;
80 clist = clist->next;
82 sprintf(outfile, "%s_s%d_r%04x-%04x.%s", basename, font_size, r->start, r->end, SUFFIX);
83 font2glyphmap(font, argv[i], outfile, font_size, r->start, r->end);
85 free(r);
86 }
87 clist = 0;
88 } else {
89 sprintf(outfile, "%s_s%d.%s", basename, font_size, SUFFIX);
90 font2glyphmap(font, argv[i], outfile, font_size, -1, -1);
91 }
92 }
93 }
95 return 0;
96 }
98 void print_usage(const char *argv0)
99 {
100 printf("usage: %s [options] <font-1> [<font-2> ... <font-n>]\n", argv0);
101 printf("options:\n");
102 printf(" -size <pt>: point size (default: %d)\n", DEF_SIZE);
103 printf(" -range <low>-<high>: unicode range (default: ascii)\n");
104 printf(" -help: print usage information and exit\n");
105 }
107 int font2glyphmap(struct dtx_font *font, const char *infname, const char *outfname, int size, int rstart, int rend)
108 {
109 struct dtx_glyphmap *gmap;
111 if(rstart != -1) {
112 dtx_prepare_range(font, size, rstart, rend);
113 if(!(gmap = dtx_get_font_glyphmap(font, size, rstart))) {
114 fprintf(stderr, "failed to retrieve unicode glyphmap (code range: %d-%d)\n", rstart, rend);
115 return -1;
116 }
117 } else {
118 dtx_prepare(font, size);
119 if(!(gmap = dtx_get_font_glyphmap(font, size, ' '))) {
120 fprintf(stderr, "failed to retrieve ASCII glyphmap!\n");
121 return -1;
122 }
123 }
125 if(dtx_save_glyphmap(outfname, gmap) == -1) {
126 fprintf(stderr, "failed to save glyphmap to: %s\n", outfname);
127 return -1;
128 }
130 return 0;
131 }