grub-gen-asciih.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2009,2010 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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <ft2build.h>
  23. #include FT_FREETYPE_H
  24. #include FT_TRUETYPE_TAGS_H
  25. #include FT_TRUETYPE_TABLES_H
  26. #include FT_SYNTHESIS_H
  27. #undef __FTERRORS_H__
  28. #define FT_ERROR_START_LIST const char *ft_errmsgs[] = {
  29. #define FT_ERRORDEF(e, v, s) [e] = s,
  30. #define FT_ERROR_END_LIST };
  31. #include FT_ERRORS_H
  32. #define GRUB_FONT_DEFAULT_SIZE 16
  33. #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
  34. struct grub_glyph_info
  35. {
  36. int width;
  37. int height;
  38. int x_ofs;
  39. int y_ofs;
  40. int device_width;
  41. int bitmap_size;
  42. unsigned char *bitmap;
  43. };
  44. static void
  45. add_pixel (unsigned char **data, int *mask, int not_blank)
  46. {
  47. if (*mask == 0)
  48. {
  49. (*data)++;
  50. **data = 0;
  51. *mask = 128;
  52. }
  53. if (not_blank)
  54. **data |= *mask;
  55. *mask >>= 1;
  56. }
  57. static void
  58. add_glyph (FT_UInt glyph_idx, FT_Face face,
  59. unsigned int char_code,
  60. struct grub_glyph_info *glyph_info)
  61. {
  62. int width, height;
  63. int cuttop, cutbottom, cutleft, cutright;
  64. unsigned char *data;
  65. int mask, i, j, bitmap_size;
  66. FT_GlyphSlot glyph;
  67. int flag = FT_LOAD_RENDER | FT_LOAD_MONOCHROME;
  68. FT_Error err;
  69. err = FT_Load_Glyph (face, glyph_idx, flag);
  70. if (err)
  71. {
  72. fprintf (stderr, "Freetype Error %d loading glyph 0x%x for U+0x%x",
  73. err, glyph_idx, char_code);
  74. if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
  75. fprintf (stderr, ": %s\n", ft_errmsgs[err]);
  76. else
  77. fprintf (stderr, "\n");
  78. exit (1);
  79. }
  80. glyph = face->glyph;
  81. if (glyph->next)
  82. printf ("%x\n", char_code);
  83. cuttop = cutbottom = cutleft = cutright = 0;
  84. width = glyph->bitmap.width;
  85. height = glyph->bitmap.rows;
  86. bitmap_size = ((width * height + 7) / 8);
  87. glyph_info->bitmap = malloc (bitmap_size);
  88. if (!glyph_info->bitmap)
  89. {
  90. fprintf (stderr, "grub-gen-asciih: error: out of memory");
  91. exit (1);
  92. }
  93. glyph_info->bitmap_size = bitmap_size;
  94. glyph_info->width = width;
  95. glyph_info->height = height;
  96. glyph_info->x_ofs = glyph->bitmap_left;
  97. glyph_info->y_ofs = glyph->bitmap_top - height;
  98. glyph_info->device_width = glyph->metrics.horiAdvance / 64;
  99. mask = 0;
  100. data = &glyph_info->bitmap[0] - 1;
  101. for (j = cuttop; j < height + cuttop; j++)
  102. for (i = cutleft; i < width + cutleft; i++)
  103. add_pixel (&data, &mask,
  104. glyph->bitmap.buffer[i / 8 + j * glyph->bitmap.pitch] &
  105. (1 << (7 - (i & 7))));
  106. }
  107. static void
  108. write_font_ascii_bitmap (FILE *file, FT_Face face)
  109. {
  110. int char_code;
  111. fprintf (file, "/* THIS CHUNK OF BYTES IS AUTOMATICALLY GENERATED */\n");
  112. fprintf (file, "unsigned char ascii_bitmaps[] =\n");
  113. fprintf (file, "{\n");
  114. for (char_code = 0; char_code <= 0x7f; char_code++)
  115. {
  116. FT_UInt glyph_idx;
  117. struct grub_glyph_info glyph;
  118. glyph_idx = FT_Get_Char_Index (face, char_code);
  119. if (!glyph_idx)
  120. return;
  121. memset (&glyph, 0, sizeof(glyph));
  122. add_glyph (glyph_idx, face, char_code, &glyph);
  123. if (glyph.width == 8 && glyph.height == 16
  124. && glyph.x_ofs == 0 && glyph.y_ofs == 0)
  125. {
  126. int row;
  127. for (row = 0; row < 16; row++)
  128. fprintf (file, "0x%02x, ", glyph.bitmap[row]);
  129. }
  130. else
  131. {
  132. unsigned char glph[16];
  133. int p = 0, mask = 0x80;
  134. int row, col;
  135. int dy = 12 - glyph.height - glyph.y_ofs;
  136. for (row = 0; row < 16; row++)
  137. glph[row] = 0;
  138. for (row = 0; row < glyph.height; row++)
  139. for (col = 0; col < glyph.width; col++)
  140. {
  141. int val = glyph.bitmap[p] & mask;
  142. mask >>= 1;
  143. if (mask == 0)
  144. {
  145. mask = 0x80;
  146. p++;
  147. }
  148. if (val && dy + row >= 0
  149. && dy + row < 16
  150. && glyph.x_ofs + col >= 0
  151. && glyph.x_ofs + col < 8)
  152. glph[dy + row] |= 1 << (7 - (glyph.x_ofs + col));
  153. }
  154. for (row = 0; row < 16; row++)
  155. fprintf (file, "0x%02x, ", glph[row]);
  156. }
  157. fprintf (file, "\n");
  158. free (glyph.bitmap);
  159. }
  160. fprintf (file, "};\n");
  161. }
  162. int
  163. main (int argc, char *argv[])
  164. {
  165. FT_Library ft_lib;
  166. FT_Face ft_face;
  167. FILE *file;
  168. if (argc != 3)
  169. {
  170. fprintf (stderr, "grub-gen-asciih: usage: INPUT OUTPUT");
  171. return 1;
  172. }
  173. if (FT_Init_FreeType (&ft_lib))
  174. {
  175. fprintf (stderr, "grub-gen-asciih: error: FT_Init_FreeType fails");
  176. return 1;
  177. }
  178. {
  179. int size;
  180. FT_Error err;
  181. err = FT_New_Face (ft_lib, argv[1], 0, &ft_face);
  182. if (err)
  183. {
  184. fprintf (stderr, "can't open file %s, index %d: error %d",
  185. argv[1], 0, err);
  186. if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
  187. fprintf (stderr, ": %s\n", ft_errmsgs[err]);
  188. else
  189. fprintf (stderr, "\n");
  190. return 1;
  191. }
  192. if ((ft_face->face_flags & FT_FACE_FLAG_SCALABLE) ||
  193. (! ft_face->num_fixed_sizes))
  194. size = GRUB_FONT_DEFAULT_SIZE;
  195. else
  196. size = ft_face->available_sizes[0].height;
  197. if (FT_Set_Pixel_Sizes (ft_face, size, size))
  198. {
  199. fprintf (stderr, "grub-gen-asciih: error: can't set %dx%d font size", size, size);
  200. return 1;
  201. }
  202. }
  203. file = fopen (argv[2], "w");
  204. if (! file)
  205. {
  206. fprintf (stderr, "grub-gen-asciih: error: cannot write to `%s': %s", argv[2],
  207. strerror (errno));
  208. return 1;
  209. }
  210. write_font_ascii_bitmap (file, ft_face);
  211. fclose (file);
  212. FT_Done_Face (ft_face);
  213. FT_Done_FreeType (ft_lib);
  214. return 0;
  215. }