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