mkfn: switch to ISC
[fbpad_mkfn.git] / mkfn.c
blob5ae083e5458aab19f5af5d8bc6fbbc90f9e509ec
1 /*
2 * fbpad_mkfn - generate fbpad fonts from truetype fonts
4 * Copyright (C) 2009-2022 Ali Gholami Rudi <ali at rudi dot ir>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <ctype.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include "chars.h"
25 #include "mkfn.h"
27 #define NGLYPHS (1 << 14)
29 static int rows, cols;
31 static int fn_glyphs(int *glyphs)
33 int i, j;
34 int n = 0;
35 for (i = 0; i < sizeof(chars) / sizeof(chars[0]); i++) {
36 for (j = chars[i][0]; j <= chars[i][1] && n < NGLYPHS; j++) {
37 if (!mkfn_bitmap(NULL, j, rows, cols))
38 glyphs[n++] = j;
39 if (!mkfn_bitmap(NULL, j, rows, cols) && isdw(j))
40 glyphs[n++] = DWCHAR | j;
43 return n;
46 static int intcmp(void *v1, void *v2)
48 return *(int *) v1 - *(int *) v2;
52 * This tinyfont header is followed by:
54 * glyphs[n] unicode character codes (int)
55 * bitmaps[n] character bitmaps (char[rows * cols])
57 struct tinyfont {
58 char sig[8]; /* tinyfont signature; "tinyfont" */
59 int ver; /* version; 0 */
60 int n; /* number of glyphs */
61 int rows, cols; /* glyph dimensions */
64 /* generate the output tinyfont font */
65 static void output(int fd)
67 char *sig = "tinyfont";
68 struct tinyfont head;
69 int glyphs[NGLYPHS];
70 char *buf = malloc(rows * cols);
71 int i;
72 memcpy(head.sig, sig, strlen(sig));
73 head.ver = 0;
74 head.rows = rows;
75 head.cols = cols;
76 head.n = fn_glyphs(glyphs);
77 qsort(glyphs, head.n, sizeof(glyphs[0]), (void *) intcmp);
78 write(fd, &head, sizeof(head));
79 write(fd, glyphs, sizeof(*glyphs) * head.n);
80 for (i = 0; i < head.n; i++) {
81 mkfn_bitmap(buf, glyphs[i], rows, cols);
82 write(fd, buf, rows * cols);
84 fprintf(stderr, "tinyfont[%5d]: height=%2d width=%2d\n",
85 head.n, rows, cols);
86 free(buf);
89 static char *usage =
90 "usage: mkfn [options] font1.ttf:size ... >font.tf\n"
91 "\noptions:\n"
92 " -h n \t\t set glyph height\n"
93 " -w n \t\t set glyph width\n";
95 int main(int argc, char *argv[])
97 int i;
98 char *wdiff = NULL;
99 char *hdiff = NULL;
100 for (i = 1; i < argc && argv[i][0] == '-'; i++) {
101 if (argv[i][1] == 'w') {
102 wdiff = argv[i][2] ? argv[i] + 2 : argv[++i];
103 } else if (argv[i][1] == 'h') {
104 hdiff = argv[i][2] ? argv[i] + 2 : argv[++i];
105 } else {
106 i = argc;
109 if (i == argc) {
110 fprintf(stderr, usage);
111 return 0;
113 mkfn_init();
114 for (; i < argc; i++) {
115 char *name = argv[i];
116 char *spec = NULL;
117 if (strchr(name, ':')) {
118 spec = strrchr(name, ':') + 1;
119 strrchr(name, ':')[0] = '\0';
121 if (mkfn_font(name, spec)) {
122 fprintf(stderr, "mkfn: failed to load <%s>\n", name);
123 return 1;
126 mkfn_dim(&rows, &cols);
127 if (hdiff)
128 rows = strchr("-+", hdiff[0]) ? rows + atoi(hdiff) : atoi(hdiff);
129 if (wdiff)
130 cols = strchr("-+", wdiff[0]) ? cols + atoi(wdiff) : atoi(wdiff);
131 output(1);
132 mkfn_free();
133 return 0;