]> git.proxmox.com Git - grub2.git/commitdiff
* util/grub-render-label.c: Move backend part to ...
authorVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 19 Oct 2013 00:34:18 +0000 (02:34 +0200)
committerVladimir 'phcoder' Serbinenko <phcoder@gmail.com>
Sat, 19 Oct 2013 00:34:18 +0000 (02:34 +0200)
* util/render-label.c: ... here.

ChangeLog
Makefile.util.def
include/grub/util/install.h
util/grub-render-label.c
util/render-label.c [new file with mode: 0644]

index c463a48d8cc6fd89764fd3ec65c6d5ea570de92e..2c0e7b9b1d3d49859521e1b091b81e38d35ff13e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2013-10-19  Vladimir Serbinenko  <phcoder@gmail.com>
+
+       * util/grub-render-label.c: Move backend part to ...
+       * util/render-label.c: ... here.
+
 2013-10-18  Vladimir Serbinenko  <phcoder@gmail.com>
 
        * grub-core/osdep/random.c: Use unix/random.c on haiku. Haiku uses
index 487675236183398b4e6258f1a85b22f546ff80d9..7400ac993cc30c98dc15249fc4fcea4dd1fc912a 100644 (file)
@@ -961,6 +961,7 @@ program = {
   mansection = 1;
 
   common = util/grub-render-label.c;
+  common = util/render-label.c;
   common = grub-core/kern/emu/argp_common.c;
   common = grub-core/kern/emu/hostfs.c;
   common = grub-core/disk/host.c;
index e12ed866760c487c913305764edc3a531083d13a..93f14eb70a6a7bfca996ebdd4bcb1dbd74e350c9 100644 (file)
@@ -79,4 +79,11 @@ grub_install_get_blocklist (grub_device_t root_dev,
 void
 grub_util_create_envblk_file (const char *name);
 
+void
+grub_util_render_label (const char *label_font,
+                       const char *label_bgcolor,
+                       const char *label_color,
+                       const char *label_string,
+                       const char *label);
+
 #endif
index 27a1c66f00c9004d0c273aff2011aaae06d9c4a7..e2ca34980a4166de80394fb2ef7e7f304c28637b 100644 (file)
@@ -25,6 +25,7 @@
 #include <grub/font.h>
 #include <grub/gfxmenu_view.h>
 #include <grub/color.h>
+#include <grub/util/install.h>
 
 #define _GNU_SOURCE    1
 
@@ -43,8 +44,8 @@ struct arguments
   char *text;
   char *output;
   char *font;
-  grub_video_rgba_color_t fgcolor;
-  grub_video_rgba_color_t bgcolor;
+  char *fgcolor;
+  char *bgcolor;
   int verbosity;
 };
 
@@ -79,7 +80,6 @@ argp_parser (int key, char *arg, struct argp_state *state)
   /* Get the input argument from argp_parse, which we
      know is a pointer to our arguments structure. */
   struct arguments *arguments = state->input;
-  grub_err_t err;
 
   switch (key)
     {
@@ -88,15 +88,11 @@ argp_parser (int key, char *arg, struct argp_state *state)
       break;
 
     case 'b':
-      err = grub_video_parse_color (arg, &arguments->bgcolor);
-      if (err)
-       grub_util_error (_("invalid color specification `%s'"), arg);
+      arguments->bgcolor = xstrdup (arg);
       break;
 
     case 'c':
-      err = grub_video_parse_color (arg, &arguments->fgcolor);
-      if (err)
-       grub_util_error (_("invalid color specification `%s'"), arg);
+      arguments->fgcolor = xstrdup (arg);
       break;
 
     case 'f':
@@ -122,104 +118,22 @@ argp_parser (int key, char *arg, struct argp_state *state)
   return 0;
 }
 
-void grub_hostfs_init (void);
-void grub_host_init (void);
-
-struct header
-{
-  grub_uint8_t magic;
-  grub_uint16_t width;
-  grub_uint16_t height;
-} __attribute__ ((packed));
-
 static struct argp argp = {
   options, argp_parser, N_("[OPTIONS]"),
   N_("Render Apple .disk_label."),
   NULL, NULL, NULL
 };
 
-static struct grub_video_palette_data ieee1275_palette[256];
-
 int
 main (int argc, char *argv[])
 {
-  FILE *out;
   char *text;
-  char *fontfull;
   struct arguments arguments;
-  grub_font_t font;
-  int width, height;
-  struct header head;
-  const grub_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
-  const grub_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
-                                0x77, 0x66, 0x3f, 0x27 };
-  int i, j, k, cptr = 0;
-  grub_uint8_t bg, fg;
-  struct grub_video_mode_info mode_info;
-
-  for (i = 0; i < 256; i++)
-    ieee1275_palette[i].a = 0xff;
-
-  for (i = 0; i < 6; i++)
-    for (j = 0; j < 6; j++)
-      for (k = 0; k < 6; k++)
-       {
-         ieee1275_palette[cptr].r = vals[i];
-         ieee1275_palette[cptr].g = vals[j];
-         ieee1275_palette[cptr].b = vals[k];
-         ieee1275_palette[cptr].a = 0xff;
-         cptr++;
-       }
-  cptr--;
-  for (i = 0; i < 10; i++)
-    {
-      ieee1275_palette[cptr].r = vals2[i];
-      ieee1275_palette[cptr].g = 0;
-      ieee1275_palette[cptr].b = 0;
-      ieee1275_palette[cptr].a = 0xff;
-      cptr++;
-    }
-  for (i = 0; i < 10; i++)
-    {
-      ieee1275_palette[cptr].r = 0;
-      ieee1275_palette[cptr].g = vals2[i];
-      ieee1275_palette[cptr].b = 0;
-      ieee1275_palette[cptr].a = 0xff;
-      cptr++;
-    }
-  for (i = 0; i < 10; i++)
-    {
-      ieee1275_palette[cptr].r = 0;
-      ieee1275_palette[cptr].g = 0;
-      ieee1275_palette[cptr].b = vals2[i];
-      ieee1275_palette[cptr].a = 0xff;
-      cptr++;
-    }
-  for (i = 0; i < 10; i++)
-    {
-      ieee1275_palette[cptr].r = vals2[i];
-      ieee1275_palette[cptr].g = vals2[i];
-      ieee1275_palette[cptr].b = vals2[i];
-      ieee1275_palette[cptr].a = 0xff;
-      cptr++;
-    }
-  ieee1275_palette[cptr].r = 0;
-  ieee1275_palette[cptr].g = 0;
-  ieee1275_palette[cptr].b = 0;
-  ieee1275_palette[cptr].a = 0xff;
 
   grub_util_host_init (&argc, &argv);
 
   /* Check for options.  */
   memset (&arguments, 0, sizeof (struct arguments));
-  arguments.bgcolor.red = 0xff;
-  arguments.bgcolor.green = 0xff;
-  arguments.bgcolor.blue = 0xff;
-  arguments.bgcolor.alpha = 0xff;
-  arguments.fgcolor.red = 0x00;
-  arguments.fgcolor.green = 0x00;
-  arguments.fgcolor.blue = 0x00;
-  arguments.fgcolor.alpha = 0xff;
   if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
     {
       fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
@@ -252,72 +166,11 @@ main (int argc, char *argv[])
       fclose (in);
     }
 
-  if (arguments.output)
-    out = grub_util_fopen (arguments.output, "wb");
-  else
-    out = stdout;
-  if (!out)
-    {
-      grub_util_error (_("cannot open `%s': %s"), arguments.output ? : "stdout",
-                      strerror (errno));
-    }
-
-  fontfull = canonicalize_file_name (arguments.font);
-  if (!fontfull)
-    {
-      grub_util_error (_("cannot open `%s': %s"), arguments.font,
-                      strerror (errno));
-    }  
-
-  fontfull = xasprintf ("(host)/%s", fontfull);
-
-  grub_init_all ();
-  grub_hostfs_init ();
-  grub_host_init ();
-
-  grub_font_loader_init ();
-  font = grub_font_load (fontfull);
-  if (!font)
-    {
-      grub_util_error (_("cannot open `%s': %s"), arguments.font,
-                      grub_errmsg);
-    }  
-
-  width = grub_font_get_string_width (font, text) + 10;
-  height = grub_font_get_height (font);
-
-  mode_info.width = width;
-  mode_info.height = height;
-  mode_info.pitch = width;
-
-  mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
-  mode_info.bpp = 8;
-  mode_info.bytes_per_pixel = 1;
-  mode_info.number_of_colors = 256;
-
-  grub_video_capture_start (&mode_info, ieee1275_palette,
-                           ARRAY_SIZE (ieee1275_palette));
-
-  fg = grub_video_map_rgb (arguments.fgcolor.red,
-                          arguments.fgcolor.green,
-                          arguments.fgcolor.blue);
-  bg = grub_video_map_rgb (arguments.bgcolor.red,
-                          arguments.bgcolor.green,
-                          arguments.bgcolor.blue);
-
-  grub_memset (grub_video_capture_get_framebuffer (), bg, height * width);
-  grub_font_draw_string (text, font, fg,
-                         5, grub_font_get_ascent (font));
-
-  head.magic = 1;
-  head.width = grub_cpu_to_be16 (width);
-  head.height = grub_cpu_to_be16 (height);
-  fwrite (&head, 1, sizeof (head), out);
-  fwrite (grub_video_capture_get_framebuffer (), 1, width * height, out);
-
-  grub_video_capture_end ();
-  if (out != stdout)
-    fclose (out);
+  grub_util_render_label (arguments.font,
+                         arguments.bgcolor,
+                         arguments.fgcolor,
+                         text,
+                         arguments.output);
 
   return 0;
 }
diff --git a/util/render-label.c b/util/render-label.c
new file mode 100644 (file)
index 0000000..316da2c
--- /dev/null
@@ -0,0 +1,221 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2010,2012,2013 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/>.
+ */
+
+
+#include <config.h>
+
+#include <grub/util/misc.h>
+#include <grub/i18n.h>
+#include <grub/term.h>
+#include <grub/font.h>
+#include <grub/gfxmenu_view.h>
+#include <grub/color.h>
+
+#define _GNU_SOURCE    1
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <argp.h>
+#include <unistd.h>
+#include <errno.h>
+
+void grub_hostfs_init (void);
+void grub_host_init (void);
+
+struct header
+{
+  grub_uint8_t magic;
+  grub_uint16_t width;
+  grub_uint16_t height;
+} __attribute__ ((packed));
+
+static struct grub_video_palette_data ieee1275_palette[256];
+
+void
+grub_util_render_label (const char *label_font,
+                       const char *label_bgcolor,
+                       const char *label_color,
+                       const char *text,
+                       const char *output)
+{
+  struct header head;
+  FILE *out;
+  int i, j, k, cptr = 0;
+  grub_font_t font;
+  char *fontfull;
+  const grub_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
+  const grub_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
+                                0x77, 0x66, 0x3f, 0x27 };
+  int width, height;
+  grub_uint8_t bg, fg;
+  struct grub_video_mode_info mode_info;
+  grub_err_t err;
+  grub_video_rgba_color_t fgcolor;
+  grub_video_rgba_color_t bgcolor;
+
+  if (output)
+    out = grub_util_fopen (output, "wb");
+  else
+    out = stdout;
+  if (!out)
+    {
+      grub_util_error (_("cannot open `%s': %s"), output ? : "stdout",
+                      strerror (errno));
+    }
+
+  if (label_color)
+    {
+      err = grub_video_parse_color (label_color, &fgcolor);
+      if (err)
+       grub_util_error (_("invalid color specification `%s'"), label_color);
+    }
+  else
+    {
+      fgcolor.red = 0x00;
+      fgcolor.green = 0x00;
+      fgcolor.blue = 0x00;
+      fgcolor.alpha = 0xff;
+    }
+
+  if (label_bgcolor)
+    {
+      err = grub_video_parse_color (label_bgcolor, &bgcolor);
+      if (err)
+       grub_util_error (_("invalid color specification `%s'"), label_bgcolor);
+    }
+  else
+    {
+      bgcolor.red = 0xff;
+      bgcolor.green = 0xff;
+      bgcolor.blue = 0xff;
+      bgcolor.alpha = 0xff;
+    }
+
+  for (i = 0; i < 256; i++)
+    ieee1275_palette[i].a = 0xff;
+
+  for (i = 0; i < 6; i++)
+    for (j = 0; j < 6; j++)
+      for (k = 0; k < 6; k++)
+       {
+         ieee1275_palette[cptr].r = vals[i];
+         ieee1275_palette[cptr].g = vals[j];
+         ieee1275_palette[cptr].b = vals[k];
+         ieee1275_palette[cptr].a = 0xff;
+         cptr++;
+       }
+  cptr--;
+  for (i = 0; i < 10; i++)
+    {
+      ieee1275_palette[cptr].r = vals2[i];
+      ieee1275_palette[cptr].g = 0;
+      ieee1275_palette[cptr].b = 0;
+      ieee1275_palette[cptr].a = 0xff;
+      cptr++;
+    }
+  for (i = 0; i < 10; i++)
+    {
+      ieee1275_palette[cptr].r = 0;
+      ieee1275_palette[cptr].g = vals2[i];
+      ieee1275_palette[cptr].b = 0;
+      ieee1275_palette[cptr].a = 0xff;
+      cptr++;
+    }
+  for (i = 0; i < 10; i++)
+    {
+      ieee1275_palette[cptr].r = 0;
+      ieee1275_palette[cptr].g = 0;
+      ieee1275_palette[cptr].b = vals2[i];
+      ieee1275_palette[cptr].a = 0xff;
+      cptr++;
+    }
+  for (i = 0; i < 10; i++)
+    {
+      ieee1275_palette[cptr].r = vals2[i];
+      ieee1275_palette[cptr].g = vals2[i];
+      ieee1275_palette[cptr].b = vals2[i];
+      ieee1275_palette[cptr].a = 0xff;
+      cptr++;
+    }
+  ieee1275_palette[cptr].r = 0;
+  ieee1275_palette[cptr].g = 0;
+  ieee1275_palette[cptr].b = 0;
+  ieee1275_palette[cptr].a = 0xff;
+
+  char * t;
+  t = canonicalize_file_name (label_font);
+  if (!t)
+    {
+      grub_util_error (_("cannot open `%s': %s"), label_font,
+                      strerror (errno));
+    }  
+
+  fontfull = xasprintf ("(host)/%s", t);
+  free (t);
+
+  grub_init_all ();
+  grub_hostfs_init ();
+  grub_host_init ();
+
+  grub_font_loader_init ();
+  font = grub_font_load (fontfull);
+  if (!font)
+    {
+      grub_util_error (_("cannot open `%s': %s"), label_font,
+                      grub_errmsg);
+    }  
+
+  width = grub_font_get_string_width (font, text) + 10;
+  height = grub_font_get_height (font);
+
+  mode_info.width = width;
+  mode_info.height = height;
+  mode_info.pitch = width;
+
+  mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
+  mode_info.bpp = 8;
+  mode_info.bytes_per_pixel = 1;
+  mode_info.number_of_colors = 256;
+
+  grub_video_capture_start (&mode_info, ieee1275_palette,
+                           ARRAY_SIZE (ieee1275_palette));
+
+  fg = grub_video_map_rgb (fgcolor.red,
+                          fgcolor.green,
+                          fgcolor.blue);
+  bg = grub_video_map_rgb (bgcolor.red,
+                          bgcolor.green,
+                          bgcolor.blue);
+
+  grub_memset (grub_video_capture_get_framebuffer (), bg, height * width);
+  grub_font_draw_string (text, font, fg,
+                         5, grub_font_get_ascent (font));
+
+  head.magic = 1;
+  head.width = grub_cpu_to_be16 (width);
+  head.height = grub_cpu_to_be16 (height);
+  fwrite (&head, 1, sizeof (head), out);
+  fwrite (grub_video_capture_get_framebuffer (), 1, width * height, out);
+
+  grub_video_capture_end ();
+  if (out != stdout)
+    fclose (out);
+
+  free (fontfull);
+}