*** empty log message ***
[m17n/libotf.git] / example / otflist.c
1 /* otflist.c -- List OpenType fonts.
2
3 Copyright (C) 2003, 2004
4   National Institute of Advanced Industrial Science and Technology (AIST)
5   Registration Number H15PRO167
6
7 This file is part of libotf.
8
9 Libotf is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published
11 by the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 Libotf is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
17 License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library, in a file named COPYING; if not,
21 write to the Free Software Foundation, Inc., 59 Temple Place, Suite
22 330, Boston, MA 02111-1307, USA.  */
23
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <string.h>
27 #include <dirent.h>
28
29 #include <ft2build.h>
30 #include FT_FREETYPE_H
31
32 #include <otf.h>
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 void
43 help_and_exit (char *prog)
44 {
45   printf ("otflist %s\n", LIBOTF_VERSION);
46   printf ("Usage: %s [-l] [-h] [DIR]\n", prog);
47   printf ("List information about OpenType font files in the directory DIR.\n");
48   printf ("It actually lists all fonts that can be handled by Freetype.\n");
49   printf ("  -h         print this help, then exit\n");
50   printf ("  -l         use a long listing mode\n");
51   exit (0);
52 }
53
54 int
55 filter (const struct dirent *direntry)
56 {
57   int len = strlen (direntry->d_name);
58   const char *ext = direntry->d_name + (len - 4);
59
60   return (len >= 5
61           && (! strncmp (ext, ".ttf", 4)
62               || ! strncmp (ext, ".TTF", 4)
63               || ! strncmp (ext, ".otf", 4)
64               || ! strncmp (ext, ".OTF", 4)
65               || ! strncmp (ext, ".PFA", 4)
66               || ! strncmp (ext, ".pfa", 4)
67               || ! strncmp (ext, ".PFB", 4)
68               || ! strncmp (ext, ".pfb", 4)));
69 }
70
71 int
72 main (int argc, char **argv)
73
74   FT_Library ft_library;
75   FT_Face face;
76   int long_format = 0;
77   struct dirent **namelist;
78   int n;
79   int i, j;
80
81   if (FT_Init_FreeType (&ft_library))
82     exit (1);
83
84   if (argc > 1)
85     {
86       if (! strcmp (argv[1], "-h") || ! strcmp (argv[1], "--help"))
87         help_and_exit (argv[0]);
88       if (! strcmp (argv[1], "-l"))
89         long_format = 1, argc--, argv++;
90     }
91   if (argc == 2)
92     {
93       if (chdir (argv[1]) < 0)
94         FATAL_ERROR ("Can't change directory to %s\n", argv[1]);
95     }
96   n = scandir (".", &namelist, filter, alphasort);
97
98   for (i = 0; i < n; i++)
99     if (! FT_New_Face (ft_library, namelist[i]->d_name, 0, &face))
100       {
101         OTF *otf = OTF_open (namelist[i]->d_name);
102         char *name, *family, *style;
103
104         if (otf)
105           {
106             OTF_get_table (otf, "name");
107
108             if (! (family = otf->name->name[16]))
109               family = otf->name->name[1];
110             if (! (style = otf->name->name[17]))
111               style = otf->name->name[2];
112           }
113         if (! family)
114           family = face->family_name;
115         if (! style)
116           style = face->style_name;
117
118         name = alloca (strlen (namelist[i]->d_name)
119                        + strlen (family)
120                        + 4);
121         sprintf (name, "%s (%s)", namelist[i]->d_name, family);
122         printf ("%-40s %s", name, style);
123         for (j = 0; j < face->num_charmaps; j++)
124           printf (" %d-%d", face->charmaps[j]->platform_id,
125                   face->charmaps[j]->encoding_id);
126         printf ("\n");
127       }
128     else
129       {
130         printf ("%s fail to open\n", namelist[i]->d_name);
131       }
132   exit (0);
133 }