libdrawtext

annotate tools/font2glyphmap/src/font2glyphmap.c @ 44:35675c3e16b4

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