2009-06-08 Felix Zielcke <fzielcke@z-51.de>
[grub2/phcoder/solaris.git] / font / font.c
blob9d788a9f867ecc83dbb0737dd8a78d5d0c34f40c
1 /* font.c - Font API and font file loader. */
2 /*
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2005,2006,2007,2008,2009 Free Software Foundation, Inc.
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.
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.
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/>.
20 #include <grub/bufio.h>
21 #include <grub/dl.h>
22 #include <grub/file.h>
23 #include <grub/font.h>
24 #include <grub/misc.h>
25 #include <grub/mm.h>
26 #include <grub/types.h>
27 #include <grub/video.h>
28 #include <grub/bitmap.h>
30 #ifndef FONT_DEBUG
31 #define FONT_DEBUG 0
32 #endif
34 struct char_index_entry
36 grub_uint32_t code;
37 grub_uint8_t storage_flags;
38 grub_uint32_t offset;
40 /* Glyph if loaded, or NULL otherwise. */
41 struct grub_font_glyph *glyph;
44 #define FONT_WEIGHT_NORMAL 100
45 #define FONT_WEIGHT_BOLD 200
47 struct grub_font
49 char *name;
50 grub_file_t file;
51 char *family;
52 short point_size;
53 short weight;
54 short max_char_width;
55 short max_char_height;
56 short ascent;
57 short descent;
58 short leading;
59 grub_uint32_t num_chars;
60 struct char_index_entry *char_index;
63 /* Definition of font registry. */
64 struct grub_font_node *grub_font_list;
66 static int register_font (grub_font_t font);
67 static void font_init (grub_font_t font);
68 static void free_font (grub_font_t font);
69 static void remove_font (grub_font_t font);
71 struct font_file_section
73 /* The file this section is in. */
74 grub_file_t file;
76 /* FOURCC name of the section. */
77 char name[4];
79 /* Length of the section contents. */
80 grub_uint32_t length;
82 /* Set by open_section() on EOF. */
83 int eof;
86 /* Font file format constants. */
87 static const char pff2_magic[4] = { 'P', 'F', 'F', '2' };
88 static const char section_names_file[4] = { 'F', 'I', 'L', 'E' };
89 static const char section_names_font_name[4] = { 'N', 'A', 'M', 'E' };
90 static const char section_names_point_size[4] = { 'P', 'T', 'S', 'Z' };
91 static const char section_names_weight[4] = { 'W', 'E', 'I', 'G' };
92 static const char section_names_max_char_width[4] = { 'M', 'A', 'X', 'W' };
93 static const char section_names_max_char_height[4] = { 'M', 'A', 'X', 'H' };
94 static const char section_names_ascent[4] = { 'A', 'S', 'C', 'E' };
95 static const char section_names_descent[4] = { 'D', 'E', 'S', 'C' };
96 static const char section_names_char_index[4] = { 'C', 'H', 'I', 'X' };
97 static const char section_names_data[4] = { 'D', 'A', 'T', 'A' };
99 /* Replace unknown glyphs with a rounded question mark. */
100 static grub_uint8_t unknown_glyph_bitmap[] =
102 /* 76543210 */
103 0x7C, /* ooooo */
104 0x82, /* o o */
105 0xBA, /* o ooo o */
106 0xAA, /* o o o o */
107 0xAA, /* o o o o */
108 0x8A, /* o o o */
109 0x9A, /* o oo o */
110 0x92, /* o o o */
111 0x92, /* o o o */
112 0x92, /* o o o */
113 0x92, /* o o o */
114 0x82, /* o o */
115 0x92, /* o o o */
116 0x82, /* o o */
117 0x7C, /* ooooo */
118 0x00 /* */
121 /* The "unknown glyph" glyph, used as a last resort. */
122 static struct grub_font_glyph *unknown_glyph;
124 /* The font structure used when no other font is loaded. This functions
125 as a "Null Object" pattern, so that code everywhere does not have to
126 check for a NULL grub_font_t to avoid dereferencing a null pointer. */
127 static struct grub_font null_font;
129 /* Flag to ensure module is initialized only once. */
130 static grub_uint8_t font_loader_initialized;
132 void
133 grub_font_loader_init (void)
135 /* Only initialize font loader once. */
136 if (font_loader_initialized)
137 return;
139 /* Make glyph for unknown glyph. */
140 unknown_glyph = grub_malloc(sizeof(struct grub_font_glyph)
141 + sizeof(unknown_glyph_bitmap));
142 if (! unknown_glyph)
143 return;
145 unknown_glyph->width = 8;
146 unknown_glyph->height = 16;
147 unknown_glyph->offset_x = 0;
148 unknown_glyph->offset_y = -3;
149 unknown_glyph->device_width = 8;
150 grub_memcpy(unknown_glyph->bitmap,
151 unknown_glyph_bitmap, sizeof(unknown_glyph_bitmap));
153 /* Initialize the null font. */
154 font_init (&null_font);
155 null_font.name = "<No Font>";
156 null_font.ascent = unknown_glyph->height-3;
157 null_font.descent = 3;
158 null_font.max_char_width = unknown_glyph->width;
159 null_font.max_char_height = unknown_glyph->height;
161 font_loader_initialized = 1;
164 /* Initialize the font object with initial default values. */
165 static void
166 font_init (grub_font_t font)
168 font->name = 0;
169 font->file = 0;
170 font->family = 0;
171 font->point_size = 0;
172 font->weight = 0;
174 /* Default leading value, not in font file yet. */
175 font->leading = 1;
177 font->max_char_width = 0;
178 font->max_char_height = 0;
179 font->ascent = 0;
180 font->descent = 0;
181 font->num_chars = 0;
182 font->char_index = 0;
185 /* Open the next section in the file.
187 On success, the section name is stored in section->name and the length in
188 section->length, and 0 is returned. On failure, 1 is returned and
189 grub_errno is set appropriately with an error message.
191 If 1 is returned due to being at the end of the file, then section->eof is
192 set to 1; otherwise, section->eof is set to 0. */
193 static int
194 open_section (grub_file_t file, struct font_file_section *section)
196 grub_ssize_t retval;
197 grub_uint32_t raw_length;
199 section->file = file;
200 section->eof = 0;
202 /* Read the FOURCC section name. */
203 retval = grub_file_read (file, section->name, 4);
204 if (retval >= 0 && retval < 4)
206 /* EOF encountered. */
207 section->eof = 1;
208 return 1;
210 else if (retval < 0)
212 grub_error (GRUB_ERR_BAD_FONT,
213 "Font format error: can't read section name");
214 return 1;
217 /* Read the big-endian 32-bit section length. */
218 retval = grub_file_read (file, (char *) &raw_length, 4);
219 if (retval >= 0 && retval < 4)
221 /* EOF encountered. */
222 section->eof = 1;
223 return 1;
225 else if (retval < 0)
227 grub_error (GRUB_ERR_BAD_FONT,
228 "Font format error: can't read section length");
229 return 1;
232 /* Convert byte-order and store in *length. */
233 section->length = grub_be_to_cpu32 (raw_length);
235 return 0;
238 /* Size in bytes of each character index (CHIX section)
239 entry in the font file. */
240 #define FONT_CHAR_INDEX_ENTRY_SIZE (4 + 1 + 4)
242 /* Load the character index (CHIX) section contents from the font file. This
243 presumes that the position of FILE is positioned immediately after the
244 section length for the CHIX section (i.e., at the start of the section
245 contents). Returns 0 upon success, nonzero for failure (in which case
246 grub_errno is set appropriately). */
247 static int
248 load_font_index (grub_file_t file, grub_uint32_t sect_length, struct
249 grub_font *font)
251 unsigned i;
252 grub_uint32_t last_code;
254 #if FONT_DEBUG >= 2
255 grub_printf("load_font_index(sect_length=%d)\n", sect_length);
256 #endif
258 /* Sanity check: ensure section length is divisible by the entry size. */
259 if ((sect_length % FONT_CHAR_INDEX_ENTRY_SIZE) != 0)
261 grub_error (GRUB_ERR_BAD_FONT,
262 "Font file format error: character index length %d "
263 "is not a multiple of the entry size %d",
264 sect_length, FONT_CHAR_INDEX_ENTRY_SIZE);
265 return 1;
268 /* Calculate the number of characters. */
269 font->num_chars = sect_length / FONT_CHAR_INDEX_ENTRY_SIZE;
271 /* Allocate the character index array. */
272 font->char_index = grub_malloc (font->num_chars
273 * sizeof (struct char_index_entry));
274 if (! font->char_index)
275 return 1;
277 #if FONT_DEBUG >= 2
278 grub_printf("num_chars=%d)\n", font->num_chars);
279 #endif
281 last_code = 0;
283 /* Load the character index data from the file. */
284 for (i = 0; i < font->num_chars; i++)
286 struct char_index_entry *entry = &font->char_index[i];
288 /* Read code point value; convert to native byte order. */
289 if (grub_file_read (file, (char *) &entry->code, 4) != 4)
290 return 1;
291 entry->code = grub_be_to_cpu32 (entry->code);
293 /* Verify that characters are in ascending order. */
294 if (i != 0 && entry->code <= last_code)
296 grub_error (GRUB_ERR_BAD_FONT,
297 "Font characters not in ascending order: %u <= %u",
298 entry->code, last_code);
299 return 1;
302 last_code = entry->code;
304 /* Read storage flags byte. */
305 if (grub_file_read (file, (char *) &entry->storage_flags, 1) != 1)
306 return 1;
308 /* Read glyph data offset; convert to native byte order. */
309 if (grub_file_read (file, (char *) &entry->offset, 4) != 4)
310 return 1;
311 entry->offset = grub_be_to_cpu32 (entry->offset);
313 /* No glyph loaded. Will be loaded on demand and cached thereafter. */
314 entry->glyph = 0;
316 #if FONT_DEBUG >= 5
317 /* Print the 1st 10 characters. */
318 if (i < 10)
319 grub_printf("c=%d o=%d\n", entry->code, entry->offset);
320 #endif
323 return 0;
326 /* Read the contents of the specified section as a string, which is
327 allocated on the heap. Returns 0 if there is an error. */
328 static char *
329 read_section_as_string (struct font_file_section *section)
331 char *str;
332 grub_ssize_t ret;
334 str = grub_malloc (section->length + 1);
335 if (! str)
336 return 0;
338 ret = grub_file_read (section->file, str, section->length);
339 if (ret < 0 || ret != (grub_ssize_t) section->length)
341 grub_free (str);
342 return 0;
345 str[section->length] = '\0';
346 return str;
349 /* Read the contents of the current section as a 16-bit integer value,
350 which is stored into *VALUE.
351 Returns 0 upon success, nonzero upon failure. */
352 static int
353 read_section_as_short (struct font_file_section *section, grub_int16_t *value)
355 grub_uint16_t raw_value;
357 if (section->length != 2)
359 grub_error (GRUB_ERR_BAD_FONT,
360 "Font file format error: section %c%c%c%c length "
361 "is %d but should be 2",
362 section->name[0], section->name[1],
363 section->name[2], section->name[3],
364 section->length);
365 return 1;
367 if (grub_file_read (section->file, (char *) &raw_value, 2) != 2)
368 return 1;
370 *value = grub_be_to_cpu16 (raw_value);
371 return 0;
374 /* Load a font and add it to the beginning of the global font list.
375 Returns 0 upon success, nonzero upon failure. */
377 grub_font_load (const char *filename)
379 grub_file_t file = 0;
380 struct font_file_section section;
381 char magic[4];
382 grub_font_t font = 0;
384 #if FONT_DEBUG >= 1
385 grub_printf("add_font(%s)\n", filename);
386 #endif
388 file = grub_buffile_open (filename, 1024);
389 if (!file)
390 goto fail;
392 #if FONT_DEBUG >= 3
393 grub_printf("file opened\n");
394 #endif
396 /* Read the FILE section. It indicates the file format. */
397 if (open_section (file, &section) != 0)
398 goto fail;
400 #if FONT_DEBUG >= 3
401 grub_printf("opened FILE section\n");
402 #endif
403 if (grub_memcmp (section.name, section_names_file, 4) != 0)
405 grub_error (GRUB_ERR_BAD_FONT,
406 "Font file format error: 1st section must be FILE");
407 goto fail;
410 #if FONT_DEBUG >= 3
411 grub_printf("section name ok\n");
412 #endif
413 if (section.length != 4)
415 grub_error (GRUB_ERR_BAD_FONT,
416 "Font file format error (file type ID length is %d "
417 "but should be 4)", section.length);
418 goto fail;
421 #if FONT_DEBUG >= 3
422 grub_printf("section length ok\n");
423 #endif
424 /* Check the file format type code. */
425 if (grub_file_read (file, magic, 4) != 4)
426 goto fail;
428 #if FONT_DEBUG >= 3
429 grub_printf("read magic ok\n");
430 #endif
432 if (grub_memcmp (magic, pff2_magic, 4) != 0)
434 grub_error (GRUB_ERR_BAD_FONT, "Invalid font magic %x %x %x %x",
435 magic[0], magic[1], magic[2], magic[3]);
436 goto fail;
439 #if FONT_DEBUG >= 3
440 grub_printf("compare magic ok\n");
441 #endif
443 /* Allocate the font object. */
444 font = (grub_font_t) grub_malloc (sizeof (struct grub_font));
445 if (! font)
446 goto fail;
448 font_init (font);
449 font->file = file;
451 #if FONT_DEBUG >= 3
452 grub_printf("allocate font ok; loading font info\n");
453 #endif
455 /* Load the font information. */
456 while (1)
458 if (open_section (file, &section) != 0)
460 if (section.eof)
461 break; /* Done reading the font file. */
462 else
463 goto fail;
466 #if FONT_DEBUG >= 2
467 grub_printf("opened section %c%c%c%c ok\n",
468 section.name[0], section.name[1],
469 section.name[2], section.name[3]);
470 #endif
472 if (grub_memcmp (section.name, section_names_font_name, 4) == 0)
474 font->name = read_section_as_string (&section);
475 if (!font->name)
476 goto fail;
478 else if (grub_memcmp (section.name, section_names_point_size, 4) == 0)
480 if (read_section_as_short (&section, &font->point_size) != 0)
481 goto fail;
483 else if (grub_memcmp (section.name, section_names_weight, 4) == 0)
485 char *wt;
486 wt = read_section_as_string (&section);
487 if (!wt)
488 continue;
489 /* Convert the weight string 'normal' or 'bold' into a number. */
490 if (grub_strcmp (wt, "normal") == 0)
491 font->weight = FONT_WEIGHT_NORMAL;
492 else if (grub_strcmp (wt, "bold") == 0)
493 font->weight = FONT_WEIGHT_BOLD;
494 grub_free (wt);
496 else if (grub_memcmp (section.name, section_names_max_char_width, 4) == 0)
498 if (read_section_as_short (&section, &font->max_char_width) != 0)
499 goto fail;
501 else if (grub_memcmp (section.name, section_names_max_char_height, 4) == 0)
503 if (read_section_as_short (&section, &font->max_char_height) != 0)
504 goto fail;
506 else if (grub_memcmp (section.name, section_names_ascent, 4) == 0)
508 if (read_section_as_short (&section, &font->ascent) != 0)
509 goto fail;
511 else if (grub_memcmp (section.name, section_names_descent, 4) == 0)
513 if (read_section_as_short (&section, &font->descent) != 0)
514 goto fail;
516 else if (grub_memcmp (section.name, section_names_char_index, 4) == 0)
518 if (load_font_index (file, section.length, font) != 0)
519 goto fail;
521 else if (grub_memcmp (section.name, section_names_data, 4) == 0)
523 /* When the DATA section marker is reached, we stop reading. */
524 break;
526 else
528 /* Unhandled section type, simply skip past it. */
529 #if FONT_DEBUG >= 3
530 grub_printf("Unhandled section type, skipping.\n");
531 #endif
532 grub_off_t section_end = grub_file_tell (file) + section.length;
533 if ((int) grub_file_seek (file, section_end) == -1)
534 goto fail;
538 if (! font->name)
540 grub_printf ("Note: Font has no name.\n");
541 font->name = grub_strdup ("Unknown");
544 #if FONT_DEBUG >= 1
545 grub_printf ("Loaded font `%s'.\n"
546 "Ascent=%d Descent=%d MaxW=%d MaxH=%d Number of characters=%d.\n",
547 font->name,
548 font->ascent, font->descent,
549 font->max_char_width, font->max_char_height,
550 font->num_chars);
551 #endif
553 if (font->max_char_width == 0
554 || font->max_char_height == 0
555 || font->num_chars == 0
556 || font->char_index == 0
557 || font->ascent == 0
558 || font->descent == 0)
560 grub_error (GRUB_ERR_BAD_FONT,
561 "Invalid font file: missing some required data.");
562 goto fail;
565 /* Add the font to the global font registry. */
566 if (register_font (font) != 0)
567 goto fail;
569 return 0;
571 fail:
572 free_font (font);
573 return 1;
576 /* Read a 16-bit big-endian integer from FILE, convert it to native byte
577 order, and store it in *VALUE.
578 Returns 0 on success, 1 on failure. */
579 static int
580 read_be_uint16 (grub_file_t file, grub_uint16_t * value)
582 if (grub_file_read (file, (char *) value, 2) != 2)
583 return 1;
584 *value = grub_be_to_cpu16 (*value);
585 return 0;
588 static int
589 read_be_int16 (grub_file_t file, grub_int16_t * value)
591 /* For the signed integer version, use the same code as for unsigned. */
592 return read_be_uint16 (file, (grub_uint16_t *) value);
595 /* Return a pointer to the character index entry for the glyph corresponding to
596 the codepoint CODE in the font FONT. If not found, return zero. */
597 static struct char_index_entry *
598 find_glyph (const grub_font_t font, grub_uint32_t code)
600 struct char_index_entry *table;
601 grub_size_t lo;
602 grub_size_t hi;
603 grub_size_t mid;
605 /* Do a binary search in `char_index', which is ordered by code point. */
606 table = font->char_index;
607 lo = 0;
608 hi = font->num_chars - 1;
610 while (lo <= hi)
612 mid = lo + (hi - lo) / 2;
613 if (code < table[mid].code)
614 hi = mid - 1;
615 else if (code > table[mid].code)
616 lo = mid + 1;
617 else
618 return &table[mid];
621 return 0;
624 /* Get a glyph for the Unicode character CODE in FONT. The glyph is loaded
625 from the font file if has not been loaded yet.
626 Returns a pointer to the glyph if found, or 0 if it is not found. */
627 static struct grub_font_glyph *
628 grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code)
630 struct char_index_entry *index_entry;
632 index_entry = find_glyph (font, code);
633 if (index_entry)
635 struct grub_font_glyph *glyph = 0;
636 grub_uint16_t width;
637 grub_uint16_t height;
638 grub_int16_t xoff;
639 grub_int16_t yoff;
640 grub_int16_t dwidth;
641 int len;
643 if (index_entry->glyph)
644 /* Return cached glyph. */
645 return index_entry->glyph;
647 if (! font->file)
648 /* No open file, can't load any glyphs. */
649 return 0;
651 /* Make sure we can find glyphs for error messages. Push active
652 error message to error stack and reset error message. */
653 grub_error_push ();
655 grub_file_seek (font->file, index_entry->offset);
657 /* Read the glyph width, height, and baseline. */
658 if (read_be_uint16(font->file, &width) != 0
659 || read_be_uint16(font->file, &height) != 0
660 || read_be_int16(font->file, &xoff) != 0
661 || read_be_int16(font->file, &yoff) != 0
662 || read_be_int16(font->file, &dwidth) != 0)
664 remove_font (font);
665 return 0;
668 len = (width * height + 7) / 8;
669 glyph = grub_malloc (sizeof (struct grub_font_glyph) + len);
670 if (! glyph)
672 remove_font (font);
673 return 0;
676 glyph->font = font;
677 glyph->width = width;
678 glyph->height = height;
679 glyph->offset_x = xoff;
680 glyph->offset_y = yoff;
681 glyph->device_width = dwidth;
683 /* Don't try to read empty bitmaps (e.g., space characters). */
684 if (len != 0)
686 if (grub_file_read (font->file, (char *) glyph->bitmap, len) != len)
688 remove_font (font);
689 return 0;
693 /* Restore old error message. */
694 grub_error_pop ();
696 /* Cache the glyph. */
697 index_entry->glyph = glyph;
699 return glyph;
702 return 0;
705 /* Free the memory used by FONT.
706 This should not be called if the font has been made available to
707 users (once it is added to the global font list), since there would
708 be the possibility of a dangling pointer. */
709 static void
710 free_font (grub_font_t font)
712 if (font)
714 if (font->file)
715 grub_file_close (font->file);
716 grub_free (font->name);
717 grub_free (font->family);
718 grub_free (font->char_index);
719 grub_free (font);
723 /* Add FONT to the global font registry.
724 Returns 0 upon success, nonzero on failure
725 (the font was not registered). */
726 static int
727 register_font (grub_font_t font)
729 struct grub_font_node *node = 0;
731 node = grub_malloc (sizeof (struct grub_font_node));
732 if (! node)
733 return 1;
735 node->value = font;
736 node->next = grub_font_list;
737 grub_font_list = node;
739 return 0;
742 /* Remove the font from the global font list. We don't actually free the
743 font's memory since users could be holding references to the font. */
744 static void
745 remove_font (grub_font_t font)
747 struct grub_font_node **nextp, *cur;
749 for (nextp = &grub_font_list, cur = *nextp;
750 cur;
751 nextp = &cur->next, cur = cur->next)
753 if (cur->value == font)
755 *nextp = cur->next;
757 /* Free the node, but not the font itself. */
758 grub_free (cur);
760 return;
765 /* Get a font from the list of loaded fonts. This function will return
766 another font if the requested font is not available. If no fonts are
767 loaded, then a special 'null font' is returned, which contains no glyphs,
768 but is not a null pointer so the caller may omit checks for NULL. */
769 grub_font_t
770 grub_font_get (const char *font_name)
772 struct grub_font_node *node;
774 for (node = grub_font_list; node; node = node->next)
776 grub_font_t font = node->value;
777 if (grub_strcmp (font->name, font_name) == 0)
778 return font;
781 /* If no font by that name is found, return the first font in the list
782 as a fallback. */
783 if (grub_font_list && grub_font_list->value)
784 return grub_font_list->value;
785 else
786 /* The null_font is a last resort. */
787 return &null_font;
790 /* Get the full name of the font. For instance, "Helvetica Bold 12". */
791 const char *
792 grub_font_get_name (grub_font_t font)
794 return font->name;
797 /* Get the maximum width of any character in the font in pixels. */
799 grub_font_get_max_char_width (grub_font_t font)
801 return font->max_char_width;
804 /* Get the maximum height of any character in the font in pixels. */
806 grub_font_get_max_char_height (grub_font_t font)
808 return font->max_char_height;
811 /* Get the distance in pixels from the top of characters to the baseline. */
813 grub_font_get_ascent (grub_font_t font)
815 return font->ascent;
818 /* Get the distance in pixels from the baseline to the lowest descenders
819 (for instance, in a lowercase 'y', 'g', etc.). */
821 grub_font_get_descent (grub_font_t font)
823 return font->descent;
826 /* Get the *standard leading* of the font in pixel, which is the spacing
827 between two lines of text. Specifically, it is the space between the
828 descent of one line and the ascent of the next line. This is included
829 in the *height* metric. */
831 grub_font_get_leading (grub_font_t font)
833 return font->leading;
836 /* Get the distance in pixels between baselines of adjacent lines of text. */
838 grub_font_get_height (grub_font_t font)
840 return font->ascent + font->descent + font->leading;
843 /* Get the width in pixels of the specified UTF-8 string, when rendered in
844 in the specified font (but falling back on other fonts for glyphs that
845 are missing). */
847 grub_font_get_string_width (grub_font_t font, const char *str)
849 int width;
850 struct grub_font_glyph *glyph;
851 grub_uint32_t code;
852 const grub_uint8_t *ptr;
854 for (ptr = (const grub_uint8_t *) str, width = 0;
855 grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0; )
857 glyph = grub_font_get_glyph_with_fallback (font, code);
858 width += glyph->device_width;
861 return width;
864 /* Get the glyph for FONT corresponding to the Unicode code point CODE.
865 Returns a pointer to an glyph indicating there is no glyph available
866 if CODE does not exist in the font. The glyphs are cached once loaded. */
867 struct grub_font_glyph *
868 grub_font_get_glyph (grub_font_t font, grub_uint32_t code)
870 struct grub_font_glyph *glyph;
871 glyph = grub_font_get_glyph_internal (font, code);
872 if (glyph == 0)
873 glyph = unknown_glyph;
874 return glyph;
878 /* Calculate a subject value representing "how similar" two fonts are.
879 This is used to prioritize the order that fonts are scanned for missing
880 glyphs. The object is to select glyphs from the most similar font
881 possible, for the best appearance.
882 The heuristic is crude, but it helps greatly when fonts of similar
883 sizes are used so that tiny 8 point glyphs are not mixed into a string
884 of 24 point text unless there is no other choice. */
885 static int
886 get_font_diversity(grub_font_t a, grub_font_t b)
888 int d;
890 d = 0;
892 if (a->ascent && b->ascent)
893 d += grub_abs (a->ascent - b->ascent) * 8;
894 else
895 /* Penalty for missing attributes. */
896 d += 50;
898 if (a->max_char_height && b->max_char_height)
899 d += grub_abs (a->max_char_height - b->max_char_height) * 8;
900 else
901 /* Penalty for missing attributes. */
902 d += 50;
904 /* Weight is a minor factor. */
905 d += (a->weight != b->weight) ? 5 : 0;
907 return d;
910 /* Get a glyph corresponding to the codepoint CODE. If FONT contains the
911 specified glyph, then it is returned. Otherwise, all other loaded fonts
912 are searched until one is found that contains a glyph for CODE.
913 If no glyph is available for CODE in the loaded fonts, then a glyph
914 representing an unknown character is returned.
915 This function never returns NULL.
916 The returned glyph is owned by the font manager and should not be freed
917 by the caller. The glyphs are cached. */
918 struct grub_font_glyph *
919 grub_font_get_glyph_with_fallback (grub_font_t font, grub_uint32_t code)
921 struct grub_font_glyph *glyph;
922 struct grub_font_node *node;
923 /* Keep track of next node, in case there's an I/O error in
924 grub_font_get_glyph_internal() and the font is removed from the list. */
925 struct grub_font_node *next;
926 /* Information on the best glyph found so far, to help find the glyph in
927 the best matching to the requested one. */
928 int best_diversity;
929 struct grub_font_glyph *best_glyph;
931 if (font)
933 /* First try to get the glyph from the specified font. */
934 glyph = grub_font_get_glyph_internal (font, code);
935 if (glyph)
936 return glyph;
939 /* Otherwise, search all loaded fonts for the glyph and use the one from
940 the font that best matches the requested font. */
941 best_diversity = 10000;
942 best_glyph = 0;
944 for (node = grub_font_list; node; node = next)
946 grub_font_t curfont;
948 curfont = node->value;
949 next = node->next;
951 glyph = grub_font_get_glyph_internal (curfont, code);
952 if (glyph)
954 int d;
956 d = get_font_diversity (curfont, font);
957 if (d < best_diversity)
959 best_diversity = d;
960 best_glyph = glyph;
965 if (best_glyph)
966 return best_glyph;
967 else
968 /* Glyph not available in any font. Return unknown glyph. */
969 return unknown_glyph;
973 /* Draw the specified glyph at (x, y). The y coordinate designates the
974 baseline of the character, while the x coordinate designates the left
975 side location of the character. */
976 grub_err_t
977 grub_font_draw_glyph (struct grub_font_glyph *glyph,
978 grub_video_color_t color,
979 int left_x, int baseline_y)
981 struct grub_video_bitmap glyph_bitmap;
983 /* Don't try to draw empty glyphs (U+0020, etc.). */
984 if (glyph->width == 0 || glyph->height == 0)
985 return GRUB_ERR_NONE;
987 glyph_bitmap.mode_info.width = glyph->width;
988 glyph_bitmap.mode_info.height = glyph->height;
989 glyph_bitmap.mode_info.mode_type =
990 (1 << GRUB_VIDEO_MODE_TYPE_DEPTH_POS)
991 | GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP;
992 glyph_bitmap.mode_info.blit_format = GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED;
993 glyph_bitmap.mode_info.bpp = 1;
995 /* Really 1 bit per pixel. */
996 glyph_bitmap.mode_info.bytes_per_pixel = 0;
998 /* Packed densely as bits. */
999 glyph_bitmap.mode_info.pitch = glyph->width;
1001 glyph_bitmap.mode_info.number_of_colors = 2;
1002 glyph_bitmap.mode_info.bg_red = 0;
1003 glyph_bitmap.mode_info.bg_green = 0;
1004 glyph_bitmap.mode_info.bg_blue = 0;
1005 glyph_bitmap.mode_info.bg_alpha = 0;
1006 grub_video_unmap_color(color,
1007 &glyph_bitmap.mode_info.fg_red,
1008 &glyph_bitmap.mode_info.fg_green,
1009 &glyph_bitmap.mode_info.fg_blue,
1010 &glyph_bitmap.mode_info.fg_alpha);
1011 glyph_bitmap.data = glyph->bitmap;
1013 int bitmap_left = left_x + glyph->offset_x;
1014 int bitmap_bottom = baseline_y - glyph->offset_y;
1015 int bitmap_top = bitmap_bottom - glyph->height;
1017 return grub_video_blit_bitmap (&glyph_bitmap, GRUB_VIDEO_BLIT_BLEND,
1018 bitmap_left, bitmap_top,
1019 0, 0,
1020 glyph->width, glyph->height);
1023 /* Draw a UTF-8 string of text on the current video render target.
1024 The x coordinate specifies the starting x position for the first character,
1025 while the y coordinate specifies the baseline position.
1026 If the string contains a character that FONT does not contain, then
1027 a glyph from another loaded font may be used instead. */
1028 grub_err_t
1029 grub_font_draw_string (const char *str, grub_font_t font,
1030 grub_video_color_t color,
1031 int left_x, int baseline_y)
1033 int x;
1034 struct grub_font_glyph *glyph;
1035 grub_uint32_t code;
1036 const grub_uint8_t *ptr;
1038 for (ptr = (const grub_uint8_t *) str, x = left_x;
1039 grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0; )
1041 glyph = grub_font_get_glyph_with_fallback (font, code);
1042 if (grub_font_draw_glyph (glyph, color, x, baseline_y)
1043 != GRUB_ERR_NONE)
1044 return grub_errno;
1045 x += glyph->device_width;
1048 return GRUB_ERR_NONE;