*** empty log message ***
[m17n/libotf.git] / example / otfimage.c
1 #include <stdio.h>
2
3 #include <ft2build.h>
4 #include FT_FREETYPE_H
5
6 #define DEFAULT_PIXEL_SIZE 16
7
8 FT_Face face;
9
10 int
11 dump_image (int pixel_size, int index, int code, int full)
12 {
13   int err = FT_Load_Glyph (face, index, FT_LOAD_RENDER | FT_LOAD_MONOCHROME);
14   int i,j, size;
15   unsigned char *buf;
16   
17   if (err)
18     return -1;
19   size = face->glyph->bitmap.rows * face->glyph->bitmap.width;
20   if (! size)
21     return -1;
22   buf = (unsigned char *) face->glyph->bitmap.buffer;
23   printf ("(#x%04X \"P4 %d %d ",
24           code, face->glyph->bitmap.width, face->glyph->bitmap.rows);
25   for (i = 0; i < face->glyph->bitmap.rows; i++)
26     for (j = 0; j < (face->glyph->bitmap.width + 7) / 8; j++)
27       {
28         printf("\\%o", buf[i * face->glyph->bitmap.pitch + j]);
29       }
30   printf ("\")\n");
31   return 0;
32 }
33
34 /* Format MSG by FMT and print the result to the stderr, and exit.  */
35
36 #define FATAL_ERROR(fmt, arg)   \
37   do {                          \
38     fprintf (stderr, fmt, arg); \
39     exit (1);                   \
40   } while (0)
41
42 int
43 main (int argc, char **argv)
44 {
45   FT_Library library;
46   int err;
47   int i;
48   int pixel_size = DEFAULT_PIXEL_SIZE;
49   FT_Uint *raw_table;
50   FT_UInt unicode_table[0x80];
51   int max_glyph_id;
52
53   if (argc != 2)
54     FATAL_ERROR ("%s\n", "Usage: otfimage [ X-OPTION ... ]  OTF-FILE");
55   
56   if ((err = FT_Init_FreeType (&library)))
57     FATAL_ERROR ("%s\n", "FT_Init_FreeType: error");
58   err = FT_New_Face (library, argv[1], 0, &face);
59   if (err == FT_Err_Unknown_File_Format)
60     FATAL_ERROR ("%s\n", "FT_New_Face: unknown file format");
61   else if (err)
62     FATAL_ERROR ("%s\n", "FT_New_Face: unknown error");
63   if ((err = FT_Set_Pixel_Sizes (face, 0, pixel_size)))
64     FATAL_ERROR ("%s\n", "FT_Set_Pixel_Sizes: error");
65
66   {
67     char *str = getenv ("PIXEL_SIZE");
68
69     if (str && (i = atoi (str)) > 0)
70       pixel_size = i;
71   }
72
73   for (i = 0, max_glyph_id = 0; i < 0x10000; i++)
74     if (FT_Load_Glyph (face, i, FT_LOAD_RENDER | FT_LOAD_MONOCHROME)
75
76
77   for (i = 0x0D00; i < 0x0D80; i++)
78     {
79       unicode_table[i - 0x0D00] = FT_Get_Char_Index (face, (FT_ULong) i);
80       if (unicode_table[i - 0x0D00])
81         printf ("%04X->%04X\n", i, unicode_table[i - 0x0D00]);
82     }
83
84   for (i = 0; i < 0x80; i++)
85     if (unicode_table[i])
86       dump_image (pixel_size, unicode_table[i], 0x0D00 + i, 1);
87   for (i = 0; i < 0x10000; i++)
88     dump_image (pixel_size, i, 0x0E00 + i, 0);
89   
90
91   exit (0);
92 }