From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1KvlAI-0006Kd-7L for mharc-grub-devel@gnu.org; Thu, 30 Oct 2008 23:59:14 -0400 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1KvlAG-0006JB-5F for grub-devel@gnu.org; Thu, 30 Oct 2008 23:59:12 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1KvlAE-0006Hg-RD for grub-devel@gnu.org; Thu, 30 Oct 2008 23:59:11 -0400 Received: from [199.232.76.173] (port=42052 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1KvlAE-0006HR-KB for grub-devel@gnu.org; Thu, 30 Oct 2008 23:59:10 -0400 Received: from gateway16.websitewelcome.com ([69.56.148.23]:50060) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1KvlAD-0003Lm-Dk for grub-devel@gnu.org; Thu, 30 Oct 2008 23:59:10 -0400 Received: (qmail 4055 invoked from network); 31 Oct 2008 04:10:56 -0000 Received: from gator297.hostgator.com (74.53.228.114) by gateway16.websitewelcome.com with SMTP; 31 Oct 2008 04:10:56 -0000 Received: from c-67-185-177-95.hsd1.wa.comcast.net ([67.185.177.95]:60067 helo=localhost) by gator297.hostgator.com with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.68) (envelope-from ) id 1KvlA1-00086i-HQ; Thu, 30 Oct 2008 22:58:57 -0500 Date: Thu, 30 Oct 2008 20:57:36 -0700 From: Colin D Bennett To: grub-devel@gnu.org, Vesa =?UTF-8?Q?J=C3=A4=C3=A4skel=C3=A4inen?= Message-ID: <20081030205736.7e080a5e@gibibit.com> In-Reply-To: <20081030121106.4efffecc@gibibit.com> References: <20080901092753.3918cf73@gamma.lan> <20081004214640.5ab21f53@gibibit.com> <48E87FB9.8070603@nic.fi> <20081030121106.4efffecc@gibibit.com> X-Mailer: Claws Mail 3.6.1 (GTK+ 2.14.4; i686-pc-linux-gnu) Mime-Version: 1.0 Content-Type: multipart/signed; boundary="Sig_/5JJPp8f2UUZ.fQ3Lqt3ogW+"; protocol="application/pgp-signature"; micalg=PGP-SHA1 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - gator297.hostgator.com X-AntiAbuse: Original Domain - gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - gibibit.com X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Cc: Subject: Re: [PATCH] GSoC #10 new font engine (UTF-8 support+bugfix) X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 31 Oct 2008 03:59:12 -0000 --Sig_/5JJPp8f2UUZ.fQ3Lqt3ogW+ Content-Type: multipart/mixed; boundary="MP_/gl5hFA.k5o19toXYw/TFnHO" --MP_/gl5hFA.k5o19toXYw/TFnHO Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Update:=20 I fixed an error pointed out to me by Y.Volta:=20 In grub_font_get(), if no fonts are loaded, a null pointer is dereferenced. This is fixed in the attached patch. The grub_font_get() function now returns a dummy font object (a statically allocated font object with no characters) so that callers of grub_font_get() can be assured that the return value will never be NULL. If no fonts are loaded, then the "unknown glyph" will be used for all characters, but it will be safe. Regards, Colin --MP_/gl5hFA.k5o19toXYw/TFnHO Content-Type: text/x-patch; name=10_font-engine-utf8_2008-10-30.patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=10_font-engine-utf8_2008-10-30.patch 2008-10-30 Colin D Bennett New font engine supporting large and proportional fonts, PFF2 file format. * commands/videotest.c (grub_cmd_videotest): Use new font API, test drawing UTF-8 Unicode text. * conf/common.rmk (font_mod_SOURCES): Update source file list. * font/font.c: New file. * font/font_cmd.c: New file. * font/manager.c: Deleted. * include/grub/font.h: Update copyright years. (GRUB_FONT_MAGIC): Removed. (grub_font): New forward declaration of struct. (grub_font_t): New typedef. (grub_font_node): New struct. (grub_font_list): New global symbol. (grub_font_glyph): Updated data structure to support larger, variable size fonts and to support font metrics. (grub_font_glyph_t): Removed. (grub_font_loader_init): New prototype. (grub_font_load): New prototype. (grub_font_get): New prototype. (grub_font_get_name): New prototype. (grub_font_get_max_char_width): New prototype. (grub_font_get_max_char_height): New prototype. (grub_font_get_ascent): New prototype. (grub_font_get_descent): New prototype. (grub_font_get_leading): New prototype. (grub_font_get_height): New prototype. (grub_font_get_string_width): New prototype. (grub_font_load): New prototype. (grub_font_get_glyph): New prototype. (grub_font_get_glyph_with_fallback): New prototype. (grub_font_draw_glyph): New prototype. (grub_font_draw_string): New prototype. * include/grub/misc.h (grub_utf8_to_ucs4): Added parameters. * include/grub/video.h (grub_font_glyph): Removed forward struct declaration. (GRUB_VIDEO_MODE_TYPE_ALPHA): Changed bit mask value to make space for 1BIT_BITMAP. (GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED): Likewise. (GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP): New bit mask constant. (GRUB_VIDEO_MODE_TYPE_COLOR_MASK): Updated to a wider mask to make space for 1BIT_BITMAP. (GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED): New blit format enum value. (grub_video_mode_info): Added bg_red, bg_green, bg_blue, bg_alpha, fg_red, fg_green, fg_blue, and fg_alpha fields. * kern/misc.c (grub_utf8_to_ucs4): Support null-terminated UTF-8 strings and allow limiting the number of Unicode characters decoded. * kern/term.c (grub_putchar): Pass additional arguments to grub_utf8_to_ucs4. * normal/menu.c (print_entry): Pass destination maximum length to grub_utf8_to_ucs4. * term/gfxterm.c (DEFAULT_CHAR_WIDTH): Removed. (DEFAULT_CHAR_HEIGHT): Removed. (grub_virtual_screen): Added font field. (grub_virtual_screen_setup): Add font_name parameter. Initialize terminal font information. (grub_gfxterm_init): Get font name from gfxterm_font environment variable. Pass font name to grub_virtual_screen_setup. (write_char): Place glyphs using their baseline, based on the font ascent. (write_cursor): Draw cursor using font ascent as a reference. (grub_gfxterm_putchar): Use new font API. Breaks bi-width font support. (grub_gfxterm_getcharwidth): Assume all characters are 1 character in width. Breaks bi-width font support. (grub_vga_getcharwidth): Always return a width of 1. * term/i386/pc/vesafb.c (grub_virtual_screen_get_glyph): Use new font API. Breaks vesafb glyph drawing, since it copied the glyph data directly, but now glyphs use a different storage format. This code shouldn't compile, but GRUB builds ok! * term/i386/pc/vga.c (font): New static variable. (grub_vga_mod_init): Get a font using the new font API. (write_char): Broken. Commented out code with #if 0 since it will need to be rewritten to handle the new glyph storage format if we want to maintain the vga terminal. (grub_vga_putchar): Update to use new font API; breaks support for bi-width fonts. * video/i386/pc/vbe.c (grub_video_vbe_map_rgb): Support GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP. (grub_video_vbe_map_rgba): Likewise. (grub_video_vbe_unmap_color_int): Likewise. (grub_video_vbe_blit_glyph): Deleted. (grub_video_vbe_adapter): Deleted blit_glyph member. * video/i386/pc/vbeutil.c (get_data_ptr): Add a comment to the switch statement indicating why it is unimplemented for 1-bit bitmaps. (get_pixel): Support 1-bit bitmaps. (set_pixel): Support 1-bit bitmaps. =09 * video/video.c (grub_video_blit_glyph): Renamed coordinate arguments for clarity. (grub_video_draw_string): New function. =3D=3D=3D modified file 'commands/videotest.c' --- commands/videotest.c 2007-07-21 22:32:33 +0000 +++ commands/videotest.c 2008-10-31 03:46:58 +0000 @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2006,2007 Free Software Foundation, Inc. + * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,17 +36,21 @@ GRUB_VIDEO_MODE_TYPE_INDEX_COLOR) !=3D GRUB_ERR_NO= NE) return grub_errno; =20 - grub_getkey (); - grub_video_color_t color; unsigned int x; unsigned int y; unsigned int width; unsigned int height; int i; - struct grub_font_glyph glyph; + grub_font_t sansbig; + grub_font_t sans; + grub_font_t sanssmall; + grub_font_t fixed; + struct grub_font_glyph *glyph; struct grub_video_render_target *text_layer; grub_video_color_t palette[16]; + const char *str; + int texty; =20 grub_video_get_viewport (&x, &y, &width, &height); =20 @@ -65,8 +69,15 @@ color =3D grub_video_map_rgb (0, 255, 255); grub_video_fill_rect (color, 100, 100, 100, 100); =20 - grub_font_get_glyph ('*', &glyph); =20 - grub_video_blit_glyph (&glyph, color, 200 ,0); + sansbig =3D grub_font_get ("Helvetica Bold 24"); + sans =3D grub_font_get ("Helvetica Bold 14"); + sanssmall =3D grub_font_get ("Helvetica 8"); + fixed =3D grub_font_get ("Fixed 20"); + if (! sansbig || ! sans || ! sanssmall || ! fixed) + return grub_error (GRUB_ERR_BAD_FONT, "No font loaded."); + + glyph =3D grub_font_get_glyph (fixed, '*'); + grub_font_draw_glyph (glyph, color, 200 ,0); =20 grub_video_set_viewport (x + 150, y + 150, width - 150 * 2, height - 150 * 2); @@ -77,18 +88,69 @@ =20 color =3D grub_video_map_rgb (255, 255, 255); =20 - grub_font_get_glyph ('A', &glyph); - grub_video_blit_glyph (&glyph, color, 16, 16); - grub_font_get_glyph ('B', &glyph); - grub_video_blit_glyph (&glyph, color, 16 * 2, 16); - - grub_font_get_glyph ('*', &glyph); + texty =3D 32; + grub_font_draw_string ("The quick brown fox jumped over the lazy dog.", + sans, color, 16, texty); + texty +=3D grub_font_get_descent (sans) + grub_font_get_leading (sans); + + texty +=3D grub_font_get_ascent (fixed); + grub_font_draw_string ("The quick brown fox jumped over the lazy dog.", + fixed, color, 16, texty); + texty +=3D grub_font_get_descent (fixed) + grub_font_get_leading (fixed); + + /* To convert Unicode characters into UTF-8 for this test, the following + command is useful: + echo -ne '\x00\x00\x26\x3A' | iconv -f UTF-32BE -t UTF-8 | od -t x1 + This converts the Unicode character U+263A to UTF-8. */ + + /* Characters used: + Code point Description UTF-8 encoding + ----------- ------------------------------ -------------- + U+263A unfilled smiley face E2 98 BA + U+00A1 inverted exclamation point C2 A1 + U+00A3 British pound currency symbol C2 A3 + U+03C4 Greek tau CF 84 + U+00E4 lowercase letter a with umlaut C3 A4 + U+2124 set 'Z' symbol (integers) E2 84 A4 + U+2287 subset symbol E2 8A 87 + U+211D set 'R' symbol (real numbers) E2 84 9D */ + + str =3D + "Unicode test: happy\xE2\x98\xBA \xC2\xA3 5.00" + " \xC2\xA1\xCF\x84\xC3\xA4u! " + " \xE2\x84\xA4\xE2\x8A\x87\xE2\x84\x9D"; + color =3D grub_video_map_rgb (128, 128, 255); + + /* All characters in the string exist in the 'Fixed 20' (10x20) font. */ + texty +=3D grub_font_get_ascent(fixed); + grub_font_draw_string (str, fixed, color, 16, texty); + texty +=3D grub_font_get_descent (fixed) + grub_font_get_leading (fixed); + + /* Some character don't exist in the Helvetica font, so the font engine + will fall back to using glyphs from another font that does contain th= em. + TODO The font engine should be smart about selecting a replacement fo= nt + and prioritize fonts with similar sizes. */ + + texty +=3D grub_font_get_ascent(sansbig); + grub_font_draw_string (str, sansbig, color, 16, texty); + texty +=3D grub_font_get_descent (sansbig) + grub_font_get_leading (sans= big); + + texty +=3D grub_font_get_ascent(sans); + grub_font_draw_string (str, sans, color, 16, texty); + texty +=3D grub_font_get_descent (sans) + grub_font_get_leading (sans); + + texty +=3D grub_font_get_ascent(sanssmall); + grub_font_draw_string (str, sanssmall, color, 16, texty); + texty +=3D (grub_font_get_descent (sanssmall) + + grub_font_get_leading (sanssmall)); + + glyph =3D grub_font_get_glyph (fixed, '*'); =20 for (i =3D 0; i < 16; i++) { color =3D grub_video_map_color (i); palette[i] =3D color; - grub_video_blit_glyph (&glyph, color, 16 + i * 16, 32); + grub_font_draw_glyph (glyph, color, 16 + i * 16, 220); } =20 grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY); =3D=3D=3D modified file 'conf/common.rmk' --- conf/common.rmk 2008-09-29 13:57:05 +0000 +++ conf/common.rmk 2008-10-30 18:24:32 +0000 @@ -364,7 +364,7 @@ help_mod_LDFLAGS =3D $(COMMON_LDFLAGS) =20 # For font.mod. -font_mod_SOURCES =3D font/manager.c +font_mod_SOURCES =3D font/font_cmd.c font/font.c font_mod_CFLAGS =3D $(COMMON_CFLAGS) font_mod_LDFLAGS =3D $(COMMON_LDFLAGS) =20 =3D=3D=3D added file 'font/font.c' --- font/font.c 1970-01-01 00:00:00 +0000 +++ font/font.c 2008-10-31 03:46:01 +0000 @@ -0,0 +1,1004 @@ +/* font.c - Font API and font file loader. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef FONT_DEBUG +#define FONT_DEBUG 0 +#endif + +struct char_index_entry +{ + grub_uint32_t code; + grub_uint8_t storage_flags; + grub_uint32_t offset; + struct grub_font_glyph *glyph; /* Glyph if loaded, or null. */ +}; + +#define FONT_WEIGHT_NORMAL 100 +#define FONT_WEIGHT_BOLD 200 + +struct grub_font +{ + char *name; + grub_file_t file; + char *family; + short point_size; + short weight; + short max_char_width; + short max_char_height; + short ascent; + short descent; + short leading; + grub_uint32_t num_chars; + struct char_index_entry *char_index; +}; + + +/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/ +/* Font loader */ + + +/* Definition of font registry. */ +struct grub_font_node *grub_font_list; + +static int register_font (grub_font_t font); +static void font_init (grub_font_t font); +static void free_font (grub_font_t font); +static void remove_font (grub_font_t font); + +struct font_file_section +{ + grub_file_t file; /* The file this section is in. */ + char name[4]; /* FOURCC name of the section. */ + grub_uint32_t length; /* Length of the section contents. */ + int eof; /* Set by open_section() on EOF. */ +}; + +/* Font file format constants. */ +static const char pff2_magic[4] =3D { 'P', 'F', 'F', '2' }; +static const char section_names_file[4] =3D { 'F', 'I', 'L', 'E' }; +static const char section_names_font_name[4] =3D { 'N', 'A', 'M', 'E' }; +static const char section_names_point_size[4] =3D { 'P', 'T', 'S', 'Z' }; +static const char section_names_weight[4] =3D { 'W', 'E', 'I', 'G' }; +static const char section_names_max_char_width[4] =3D { 'M', 'A', 'X', 'W'= }; +static const char section_names_max_char_height[4] =3D { 'M', 'A', 'X', 'H= ' }; +static const char section_names_ascent[4] =3D { 'A', 'S', 'C', 'E' }; +static const char section_names_descent[4] =3D { 'D', 'E', 'S', 'C' }; +static const char section_names_char_index[4] =3D { 'C', 'H', 'I', 'X' }; +static const char section_names_data[4] =3D { 'D', 'A', 'T', 'A' }; + +/* Replace unknown glyphs with a rounded question mark. */ +static grub_uint8_t unknown_glyph_bitmap[] =3D +{ + /* 76543210 */ + 0x7C, /* ooooo */ + 0x82, /* o o */ + 0xBA, /* o ooo o */ + 0xAA, /* o o o o */ + 0xAA, /* o o o o */ + 0x8A, /* o o o */ + 0x9A, /* o oo o */ + 0x92, /* o o o */ + 0x92, /* o o o */ + 0x92, /* o o o */ + 0x92, /* o o o */ + 0x82, /* o o */ + 0x92, /* o o o */ + 0x82, /* o o */ + 0x7C, /* ooooo */ + 0x00 /* */ +}; + +/* The "unknown glyph" glyph, used as a last resort. */ +static struct grub_font_glyph *unknown_glyph; +/* The font structure used when no other font is loaded. This functions + as a "Null Object" pattern, so that code everywhere does not have to + check for a NULL grub_font_t to avoid dereferencing a null pointer. */ +static struct grub_font null_font; +/* Flag to ensure module is initialized only once. */ +static grub_uint8_t font_loader_initialized; + +void +grub_font_loader_init (void) +{ + if (font_loader_initialized) + return; /* Only initialize font loader once. */ + + unknown_glyph =3D grub_malloc(sizeof(struct grub_font_glyph) + + sizeof(unknown_glyph_bitmap)); + if (! unknown_glyph) + return; + + unknown_glyph->width =3D 8; + unknown_glyph->height =3D 16; + unknown_glyph->offset_x =3D 0; + unknown_glyph->offset_y =3D 0; + unknown_glyph->device_width =3D 8; + grub_memcpy(unknown_glyph->bitmap, + unknown_glyph_bitmap, sizeof(unknown_glyph_bitmap)); + + font_init (&null_font); /* Initialize the null font. */ + null_font.name =3D ""; + null_font.ascent =3D unknown_glyph->height; + null_font.descent =3D 1; + null_font.max_char_width =3D unknown_glyph->width; + null_font.max_char_height =3D unknown_glyph->height; + + font_loader_initialized =3D 1; +} + +/* Initialize the font object with initial default values. */ +static void +font_init (grub_font_t font) +{ + font->name =3D 0; + font->file =3D 0; + font->family =3D 0; + font->point_size =3D 0; + font->weight =3D 0; + font->leading =3D 1; /* Default leading value, not in font file yet. = */ + font->max_char_width =3D 0; + font->max_char_height =3D 0; + font->ascent =3D 0; + font->descent =3D 0; + font->num_chars =3D 0; + font->char_index =3D 0; +} + +/* Open the next section in the file. + + On success, the section name is stored in section->name and the length = in + section->length, and 0 is returned. On failure, 1 is returned and + grub_errno is set approriately with an error message. + + If 1 is returned due to being at the end of the file, then section->eof= is + set to 1; otherwise, section->eof is set to 0. */ +static int +open_section (grub_file_t file, struct font_file_section *section) +{ + grub_ssize_t retval; + grub_uint32_t raw_length; + + section->file =3D file; + section->eof =3D 0; + + /* Read the FOURCC section name. */ + retval =3D grub_file_read (file, section->name, 4); + if (retval >=3D 0 && retval < 4) + { + section->eof =3D 1; + return 1; /* EOF encountered. */ + } + else if (retval < 0) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font format error: can't read section name"); + return 1; /* Read error. */ + } + + /* Read the big-endian 32-bit section length. */ + retval =3D grub_file_read (file, (char *) &raw_length, 4); + if (retval >=3D 0 && retval < 4) + { + section->eof =3D 1; + return 1; /* EOF encountered. */ + } + else if (retval < 0) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font format error: can't read section length"); + return 1; /* Read error. */ + } + + /* Convert byte-order and store in *length. */ + section->length =3D grub_be_to_cpu32 (raw_length); + + return 0; +} + +/* Size in bytes of each character index (CHIX section) + entry in the font file. */ +#define FONT_CHAR_INDEX_ENTRY_SIZE (4 + 1 + 4) + +/* Load the character index (CHIX) section contents from the font file. T= his + presumes that the position of FILE is positioned immediately after the + section length for the CHIX section (i.e., at the start of the section + contents). Returns 0 upon success, nonzero for failure (in which case + grub_errno is set appropriately). */ +static int +load_font_index (grub_file_t file, grub_uint32_t sect_length, struct + grub_font *font) +{ + unsigned i; + +#if FONT_DEBUG >=3D 2 + grub_printf("load_font_index(sect_length=3D%d)\n", sect_length); +#endif + + /* Sanity check: ensure section length is divisible by the entry size. = */ + if (sect_length % FONT_CHAR_INDEX_ENTRY_SIZE !=3D 0) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font file format error: character index length %d " + "is not a multiple of the entry size %d", + sect_length, FONT_CHAR_INDEX_ENTRY_SIZE); + return 1; /* Invalid index section length. */ + } + + /* Calculate the number of characters. */ + font->num_chars =3D sect_length / FONT_CHAR_INDEX_ENTRY_SIZE; + + /* Allocate the character index array. */ + font->char_index =3D grub_malloc (font->num_chars + * sizeof (struct char_index_entry)); + if (!font->char_index) + return 1; /* Error allocating memory. */ + +#if FONT_DEBUG >=3D 2 + grub_printf("num_chars=3D%d)\n", font->num_chars); +#endif + + /* Load the character index data from the file. */ + for (i =3D 0; i < font->num_chars; i++) + { + struct char_index_entry *entry =3D &font->char_index[i]; + + /* Read code point value; convert to native byte order. */ + if (grub_file_read (file, (char *) &entry->code, 4) !=3D 4) + return 1; + entry->code =3D grub_be_to_cpu32 (entry->code); + + /* Read storage flags byte. */ + if (grub_file_read (file, (char *) &entry->storage_flags, 1) !=3D 1) + return 1; + + /* Read glyph data offset; convert to native byte order. */ + if (grub_file_read (file, (char *) &entry->offset, 4) !=3D 4) + return 1; + entry->offset =3D grub_be_to_cpu32 (entry->offset); + + /* No glyph loaded. Will be loaded on demand and cached thereafter.= */ + entry->glyph =3D 0; + +#if FONT_DEBUG >=3D 5 + if (i < 10) /* Print the 1st 10 characters. */ + grub_printf("c=3D%d o=3D%d\n", entry->code, entry->offset); +#endif + } + + return 0; /* Index loaded OK. */ +} + +/* Read the contents of the specified section as a string, which is + allocated on the heap. Returns 0 if there is an error. */ +static char * +read_section_as_string (struct font_file_section *section) +{ + char *str; + grub_ssize_t ret; + + str =3D grub_malloc (section->length + 1); + if (!str) + return 0; + + ret =3D grub_file_read (section->file, str, section->length); + if (ret < 0 || ret !=3D (grub_ssize_t) section->length) + { + grub_free (str); + return 0; + } + + str[section->length] =3D '\0'; + return str; +} + +/* Read the contents of the current section as a 16-bit integer value, + which is stored into *VALUE. + Returns 0 upon success, nonzero upon failure. */ +static int +read_section_as_short (struct font_file_section *section, grub_int16_t *va= lue) +{ + grub_uint16_t raw_value; + + if (section->length !=3D 2) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font file format error: section %c%c%c%c length " + "is %d but should be 2", + section->name[0], section->name[1], + section->name[2], section->name[3], + section->length); + return 1; /* An error occurred. */ + } + if (grub_file_read (section->file, (char *) &raw_value, 2) !=3D 2) + return 1; /* An error occurred. */ + + *value =3D grub_be_to_cpu16 (raw_value); + return 0; /* Successfully read the value. */ +} + +/* Load a font and add it to the beginning of the global font list. + Returns 0 upon success, nonzero upon failure. */ +int +grub_font_load (const char *filename) +{ + grub_file_t file =3D 0; + struct font_file_section section; + char magic[4]; + grub_font_t font =3D 0; + +#if FONT_DEBUG >=3D 1 + grub_printf("add_font(%s)\n", filename); +#endif + + file =3D grub_buffile_open (filename, 1024); + if (!file) + goto fail; + +#if FONT_DEBUG >=3D 3 + grub_printf("file opened\n"); +#endif + + /* Read the FILE section. It indicates the file format. */ + if (open_section (file, §ion) !=3D 0) + goto fail; + +#if FONT_DEBUG >=3D 3 + grub_printf("opened FILE section\n"); +#endif + if (grub_memcmp (section.name, section_names_file, 4) !=3D 0) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font file format error: 1st section must be FILE"); + goto fail; + } + +#if FONT_DEBUG >=3D 3 + grub_printf("section name ok\n"); +#endif + if (section.length !=3D 4) + { + grub_error (GRUB_ERR_BAD_FONT, + "Font file format error (file type ID length is %d " + "but should be 4)", section.length); + goto fail; + } + +#if FONT_DEBUG >=3D 3 + grub_printf("section length ok\n"); +#endif + /* Check the file format type code. */ + if (grub_file_read (file, magic, 4) !=3D 4) + goto fail; + +#if FONT_DEBUG >=3D 3 + grub_printf("read magic ok\n"); +#endif + + if (grub_memcmp (magic, pff2_magic, 4) !=3D 0) + { + grub_error (GRUB_ERR_BAD_FONT, "Invalid font magic %x %x %x %x", + magic[0], magic[1], magic[2], magic[3]); + goto fail; + } + +#if FONT_DEBUG >=3D 3 + grub_printf("compare magic ok\n"); +#endif + + /* Allocate the font object. */ + font =3D (grub_font_t) grub_malloc (sizeof (struct grub_font)); + if (!font) + goto fail; + + font_init (font); + font->file =3D file; + +#if FONT_DEBUG >=3D 3 + grub_printf("allocate font ok; loading font info\n"); +#endif + + /* Load the font information. */ + while (1) + { + if (open_section (file, §ion) !=3D 0) + { + if (section.eof) + break; /* Done reading the font file. */ + else + goto fail; + } + +#if FONT_DEBUG >=3D 2 + grub_printf("opened section %c%c%c%c ok\n", + section.name[0], section.name[1], + section.name[2], section.name[3]); +#endif + + if (grub_memcmp (section.name, section_names_font_name, 4) =3D=3D 0) + { + font->name =3D read_section_as_string (§ion); + if (!font->name) + goto fail; + } + else if (grub_memcmp (section.name, section_names_point_size, 4) =3D= =3D 0) + { + if (read_section_as_short (§ion, &font->point_size) !=3D 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_weight, 4) =3D=3D = 0) + { + char *wt; + wt =3D read_section_as_string (§ion); + if (!wt) + continue; + /* Convert the weight string 'normal' or 'bold' into a number. = */ + if (grub_strcmp (wt, "normal") =3D=3D 0) + font->weight =3D FONT_WEIGHT_NORMAL; + else if (grub_strcmp (wt, "bold") =3D=3D 0) + font->weight =3D FONT_WEIGHT_BOLD; + grub_free (wt); + } + else if (grub_memcmp (section.name, section_names_max_char_width, 4)= =3D=3D 0) + { + if (read_section_as_short (§ion, &font->max_char_width) !=3D= 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_max_char_height, 4= ) =3D=3D 0) + { + if (read_section_as_short (§ion, &font->max_char_height) != =3D 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_ascent, 4) =3D=3D = 0) + { + if (read_section_as_short (§ion, &font->ascent) !=3D 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_descent, 4) =3D=3D= 0) + { + if (read_section_as_short (§ion, &font->descent) !=3D 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_char_index, 4) =3D= =3D 0) + { + /* Load the font index. */ + if (load_font_index (file, section.length, font) !=3D 0) + goto fail; + } + else if (grub_memcmp (section.name, section_names_data, 4) =3D=3D 0) + { + /* When the DATA section marker is reached, we stop reading. */ + break; + } + else + { + /* Unhandled section type, simply skip past it. */ +#if FONT_DEBUG >=3D 3 + grub_printf("Unhandled section type, skipping.\n"); +#endif + grub_off_t section_end =3D grub_file_tell (file) + section.lengt= h; + if ((int) grub_file_seek (file, section_end) =3D=3D -1) + goto fail; + } + } + + if (!font->name) + { + grub_printf ("Note: Font has no name.\n"); + font->name =3D grub_strdup ("Unknown"); + } + +#if FONT_DEBUG >=3D 1 + grub_printf ("Loaded font `%s'.\n" + "Ascent=3D%d Descent=3D%d MaxW=3D%d MaxH=3D%d Number of cha= racters=3D%d.\n", + font->name, + font->ascent, font->descent, + font->max_char_width, font->max_char_height, + font->num_chars); +#endif + + if (font->max_char_width =3D=3D 0 + || font->max_char_height =3D=3D 0 + || font->num_chars =3D=3D 0 + || font->char_index =3D=3D 0 + || font->ascent =3D=3D 0 + || font->descent =3D=3D 0) + { + grub_error (GRUB_ERR_BAD_FONT, + "Invalid font file: missing some required data."); + goto fail; + } + + /* Add the font to the global font registry. */ + if (register_font (font) !=3D 0) + goto fail; + + return 0; /* Font loaded ok. */ + +fail: + free_font (font); + return 1; /* Failed to load font. */ +} + +/* Read a 16-bit big-endian integer from FILE, convert it to native byte + order, and store it in *VALUE. + Returns 0 on success, 1 on failure. */ +static int +read_be_uint16 (grub_file_t file, grub_uint16_t * value) +{ + if (grub_file_read (file, (char *) value, 2) !=3D 2) + return 1; + *value =3D grub_be_to_cpu16 (*value); + return 0; +} + +static int +read_be_int16 (grub_file_t file, grub_int16_t * value) +{ + /* For the signed integer version, use the same code as for unsigned. */ + return read_be_uint16 (file, (grub_uint16_t *) value); +} + +/* Return a pointer to the character index entry for the glyph correspondi= ng to + the codepoint CODE in the font FONT. If not found, return zero. */ +static struct char_index_entry * +find_glyph (const grub_font_t font, grub_uint32_t code) +{ + grub_uint32_t i; + grub_uint32_t len =3D font->num_chars; + struct char_index_entry *table =3D font->char_index; + + /* Do a linear search. */ + for (i =3D 0; i < len; i++) + { + if (table[i].code =3D=3D code) + return &table[i]; + } + + return 0; /* No entry found for code point CODE. */ +} + +/* Get a glyph for the Unicode character CODE in FONT. The glyph is loaded + from the font file if has not been loaded yet. + Returns a pointer to the glyph if found, or 0 if it is not found. */ +static struct grub_font_glyph * +grub_font_get_glyph_internal (grub_font_t font, grub_uint32_t code) +{ + struct char_index_entry *index_entry; + + index_entry =3D find_glyph (font, code); + if (index_entry) + { + struct grub_font_glyph *glyph =3D 0; + grub_uint16_t width; + grub_uint16_t height; + grub_int16_t xoff; + grub_int16_t yoff; + grub_int16_t dwidth; + int len; + + if (index_entry->glyph) + return index_entry->glyph; /* Return cached glyph. */ + + if (! font->file) + return 0; /* No open file, can't load any glyphs. */ + + /* Make sure we can find glyphs for error messages. Push active + error message to error stack and reset error message. */ + grub_error_push (); + + grub_file_seek (font->file, index_entry->offset); + + /* Read the glyph width, height, and baseline. */ + if (read_be_uint16(font->file, &width) !=3D 0 + || read_be_uint16(font->file, &height) !=3D 0 + || read_be_int16(font->file, &xoff) !=3D 0 + || read_be_int16(font->file, &yoff) !=3D 0 + || read_be_int16(font->file, &dwidth) !=3D 0) + { + remove_font (font); + return 0; + } + + len =3D (width * height + 7) / 8; + glyph =3D grub_malloc (sizeof (struct grub_font_glyph) + len); + if (! glyph) + { + remove_font (font); + return 0; + } + + glyph->font =3D font; + glyph->width =3D width; + glyph->height =3D height; + glyph->offset_x =3D xoff; + glyph->offset_y =3D yoff; + glyph->device_width =3D dwidth; + + /* Don't try to read empty bitmaps (e.g., space characters). */ + if (len !=3D 0) + { + if (grub_file_read (font->file, (char *) glyph->bitmap, len) != =3D len) + { + remove_font (font); + return 0; + } + } + + /* Restore old error message. */ + grub_error_pop (); + + /* Cache the glyph. */ + index_entry->glyph =3D glyph; + + return glyph; /* Glyph loaded ok. */ + } + + return 0; +} + +/* Free the memory used by FONT. + This should not be called if the font has been made available to + users (once it is added to the global font list), since there would + be the possibility of a dangling pointer. */ +static void +free_font (grub_font_t font) +{ + if (font) + { + if (font->file) + grub_file_close (font->file); + grub_free (font->name); + grub_free (font->family); + grub_free (font->char_index); + grub_free (font); + } +} + +/* Add FONT to the global font registry. + Returns 0 upon success, nonzero on failure + (the font was not registered). */ +static int +register_font (grub_font_t font) +{ + struct grub_font_node *node =3D 0; + + node =3D grub_malloc (sizeof (struct grub_font_node)); + if (! node) + return 1; /* Error. */ + + node->value =3D font; + node->next =3D grub_font_list; + grub_font_list =3D node; + + return 0; /* Success. */ +} + +/* Remove the font from the global font list. We don't actually free the + font's memory since users could be holding references to the font. */ +static void +remove_font (grub_font_t font) +{ + struct grub_font_node **nextp, *cur; + + for (nextp =3D &grub_font_list, cur =3D *nextp; + cur; + nextp =3D &cur->next, cur =3D cur->next) + { + if (cur->value =3D=3D font) + { + *nextp =3D cur->next; + + /* Free the node, but not the font itself. */ + grub_free (cur); + + return; + } + } +} + + +/*=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D*/ +/* Public font API */ + +/* Get a font from the list of loaded fonts. This function will return + another font if the requested font is not available. If no fonts are + loaded, then a special 'null font' is returned, which contains no glyph= s, + but is not a null pointer so the caller may omit checks for NULL. */ +grub_font_t +grub_font_get (const char *font_name) +{ + struct grub_font_node *node; + + for (node =3D grub_font_list; node; node =3D node->next) + { + grub_font_t font =3D node->value; + if (grub_strcmp (font->name, font_name) =3D=3D 0) + return font; + } + + /* If no font by that name is found, return the first font in the list + as a fallback. */ + if (grub_font_list && grub_font_list->value) + return grub_font_list->value; + else + return &null_font; /* The null_font is a last resort. */ +} + +/* Get the full name of the font. For instance, "Helvetica Bold 12". */ +const char * +grub_font_get_name (grub_font_t font) +{ + return font->name; +} + +/* Get the maximum width of any character in the font in pixels. */ +int +grub_font_get_max_char_width (grub_font_t font) +{ + return font->max_char_width; +} + +/* Get the maximum height of any character in the font in pixels. */ +int +grub_font_get_max_char_height (grub_font_t font) +{ + return font->max_char_height; +} + +/* Get the distance in pixels from the top of characters to the baseline. = */ +int +grub_font_get_ascent (grub_font_t font) +{ + return font->ascent; +} + +/* Get the distance in pixels from the baseline to the lowest descenders + (for instance, in a lowercase 'y', 'g', etc.). */ +int +grub_font_get_descent (grub_font_t font) +{ + return font->descent; +} + +/* Get the *standard leading* of the font in pixel, which is the spacing + between two lines of text. Specifically, it is the space between the + descent of one line and the ascent of the next line. This is included + in the *height* metric. */ +int +grub_font_get_leading (grub_font_t font) +{ + return font->leading; +} + +/* Get the distance in pixels between baselines of adjacent lines of text.= */ +int +grub_font_get_height (grub_font_t font) +{ + return font->ascent + font->descent + font->leading; +} + +/* Get the width in pixels of the specified UTF-8 string, when rendered in + in the specified font (but falling back on other fonts for glyphs that + are missing). */ +int +grub_font_get_string_width (grub_font_t font, const char *str) +{ + int width; + struct grub_font_glyph *glyph; + grub_uint32_t code; + const grub_uint8_t *ptr; + + for (ptr =3D (const grub_uint8_t *) str, width =3D 0; + grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0; ) + { + glyph =3D grub_font_get_glyph_with_fallback (font, code); + width +=3D glyph->device_width; + } + + return width; +} + +/* Get the glyph for FONT corresponding to the Unicode code point CODE. + Returns a pointer to an glyph indicating there is no glyph available + if CODE does not exist in the font. The glyphs are cached once loaded.= */ +struct grub_font_glyph * +grub_font_get_glyph (grub_font_t font, grub_uint32_t code) +{ + struct grub_font_glyph *glyph; + glyph =3D grub_font_get_glyph_internal (font, code); + if (glyph =3D=3D 0) + glyph =3D unknown_glyph; + return glyph; +} + + +/* Calculate a subject value representing "how similar" two fonts are. + This is used to prioritize the order that fonts are scanned for missing + glyphs. The object is to select glyphs from the most similar font + possible, for the best appearance. + The heuristic is crude, but it helps greatly when fonts of similar + sizes are used so that tiny 8 point glyphs are not mixed into a string + of 24 point text unless there is no other choice. */ +static int +get_font_diversity(grub_font_t a, grub_font_t b) +{ + int d; + + d =3D 0; + + if (a->ascent && b->ascent) + d +=3D grub_abs (a->ascent - b->ascent) * 8; + else + d +=3D 50; /* Penalty for missing attributes. */ + + if (a->max_char_height && b->max_char_height) + d +=3D grub_abs (a->max_char_height - b->max_char_height) * 8; + else + d +=3D 50; /* Penalty for missing attributes. */ + + d +=3D (a->weight !=3D b->weight) ? 5 : 0; /* Weight is a minor factor= . */ + + return d; +} + +/* Get a glyph corresponding to the codepoint CODE. If FONT contains the + specified glyph, then it is returned. Otherwise, all other loaded fonts + are searched until one is found that contains a glyph for CODE. + If no glyph is available for CODE in the loaded fonts, then a glyph + representing an unknown character is returned. + This function never returns NULL. + The returned glyph is owned by the font manager and should not be freed + by the caller. The glyphs are cached. */ +struct grub_font_glyph * +grub_font_get_glyph_with_fallback (grub_font_t font, grub_uint32_t code) +{ + struct grub_font_glyph *glyph; + struct grub_font_node *node; + /* Keep track of next node, in case there's an I/O error in + grub_font_get_glyph_internal() and the font is removed from the list.= */ + struct grub_font_node *next; + /* Information on the best glyph found so far, to help find the glyph in + the best matching to the requested one. */ + int best_diversity; + struct grub_font_glyph *best_glyph; + + if (font) + { + /* First try to get the glyph from the specified font. */ + glyph =3D grub_font_get_glyph_internal (font, code); + if (glyph) + return glyph; + } + + /* Otherwise, search all loaded fonts for the glyph and use the one from + the font that best matches the requested font. */ + best_diversity =3D 10000; + best_glyph =3D 0; + + for (node =3D grub_font_list; node; node =3D next) + { + grub_font_t curfont; + + curfont =3D node->value; + next =3D node->next; + + glyph =3D grub_font_get_glyph_internal (curfont, code); + if (glyph) + { + int d; + + d =3D get_font_diversity (curfont, font); + if (d < best_diversity) + { + best_diversity =3D d; + best_glyph =3D glyph; + } + } + } + + if (best_glyph) + return best_glyph; + else + return unknown_glyph; /* Ugh... Glyph not available in any font. */ +} + + +/* Draw the specified glyph at (x, y). The y coordinate designates the + baseline of the character, while the x coordinate designates the left + side location of the character. */ +grub_err_t +grub_font_draw_glyph (struct grub_font_glyph *glyph, + grub_video_color_t color, + int left_x, int baseline_y) +{ + struct grub_video_bitmap glyph_bitmap; + + /* Don't try to draw empty glyphs (U+0020, etc.). */ + if (glyph->width =3D=3D 0 || glyph->height =3D=3D 0) + return GRUB_ERR_NONE; + + glyph_bitmap.mode_info.width =3D glyph->width; + glyph_bitmap.mode_info.height =3D glyph->height; + glyph_bitmap.mode_info.mode_type =3D + (1 << GRUB_VIDEO_MODE_TYPE_DEPTH_POS) + | GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP; + glyph_bitmap.mode_info.blit_format =3D GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKE= D; + glyph_bitmap.mode_info.bpp =3D 1; + glyph_bitmap.mode_info.bytes_per_pixel =3D 0; /* Really 1 bit per pixel= . */ + glyph_bitmap.mode_info.pitch =3D glyph->width; /* Packed densely as bits= . */ + glyph_bitmap.mode_info.number_of_colors =3D 2; + glyph_bitmap.mode_info.bg_red =3D 0; + glyph_bitmap.mode_info.bg_green =3D 0; + glyph_bitmap.mode_info.bg_blue =3D 0; + glyph_bitmap.mode_info.bg_alpha =3D 0; + grub_video_unmap_color(color, + &glyph_bitmap.mode_info.fg_red, + &glyph_bitmap.mode_info.fg_green, + &glyph_bitmap.mode_info.fg_blue, + &glyph_bitmap.mode_info.fg_alpha); + glyph_bitmap.data =3D glyph->bitmap; + + int bitmap_left =3D left_x + glyph->offset_x; + int bitmap_bottom =3D baseline_y - glyph->offset_y; + int bitmap_top =3D bitmap_bottom - glyph->height; + + return grub_video_blit_bitmap (&glyph_bitmap, GRUB_VIDEO_BLIT_BLEND, + bitmap_left, bitmap_top, + 0, 0, + glyph->width, glyph->height); +} + +/* Draw a UTF-8 string of text on the current video render target. + The x coordinate specifies the starting x position for the first charac= ter, + while the y coordinate specifies the baseline position. + If the string contains a character that FONT does not contain, then + a glyph from another loaded font may be used instead. */ +grub_err_t +grub_font_draw_string (const char *str, grub_font_t font, + grub_video_color_t color, + int left_x, int baseline_y) +{ + int x; + struct grub_font_glyph *glyph; + grub_uint32_t code; + const grub_uint8_t *ptr; + + for (ptr =3D (const grub_uint8_t *) str, x =3D left_x; + grub_utf8_to_ucs4 (&code, 1, ptr, -1, &ptr) > 0; ) + { + glyph =3D grub_font_get_glyph_with_fallback (font, code); + if (grub_font_draw_glyph (glyph, color, x, baseline_y) + !=3D GRUB_ERR_NONE) + return grub_errno; + x +=3D glyph->device_width; + } + + return GRUB_ERR_NONE; +} + =3D=3D=3D added file 'font/font_cmd.c' --- font/font_cmd.c 1970-01-01 00:00:00 +0000 +++ font/font_cmd.c 2008-10-31 03:44:24 +0000 @@ -0,0 +1,77 @@ +/* font_cmd.c - Font command definition. */ +/* + * GRUB -- GRand Unified Bootloader + * Copyright (C) 2003,2005,2006,2007,2008 Free Software Foundation, Inc. + * + * GRUB is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GRUB is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GRUB. If not, see . + */ + +#include +#include +#include +#include + +static grub_err_t +loadfont_command (struct grub_arg_list *state __attribute__ ((unused)), + int argc, + char **args) +{ + if (argc =3D=3D 0) + return grub_error (GRUB_ERR_BAD_ARGUMENT, "no font specified"); + + while (argc--) + if (grub_font_load (*args++) !=3D 0) + return GRUB_ERR_BAD_FONT; + + return GRUB_ERR_NONE; +} + +static grub_err_t +lsfonts_command (struct grub_arg_list *state __attribute__ ((unused)), + int argc __attribute__ ((unused)), + char **args __attribute__ ((unused))) +{ + struct grub_font_node *node; + + grub_printf ("Loaded fonts:\n"); + for (node =3D grub_font_list; node; node =3D node->next) + { + grub_font_t font =3D node->value; + grub_printf ("%s\n", grub_font_get_name (font)); + } + + return GRUB_ERR_NONE; +} + +GRUB_MOD_INIT(font_manager) +{ + grub_font_loader_init (); + + grub_register_command ("loadfont", loadfont_command, GRUB_COMMAND_FLAG_B= OTH, + "loadfont FILE...", + "Specify one or more font files to load.", 0); + + grub_register_command ("lsfonts", lsfonts_command, GRUB_COMMAND_FLAG_BOT= H, + "lsfonts", + "List the loaded fonts.", 0); +} + +GRUB_MOD_FINI(font_manager) +{ + /* Should this free fonts, unknown_glyph, etc.? Freeing fonts could=20 + be a Bad Thing if there are still references to any of them. */ + + grub_unregister_command ("loadfont"); +} + =3D=3D=3D removed file 'font/manager.c' --- font/manager.c 2008-08-01 03:06:55 +0000 +++ font/manager.c 1970-01-01 00:00:00 +0000 @@ -1,283 +0,0 @@ -/* - * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003,2005,2006,2007 Free Software Foundation, Inc. - * - * GRUB is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * GRUB is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GRUB. If not, see . - */ - -#include -#include -#include -#include -#include -#include -#include -#include - -struct entry -{ - grub_uint32_t code; - grub_uint32_t offset; -}; - -struct font -{ - struct font *next; - grub_file_t file; - grub_uint32_t num; - struct entry table[0]; -}; - -static struct font *font_list; - -/* Fill unknown glyph's with rounded question mark. */ -static grub_uint8_t unknown_glyph[16] =3D -{ /* 76543210 */ - 0x7C, /* ooooo */ - 0x82, /* o o */ - 0xBA, /* o ooo o */ - 0xAA, /* o o o o */ - 0xAA, /* o o o o */ - 0x8A, /* o o o */ - 0x9A, /* o oo o */ - 0x92, /* o o o */ - 0x92, /* o o o */ - 0x92, /* o o o */ - 0x92, /* o o o */ - 0x82, /* o o */ - 0x92, /* o o o */ - 0x82, /* o o */ - 0x7C, /* ooooo */ - 0x00 /* */ -}; - -static int -add_font (const char *filename) -{ - grub_file_t file =3D 0; - char magic[4]; - grub_uint32_t num, i; - struct font *font =3D 0; - - file =3D grub_buffile_open (filename, 0); - if (! file) - goto fail; - - if (grub_file_read (file, magic, 4) !=3D 4) - goto fail; - - if (grub_memcmp (magic, GRUB_FONT_MAGIC, 4) !=3D 0) - { - grub_error (GRUB_ERR_BAD_FONT, "invalid font magic"); - goto fail; - } - - if (grub_file_read (file, (char *) &num, 4) !=3D 4) - goto fail; - - num =3D grub_le_to_cpu32 (num); - font =3D (struct font *) grub_malloc (sizeof (struct font) - + sizeof (struct entry) * num); - if (! font) - goto fail; - - font->file =3D file; - font->num =3D num; - - for (i =3D 0; i < num; i++) - { - grub_uint32_t code, offset; - =20 - if (grub_file_read (file, (char *) &code, 4) !=3D 4) - goto fail; - - if (grub_file_read (file, (char *) &offset, 4) !=3D 4) - goto fail; - - font->table[i].code =3D grub_le_to_cpu32 (code); - font->table[i].offset =3D grub_le_to_cpu32 (offset); - } - - font->next =3D font_list; - font_list =3D font; - - return 1; - - fail: - if (font) - grub_free (font); - - if (file) - grub_file_close (file); - - return 0; -} - -static void -remove_font (struct font *font) -{ - struct font **p, *q; - - for (p =3D &font_list, q =3D *p; q; p =3D &(q->next), q =3D q->next) - if (q =3D=3D font) - { - *p =3D q->next; -=09 - grub_file_close (font->file); - grub_free (font); -=09 - break; - } -} - -/* Return the offset of the glyph corresponding to the codepoint CODE - in the font FONT. If no found, return zero. */ -static grub_uint32_t -find_glyph (const struct font *font, grub_uint32_t code) -{ - grub_uint32_t start =3D 0; - grub_uint32_t end =3D font->num - 1; - const struct entry *table =3D font->table; - =20 - /* This shouldn't happen. */ - if (font->num =3D=3D 0) - return 0; - - /* Do a binary search. */ - while (start <=3D end) - { - grub_uint32_t i =3D (start + end) / 2; - - if (table[i].code < code) - start =3D i + 1; - else if (table[i].code > code) - end =3D i - 1; - else - return table[i].offset; - } - - return 0; -} - -/* Set the glyph to something stupid. */ -static void -fill_with_default_glyph (grub_font_glyph_t glyph) -{ - unsigned i; - - /* Use pre-defined pattern to fill unknown glyphs. */ - for (i =3D 0; i < 16; i++) - glyph->bitmap[i] =3D unknown_glyph[i]; - - glyph->char_width =3D 1; - glyph->width =3D glyph->char_width * 8; - glyph->height =3D 16; - glyph->baseline =3D (16 * 3) / 4; -} - -/* Get a glyph corresponding to the codepoint CODE. Always fill glyph - information with something, even if no glyph is found. */ -int -grub_font_get_glyph (grub_uint32_t code, - grub_font_glyph_t glyph) -{ - struct font *font; - grub_uint8_t bitmap[32]; - - /* FIXME: It is necessary to cache glyphs! */ - =20 - restart: - for (font =3D font_list; font; font =3D font->next) - { - grub_uint32_t offset; - - offset =3D find_glyph (font, code); - if (offset) - { - grub_uint32_t w; - int len; - - /* Make sure we can find glyphs for error messages. Push active - error message to error stack and reset error message. */ - grub_error_push (); - =20 - grub_file_seek (font->file, offset); - if ((len =3D grub_file_read (font->file, (char *) &w, sizeof (w))) - !=3D sizeof (w)) - { - remove_font (font); - goto restart; - } - - w =3D grub_le_to_cpu32 (w); - if (w !=3D 1 && w !=3D 2) - { - /* grub_error (GRUB_ERR_BAD_FONT, "invalid width"); */ - remove_font (font); - goto restart; - } - - if (grub_file_read (font->file, (char *) bitmap, w * 16) - !=3D (grub_ssize_t) w * 16) - { - remove_font (font); - goto restart; - } - - /* Fill glyph with information. */ =20 - grub_memcpy (glyph->bitmap, bitmap, w * 16); - =20 - glyph->char_width =3D w; - glyph->width =3D glyph->char_width * 8; - glyph->height =3D 16; - glyph->baseline =3D (16 * 3) / 4; - =20 - /* Restore old error message. */ - grub_error_pop (); - =20 - return 1; - } - } - - /* Uggh... No font was found. */ - fill_with_default_glyph (glyph); - return 0; -} - -static grub_err_t -font_command (struct grub_arg_list *state __attribute__ ((unused)), - int argc __attribute__ ((unused)), - char **args __attribute__ ((unused))) -{ - if (argc =3D=3D 0) - return grub_error (GRUB_ERR_BAD_ARGUMENT, "no font specified"); - - while (argc--) - if (! add_font (*args++)) - return 1; - - return 0; -} - -GRUB_MOD_INIT(font_manager) -{ - grub_register_command ("font", font_command, GRUB_COMMAND_FLAG_BOTH, - "font FILE...", - "Specify one or more font files to display.", 0); -} - -GRUB_MOD_FINI(font_manager) -{ - grub_unregister_command ("font"); -} =3D=3D=3D modified file 'include/grub/font.h' --- include/grub/font.h 2007-07-21 22:32:33 +0000 +++ include/grub/font.h 2008-10-31 03:44:24 +0000 @@ -1,6 +1,6 @@ /* * GRUB -- GRand Unified Bootloader - * Copyright (C) 2003,2007 Free Software Foundation, Inc. + * Copyright (C) 2003,2007,2008 Free Software Foundation, Inc. * * GRUB is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,34 +20,96 @@ #define GRUB_FONT_HEADER 1 =20 #include - -#define GRUB_FONT_MAGIC "PPF\x7f" +#include + +/* Forward declaration of opaque structure grub_font. + Users only pass struct grub_font pointers to the font module functions, + and do not have knowledge of the structure contents. */ +struct grub_font; + +/* Font type used to access font functions. */ +typedef struct grub_font *grub_font_t; + +struct grub_font_node +{ + struct grub_font_node *next; + grub_font_t value; +}; + +/* Global font registry. */ +extern struct grub_font_node *grub_font_list; =20 struct grub_font_glyph { - /* Glyph width in pixels. */ - grub_uint8_t width; - =20 - /* Glyph height in pixels. */ - grub_uint8_t height; - =20 - /* Glyph width in characters. */ - grub_uint8_t char_width; - =20 - /* Glyph baseline position in pixels (from up). */ - grub_uint8_t baseline; - =20 - /* Glyph bitmap data array of bytes in ((width + 7) / 8) * height. - Bitmap is formulated by height scanlines, each scanline having - width number of pixels. Pixels are coded as bits, value 1 meaning - of opaque pixel and 0 is transparent. If width does not fit byte - boundary, it will be padded with 0 to make it fit. */ - grub_uint8_t bitmap[32]; + /* Reference to the font this glyph belongs to. */ + grub_font_t font; + + /* Glyph bitmap width in pixels. */ + grub_uint16_t width; + + /* Glyph bitmap height in pixels. */ + grub_uint16_t height; + + /* Glyph bitmap x offset in pixels. Add to screen coordinate. */ + grub_int16_t offset_x; + + /* Glyph bitmap y offset in pixels. Subtract from screen coordinate. */ + grub_int16_t offset_y; + + /* Number of pixels to advance to start the next character. */ + grub_uint16_t device_width; + + /* Row-major order, packed bits (no padding; rows can break within a byt= e). + The length of the array is (width * height + 7) / 8. Within a + byte, the most significant bit is the first (leftmost/uppermost) pixe= l. + Pixels are coded as bits, value 1 meaning of opaque pixel and 0 is + transparent. If the length of the array does not fit byte boundary, it + will be padded with 0 bits to make it fit. */ + grub_uint8_t bitmap[0]; }; =20 -typedef struct grub_font_glyph *grub_font_glyph_t; - -int grub_font_get_glyph (grub_uint32_t code, - grub_font_glyph_t glyph); +/* Initialize the font loader. + Must be called before any fonts are loaded or used. */ +void grub_font_loader_init (void); + +/* Load a font and add it to the beginning of the global font list. + Returns: 0 upon success; nonzero upon failure. */ +int grub_font_load (const char *filename); + +/* Get the font that has the specified name. Font names are in the form + "Family Name Bold Italic 14", where Bold and Italic are optional. + If no font matches the name specified, the most recently loaded font + is returned as a fallback. */ +grub_font_t grub_font_get (const char *font_name); + +const char *grub_font_get_name (grub_font_t font); + +int grub_font_get_max_char_width (grub_font_t font); + +int grub_font_get_max_char_height (grub_font_t font); + +int grub_font_get_ascent (grub_font_t font); + +int grub_font_get_descent (grub_font_t font); + +int grub_font_get_leading (grub_font_t font); + +int grub_font_get_height (grub_font_t font); + +int grub_font_get_string_width (grub_font_t font, const char *str); + +struct grub_font_glyph *grub_font_get_glyph (grub_font_t font, + grub_uint32_t code); + +struct grub_font_glyph *grub_font_get_glyph_with_fallback (grub_font_t fon= t, + grub_uint32_t c= ode); + +grub_err_t grub_font_draw_glyph (struct grub_font_glyph *glyph, + grub_video_color_t color, + int left_x, int baseline_y); + +grub_err_t grub_font_draw_string (const char *str, grub_font_t font, + grub_video_color_t color, + int left_x, int baseline_y); =20 #endif /* ! GRUB_FONT_HEADER */ =3D=3D=3D modified file 'include/grub/misc.h' --- include/grub/misc.h 2008-09-19 05:55:20 +0000 +++ include/grub/misc.h 2008-10-30 18:24:32 +0000 @@ -77,8 +77,10 @@ grub_uint16_t *src, grub_size_t size); grub_ssize_t EXPORT_FUNC(grub_utf8_to_ucs4) (grub_uint32_t *dest, + grub_size_t destsize, const grub_uint8_t *src, - grub_size_t size); + grub_size_t srcsize, + const grub_uint8_t **srcend); grub_uint64_t EXPORT_FUNC(grub_divmod64) (grub_uint64_t n, grub_uint32_t d, grub_uint32_t *r); =20 =3D=3D=3D modified file 'include/grub/video.h' --- include/grub/video.h 2008-10-05 04:28:39 +0000 +++ include/grub/video.h 2008-10-30 18:28:52 +0000 @@ -31,17 +31,17 @@ struct grub_video_render_target; =20 /* Forward declarations for used data structures. */ -struct grub_font_glyph; struct grub_video_bitmap; =20 /* Defines used to describe video mode or rendering target. */ -#define GRUB_VIDEO_MODE_TYPE_ALPHA 0x00000008 -#define GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED 0x00000004 +#define GRUB_VIDEO_MODE_TYPE_ALPHA 0x00000020 +#define GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED 0x00000010 +#define GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP 0x00000004 #define GRUB_VIDEO_MODE_TYPE_INDEX_COLOR 0x00000002 #define GRUB_VIDEO_MODE_TYPE_RGB 0x00000001 =20 /* Defines used to mask flags. */ -#define GRUB_VIDEO_MODE_TYPE_COLOR_MASK 0x00000003 +#define GRUB_VIDEO_MODE_TYPE_COLOR_MASK 0x0000000F =20 /* Defines used to specify requested bit depth. */ #define GRUB_VIDEO_MODE_TYPE_DEPTH_MASK 0x0000ff00 @@ -72,7 +72,9 @@ GRUB_VIDEO_BLIT_FORMAT_BGR_565, =20 /* When needed, decode color or just use value as is. */ - GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR + GRUB_VIDEO_BLIT_FORMAT_INDEXCOLOR, + /* Two color bitmap; bits packed: rows are not padded to byte boundary= . */ + GRUB_VIDEO_BLIT_FORMAT_1BIT_PACKED }; =20 /* Define blitting operators. */ @@ -135,6 +137,18 @@ /* What is location of reserved color bits. In Index Color mode, this is 0. */ unsigned int reserved_field_pos; + + /* For 1-bit bitmaps, the background color. Used for bits =3D 0. */ + grub_uint8_t bg_red; + grub_uint8_t bg_green; + grub_uint8_t bg_blue; + grub_uint8_t bg_alpha; + + /* For 1-bit bitmaps, the foreground color. Used for bits =3D 1. */ + grub_uint8_t fg_red; + grub_uint8_t fg_green; + grub_uint8_t fg_blue; + grub_uint8_t fg_alpha; }; =20 struct grub_video_palette_data @@ -188,9 +202,6 @@ grub_err_t (*fill_rect) (grub_video_color_t color, int x, int y, unsigned int width, unsigned int height); =20 - grub_err_t (*blit_glyph) (struct grub_font_glyph *glyph, - grub_video_color_t color, int x, int y); - grub_err_t (*blit_bitmap) (struct grub_video_bitmap *bitmap, enum grub_video_blit_operators oper, int x, int y, int offset_x, int offset_y, @@ -260,9 +271,6 @@ grub_err_t grub_video_fill_rect (grub_video_color_t color, int x, int y, unsigned int width, unsigned int height); =20 -grub_err_t grub_video_blit_glyph (struct grub_font_glyph *glyph, - grub_video_color_t color, int x, int y); - grub_err_t grub_video_blit_bitmap (struct grub_video_bitmap *bitmap, enum grub_video_blit_operators oper, int x, int y, int offset_x, int offset_= y, =3D=3D=3D modified file 'kern/misc.c' --- kern/misc.c 2008-09-19 05:55:20 +0000 +++ kern/misc.c 2008-10-30 18:24:32 +0000 @@ -951,22 +951,29 @@ return dest; } =20 -/* Convert an UTF-8 string to an UCS-4 string. Return the number of - characters converted. DEST must be able to hold at least SIZE - characters (when the input is unknown). If an invalid sequence is found, - return -1. */ +/* Convert a (possibly null-terminated) UTF-8 string of at most SRCSIZE + bytes (if SRCSIZE is -1, it is ignored) in length to a UCS-4 string. + Return the number of characters converted. DEST must be able to hold + at least DESTSIZE characters. If an invalid sequence is found, return -= 1. + If SRCEND is not NULL, then *SRCEND is set to the next byte after the + last byte used in SRC. */ grub_ssize_t -grub_utf8_to_ucs4 (grub_uint32_t *dest, const grub_uint8_t *src, - grub_size_t size) +grub_utf8_to_ucs4 (grub_uint32_t *dest, grub_size_t destsize, + const grub_uint8_t *src, grub_size_t srcsize, + const grub_uint8_t **srcend) { grub_uint32_t *p =3D dest; int count =3D 0; grub_uint32_t code =3D 0; =20 - while (size--) + if (*srcend) + *srcend =3D src; + + while (srcsize && destsize) { grub_uint32_t c =3D *src++; - =20 + if (srcsize !=3D -1) + srcsize--; if (count) { if ((c & 0xc0) !=3D 0x80) @@ -983,6 +990,8 @@ } else { + if (c =3D=3D 0) + break; /* NUL character terminates the string. */ if ((c & 0x80) =3D=3D 0x00) code =3D c; else if ((c & 0xe0) =3D=3D 0xc0) @@ -1011,14 +1020,18 @@ code =3D c & 0x01; } else - /* invalid */ return -1; } =20 if (count =3D=3D 0) - *p++ =3D code; + { + *p++ =3D code; + destsize--; + } } =20 + if (srcend) + *srcend =3D src; return p - dest; } =20 =3D=3D=3D modified file 'kern/term.c' --- kern/term.c 2007-12-25 11:10:47 +0000 +++ kern/term.c 2008-10-30 18:24:32 +0000 @@ -153,7 +153,7 @@ grub_ssize_t ret; =20 buf[size++] =3D c; - ret =3D grub_utf8_to_ucs4 (&code, buf, size); + ret =3D grub_utf8_to_ucs4 (&code, 1, buf, size, 0); =20 if (ret > 0) { =3D=3D=3D modified file 'normal/menu.c' --- normal/menu.c 2008-08-31 00:48:14 +0000 +++ normal/menu.c 2008-10-30 18:43:35 +0000 @@ -117,19 +117,21 @@ { int x; const char *title; + grub_size_t title_len; grub_ssize_t len; grub_uint32_t *unicode_title; grub_ssize_t i; grub_uint8_t old_color_normal, old_color_highlight; =20 title =3D entry ? entry->title : ""; - unicode_title =3D grub_malloc (grub_strlen (title) * sizeof (*unicode_ti= tle)); + title_len =3D grub_strlen (title); + unicode_title =3D grub_malloc (title_len * sizeof (*unicode_title)); if (! unicode_title) /* XXX How to show this error? */ return; =20 - len =3D grub_utf8_to_ucs4 (unicode_title, (grub_uint8_t *) title, - grub_strlen (title)); + len =3D grub_utf8_to_ucs4 (unicode_title, title_len, + (grub_uint8_t *) title, -1, 0); if (len < 0) { /* It is an invalid sequence. */ =3D=3D=3D modified file 'term/gfxterm.c' --- term/gfxterm.c 2008-08-31 03:08:13 +0000 +++ term/gfxterm.c 2008-10-30 18:24:32 +0000 @@ -35,9 +35,6 @@ #define DEFAULT_VIDEO_HEIGHT 480 #define DEFAULT_VIDEO_FLAGS 0 =20 -#define DEFAULT_CHAR_WIDTH 8 -#define DEFAULT_CHAR_HEIGHT 16 - #define DEFAULT_BORDER_WIDTH 10 =20 #define DEFAULT_STANDARD_COLOR 0x07 @@ -91,6 +88,9 @@ unsigned int cursor_y; int cursor_state; =20 + /* Font settings. */ + grub_font_t font; + /* Terminal color settings. */ grub_uint8_t standard_color_setting; grub_uint8_t normal_color_setting; @@ -169,18 +169,25 @@ =20 static grub_err_t grub_virtual_screen_setup (unsigned int x, unsigned int y, - unsigned int width, unsigned int height) + unsigned int width, unsigned int height, + const char *font_name) { /* Free old virtual screen. */ grub_virtual_screen_free (); =20 /* Initialize with default data. */ + virtual_screen.font =3D grub_font_get (font_name); + if (!virtual_screen.font) + return grub_error (GRUB_ERR_BAD_FONT, + "No font loaded."); virtual_screen.width =3D width; virtual_screen.height =3D height; virtual_screen.offset_x =3D x; virtual_screen.offset_y =3D y; - virtual_screen.char_width =3D DEFAULT_CHAR_WIDTH; - virtual_screen.char_height =3D DEFAULT_CHAR_HEIGHT; + virtual_screen.char_width =3D=20 + grub_font_get_max_char_width (virtual_screen.font); + virtual_screen.char_height =3D=20 + grub_font_get_max_char_height (virtual_screen.font); virtual_screen.cursor_x =3D 0; virtual_screen.cursor_y =3D 0; virtual_screen.cursor_state =3D 1; @@ -226,6 +233,7 @@ static grub_err_t grub_gfxterm_init (void) { + char *font_name; char *modevar; int width =3D DEFAULT_VIDEO_WIDTH; int height =3D DEFAULT_VIDEO_HEIGHT; @@ -233,6 +241,11 @@ int flags =3D DEFAULT_VIDEO_FLAGS; grub_video_color_t color; =20 + /* Select the font to use. */ + font_name =3D grub_env_get ("gfxterm_font"); + if (!font_name) + font_name =3D ""; /* Allow fallback to any font. */ + /* Parse gfxmode environment variable if set. */ modevar =3D grub_env_get ("gfxmode"); if (modevar) @@ -472,7 +485,7 @@ =20 /* Create virtual screen. */ if (grub_virtual_screen_setup (DEFAULT_BORDER_WIDTH, DEFAULT_BORDER_WIDT= H, - width, height) !=3D GRUB_ERR_NONE) + width, height, font_name) !=3D GRUB_ERR_N= ONE) { grub_video_restore (); return grub_errno; @@ -658,11 +671,12 @@ write_char (void) { struct grub_colored_char *p; - struct grub_font_glyph glyph; + struct grub_font_glyph *glyph; grub_video_color_t color; grub_video_color_t bgcolor; unsigned int x; unsigned int y; + int ascent; =20 /* Find out active character. */ p =3D (virtual_screen.text_buffer @@ -672,7 +686,8 @@ p -=3D p->index; =20 /* Get glyph for character. */ - grub_font_get_glyph (p->code, &glyph); + glyph =3D grub_font_get_glyph (virtual_screen.font, p->code); + ascent =3D grub_font_get_ascent (virtual_screen.font); =20 color =3D p->fg_color; bgcolor =3D p->bg_color; @@ -682,13 +697,13 @@ =20 /* Render glyph to text layer. */ grub_video_set_active_render_target (text_layer); - grub_video_fill_rect (bgcolor, x, y, glyph.width, glyph.height); - grub_video_blit_glyph (&glyph, color, x, y); + grub_video_fill_rect (bgcolor, x, y, glyph->width, glyph->height); + grub_font_draw_glyph (glyph, color, x, y + ascent); grub_video_set_active_render_target (GRUB_VIDEO_RENDER_TARGET_DISPLAY); =20 /* Mark character to be drawn. */ dirty_region_add (virtual_screen.offset_x + x, virtual_screen.offset_y += y, - glyph.width, glyph.height); + glyph->width, glyph->height); } =20 static void @@ -702,7 +717,8 @@ =20 /* Determine cursor properties and position on text layer. */ x =3D virtual_screen.cursor_x * virtual_screen.char_width; - y =3D ((virtual_screen.cursor_y + 1) * virtual_screen.char_height) - 3; = =20 + y =3D (virtual_screen.cursor_y * virtual_screen.char_height =20 + + grub_font_get_ascent (virtual_screen.font)); width =3D virtual_screen.char_width; height =3D 2; =20 @@ -819,14 +835,18 @@ } else { - struct grub_font_glyph glyph; + struct grub_font_glyph *glyph; struct grub_colored_char *p; + unsigned char_width; =20 /* Get properties of the character. */ =20 - grub_font_get_glyph (c, &glyph); + glyph =3D grub_font_get_glyph (virtual_screen.font, c); + + /* TODO Fix wide characters. Bi-width font support? */ + char_width =3D 1; =20 /* If we are about to exceed line length, wrap to next line. */ - if (virtual_screen.cursor_x + glyph.char_width > virtual_screen.colu= mns) + if (virtual_screen.cursor_x + char_width > virtual_screen.columns) grub_putchar ('\n'); =20 /* Find position on virtual screen, and fill information. */ @@ -836,18 +856,18 @@ p->code =3D c; p->fg_color =3D virtual_screen.fg_color; p->bg_color =3D virtual_screen.bg_color; - p->width =3D glyph.char_width - 1; + p->width =3D char_width - 1; p->index =3D 0; =20 /* If we have large glyph, add fixup info. */ - if (glyph.char_width > 1) + if (char_width > 1) { unsigned i; =20 - for (i =3D 1; i < glyph.char_width; i++) + for (i =3D 1; i < char_width; i++) { p[i].code =3D ' '; - p[i].width =3D glyph.char_width - 1; + p[i].width =3D char_width - 1; p[i].index =3D i; } } @@ -856,7 +876,7 @@ write_char (); =20 /* Make sure we scroll screen when needed and wrap line correctly. = */ - virtual_screen.cursor_x +=3D glyph.char_width; + virtual_screen.cursor_x +=3D char_width; if (virtual_screen.cursor_x >=3D virtual_screen.columns) { virtual_screen.cursor_x =3D 0; @@ -876,11 +896,16 @@ static grub_ssize_t grub_gfxterm_getcharwidth (grub_uint32_t c) { - struct grub_font_glyph glyph; - - grub_font_get_glyph (c, &glyph); - - return glyph.char_width; +#if 0 + struct grub_font_glyph *glyph; + + glyph =3D grub_font_get_glyph (c); + + return glyph->char_width; +#else + (void) c; /* Prevent warning. */ + return 1; /* TODO Fix wide characters. */ +#endif } =20 static grub_uint16_t =3D=3D=3D modified file 'term/i386/pc/vesafb.c' --- term/i386/pc/vesafb.c 2007-12-30 08:52:06 +0000 +++ term/i386/pc/vesafb.c 2008-10-30 18:24:32 +0000 @@ -250,10 +250,11 @@ break; =20 default: - return grub_font_get_glyph (code, bitmap, width); + return grub_font_get_glyph_any (code, bitmap, width); } } =20 + /* TODO This is wrong for the new font module. Should it be fixed? */ if (bitmap) grub_memcpy (bitmap, vga_font + code * virtual_screen.char_height, =3D=3D=3D modified file 'term/i386/pc/vga.c' --- term/i386/pc/vga.c 2008-01-21 15:48:27 +0000 +++ term/i386/pc/vga.c 2008-10-30 18:24:32 +0000 @@ -65,6 +65,7 @@ static struct colored_char text_buf[TEXT_WIDTH * TEXT_HEIGHT]; static unsigned char saved_map_mask; static int page =3D 0; +static grub_font_t font =3D 0; =20 #define SEQUENCER_ADDR_PORT 0x3C4 #define SEQUENCER_DATA_PORT 0x3C5 @@ -161,6 +162,9 @@ saved_map_mask =3D get_map_mask (); set_map_mask (0x0f); set_start_address (PAGE_OFFSET (page)); + font =3D grub_font_get (""); /* Choose any font, for now. */ + if (!font) + return grub_error (GRUB_ERR_BAD_FONT, "No font loaded."); =20 return GRUB_ERR_NONE; } @@ -185,7 +189,7 @@ write_char (void) { struct colored_char *p =3D text_buf + xpos + ypos * TEXT_WIDTH; - struct grub_font_glyph glyph; + struct grub_font_glyph *glyph; unsigned char *mem_base; unsigned plane; =20 @@ -194,7 +198,7 @@ p -=3D p->index; =20 /* Get glyph for character. */ - grub_font_get_glyph (p->code, &glyph); + glyph =3D grub_font_get_glyph (font, p->code); =20 for (plane =3D 0x01; plane <=3D 0x08; plane <<=3D 1) { @@ -208,19 +212,23 @@ y < CHAR_HEIGHT; y++, mem +=3D TEXT_WIDTH) { + /* TODO Re-implement glyph drawing for vga module. */ +#if 0 unsigned i; =20 - for (i =3D 0; i < glyph.char_width && offset < 32; i++) + unsigned char_width =3D 1; /* TODO Figure out wide characters. = */ + for (i =3D 0; i < char_width && offset < 32; i++) { unsigned char fg_mask, bg_mask; =20 - fg_mask =3D (p->fg_color & plane) ? glyph.bitmap[offset] : 0; - bg_mask =3D (p->bg_color & plane) ? ~(glyph.bitmap[offset]) : 0; + fg_mask =3D (p->fg_color & plane) ? glyph->bitmap[offset] : 0; + bg_mask =3D (p->bg_color & plane) ? ~(glyph->bitmap[offset]) : 0; offset++; =20 if (check_vga_mem (mem + i)) mem[i] =3D (fg_mask | bg_mask); } +#endif /* 0 */=20 } } =20 @@ -320,36 +328,37 @@ } else { - struct grub_font_glyph glyph; + struct grub_font_glyph *glyph; struct colored_char *p; + unsigned char_width =3D 1; =20 - grub_font_get_glyph(c, &glyph); + glyph =3D grub_font_get_glyph(font, c); =20 - if (xpos + glyph.char_width > TEXT_WIDTH) + if (xpos + char_width > TEXT_WIDTH) grub_putchar ('\n'); =20 p =3D text_buf + xpos + ypos * TEXT_WIDTH; p->code =3D c; p->fg_color =3D fg_color; p->bg_color =3D bg_color; - p->width =3D glyph.char_width - 1; + p->width =3D char_width - 1; p->index =3D 0; =20 - if (glyph.char_width > 1) + if (char_width > 1) { unsigned i; =20 - for (i =3D 1; i < glyph.char_width; i++) + for (i =3D 1; i < char_width; i++) { p[i].code =3D ' '; - p[i].width =3D glyph.char_width - 1; + p[i].width =3D char_width - 1; p[i].index =3D i; } } =20 write_char (); =20 - xpos +=3D glyph.char_width; + xpos +=3D char_width; if (xpos >=3D TEXT_WIDTH) { xpos =3D 0; @@ -381,11 +390,16 @@ static grub_ssize_t grub_vga_getcharwidth (grub_uint32_t c) { +#if 0 struct grub_font_glyph glyph; =20 - grub_font_get_glyph (c, &glyph); + glyph =3D grub_font_get_glyph (c); =20 return glyph.char_width; +#else + (void) c; /* Prevent warning. */ + return 1; /* TODO Fix wide characters? */ +#endif } =20 static grub_uint16_t =3D=3D=3D modified file 'video/i386/pc/vbe.c' --- video/i386/pc/vbe.c 2008-10-03 15:25:34 +0000 +++ video/i386/pc/vbe.c 2008-10-30 18:24:32 +0000 @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -896,6 +895,16 @@ =20 return minindex; } + else if ((render_target->mode_info.mode_type=20 + & GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP) !=3D 0) + { + if (red =3D=3D render_target->mode_info.fg_red + && green =3D=3D render_target->mode_info.fg_green + && blue =3D=3D render_target->mode_info.fg_blue) + return 1; + else + return 0; + } else { grub_uint32_t value; @@ -926,6 +935,17 @@ /* No alpha available in index color modes, just use same value as in only RGB modes. */ return grub_video_vbe_map_rgb (red, green, blue); + else if ((render_target->mode_info.mode_type=20 + & GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP) !=3D 0) + { + if (red =3D=3D render_target->mode_info.fg_red + && green =3D=3D render_target->mode_info.fg_green + && blue =3D=3D render_target->mode_info.fg_blue + && alpha =3D=3D render_target->mode_info.fg_alpha) + return 1; + else + return 0; + } else { grub_uint32_t value; @@ -988,6 +1008,24 @@ *alpha =3D framebuffer.palette[color].a; return; } + else if ((mode_info->mode_type + & GRUB_VIDEO_MODE_TYPE_1BIT_BITMAP) !=3D 0) + { + if (color & 1) + { + *red =3D mode_info->fg_red; + *green =3D mode_info->fg_green; + *blue =3D mode_info->fg_blue; + *alpha =3D mode_info->fg_alpha; + } + else + { + *red =3D mode_info->bg_red; + *green =3D mode_info->bg_green; + *blue =3D mode_info->bg_blue; + *alpha =3D mode_info->bg_alpha; + } + } else { grub_uint32_t tmp; @@ -1111,76 +1149,6 @@ return GRUB_ERR_NONE; } =20 -// TODO: Remove this method and replace with bitmap based glyphs -static grub_err_t -grub_video_vbe_blit_glyph (struct grub_font_glyph * glyph, - grub_video_color_t color, int x, int y) -{ - struct grub_video_i386_vbeblit_info target; - unsigned int width; - unsigned int charwidth; - unsigned int height; - unsigned int i; - unsigned int j; - unsigned int x_offset =3D 0; - unsigned int y_offset =3D 0; - - /* Make sure there is something to do. */ - if (x >=3D (int)render_target->viewport.width) - return GRUB_ERR_NONE; - - if (y >=3D (int)render_target->viewport.height) - return GRUB_ERR_NONE; - - /* Calculate glyph dimensions. */ - width =3D ((glyph->width + 7) / 8) * 8; - charwidth =3D width; - height =3D glyph->height; - - if (x + (int)width < 0) - return GRUB_ERR_NONE; - - if (y + (int)height < 0) - return GRUB_ERR_NONE; - - /* Do not allow drawing out of viewport. */ - if (x < 0) - { - width +=3D x; - x_offset =3D (unsigned int)-x; - x =3D 0; - } - if (y < 0) - { - height +=3D y; - y_offset =3D (unsigned int)-y; - y =3D 0; - } - - if ((x + width) > render_target->viewport.width) - width =3D render_target->viewport.width - x; - if ((y + height) > render_target->viewport.height) - height =3D render_target->viewport.height - y; - - /* Add viewport offset. */ - x +=3D render_target->viewport.x; - y +=3D render_target->viewport.y; - - /* Use vbeblit_info to encapsulate rendering. */ - target.mode_info =3D &render_target->mode_info; - target.data =3D render_target->data; - - /* Draw glyph. */ - for (j =3D 0; j < height; j++) - for (i =3D 0; i < width; i++) - if ((glyph->bitmap[((i + x_offset) / 8) - + (j + y_offset) * (charwidth / 8)] - & (1 << ((charwidth - (i + x_offset) - 1) % 8)))) - set_pixel (&target, x+i, y+j, color); - - return GRUB_ERR_NONE; -} - /* NOTE: This function assumes that given coordinates are within bounds of handled data. */ static void @@ -1804,7 +1772,6 @@ .map_rgba =3D grub_video_vbe_map_rgba, .unmap_color =3D grub_video_vbe_unmap_color, .fill_rect =3D grub_video_vbe_fill_rect, - .blit_glyph =3D grub_video_vbe_blit_glyph, .blit_bitmap =3D grub_video_vbe_blit_bitmap, .blit_render_target =3D grub_video_vbe_blit_render_target, .scroll =3D grub_video_vbe_scroll, =3D=3D=3D modified file 'video/i386/pc/vbeutil.c' --- video/i386/pc/vbeutil.c 2007-07-21 22:32:33 +0000 +++ video/i386/pc/vbeutil.c 2008-10-30 18:24:32 +0000 @@ -52,6 +52,11 @@ + y * source->mode_info->pitch + x; break; + + /* case 1: */ + /* For 1-bit bitmaps, addressing needs to be done at the bit level + * and it doesn't make sense, in general, to ask for a pointer + * to a particular pixel's data. */ } =20 return ptr; @@ -86,6 +91,17 @@ color =3D *(grub_uint8_t *)get_data_ptr (source, x, y); break; =20 + case 1: + if (source->mode_info->blit_format =3D=3D GRUB_VIDEO_BLIT_FORMAT_1BI= T_PACKED) + { + int bit_index =3D y * source->mode_info->width + x; + grub_uint8_t *ptr =3D (grub_uint8_t *)source->data + + bit_index / 8; + int bit_pos =3D 7 - bit_index % 8; + color =3D (*ptr >> bit_pos) & 0x01; + } + break; + default: break; } @@ -143,6 +159,17 @@ } break; =20 + case 1: + if (source->mode_info->blit_format =3D=3D GRUB_VIDEO_BLIT_FORMAT_1BI= T_PACKED) + { + int bit_index =3D y * source->mode_info->width + x; + grub_uint8_t *ptr =3D (grub_uint8_t *)source->data + + bit_index / 8; + int bit_pos =3D 7 - bit_index % 8; + *ptr =3D (*ptr & ~(1 << bit_pos)) | ((color & 0x01) << bit_pos); + } + break; + default: break; } =3D=3D=3D modified file 'video/video.c' --- video/video.c 2008-10-03 15:25:34 +0000 +++ video/video.c 2008-10-30 18:24:32 +0000 @@ -336,17 +336,6 @@ return grub_video_adapter_active->fill_rect (color, x, y, width, height); } =20 -/* Blit glyph to screen using specified color. */ -grub_err_t -grub_video_blit_glyph (struct grub_font_glyph *glyph, - grub_video_color_t color, int x, int y) -{ - if (! grub_video_adapter_active) - return grub_error (GRUB_ERR_BAD_DEVICE, "No video mode activated"); - - return grub_video_adapter_active->blit_glyph (glyph, color, x, y); -} - /* Blit bitmap to screen. */ grub_err_t grub_video_blit_bitmap (struct grub_video_bitmap *bitmap, --MP_/gl5hFA.k5o19toXYw/TFnHO-- --Sig_/5JJPp8f2UUZ.fQ3Lqt3ogW+ Content-Type: application/pgp-signature; name=signature.asc Content-Disposition: attachment; filename=signature.asc -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.9 (GNU/Linux) iEYEARECAAYFAkkKgjQACgkQokx8fzcGbYeHogCdEABi3ngVLZlSLpt3MUflqPte U50An240EDlZsz1w09AD/7Bzm3Q9KS+P =t+6+ -----END PGP SIGNATURE----- --Sig_/5JJPp8f2UUZ.fQ3Lqt3ogW+--