font.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  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/charset.h>
  29. #include <grub/unicode.h>
  30. #include <grub/fontformat.h>
  31. #include <grub/env.h>
  32. GRUB_MOD_LICENSE ("GPLv3+");
  33. #if HAVE_FONT_SOURCE
  34. #include "ascii.h"
  35. #endif
  36. #ifndef FONT_DEBUG
  37. #define FONT_DEBUG 0
  38. #endif
  39. struct char_index_entry
  40. {
  41. grub_uint32_t code;
  42. grub_uint8_t storage_flags;
  43. grub_uint32_t offset;
  44. /* Glyph if loaded, or NULL otherwise. */
  45. struct grub_font_glyph *glyph;
  46. };
  47. #define FONT_WEIGHT_NORMAL 100
  48. #define FONT_WEIGHT_BOLD 200
  49. #define ASCII_BITMAP_SIZE 16
  50. /* Definition of font registry. */
  51. struct grub_font_node *grub_font_list;
  52. static int register_font (grub_font_t font);
  53. static void font_init (grub_font_t font);
  54. static void free_font (grub_font_t font);
  55. static void remove_font (grub_font_t font);
  56. struct font_file_section
  57. {
  58. /* The file this section is in. */
  59. grub_file_t file;
  60. /* FOURCC name of the section. */
  61. char name[4];
  62. /* Length of the section contents. */
  63. grub_uint32_t length;
  64. /* Set by open_section() on EOF. */
  65. int eof;
  66. };
  67. /* Replace unknown glyphs with a rounded question mark. */
  68. static grub_uint8_t unknown_glyph_bitmap[] = {
  69. /* 76543210 */
  70. 0x7C, /* ooooo */
  71. 0x82, /* o o */
  72. 0xBA, /* o ooo o */
  73. 0xAA, /* o o o o */
  74. 0xAA, /* o o o o */
  75. 0x8A, /* o o o */
  76. 0x9A, /* o oo o */
  77. 0x92, /* o o o */
  78. 0x92, /* o o o */
  79. 0x92, /* o o o */
  80. 0x92, /* o o o */
  81. 0x82, /* o o */
  82. 0x92, /* o o o */
  83. 0x82, /* o o */
  84. 0x7C, /* ooooo */
  85. 0x00 /* */
  86. };
  87. /* The "unknown glyph" glyph, used as a last resort. */
  88. static struct grub_font_glyph *unknown_glyph;
  89. /* The font structure used when no other font is loaded. This functions
  90. as a "Null Object" pattern, so that code everywhere does not have to
  91. check for a NULL grub_font_t to avoid dereferencing a null pointer. */
  92. static struct grub_font null_font;
  93. /* Flag to ensure module is initialized only once. */
  94. static grub_uint8_t font_loader_initialized;
  95. #if HAVE_FONT_SOURCE
  96. static struct grub_font_glyph *ascii_font_glyph[0x80];
  97. #endif
  98. static struct grub_font_glyph *
  99. ascii_glyph_lookup (grub_uint32_t code)
  100. {
  101. #if HAVE_FONT_SOURCE
  102. static int ascii_failback_initialized = 0;
  103. if (code >= 0x80)
  104. return NULL;
  105. if (ascii_failback_initialized == 0)
  106. {
  107. int current;
  108. for (current = 0; current < 0x80; current++)
  109. {
  110. ascii_font_glyph[current] =
  111. grub_malloc (sizeof (struct grub_font_glyph) + ASCII_BITMAP_SIZE);
  112. ascii_font_glyph[current]->width = 8;
  113. ascii_font_glyph[current]->height = 16;
  114. ascii_font_glyph[current]->offset_x = 0;
  115. ascii_font_glyph[current]->offset_y = -2;
  116. ascii_font_glyph[current]->device_width = 8;
  117. ascii_font_glyph[current]->font = NULL;
  118. grub_memcpy (ascii_font_glyph[current]->bitmap,
  119. &ascii_bitmaps[current * ASCII_BITMAP_SIZE],
  120. ASCII_BITMAP_SIZE);
  121. }
  122. ascii_failback_initialized = 1;
  123. }
  124. return ascii_font_glyph[code];
  125. #else
  126. (void) code;
  127. return NULL;
  128. #endif
  129. }
  130. void
  131. grub_font_loader_init (void)
  132. {
  133. /* Only initialize font loader once. */
  134. if (font_loader_initialized)
  135. return;
  136. /* Make glyph for unknown glyph. */
  137. unknown_glyph = grub_malloc (sizeof (struct grub_font_glyph)
  138. + sizeof (unknown_glyph_bitmap));
  139. if (!unknown_glyph)
  140. return;
  141. unknown_glyph->width = 8;
  142. unknown_glyph->height = 16;
  143. unknown_glyph->offset_x = 0;
  144. unknown_glyph->offset_y = -3;
  145. unknown_glyph->device_width = 8;
  146. grub_memcpy (unknown_glyph->bitmap,
  147. unknown_glyph_bitmap, sizeof (unknown_glyph_bitmap));
  148. /* Initialize the null font. */
  149. font_init (&null_font);
  150. /* FIXME: Fix this slightly improper cast. */
  151. null_font.name = (char *) "<No Font>";
  152. null_font.ascent = unknown_glyph->height - 3;
  153. null_font.descent = 3;
  154. null_font.max_char_width = unknown_glyph->width;
  155. null_font.max_char_height = unknown_glyph->height;
  156. font_loader_initialized = 1;
  157. }
  158. /* Initialize the font object with initial default values. */
  159. static void
  160. font_init (grub_font_t font)
  161. {
  162. font->name = 0;
  163. font->file = 0;
  164. font->family = 0;
  165. font->point_size = 0;
  166. font->weight = 0;
  167. /* Default leading value, not in font file yet. */
  168. font->leading = 1;
  169. font->max_char_width = 0;
  170. font->max_char_height = 0;
  171. font->ascent = 0;
  172. font->descent = 0;
  173. font->num_chars = 0;
  174. font->char_index = 0;
  175. font->bmp_idx = 0;
  176. }
  177. /* Open the next section in the file.
  178. On success, the section name is stored in section->name and the length in
  179. section->length, and 0 is returned. On failure, 1 is returned and
  180. grub_errno is set appropriately with an error message.
  181. If 1 is returned due to being at the end of the file, then section->eof is
  182. set to 1; otherwise, section->eof is set to 0. */
  183. static int
  184. open_section (grub_file_t file, struct font_file_section *section)
  185. {
  186. grub_ssize_t retval;
  187. grub_uint32_t raw_length;
  188. section->file = file;
  189. section->eof = 0;
  190. /* Read the FOURCC section name. */
  191. retval = grub_file_read (file, section->name, 4);
  192. if (retval >= 0 && retval < 4)
  193. {
  194. /* EOF encountered. */
  195. section->eof = 1;
  196. return 1;
  197. }
  198. else if (retval < 0)
  199. {
  200. /* Read error. */
  201. return 1;
  202. }
  203. /* Read the big-endian 32-bit section length. */
  204. retval = grub_file_read (file, &raw_length, 4);
  205. if (retval >= 0 && retval < 4)
  206. {
  207. /* EOF encountered. */
  208. section->eof = 1;
  209. return 1;
  210. }
  211. else if (retval < 0)
  212. {
  213. /* Read error. */
  214. return 1;
  215. }
  216. /* Convert byte-order and store in *length. */
  217. section->length = grub_be_to_cpu32 (raw_length);
  218. return 0;
  219. }
  220. /* Size in bytes of each character index (CHIX section)
  221. entry in the font file. */
  222. #define FONT_CHAR_INDEX_ENTRY_SIZE (4 + 1 + 4)
  223. /* Load the character index (CHIX) section contents from the font file. This
  224. presumes that the position of FILE is positioned immediately after the
  225. section length for the CHIX section (i.e., at the start of the section
  226. contents). Returns 0 upon success, nonzero for failure (in which case
  227. grub_errno is set appropriately). */
  228. static int
  229. load_font_index (grub_file_t file, grub_uint32_t sect_length, struct
  230. grub_font *font)
  231. {
  232. unsigned i;
  233. grub_uint32_t last_code;
  234. #if FONT_DEBUG >= 2
  235. grub_dprintf ("font", "load_font_index(sect_length=%d)\n", sect_length);
  236. #endif
  237. /* Sanity check: ensure section length is divisible by the entry size. */
  238. if ((sect_length % FONT_CHAR_INDEX_ENTRY_SIZE) != 0)
  239. {
  240. grub_error (GRUB_ERR_BAD_FONT,
  241. "font file format error: character index length %d "
  242. "is not a multiple of the entry size %d",
  243. sect_length, FONT_CHAR_INDEX_ENTRY_SIZE);
  244. return 1;
  245. }
  246. /* Calculate the number of characters. */
  247. font->num_chars = sect_length / FONT_CHAR_INDEX_ENTRY_SIZE;
  248. /* Allocate the character index array. */
  249. font->char_index = grub_malloc (font->num_chars
  250. * sizeof (struct char_index_entry));
  251. if (!font->char_index)
  252. return 1;
  253. font->bmp_idx = grub_malloc (0x10000 * sizeof (grub_uint16_t));
  254. if (!font->bmp_idx)
  255. return 1;
  256. grub_memset (font->bmp_idx, 0xff, 0x10000 * sizeof (grub_uint16_t));
  257. #if FONT_DEBUG >= 2
  258. grub_dprintf ("font", "num_chars=%d)\n", font->num_chars);
  259. #endif
  260. last_code = 0;
  261. /* Load the character index data from the file. */
  262. for (i = 0; i < font->num_chars; i++)
  263. {
  264. struct char_index_entry *entry = &font->char_index[i];
  265. /* Read code point value; convert to native byte order. */
  266. if (grub_file_read (file, &entry->code, 4) != 4)
  267. return 1;
  268. entry->code = grub_be_to_cpu32 (entry->code);
  269. /* Verify that characters are in ascending order. */
  270. if (i != 0 && entry->code <= last_code)
  271. {
  272. grub_error (GRUB_ERR_BAD_FONT,
  273. "font characters not in ascending order: %u <= %u",
  274. entry->code, last_code);
  275. return 1;
  276. }
  277. if (entry->code < 0x10000)
  278. font->bmp_idx[entry->code] = i;
  279. last_code = entry->code;
  280. /* Read storage flags byte. */
  281. if (grub_file_read (file, &entry->storage_flags, 1) != 1)
  282. return 1;
  283. /* Read glyph data offset; convert to native byte order. */
  284. if (grub_file_read (file, &entry->offset, 4) != 4)
  285. return 1;
  286. entry->offset = grub_be_to_cpu32 (entry->offset);
  287. /* No glyph loaded. Will be loaded on demand and cached thereafter. */
  288. entry->glyph = 0;
  289. #if FONT_DEBUG >= 5
  290. /* Print the 1st 10 characters. */
  291. if (i < 10)
  292. grub_dprintf ("font", "c=%d o=%d\n", entry->code, entry->offset);
  293. #endif
  294. }
  295. return 0;
  296. }
  297. /* Read the contents of the specified section as a string, which is
  298. allocated on the heap. Returns 0 if there is an error. */
  299. static char *
  300. read_section_as_string (struct font_file_section *section)
  301. {
  302. char *str;
  303. grub_ssize_t ret;
  304. str = grub_malloc (section->length + 1);
  305. if (!str)
  306. return 0;
  307. ret = grub_file_read (section->file, str, section->length);
  308. if (ret < 0 || ret != (grub_ssize_t) section->length)
  309. {
  310. grub_free (str);
  311. return 0;
  312. }
  313. str[section->length] = '\0';
  314. return str;
  315. }
  316. /* Read the contents of the current section as a 16-bit integer value,
  317. which is stored into *VALUE.
  318. Returns 0 upon success, nonzero upon failure. */
  319. static int
  320. read_section_as_short (struct font_file_section *section,
  321. grub_int16_t * value)
  322. {
  323. grub_uint16_t raw_value;
  324. if (section->length != 2)
  325. {
  326. grub_error (GRUB_ERR_BAD_FONT,
  327. "font file format error: section %c%c%c%c length "
  328. "is %d but should be 2",
  329. section->name[0], section->name[1],
  330. section->name[2], section->name[3], section->length);
  331. return 1;
  332. }
  333. if (grub_file_read (section->file, &raw_value, 2) != 2)
  334. return 1;
  335. *value = grub_be_to_cpu16 (raw_value);
  336. return 0;
  337. }
  338. /* Load a font and add it to the beginning of the global font list.
  339. Returns 0 upon success, nonzero upon failure. */
  340. grub_font_t
  341. grub_font_load (const char *filename)
  342. {
  343. grub_file_t file = 0;
  344. struct font_file_section section;
  345. char magic[4];
  346. grub_font_t font = 0;
  347. #if FONT_DEBUG >= 1
  348. grub_dprintf ("font", "add_font(%s)\n", filename);
  349. #endif
  350. if (filename[0] == '(' || filename[0] == '/' || filename[0] == '+')
  351. file = grub_buffile_open (filename, 1024);
  352. else
  353. {
  354. const char *prefix = grub_env_get ("prefix");
  355. char *fullname, *ptr;
  356. if (!prefix)
  357. {
  358. grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"),
  359. "prefix");
  360. goto fail;
  361. }
  362. fullname = grub_malloc (grub_strlen (prefix) + grub_strlen (filename) + 1
  363. + sizeof ("/fonts/") + sizeof (".pf2"));
  364. if (!fullname)
  365. goto fail;
  366. ptr = grub_stpcpy (fullname, prefix);
  367. ptr = grub_stpcpy (ptr, "/fonts/");
  368. ptr = grub_stpcpy (ptr, filename);
  369. ptr = grub_stpcpy (ptr, ".pf2");
  370. *ptr = 0;
  371. file = grub_buffile_open (fullname, 1024);
  372. grub_free (fullname);
  373. }
  374. if (!file)
  375. goto fail;
  376. #if FONT_DEBUG >= 3
  377. grub_dprintf ("font", "file opened\n");
  378. #endif
  379. /* Read the FILE section. It indicates the file format. */
  380. if (open_section (file, &section) != 0)
  381. goto fail;
  382. #if FONT_DEBUG >= 3
  383. grub_dprintf ("font", "opened FILE section\n");
  384. #endif
  385. if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FILE,
  386. sizeof (FONT_FORMAT_SECTION_NAMES_FILE) - 1) != 0)
  387. {
  388. grub_error (GRUB_ERR_BAD_FONT,
  389. "font file format error: 1st section must be FILE");
  390. goto fail;
  391. }
  392. #if FONT_DEBUG >= 3
  393. grub_dprintf ("font", "section name ok\n");
  394. #endif
  395. if (section.length != 4)
  396. {
  397. grub_error (GRUB_ERR_BAD_FONT,
  398. "font file format error (file type ID length is %d "
  399. "but should be 4)", section.length);
  400. goto fail;
  401. }
  402. #if FONT_DEBUG >= 3
  403. grub_dprintf ("font", "section length ok\n");
  404. #endif
  405. /* Check the file format type code. */
  406. if (grub_file_read (file, magic, 4) != 4)
  407. goto fail;
  408. #if FONT_DEBUG >= 3
  409. grub_dprintf ("font", "read magic ok\n");
  410. #endif
  411. if (grub_memcmp (magic, FONT_FORMAT_PFF2_MAGIC, 4) != 0)
  412. {
  413. grub_error (GRUB_ERR_BAD_FONT, "invalid font magic %x %x %x %x",
  414. magic[0], magic[1], magic[2], magic[3]);
  415. goto fail;
  416. }
  417. #if FONT_DEBUG >= 3
  418. grub_dprintf ("font", "compare magic ok\n");
  419. #endif
  420. /* Allocate the font object. */
  421. font = (grub_font_t) grub_zalloc (sizeof (struct grub_font));
  422. if (!font)
  423. goto fail;
  424. font_init (font);
  425. font->file = file;
  426. #if FONT_DEBUG >= 3
  427. grub_dprintf ("font", "allocate font ok; loading font info\n");
  428. #endif
  429. /* Load the font information. */
  430. while (1)
  431. {
  432. if (open_section (file, &section) != 0)
  433. {
  434. if (section.eof)
  435. break; /* Done reading the font file. */
  436. else
  437. goto fail;
  438. }
  439. #if FONT_DEBUG >= 2
  440. grub_dprintf ("font", "opened section %c%c%c%c ok\n",
  441. section.name[0], section.name[1],
  442. section.name[2], section.name[3]);
  443. #endif
  444. if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FONT_NAME,
  445. sizeof (FONT_FORMAT_SECTION_NAMES_FONT_NAME) - 1) == 0)
  446. {
  447. font->name = read_section_as_string (&section);
  448. if (!font->name)
  449. goto fail;
  450. }
  451. else if (grub_memcmp (section.name,
  452. FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
  453. sizeof (FONT_FORMAT_SECTION_NAMES_POINT_SIZE) -
  454. 1) == 0)
  455. {
  456. if (read_section_as_short (&section, &font->point_size) != 0)
  457. goto fail;
  458. }
  459. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_WEIGHT,
  460. sizeof (FONT_FORMAT_SECTION_NAMES_WEIGHT) - 1)
  461. == 0)
  462. {
  463. char *wt;
  464. wt = read_section_as_string (&section);
  465. if (!wt)
  466. continue;
  467. /* Convert the weight string 'normal' or 'bold' into a number. */
  468. if (grub_strcmp (wt, "normal") == 0)
  469. font->weight = FONT_WEIGHT_NORMAL;
  470. else if (grub_strcmp (wt, "bold") == 0)
  471. font->weight = FONT_WEIGHT_BOLD;
  472. grub_free (wt);
  473. }
  474. else if (grub_memcmp (section.name,
  475. FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
  476. sizeof (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH)
  477. - 1) == 0)
  478. {
  479. if (read_section_as_short (&section, &font->max_char_width) != 0)
  480. goto fail;
  481. }
  482. else if (grub_memcmp (section.name,
  483. FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
  484. sizeof (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT)
  485. - 1) == 0)
  486. {
  487. if (read_section_as_short (&section, &font->max_char_height) != 0)
  488. goto fail;
  489. }
  490. else if (grub_memcmp (section.name,
  491. FONT_FORMAT_SECTION_NAMES_ASCENT,
  492. sizeof (FONT_FORMAT_SECTION_NAMES_ASCENT) - 1)
  493. == 0)
  494. {
  495. if (read_section_as_short (&section, &font->ascent) != 0)
  496. goto fail;
  497. }
  498. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DESCENT,
  499. sizeof (FONT_FORMAT_SECTION_NAMES_DESCENT) - 1)
  500. == 0)
  501. {
  502. if (read_section_as_short (&section, &font->descent) != 0)
  503. goto fail;
  504. }
  505. else if (grub_memcmp (section.name,
  506. FONT_FORMAT_SECTION_NAMES_CHAR_INDEX,
  507. sizeof (FONT_FORMAT_SECTION_NAMES_CHAR_INDEX) -
  508. 1) == 0)
  509. {
  510. if (load_font_index (file, section.length, font) != 0)
  511. goto fail;
  512. }
  513. else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DATA,
  514. sizeof (FONT_FORMAT_SECTION_NAMES_DATA) - 1) == 0)
  515. {
  516. /* When the DATA section marker is reached, we stop reading. */
  517. break;
  518. }
  519. else
  520. {
  521. /* Unhandled section type, simply skip past it. */
  522. #if FONT_DEBUG >= 3
  523. grub_dprintf ("font", "Unhandled section type, skipping.\n");
  524. #endif
  525. grub_off_t section_end = grub_file_tell (file) + section.length;
  526. if ((int) grub_file_seek (file, section_end) == -1)
  527. goto fail;
  528. }
  529. }
  530. if (!font->name)
  531. {
  532. grub_dprintf ("font", "Font has no name.\n");
  533. font->name = grub_strdup ("Unknown");
  534. }
  535. #if FONT_DEBUG >= 1
  536. grub_dprintf ("font", "Loaded font `%s'.\n"
  537. "Ascent=%d Descent=%d MaxW=%d MaxH=%d Number of characters=%d.\n",
  538. font->name,
  539. font->ascent, font->descent,
  540. font->max_char_width, font->max_char_height, font->num_chars);
  541. #endif
  542. if (font->max_char_width == 0
  543. || font->max_char_height == 0
  544. || font->num_chars == 0
  545. || font->char_index == 0 || font->ascent == 0 || font->descent == 0)
  546. {
  547. grub_error (GRUB_ERR_BAD_FONT,
  548. "invalid font file: missing some required data");
  549. goto fail;
  550. }
  551. /* Add the font to the global font registry. */
  552. if (register_font (font) != 0)
  553. goto fail;
  554. return font;
  555. fail:
  556. if (file)
  557. grub_file_close (file);
  558. if (font)
  559. font->file = 0;
  560. free_font (font);
  561. return 0;
  562. }
  563. /* Read a 16-bit big-endian integer from FILE, convert it to native byte
  564. order, and store it in *VALUE.
  565. Returns 0 on success, 1 on failure. */
  566. static int
  567. read_be_uint16 (grub_file_t file, grub_uint16_t * value)
  568. {
  569. if (grub_file_read (file, value, 2) != 2)
  570. return 1;
  571. *value = grub_be_to_cpu16 (*value);
  572. return 0;
  573. }
  574. static int
  575. read_be_int16 (grub_file_t file, grub_int16_t * value)
  576. {
  577. /* For the signed integer version, use the same code as for unsigned. */
  578. return read_be_uint16 (file, (grub_uint16_t *) value);
  579. }
  580. /* Return a pointer to the character index entry for the glyph corresponding to
  581. the codepoint CODE in the font FONT. If not found, return zero. */
  582. static inline struct char_index_entry *
  583. find_glyph (const grub_font_t font, grub_uint32_t code)
  584. {
  585. struct char_index_entry *table;
  586. grub_size_t lo;
  587. grub_size_t hi;
  588. grub_size_t mid;
  589. table = font->char_index;
  590. /* Use BMP index if possible. */
  591. if (code < 0x10000 && font->bmp_idx)
  592. {
  593. if (font->bmp_idx[code] == 0xffff)
  594. return 0;
  595. return &table[font->bmp_idx[code]];
  596. }
  597. /* Do a binary search in `char_index', which is ordered by code point. */
  598. lo = 0;
  599. hi = font->num_chars - 1;
  600. if (!table)
  601. return 0;
  602. while (lo <= hi)
  603. {
  604. mid = lo + (hi - lo) / 2;
  605. if (code < table[mid].code)
  606. hi = mid - 1;
  607. else if (code > table[mid].code)
  608. lo = mid + 1;
  609. else
  610. return &table[mid];
  611. }
  612. return 0;
  613. }
  614. /* Get a glyph for the Unicode character CODE in FONT. The glyph is loaded
  615. from the font file if has not been loaded yet.
  616. Returns a pointer to the glyph if found, or 0 if it is not found. */
  617. static struct grub_font_glyph *
  618. grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)
  619. {
  620. struct char_index_entry *index_entry;
  621. index_entry = find_glyph (font, code);
  622. if (index_entry)
  623. {
  624. struct grub_font_glyph *glyph = 0;
  625. grub_uint16_t width;
  626. grub_uint16_t height;
  627. grub_int16_t xoff;
  628. grub_int16_t yoff;
  629. grub_int16_t dwidth;
  630. int len;
  631. if (index_entry->glyph)
  632. /* Return cached glyph. */
  633. return index_entry->glyph;
  634. if (!font->file)
  635. /* No open file, can't load any glyphs. */
  636. return 0;
  637. /* Make sure we can find glyphs for error messages. Push active
  638. error message to error stack and reset error message. */
  639. grub_error_push ();
  640. grub_file_seek (font->file, index_entry->offset);
  641. /* Read the glyph width, height, and baseline. */
  642. if (read_be_uint16 (font->file, &width) != 0
  643. || read_be_uint16 (font->file, &height) != 0
  644. || read_be_int16 (font->file, &xoff) != 0
  645. || read_be_int16 (font->file, &yoff) != 0
  646. || read_be_int16 (font->file, &dwidth) != 0)
  647. {
  648. remove_font (font);
  649. return 0;
  650. }
  651. len = (width * height + 7) / 8;
  652. glyph = grub_malloc (sizeof (struct grub_font_glyph) + len);
  653. if (!glyph)
  654. {
  655. remove_font (font);
  656. return 0;
  657. }
  658. glyph->font = font;
  659. glyph->width = width;
  660. glyph->height = height;
  661. glyph->offset_x = xoff;
  662. glyph->offset_y = yoff;
  663. glyph->device_width = dwidth;
  664. /* Don't try to read empty bitmaps (e.g., space characters). */
  665. if (len != 0)
  666. {
  667. if (grub_file_read (font->file, glyph->bitmap, len) != len)
  668. {
  669. remove_font (font);
  670. grub_free (glyph);
  671. return 0;
  672. }
  673. }
  674. /* Restore old error message. */
  675. grub_error_pop ();
  676. /* Cache the glyph. */
  677. index_entry->glyph = glyph;
  678. return glyph;
  679. }
  680. return 0;
  681. }
  682. /* Free the memory used by FONT.
  683. This should not be called if the font has been made available to
  684. users (once it is added to the global font list), since there would
  685. be the possibility of a dangling pointer. */
  686. static void
  687. free_font (grub_font_t font)
  688. {
  689. if (font)
  690. {
  691. if (font->file)
  692. grub_file_close (font->file);
  693. grub_free (font->name);
  694. grub_free (font->family);
  695. grub_free (font->char_index);
  696. grub_free (font->bmp_idx);
  697. grub_free (font);
  698. }
  699. }
  700. /* Add FONT to the global font registry.
  701. Returns 0 upon success, nonzero on failure
  702. (the font was not registered). */
  703. static int
  704. register_font (grub_font_t font)
  705. {
  706. struct grub_font_node *node = 0;
  707. node = grub_malloc (sizeof (struct grub_font_node));
  708. if (!node)
  709. return 1;
  710. node->value = font;
  711. node->next = grub_font_list;
  712. grub_font_list = node;
  713. return 0;
  714. }
  715. /* Remove the font from the global font list. We don't actually free the
  716. font's memory since users could be holding references to the font. */
  717. static void
  718. remove_font (grub_font_t font)
  719. {
  720. struct grub_font_node **nextp, *cur;
  721. for (nextp = &grub_font_list, cur = *nextp;
  722. cur; nextp = &cur->next, cur = cur->next)
  723. {
  724. if (cur->value == font)
  725. {
  726. *nextp = cur->next;
  727. /* Free the node, but not the font itself. */
  728. grub_free (cur);
  729. return;
  730. }
  731. }
  732. }
  733. /* Get a font from the list of loaded fonts. This function will return
  734. another font if the requested font is not available. If no fonts are
  735. loaded, then a special 'null font' is returned, which contains no glyphs,
  736. but is not a null pointer so the caller may omit checks for NULL. */
  737. grub_font_t
  738. grub_font_get (const char *font_name)
  739. {
  740. struct grub_font_node *node;
  741. for (node = grub_font_list; node; node = node->next)
  742. {
  743. grub_font_t font = node->value;
  744. if (grub_strcmp (font->name, font_name) == 0)
  745. return font;
  746. }
  747. /* If no font by that name is found, return the first font in the list
  748. as a fallback. */
  749. if (grub_font_list && grub_font_list->value)
  750. return grub_font_list->value;
  751. else
  752. /* The null_font is a last resort. */
  753. return &null_font;
  754. }
  755. /* Get the full name of the font. */
  756. const char *
  757. grub_font_get_name (grub_font_t font)
  758. {
  759. return font->name;
  760. }
  761. /* Get the maximum width of any character in the font in pixels. */
  762. int
  763. grub_font_get_max_char_width (grub_font_t font)
  764. {
  765. return font->max_char_width;
  766. }
  767. /* Get the distance in pixels from the baseline to the lowest descenders
  768. (for instance, in a lowercase 'y', 'g', etc.). */
  769. int
  770. grub_font_get_descent (grub_font_t font)
  771. {
  772. return font->descent;
  773. }
  774. /* FIXME: not correct for all fonts. */
  775. int
  776. grub_font_get_xheight (grub_font_t font)
  777. {
  778. return font->ascent / 2;
  779. }
  780. /* Get the *standard leading* of the font in pixel, which is the spacing
  781. between two lines of text. Specifically, it is the space between the
  782. descent of one line and the ascent of the next line. This is included
  783. in the *height* metric. */
  784. int
  785. grub_font_get_leading (grub_font_t font)
  786. {
  787. return font->leading;
  788. }
  789. /* Get the distance in pixels between baselines of adjacent lines of text. */
  790. int
  791. grub_font_get_height (grub_font_t font)
  792. {
  793. return font->ascent + font->descent + font->leading;
  794. }
  795. /* Get the glyph for FONT corresponding to the Unicode code point CODE.
  796. Returns the ASCII glyph for the code if no other fonts are available.
  797. The glyphs are cached once loaded. */
  798. struct grub_font_glyph *
  799. grub_font_get_glyph (grub_font_t font, grub_uint32_t code)
  800. {
  801. struct grub_font_glyph *glyph = 0;
  802. if (font)
  803. glyph = grub_font_get_glyph_internal (font, code);
  804. if (glyph == 0)
  805. {
  806. glyph = ascii_glyph_lookup (code);
  807. }
  808. return glyph;
  809. }
  810. /* Calculate a subject value representing "how similar" two fonts are.
  811. This is used to prioritize the order that fonts are scanned for missing
  812. glyphs. The object is to select glyphs from the most similar font
  813. possible, for the best appearance.
  814. The heuristic is crude, but it helps greatly when fonts of similar
  815. sizes are used so that tiny 8 point glyphs are not mixed into a string
  816. of 24 point text unless there is no other choice. */
  817. static int
  818. get_font_diversity (grub_font_t a, grub_font_t b)
  819. {
  820. int d;
  821. d = 0;
  822. if (a->ascent && b->ascent)
  823. d += grub_abs (a->ascent - b->ascent) * 8;
  824. else
  825. /* Penalty for missing attributes. */
  826. d += 50;
  827. if (a->max_char_height && b->max_char_height)
  828. d += grub_abs (a->max_char_height - b->max_char_height) * 8;
  829. else
  830. /* Penalty for missing attributes. */
  831. d += 50;
  832. /* Weight is a minor factor. */
  833. d += (a->weight != b->weight) ? 5 : 0;
  834. return d;
  835. }
  836. /* Get a glyph corresponding to the codepoint CODE. If FONT contains the
  837. specified glyph, then it is returned. Otherwise, all other loaded fonts
  838. are searched until one is found that contains a glyph for CODE.
  839. If no glyph is available for CODE in the loaded fonts, then a glyph
  840. representing an unknown character is returned.
  841. This function never returns NULL.
  842. The returned glyph is owned by the font manager and should not be freed
  843. by the caller. The glyphs are cached. */
  844. struct grub_font_glyph *
  845. grub_font_get_glyph_with_fallback (grub_font_t font, grub_uint32_t code)
  846. {
  847. struct grub_font_glyph *glyph;
  848. struct grub_font_node *node;
  849. /* Keep track of next node, in case there's an I/O error in
  850. grub_font_get_glyph_internal() and the font is removed from the list. */
  851. struct grub_font_node *next;
  852. /* Information on the best glyph found so far, to help find the glyph in
  853. the best matching to the requested one. */
  854. int best_diversity;
  855. struct grub_font_glyph *best_glyph;
  856. if (font)
  857. {
  858. /* First try to get the glyph from the specified font. */
  859. glyph = grub_font_get_glyph_internal (font, code);
  860. if (glyph)
  861. return glyph;
  862. }
  863. /* Otherwise, search all loaded fonts for the glyph and use the one from
  864. the font that best matches the requested font. */
  865. best_diversity = 10000;
  866. best_glyph = 0;
  867. for (node = grub_font_list; node; node = next)
  868. {
  869. grub_font_t curfont;
  870. curfont = node->value;
  871. next = node->next;
  872. glyph = grub_font_get_glyph_internal (curfont, code);
  873. if (glyph && !font)
  874. return glyph;
  875. if (glyph)
  876. {
  877. int d;
  878. d = get_font_diversity (curfont, font);
  879. if (d < best_diversity)
  880. {
  881. best_diversity = d;
  882. best_glyph = glyph;
  883. }
  884. }
  885. }
  886. return best_glyph;
  887. }
  888. #if 0
  889. static struct grub_font_glyph *
  890. grub_font_dup_glyph (struct grub_font_glyph *glyph)
  891. {
  892. static struct grub_font_glyph *ret;
  893. ret = grub_malloc (sizeof (*ret) + (glyph->width * glyph->height + 7) / 8);
  894. if (!ret)
  895. return NULL;
  896. grub_memcpy (ret, glyph, sizeof (*ret)
  897. + (glyph->width * glyph->height + 7) / 8);
  898. return ret;
  899. }
  900. #endif
  901. /* FIXME: suboptimal. */
  902. static void
  903. grub_font_blit_glyph (struct grub_font_glyph *target,
  904. struct grub_font_glyph *src, unsigned dx, unsigned dy)
  905. {
  906. unsigned src_bit, tgt_bit, src_byte, tgt_byte;
  907. unsigned i, j;
  908. for (i = 0; i < src->height; i++)
  909. {
  910. src_bit = (src->width * i) % 8;
  911. src_byte = (src->width * i) / 8;
  912. tgt_bit = (target->width * (dy + i) + dx) % 8;
  913. tgt_byte = (target->width * (dy + i) + dx) / 8;
  914. for (j = 0; j < src->width; j++)
  915. {
  916. target->bitmap[tgt_byte] |= ((src->bitmap[src_byte] << src_bit)
  917. & 0x80) >> tgt_bit;
  918. src_bit++;
  919. tgt_bit++;
  920. if (src_bit == 8)
  921. {
  922. src_byte++;
  923. src_bit = 0;
  924. }
  925. if (tgt_bit == 8)
  926. {
  927. tgt_byte++;
  928. tgt_bit = 0;
  929. }
  930. }
  931. }
  932. }
  933. static void
  934. grub_font_blit_glyph_mirror (struct grub_font_glyph *target,
  935. struct grub_font_glyph *src,
  936. unsigned dx, unsigned dy)
  937. {
  938. unsigned tgt_bit, src_byte, tgt_byte;
  939. signed src_bit;
  940. unsigned i, j;
  941. for (i = 0; i < src->height; i++)
  942. {
  943. src_bit = (src->width * i + src->width - 1) % 8;
  944. src_byte = (src->width * i + src->width - 1) / 8;
  945. tgt_bit = (target->width * (dy + i) + dx) % 8;
  946. tgt_byte = (target->width * (dy + i) + dx) / 8;
  947. for (j = 0; j < src->width; j++)
  948. {
  949. target->bitmap[tgt_byte] |= ((src->bitmap[src_byte] << src_bit)
  950. & 0x80) >> tgt_bit;
  951. src_bit--;
  952. tgt_bit++;
  953. if (src_bit == -1)
  954. {
  955. src_byte--;
  956. src_bit = 7;
  957. }
  958. if (tgt_bit == 8)
  959. {
  960. tgt_byte++;
  961. tgt_bit = 0;
  962. }
  963. }
  964. }
  965. }
  966. /* Context for blit_comb. */
  967. struct blit_comb_ctx
  968. {
  969. struct grub_font_glyph *glyph;
  970. int *device_width;
  971. struct grub_video_signed_rect bounds;
  972. };
  973. /* Helper for blit_comb. */
  974. static void
  975. do_blit (struct grub_font_glyph *src, signed dx, signed dy,
  976. struct blit_comb_ctx *ctx)
  977. {
  978. if (ctx->glyph)
  979. grub_font_blit_glyph (ctx->glyph, src, dx - ctx->glyph->offset_x,
  980. (ctx->glyph->height + ctx->glyph->offset_y) + dy);
  981. if (dx < ctx->bounds.x)
  982. {
  983. ctx->bounds.width += ctx->bounds.x - dx;
  984. ctx->bounds.x = dx;
  985. }
  986. if (ctx->bounds.y > -src->height - dy)
  987. {
  988. ctx->bounds.height += ctx->bounds.y - (-src->height - dy);
  989. ctx->bounds.y = (-src->height - dy);
  990. }
  991. if (dx + src->width - ctx->bounds.x >= (signed) ctx->bounds.width)
  992. ctx->bounds.width = dx + src->width - ctx->bounds.x + 1;
  993. if ((signed) ctx->bounds.height < src->height + (-src->height - dy)
  994. - ctx->bounds.y)
  995. ctx->bounds.height = src->height + (-src->height - dy) - ctx->bounds.y;
  996. }
  997. /* Helper for blit_comb. */
  998. static inline void
  999. add_device_width (int val, struct blit_comb_ctx *ctx)
  1000. {
  1001. if (ctx->glyph)
  1002. ctx->glyph->device_width += val;
  1003. if (ctx->device_width)
  1004. *ctx->device_width += val;
  1005. }
  1006. static void
  1007. blit_comb (const struct grub_unicode_glyph *glyph_id,
  1008. struct grub_font_glyph *glyph,
  1009. struct grub_video_signed_rect *bounds_out,
  1010. struct grub_font_glyph *main_glyph,
  1011. struct grub_font_glyph **combining_glyphs, int *device_width)
  1012. {
  1013. struct blit_comb_ctx ctx = {
  1014. .glyph = glyph,
  1015. .device_width = device_width
  1016. };
  1017. unsigned i;
  1018. signed above_rightx, above_righty;
  1019. signed above_leftx, above_lefty;
  1020. signed below_rightx, below_righty;
  1021. signed min_devwidth = 0;
  1022. const struct grub_unicode_combining *comb;
  1023. if (glyph)
  1024. glyph->device_width = main_glyph->device_width;
  1025. if (device_width)
  1026. *device_width = main_glyph->device_width;
  1027. ctx.bounds.x = main_glyph->offset_x;
  1028. ctx.bounds.y = main_glyph->offset_y;
  1029. ctx.bounds.width = main_glyph->width;
  1030. ctx.bounds.height = main_glyph->height;
  1031. above_rightx = main_glyph->offset_x + main_glyph->width;
  1032. above_righty = ctx.bounds.y + ctx.bounds.height;
  1033. above_leftx = main_glyph->offset_x;
  1034. above_lefty = ctx.bounds.y + ctx.bounds.height;
  1035. below_rightx = ctx.bounds.x + ctx.bounds.width;
  1036. below_righty = ctx.bounds.y;
  1037. comb = grub_unicode_get_comb (glyph_id);
  1038. for (i = 0; i < glyph_id->ncomb; i++)
  1039. {
  1040. grub_int16_t space = 0;
  1041. /* Center by default. */
  1042. grub_int16_t targetx;
  1043. if (!combining_glyphs[i])
  1044. continue;
  1045. targetx = (ctx.bounds.width - combining_glyphs[i]->width) / 2 + ctx.bounds.x;
  1046. /* CGJ is to avoid diacritics reordering. */
  1047. if (comb[i].code
  1048. == GRUB_UNICODE_COMBINING_GRAPHEME_JOINER)
  1049. continue;
  1050. switch (comb[i].type)
  1051. {
  1052. case GRUB_UNICODE_COMB_OVERLAY:
  1053. do_blit (combining_glyphs[i],
  1054. targetx,
  1055. (ctx.bounds.height - combining_glyphs[i]->height) / 2
  1056. - (ctx.bounds.height + ctx.bounds.y), &ctx);
  1057. if (min_devwidth < combining_glyphs[i]->width)
  1058. min_devwidth = combining_glyphs[i]->width;
  1059. break;
  1060. case GRUB_UNICODE_COMB_ATTACHED_ABOVE_RIGHT:
  1061. do_blit (combining_glyphs[i], above_rightx, -above_righty, &ctx);
  1062. above_rightx += combining_glyphs[i]->width;
  1063. break;
  1064. case GRUB_UNICODE_COMB_ABOVE_RIGHT:
  1065. do_blit (combining_glyphs[i], above_rightx,
  1066. -(above_righty + combining_glyphs[i]->height), &ctx);
  1067. above_rightx += combining_glyphs[i]->width;
  1068. break;
  1069. case GRUB_UNICODE_COMB_ABOVE_LEFT:
  1070. above_leftx -= combining_glyphs[i]->width;
  1071. do_blit (combining_glyphs[i], above_leftx,
  1072. -(above_lefty + combining_glyphs[i]->height), &ctx);
  1073. break;
  1074. case GRUB_UNICODE_COMB_BELOW_RIGHT:
  1075. do_blit (combining_glyphs[i], below_rightx, below_righty, &ctx);
  1076. below_rightx += combining_glyphs[i]->width;
  1077. break;
  1078. case GRUB_UNICODE_COMB_HEBREW_HOLAM:
  1079. if (glyph_id->base != GRUB_UNICODE_HEBREW_WAW)
  1080. targetx =
  1081. main_glyph->offset_x - combining_glyphs[i]->width -
  1082. (combining_glyphs[i]->width + 3) / 4;
  1083. goto above_on_main;
  1084. case GRUB_UNICODE_COMB_HEBREW_SIN_DOT:
  1085. targetx = main_glyph->offset_x + combining_glyphs[i]->width / 4;
  1086. goto above_on_main;
  1087. case GRUB_UNICODE_COMB_HEBREW_SHIN_DOT:
  1088. targetx =
  1089. main_glyph->width + main_glyph->offset_x -
  1090. combining_glyphs[i]->width;
  1091. above_on_main:
  1092. space = combining_glyphs[i]->offset_y
  1093. - grub_font_get_xheight (combining_glyphs[i]->font) - 1;
  1094. if (space <= 0)
  1095. space = 1 + (grub_font_get_xheight (main_glyph->font)) / 8;
  1096. do_blit (combining_glyphs[i], targetx,
  1097. -(main_glyph->height + main_glyph->offset_y + space
  1098. + combining_glyphs[i]->height), &ctx);
  1099. if (min_devwidth < combining_glyphs[i]->width)
  1100. min_devwidth = combining_glyphs[i]->width;
  1101. break;
  1102. /* TODO: Put dammah, fathah and alif nearer to shadda. */
  1103. case GRUB_UNICODE_COMB_SYRIAC_SUPERSCRIPT_ALAPH:
  1104. case GRUB_UNICODE_COMB_ARABIC_DAMMAH:
  1105. case GRUB_UNICODE_COMB_ARABIC_DAMMATAN:
  1106. case GRUB_UNICODE_COMB_ARABIC_FATHATAN:
  1107. case GRUB_UNICODE_COMB_ARABIC_FATHAH:
  1108. case GRUB_UNICODE_COMB_ARABIC_SUPERSCRIPT_ALIF:
  1109. case GRUB_UNICODE_COMB_ARABIC_SUKUN:
  1110. case GRUB_UNICODE_COMB_ARABIC_SHADDA:
  1111. case GRUB_UNICODE_COMB_HEBREW_RAFE:
  1112. case GRUB_UNICODE_STACK_ABOVE:
  1113. stacked_above:
  1114. space = combining_glyphs[i]->offset_y
  1115. - grub_font_get_xheight (combining_glyphs[i]->font) - 1;
  1116. if (space <= 0)
  1117. space = 1 + (grub_font_get_xheight (main_glyph->font)) / 8;
  1118. /* Fallthrough. */
  1119. case GRUB_UNICODE_STACK_ATTACHED_ABOVE:
  1120. do_blit (combining_glyphs[i], targetx,
  1121. -(ctx.bounds.height + ctx.bounds.y + space
  1122. + combining_glyphs[i]->height), &ctx);
  1123. if (min_devwidth < combining_glyphs[i]->width)
  1124. min_devwidth = combining_glyphs[i]->width;
  1125. break;
  1126. case GRUB_UNICODE_COMB_HEBREW_DAGESH:
  1127. do_blit (combining_glyphs[i], targetx,
  1128. -(ctx.bounds.height / 2 + ctx.bounds.y
  1129. + combining_glyphs[i]->height / 2), &ctx);
  1130. if (min_devwidth < combining_glyphs[i]->width)
  1131. min_devwidth = combining_glyphs[i]->width;
  1132. break;
  1133. case GRUB_UNICODE_COMB_HEBREW_SHEVA:
  1134. case GRUB_UNICODE_COMB_HEBREW_HIRIQ:
  1135. case GRUB_UNICODE_COMB_HEBREW_QAMATS:
  1136. case GRUB_UNICODE_COMB_HEBREW_TSERE:
  1137. case GRUB_UNICODE_COMB_HEBREW_SEGOL:
  1138. /* TODO: placement in final kaf and under reish. */
  1139. case GRUB_UNICODE_COMB_HEBREW_HATAF_SEGOL:
  1140. case GRUB_UNICODE_COMB_HEBREW_HATAF_PATAH:
  1141. case GRUB_UNICODE_COMB_HEBREW_HATAF_QAMATS:
  1142. case GRUB_UNICODE_COMB_HEBREW_PATAH:
  1143. case GRUB_UNICODE_COMB_HEBREW_QUBUTS:
  1144. case GRUB_UNICODE_COMB_HEBREW_METEG:
  1145. /* TODO: Put kasra and kasratan under shadda. */
  1146. case GRUB_UNICODE_COMB_ARABIC_KASRA:
  1147. case GRUB_UNICODE_COMB_ARABIC_KASRATAN:
  1148. /* I don't know how ypogegrammeni differs from subscript. */
  1149. case GRUB_UNICODE_COMB_YPOGEGRAMMENI:
  1150. case GRUB_UNICODE_STACK_BELOW:
  1151. stacked_below:
  1152. space = -(combining_glyphs[i]->offset_y
  1153. + combining_glyphs[i]->height);
  1154. if (space <= 0)
  1155. space = 1 + (grub_font_get_xheight (main_glyph->font)) / 8;
  1156. /* Fallthrough. */
  1157. case GRUB_UNICODE_STACK_ATTACHED_BELOW:
  1158. do_blit (combining_glyphs[i], targetx, -(ctx.bounds.y - space),
  1159. &ctx);
  1160. if (min_devwidth < combining_glyphs[i]->width)
  1161. min_devwidth = combining_glyphs[i]->width;
  1162. break;
  1163. case GRUB_UNICODE_COMB_MN:
  1164. switch (comb[i].code)
  1165. {
  1166. case GRUB_UNICODE_THAANA_ABAFILI:
  1167. case GRUB_UNICODE_THAANA_AABAAFILI:
  1168. case GRUB_UNICODE_THAANA_UBUFILI:
  1169. case GRUB_UNICODE_THAANA_OOBOOFILI:
  1170. case GRUB_UNICODE_THAANA_EBEFILI:
  1171. case GRUB_UNICODE_THAANA_EYBEYFILI:
  1172. case GRUB_UNICODE_THAANA_OBOFILI:
  1173. case GRUB_UNICODE_THAANA_OABOAFILI:
  1174. case GRUB_UNICODE_THAANA_SUKUN:
  1175. goto stacked_above;
  1176. case GRUB_UNICODE_THAANA_IBIFILI:
  1177. case GRUB_UNICODE_THAANA_EEBEEFILI:
  1178. goto stacked_below;
  1179. }
  1180. /* Fall through. */
  1181. default:
  1182. {
  1183. /* Default handling. Just draw combining character on top
  1184. of base character.
  1185. FIXME: support more unicode types correctly.
  1186. */
  1187. do_blit (combining_glyphs[i],
  1188. main_glyph->device_width
  1189. + combining_glyphs[i]->offset_x,
  1190. -(combining_glyphs[i]->height
  1191. + combining_glyphs[i]->offset_y), &ctx);
  1192. add_device_width (combining_glyphs[i]->device_width, &ctx);
  1193. }
  1194. }
  1195. }
  1196. add_device_width ((above_rightx >
  1197. below_rightx ? above_rightx : below_rightx) -
  1198. (main_glyph->offset_x + main_glyph->width), &ctx);
  1199. add_device_width (above_leftx - main_glyph->offset_x, &ctx);
  1200. if (glyph && glyph->device_width < min_devwidth)
  1201. glyph->device_width = min_devwidth;
  1202. if (device_width && *device_width < min_devwidth)
  1203. *device_width = min_devwidth;
  1204. if (bounds_out)
  1205. *bounds_out = ctx.bounds;
  1206. }
  1207. static struct grub_font_glyph *
  1208. grub_font_construct_dry_run (grub_font_t hinted_font,
  1209. const struct grub_unicode_glyph *glyph_id,
  1210. struct grub_video_signed_rect *bounds,
  1211. struct grub_font_glyph **combining_glyphs,
  1212. int *device_width)
  1213. {
  1214. struct grub_font_glyph *main_glyph = NULL;
  1215. grub_uint32_t desired_attributes = 0;
  1216. unsigned i;
  1217. grub_uint32_t base = glyph_id->base;
  1218. const struct grub_unicode_combining *comb;
  1219. if (glyph_id->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_RIGHT_JOINED)
  1220. desired_attributes |= GRUB_FONT_CODE_RIGHT_JOINED;
  1221. if (glyph_id->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_LEFT_JOINED)
  1222. desired_attributes |= GRUB_FONT_CODE_LEFT_JOINED;
  1223. comb = grub_unicode_get_comb (glyph_id);
  1224. if (base == 'i' || base == 'j')
  1225. {
  1226. for (i = 0; i < glyph_id->ncomb; i++)
  1227. if (comb[i].type == GRUB_UNICODE_STACK_ABOVE)
  1228. break;
  1229. if (i < glyph_id->ncomb && base == 'i')
  1230. base = GRUB_UNICODE_DOTLESS_LOWERCASE_I;
  1231. if (i < glyph_id->ncomb && base == 'j')
  1232. base = GRUB_UNICODE_DOTLESS_LOWERCASE_J;
  1233. }
  1234. main_glyph = grub_font_get_glyph_with_fallback (hinted_font, base
  1235. | desired_attributes);
  1236. if (!main_glyph)
  1237. main_glyph = grub_font_get_glyph_with_fallback (hinted_font,
  1238. base);
  1239. /* Glyph not available in any font. Use ASCII fallback. */
  1240. if (!main_glyph)
  1241. main_glyph = ascii_glyph_lookup (base);
  1242. /* Glyph not available in any font. Return unknown glyph. */
  1243. if (!main_glyph)
  1244. return NULL;
  1245. if (device_width)
  1246. *device_width = main_glyph->device_width;
  1247. if (!glyph_id->ncomb && !glyph_id->attributes)
  1248. return main_glyph;
  1249. if (glyph_id->ncomb && !combining_glyphs)
  1250. {
  1251. grub_errno = GRUB_ERR_NONE;
  1252. return main_glyph;
  1253. }
  1254. for (i = 0; i < glyph_id->ncomb; i++)
  1255. combining_glyphs[i]
  1256. = grub_font_get_glyph_with_fallback (main_glyph->font,
  1257. comb[i].code);
  1258. blit_comb (glyph_id, NULL, bounds, main_glyph, combining_glyphs,
  1259. device_width);
  1260. return main_glyph;
  1261. }
  1262. static struct grub_font_glyph **render_combining_glyphs = 0;
  1263. static grub_size_t render_max_comb_glyphs = 0;
  1264. static void
  1265. ensure_comb_space (const struct grub_unicode_glyph *glyph_id)
  1266. {
  1267. if (glyph_id->ncomb <= render_max_comb_glyphs)
  1268. return;
  1269. render_max_comb_glyphs = 2 * glyph_id->ncomb;
  1270. if (render_max_comb_glyphs < 8)
  1271. render_max_comb_glyphs = 8;
  1272. grub_free (render_combining_glyphs);
  1273. render_combining_glyphs = grub_malloc (render_max_comb_glyphs
  1274. * sizeof (render_combining_glyphs[0]));
  1275. if (!render_combining_glyphs)
  1276. grub_errno = 0;
  1277. }
  1278. int
  1279. grub_font_get_constructed_device_width (grub_font_t hinted_font,
  1280. const struct grub_unicode_glyph
  1281. *glyph_id)
  1282. {
  1283. int ret;
  1284. struct grub_font_glyph *main_glyph;
  1285. ensure_comb_space (glyph_id);
  1286. main_glyph = grub_font_construct_dry_run (hinted_font, glyph_id, NULL,
  1287. render_combining_glyphs, &ret);
  1288. if (!main_glyph)
  1289. return unknown_glyph->device_width;
  1290. return ret;
  1291. }
  1292. struct grub_font_glyph *
  1293. grub_font_construct_glyph (grub_font_t hinted_font,
  1294. const struct grub_unicode_glyph *glyph_id)
  1295. {
  1296. struct grub_font_glyph *main_glyph;
  1297. struct grub_video_signed_rect bounds;
  1298. static struct grub_font_glyph *glyph = 0;
  1299. static grub_size_t max_glyph_size = 0;
  1300. ensure_comb_space (glyph_id);
  1301. main_glyph = grub_font_construct_dry_run (hinted_font, glyph_id,
  1302. &bounds, render_combining_glyphs,
  1303. NULL);
  1304. if (!main_glyph)
  1305. return unknown_glyph;
  1306. if (!render_combining_glyphs && glyph_id->ncomb)
  1307. return main_glyph;
  1308. if (!glyph_id->ncomb && !glyph_id->attributes)
  1309. return main_glyph;
  1310. if (max_glyph_size < sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT)
  1311. {
  1312. grub_free (glyph);
  1313. max_glyph_size = (sizeof (*glyph) + (bounds.width * bounds.height + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT) * 2;
  1314. if (max_glyph_size < 8)
  1315. max_glyph_size = 8;
  1316. glyph = grub_malloc (max_glyph_size);
  1317. }
  1318. if (!glyph)
  1319. {
  1320. grub_errno = GRUB_ERR_NONE;
  1321. return main_glyph;
  1322. }
  1323. grub_memset (glyph, 0, sizeof (*glyph)
  1324. + (bounds.width * bounds.height
  1325. + GRUB_CHAR_BIT - 1) / GRUB_CHAR_BIT);
  1326. glyph->font = main_glyph->font;
  1327. glyph->width = bounds.width;
  1328. glyph->height = bounds.height;
  1329. glyph->offset_x = bounds.x;
  1330. glyph->offset_y = bounds.y;
  1331. if (glyph_id->attributes & GRUB_UNICODE_GLYPH_ATTRIBUTE_MIRROR)
  1332. grub_font_blit_glyph_mirror (glyph, main_glyph,
  1333. main_glyph->offset_x - glyph->offset_x,
  1334. (glyph->height + glyph->offset_y)
  1335. - (main_glyph->height +
  1336. main_glyph->offset_y));
  1337. else
  1338. grub_font_blit_glyph (glyph, main_glyph,
  1339. main_glyph->offset_x - glyph->offset_x,
  1340. (glyph->height + glyph->offset_y)
  1341. - (main_glyph->height + main_glyph->offset_y));
  1342. blit_comb (glyph_id, glyph, NULL, main_glyph, render_combining_glyphs, NULL);
  1343. return glyph;
  1344. }
  1345. /* Draw the specified glyph at (x, y). The y coordinate designates the
  1346. baseline of the character, while the x coordinate designates the left
  1347. side location of the character. */
  1348. grub_err_t
  1349. grub_font_draw_glyph (struct grub_font_glyph * glyph,
  1350. grub_video_color_t color, int left_x, int baseline_y)
  1351. {
  1352. struct grub_video_bitmap glyph_bitmap;
  1353. /* Don't try to draw empty glyphs (U+0020, etc.). */
  1354. if (glyph->width == 0 || glyph->height == 0)
  1355. return GRUB_ERR_NONE;
  1356. glyph_bitmap.mode_info.width = glyph->width;
  1357. glyph_bitmap.mode_info.height = glyph->height;
  1358. glyph_bitmap.mode_info.mode_type
  1359. = (1 << GRUB_VIDEO_MODE_TYPE_DEPTH_POS) | GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP;
  1360. glyph_bitmap.mode_info.blit_format = GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
  1361. glyph_bitmap.mode_info.bpp = 1;
  1362. /* Really 1 bit per pixel. */
  1363. glyph_bitmap.mode_info.bytes_per_pixel = 0;
  1364. /* Packed densely as bits. */
  1365. glyph_bitmap.mode_info.pitch = glyph->width;
  1366. glyph_bitmap.mode_info.number_of_colors = 2;
  1367. glyph_bitmap.mode_info.bg_red = 0;
  1368. glyph_bitmap.mode_info.bg_green = 0;
  1369. glyph_bitmap.mode_info.bg_blue = 0;
  1370. glyph_bitmap.mode_info.bg_alpha = 0;
  1371. grub_video_unmap_color (color,
  1372. &glyph_bitmap.mode_info.fg_red,
  1373. &glyph_bitmap.mode_info.fg_green,
  1374. &glyph_bitmap.mode_info.fg_blue,
  1375. &glyph_bitmap.mode_info.fg_alpha);
  1376. glyph_bitmap.data = glyph->bitmap;
  1377. int bitmap_left = left_x + glyph->offset_x;
  1378. int bitmap_bottom = baseline_y - glyph->offset_y;
  1379. int bitmap_top = bitmap_bottom - glyph->height;
  1380. return grub_video_blit_bitmap (&glyph_bitmap, GRUB_VIDEO_BLIT_BLEND,
  1381. bitmap_left, bitmap_top,
  1382. 0, 0, glyph->width, glyph->height);
  1383. }