2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2009,2010 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/>.
26 #include FT_FREETYPE_H
27 #include FT_TRUETYPE_TAGS_H
28 #include FT_TRUETYPE_TABLES_H
29 #include FT_SYNTHESIS_H
32 #define FT_ERROR_START_LIST const char *ft_errmsgs[] = {
33 #define FT_ERRORDEF(e, v, s) [e] = s,
34 #define FT_ERROR_END_LIST };
37 #define GRUB_FONT_DEFAULT_SIZE 16
39 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (array[0]))
41 #define MAX_CODE 65536
42 static unsigned char result
[MAX_CODE
/ 8];
45 add_glyph (FT_Face face
,
46 unsigned int char_code
)
48 int flag
= FT_LOAD_RENDER
| FT_LOAD_MONOCHROME
;
52 glyph_idx
= FT_Get_Char_Index (face
, char_code
);
56 err
= FT_Load_Glyph (face
, glyph_idx
, flag
);
59 printf ("Freetype Error %d loading glyph 0x%x for U+0x%x",
60 err
, glyph_idx
, char_code
);
62 if (err
> 0 && err
< (signed) ARRAY_SIZE (ft_errmsgs
))
63 printf (": %s\n", ft_errmsgs
[err
]);
69 if (face
->glyph
->bitmap
.width
> 12 && char_code
< MAX_CODE
)
70 result
[char_code
>> 3] |= (1 << (char_code
& 7));
74 main (int argc
, char *argv
[])
82 fprintf (stderr
, "grub-gen-widthspec: usage: INPUT OUTPUT");
86 if (FT_Init_FreeType (&ft_lib
))
88 fprintf (stderr
, "grub-gen-widthspec: error: FT_Init_FreeType fails");
96 unsigned int char_code
, glyph_index
;
98 err
= FT_New_Face (ft_lib
, argv
[1], 0, &ft_face
);
101 fprintf (stderr
, "can't open file %s, index %d: error %d",
103 if (err
> 0 && err
< (signed) ARRAY_SIZE (ft_errmsgs
))
104 fprintf (stderr
, ": %s\n", ft_errmsgs
[err
]);
106 fprintf (stderr
, "\n");
111 if ((ft_face
->face_flags
& FT_FACE_FLAG_SCALABLE
) ||
112 (! ft_face
->num_fixed_sizes
))
113 size
= GRUB_FONT_DEFAULT_SIZE
;
115 size
= ft_face
->available_sizes
[0].height
;
117 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
))
119 fprintf (stderr
, "grub-gen-widthspec: error: can't set %dx%d font size", size
, size
);
123 for (char_code
= FT_Get_First_Char (ft_face
, &glyph_index
);
125 char_code
= FT_Get_Next_Char (ft_face
, char_code
, &glyph_index
))
126 add_glyph (ft_face
, char_code
);
128 FT_Done_Face (ft_face
);
131 FT_Done_FreeType (ft_lib
);
133 file
= fopen (argv
[2], "w");
136 fprintf (stderr
, "grub-gen-asciih: error: cannot write to `%s': %s", argv
[2],
141 fprintf (file
, "/* THIS CHUNK OF BYTES IS AUTOMATICALLY GENERATED */\n");
142 fprintf (file
, "unsigned char widthspec[] =\n");
143 fprintf (file
, "{\n");
145 for (i
= 0; i
< MAX_CODE
/ 8; i
++)
146 fprintf (file
, "0x%02x,%c", result
[i
], ((i
& 0xf) == 0xf) ? '\n' : ' ');
148 fprintf (file
, "};\n");