libdrawtext

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