font.c 46 KB

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