]> Pileus Git - ~andy/gtk/blob - gtk/gen-paper-names.c
30626963ce74ce4af92b3d7ca5ceb508b6e8bf69
[~andy/gtk] / gtk / gen-paper-names.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2006 Matthias Clasen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23
24 #include <glib.h>
25
26 #include "paper_names.c"
27
28 static const gint n_infos = G_N_ELEMENTS (standard_names);
29 static const gint n_extra = G_N_ELEMENTS (extra_ppd_names);
30
31 typedef struct {
32   const gchar *s;
33   gint         len;
34   gint         suffix;
35   gint         offset;
36 } NameInfo;
37
38 static NameInfo *names = NULL;
39 static gint n_names = 0;
40
41 static void
42 add_name (const gchar *name)
43 {
44   names[n_names].s = name;
45   names[n_names].len = strlen (name);
46   names[n_names].suffix = -1;
47   names[n_names].offset = 0;
48
49   n_names++;
50 }
51
52 static gint
53 find_name (const gchar *name)
54 {
55   gint i;
56
57   if (!name)
58     return -1;
59
60   for (i = 0; i < n_names; i++)
61     {
62       if (strcmp (names[i].s, name) == 0)
63         return names[i].offset;
64     }
65
66   fprintf (stderr, "BOO! %s not found\n", name);
67
68   return -2;
69 }
70
71 #define MM_PER_INCH 25.4
72 #define POINTS_PER_INCH 72
73
74 static gboolean
75 parse_media_size (const gchar *size,
76                   gdouble     *width_mm, 
77                   gdouble     *height_mm)
78 {
79   const gchar *p;
80   gchar *e;
81   gdouble short_dim, long_dim;
82
83   p = size;
84   
85   short_dim = g_ascii_strtod (p, &e);
86
87   if (p == e || *e != 'x')
88     return FALSE;
89
90   p = e + 1; /* Skip x */
91
92   long_dim = g_ascii_strtod (p, &e);
93
94   if (p == e)
95     return TRUE;
96
97   p = e;
98
99   if (strcmp (p, "in") == 0)
100     {
101       short_dim = short_dim * MM_PER_INCH;
102       long_dim = long_dim * MM_PER_INCH;
103     }
104   else if (strcmp (p, "mm") != 0)
105     return FALSE;
106
107   if (width_mm)
108     *width_mm = short_dim;
109   if (height_mm)
110     *height_mm = long_dim;
111   
112   return TRUE;  
113 }
114
115 int 
116 main (int argc, char *argv[])
117 {
118   gint i, j, offset;
119   gdouble width, height;
120
121   names = (NameInfo *) malloc (sizeof (NameInfo) * (4 + n_infos + 2 * n_extra));
122   n_names = 0;
123
124   /* collect names */
125   for (i = 0; i < n_infos; i++)
126     {
127       add_name (standard_names[i].name);
128       add_name (standard_names[i].display_name);
129       if (standard_names[i].ppd_name)
130         add_name (standard_names[i].ppd_name);
131     }
132
133   for (i = 0; i < n_extra; i++)
134     {
135       add_name (extra_ppd_names[i].ppd_name);
136       add_name (extra_ppd_names[i].standard_name);
137     }
138
139   /* find suffixes and dupes */
140   for (i = 0; i < n_names; i++)
141     for (j = 0; j < n_names; j++)
142       {
143         if (i == j) continue;
144
145         if (names[i].len < names[j].len ||
146             (names[i].len == names[j].len && j < i))
147           {
148             if (strcmp (names[i].s, names[j].s + names[j].len - names[i].len) == 0)
149               {
150                 names[i].suffix = j;
151                 break;
152               }
153           }
154       }
155
156   /* calculate offsets for regular entries */
157   offset = 0;
158   for (i = 0; i < n_names; i++)
159     {
160       if (names[i].suffix == -1)
161         {
162           names[i].offset = offset;
163           offset += names[i].len + 1;
164         }
165     }
166
167   /* calculate offsets for suffixes */
168   for (i = 0; i < n_names; i++)
169     {
170       if (names[i].suffix != -1)
171         {
172           j = i;
173           do {
174             j = names[j].suffix;
175           } while (names[j].suffix != -1);
176           names[i].offset = names[j].offset + names[j].len - names[i].len;
177         }
178     }
179
180   printf ("/* Generated by gen-paper-names */\n\n");
181
182   /* write N_ marked names */
183
184   printf ("#if 0\n");
185   for (i = 0; i < n_infos; i++)
186     printf ("NC_(\"paper size\", \"%s\")\n", standard_names[i].display_name);
187   printf ("#endif\n\n");
188
189   /* write strings */
190   printf ("static const char paper_names[] =");
191   for (i = 0; i < n_names; i++)
192     {
193       if (names[i].suffix == -1)
194         printf ("\n  \"%s\\0\"", names[i].s);
195     }
196   printf (";\n\n");
197
198   /* dump PaperInfo array */
199   printf ("typedef struct {\n"
200           "  int name;\n"
201           "  float width;\n"
202           "  float height;\n"
203           "  int display_name;\n"
204           "  int ppd_name;\n"
205           "} PaperInfo;\n\n"
206           "static const PaperInfo standard_names_offsets[] = {\n");
207
208   for (i = 0; i < n_infos; i++)
209     {
210       width = height = 0.0;
211       if (!parse_media_size (standard_names[i].size, &width, &height))
212         printf ("failed to parse size %s\n", standard_names[i].size);
213
214       printf ("  { %4d, %g, %g, %4d, %4d },\n",
215               find_name (standard_names[i].name),
216               width, height,
217               find_name (standard_names[i].display_name),
218               find_name (standard_names[i].ppd_name));
219     }
220
221   printf ("};\n\n");
222
223
224   /* dump extras */
225
226   printf ("static const struct {\n"
227           "  int ppd_name;\n"
228           "  int standard_name;\n"
229           "} extra_ppd_names_offsets[] = {\n");
230
231   for (i = 0; i < n_extra; i++)
232     {
233       printf ("  { %4d, %4d },\n",
234               find_name (extra_ppd_names[i].ppd_name),
235               find_name (extra_ppd_names[i].standard_name));
236     }
237
238   printf ("};\n\n");
239
240   return 0;
241 }