Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-gen-widthspec.c
blob33bc8cb2d9d1d06b0f1005cf634829f795bc8e63
1 /*
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/>.
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
23 #include <errno.h>
25 #include <ft2build.h>
26 #include FT_FREETYPE_H
27 #include FT_TRUETYPE_TAGS_H
28 #include FT_TRUETYPE_TABLES_H
29 #include FT_SYNTHESIS_H
31 #undef __FTERRORS_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 };
35 #include FT_ERRORS_H
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];
44 static void
45 add_glyph (FT_Face face,
46 unsigned int char_code)
48 int flag = FT_LOAD_RENDER | FT_LOAD_MONOCHROME;
49 FT_Error err;
50 FT_UInt glyph_idx;
52 glyph_idx = FT_Get_Char_Index (face, char_code);
53 if (!glyph_idx)
54 return;
56 err = FT_Load_Glyph (face, glyph_idx, flag);
57 if (err)
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]);
64 else
65 printf ("\n");
66 return;
69 if (face->glyph->bitmap.width > 12 && char_code < MAX_CODE)
70 result[char_code >> 3] |= (1 << (char_code & 7));
73 int
74 main (int argc, char *argv[])
76 FT_Library ft_lib;
77 FILE *file;
78 int i;
80 if (argc != 3)
82 fprintf (stderr, "grub-gen-widthspec: usage: INPUT OUTPUT");
83 return 1;
86 if (FT_Init_FreeType (&ft_lib))
88 fprintf (stderr, "grub-gen-widthspec: error: FT_Init_FreeType fails");
89 return 1;
93 FT_Face ft_face;
94 int size;
95 FT_Error err;
96 unsigned int char_code, glyph_index;
98 err = FT_New_Face (ft_lib, argv[1], 0, &ft_face);
99 if (err)
101 fprintf (stderr, "can't open file %s, index %d: error %d",
102 argv[1], 0, err);
103 if (err > 0 && err < (signed) ARRAY_SIZE (ft_errmsgs))
104 fprintf (stderr, ": %s\n", ft_errmsgs[err]);
105 else
106 fprintf (stderr, "\n");
108 return 1;
111 if ((ft_face->face_flags & FT_FACE_FLAG_SCALABLE) ||
112 (! ft_face->num_fixed_sizes))
113 size = GRUB_FONT_DEFAULT_SIZE;
114 else
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);
120 return 1;
123 for (char_code = FT_Get_First_Char (ft_face, &glyph_index);
124 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");
134 if (! file)
136 fprintf (stderr, "grub-gen-asciih: error: cannot write to `%s': %s", argv[2],
137 strerror (errno));
138 return 1;
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");
150 fclose (file);
152 return 0;