grub-mkfont.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009,2010 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <config.h>
  19. #include <grub/types.h>
  20. #include <grub/util/misc.h>
  21. #include <grub/misc.h>
  22. #include <grub/handler.h>
  23. #include <grub/i18n.h>
  24. #include <grub/fontformat.h>
  25. #include "progname.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <getopt.h>
  30. #include <ft2build.h>
  31. #include FT_FREETYPE_H
  32. #include <freetype/ftsynth.h>
  33. void
  34. grub_putchar (int c)
  35. {
  36. putchar (c);
  37. }
  38. int
  39. grub_getkey (void)
  40. {
  41. return -1;
  42. }
  43. struct grub_handler_class grub_term_input_class;
  44. struct grub_handler_class grub_term_output_class;
  45. void
  46. grub_refresh (void)
  47. {
  48. fflush (stdout);
  49. }
  50. char *
  51. grub_env_get (const char *name __attribute__ ((unused)))
  52. {
  53. return NULL;
  54. }
  55. #define GRUB_FONT_DEFAULT_SIZE 16
  56. #define GRUB_FONT_RANGE_BLOCK 1024
  57. struct grub_glyph_info
  58. {
  59. struct grub_glyph_info *next;
  60. grub_uint32_t char_code;
  61. int width;
  62. int height;
  63. int x_ofs;
  64. int y_ofs;
  65. int device_width;
  66. int bitmap_size;
  67. grub_uint8_t bitmap[0];
  68. };
  69. enum file_formats
  70. {
  71. PF2,
  72. ASCII_BITMAPS
  73. };
  74. #define GRUB_FONT_FLAG_BOLD 1
  75. #define GRUB_FONT_FLAG_NOBITMAP 2
  76. #define GRUB_FONT_FLAG_NOHINTING 4
  77. #define GRUB_FONT_FLAG_FORCEHINT 8
  78. struct grub_font_info
  79. {
  80. char* name;
  81. int style;
  82. int desc;
  83. int asce;
  84. int size;
  85. int max_width;
  86. int max_height;
  87. int min_y;
  88. int max_y;
  89. int flags;
  90. int num_range;
  91. int num_point;
  92. grub_uint32_t *ranges;
  93. grub_uint32_t *points;
  94. struct grub_glyph_info *glyph;
  95. };
  96. static struct option options[] =
  97. {
  98. {"output", required_argument, 0, 'o'},
  99. {"name", required_argument, 0, 'n'},
  100. {"index", required_argument, 0, 'I'},
  101. {"range", required_argument, 0, 'r'},
  102. {"size", required_argument, 0, 's'},
  103. {"desc", required_argument, 0, 'd'},
  104. {"asce", required_argument, 0, 'c'},
  105. {"bold", no_argument, 0, 'b'},
  106. {"no-bitmap", no_argument, 0, 0x100},
  107. {"no-hinting", no_argument, 0, 0x101},
  108. {"add-ascii", no_argument, 0, 0x103},
  109. {"add-text", required_argument, 0, 't'},
  110. {"force-autohint", no_argument, 0, 'a'},
  111. {"info", no_argument, 0, 'i'},
  112. {"help", no_argument, 0, 'h'},
  113. {"version", no_argument, 0, 'V'},
  114. {"verbose", no_argument, 0, 'v'},
  115. {"ascii-bitmaps", no_argument, 0, 0x102},
  116. {0, 0, 0, 0}
  117. };
  118. int font_verbosity;
  119. static void
  120. usage (int status)
  121. {
  122. if (status)
  123. fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
  124. else
  125. printf ("\
  126. Usage: %s [OPTIONS] FONT_FILES\n\
  127. \nOptions:\n\
  128. -o, --output=FILE_NAME set output file name\n\
  129. --ascii-bitmaps save only the ASCII bitmaps\n\
  130. -I, --index=N set face index\n\
  131. -r, --range=A-B[,C-D] set font range\n\
  132. -n, --name=S set font family name\n\
  133. -s, --size=N set font size\n\
  134. -d, --desc=N set font descent\n\
  135. -c, --asce=N set font ascent\n\
  136. -b, --bold convert to bold font\n\
  137. -a, --force-autohint force autohint\n\
  138. --no-hinting disable hinting\n\
  139. --no-bitmap ignore bitmap strikes when loading\n\
  140. --add-ascii add ascii characters\n\
  141. --add-text,-t FILENAME add characters from sample file\n\
  142. -i, --info print pf2 font information\n\
  143. -h, --help display this message and exit\n\
  144. -V, --version print version information and exit\n\
  145. -v, --verbose print verbose messages\n\
  146. \n\
  147. Report bugs to <%s>.\n", program_name, PACKAGE_BUGREPORT);
  148. exit (status);
  149. }
  150. void
  151. add_pixel (grub_uint8_t **data, int *mask, int not_blank)
  152. {
  153. if (*mask == 0)
  154. {
  155. (*data)++;
  156. **data = 0;
  157. *mask = 128;
  158. }
  159. if (not_blank)
  160. **data |= *mask;
  161. *mask >>= 1;
  162. }
  163. void
  164. add_char (struct grub_font_info *font_info, FT_Face face,
  165. grub_uint32_t char_code)
  166. {
  167. struct grub_glyph_info *glyph_info, **p_glyph;
  168. int width, height;
  169. grub_uint8_t *data;
  170. int mask, i, j, bitmap_size;
  171. FT_GlyphSlot glyph;
  172. int flag = FT_LOAD_RENDER | FT_LOAD_MONOCHROME;
  173. if (font_info->flags & GRUB_FONT_FLAG_NOBITMAP)
  174. flag |= FT_LOAD_NO_BITMAP;
  175. if (font_info->flags & GRUB_FONT_FLAG_NOHINTING)
  176. flag |= FT_LOAD_NO_HINTING;
  177. else if (font_info->flags & GRUB_FONT_FLAG_FORCEHINT)
  178. flag |= FT_LOAD_FORCE_AUTOHINT;
  179. if (FT_Load_Char (face, char_code, flag))
  180. return;
  181. glyph = face->glyph;
  182. if (font_info->flags & GRUB_FONT_FLAG_BOLD)
  183. FT_GlyphSlot_Embolden (glyph);
  184. p_glyph = &font_info->glyph;
  185. while ((*p_glyph) && ((*p_glyph)->char_code > char_code))
  186. {
  187. p_glyph = &(*p_glyph)->next;
  188. }
  189. /* Ignore duplicated glyph. */
  190. if ((*p_glyph) && ((*p_glyph)->char_code == char_code))
  191. return;
  192. width = glyph->bitmap.width;
  193. height = glyph->bitmap.rows;
  194. bitmap_size = ((width * height + 7) / 8);
  195. glyph_info = xmalloc (sizeof (struct grub_glyph_info) + bitmap_size);
  196. glyph_info->bitmap_size = bitmap_size;
  197. glyph_info->next = *p_glyph;
  198. *p_glyph = glyph_info;
  199. glyph_info->char_code = char_code;
  200. glyph_info->width = width;
  201. glyph_info->height = height;
  202. glyph_info->x_ofs = glyph->bitmap_left;
  203. glyph_info->y_ofs = glyph->bitmap_top - height;
  204. glyph_info->device_width = glyph->metrics.horiAdvance / 64;
  205. if (width > font_info->max_width)
  206. font_info->max_width = width;
  207. if (height > font_info->max_height)
  208. font_info->max_height = height;
  209. if (glyph_info->y_ofs < font_info->min_y && glyph_info->y_ofs > -font_info->size)
  210. font_info->min_y = glyph_info->y_ofs;
  211. if (glyph_info->y_ofs + height > font_info->max_y)
  212. font_info->max_y = glyph_info->y_ofs + height;
  213. mask = 0;
  214. data = &glyph_info->bitmap[0] - 1;
  215. for (j = 0; j < height; j++)
  216. for (i = 0; i < width; i++)
  217. add_pixel (&data, &mask,
  218. glyph->bitmap.buffer[i / 8 + j * glyph->bitmap.pitch] &
  219. (1 << (7 - (i & 7))));
  220. }
  221. void
  222. add_font (struct grub_font_info *font_info, FT_Face face)
  223. {
  224. if ((font_info->num_range) || (font_info->num_point))
  225. {
  226. int i;
  227. grub_uint32_t j;
  228. for (i = 0; i < font_info->num_range; i++)
  229. for (j = font_info->ranges[i * 2]; j <= font_info->ranges[i * 2 + 1];
  230. j++)
  231. add_char (font_info, face, j);
  232. for (i = 0; i < font_info->num_point; i++)
  233. add_char (font_info, face, font_info->points[i]);
  234. }
  235. else
  236. {
  237. grub_uint32_t char_code, glyph_index;
  238. for (char_code = FT_Get_First_Char (face, &glyph_index);
  239. glyph_index;
  240. char_code = FT_Get_Next_Char (face, char_code, &glyph_index))
  241. add_char (font_info, face, char_code);
  242. }
  243. }
  244. void
  245. write_string_section (char *name, char *str, int* offset, FILE* file)
  246. {
  247. grub_uint32_t leng, leng_be32;
  248. leng = strlen (str) + 1;
  249. leng_be32 = grub_cpu_to_be32 (leng);
  250. grub_util_write_image (name, 4, file);
  251. grub_util_write_image ((char *) &leng_be32, 4, file);
  252. grub_util_write_image (str, leng, file);
  253. *offset += 8 + leng;
  254. }
  255. void
  256. write_be16_section (char *name, grub_uint16_t data, int* offset, FILE* file)
  257. {
  258. grub_uint32_t leng;
  259. leng = grub_cpu_to_be32 (2);
  260. data = grub_cpu_to_be16 (data);
  261. grub_util_write_image (name, 4, file);
  262. grub_util_write_image ((char *) &leng, 4, file);
  263. grub_util_write_image ((char *) &data, 2, file);
  264. *offset += 10;
  265. }
  266. void
  267. print_glyphs (struct grub_font_info *font_info)
  268. {
  269. int num;
  270. struct grub_glyph_info *glyph;
  271. char line[512];
  272. for (glyph = font_info->glyph, num = 0; glyph; glyph = glyph->next, num++)
  273. {
  274. int x, y, xmax, xmin, ymax, ymin;
  275. grub_uint8_t *bitmap, mask;
  276. printf ("\nGlyph #%d, U+%04x\n", num, glyph->char_code);
  277. printf ("Width %d, Height %d, X offset %d, Y offset %d,"
  278. "Device width %d\n",
  279. glyph->width, glyph->height, glyph->x_ofs, glyph->y_ofs,
  280. glyph->device_width);
  281. xmax = glyph->x_ofs + glyph->width;
  282. if (xmax < glyph->device_width)
  283. xmax = glyph->device_width;
  284. xmin = glyph->x_ofs;
  285. if (xmin > 0)
  286. xmin = 0;
  287. ymax = glyph->y_ofs + glyph->height;
  288. if (ymax < font_info->asce)
  289. ymax = font_info->asce;
  290. ymin = glyph->y_ofs;
  291. if (ymin > - font_info->desc)
  292. ymin = - font_info->desc;
  293. bitmap = glyph->bitmap;
  294. mask = 0x80;
  295. for (y = ymax - 1; y >= ymin; y--)
  296. {
  297. int line_pos;
  298. line_pos = 0;
  299. for (x = xmin; x < xmax; x++)
  300. {
  301. if ((x >= glyph->x_ofs) &&
  302. (x < glyph->x_ofs + glyph->width) &&
  303. (y >= glyph->y_ofs) &&
  304. (y < glyph->y_ofs + glyph->height))
  305. {
  306. line[line_pos++] = (*bitmap & mask) ? '#' : '_';
  307. mask >>= 1;
  308. if (mask == 0)
  309. {
  310. mask = 0x80;
  311. bitmap++;
  312. }
  313. }
  314. else if ((x >= 0) &&
  315. (x < glyph->device_width) &&
  316. (y >= - font_info->desc) &&
  317. (y < font_info->asce))
  318. {
  319. line[line_pos++] = ((x == 0) || (y == 0)) ? '+' : '.';
  320. }
  321. else
  322. line[line_pos++] = '*';
  323. }
  324. line[line_pos] = 0;
  325. printf ("%s\n", line);
  326. }
  327. }
  328. }
  329. void
  330. write_font_ascii_bitmap (struct grub_font_info *font_info, char *output_file)
  331. {
  332. FILE *file;
  333. struct grub_glyph_info *glyph;
  334. int num;
  335. file = fopen (output_file, "wb");
  336. if (! file)
  337. grub_util_error ("Can\'t write to file %s.", output_file);
  338. int correct_size;
  339. for (glyph = font_info->glyph, num = 0; glyph; glyph = glyph->next, num++)
  340. {
  341. correct_size = 1;
  342. if (glyph->width != 8 || glyph->height != 16)
  343. {
  344. /* printf ("Width or height from glyph U+%04x not supported, skipping.\n", glyph->char_code); */
  345. correct_size = 0;
  346. }
  347. int row;
  348. for (row = 0; row < glyph->height; row++)
  349. {
  350. if (correct_size)
  351. fwrite (&glyph->bitmap[row], sizeof(glyph->bitmap[row]), 1, file);
  352. else
  353. fwrite (&correct_size, 1, 1, file);
  354. }
  355. }
  356. fclose (file);
  357. }
  358. void
  359. write_font_pf2 (struct grub_font_info *font_info, char *output_file)
  360. {
  361. FILE *file;
  362. grub_uint32_t leng, data;
  363. char style_name[20], *font_name;
  364. struct grub_glyph_info *cur, *pre;
  365. int num, offset;
  366. file = fopen (output_file, "wb");
  367. if (! file)
  368. grub_util_error ("can\'t write to file %s.", output_file);
  369. offset = 0;
  370. leng = grub_cpu_to_be32 (4);
  371. grub_util_write_image (FONT_FORMAT_SECTION_NAMES_FILE,
  372. sizeof(FONT_FORMAT_SECTION_NAMES_FILE) - 1, file);
  373. grub_util_write_image ((char *) &leng, 4, file);
  374. grub_util_write_image (FONT_FORMAT_PFF2_MAGIC, 4, file);
  375. offset += 12;
  376. if (! font_info->name)
  377. font_info->name = "Unknown";
  378. if (font_info->flags & GRUB_FONT_FLAG_BOLD)
  379. font_info->style |= FT_STYLE_FLAG_BOLD;
  380. style_name[0] = 0;
  381. if (font_info->style & FT_STYLE_FLAG_BOLD)
  382. strcpy (style_name, " Bold");
  383. if (font_info->style & FT_STYLE_FLAG_ITALIC)
  384. strcat (style_name, " Italic");
  385. if (! style_name[0])
  386. strcpy (style_name, " Regular");
  387. font_name = xasprintf ("%s %s %d", font_info->name, &style_name[1],
  388. font_info->size);
  389. write_string_section (FONT_FORMAT_SECTION_NAMES_FONT_NAME,
  390. font_name, &offset, file);
  391. write_string_section (FONT_FORMAT_SECTION_NAMES_FAMILY,
  392. font_info->name, &offset, file);
  393. write_string_section (FONT_FORMAT_SECTION_NAMES_WEIGHT,
  394. (font_info->style & FT_STYLE_FLAG_BOLD) ?
  395. "bold" : "normal",
  396. &offset, file);
  397. write_string_section (FONT_FORMAT_SECTION_NAMES_SLAN,
  398. (font_info->style & FT_STYLE_FLAG_ITALIC) ?
  399. "italic" : "normal",
  400. &offset, file);
  401. write_be16_section (FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
  402. font_info->size, &offset, file);
  403. write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
  404. font_info->max_width, &offset, file);
  405. write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
  406. font_info->max_height, &offset, file);
  407. if (! font_info->desc)
  408. {
  409. if (font_info->min_y >= 0)
  410. font_info->desc = 1;
  411. else
  412. font_info->desc = - font_info->min_y;
  413. }
  414. if (! font_info->asce)
  415. {
  416. if (font_info->max_y <= 0)
  417. font_info->asce = 1;
  418. else
  419. font_info->asce = font_info->max_y;
  420. }
  421. write_be16_section (FONT_FORMAT_SECTION_NAMES_ASCENT,
  422. font_info->asce, &offset, file);
  423. write_be16_section (FONT_FORMAT_SECTION_NAMES_DESCENT,
  424. font_info->desc, &offset, file);
  425. if (font_verbosity > 0)
  426. {
  427. printf ("Font name: %s\n", font_name);
  428. printf ("Max width: %d\n", font_info->max_width);
  429. printf ("Max height: %d\n", font_info->max_height);
  430. printf ("Font ascent: %d\n", font_info->asce);
  431. printf ("Font descent: %d\n", font_info->desc);
  432. }
  433. num = 0;
  434. pre = 0;
  435. cur = font_info->glyph;
  436. while (cur)
  437. {
  438. struct grub_glyph_info *nxt;
  439. nxt = cur->next;
  440. cur->next = pre;
  441. pre = cur;
  442. cur = nxt;
  443. num++;
  444. }
  445. font_info->glyph = pre;
  446. if (font_verbosity > 0)
  447. printf ("Number of glyph: %d\n", num);
  448. leng = grub_cpu_to_be32 (num * 9);
  449. grub_util_write_image (FONT_FORMAT_SECTION_NAMES_CHAR_INDEX,
  450. sizeof(FONT_FORMAT_SECTION_NAMES_CHAR_INDEX) - 1,
  451. file);
  452. grub_util_write_image ((char *) &leng, 4, file);
  453. offset += 8 + num * 9 + 8;
  454. for (cur = font_info->glyph; cur; cur = cur->next)
  455. {
  456. data = grub_cpu_to_be32 (cur->char_code);
  457. grub_util_write_image ((char *) &data, 4, file);
  458. data = 0;
  459. grub_util_write_image ((char *) &data, 1, file);
  460. data = grub_cpu_to_be32 (offset);
  461. grub_util_write_image ((char *) &data, 4, file);
  462. offset += 10 + cur->bitmap_size;
  463. }
  464. leng = 0xffffffff;
  465. grub_util_write_image (FONT_FORMAT_SECTION_NAMES_DATA,
  466. sizeof(FONT_FORMAT_SECTION_NAMES_DATA) - 1, file);
  467. grub_util_write_image ((char *) &leng, 4, file);
  468. for (cur = font_info->glyph; cur; cur = cur->next)
  469. {
  470. data = grub_cpu_to_be16 (cur->width);
  471. grub_util_write_image ((char *) &data, 2, file);
  472. data = grub_cpu_to_be16 (cur->height);
  473. grub_util_write_image ((char *) &data, 2, file);
  474. data = grub_cpu_to_be16 (cur->x_ofs);
  475. grub_util_write_image ((char *) &data, 2, file);
  476. data = grub_cpu_to_be16 (cur->y_ofs);
  477. grub_util_write_image ((char *) &data, 2, file);
  478. data = grub_cpu_to_be16 (cur->device_width);
  479. grub_util_write_image ((char *) &data, 2, file);
  480. grub_util_write_image ((char *) &cur->bitmap[0], cur->bitmap_size, file);
  481. }
  482. fclose (file);
  483. }
  484. static void
  485. add_point (struct grub_font_info *font_info, grub_uint32_t a)
  486. {
  487. int i;
  488. for (i = 0; i < font_info->num_point; i++)
  489. if (font_info->points[i] == a)
  490. return;
  491. if ((font_info->num_point & (GRUB_FONT_RANGE_BLOCK - 1)) == 0)
  492. font_info->points = xrealloc (font_info->points,
  493. (font_info->num_point +
  494. GRUB_FONT_RANGE_BLOCK) *
  495. sizeof (grub_uint32_t));
  496. font_info->points[font_info->num_point++] = a;
  497. }
  498. static void
  499. add_range (struct grub_font_info *font_info, grub_uint32_t a,
  500. grub_uint32_t b)
  501. {
  502. if (a == b)
  503. add_point (font_info, a);
  504. else if (a < b)
  505. {
  506. if ((font_info->num_range & (GRUB_FONT_RANGE_BLOCK - 1)) == 0)
  507. font_info->ranges = xrealloc (font_info->ranges,
  508. (font_info->num_range +
  509. GRUB_FONT_RANGE_BLOCK) *
  510. sizeof (grub_uint32_t) * 2);
  511. font_info->ranges[font_info->num_range * 2] = a;
  512. font_info->ranges[font_info->num_range * 2 + 1] = b;
  513. font_info->num_range++;
  514. }
  515. }
  516. static void
  517. add_file (struct grub_font_info *font_info, const char *name)
  518. {
  519. grub_uint8_t *data;
  520. const grub_uint8_t *ptr;
  521. size_t size;
  522. grub_uint32_t code;
  523. size = grub_util_get_image_size (name);
  524. data = (grub_uint8_t *) grub_util_read_image (name);
  525. ptr = (const grub_uint8_t *) data;
  526. while ((ptr < data + size) &&
  527. (grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0))
  528. {
  529. if ((code != 0) && (code != 0xfeff) &&
  530. (code != '\r') && (code != '\n'))
  531. add_point (font_info, code);
  532. }
  533. free (data);
  534. }
  535. static char *
  536. read_section (char **str, int *value, off_t *ofs, FILE* file)
  537. {
  538. static char section_name[8];
  539. char *p;
  540. int len;
  541. *str = NULL;
  542. *value = 0;
  543. grub_util_read_at (section_name, 8, *ofs, file);
  544. len = grub_be_to_cpu32 (*((grub_uint32_t *) (section_name + 4)));
  545. *ofs += 8;
  546. p = section_name;
  547. if (len == 2)
  548. {
  549. grub_util_read_at (section_name + 4, 2, *ofs, file);
  550. *value = grub_be_to_cpu16 (*((grub_uint16_t *) (section_name + 4)));
  551. }
  552. else
  553. {
  554. if (memcmp (section_name, "CHIX", 4))
  555. {
  556. *str = xmalloc (len);
  557. grub_util_read_at (*str, len, *ofs, file);
  558. }
  559. else
  560. p = NULL;
  561. }
  562. *ofs += len;
  563. section_name[4] = 0;
  564. return p;
  565. }
  566. static void
  567. print_info (const char *filename)
  568. {
  569. FILE *file;
  570. char buf[12];
  571. char *name, *str;
  572. int value;
  573. off_t ofs;
  574. file = fopen (filename, "rb");
  575. if (! file)
  576. grub_util_error ("can\'t read from file %s", filename);
  577. grub_util_read_at (buf, 12, 0, file);
  578. if ((memcmp (buf, "FILE", 4)) ||
  579. (grub_be_to_cpu32 (*((grub_uint32_t *) (buf + 4))) != 4) ||
  580. (memcmp (buf + 8, "PFF2", 4)))
  581. grub_util_error ("invalid format");
  582. if (font_verbosity > 0)
  583. printf ("%s:\n", filename);
  584. ofs = 12;
  585. while ((name = read_section (&str, &value, &ofs, file)) != NULL)
  586. {
  587. if (font_verbosity > 0)
  588. {
  589. if (str)
  590. printf ("%s: %s\n", name, str);
  591. else
  592. printf ("%s: %d\n", name, value);
  593. }
  594. else if (! strcmp (name, "NAME"))
  595. printf ("%s: %s\n", str, filename);
  596. free (str);
  597. }
  598. fclose (file);
  599. }
  600. static grub_uint32_t box_chars[] =
  601. {
  602. 0x250F, 0x2501, 0x2513, 0x2503, 0x2503, 0x2517, 0x2501, 0x251B,
  603. 0x2554, 0x2550, 0x2557, 0x2551, 0x2551, 0x255A, 0x2550, 0x255D, 0
  604. };
  605. int
  606. main (int argc, char *argv[])
  607. {
  608. struct grub_font_info font_info;
  609. FT_Library ft_lib;
  610. int font_index = 0;
  611. int font_size = 0;
  612. char *output_file = NULL;
  613. int info_mode = 0;
  614. enum file_formats file_format = PF2;
  615. memset (&font_info, 0, sizeof (font_info));
  616. set_program_name (argv[0]);
  617. grub_util_init_nls ();
  618. /* Check for options. */
  619. while (1)
  620. {
  621. int c = getopt_long (argc, argv, "bao:n:I:s:d:r:t:ihVv", options, 0);
  622. if (c == -1)
  623. break;
  624. else
  625. switch (c)
  626. {
  627. case 'b':
  628. font_info.flags |= GRUB_FONT_FLAG_BOLD;
  629. break;
  630. case 0x100:
  631. font_info.flags |= GRUB_FONT_FLAG_NOBITMAP;
  632. break;
  633. case 0x101:
  634. font_info.flags |= GRUB_FONT_FLAG_NOHINTING;
  635. break;
  636. case 0x103:
  637. {
  638. grub_uint32_t *c;
  639. add_range (&font_info, 32, 126);
  640. for (c = box_chars; *c; c++)
  641. add_point (&font_info, *c);
  642. break;
  643. }
  644. case 'a':
  645. font_info.flags |= GRUB_FONT_FLAG_FORCEHINT;
  646. break;
  647. case 'o':
  648. output_file = optarg;
  649. break;
  650. case 'n':
  651. font_info.name = optarg;
  652. break;
  653. case 'I':
  654. font_index = strtoul (optarg, NULL, 0);
  655. break;
  656. case 's':
  657. font_size = strtoul (optarg, NULL, 0);
  658. break;
  659. case 'r':
  660. {
  661. char *p = optarg;
  662. while (1)
  663. {
  664. grub_uint32_t a, b;
  665. a = strtoul (p, &p, 0);
  666. if (*p == '-')
  667. {
  668. b = strtoul (p + 1, &p, 0);
  669. add_range (&font_info, a, b);
  670. }
  671. else
  672. add_point (&font_info, a);
  673. if (*p)
  674. {
  675. if (*p != ',')
  676. grub_util_error ("invalid font range");
  677. else
  678. p++;
  679. }
  680. else
  681. break;
  682. }
  683. break;
  684. }
  685. case 't':
  686. add_file (&font_info, optarg);
  687. break;
  688. case 'i':
  689. info_mode = 1;
  690. break;
  691. case 'd':
  692. font_info.desc = strtoul (optarg, NULL, 0);
  693. break;
  694. case 'e':
  695. font_info.asce = strtoul (optarg, NULL, 0);
  696. break;
  697. case 'h':
  698. usage (0);
  699. break;
  700. case 'V':
  701. printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
  702. return 0;
  703. case 'v':
  704. font_verbosity++;
  705. break;
  706. case 0x102:
  707. file_format = ASCII_BITMAPS;
  708. break;
  709. default:
  710. usage (1);
  711. break;
  712. }
  713. }
  714. if (info_mode)
  715. {
  716. for (; optind < argc; optind++)
  717. print_info (argv[optind]);
  718. return 0;
  719. }
  720. if (file_format == ASCII_BITMAPS && font_info.num_range > 0)
  721. {
  722. grub_util_error ("Option --ascii-bitmaps doesn't accept ranges (use ASCII).");
  723. return 1;
  724. }
  725. else if (file_format == ASCII_BITMAPS)
  726. {
  727. font_info.ranges = xrealloc (font_info.ranges,
  728. GRUB_FONT_RANGE_BLOCK *
  729. sizeof (grub_uint32_t) * 2);
  730. font_info.ranges[0] = (grub_uint32_t) 0x00;
  731. font_info.ranges[1] = (grub_uint32_t) 0x7f;
  732. font_info.num_range = 1;
  733. }
  734. if (! output_file)
  735. grub_util_error ("no output file is specified");
  736. if (FT_Init_FreeType (&ft_lib))
  737. grub_util_error ("FT_Init_FreeType fails");
  738. for (; optind < argc; optind++)
  739. {
  740. FT_Face ft_face;
  741. int size;
  742. if (FT_New_Face (ft_lib, argv[optind], font_index, &ft_face))
  743. {
  744. grub_util_info ("can't open file %s, index %d", argv[optind],
  745. font_index);
  746. continue;
  747. }
  748. if ((! font_info.name) && (ft_face->family_name))
  749. font_info.name = xstrdup (ft_face->family_name);
  750. size = font_size;
  751. if (! size)
  752. {
  753. if ((ft_face->face_flags & FT_FACE_FLAG_SCALABLE) ||
  754. (! ft_face->num_fixed_sizes))
  755. size = GRUB_FONT_DEFAULT_SIZE;
  756. else
  757. size = ft_face->available_sizes[0].height;
  758. }
  759. font_info.style = ft_face->style_flags;
  760. font_info.size = size;
  761. FT_Set_Pixel_Sizes (ft_face, size, size);
  762. add_font (&font_info, ft_face);
  763. FT_Done_Face (ft_face);
  764. }
  765. FT_Done_FreeType (ft_lib);
  766. if (file_format == PF2)
  767. write_font_pf2 (&font_info, output_file);
  768. else if (file_format == ASCII_BITMAPS)
  769. write_font_ascii_bitmap (&font_info, output_file);
  770. if (font_verbosity > 1)
  771. print_glyphs (&font_info);
  772. return 0;
  773. }