]>
git.proxmox.com Git - vncterm.git/blob - genfont2.c
3 Copyright (C) 2017 Proxmox Server Solutions GmbH
5 Copyright: vncterm is under GNU GPL, the GNU General Public License.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; version 2 dated June, 1991.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 Author: Dominik Csapak <d.csapak@proxmox.com>
23 This tool converts the unifont.hex file format into
24 a binary format used in vncterm to render glyphs.
34 #define NUMCODEPOINTS 0xFFFF
38 /* parses strings like 00F0 to the integer */
40 parsehex(char *val
, size_t length
)
42 unsigned int value
= 0;
44 for (size_t i
= 0; i
< length
; i
++) {
46 if (val
[i
] >= '0' && val
[i
] <= '9') {
47 value
+= (val
[i
] - '0');
48 } else if (val
[i
] >= 'A' && val
[i
] <= 'F') {
49 value
+= (val
[i
] - 'A' + 10);
50 } else if (val
[i
] >= 'a' && val
[i
] <= 'f') {
51 value
+= (val
[i
] - 'a' + 10);
60 void usage(char **argv
) {
61 printf("Usage: %s [OPTION]...\n", argv
[0]);
62 printf("Converts font data from hex format into binary format used by vncterm.\n");
65 printf(" -o, --output file for output, if omitted, write to STDOUT\n");
66 printf(" -i, --input file for input, if omitted read from STDIN\n");
67 printf(" -h, --help display this help\n");
69 printf("\nThe input has to be formatted in the hex format of unifont.\n");
73 main (int argc
, char** argv
)
79 char *fontfile
= NULL
;
82 uint8_t emptyglyph
[GLYPHLINES
*2] = { 0 };
83 uint8_t glyph
[GLYPHLINES
*2] = { 0 };
84 int nextcodepoint
= 0;
88 static struct option long_options
[] = {
89 {"help", no_argument
, 0, 'h'},
90 {"output", required_argument
, 0, 'o'},
91 {"input", required_argument
, 0, 'i'},
96 while((c
= getopt_long(argc
, argv
, "hi:o:", long_options
, &option_index
)) != -1) {
114 if (fontfile
!= NULL
){
115 fd
= fopen(fontfile
, "r");
117 fprintf(stderr
, "Error opening '%s'\n", fontfile
);
125 if (outfile
!= NULL
) {
126 outfd
= fopen(outfile
, "w");
128 fprintf(stderr
, "Error opening '%s'\n", outfile
);
137 while (getline(&line
, &linesize
, fd
) != -1) {
138 codepoint
= parsehex(line
, INDEXLENGTH
);
139 if (codepoint
== -1) {
140 fprintf(stderr
, "Cannot parse codepoint index: '%s'\n", line
);
145 /* fill in missing codepoints with empty glyphs */
146 while (nextcodepoint
++ < codepoint
) {
147 fwrite(emptyglyph
, sizeof(emptyglyph
), 1, outfd
);
150 tmp
= line
+ INDEXLENGTH
+ 1;
153 /* parse until end of line */
154 while (*(tmp
+i
*2) != '\n' && i
< sizeof(glyph
)) {
155 int value
= parsehex(tmp
+i
*2, 2);
158 fprintf(stderr
, "Cannot parse glyph from line: '%s' at position %ld ('%s')\n", line
, i
*2, tmp
+i
*2);
163 glyph
[i
++] = (uint8_t)value
;
166 /* if we have a 1width glyph, fill the rest with zeroes */
167 while (i
< sizeof(glyph
)) {
171 fwrite(glyph
, sizeof(glyph
), 1, outfd
);
175 perror("Cannot not read line from file");
178 while (nextcodepoint
++ <= NUMCODEPOINTS
) {
179 fwrite(emptyglyph
, sizeof(emptyglyph
), 1, outfd
);