grub-gen-widthspec.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. #define MAX_CODE 65536
  35. static unsigned char result[MAX_CODE / 8];
  36. static void
  37. add_glyph (FT_Face face,
  38. unsigned int char_code)
  39. {
  40. int flag = FT_LOAD_RENDER | FT_LOAD_MONOCHROME;
  41. FT_Error err;
  42. FT_UInt glyph_idx;
  43. glyph_idx = FT_Get_Char_Index (face, char_code);
  44. if (!glyph_idx)
  45. return;
  46. err = FT_Load_Glyph (face, glyph_idx, flag);
  47. if (err)
  48. {
  49. printf ("Freetype Error %d loading glyph 0x%x for U+0x%x",
  50. err, glyph_idx, char_code);
  51. if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
  52. printf (": %s\n", ft_errmsgs[err]);
  53. else
  54. printf ("\n");
  55. return;
  56. }
  57. if (face->glyph->bitmap.width > 12 && char_code < MAX_CODE)
  58. result[char_code >> 3] |= (1 << (char_code & 7));
  59. }
  60. int
  61. main (int argc, char *argv[])
  62. {
  63. FT_Library ft_lib;
  64. FILE *file;
  65. int i;
  66. if (argc != 3)
  67. {
  68. fprintf (stderr, "grub-gen-widthspec: usage: INPUT OUTPUT");
  69. return 1;
  70. }
  71. if (FT_Init_FreeType (&ft_lib))
  72. {
  73. fprintf (stderr, "grub-gen-widthspec: error: FT_Init_FreeType fails");
  74. return 1;
  75. }
  76. {
  77. FT_Face ft_face;
  78. int size;
  79. FT_Error err;
  80. unsigned int char_code, glyph_index;
  81. err = FT_New_Face (ft_lib, argv[1], 0, &ft_face);
  82. if (err)
  83. {
  84. fprintf (stderr, "can't open file %s, index %d: error %d",
  85. argv[1], 0, err);
  86. if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
  87. fprintf (stderr, ": %s\n", ft_errmsgs[err]);
  88. else
  89. fprintf (stderr, "\n");
  90. return 1;
  91. }
  92. if ((ft_face->face_flags & FT_FACE_FLAG_SCALABLE) ||
  93. (! ft_face->num_fixed_sizes))
  94. size = GRUB_FONT_DEFAULT_SIZE;
  95. else
  96. size = ft_face->available_sizes[0].height;
  97. if (FT_Set_Pixel_Sizes (ft_face, size, size))
  98. {
  99. fprintf (stderr, "grub-gen-widthspec: error: can't set %dx%d font size", size, size);
  100. return 1;
  101. }
  102. for (char_code = FT_Get_First_Char (ft_face, &glyph_index);
  103. glyph_index;
  104. char_code = FT_Get_Next_Char (ft_face, char_code, &glyph_index))
  105. add_glyph (ft_face, char_code);
  106. FT_Done_Face (ft_face);
  107. }
  108. FT_Done_FreeType (ft_lib);
  109. file = fopen (argv[2], "w");
  110. if (! file)
  111. {
  112. fprintf (stderr, "grub-gen-asciih: error: cannot write to `%s': %s", argv[2],
  113. strerror (errno));
  114. return 1;
  115. }
  116. fprintf (file, "/* THIS CHUNK OF BYTES IS AUTOMATICALLY GENERATED */\n");
  117. fprintf (file, "unsigned char widthspec[] =\n");
  118. fprintf (file, "{\n");
  119. for (i = 0; i < MAX_CODE / 8; i++)
  120. fprintf (file, "0x%02x,%c", result[i], ((i & 0xf) == 0xf) ? '\n' : ' ');
  121. fprintf (file, "};\n");
  122. fclose (file);
  123. return 0;
  124. }