vtfontcvt.c revision 267012
1/*- 2 * Copyright (c) 2009 The FreeBSD Foundation 3 * All rights reserved. 4 * 5 * This software was developed by Ed Schouten under sponsorship from the 6 * FreeBSD Foundation. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/tools/tools/vt/fontcvt/fontcvt.c 267012 2014-06-03 17:54:18Z emaste $"); 32 33#include <sys/endian.h> 34#include <sys/param.h> 35#include <sys/queue.h> 36 37#include <assert.h> 38#include <stdint.h> 39#include <stdio.h> 40#include <stdlib.h> 41#include <string.h> 42#include <unistd.h> 43 44#define VFNT_MAPS 4 45#define VFNT_MAP_NORMAL 0 46#define VFNT_MAP_BOLD 2 47 48static unsigned int width = 8, wbytes, height = 16; 49 50struct glyph { 51 TAILQ_ENTRY(glyph) g_list; 52 uint8_t *g_data; 53 unsigned int g_index; 54}; 55 56TAILQ_HEAD(glyph_list, glyph); 57static struct glyph_list glyphs[VFNT_MAPS] = { 58 TAILQ_HEAD_INITIALIZER(glyphs[0]), 59 TAILQ_HEAD_INITIALIZER(glyphs[1]), 60 TAILQ_HEAD_INITIALIZER(glyphs[2]), 61 TAILQ_HEAD_INITIALIZER(glyphs[3]), 62}; 63static unsigned int glyph_total, glyph_count[4], glyph_unique, glyph_dupe; 64 65struct mapping { 66 TAILQ_ENTRY(mapping) m_list; 67 unsigned int m_char; 68 unsigned int m_length; 69 struct glyph *m_glyph; 70}; 71 72TAILQ_HEAD(mapping_list, mapping); 73static struct mapping_list maps[VFNT_MAPS] = { 74 TAILQ_HEAD_INITIALIZER(maps[0]), 75 TAILQ_HEAD_INITIALIZER(maps[1]), 76 TAILQ_HEAD_INITIALIZER(maps[2]), 77 TAILQ_HEAD_INITIALIZER(maps[3]), 78}; 79static unsigned int mapping_total, map_count[4], map_folded_count[4], 80 mapping_unique, mapping_dupe; 81 82static void 83usage(void) 84{ 85 86 fprintf(stderr, 87"usage: fontcvt [-w width] [-h height] normal.bdf [bold.bdf] out.fnt\n"); 88 exit(1); 89} 90 91static int 92add_mapping(struct glyph *gl, unsigned int c, unsigned int map_idx) 93{ 94 struct mapping *mp; 95 struct mapping_list *ml; 96 97 mapping_total++; 98 99 if (map_idx >= VFNT_MAP_BOLD) { 100 int found = 0; 101 unsigned normal_map_idx = map_idx - VFNT_MAP_BOLD; 102 103 TAILQ_FOREACH(mp, &maps[normal_map_idx], m_list) { 104 if (mp->m_char < c) 105 continue; 106 else if (mp->m_char > c) 107 break; 108 found = 1; 109 110 /* 111 * No mapping is needed if it's equal to the 112 * normal mapping. 113 */ 114 if (mp->m_glyph == gl) { 115 mapping_dupe++; 116 return (0); 117 } 118 } 119 120 if (!found) { 121 fprintf(stderr, 122 "Character %u not in normal font!\n", c); 123 return (1); 124 } 125 } 126 127 mp = malloc(sizeof *mp); 128 mp->m_char = c; 129 mp->m_glyph = gl; 130 mp->m_length = 0; 131 132 ml = &maps[map_idx]; 133 if (TAILQ_LAST(ml, mapping_list) != NULL && 134 TAILQ_LAST(ml, mapping_list)->m_char >= c) { 135 fprintf(stderr, "Bad ordering at character %u\n", c); 136 return (1); 137 } 138 TAILQ_INSERT_TAIL(ml, mp, m_list); 139 140 map_count[map_idx]++; 141 mapping_unique++; 142 143 return (0); 144} 145 146static struct glyph * 147add_glyph(const uint8_t *bytes, unsigned int map_idx, int fallback) 148{ 149 struct glyph *gl; 150 unsigned int i; 151 152 glyph_total++; 153 glyph_count[map_idx]++; 154 155 for (i = 0; i < VFNT_MAPS; i++) { 156 TAILQ_FOREACH(gl, &glyphs[i], g_list) { 157 if (memcmp(gl->g_data, bytes, wbytes * height) == 0) { 158 glyph_dupe++; 159 return (gl); 160 } 161 } 162 } 163 164 gl = malloc(sizeof *gl); 165 gl->g_data = malloc(wbytes * height); 166 memcpy(gl->g_data, bytes, wbytes * height); 167 if (fallback) 168 TAILQ_INSERT_HEAD(&glyphs[map_idx], gl, g_list); 169 else 170 TAILQ_INSERT_TAIL(&glyphs[map_idx], gl, g_list); 171 172 glyph_unique++; 173 return (gl); 174} 175 176static int 177parse_bitmap_line(uint8_t *left, uint8_t *right, unsigned int line, 178 unsigned int dwidth) 179{ 180 uint8_t *p; 181 unsigned int i, subline; 182 183 if (dwidth != width && dwidth != width * 2) { 184 fprintf(stderr, 185 "Unsupported width %u!\n", dwidth); 186 return (1); 187 } 188 189 /* Move pixel data right to simplify splitting double characters. */ 190 line >>= (howmany(dwidth, 8) * 8) - dwidth; 191 192 for (i = dwidth / width; i > 0; i--) { 193 p = (i == 2) ? right : left; 194 195 subline = line & ((1 << width) - 1); 196 subline <<= (howmany(width, 8) * 8) - width; 197 198 if (wbytes == 1) { 199 *p = subline; 200 } else if (wbytes == 2) { 201 *p++ = subline >> 8; 202 *p = subline; 203 } else { 204 fprintf(stderr, 205 "Unsupported wbytes %u!\n", wbytes); 206 return (1); 207 } 208 209 line >>= width; 210 } 211 212 return (0); 213} 214 215static int 216parse_bdf(const char *filename, unsigned int map_idx) 217{ 218 FILE *fp; 219 char *ln; 220 size_t length; 221 uint8_t bytes[wbytes * height], bytes_r[wbytes * height]; 222 unsigned int curchar = 0, dwidth = 0, i, line; 223 struct glyph *gl; 224 225 fp = fopen(filename, "r"); 226 if (fp == NULL) { 227 perror(filename); 228 return (1); 229 } 230 231 while ((ln = fgetln(fp, &length)) != NULL) { 232 ln[length - 1] = '\0'; 233 234 if (strncmp(ln, "ENCODING ", 9) == 0) { 235 curchar = atoi(ln + 9); 236 } 237 238 if (strncmp(ln, "DWIDTH ", 7) == 0) { 239 dwidth = atoi(ln + 7); 240 } 241 242 if (strcmp(ln, "BITMAP") == 0) { 243 for (i = 0; i < height; i++) { 244 if ((ln = fgetln(fp, &length)) == NULL) { 245 fprintf(stderr, "Unexpected EOF!\n"); 246 return (1); 247 } 248 ln[length - 1] = '\0'; 249 sscanf(ln, "%x", &line); 250 if (parse_bitmap_line(bytes + i * wbytes, 251 bytes_r + i * wbytes, line, dwidth) != 0) 252 return (1); 253 } 254 255 /* Prevent adding two glyphs for 0xFFFD */ 256 if (curchar == 0xFFFD) { 257 if (map_idx < VFNT_MAP_BOLD) 258 gl = add_glyph(bytes, 0, 1); 259 } else if (curchar >= 0x20) { 260 gl = add_glyph(bytes, map_idx, 0); 261 if (add_mapping(gl, curchar, map_idx) != 0) 262 return (1); 263 if (dwidth == width * 2) { 264 gl = add_glyph(bytes_r, map_idx + 1, 0); 265 if (add_mapping(gl, curchar, 266 map_idx + 1) != 0) 267 return (1); 268 } 269 } 270 } 271 } 272 273 return (0); 274} 275 276static void 277number_glyphs(void) 278{ 279 struct glyph *gl; 280 unsigned int i, idx = 0; 281 282 for (i = 0; i < VFNT_MAPS; i++) 283 TAILQ_FOREACH(gl, &glyphs[i], g_list) 284 gl->g_index = idx++; 285} 286 287static void 288write_glyphs(FILE *fp) 289{ 290 struct glyph *gl; 291 unsigned int i; 292 293 for (i = 0; i < VFNT_MAPS; i++) { 294 TAILQ_FOREACH(gl, &glyphs[i], g_list) 295 fwrite(gl->g_data, wbytes * height, 1, fp); 296 } 297} 298 299static void 300fold_mappings(unsigned int map_idx) 301{ 302 struct mapping_list *ml = &maps[map_idx]; 303 struct mapping *mn, *mp, *mbase; 304 305 mp = mbase = TAILQ_FIRST(ml); 306 for (mp = mbase = TAILQ_FIRST(ml); mp != NULL; mp = mn) { 307 mn = TAILQ_NEXT(mp, m_list); 308 if (mn != NULL && mn->m_char == mp->m_char + 1 && 309 mn->m_glyph->g_index == mp->m_glyph->g_index + 1) 310 continue; 311 mbase->m_length = mp->m_char - mbase->m_char + 1; 312 mbase = mp = mn; 313 map_folded_count[map_idx]++; 314 } 315} 316 317struct file_mapping { 318 uint32_t source; 319 uint16_t destination; 320 uint16_t length; 321} __packed; 322 323static void 324write_mappings(FILE *fp, unsigned int map_idx) 325{ 326 struct mapping_list *ml = &maps[map_idx]; 327 struct mapping *mp; 328 struct file_mapping fm; 329 unsigned int i = 0, j = 0; 330 331 TAILQ_FOREACH(mp, ml, m_list) { 332 j++; 333 if (mp->m_length > 0) { 334 i += mp->m_length; 335 fm.source = htobe32(mp->m_char); 336 fm.destination = htobe16(mp->m_glyph->g_index); 337 fm.length = htobe16(mp->m_length - 1); 338 fwrite(&fm, sizeof fm, 1, fp); 339 } 340 } 341 assert(i == j); 342} 343 344struct file_header { 345 uint8_t magic[8]; 346 uint8_t width; 347 uint8_t height; 348 uint16_t pad; 349 uint32_t glyph_count; 350 uint32_t map_count[4]; 351} __packed; 352 353static int 354write_fnt(const char *filename) 355{ 356 FILE *fp; 357 struct file_header fh = { 358 .magic = "VFNT0002", 359 }; 360 361 fp = fopen(filename, "wb"); 362 if (fp == NULL) { 363 perror(filename); 364 return (1); 365 } 366 367 fh.width = width; 368 fh.height = height; 369 fh.glyph_count = htobe32(glyph_unique); 370 fh.map_count[0] = htobe32(map_folded_count[0]); 371 fh.map_count[1] = htobe32(map_folded_count[1]); 372 fh.map_count[2] = htobe32(map_folded_count[2]); 373 fh.map_count[3] = htobe32(map_folded_count[3]); 374 fwrite(&fh, sizeof fh, 1, fp); 375 376 write_glyphs(fp); 377 write_mappings(fp, VFNT_MAP_NORMAL); 378 write_mappings(fp, 1); 379 write_mappings(fp, VFNT_MAP_BOLD); 380 write_mappings(fp, 3); 381 382 return (0); 383} 384 385int 386main(int argc, char *argv[]) 387{ 388 int ch; 389 390 assert(sizeof(struct file_header) == 32); 391 assert(sizeof(struct file_mapping) == 8); 392 393 while ((ch = getopt(argc, argv, "h:w:")) != -1) { 394 switch (ch) { 395 case 'h': 396 height = atoi(optarg); 397 break; 398 case 'w': 399 height = atoi(optarg); 400 break; 401 case '?': 402 default: 403 usage(); 404 } 405 } 406 argc -= optind; 407 argv += optind; 408 409 if (argc < 2 || argc > 3) 410 usage(); 411 412 wbytes = howmany(width, 8); 413 414 if (parse_bdf(argv[0], VFNT_MAP_NORMAL) != 0) 415 return (1); 416 argc--; 417 argv++; 418 if (argc == 2) { 419 if (parse_bdf(argv[0], VFNT_MAP_BOLD) != 0) 420 return (1); 421 argc--; 422 argv++; 423 } 424 number_glyphs(); 425 fold_mappings(0); 426 fold_mappings(1); 427 fold_mappings(2); 428 fold_mappings(3); 429 if (write_fnt(argv[0]) != 0) 430 return (1); 431 432 printf( 433"Statistics:\n" 434"- glyph_total: %5u\n" 435"- glyph_normal: %5u\n" 436"- glyph_normal_right: %5u\n" 437"- glyph_bold: %5u\n" 438"- glyph_bold_right: %5u\n" 439"- glyph_unique: %5u\n" 440"- glyph_dupe: %5u\n" 441"- mapping_total: %5u\n" 442"- mapping_normal: %5u\n" 443"- mapping_normal_folded: %5u\n" 444"- mapping_normal_right: %5u\n" 445"- mapping_normal_right_folded: %5u\n" 446"- mapping_bold: %5u\n" 447"- mapping_bold_folded: %5u\n" 448"- mapping_bold_right: %5u\n" 449"- mapping_bold_right_folded: %5u\n" 450"- mapping_unique: %5u\n" 451"- mapping_dupe: %5u\n", 452 glyph_total, 453 glyph_count[0], 454 glyph_count[1], 455 glyph_count[2], 456 glyph_count[3], 457 glyph_unique, glyph_dupe, 458 mapping_total, 459 map_count[0], map_folded_count[0], 460 map_count[1], map_folded_count[1], 461 map_count[2], map_folded_count[2], 462 map_count[3], map_folded_count[3], 463 mapping_unique, mapping_dupe); 464 465 return (0); 466} 467