convert_font.c (14467B)
1 /* 2 * Copyright © 2012 Philipp Brüschweiler 3 * 4 * Permission to use, copy, modify, distribute, and sell this software and its 5 * documentation for any purpose is hereby granted without fee, provided that 6 * the above copyright notice appear in all copies and that both that copyright 7 * notice and this permission notice appear in supporting documentation, and 8 * that the name of the copyright holders not be used in advertising or 9 * publicity pertaining to distribution of the software without specific, 10 * written prior permission. The copyright holders make no representations 11 * about the suitability of this software for any purpose. It is provided "as 12 * is" without express or implied warranty. 13 * 14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 15 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO 16 * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR 17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, 18 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 19 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 20 * OF THIS SOFTWARE. 21 */ 22 23 /* 24 * This is a small, hacky tool to extract cursors from a .pcf file. 25 * The information about the file format has been gathered from 26 * http://fontforge.org/pcf-format.html 27 */ 28 29 #define _POSIX_C_SOURCE 200809L /* strdup, as a tool of the build machine has no project flags */ 30 31 #include <assert.h> 32 #include <fcntl.h> 33 #include <stdint.h> 34 #include <stdio.h> 35 #include <stdlib.h> 36 #include <string.h> 37 #include <sys/mman.h> 38 #include <sys/stat.h> 39 #include <sys/types.h> 40 41 #define min(a, b) ((a) < (b) ? (a) : (b)) 42 #define max(a, b) ((a) > (b) ? (a) : (b)) 43 44 struct glyph { 45 char *name; 46 int16_t left_bearing, right_bearing, ascent, descent; 47 48 int16_t width, height; 49 int16_t hotx, hoty; 50 51 int32_t data_format; 52 char *data; 53 }; 54 55 static struct { 56 int count; 57 struct glyph *glyphs; 58 } extracted_font = {0, NULL}; 59 60 #define PCF_PROPERTIES (1 << 0) 61 #define PCF_ACCELERATORS (1 << 1) 62 #define PCF_METRICS (1 << 2) 63 #define PCF_BITMAPS (1 << 3) 64 #define PCF_INK_METRICS (1 << 4) 65 #define PCF_BDF_ENCODINGS (1 << 5) 66 #define PCF_SWIDTHS (1 << 6) 67 #define PCF_GLYPH_NAMES (1 << 7) 68 #define PCF_BDF_ACCELERATORS (1 << 8) 69 70 #define PCF_DEFAULT_FORMAT 0x00000000 71 #define PCF_INKBOUNDS 0x00000200 72 #define PCF_ACCEL_W_INKBOUNDS 0x00000100 73 #define PCF_COMPRESSED_METRICS 0x00000100 74 75 #define PCF_FORMAT_MASK 0xffffff00 76 77 struct pcf_header { 78 char header[4]; 79 int32_t table_count; 80 struct toc_entry { 81 int32_t type; 82 int32_t format; 83 int32_t size; 84 int32_t offset; 85 } tables[]; 86 }; 87 88 struct compressed_metrics { 89 uint8_t left_sided_bearing; 90 uint8_t right_side_bearing; 91 uint8_t character_width; 92 uint8_t character_ascent; 93 uint8_t character_descent; 94 }; 95 96 struct uncompressed_metrics { 97 int16_t left_sided_bearing; 98 int16_t right_side_bearing; 99 int16_t character_width; 100 int16_t character_ascent; 101 int16_t character_descent; 102 uint16_t character_attributes; 103 }; 104 105 struct metrics { 106 int32_t format; 107 union { 108 struct { 109 int16_t count; 110 struct compressed_metrics compressed_metrics[]; 111 } compressed; 112 struct { 113 int32_t count; 114 struct uncompressed_metrics uncompressed_metrics[]; 115 } uncompressed; 116 }; 117 }; 118 119 struct glyph_names { 120 int32_t format; 121 int32_t glyph_count; 122 int32_t offsets[]; 123 }; 124 125 struct bitmaps { 126 int32_t format; 127 int32_t glyph_count; 128 int32_t offsets[]; 129 }; 130 131 static void 132 handle_compressed_metrics(int32_t count, struct compressed_metrics *m) 133 { 134 #if ENABLE_DEBUG 135 fprintf(stderr, "metrics count: %d\n", count); 136 #endif 137 extracted_font.count = count; 138 extracted_font.glyphs = calloc(count, sizeof(struct glyph)); 139 140 int i; 141 for (i = 0; i < count; ++i) { 142 struct glyph *glyph = &extracted_font.glyphs[i]; 143 glyph->left_bearing = ((int16_t)m[i].left_sided_bearing) - 0x80; 144 glyph->right_bearing = ((int16_t)m[i].right_side_bearing) - 0x80; 145 glyph->width = ((int16_t)m[i].character_width) - 0x80; 146 glyph->ascent = ((int16_t)m[i].character_ascent) - 0x80; 147 glyph->descent = ((int16_t)m[i].character_descent) - 0x80; 148 149 /* computed stuff */ 150 glyph->height = glyph->ascent + glyph->descent; 151 152 glyph->hotx = -glyph->left_bearing; 153 glyph->hoty = glyph->ascent; 154 } 155 } 156 157 static void 158 handle_metrics(void *metricbuf) 159 { 160 struct metrics *metrics = metricbuf; 161 #if ENABLE_DEBUG 162 fprintf(stderr, "metric format: %x\n", metrics->format); 163 #endif 164 165 if ((metrics->format & PCF_FORMAT_MASK) == PCF_DEFAULT_FORMAT) { 166 #if ENABLE_DEBUG 167 fprintf(stderr, "todo...\n"); 168 #endif 169 } else if ((metrics->format & PCF_FORMAT_MASK) == PCF_COMPRESSED_METRICS) { 170 handle_compressed_metrics(metrics->compressed.count, 171 &metrics->compressed.compressed_metrics[0]); 172 } else { 173 #if ENABLE_DEBUG 174 fprintf(stderr, "incompatible format\n"); 175 #endif 176 abort(); 177 } 178 } 179 180 static void 181 handle_glyph_names(struct glyph_names *names) 182 { 183 #if ENABLE_DEBUG 184 fprintf(stderr, "glyph count %d\n", names->glyph_count); 185 #endif 186 187 if (names->glyph_count != extracted_font.count) { 188 abort(); 189 } 190 191 #if ENABLE_DEBUG 192 fprintf(stderr, "glyph names format %x\n", names->format); 193 #endif 194 195 char *names_start = ((char *)names) + sizeof(struct glyph_names) + 196 (names->glyph_count + 1) * sizeof(int32_t); 197 198 int i; 199 for (i = 0; i < names->glyph_count; ++i) { 200 int32_t start = names->offsets[i]; 201 int32_t end = names->offsets[i + 1]; 202 char *name = names_start + start; 203 extracted_font.glyphs[i].name = calloc(1, end - start + 1); 204 memcpy(extracted_font.glyphs[i].name, name, end - start); 205 } 206 } 207 208 static void 209 handle_bitmaps(struct bitmaps *bitmaps) 210 { 211 #if ENABLE_DEBUG 212 fprintf(stderr, "bitmaps count %d\n", bitmaps->glyph_count); 213 #endif 214 215 if (bitmaps->glyph_count != extracted_font.count) { 216 abort(); 217 } 218 #if ENABLE_DEBUG 219 fprintf(stderr, "format %x\n", bitmaps->format); 220 #endif 221 222 if (bitmaps->format != 2) { 223 #if ENABLE_DEBUG 224 fprintf(stderr, "format not yet supported\n"); 225 #endif 226 abort(); 227 } 228 229 char *bitmaps_start = ((char *)bitmaps) + sizeof(struct bitmaps) + 230 (bitmaps->glyph_count + 4) * sizeof(int32_t); 231 232 for (unsigned i = 0; i < bitmaps->glyph_count; ++i) { 233 int32_t offset = bitmaps->offsets[i]; 234 struct glyph *glyph = &extracted_font.glyphs[i]; 235 glyph->data_format = bitmaps->format; 236 237 glyph->data = bitmaps_start + offset; 238 } 239 } 240 241 static void 242 handle_pcf(void *fontbuf) 243 { 244 struct pcf_header *header = fontbuf; 245 #if ENABLE_DEBUG 246 fprintf(stderr, "tablecount %d\n", header->table_count); 247 #endif 248 249 for (unsigned i = 0; i < header->table_count; ++i) { 250 struct toc_entry *entry = &header->tables[i]; 251 #if ENABLE_DEBUG 252 fprintf(stderr, "type: %d\n", entry->type); 253 #endif 254 if (entry->type == PCF_METRICS) { 255 handle_metrics((void *)((uintptr_t)fontbuf + entry->offset)); 256 } else if (entry->type == PCF_GLYPH_NAMES) { 257 handle_glyph_names((void *)((uintptr_t)fontbuf + entry->offset)); 258 } else if (entry->type == PCF_BITMAPS) { 259 handle_bitmaps((void *)((uintptr_t)fontbuf + entry->offset)); 260 } 261 } 262 } 263 264 static char 265 get_glyph_pixel(struct glyph *glyph, int x, int y) 266 { 267 int absx = glyph->hotx + x; 268 int absy = glyph->hoty + y; 269 270 if (absx < 0 || absx >= glyph->width || absy < 0 || absy >= glyph->height) { 271 return 0; 272 } 273 274 int stride = (glyph->width + 31) / 32 * 4; 275 unsigned char block = glyph->data[absy * stride + (absx / 8)]; 276 int idx = absx % 8; 277 return (block >> idx) & 1; 278 } 279 280 static struct { 281 uint32_t *data; 282 size_t capacity, size; 283 } data_buffer; 284 285 static void 286 init_data_buffer(void) 287 { 288 data_buffer.data = malloc(sizeof(uint32_t) * 10); 289 data_buffer.capacity = 10; 290 data_buffer.size = 0; 291 } 292 293 static void 294 add_pixel(uint32_t pixel) 295 { 296 if (data_buffer.size == data_buffer.capacity) { 297 data_buffer.capacity *= 2; 298 data_buffer.data = 299 realloc(data_buffer.data, sizeof(uint32_t) * data_buffer.capacity); 300 } 301 data_buffer.data[data_buffer.size++] = pixel; 302 } 303 304 struct reconstructed_glyph { 305 int32_t width, height; 306 int32_t hotspot_x, hotspot_y; 307 size_t offset; 308 char *name; 309 }; 310 311 static void 312 reconstruct_glyph(struct glyph *cursor, struct glyph *mask, char *name, 313 struct reconstructed_glyph *glyph) 314 { 315 int minx = min(-cursor->hotx, -mask->hotx); 316 int maxx = max(cursor->right_bearing, mask->right_bearing); 317 318 int miny = min(-cursor->hoty, -mask->hoty); 319 int maxy = max(cursor->height - cursor->hoty, mask->height - mask->hoty); 320 321 int width = maxx - minx; 322 int height = maxy - miny; 323 324 glyph->name = strdup(name); 325 glyph->width = width; 326 glyph->height = height; 327 glyph->hotspot_x = -minx; 328 glyph->hotspot_y = -miny; 329 glyph->offset = data_buffer.size; 330 331 int x, y; 332 for (y = miny; y < maxy; ++y) { 333 for (x = minx; x < maxx; ++x) { 334 char alpha = get_glyph_pixel(mask, x, y); 335 if (alpha) { 336 char color = get_glyph_pixel(cursor, x, y); 337 if (color) { 338 add_pixel(0xff000000); 339 } else { 340 add_pixel(0xffffffff); 341 } 342 } else { 343 add_pixel(0); 344 } 345 } 346 } 347 } 348 349 /* From http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/builtins/fonts.c 350 */ 351 static const char cursor_licence[] = 352 "/*\n" 353 "* Copyright 1999 SuSE, Inc.\n" 354 "*\n" 355 "* Permission to use, copy, modify, distribute, and sell this software and " 356 "its\n" 357 "* documentation for any purpose is hereby granted without fee, provided " 358 "that\n" 359 "* the above copyright notice appear in all copies and that both that\n" 360 "* copyright notice and this permission notice appear in supporting\n" 361 "* documentation, and that the name of SuSE not be used in advertising or\n" 362 "* publicity pertaining to distribution of the software without specific,\n" 363 "* written prior permission. SuSE makes no representations about the\n" 364 "* suitability of this software for any purpose. It is provided \"as " 365 "is\"\n" 366 "* without express or implied warranty.\n" 367 "*\n" 368 "* SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING " 369 "ALL\n" 370 "* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL " 371 "SuSE\n" 372 "* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY " 373 "DAMAGES\n" 374 "* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN " 375 "ACTION\n" 376 "* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN\n" 377 "* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.\n" 378 "*\n" 379 "* Author: Keith Packard, SuSE, Inc.\n" 380 "*/\n"; 381 382 static void 383 write_output_file(FILE *file, struct reconstructed_glyph *glyphs, int n) 384 { 385 int i, j, counter, size; 386 uint32_t *data; 387 388 fprintf(file, "%s\n", cursor_licence); 389 390 fprintf(file, "static uint32_t cursor_data[] = {\n\t"); 391 392 counter = 0; 393 for (i = 0; i < n; ++i) { 394 data = data_buffer.data + glyphs[i].offset; 395 size = glyphs[i].width * glyphs[i].height; 396 397 for (j = 0; j < size; ++j) { 398 fprintf(file, "0x%08x, ", data[j]); 399 if (++counter % 6 == 0) { 400 fprintf(file, "\n\t"); 401 } 402 } 403 } 404 fprintf(file, "\n};\n\n"); 405 406 fputs("enum cursor_type {\n", file); 407 408 for (i = 0; i < n; ++i) { 409 fprintf(file, "\tcursor_%s,\n", glyphs[i].name); 410 } 411 412 fputs("};\n\n", file); 413 414 fprintf(file, 415 "static struct cursor {\n" 416 "\tint width, height;\n" 417 "\tint hotspot_x, hotspot_y;\n" 418 "\tsize_t offset;\n" 419 "} cursor_metadata[] = {\n"); 420 421 for (i = 0; i < n; ++i) { 422 fprintf(file, "\t{ %d, %d, %d, %d, %zu }, /* %s */\n", glyphs[i].width, 423 glyphs[i].height, glyphs[i].hotspot_x, glyphs[i].hotspot_y, 424 glyphs[i].offset, glyphs[i].name); 425 } 426 427 fputs("};\n", file); 428 } 429 430 struct glyph * 431 find_mask_glyph(char *name) 432 { 433 const char mask[] = "_mask"; 434 const int masklen = strlen(mask); 435 436 int len = strlen(name); 437 int i; 438 for (i = 0; i < extracted_font.count; ++i) { 439 struct glyph *g = &extracted_font.glyphs[i]; 440 int l2 = strlen(g->name); 441 if ((l2 == len + masklen) && (memcmp(g->name, name, len) == 0) && 442 (memcmp(g->name + len, mask, masklen) == 0)) { 443 return g; 444 } 445 } 446 return NULL; 447 } 448 449 static void 450 find_cursor_and_mask(const char *name, 451 struct glyph **cursor, 452 struct glyph **mask) 453 { 454 int i; 455 char mask_name[100]; 456 snprintf(mask_name, sizeof(mask_name), "%s_mask", name); 457 458 *cursor = *mask = NULL; 459 460 for (i = 0; i < extracted_font.count && (!*mask || !*cursor); ++i) { 461 struct glyph *g = &extracted_font.glyphs[i]; 462 if (!strcmp(name, g->name)) { 463 *cursor = g; 464 } else if (!strcmp(mask_name, g->name)) { 465 *mask = g; 466 } 467 } 468 } 469 470 static struct { 471 char *target_name, *source_name; 472 } interesting_cursors[] = {{"bottom_left_corner", "bottom_left_corner"}, 473 {"bottom_right_corner", "bottom_right_corner"}, 474 {"bottom_side", "bottom_side"}, 475 {"grabbing", "fleur"}, 476 {"left_ptr", "left_ptr"}, 477 {"left_side", "left_side"}, 478 {"right_side", "right_side"}, 479 {"top_left_corner", "top_left_corner"}, 480 {"top_right_corner", "top_right_corner"}, 481 {"top_side", "top_side"}, 482 {"xterm", "xterm"}, 483 {"hand1", "hand1"}, 484 {"watch", "watch"}}; 485 486 static void 487 output_interesting_cursors(FILE *file) 488 { 489 int i; 490 int n = sizeof(interesting_cursors) / sizeof(interesting_cursors[0]); 491 struct reconstructed_glyph *glyphs = malloc(n * sizeof(*glyphs)); 492 493 for (i = 0; i < n; ++i) { 494 struct glyph *cursor, *mask; 495 find_cursor_and_mask(interesting_cursors[i].source_name, &cursor, 496 &mask); 497 if (!cursor) { 498 #if ENABLE_DEBUG 499 fprintf(stderr, "no cursor for %s\n", 500 interesting_cursors[i].source_name); 501 #endif 502 abort(); 503 } 504 if (!mask) { 505 #if ENABLE_DEBUG 506 fprintf(stderr, "no mask for %s\n", 507 interesting_cursors[i].source_name); 508 #endif 509 abort(); 510 } 511 reconstruct_glyph(cursor, mask, interesting_cursors[i].target_name, 512 &glyphs[i]); 513 } 514 515 write_output_file(file, glyphs, n); 516 } 517 518 int 519 main(int argc, char *argv[]) 520 { 521 if (argc != 3) { 522 #if ENABLE_DEBUG 523 fprintf(stderr, "Usage: %s input.pcf output.h\n", argv[0]); 524 #endif 525 return EXIT_FAILURE; 526 } 527 528 int fd = open(argv[1], O_RDONLY); 529 struct stat filestat; 530 531 fstat(fd, &filestat); 532 533 void *fontbuf = mmap(NULL, filestat.st_size, PROT_READ, MAP_PRIVATE, fd, 0); 534 535 handle_pcf(fontbuf); 536 537 init_data_buffer(); 538 539 FILE *file = fopen(argv[2], "w"); 540 output_interesting_cursors(file); 541 fclose(file); 542 543 return EXIT_SUCCESS; 544 }