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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #include <glib.h>
23
24 #include "paper_names.c"
25
26 static const gint n_infos = G_N_ELEMENTS (standard_names);
27 static const gint n_extra = G_N_ELEMENTS (extra_ppd_names);
28
29 typedef struct {
30 const gchar *s;
31 gint len;
32 gint suffix;
33 gint offset;
34 } NameInfo;
35
36 static NameInfo *names = NULL;
37 static gint n_names = 0;
38
39 static void
add_name(const gchar * name)40 add_name (const gchar *name)
41 {
42 names[n_names].s = name;
43 names[n_names].len = strlen (name);
44 names[n_names].suffix = -1;
45 names[n_names].offset = 0;
46
47 n_names++;
48 }
49
50 static gint
find_name(const gchar * name)51 find_name (const gchar *name)
52 {
53 gint i;
54
55 if (!name)
56 return -1;
57
58 for (i = 0; i < n_names; i++)
59 {
60 if (strcmp (names[i].s, name) == 0)
61 return names[i].offset;
62 }
63
64 fprintf (stderr, "BOO! %s not found\n", name);
65
66 return -2;
67 }
68
69 #define MM_PER_INCH 25.4
70 #define POINTS_PER_INCH 72
71
72 static gboolean
parse_media_size(const gchar * size,gdouble * width_mm,gdouble * height_mm)73 parse_media_size (const gchar *size,
74 gdouble *width_mm,
75 gdouble *height_mm)
76 {
77 const gchar *p;
78 gchar *e;
79 gdouble short_dim, long_dim;
80
81 p = size;
82
83 short_dim = g_ascii_strtod (p, &e);
84
85 if (p == e || *e != 'x')
86 return FALSE;
87
88 p = e + 1; /* Skip x */
89
90 long_dim = g_ascii_strtod (p, &e);
91
92 if (p == e)
93 return TRUE;
94
95 p = e;
96
97 if (strcmp (p, "in") == 0)
98 {
99 short_dim = short_dim * MM_PER_INCH;
100 long_dim = long_dim * MM_PER_INCH;
101 }
102 else if (strcmp (p, "mm") != 0)
103 return FALSE;
104
105 if (width_mm)
106 *width_mm = short_dim;
107 if (height_mm)
108 *height_mm = long_dim;
109
110 return TRUE;
111 }
112
113 int
main(int argc,char * argv[])114 main (int argc, char *argv[])
115 {
116 gint i, j, offset;
117 gdouble width, height;
118
119 names = (NameInfo *) malloc (sizeof (NameInfo) * (4 + n_infos + 2 * n_extra));
120 n_names = 0;
121
122 /* collect names */
123 for (i = 0; i < n_infos; i++)
124 {
125 add_name (standard_names[i].name);
126 add_name (standard_names[i].display_name);
127 if (standard_names[i].ppd_name)
128 add_name (standard_names[i].ppd_name);
129 }
130
131 for (i = 0; i < n_extra; i++)
132 {
133 add_name (extra_ppd_names[i].ppd_name);
134 add_name (extra_ppd_names[i].standard_name);
135 }
136
137 /* find suffixes and dupes */
138 for (i = 0; i < n_names; i++)
139 for (j = 0; j < n_names; j++)
140 {
141 if (i == j) continue;
142
143 if (names[i].len < names[j].len ||
144 (names[i].len == names[j].len && j < i))
145 {
146 if (strcmp (names[i].s, names[j].s + names[j].len - names[i].len) == 0)
147 {
148 names[i].suffix = j;
149 break;
150 }
151 }
152 }
153
154 /* calculate offsets for regular entries */
155 offset = 0;
156 for (i = 0; i < n_names; i++)
157 {
158 if (names[i].suffix == -1)
159 {
160 names[i].offset = offset;
161 offset += names[i].len + 1;
162 }
163 }
164
165 /* calculate offsets for suffixes */
166 for (i = 0; i < n_names; i++)
167 {
168 if (names[i].suffix != -1)
169 {
170 j = i;
171 do {
172 j = names[j].suffix;
173 } while (names[j].suffix != -1);
174 names[i].offset = names[j].offset + names[j].len - names[i].len;
175 }
176 }
177
178 printf ("/* Generated by gen-paper-names */\n\n");
179
180 /* write N_ marked names */
181
182 printf ("#if 0\n");
183 for (i = 0; i < n_infos; i++)
184 printf ("NC_(\"paper size\", \"%s\")\n", standard_names[i].display_name);
185 printf ("#endif\n\n");
186
187 /* write strings */
188 printf ("static const char paper_names[] =");
189 for (i = 0; i < n_names; i++)
190 {
191 if (names[i].suffix == -1)
192 printf ("\n \"%s\\0\"", names[i].s);
193 }
194 printf (";\n\n");
195
196 /* dump PaperInfo array */
197 printf ("typedef struct {\n"
198 " int name;\n"
199 " float width;\n"
200 " float height;\n"
201 " int display_name;\n"
202 " int ppd_name;\n"
203 "} PaperInfo;\n\n"
204 "static const PaperInfo standard_names_offsets[] = {\n");
205
206 for (i = 0; i < n_infos; i++)
207 {
208 width = height = 0.0;
209 if (!parse_media_size (standard_names[i].size, &width, &height))
210 printf ("failed to parse size %s\n", standard_names[i].size);
211
212 printf (" { %4d, %g, %g, %4d, %4d },\n",
213 find_name (standard_names[i].name),
214 width, height,
215 find_name (standard_names[i].display_name),
216 find_name (standard_names[i].ppd_name));
217 }
218
219 printf ("};\n\n");
220
221
222 /* dump extras */
223
224 printf ("static const struct {\n"
225 " int ppd_name;\n"
226 " int standard_name;\n"
227 "} extra_ppd_names_offsets[] = {\n");
228
229 for (i = 0; i < n_extra; i++)
230 {
231 printf (" { %4d, %4d },\n",
232 find_name (extra_ppd_names[i].ppd_name),
233 find_name (extra_ppd_names[i].standard_name));
234 }
235
236 printf ("};\n\n");
237
238 return 0;
239 }
240