render-label.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2012,2013 Free Software Foundation, Inc.
  4. *
  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.
  9. *
  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.
  14. *
  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/>.
  17. */
  18. #include <config.h>
  19. #include <grub/util/misc.h>
  20. #include <grub/util/install.h>
  21. #include <grub/i18n.h>
  22. #include <grub/term.h>
  23. #include <grub/font.h>
  24. #include <grub/gfxmenu_view.h>
  25. #include <grub/color.h>
  26. #include <grub/emu/hostdisk.h>
  27. #define _GNU_SOURCE 1
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <errno.h>
  33. struct header
  34. {
  35. grub_uint8_t magic;
  36. grub_uint16_t width;
  37. grub_uint16_t height;
  38. } GRUB_PACKED;
  39. static struct grub_video_palette_data ieee1275_palette[256];
  40. void
  41. grub_util_render_label (const char *label_font,
  42. const char *label_bgcolor,
  43. const char *label_color,
  44. const char *text,
  45. const char *output)
  46. {
  47. struct header head;
  48. FILE *out;
  49. int i, j, k, cptr = 0;
  50. grub_font_t font;
  51. char *fontfull;
  52. const grub_uint8_t vals[] = { 0xff, 0xda, 0xb3, 0x87, 0x54, 0x00 };
  53. const grub_uint8_t vals2[] = { 0xf3, 0xe7, 0xcd, 0xc0, 0xa5, 0x96,
  54. 0x77, 0x66, 0x3f, 0x27 };
  55. int width, height;
  56. grub_uint8_t bg, fg;
  57. struct grub_video_mode_info mode_info;
  58. grub_err_t err;
  59. grub_video_rgba_color_t fgcolor;
  60. grub_video_rgba_color_t bgcolor;
  61. if (output)
  62. out = grub_util_fopen (output, "wb");
  63. else
  64. out = stdout;
  65. if (!out)
  66. {
  67. grub_util_error (_("cannot open `%s': %s"), output ? : "stdout",
  68. strerror (errno));
  69. }
  70. if (label_color)
  71. {
  72. err = grub_video_parse_color (label_color, &fgcolor);
  73. if (err)
  74. grub_util_error (_("invalid color specification `%s'"), label_color);
  75. }
  76. else
  77. {
  78. fgcolor.red = 0x00;
  79. fgcolor.green = 0x00;
  80. fgcolor.blue = 0x00;
  81. fgcolor.alpha = 0xff;
  82. }
  83. if (label_bgcolor)
  84. {
  85. err = grub_video_parse_color (label_bgcolor, &bgcolor);
  86. if (err)
  87. grub_util_error (_("invalid color specification `%s'"), label_bgcolor);
  88. }
  89. else
  90. {
  91. bgcolor.red = 0xff;
  92. bgcolor.green = 0xff;
  93. bgcolor.blue = 0xff;
  94. bgcolor.alpha = 0xff;
  95. }
  96. for (i = 0; i < 256; i++)
  97. ieee1275_palette[i].a = 0xff;
  98. for (i = 0; i < 6; i++)
  99. for (j = 0; j < 6; j++)
  100. for (k = 0; k < 6; k++)
  101. {
  102. ieee1275_palette[cptr].r = vals[i];
  103. ieee1275_palette[cptr].g = vals[j];
  104. ieee1275_palette[cptr].b = vals[k];
  105. ieee1275_palette[cptr].a = 0xff;
  106. cptr++;
  107. }
  108. cptr--;
  109. for (i = 0; i < 10; i++)
  110. {
  111. ieee1275_palette[cptr].r = vals2[i];
  112. ieee1275_palette[cptr].g = 0;
  113. ieee1275_palette[cptr].b = 0;
  114. ieee1275_palette[cptr].a = 0xff;
  115. cptr++;
  116. }
  117. for (i = 0; i < 10; i++)
  118. {
  119. ieee1275_palette[cptr].r = 0;
  120. ieee1275_palette[cptr].g = vals2[i];
  121. ieee1275_palette[cptr].b = 0;
  122. ieee1275_palette[cptr].a = 0xff;
  123. cptr++;
  124. }
  125. for (i = 0; i < 10; i++)
  126. {
  127. ieee1275_palette[cptr].r = 0;
  128. ieee1275_palette[cptr].g = 0;
  129. ieee1275_palette[cptr].b = vals2[i];
  130. ieee1275_palette[cptr].a = 0xff;
  131. cptr++;
  132. }
  133. for (i = 0; i < 10; i++)
  134. {
  135. ieee1275_palette[cptr].r = vals2[i];
  136. ieee1275_palette[cptr].g = vals2[i];
  137. ieee1275_palette[cptr].b = vals2[i];
  138. ieee1275_palette[cptr].a = 0xff;
  139. cptr++;
  140. }
  141. ieee1275_palette[cptr].r = 0;
  142. ieee1275_palette[cptr].g = 0;
  143. ieee1275_palette[cptr].b = 0;
  144. ieee1275_palette[cptr].a = 0xff;
  145. char * t;
  146. t = grub_canonicalize_file_name (label_font);
  147. if (!t)
  148. {
  149. grub_util_error (_("cannot open `%s': %s"), label_font,
  150. strerror (errno));
  151. }
  152. fontfull = xasprintf ("(host)/%s", t);
  153. free (t);
  154. grub_font_loader_init ();
  155. font = grub_font_load (fontfull);
  156. if (!font)
  157. {
  158. grub_util_error (_("cannot open `%s': %s"), label_font,
  159. grub_errmsg);
  160. }
  161. width = grub_font_get_string_width (font, text) + 10;
  162. height = grub_font_get_height (font);
  163. mode_info.width = width;
  164. mode_info.height = height;
  165. mode_info.pitch = width;
  166. mode_info.mode_type = GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
  167. mode_info.bpp = 8;
  168. mode_info.bytes_per_pixel = 1;
  169. mode_info.number_of_colors = 256;
  170. grub_video_capture_start (&mode_info, ieee1275_palette,
  171. ARRAY_SIZE (ieee1275_palette));
  172. fg = grub_video_map_rgb (fgcolor.red,
  173. fgcolor.green,
  174. fgcolor.blue);
  175. bg = grub_video_map_rgb (bgcolor.red,
  176. bgcolor.green,
  177. bgcolor.blue);
  178. grub_memset (grub_video_capture_get_framebuffer (), bg, height * width);
  179. grub_font_draw_string (text, font, fg,
  180. 5, grub_font_get_ascent (font));
  181. head.magic = 1;
  182. head.width = grub_cpu_to_be16 (width);
  183. head.height = grub_cpu_to_be16 (height);
  184. fwrite (&head, 1, sizeof (head), out);
  185. fwrite (grub_video_capture_get_framebuffer (), 1, width * height, out);
  186. grub_video_capture_end ();
  187. if (out != stdout)
  188. fclose (out);
  189. free (fontfull);
  190. }