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 struct grub_glyph_info
49 unsigned char *bitmap
;
53 add_pixel (unsigned char **data
, int *mask
, int not_blank
)
69 add_glyph (FT_UInt glyph_idx
, FT_Face face
,
70 unsigned int char_code
,
71 struct grub_glyph_info
*glyph_info
)
74 int cuttop
, cutbottom
, cutleft
, cutright
;
76 int mask
, i
, j
, bitmap_size
;
78 int flag
= FT_LOAD_RENDER
| FT_LOAD_MONOCHROME
;
81 err
= FT_Load_Glyph (face
, glyph_idx
, flag
);
84 fprintf (stderr
, "Freetype Error %d loading glyph 0x%x for U+0x%x",
85 err
, glyph_idx
, char_code
);
87 if (err
> 0 && err
< (signed) ARRAY_SIZE (ft_errmsgs
))
88 fprintf (stderr
, ": %s\n", ft_errmsgs
[err
]);
90 fprintf (stderr
, "\n");
97 printf ("%x\n", char_code
);
99 cuttop
= cutbottom
= cutleft
= cutright
= 0;
101 width
= glyph
->bitmap
.width
;
102 height
= glyph
->bitmap
.rows
;
104 bitmap_size
= ((width
* height
+ 7) / 8);
105 glyph_info
->bitmap
= malloc (bitmap_size
);
106 if (!glyph_info
->bitmap
)
108 fprintf (stderr
, "grub-gen-asciih: error: out of memory");
111 glyph_info
->bitmap_size
= bitmap_size
;
113 glyph_info
->width
= width
;
114 glyph_info
->height
= height
;
115 glyph_info
->x_ofs
= glyph
->bitmap_left
;
116 glyph_info
->y_ofs
= glyph
->bitmap_top
- height
;
117 glyph_info
->device_width
= glyph
->metrics
.horiAdvance
/ 64;
120 data
= &glyph_info
->bitmap
[0] - 1;
121 for (j
= cuttop
; j
< height
+ cuttop
; j
++)
122 for (i
= cutleft
; i
< width
+ cutleft
; i
++)
123 add_pixel (&data
, &mask
,
124 glyph
->bitmap
.buffer
[i
/ 8 + j
* glyph
->bitmap
.pitch
] &
125 (1 << (7 - (i
& 7))));
129 write_font_ascii_bitmap (FILE *file
, FT_Face face
)
133 fprintf (file
, "/* THIS CHUNK OF BYTES IS AUTOMATICALLY GENERATED */\n");
134 fprintf (file
, "unsigned char ascii_bitmaps[] =\n");
135 fprintf (file
, "{\n");
137 for (char_code
= 0; char_code
<= 0x7f; char_code
++)
140 struct grub_glyph_info glyph
;
142 glyph_idx
= FT_Get_Char_Index (face
, char_code
);
146 memset (&glyph
, 0, sizeof(glyph
));
148 add_glyph (glyph_idx
, face
, char_code
, &glyph
);
150 if (glyph
.width
== 8 && glyph
.height
== 16
151 && glyph
.x_ofs
== 0 && glyph
.y_ofs
== 0)
154 for (row
= 0; row
< 16; row
++)
155 fprintf (file
, "0x%02x, ", glyph
.bitmap
[row
]);
159 unsigned char glph
[16];
160 int p
= 0, mask
= 0x80;
162 int dy
= 12 - glyph
.height
- glyph
.y_ofs
;
163 for (row
= 0; row
< 16; row
++)
165 for (row
= 0; row
< glyph
.height
; row
++)
166 for (col
= 0; col
< glyph
.width
; col
++)
168 int val
= glyph
.bitmap
[p
] & mask
;
175 if (val
&& dy
+ row
>= 0
177 && glyph
.x_ofs
+ col
>= 0
178 && glyph
.x_ofs
+ col
< 8)
179 glph
[dy
+ row
] |= 1 << (7 - (glyph
.x_ofs
+ col
));
181 for (row
= 0; row
< 16; row
++)
182 fprintf (file
, "0x%02x, ", glph
[row
]);
184 fprintf (file
, "\n");
187 fprintf (file
, "};\n");
191 main (int argc
, char *argv
[])
199 fprintf (stderr
, "grub-gen-asciih: usage: INPUT OUTPUT");
203 if (FT_Init_FreeType (&ft_lib
))
205 fprintf (stderr
, "grub-gen-asciih: error: FT_Init_FreeType fails");
213 err
= FT_New_Face (ft_lib
, argv
[1], 0, &ft_face
);
216 fprintf (stderr
, "can't open file %s, index %d: error %d",
218 if (err
> 0 && err
< (signed) ARRAY_SIZE (ft_errmsgs
))
219 fprintf (stderr
, ": %s\n", ft_errmsgs
[err
]);
221 fprintf (stderr
, "\n");
226 if ((ft_face
->face_flags
& FT_FACE_FLAG_SCALABLE
) ||
227 (! ft_face
->num_fixed_sizes
))
228 size
= GRUB_FONT_DEFAULT_SIZE
;
230 size
= ft_face
->available_sizes
[0].height
;
232 if (FT_Set_Pixel_Sizes (ft_face
, size
, size
))
234 fprintf (stderr
, "grub-gen-asciih: error: can't set %dx%d font size", size
, size
);
239 file
= fopen (argv
[2], "w");
242 fprintf (stderr
, "grub-gen-asciih: error: cannot write to `%s': %s", argv
[2],
247 write_font_ascii_bitmap (file
, ft_face
);
251 FT_Done_Face (ft_face
);
253 FT_Done_FreeType (ft_lib
);