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.
27 #define NGLYPHS (1 << 14)
29 static int rows
, cols
;
31 static int fn_glyphs(int *glyphs
)
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
))
39 if (!mkfn_bitmap(NULL
, j
, rows
, cols
) && isdw(j
))
40 glyphs
[n
++] = DWCHAR
| j
;
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])
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";
70 char *buf
= malloc(rows
* cols
);
72 memcpy(head
.sig
, sig
, strlen(sig
));
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",
90 "usage: mkfn [options] font1.ttf:size ... >font.tf\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
[])
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
];
110 fprintf(stderr
, usage
);
114 for (; i
< argc
; i
++) {
115 char *name
= argv
[i
];
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
);
126 mkfn_dim(&rows
, &cols
);
128 rows
= strchr("-+", hdiff
[0]) ? rows
+ atoi(hdiff
) : atoi(hdiff
);
130 cols
= strchr("-+", wdiff
[0]) ? cols
+ atoi(wdiff
) : atoi(wdiff
);