Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / render-label.c
blob39663a8a714f943d406b1f88aa705be44dd6dfb2
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
22 #include <grub/util/misc.h>
23 #include <grub/util/install.h>
24 #include <grub/i18n.h>
25 #include <grub/term.h>
26 #include <grub/font.h>
27 #include <grub/gfxmenu_view.h>
28 #include <grub/color.h>
29 #include <grub/emu/hostdisk.h>
31 #define _GNU_SOURCE 1
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <errno.h>
39 struct header
41 grub_uint8_t magic;
42 grub_uint16_t width;
43 grub_uint16_t height;
44 } GRUB_PACKED;
46 static struct grub_video_palette_data ieee1275_palette[256];
48 void
49 grub_util_render_label (const char *label_font,
50 const char *label_bgcolor,
51 const char *label_color,
52 const char *text,
53 const char *output)
55 struct header head;
56 FILE *out;
57 int i, j, k, cptr = 0;
58 grub_font_t font;
59 char *fontfull;
60 const grub_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
61 const grub_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
62 0x77, 0x66, 0x3f, 0x27 };
63 int width, height;
64 grub_uint8_t bg, fg;
65 struct grub_video_mode_info mode_info;
66 grub_err_t err;
67 grub_video_rgba_color_t fgcolor;
68 grub_video_rgba_color_t bgcolor;
70 if (output)
71 out = grub_util_fopen (output, "wb");
72 else
73 out = stdout;
74 if (!out)
76 grub_util_error (_("cannot open `%s': %s"), output ? : "stdout",
77 strerror (errno));
80 if (label_color)
82 err = grub_video_parse_color (label_color, &fgcolor);
83 if (err)
84 grub_util_error (_("invalid color specification `%s'"), label_color);
86 else
88 fgcolor.red = 0x00;
89 fgcolor.green = 0x00;
90 fgcolor.blue = 0x00;
91 fgcolor.alpha = 0xff;
94 if (label_bgcolor)
96 err = grub_video_parse_color (label_bgcolor, &bgcolor);
97 if (err)
98 grub_util_error (_("invalid color specification `%s'"), label_bgcolor);
100 else
102 bgcolor.red = 0xff;
103 bgcolor.green = 0xff;
104 bgcolor.blue = 0xff;
105 bgcolor.alpha = 0xff;
108 for (i = 0; i < 256; i++)
109 ieee1275_palette[i].a = 0xff;
111 for (i = 0; i < 6; i++)
112 for (j = 0; j < 6; j++)
113 for (k = 0; k < 6; k++)
115 ieee1275_palette[cptr].r = vals[i];
116 ieee1275_palette[cptr].g = vals[j];
117 ieee1275_palette[cptr].b = vals[k];
118 ieee1275_palette[cptr].a = 0xff;
119 cptr++;
121 cptr--;
122 for (i = 0; i < 10; i++)
124 ieee1275_palette[cptr].r = vals2[i];
125 ieee1275_palette[cptr].g = 0;
126 ieee1275_palette[cptr].b = 0;
127 ieee1275_palette[cptr].a = 0xff;
128 cptr++;
130 for (i = 0; i < 10; i++)
132 ieee1275_palette[cptr].r = 0;
133 ieee1275_palette[cptr].g = vals2[i];
134 ieee1275_palette[cptr].b = 0;
135 ieee1275_palette[cptr].a = 0xff;
136 cptr++;
138 for (i = 0; i < 10; i++)
140 ieee1275_palette[cptr].r = 0;
141 ieee1275_palette[cptr].g = 0;
142 ieee1275_palette[cptr].b = vals2[i];
143 ieee1275_palette[cptr].a = 0xff;
144 cptr++;
146 for (i = 0; i < 10; i++)
148 ieee1275_palette[cptr].r = vals2[i];
149 ieee1275_palette[cptr].g = vals2[i];
150 ieee1275_palette[cptr].b = vals2[i];
151 ieee1275_palette[cptr].a = 0xff;
152 cptr++;
154 ieee1275_palette[cptr].r = 0;
155 ieee1275_palette[cptr].g = 0;
156 ieee1275_palette[cptr].b = 0;
157 ieee1275_palette[cptr].a = 0xff;
159 char * t;
160 t = canonicalize_file_name (label_font);
161 if (!t)
163 grub_util_error (_("cannot open `%s': %s"), label_font,
164 strerror (errno));
167 fontfull = xasprintf ("(host)/%s", t);
168 free (t);
170 grub_font_loader_init ();
171 font = grub_font_load (fontfull);
172 if (!font)
174 grub_util_error (_("cannot open `%s': %s"), label_font,
175 grub_errmsg);
178 width = grub_font_get_string_width (font, text) + 10;
179 height = grub_font_get_height (font);
181 mode_info.width = width;
182 mode_info.height = height;
183 mode_info.pitch = width;
185 mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
186 mode_info.bpp = 8;
187 mode_info.bytes_per_pixel = 1;
188 mode_info.number_of_colors = 256;
190 grub_video_capture_start (&mode_info, ieee1275_palette,
191 ARRAY_SIZE (ieee1275_palette));
193 fg = grub_video_map_rgb (fgcolor.red,
194 fgcolor.green,
195 fgcolor.blue);
196 bg = grub_video_map_rgb (bgcolor.red,
197 bgcolor.green,
198 bgcolor.blue);
200 grub_memset (grub_video_capture_get_framebuffer (), bg, height * width);
201 grub_font_draw_string (text, font, fg,
202 5, grub_font_get_ascent (font));
204 head.magic = 1;
205 head.width = grub_cpu_to_be16 (width);
206 head.height = grub_cpu_to_be16 (height);
207 fwrite (&head, 1, sizeof (head), out);
208 fwrite (grub_video_capture_get_framebuffer (), 1, width * height, out);
210 grub_video_capture_end ();
211 if (out != stdout)
212 fclose (out);
214 free (fontfull);