font.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177
  1. /* font.c - Font API and font file loader. */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2003,2005,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB 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.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/bufio.h>
  20. #include <grub/dl.h>
  21. #include <grub/file.h>
  22. #include <grub/font.h>
  23. #include <grub/misc.h>
  24. #include <grub/mm.h>
  25. #include <grub/types.h>
  26. #include <grub/video.h>
  27. #include <grub/bitmap.h>
  28. #include <grub/fontformat.h>
  29. #include <grub/list.h>
  30. #include <grub/env.h>
  31. #include <grub/lib.h>
  32. GRUB_EXPORT(grub_font_draw_glyph);
  33. GRUB_EXPORT(grub_font_draw_string);
  34. GRUB_EXPORT(grub_font_get);
  35. GRUB_EXPORT(grub_font_get_ascent);
  36. GRUB_EXPORT(grub_font_get_descent);
  37. GRUB_EXPORT(grub_font_get_glyph);
  38. GRUB_EXPORT(grub_font_get_glyph_with_fallback);
  39. GRUB_EXPORT(grub_font_get_leading);
  40. GRUB_EXPORT(grub_font_get_max_char_height);
  41. GRUB_EXPORT(grub_font_get_code_width);
  42. GRUB_EXPORT(grub_font_get_string_width);
  43. GRUB_EXPORT(grub_font_get_name);
  44. GRUB_EXPORT(grub_font_get_height);
  45. #ifdef USE_ASCII_FAILBACK
  46. #include "ascii.h"
  47. #endif
  48. #ifndef FONT_DEBUG
  49. #define FONT_DEBUG 0
  50. #endif
  51. struct char_index_entry
  52. {
  53. grub_uint32_t code;
  54. grub_uint8_t storage_flags;
  55. grub_uint32_t offset;
  56. /* Glyph if loaded, or NULL otherwise. */
  57. struct grub_font_glyph *glyph;
  58. };
  59. #define FONT_WEIGHT_NORMAL 100
  60. #define FONT_WEIGHT_BOLD 200
  61. #define ASCII_BITMAP_SIZE 16
  62. struct grub_font
  63. {
  64. char *name;
  65. grub_file_t file;
  66. char *family;
  67. short point_size;
  68. short weight;
  69. short max_char_width;
  70. short max_char_height;
  71. short ascent;
  72. short descent;
  73. short leading;
  74. grub_uint32_t num_chars;
  75. struct char_index_entry *char_index;
  76. grub_uint16_t *bmp_idx;
  77. };
  78. /* Definition of font registry. */
  79. struct grub_font_node *grub_font_list;
  80. static int register_font (grub_font_t font);
  81. static void font_init (grub_font_t font);
  82. static void free_font (grub_font_t font);
  83. static void remove_font (grub_font_t font);
  84. struct font_file_section
  85. {
  86. /* The file this section is in. */
  87. grub_file_t file;
  88. /* FOURCC name of the section. */
  89. char name[4];
  90. /* Length of the section contents. */
  91. grub_uint32_t length;
  92. /* Set by open_section() on EOF. */
  93. int eof;
  94. };
  95. /* Replace unknown glyphs with a rounded question mark. */
  96. static grub_uint8_t unknown_glyph_bitmap[] = {
  97. /* 76543210 */
  98. 0x7C, /* ooooo */
  99. 0x82, /* o o */
  100. 0xBA, /* o ooo o */
  101. 0xAA, /* o o o o */
  102. 0xAA, /* o o o o */
  103. 0x8A, /* o o o */
  104. 0x9A, /* o oo o */
  105. 0x92, /* o o o */
  106. 0x92, /* o o o */
  107. 0x92, /* o o o */
  108. 0x92, /* o o o */
  109. 0x82, /* o o */
  110. 0x92, /* o o o */
  111. 0x82, /* o o */
  112. 0x7C, /* ooooo */
  113. 0x00 /* */
  114. };
  115. /* The "unknown glyph" glyph, used as a last resort. */
  116. static struct grub_font_glyph *unknown_glyph;
  117. /* The font structure used when no other font is loaded. This functions
  118. as a "Null Object" pattern, so that code everywhere does not have to
  119. check for a NULL grub_font_t to avoid dereferencing a null pointer. */
  120. static struct grub_font null_font;
  121. /* Flag to ensure module is initialized only once. */
  122. static grub_uint8_t font_loader_initialized;
  123. #ifdef USE_ASCII_FAILBACK
  124. static struct grub_font_glyph *ascii_font_glyph[0x80];
  125. #endif
  126. static struct grub_font_glyph *
  127. ascii_glyph_lookup (grub_uint32_t code)
  128. {
  129. #ifdef USE_ASCII_FAILBACK
  130. static int ascii_failback_initialized = 0;
  131. if (code >= 0x80)
  132. return unknown_glyph;
  133. if (ascii_failback_initialized == 0)
  134. {
  135. int current;
  136. for (current = 0; current < 0x80; current++)
  137. {
  138. ascii_font_glyph[current] =
  139. grub_malloc (sizeof (struct grub_font_glyph) + ASCII_BITMAP_SIZE);
  140. ascii_font_glyph[current]->width = 8;
  141. ascii_font_glyph[current]->height = 16;
  142. ascii_font_glyph[current]->offset_x = 0;
  143. ascii_font_glyph[current]->offset_y = -2;
  144. ascii_font_glyph[current]->device_width = 8;
  145. grub_memcpy (ascii_font_glyph[current]->bitmap,
  146. &ascii_bitmaps[(0x7f - current) * ASCII_BITMAP_SIZE],
  147. ASCII_BITMAP_SIZE);
  148. }
  149. ascii_failback_initialized = 1;
  150. }
  151. return ascii_font_glyph[code];
  152. #else
  153. (void) code;
  154. return unknown_glyph;
  155. #endif
  156. }
  157. void
  158. grub_font_loader_init (void)
  159. {
  160. /* Only initialize font loader once. */
  161. if (font_loader_initialized)
  162. return;
  163. /* Make glyph for unknown glyph. */
  164. unknown_glyph = grub_malloc (sizeof (struct grub_font_glyph)
  165. + sizeof (unknown_glyph_bitmap));
  166. if (!unknown_glyph)
  167. return;
  168. unknown_glyph->width = 8;
  169. unknown_glyph->height = 16;
  170. unknown_glyph->offset_x = 0;
  171. unknown_glyph->offset_y = -3;
  172. unknown_glyph->device_width = 8;
  173. grub_memcpy (unknown_glyph->bitmap,
  174. unknown_glyph_bitmap, sizeof (unknown_glyph_bitmap));
  175. /* Initialize the null font. */
  176. font_init (&null_font);
  177. null_font.name = "<No Font>";
  178. null_font.ascent = unknown_glyph->height - 3;
  179. null_font.descent = 3;
  180. null_font.max_char_width = unknown_glyph->width;
  181. null_font.max_char_height = unknown_glyph->height;
  182. font_loader_initialized = 1;
  183. }
  184. /* Initialize the font object with initial default values. */
  185. static void
  186. font_init (grub_font_t font)
  187. {
  188. font->name = 0;
  189. font->file = 0;
  190. font->family = 0;
  191. font->point_size = 0;
  192. font->weight = 0;
  193. /* Default leading value, not in font file yet. */
  194. font->leading = 1;
  195. font->max_char_width = 0;
  196. font->max_char_height = 0;
  197. font->ascent = 0;
  198. font->descent = 0;
  199. font->num_chars = 0;
  200. font->char_index = 0;
  201. font->bmp_idx = 0;
  202. }
  203. /* Open the next section in the file.
  204. On success, the section name is stored in section->name and the length in
  205. section->length, and 0 is returned. On failure, 1 is returned and
  206. grub_errno is set appropriately with an error message.
  207. If 1 is returned due to being at the end of the file, then section->eof is
  208. set to 1; otherwise, section->eof is set to 0. */
  209. static int
  210. open_section (grub_file_t file, struct font_file_section *section)
  211. {
  212. grub_ssize_t retval;
  213. grub_uint32_t raw_length;
  214. section->file = file;
  215. section->eof = 0;
  216. /* Read the FOURCC section name. */
  217. retval = grub_file_read (file, section->name, 4);
  218. if (retval >= 0 && retval < 4)
  219. {
  220. /* EOF encountered. */
  221. section->eof = 1;
  222. return 1;
  223. }
  224. else if (retval < 0)
  225. {
  226. grub_error (GRUB_ERR_BAD_FONT,
  227. "font format error: can't read section name");
  228. return 1;
  229. }
  230. /* Read the big-endian 32-bit section length. */
  231. retval = grub_file_read (file, &raw_length, 4);
  232. if (retval >= 0 && retval < 4)
  233. {
  234. /* EOF encountered. */
  235. section->eof = 1;
  236. return 1;
  237. }
  238. else if (retval < 0)
  239. {
  240. grub_error (GRUB_ERR_BAD_FONT,
  241. "font format error: can't read section length");
  242. return 1;
  243. }
  244. /* Convert byte-order and store in *length. */
  245. section->length = grub_be_to_cpu32 (raw_length);
  246. return 0;
  247. }
  248. /* Size in bytes of each character index (CHIX section)
  249. entry in the font file. */
  250. #define FONT_CHAR_INDEX_ENTRY_SIZE (4 + 1 + 4)
  251. /* Load the character index (CHIX) section contents from the font file. This
  252. presumes that the position of FILE is positioned immediately after the
  253. section length for the CHIX section (i.e., at the start of the section
  254. contents). Returns 0 upon success, nonzero for failure (in which case
  255. grub_errno is set appropriately). */
  256. static int
  257. load_font_index (grub_file_t file, grub_uint32_t sect_length, struct
  258. grub_font *font)
  259. {
  260. unsigned i;
  261. grub_uint32_t last_code;
  262. #if FONT_DEBUG >= 2
  263. grub_printf ("load_font_index(sect_length=%d)\n", sect_length);
  264. #endif
  265. /* Sanity check: ensure section length is divisible by the entry size. */
  266. if ((sect_length % FONT_CHAR_INDEX_ENTRY_SIZE) != 0)
  267. {
  268. grub_error (GRUB_ERR_BAD_FONT,
  269. "font file format error: character index length %d "
  270. "is not a multiple of the entry size %d",
  271. sect_length, FONT_CHAR_INDEX_ENTRY_SIZE);
  272. return 1;
  273. }
  274. /* Calculate the number of characters. */
  275. font->num_chars = sect_length / FONT_CHAR_INDEX_ENTRY_SIZE;
  276. /* Allocate the character index array. */
  277. font->char_index = grub_malloc (font->num_chars
  278. * sizeof (struct char_index_entry));
  279. if (!font->char_index)
  280. return 1;
  281. font->bmp_idx = grub_malloc (0x10000 * sizeof (grub_uint16_t));
  282. if (!font->bmp_idx)
  283. {
  284. grub_free (font->char_index);
  285. return 1;
  286. }
  287. grub_memset (font->bmp_idx, 0xff, 0x10000 * sizeof (grub_uint16_t));
  288. #if FONT_DEBUG >= 2
  289. grub_printf ("num_chars=%d)\n", font->num_chars);
  290. #endif
  291. last_code = 0;
  292. /* Load the character index data from the file. */
  293. for (i = 0; i < font->num_chars; i++)
  294. {
  295. struct char_index_entry *entry = &font->char_index[i];
  296. /* Read code point value; convert to native byte order. */
  297. if (grub_file_read (file, &entry->code, 4) != 4)
  298. return 1;
  299. entry->code = grub_be_to_cpu32 (entry->code);
  300. /* Verify that characters are in ascending order. */
  301. if (i != 0 && entry->code <= last_code)
  302. {
  303. grub_error (GRUB_ERR_BAD_FONT,
  304. "font characters not in ascending order: %u <= %u",
  305. entry->code, last_code);
  306. return 1;
  307. }
  308. if (entry->code < 0x10000)
  309. font->bmp_idx[entry->code] = i;
  310. last_code = entry->code;
  311. /* Read storage flags byte. */
  312. if (grub_file_read (file, &entry->storage_flags, 1) != 1)
  313. return 1;
  314. /* Read glyph data offset; convert to native byte order. */
  315. if (grub_file_read (file, &entry->offset, 4) != 4)
  316. return 1;
  317. entry->offset = grub_be_to_cpu32 (entry->offset);
  318. /* No glyph loaded. Will be loaded on demand and cached thereafter. */
  319. entry->glyph = 0;
  320. #if FONT_DEBUG >= 5
  321. /* Print the 1st 10 characters. */
  322. if (i < 10)
  323. grub_printf ("c=%d o=%d\n", entry->code, entry->offset);
  324. #endif
  325. }
  326. return 0;
  327. }
  328. /* Read the contents of the specified section as a string, which is
  329. allocated on the heap. Returns 0 if there is an error. */
  330. static char *
  331. read_section_as_string (struct font_file_section *section)
  332. {
  333. char *str;
  334. grub_ssize_t ret;
  335. str = grub_malloc (section->length + 1);
  336. if (!str)
  337. return 0;
  338. ret = grub_file_read (section->file, str, section->length);
  339. if (ret < 0 || ret != (grub_ssize_t) section->length)
  340. {
  341. grub_free (str);
  342. return 0;
  343. }
  344. str[section->length] = '\0';
  345. return str;
  346. }
  347. /* Read the contents of the current section as a 16-bit integer value,
  348. which is stored into *VALUE.
  349. Returns 0 upon success, nonzero upon failure. */
  350. static int
  351. read_section_as_short (struct font_file_section *section,
  352. grub_int16_t * value)
  353. {
  354. grub_uint16_t raw_value;
  355. if (section->length != 2)
  356. {
  357. grub_error (GRUB_ERR_BAD_FONT,
  358. "font file format error: section %c%c%c%c length "
  359. "is %d but should be 2",
  360. section->name[0], section->name[1],
  361. section->name[2], section->name[3], section->length);
  362. return 1;
  363. }
  364. if (grub_file_read (section->file, &raw_value, 2) != 2)
  365. return 1;
  366. *value = grub_be_to_cpu16 (raw_value);
  367. return 0;
  368. }
  369. /* Load a font and add it to the beginning of the global font list.
  370. Returns 0 upon success, nonzero upon failure. */
  371. int
  372. grub_font_load (const char *filename)
  373. {
  374. grub_file_t file = 0;
  375. struct font_file_section section;
  376. char magic[4];
  377. grub_font_t font = 0;
  378. #if FONT_DEBUG >= 1
  379. grub_printf ("add_font(%s)\n", filename);
  380. #endif
  381. file = grub_buffile_open (filename, 1024);
  382. if (!file)
  383. goto fail;
  384. #if FONT_DEBUG >= 3
  385. grub_printf ("file opened\n");
  386. #endif
  387. /* Read the FILE section. It indicates the file format. */
  388. if (open_section (file, &section) != 0)
  389. goto fail;
  390. #if FONT_DEBUG >= 3
  391. grub_printf ("opened FILE section\n");
  392. #endif
  393. if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FILE,
  394. sizeof (FONT_FORMAT_SECTION_NAMES_FILE) - 1) != 0)
  395. {
  396. grub_error (GRUB_ERR_BAD_FONT,
  397. "font file format error: 1st section must be FILE");
  398. goto fail;
  399. }
  400. #if FONT_DEBUG >= 3
  401. grub_printf ("section name ok\n");
  402. #endif
  403. if (section.length != 4)
  404. {
  405. grub_error (GRUB_ERR_BAD_FONT,
  406. "font file format error (file type ID length is %d "
  407. "but should be 4)", section.length);
  408. goto fail;
  409. }
  410. #if FONT_DEBUG >= 3
  411. grub_printf ("section length ok\n");
  412. #endif
  413. /* Check the file format type code. */
  414. if (grub_file_read (file, magic, 4) != 4)
  415. goto fail;
  416. #if FONT_DEBUG >= 3
  417. grub_printf ("read magic ok\n");
  418. #endif
  419. if (grub_memcmp (magic, FONT_FORMAT_PFF2_MAGIC, 4) != 0)
  420. {
  421. grub_error (GRUB_ERR_BAD_FONT, "invalid font magic %x %x %x %x",
  422. magic[0], magic[1], magic[2], magic[3]);
  423. goto fail;
  424. }
  425. #if FONT_DEBUG >= 3
  426. grub_printf ("compare magic ok\n");
  427. #endif
  428. /* Allocate the font object. */
  429. font = (grub_font_t) grub_malloc (sizeof (struct grub_font));
  430. if (!font)
  431. goto fail;
  432. font_init (font);
  433. font->file = file;
  434. #if FONT_DEBUG >= 3
  435. grub_printf ("allocate font ok; loading font info\n");
  436. #endif
  437. /* Load the font information. */
  438. while (1)
  439. {
  440. if (open_section (file, &section) != 0)
  441. {
  442. if (section.eof)
  443. break; /* Done reading the font file. */
  444. else
  445. goto fail;
  446. }
  447. #if FONT_DEBUG >= 2
  448. grub_printf ("opened section %c%c%c%c ok\n",
  449. section.name[0], section.name[1],
  450. section.name[2], section.name[3]);
  451. #endif
  452. if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FONT_NAME,
  453. sizeof (FONT_FORMAT_SECTION_NAMES_FONT_NAME) - 1) == 0)
  454. {
  455. font->name = read_section_as_string (&section);
  456. if (!font->name)
  457. goto fail;
  458. }
  459. else if (grub_memcmp (section.name,
  460. FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
  461. sizeof (FONT_FORMAT_SECTION_NAMES_POINT_SIZE) -
  462. 1) == 0)
  463. {
  464. if (read_section_as_short (&section, &font->point_size) != 0)
  465. goto fail;
  466. }
  467. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_WEIGHT,
  468. sizeof (FONT_FORMAT_SECTION_NAMES_WEIGHT) - 1)
  469. == 0)
  470. {
  471. char *wt;
  472. wt = read_section_as_string (&section);
  473. if (!wt)
  474. continue;
  475. /* Convert the weight string 'normal' or 'bold' into a number. */
  476. if (grub_strcmp (wt, "normal") == 0)
  477. font->weight = FONT_WEIGHT_NORMAL;
  478. else if (grub_strcmp (wt, "bold") == 0)
  479. font->weight = FONT_WEIGHT_BOLD;
  480. grub_free (wt);
  481. }
  482. else if (grub_memcmp (section.name,
  483. FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
  484. sizeof (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH)
  485. - 1) == 0)
  486. {
  487. if (read_section_as_short (&section, &font->max_char_width) != 0)
  488. goto fail;
  489. }
  490. else if (grub_memcmp (section.name,
  491. FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
  492. sizeof (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT)
  493. - 1) == 0)
  494. {
  495. if (read_section_as_short (&section, &font->max_char_height) != 0)
  496. goto fail;
  497. }
  498. else if (grub_memcmp (section.name,
  499. FONT_FORMAT_SECTION_NAMES_ASCENT,
  500. sizeof (FONT_FORMAT_SECTION_NAMES_ASCENT) - 1)
  501. == 0)
  502. {
  503. if (read_section_as_short (&section, &font->ascent) != 0)
  504. goto fail;
  505. }
  506. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DESCENT,
  507. sizeof (FONT_FORMAT_SECTION_NAMES_DESCENT) - 1)
  508. == 0)
  509. {
  510. if (read_section_as_short (&section, &font->descent) != 0)
  511. goto fail;
  512. }
  513. else if (grub_memcmp (section.name,
  514. FONT_FORMAT_SECTION_NAMES_CHAR_INDEX,
  515. sizeof (FONT_FORMAT_SECTION_NAMES_CHAR_INDEX) -
  516. 1) == 0)
  517. {
  518. if (load_font_index (file, section.length, font) != 0)
  519. goto fail;
  520. }
  521. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DATA,
  522. sizeof (FONT_FORMAT_SECTION_NAMES_DATA) - 1) == 0)
  523. {
  524. /* When the DATA section marker is reached, we stop reading. */
  525. break;
  526. }
  527. else
  528. {
  529. /* Unhandled section type, simply skip past it. */
  530. #if FONT_DEBUG >= 3
  531. grub_printf ("Unhandled section type, skipping.\n");
  532. #endif
  533. grub_off_t section_end = grub_file_tell (file) + section.length;
  534. if ((int) grub_file_seek (file, section_end) == -1)
  535. goto fail;
  536. }
  537. }
  538. if (!font->name)
  539. {
  540. grub_printf ("Note: Font has no name.\n");
  541. font->name = grub_strdup ("Unknown");
  542. }
  543. #if FONT_DEBUG >= 1
  544. grub_printf ("Loaded font `%s'.\n"
  545. "Ascent=%d Descent=%d MaxW=%d MaxH=%d Number of characters=%d.\n",
  546. font->name,
  547. font->ascent, font->descent,
  548. font->max_char_width, font->max_char_height, font->num_chars);
  549. #endif
  550. if (font->max_char_width == 0
  551. || font->max_char_height == 0
  552. || font->num_chars == 0
  553. || font->char_index == 0 || font->ascent == 0 || font->descent == 0)
  554. {
  555. grub_error (GRUB_ERR_BAD_FONT,
  556. "invalid font file: missing some required data");
  557. goto fail;
  558. }
  559. /* Add the font to the global font registry. */
  560. if (register_font (font) != 0)
  561. goto fail;
  562. return 0;
  563. fail:
  564. free_font (font);
  565. return 1;
  566. }
  567. /* Read a 16-bit big-endian integer from FILE, convert it to native byte
  568. order, and store it in *VALUE.
  569. Returns 0 on success, 1 on failure. */
  570. static int
  571. read_be_uint16 (grub_file_t file, grub_uint16_t * value)
  572. {
  573. if (grub_file_read (file, value, 2) != 2)
  574. return 1;
  575. *value = grub_be_to_cpu16 (*value);
  576. return 0;
  577. }
  578. static int
  579. read_be_int16 (grub_file_t file, grub_int16_t * value)
  580. {
  581. /* For the signed integer version, use the same code as for unsigned. */
  582. return read_be_uint16 (file, (grub_uint16_t *) value);
  583. }
  584. /* Return a pointer to the character index entry for the glyph corresponding to
  585. the codepoint CODE in the font FONT. If not found, return zero. */
  586. static inline struct char_index_entry *
  587. find_glyph (const grub_font_t font, grub_uint32_t code)
  588. {
  589. struct char_index_entry *table;
  590. grub_size_t lo;
  591. grub_size_t hi;
  592. grub_size_t mid;
  593. table = font->char_index;
  594. /* Use BMP index if possible. */
  595. if (code < 0x10000 && font->bmp_idx)
  596. {
  597. if (font->bmp_idx[code] == 0xffff)
  598. return 0;
  599. return &table[font->bmp_idx[code]];
  600. }
  601. /* Do a binary search in `char_index', which is ordered by code point. */
  602. lo = 0;
  603. hi = font->num_chars - 1;
  604. if (!table)
  605. return 0;
  606. while (lo <= hi)
  607. {
  608. mid = lo + (hi - lo) / 2;
  609. if (code < table[mid].code)
  610. hi = mid - 1;
  611. else if (code > table[mid].code)
  612. lo = mid + 1;
  613. else
  614. return &table[mid];
  615. }
  616. return 0;
  617. }
  618. /* Get a glyph for the Unicode character CODE in FONT. The glyph is loaded
  619. from the font file if has not been loaded yet.
  620. Returns a pointer to the glyph if found, or 0 if it is not found. */
  621. static struct grub_font_glyph *
  622. grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)
  623. {
  624. struct char_index_entry *index_entry;
  625. index_entry = find_glyph (font, code);
  626. if (index_entry)
  627. {
  628. struct grub_font_glyph *glyph = 0;
  629. grub_uint16_t width;
  630. grub_uint16_t height;
  631. grub_int16_t xoff;
  632. grub_int16_t yoff;
  633. grub_int16_t dwidth;
  634. int len;
  635. if (index_entry->glyph)
  636. /* Return cached glyph. */
  637. return index_entry->glyph;
  638. if (!font->file)
  639. /* No open file, can't load any glyphs. */
  640. return 0;
  641. /* Make sure we can find glyphs for error messages. Push active
  642. error message to error stack and reset error message. */
  643. grub_error_push ();
  644. grub_file_seek (font->file, index_entry->offset);
  645. /* Read the glyph width, height, and baseline. */
  646. if (read_be_uint16 (font->file, &width) != 0
  647. || read_be_uint16 (font->file, &height) != 0
  648. || read_be_int16 (font->file, &xoff) != 0
  649. || read_be_int16 (font->file, &yoff) != 0
  650. || read_be_int16 (font->file, &dwidth) != 0)
  651. {
  652. remove_font (font);
  653. return 0;
  654. }
  655. len = (width * height + 7) / 8;
  656. glyph = grub_malloc (sizeof (struct grub_font_glyph) + len);
  657. if (!glyph)
  658. {
  659. remove_font (font);
  660. return 0;
  661. }
  662. glyph->font = font;
  663. glyph->width = width;
  664. glyph->height = height;
  665. glyph->offset_x = xoff;
  666. glyph->offset_y = yoff;
  667. glyph->device_width = dwidth;
  668. /* Don't try to read empty bitmaps (e.g., space characters). */
  669. if (len != 0)
  670. {
  671. if (grub_file_read (font->file, glyph->bitmap, len) != len)
  672. {
  673. remove_font (font);
  674. return 0;
  675. }
  676. }
  677. /* Restore old error message. */
  678. grub_error_pop ();
  679. /* Cache the glyph. */
  680. index_entry->glyph = glyph;
  681. return glyph;
  682. }
  683. return 0;
  684. }
  685. /* Free the memory used by FONT.
  686. This should not be called if the font has been made available to
  687. users (once it is added to the global font list), since there would
  688. be the possibility of a dangling pointer. */
  689. static void
  690. free_font (grub_font_t font)
  691. {
  692. if (font)
  693. {
  694. if (font->file)
  695. grub_file_close (font->file);
  696. grub_free (font->name);
  697. grub_free (font->family);
  698. grub_free (font->char_index);
  699. grub_free (font);
  700. }
  701. }
  702. /* Add FONT to the global font registry.
  703. Returns 0 upon success, nonzero on failure
  704. (the font was not registered). */
  705. static int
  706. register_font (grub_font_t font)
  707. {
  708. struct grub_font_node *node = 0;
  709. node = grub_malloc (sizeof (struct grub_font_node));
  710. if (!node)
  711. return 1;
  712. node->value = font;
  713. node->next = grub_font_list;
  714. grub_font_list = node;
  715. return 0;
  716. }
  717. /* Remove the font from the global font list. We don't actually free the
  718. font's memory since users could be holding references to the font. */
  719. static void
  720. remove_font (grub_font_t font)
  721. {
  722. struct grub_font_node **nextp, *cur;
  723. for (nextp = &grub_font_list, cur = *nextp;
  724. cur; nextp = &cur->next, cur = cur->next)
  725. {
  726. if (cur->value == font)
  727. {
  728. *nextp = cur->next;
  729. /* Free the node, but not the font itself. */
  730. grub_free (cur);
  731. return;
  732. }
  733. }
  734. }
  735. /* Get a font from the list of loaded fonts. This function will return
  736. another font if the requested font is not available. If no fonts are
  737. loaded, then a special 'null font' is returned, which contains no glyphs,
  738. but is not a null pointer so the caller may omit checks for NULL. */
  739. grub_font_t
  740. grub_font_get (const char *font_name)
  741. {
  742. struct grub_font_node *node;
  743. grub_autolist_t p;
  744. for (node = grub_font_list; node; node = node->next)
  745. {
  746. grub_font_t font = node->value;
  747. if (grub_strcmp (font->name, font_name) == 0)
  748. return font;
  749. }
  750. p = grub_named_list_find (GRUB_AS_NAMED_LIST (grub_autolist_font), font_name);
  751. if (p)
  752. {
  753. char *prefix;
  754. prefix = grub_env_get ("prefix");
  755. if (prefix)
  756. {
  757. char *filename;
  758. filename = grub_xasprintf ("%s/fonts/%s", prefix, p->value);
  759. if (filename)
  760. {
  761. grub_font_load (filename);
  762. grub_free (filename);
  763. }
  764. grub_errno = 0;
  765. }
  766. grub_list_remove (GRUB_AS_LIST_P (&grub_autolist_font), GRUB_AS_LIST (p));
  767. grub_free (p->name);
  768. grub_free (p);
  769. }
  770. /* If no font by that name is found, return the first font in the list
  771. as a fallback. */
  772. if (grub_font_list && grub_font_list->value)
  773. return grub_font_list->value;
  774. else
  775. /* The null_font is a last resort. */
  776. return &null_font;
  777. }
  778. /* Get the full name of the font. */
  779. const char *
  780. grub_font_get_name (grub_font_t font)
  781. {
  782. return font->name;
  783. }
  784. /* Get the maximum width of any character in the font in pixels. */
  785. int
  786. grub_font_get_max_char_width (grub_font_t font)
  787. {
  788. return font->max_char_width;
  789. }
  790. /* Get the maximum height of any character in the font in pixels. */
  791. int
  792. grub_font_get_max_char_height (grub_font_t font)
  793. {
  794. return font->max_char_height;
  795. }
  796. /* Get the distance in pixels from the top of characters to the baseline. */
  797. int
  798. grub_font_get_ascent (grub_font_t font)
  799. {
  800. return font->ascent;
  801. }
  802. /* Get the distance in pixels from the baseline to the lowest descenders
  803. (for instance, in a lowercase 'y', 'g', etc.). */
  804. int
  805. grub_font_get_descent (grub_font_t font)
  806. {
  807. return font->descent;
  808. }
  809. /* Get the *standard leading* of the font in pixel, which is the spacing
  810. between two lines of text. Specifically, it is the space between the
  811. descent of one line and the ascent of the next line. This is included
  812. in the *height* metric. */
  813. int
  814. grub_font_get_leading (grub_font_t font)
  815. {
  816. return font->leading;
  817. }
  818. /* Get the distance in pixels between baselines of adjacent lines of text. */
  819. int
  820. grub_font_get_height (grub_font_t font)
  821. {
  822. return font->ascent + font->descent + font->leading;
  823. }
  824. int
  825. grub_font_get_code_width (grub_font_t font, const char *str, const char **end)
  826. {
  827. grub_uint32_t code;
  828. if (grub_utf8_to_ucs4 (&code, 1, (const grub_uint8_t *) str,
  829. -1, (const grub_uint8_t **) end) > 0)
  830. {
  831. struct grub_font_glyph *glyph;
  832. glyph = grub_font_get_glyph_with_fallback (font, code);
  833. return glyph->device_width;
  834. }
  835. else
  836. return 0;
  837. }
  838. /* Get the width in pixels of the specified UTF-8 string, when rendered in
  839. in the specified font (but falling back on other fonts for glyphs that
  840. are missing). */
  841. int
  842. grub_font_get_string_width (grub_font_t font, const char *str)
  843. {
  844. int width, w;
  845. width = 0;
  846. while ((w = grub_font_get_code_width (font, str, &str)) > 0)
  847. width += w;
  848. return width;
  849. }
  850. /* Get the glyph for FONT corresponding to the Unicode code point CODE.
  851. Returns the ASCII glyph for the code if no other fonts are available.
  852. The glyphs are cached once loaded. */
  853. struct grub_font_glyph *
  854. grub_font_get_glyph (grub_font_t font, grub_uint32_t code)
  855. {
  856. struct grub_font_glyph *glyph = 0;
  857. if (font)
  858. glyph = grub_font_get_glyph_internal (font, code);
  859. if (glyph == 0)
  860. {
  861. glyph = ascii_glyph_lookup (code);
  862. }
  863. return glyph;
  864. }
  865. /* Calculate a subject value representing "how similar" two fonts are.
  866. This is used to prioritize the order that fonts are scanned for missing
  867. glyphs. The object is to select glyphs from the most similar font
  868. possible, for the best appearance.
  869. The heuristic is crude, but it helps greatly when fonts of similar
  870. sizes are used so that tiny 8 point glyphs are not mixed into a string
  871. of 24 point text unless there is no other choice. */
  872. static int
  873. get_font_diversity (grub_font_t a, grub_font_t b)
  874. {
  875. int d;
  876. d = 0;
  877. if (a->ascent && b->ascent)
  878. d += grub_abs (a->ascent - b->ascent) * 8;
  879. else
  880. /* Penalty for missing attributes. */
  881. d += 50;
  882. if (a->max_char_height && b->max_char_height)
  883. d += grub_abs (a->max_char_height - b->max_char_height) * 8;
  884. else
  885. /* Penalty for missing attributes. */
  886. d += 50;
  887. /* Weight is a minor factor. */
  888. d += (a->weight != b->weight) ? 5 : 0;
  889. return d;
  890. }
  891. /* Get a glyph corresponding to the codepoint CODE. If FONT contains the
  892. specified glyph, then it is returned. Otherwise, all other loaded fonts
  893. are searched until one is found that contains a glyph for CODE.
  894. If no glyph is available for CODE in the loaded fonts, then a glyph
  895. representing an unknown character is returned.
  896. This function never returns NULL.
  897. The returned glyph is owned by the font manager and should not be freed
  898. by the caller. The glyphs are cached. */
  899. struct grub_font_glyph *
  900. grub_font_get_glyph_with_fallback (grub_font_t font, grub_uint32_t code)
  901. {
  902. struct grub_font_glyph *glyph;
  903. struct grub_font_node *node;
  904. /* Keep track of next node, in case there's an I/O error in
  905. grub_font_get_glyph_internal() and the font is removed from the list. */
  906. struct grub_font_node *next;
  907. /* Information on the best glyph found so far, to help find the glyph in
  908. the best matching to the requested one. */
  909. int best_diversity;
  910. struct grub_font_glyph *best_glyph;
  911. if (font)
  912. {
  913. /* First try to get the glyph from the specified font. */
  914. glyph = grub_font_get_glyph_internal (font, code);
  915. if (glyph)
  916. return glyph;
  917. }
  918. /* Otherwise, search all loaded fonts for the glyph and use the one from
  919. the font that best matches the requested font. */
  920. best_diversity = 10000;
  921. best_glyph = 0;
  922. for (node = grub_font_list; node; node = next)
  923. {
  924. grub_font_t curfont;
  925. curfont = node->value;
  926. next = node->next;
  927. glyph = grub_font_get_glyph_internal (curfont, code);
  928. if (glyph)
  929. {
  930. int d;
  931. d = get_font_diversity (curfont, font);
  932. if (d < best_diversity)
  933. {
  934. best_diversity = d;
  935. best_glyph = glyph;
  936. }
  937. }
  938. }
  939. if (best_glyph)
  940. return best_glyph;
  941. else
  942. /* Glyph not available in any font. Return ASCII failback. */
  943. return ascii_glyph_lookup (code);
  944. }
  945. /* Draw the specified glyph at (x, y). The y coordinate designates the
  946. baseline of the character, while the x coordinate designates the left
  947. side location of the character. */
  948. grub_err_t
  949. grub_font_draw_glyph (struct grub_font_glyph * glyph,
  950. grub_video_color_t color, int left_x, int baseline_y)
  951. {
  952. struct grub_video_bitmap glyph_bitmap;
  953. /* Don't try to draw empty glyphs (U+0020, etc.). */
  954. if (glyph->width == 0 || glyph->height == 0)
  955. return GRUB_ERR_NONE;
  956. glyph_bitmap.mode_info.width = glyph->width;
  957. glyph_bitmap.mode_info.height = glyph->height;
  958. glyph_bitmap.mode_info.mode_type =
  959. (1 << GRUB_VIDEO_MODE_TYPE_DEPTH_POS) | GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP;
  960. glyph_bitmap.mode_info.blit_format = GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
  961. glyph_bitmap.mode_info.bpp = 1;
  962. /* Really 1 bit per pixel. */
  963. glyph_bitmap.mode_info.bytes_per_pixel = 0;
  964. /* Packed densely as bits. */
  965. glyph_bitmap.mode_info.pitch = glyph->width;
  966. glyph_bitmap.mode_info.number_of_colors = 2;
  967. glyph_bitmap.mode_info.bg_red = 0;
  968. glyph_bitmap.mode_info.bg_green = 0;
  969. glyph_bitmap.mode_info.bg_blue = 0;
  970. glyph_bitmap.mode_info.bg_alpha = 0;
  971. grub_video_unmap_color (color,
  972. &glyph_bitmap.mode_info.fg_red,
  973. &glyph_bitmap.mode_info.fg_green,
  974. &glyph_bitmap.mode_info.fg_blue,
  975. &glyph_bitmap.mode_info.fg_alpha);
  976. glyph_bitmap.data = glyph->bitmap;
  977. int bitmap_left = left_x + glyph->offset_x;
  978. int bitmap_bottom = baseline_y - glyph->offset_y;
  979. int bitmap_top = bitmap_bottom - glyph->height;
  980. return grub_video_blit_bitmap (&glyph_bitmap, GRUB_VIDEO_BLIT_BLEND,
  981. bitmap_left, bitmap_top,
  982. 0, 0, glyph->width, glyph->height);
  983. }
  984. /* Draw a UTF-8 string of text on the current video render target.
  985. The x coordinate specifies the starting x position for the first character,
  986. while the y coordinate specifies the baseline position.
  987. If the string contains a character that FONT does not contain, then
  988. a glyph from another loaded font may be used instead. */
  989. grub_err_t
  990. grub_font_draw_string (const char *str, grub_font_t font,
  991. grub_video_color_t color, int left_x, int baseline_y)
  992. {
  993. int x;
  994. struct grub_font_glyph *glyph;
  995. grub_uint32_t code;
  996. const grub_uint8_t *ptr;
  997. for (ptr = (const grub_uint8_t *) str, x = left_x;
  998. grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0;)
  999. {
  1000. glyph = grub_font_get_glyph_with_fallback (font, code);
  1001. if (grub_font_draw_glyph (glyph, color, x, baseline_y) != GRUB_ERR_NONE)
  1002. return grub_errno;
  1003. x += glyph->device_width;
  1004. }
  1005. return GRUB_ERR_NONE;
  1006. }