convert_font.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright © 2012 Philipp Brüschweiler
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files (the
  6. * "Software"), to deal in the Software without restriction, including
  7. * without limitation the rights to use, copy, modify, merge, publish,
  8. * distribute, sublicense, and/or sell copies of the Software, and to
  9. * permit persons to whom the Software is furnished to do so, subject to
  10. * the following conditions:
  11. *
  12. * The above copyright notice and this permission notice (including the
  13. * next paragraph) shall be included in all copies or substantial
  14. * portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. */
  25. /*
  26. * This is a small, hacky tool to extract cursors from a .pcf file.
  27. * The information about the file format has been gathered from
  28. * http://fontforge.org/pcf-format.html
  29. */
  30. #include <fcntl.h>
  31. #include <stdint.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #define min(a, b) ((a) < (b) ? (a) : (b))
  39. #define max(a, b) ((a) > (b) ? (a) : (b))
  40. struct glyph {
  41. char *name;
  42. int16_t left_bearing, right_bearing, ascent, descent;
  43. int16_t width, height;
  44. int16_t hotx, hoty;
  45. int32_t data_format;
  46. char *data;
  47. };
  48. static struct {
  49. int count;
  50. struct glyph *glyphs;
  51. } extracted_font = {0, NULL};
  52. #define PCF_PROPERTIES (1<<0)
  53. #define PCF_ACCELERATORS (1<<1)
  54. #define PCF_METRICS (1<<2)
  55. #define PCF_BITMAPS (1<<3)
  56. #define PCF_INK_METRICS (1<<4)
  57. #define PCF_BDF_ENCODINGS (1<<5)
  58. #define PCF_SWIDTHS (1<<6)
  59. #define PCF_GLYPH_NAMES (1<<7)
  60. #define PCF_BDF_ACCELERATORS (1<<8)
  61. #define PCF_DEFAULT_FORMAT 0x00000000
  62. #define PCF_INKBOUNDS 0x00000200
  63. #define PCF_ACCEL_W_INKBOUNDS 0x00000100
  64. #define PCF_COMPRESSED_METRICS 0x00000100
  65. #define PCF_FORMAT_MASK 0xffffff00
  66. struct pcf_header {
  67. char header[4];
  68. int32_t table_count;
  69. struct toc_entry {
  70. int32_t type;
  71. int32_t format;
  72. int32_t size;
  73. int32_t offset;
  74. } tables[0];
  75. };
  76. struct compressed_metrics {
  77. uint8_t left_sided_bearing;
  78. uint8_t right_side_bearing;
  79. uint8_t character_width;
  80. uint8_t character_ascent;
  81. uint8_t character_descent;
  82. };
  83. struct uncompressed_metrics {
  84. int16_t left_sided_bearing;
  85. int16_t right_side_bearing;
  86. int16_t character_width;
  87. int16_t character_ascent;
  88. int16_t character_descent;
  89. uint16_t character_attributes;
  90. };
  91. struct metrics {
  92. int32_t format;
  93. union {
  94. struct {
  95. int16_t count;
  96. struct compressed_metrics compressed_metrics[0];
  97. } compressed;
  98. struct {
  99. int32_t count;
  100. struct uncompressed_metrics uncompressed_metrics[0];
  101. } uncompressed;
  102. };
  103. };
  104. struct glyph_names {
  105. int32_t format;
  106. int32_t glyph_count;
  107. int32_t offsets[0];
  108. };
  109. struct bitmaps {
  110. int32_t format;
  111. int32_t glyph_count;
  112. int32_t offsets[0];
  113. };
  114. static void
  115. handle_compressed_metrics(int32_t count, struct compressed_metrics *m)
  116. {
  117. printf("metrics count: %d\n", count);
  118. extracted_font.count = count;
  119. extracted_font.glyphs = calloc(count, sizeof(struct glyph));
  120. int i;
  121. for (i = 0; i < count; ++i) {
  122. struct glyph *glyph = &extracted_font.glyphs[i];
  123. glyph->left_bearing =
  124. ((int16_t) m[i].left_sided_bearing) - 0x80;
  125. glyph->right_bearing =
  126. ((int16_t) m[i].right_side_bearing) - 0x80;
  127. glyph->width = ((int16_t) m[i].character_width) - 0x80;
  128. glyph->ascent = ((int16_t) m[i].character_ascent) - 0x80;
  129. glyph->descent = ((int16_t) m[i].character_descent) - 0x80;
  130. /* computed stuff */
  131. glyph->height = glyph->ascent + glyph->descent;
  132. glyph->hotx = -glyph->left_bearing;
  133. glyph->hoty = glyph->ascent;
  134. }
  135. }
  136. static void
  137. handle_metrics(void *metricbuf)
  138. {
  139. struct metrics *metrics = metricbuf;
  140. printf("metric format: %x\n", metrics->format);
  141. if ((metrics->format & PCF_FORMAT_MASK) == PCF_DEFAULT_FORMAT) {
  142. printf("todo...\n");
  143. } else if ((metrics->format & PCF_FORMAT_MASK) ==
  144. PCF_COMPRESSED_METRICS) {
  145. handle_compressed_metrics(
  146. metrics->compressed.count,
  147. &metrics->compressed.compressed_metrics[0]);
  148. } else {
  149. printf("incompatible format\n");
  150. abort();
  151. }
  152. }
  153. static void
  154. handle_glyph_names(struct glyph_names *names)
  155. {
  156. printf("glyph count %d\n", names->glyph_count);
  157. if (names->glyph_count != extracted_font.count) {
  158. abort();
  159. }
  160. printf("glyph names format %x\n", names->format);
  161. void *names_start = ((void*) names) + sizeof(struct glyph_names)
  162. + (names->glyph_count + 1) * sizeof(int32_t);
  163. int i;
  164. for (i = 0; i < names->glyph_count; ++i) {
  165. int32_t start = names->offsets[i];
  166. int32_t end = names->offsets[i+1];
  167. char *name = names_start + start;
  168. extracted_font.glyphs[i].name = calloc(1, end - start + 1);
  169. memcpy(extracted_font.glyphs[i].name, name, end - start);
  170. }
  171. }
  172. static void
  173. handle_bitmaps(struct bitmaps *bitmaps)
  174. {
  175. printf("bitmaps count %d\n", bitmaps->glyph_count);
  176. if (bitmaps->glyph_count != extracted_font.count) {
  177. abort();
  178. }
  179. printf("format %x\n", bitmaps->format);
  180. if (bitmaps->format != 2) {
  181. printf("format not yet supported\n");
  182. abort();
  183. }
  184. void *bitmaps_start = ((void*) bitmaps) + sizeof(struct bitmaps)
  185. + (bitmaps->glyph_count + 4) * sizeof(int32_t);
  186. int i;
  187. for (i = 0; i < bitmaps->glyph_count; ++i) {
  188. int32_t offset = bitmaps->offsets[i];
  189. struct glyph *glyph = &extracted_font.glyphs[i];
  190. glyph->data_format = bitmaps->format;
  191. glyph->data = bitmaps_start + offset;
  192. }
  193. }
  194. static void
  195. handle_pcf(void *fontbuf)
  196. {
  197. struct pcf_header *header = fontbuf;
  198. printf("tablecount %d\n", header->table_count);
  199. int i;
  200. for (i = 0; i < header->table_count; ++i) {
  201. struct toc_entry *entry = &header->tables[i];
  202. printf("type: %d\n", entry->type);
  203. if (entry->type == PCF_METRICS) {
  204. handle_metrics(fontbuf + entry->offset);
  205. } else if (entry->type == PCF_GLYPH_NAMES) {
  206. handle_glyph_names(fontbuf + entry->offset);
  207. } else if (entry->type == PCF_BITMAPS) {
  208. handle_bitmaps(fontbuf + entry->offset);
  209. }
  210. }
  211. }
  212. static char
  213. get_glyph_pixel(struct glyph *glyph, int x, int y)
  214. {
  215. int absx = glyph->hotx + x;
  216. int absy = glyph->hoty + y;
  217. if (absx < 0 || absx >= glyph->width ||
  218. absy < 0 || absy >= glyph->height)
  219. return 0;
  220. int stride = (glyph->width + 31) / 32 * 4;
  221. unsigned char block = glyph->data[absy * stride + (absx/8)];
  222. int idx = absx % 8;
  223. return (block >> idx) & 1;
  224. }
  225. static struct {
  226. uint32_t *data;
  227. size_t capacity, size;
  228. } data_buffer;
  229. static void
  230. init_data_buffer()
  231. {
  232. data_buffer.data = malloc(sizeof(uint32_t) * 10);
  233. data_buffer.capacity = 10;
  234. data_buffer.size = 0;
  235. }
  236. static void
  237. add_pixel(uint32_t pixel)
  238. {
  239. if (data_buffer.size == data_buffer.capacity) {
  240. data_buffer.capacity *= 2;
  241. data_buffer.data =
  242. realloc(data_buffer.data,
  243. sizeof(uint32_t) * data_buffer.capacity);
  244. }
  245. data_buffer.data[data_buffer.size++] = pixel;
  246. }
  247. struct reconstructed_glyph {
  248. int32_t width, height;
  249. int32_t hotspot_x, hotspot_y;
  250. size_t offset;
  251. char *name;
  252. };
  253. static void
  254. reconstruct_glyph(struct glyph *cursor, struct glyph *mask, char *name,
  255. struct reconstructed_glyph *glyph)
  256. {
  257. int minx = min(-cursor->hotx, -mask->hotx);
  258. int maxx = max(cursor->right_bearing, mask->right_bearing);
  259. int miny = min(-cursor->hoty, -mask->hoty);
  260. int maxy = max(cursor->height - cursor->hoty,
  261. mask->height - mask->hoty);
  262. int width = maxx - minx;
  263. int height = maxy - miny;
  264. glyph->name = strdup(name);
  265. glyph->width = width;
  266. glyph->height = height;
  267. glyph->hotspot_x = -minx;
  268. glyph->hotspot_y = -miny;
  269. glyph->offset = data_buffer.size;
  270. int x, y;
  271. for (y = miny; y < maxy; ++y) {
  272. for (x = minx; x < maxx; ++x) {
  273. char alpha = get_glyph_pixel(mask, x, y);
  274. if (alpha) {
  275. char color = get_glyph_pixel(cursor, x, y);
  276. if (color)
  277. add_pixel(0xff000000);
  278. else
  279. add_pixel(0xffffffff);
  280. } else {
  281. add_pixel(0);
  282. }
  283. }
  284. }
  285. }
  286. /*
  287. * Originally from
  288. * http://cgit.freedesktop.org/xorg/lib/libXfont/tree/src/builtins/fonts.c
  289. * Changed to the MIT "Expat" style license for Wayland..
  290. */
  291. static const char cursor_licence[] =
  292. "/*\n"
  293. "* Copyright 1999 SuSE, Inc.\n"
  294. "*\n"
  295. "* Permission is hereby granted, free of charge, to any person obtaining\n"
  296. "* a copy of this software and associated documentation files (the\n"
  297. "* \"Software\"), to deal in the Software without restriction, including\n"
  298. "* without limitation the rights to use, copy, modify, merge, publish,\n"
  299. "* distribute, sublicense, and/or sell copies of the Software, and to\n"
  300. "* permit persons to whom the Software is furnished to do so, subject to\n"
  301. "* the following conditions:\n"
  302. "*\n"
  303. "* The above copyright notice and this permission notice (including the\n"
  304. "* next paragraph) shall be included in all copies or substantial\n"
  305. "* portions of the Software.\n"
  306. "*\n"
  307. "* THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND,\n"
  308. "* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n"
  309. "* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n"
  310. "* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\n"
  311. "* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\n"
  312. "* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\n"
  313. "* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n"
  314. "* SOFTWARE.\n"
  315. "*\n"
  316. "* Author: Keith Packard, SuSE, Inc.\n"
  317. "*/\n";
  318. static void
  319. write_output_file(struct reconstructed_glyph *glyphs, int n)
  320. {
  321. int i, j, counter, size;
  322. FILE *file = fopen("cursor-data.h", "w");
  323. uint32_t *data;
  324. fprintf(file, "%s\n", cursor_licence);
  325. fprintf(file, "static uint32_t cursor_data[] = {\n\t");
  326. counter = 0;
  327. for (i = 0; i < n; ++i) {
  328. data = data_buffer.data + glyphs[i].offset;
  329. size = glyphs[i].width * glyphs[i].height;
  330. for (j = 0; j < size; ++j) {
  331. fprintf(file, "0x%08x, ", data[j]);
  332. if (++counter % 6 == 0)
  333. fprintf(file, "\n\t");
  334. }
  335. }
  336. fprintf(file, "\n};\n\n");
  337. fprintf(file,
  338. "static struct {\n"
  339. "\tchar *name;\n"
  340. "\tint width, height;\n"
  341. "\tint hotspot_x, hotspot_y;\n"
  342. "\tsize_t offset;\n"
  343. "} cursor_metadata[] = {\n");
  344. for (i = 0; i < n; ++i)
  345. fprintf(file, "\t{ \"%s\", %d, %d, %d, %d, %zu },\n",
  346. glyphs[i].name,
  347. glyphs[i].width, glyphs[i].height,
  348. glyphs[i].hotspot_x, glyphs[i].hotspot_y,
  349. glyphs[i].offset);
  350. fprintf(file, "};");
  351. fclose(file);
  352. }
  353. struct glyph *
  354. find_mask_glyph(char *name)
  355. {
  356. const char mask[] = "_mask";
  357. const int masklen = strlen(mask);
  358. int len = strlen(name);
  359. int i;
  360. for (i = 0; i < extracted_font.count; ++i) {
  361. struct glyph *g = &extracted_font.glyphs[i];
  362. int l2 = strlen(g->name);
  363. if ((l2 == len + masklen) &&
  364. (memcmp(g->name, name, len) == 0) &&
  365. (memcmp(g->name + len, mask, masklen) == 0)) {
  366. return g;
  367. }
  368. }
  369. return NULL;
  370. }
  371. static void
  372. output_all_cursors()
  373. {
  374. int i, j;
  375. struct reconstructed_glyph *glyphs =
  376. malloc(sizeof(struct reconstructed_glyph) *
  377. extracted_font.count/2);
  378. j = 0;
  379. for (i = 0; i < extracted_font.count; ++i) {
  380. struct glyph *g = &extracted_font.glyphs[i];
  381. if (strstr(g->name, "_mask"))
  382. continue;
  383. struct glyph *mask = find_mask_glyph(g->name);
  384. reconstruct_glyph(g, mask, g->name, &glyphs[j]);
  385. j++;
  386. }
  387. write_output_file(glyphs, extracted_font.count/2);
  388. }
  389. static void
  390. find_cursor_and_mask(const char *name,
  391. struct glyph **cursor,
  392. struct glyph **mask)
  393. {
  394. int i;
  395. char mask_name[100];
  396. sprintf(mask_name, "%s_mask", name);
  397. *cursor = *mask = NULL;
  398. for (i = 0; i < extracted_font.count && (!*mask || !*cursor); ++i) {
  399. struct glyph *g = &extracted_font.glyphs[i];
  400. if (!strcmp(name, g->name))
  401. *cursor = g;
  402. else if (!strcmp(mask_name, g->name))
  403. *mask = g;
  404. }
  405. }
  406. static struct {
  407. char *target_name, *source_name;
  408. } interesting_cursors[] = {
  409. { "bottom_left_corner", "bottom_left_corner" },
  410. { "bottom_right_corner", "bottom_right_corner" },
  411. { "bottom_side", "bottom_side" },
  412. { "grabbing", "fleur" },
  413. { "left_ptr", "left_ptr" },
  414. { "left_side", "left_side" },
  415. { "right_side", "right_side" },
  416. { "top_left_corner", "top_left_corner" },
  417. { "top_right_corner", "top_right_corner" },
  418. { "top_side", "top_side" },
  419. { "xterm", "xterm" },
  420. { "hand1", "hand1" },
  421. { "watch", "watch" }
  422. };
  423. static void
  424. output_interesting_cursors()
  425. {
  426. int i;
  427. int n = sizeof(interesting_cursors) / sizeof(interesting_cursors[0]);
  428. struct reconstructed_glyph *glyphs =
  429. malloc(n * sizeof(*glyphs));
  430. if (!glyphs) {
  431. printf("reconstructed_glyph malloc failed\n");
  432. abort();
  433. }
  434. for (i = 0; i < n; ++i) {
  435. struct glyph *cursor, *mask;
  436. find_cursor_and_mask(interesting_cursors[i].source_name,
  437. &cursor, &mask);
  438. if (!cursor) {
  439. printf("no cursor for %s\n",
  440. interesting_cursors[i].source_name);
  441. abort();
  442. }
  443. if (!mask) {
  444. printf("no mask for %s\n",
  445. interesting_cursors[i].source_name);
  446. abort();
  447. }
  448. reconstruct_glyph(cursor, mask,
  449. interesting_cursors[i].target_name,
  450. &glyphs[i]);
  451. }
  452. write_output_file(glyphs, n);
  453. }
  454. int main()
  455. {
  456. const char filename[] = "cursor.pcf";
  457. int fd = open(filename, O_RDONLY);
  458. struct stat filestat;
  459. fstat(fd, &filestat);
  460. void *fontbuf = mmap(NULL, filestat.st_size, PROT_READ,
  461. MAP_PRIVATE, fd, 0);
  462. handle_pcf(fontbuf);
  463. init_data_buffer();
  464. //output_all_cursors();
  465. output_interesting_cursors();
  466. }