2 * Copyright 2001 Ian Pilcher
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.1 of the License, or (at your option) any later version.
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.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <sys/types.h>
28 * The array of glyph information
34 int index; /* in PSDRV_AGLGlyphNames */
40 static GLYPHINFO glyphs[1500];
41 static int num_glyphs = 0;
45 * Functions to search and sort the array
48 static int cmp_by_UV(const void *a, const void *b)
50 return ((const GLYPHINFO *)a)->UV - ((const GLYPHINFO *)b)->UV;
53 static int cmp_by_name(const void *a, const void *b)
55 return strcmp(((const GLYPHINFO *)a)->name, ((const GLYPHINFO *)b)->name);
58 static inline void sort_by_UV(void)
60 qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_UV);
63 static inline void sort_by_name(void)
65 qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_name);
68 static inline GLYPHINFO *search_by_name(const char *name)
74 return (GLYPHINFO *)bsearch(&gi, glyphs, num_glyphs, sizeof(GLYPHINFO),
80 * Use the 'optimal' combination of tabs and spaces to position the cursor
83 static inline void fcpto(FILE *f, int newpos, int curpos)
85 int newtpos = newpos & ~7;
86 int curtpos = curpos & ~7;
88 while (curtpos < newtpos)
95 while (curpos < newpos)
103 * Make main() look "purty"
106 static inline void triple_space(FILE *f)
108 fputc('\n', f); fputc('\n', f);
113 * Read the Adobe Glyph List from 'glyphlist.txt'
116 static void read_agl(void)
118 FILE *f = fopen("glyphlist.txt", "r");
119 char linebuf[256], namebuf[128], commbuf[128];
123 fprintf(stderr, "Error opening glyphlist.txt\n");
127 while (fgets(linebuf, sizeof(linebuf), f) != NULL)
131 if (linebuf[0] == '#')
134 sscanf(linebuf, "%X;%[^;];%[^\n]", &UV, namebuf, commbuf);
136 glyphs[num_glyphs].UV = (int)UV;
137 glyphs[num_glyphs].name = strdup(namebuf);
138 glyphs[num_glyphs].comment = strdup(commbuf);
140 if (glyphs[num_glyphs].name == NULL ||
141 glyphs[num_glyphs].comment == NULL)
143 fprintf(stderr, "Memory allocation failure\n");
152 if (num_glyphs != 1051)
154 fprintf(stderr, "Read %i glyphs\n", num_glyphs);
161 * Read glyph names from all AFM files in current directory
164 static void read_afms(FILE *f_c, FILE *f_h)
166 DIR *d = opendir(".");
170 " * Built-in font metrics\n"
173 "const AFM *const PSDRV_BuiltinAFMs[] =\n"
179 fprintf(stderr, "Error opening current directory\n");
183 while ((de = readdir(d)) != NULL)
186 char *cp, linebuf[256], font_family[128];
189 cp = strrchr(de->d_name, '.'); /* Does it end in */
190 if (cp == NULL || strcasecmp(cp, ".afm") != 0) /* .afm or .AFM? */
193 f = fopen(de->d_name, "r");
196 fprintf(stderr, "Error opening %s\n", de->d_name);
202 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
204 fprintf(stderr, "FontName not found in %s\n", de->d_name);
208 if (strncmp(linebuf, "FontName ", 9) == 0)
212 sscanf(linebuf, "FontName %[^\r\n]", font_family);
214 for (i = 0; font_family[i] != '\0'; ++i)
215 if (font_family[i] == '-')
216 font_family[i] = '_';
218 fprintf(f_h, "extern const AFM PSDRV_%s;\n", font_family);
219 fprintf(f_c, " &PSDRV_%s,\n", font_family);
223 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
225 fprintf(stderr, "FamilyName not found in %s\n", de->d_name);
229 if (strncmp(linebuf, "FamilyName ", 11) == 0)
233 sscanf(linebuf, "FamilyName %[^\r\n]", font_family);
237 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
239 fprintf(stderr, "StartCharMetrics not found in %s\n",
244 if (strncmp(linebuf, "StartCharMetrics ", 17) == 0)
248 sscanf(linebuf, "StartCharMetrics %i", &num_metrics);
250 for (i = 0; i < num_metrics; ++i)
254 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
256 fprintf(stderr, "Unexpected EOF after %i glyphs in %s\n", i,
261 cp = strchr(linebuf, 'N');
262 if (cp == NULL || strlen(cp) < 3)
264 fprintf(stderr, "Parse error after %i glyphs in %s\n", i,
269 sscanf(cp, "N %s", namebuf);
270 if (search_by_name(namebuf) != NULL)
273 sprintf(linebuf, "FONT FAMILY;%s", font_family);
275 glyphs[num_glyphs].UV = -1;
276 glyphs[num_glyphs].name = strdup(namebuf);
277 glyphs[num_glyphs].comment = strdup(linebuf);
279 if (glyphs[num_glyphs].name == NULL ||
280 glyphs[num_glyphs].comment == NULL)
282 fprintf(stderr, "Memory allocation failure\n");
296 fputs(" NULL\n};\n", f_c);
301 * Write opening comments, etc.
304 static void write_header(FILE *f)
309 for (i = 0; i < 79; ++i)
313 " *\tFont and glyph data for the Wine PostScript driver\n"
315 " *\tCopyright 2001 Ian Pilcher\n"
318 " *\tThis data is derived from the Adobe Glyph list at\n"
321 "http://partners.adobe.com/asn/developer/type/glyphlist.txt\n"
323 " *\tand the Adobe Font Metrics files at\n"
326 "ftp://ftp.adobe.com/pub/adobe/type/win/all/afmfiles/base35/\n"
328 " *\twhich are Copyright 1985-1998 Adobe Systems Incorporated.\n"
332 "#include \"psdrv.h\"\n"
333 "#include \"data/agl.h\"\n", f);
337 * Write the array of glyph names (also populates indexes)
340 static void write_glyph_names(FILE *f_c, FILE *f_h)
342 int i, num_names = 0, index = 0;
344 for (i = 0; i < num_glyphs; ++i)
345 if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
349 " * Every glyph name in the AGL and the 35 core PostScript fonts\n"
353 fprintf(f_c, "const INT PSDRV_AGLGlyphNamesSize = %i;\n\n", num_names);
355 fprintf(f_c, "GLYPHNAME PSDRV_AGLGlyphNames[%i] =\n{\n", num_names);
357 for (i = 0; i < num_glyphs - 1; ++i)
361 if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
363 fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
364 fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
366 cp = fprintf(f_c, " { %4i, \"%s\" },", index, glyphs[i].name);
367 glyphs[i].index = index;
372 glyphs[i].index = glyphs[i - 1].index;
377 fprintf(f_c, "/* %s */\n", glyphs[i].comment);
380 fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
381 fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
383 glyphs[i].index = index;
384 fcpto(f_c, 40, fprintf(f_c, " { %4i, \"%s\" }", index, glyphs[i].name));
385 fprintf(f_c, "/* %s */\n};\n", glyphs[i].comment);
390 * Write the AGL encoding vector, sorted by glyph name
393 static void write_encoding_by_name(FILE *f)
395 int i, size = 0, even = 1;
397 for (i = 0; i < num_glyphs; ++i)
398 if (glyphs[i].UV != -1 &&
399 (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0))
400 ++size; /* should be 1039 */
403 " * The AGL encoding vector, sorted by glyph name - "
404 "duplicates omitted\n"
408 fprintf(f, "const INT PSDRV_AGLbyNameSize = %i;\n\n", size);
409 fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyName[%i] =\n{\n", size);
411 for (i = 0; i < num_glyphs - 1; ++i)
415 if (glyphs[i].UV == -1)
418 if (i != 0 && strcmp(glyphs[i - 1].name, glyphs[i].name) == 0)
421 cp = fprintf(f, " { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
430 fprintf(f, " { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
434 * Write the AGL encoding vector, sorted by Unicode value
437 static void write_encoding_by_UV(FILE *f)
439 int i, size = 0, even = 1;
441 for (i = 0; i < num_glyphs; ++i)
442 if (glyphs[i].UV != -1)
443 ++size; /* better be 1051! */
448 " * The AGL encoding vector, sorted by Unicode value - "
449 "duplicates included\n"
453 fprintf(f, "const INT PSDRV_AGLbyUVSize = %i;\n\n", size);
454 fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyUV[%i] =\n{\n", size);
456 for (i = 0; i < num_glyphs - 1; ++i)
460 if (glyphs[i].UV == -1)
463 cp = fprintf(f, " { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
472 fprintf(f, " { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
480 int main(int argc, char *argv[])
486 fprintf(stderr, "Usage: %s <C file> <header file>\n", argv[0]);
490 f_c = fopen(argv[1], "w");
493 fprintf(stderr, "Error opening %s for writing\n", argv[1]);
497 f_h = fopen(argv[2], "w");
500 fprintf(stderr, "Error opening %s for writing\n", argv[2]);
507 read_afms(f_c, f_h); /* also writes font list */
509 write_glyph_names(f_c, f_h);
511 write_encoding_by_name(f_c);
513 write_encoding_by_UV(f_c);