Hello,

Find attached a way to macrofy the constants from font files between
util and grub kernel.

Comments, name changes, etc. are welcomed.

-- 
Carles Pina i Estany
        http://pinux.info
=== modified file 'ChangeLog'
--- ChangeLog	2010-01-25 17:04:22 +0000
+++ ChangeLog	2010-01-25 23:03:56 +0000
@@ -1,3 +1,12 @@
+2010-01-25  Carles Pina i Estany  <car...@pina.cat>
+
+	* font/font.c: Include `grub/fontformat.h.
+	Remove font file format constants.
+	(grub_font_load): Use the new constant names.
+	* include/grub/fontformat.h: New file.
+	* util/grub-mkfont.c: Include `grub/fontformat.c'.
+	(write_font_pf2): Use the new macros instead of hardcoded literals.
+
 2010-01-25  Colin Watson  <cjwat...@ubuntu.com>
 
 	* util/hostdisk.c (open_device): Add trailing newline to debug

=== modified file 'font/font.c'
--- font/font.c	2010-01-20 20:53:43 +0000
+++ font/font.c	2010-01-25 23:04:38 +0000
@@ -26,6 +26,7 @@
 #include <grub/types.h>
 #include <grub/video.h>
 #include <grub/bitmap.h>
+#include <grub/fontformat.h>
 
 #ifdef USE_ASCII_FAILBACK
 #include "ascii.h"
@@ -89,19 +90,6 @@ struct font_file_section
   int eof;
 };
 
-/* Font file format constants.  */
-static const char pff2_magic[4] = { 'P', 'F', 'F', '2' };
-static const char section_names_file[4] = { 'F', 'I', 'L', 'E' };
-static const char section_names_font_name[4] = { 'N', 'A', 'M', 'E' };
-static const char section_names_point_size[4] = { 'P', 'T', 'S', 'Z' };
-static const char section_names_weight[4] = { 'W', 'E', 'I', 'G' };
-static const char section_names_max_char_width[4] = { 'M', 'A', 'X', 'W' };
-static const char section_names_max_char_height[4] = { 'M', 'A', 'X', 'H' };
-static const char section_names_ascent[4] = { 'A', 'S', 'C', 'E' };
-static const char section_names_descent[4] = { 'D', 'E', 'S', 'C' };
-static const char section_names_char_index[4] = { 'C', 'H', 'I', 'X' };
-static const char section_names_data[4] = { 'D', 'A', 'T', 'A' };
-
 /* Replace unknown glyphs with a rounded question mark.  */
 static grub_uint8_t unknown_glyph_bitmap[] =
 {
@@ -460,7 +448,8 @@ grub_font_load (const char *filename)
 #if FONT_DEBUG >= 3
   grub_printf("opened FILE section\n");
 #endif
-  if (grub_memcmp (section.name, section_names_file, 4) != 0)
+  if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FILE,
+  		   sizeof(FONT_FORMAT_SECTION_NAMES_FILE) - 1) != 0)
     {
       grub_error (GRUB_ERR_BAD_FONT,
                   "font file format error: 1st section must be FILE");
@@ -489,7 +478,7 @@ grub_font_load (const char *filename)
   grub_printf("read magic ok\n");
 #endif
 
-  if (grub_memcmp (magic, pff2_magic, 4) != 0)
+  if (grub_memcmp (magic, FONT_FORMAT_PFF2_MAGIC, 4) != 0)
     {
       grub_error (GRUB_ERR_BAD_FONT, "invalid font magic %x %x %x %x",
                   magic[0], magic[1], magic[2], magic[3]);
@@ -529,18 +518,22 @@ grub_font_load (const char *filename)
                   section.name[2], section.name[3]);
 #endif
 
-      if (grub_memcmp (section.name, section_names_font_name, 4) == 0)
+      if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_FONT_NAME,
+      		       sizeof(FONT_FORMAT_SECTION_NAMES_FONT_NAME) - 1) == 0)
         {
           font->name = read_section_as_string (&section);
           if (!font->name)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_point_size, 4) == 0)
+      else if (grub_memcmp (section.name,
+      			    FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
+			    sizeof(FONT_FORMAT_SECTION_NAMES_POINT_SIZE) - 1) == 0)
         {
           if (read_section_as_short (&section, &font->point_size) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_weight, 4) == 0)
+      else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_WEIGHT,
+      			    sizeof(FONT_FORMAT_SECTION_NAMES_WEIGHT) - 1) == 0)
         {
           char *wt;
           wt = read_section_as_string (&section);
@@ -553,32 +546,42 @@ grub_font_load (const char *filename)
             font->weight = FONT_WEIGHT_BOLD;
           grub_free (wt);
         }
-      else if (grub_memcmp (section.name, section_names_max_char_width, 4) == 0)
+      else if (grub_memcmp (section.name,
+      	       FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
+	       sizeof(FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH) - 1) == 0)
         {
           if (read_section_as_short (&section, &font->max_char_width) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_max_char_height, 4) == 0)
+      else if (grub_memcmp (section.name,
+      	       FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
+	       sizeof(FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT) - 1) == 0)
         {
           if (read_section_as_short (&section, &font->max_char_height) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_ascent, 4) == 0)
+      else if (grub_memcmp (section.name,
+      	       FONT_FORMAT_SECTION_NAMES_ASCENT,
+	       sizeof(FONT_FORMAT_SECTION_NAMES_ASCENT) - 1) == 0)
         {
           if (read_section_as_short (&section, &font->ascent) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_descent, 4) == 0)
+      else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DESCENT,
+      			    sizeof(FONT_FORMAT_SECTION_NAMES_DESCENT) - 1) == 0)
         {
           if (read_section_as_short (&section, &font->descent) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_char_index, 4) == 0)
+      else if (grub_memcmp (section.name,
+      			    FONT_FORMAT_SECTION_NAMES_CHAR_INDEX,
+			    sizeof(FONT_FORMAT_SECTION_NAMES_CHAR_INDEX) - 1) == 0)
         {
           if (load_font_index (file, section.length, font) != 0)
             goto fail;
         }
-      else if (grub_memcmp (section.name, section_names_data, 4) == 0)
+      else if (grub_memcmp (section.name, FONT_FORMAT_SECTION_NAMES_DATA,
+      			    sizeof(FONT_FORMAT_SECTION_NAMES_DATA) - 1) == 0)
         {
           /* When the DATA section marker is reached, we stop reading.  */
           break;

=== added file 'include/grub/fontformat.h'
--- include/grub/fontformat.h	1970-01-01 00:00:00 +0000
+++ include/grub/fontformat.h	2010-01-25 22:58:28 +0000
@@ -0,0 +1,38 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GRUB_FONT_FORMAT_HEADER
+#define GRUB_FONT_FORMAT_HEADER	1
+
+/* FONT_FORMAT_PFF2_MAGIC use only 4 relevants bytes and the \0.  */
+#define FONT_FORMAT_PFF2_MAGIC "PFF2"
+#define FONT_FORMAT_SECTION_NAMES_FILE "FILE"
+#define FONT_FORMAT_SECTION_NAMES_FONT_NAME "NAME"
+#define FONT_FORMAT_SECTION_NAMES_POINT_SIZE "PTSZ"
+#define FONT_FORMAT_SECTION_NAMES_WEIGHT "WEIG"
+#define FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH "MAXW"
+#define FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT "MAXH"
+#define FONT_FORMAT_SECTION_NAMES_ASCENT "ASCE"
+#define FONT_FORMAT_SECTION_NAMES_DESCENT "DESC"
+#define FONT_FORMAT_SECTION_NAMES_CHAR_INDEX "CHIX"
+#define FONT_FORMAT_SECTION_NAMES_DATA "DATA"
+#define FONT_FORMAT_SECTION_NAMES_FAMILY "FAMI"
+#define FONT_FORMAT_SECTION_NAMES_SLAN "SLAN"
+
+#endif /* ! GRUB_FONT_FORMAT_HEADER */
+

=== modified file 'util/grub-mkfont.c'
--- util/grub-mkfont.c	2010-01-18 19:34:26 +0000
+++ util/grub-mkfont.c	2010-01-25 22:51:57 +0000
@@ -20,6 +20,7 @@
 #include <grub/types.h>
 #include <grub/util/misc.h>
 #include <grub/i18n.h>
+#include <grub/fontformat.h>
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -392,9 +393,10 @@ write_font_pf2 (struct grub_font_info *f
   offset = 0;
 
   leng = grub_cpu_to_be32 (4);
-  grub_util_write_image ("FILE", 4, file);
+  grub_util_write_image (FONT_FORMAT_SECTION_NAMES_FILE,
+  			 sizeof(FONT_FORMAT_SECTION_NAMES_FILE) - 1, file);
   grub_util_write_image ((char *) &leng, 4, file);
-  grub_util_write_image ("PFF2", 4, file);
+  grub_util_write_image (FONT_FORMAT_PFF2_MAGIC, 4, file);
   offset += 12;
 
   if (! font_info->name)
@@ -416,20 +418,25 @@ write_font_pf2 (struct grub_font_info *f
   font_name = xasprintf ("%s %s %d", font_info->name, &style_name[1],
 			 font_info->size);
 
-  write_string_section ("NAME", font_name, &offset, file);
-  write_string_section ("FAMI", font_info->name, &offset, file);
-  write_string_section ("WEIG",
+  write_string_section (FONT_FORMAT_SECTION_NAMES_FONT_NAME,
+  			font_name, &offset, file);
+  write_string_section (FONT_FORMAT_SECTION_NAMES_FAMILY,
+  			font_info->name, &offset, file);
+  write_string_section (FONT_FORMAT_SECTION_NAMES_WEIGHT,
 			(font_info->style & FT_STYLE_FLAG_BOLD) ?
 			"bold" : "normal",
 			&offset, file);
-  write_string_section ("SLAN",
+  write_string_section (FONT_FORMAT_SECTION_NAMES_SLAN,
 			(font_info->style & FT_STYLE_FLAG_ITALIC) ?
 			"italic" : "normal",
 			&offset, file);
 
-  write_be16_section ("PTSZ", font_info->size, &offset, file);
-  write_be16_section ("MAXW", font_info->max_width, &offset, file);
-  write_be16_section ("MAXH", font_info->max_height, &offset, file);
+  write_be16_section (FONT_FORMAT_SECTION_NAMES_POINT_SIZE,
+  		      font_info->size, &offset, file);
+  write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_WIDTH,
+  		      font_info->max_width, &offset, file);
+  write_be16_section (FONT_FORMAT_SECTION_NAMES_MAX_CHAR_HEIGHT,
+  		      font_info->max_height, &offset, file);
 
   if (! font_info->desc)
     {
@@ -447,8 +454,10 @@ write_font_pf2 (struct grub_font_info *f
 	font_info->asce = font_info->max_y;
     }
 
-  write_be16_section ("ASCE", font_info->asce, &offset, file);
-  write_be16_section ("DESC", font_info->desc, &offset, file);
+  write_be16_section (FONT_FORMAT_SECTION_NAMES_ASCENT,
+  		      font_info->asce, &offset, file);
+  write_be16_section (FONT_FORMAT_SECTION_NAMES_DESCENT,
+  		      font_info->desc, &offset, file);
 
   if (font_verbosity > 0)
     {
@@ -479,7 +488,9 @@ write_font_pf2 (struct grub_font_info *f
     printf ("Number of glyph: %d\n", num);
 
   leng = grub_cpu_to_be32 (num * 9);
-  grub_util_write_image ("CHIX", 4, file);
+  grub_util_write_image (FONT_FORMAT_SECTION_NAMES_CHAR_INDEX,
+  			 sizeof(FONT_FORMAT_SECTION_NAMES_CHAR_INDEX) - 1,
+			 file);
   grub_util_write_image ((char *) &leng, 4, file);
   offset += 8 + num * 9 + 8;
 
@@ -495,7 +506,7 @@ write_font_pf2 (struct grub_font_info *f
     }
 
   leng = 0xffffffff;
-  grub_util_write_image ("DATA", 4, file);
+  grub_util_write_image (FONT_FORMAT_SECTION_NAMES_DATA, 4, file);
   grub_util_write_image ((char *) &leng, 4, file);
 
   for (cur = font_info->glyph; cur; cur = cur->next)

_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to