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
21 #include <sys/types.h>
32 * The array of glyph information
38 int index; /* in PSDRV_AGLGlyphNames */
44 static GLYPHINFO glyphs[1500];
45 static int num_glyphs = 0;
49 * Functions to search and sort the array
52 static int cmp_by_UV(const void *a, const void *b)
54 return ((const GLYPHINFO *)a)->UV - ((const GLYPHINFO *)b)->UV;
57 static int cmp_by_name(const void *a, const void *b)
59 return strcmp(((const GLYPHINFO *)a)->name, ((const GLYPHINFO *)b)->name);
62 static inline void sort_by_UV(void)
64 qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_UV);
67 static inline void sort_by_name(void)
69 qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_name);
72 static inline GLYPHINFO *search_by_name(const char *name)
78 return (GLYPHINFO *)bsearch(&gi, glyphs, num_glyphs, sizeof(GLYPHINFO),
84 * Use the 'optimal' combination of tabs and spaces to position the cursor
87 static inline void fcpto(FILE *f, int newpos, int curpos)
89 int newtpos = newpos & ~7;
90 int curtpos = curpos & ~7;
92 while (curtpos < newtpos)
99 while (curpos < newpos)
107 * Make main() look "purty"
110 static inline void triple_space(FILE *f)
112 fputc('\n', f); fputc('\n', f);
117 * Read the Adobe Glyph List from 'glyphlist.txt'
120 static void read_agl(void)
122 FILE *f = fopen("glyphlist.txt", "r");
123 char linebuf[256], namebuf[128], commbuf[128];
127 fprintf(stderr, "Error opening glyphlist.txt\n");
131 while (fgets(linebuf, sizeof(linebuf), f) != NULL)
135 if (linebuf[0] == '#')
138 sscanf(linebuf, "%X;%[^;];%[^\n]", &UV, namebuf, commbuf);
140 glyphs[num_glyphs].UV = (int)UV;
141 glyphs[num_glyphs].name = strdup(namebuf);
142 glyphs[num_glyphs].comment = strdup(commbuf);
144 if (glyphs[num_glyphs].name == NULL ||
145 glyphs[num_glyphs].comment == NULL)
147 fprintf(stderr, "Memory allocation failure\n");
156 if (num_glyphs != 1051)
158 fprintf(stderr, "Read %i glyphs\n", num_glyphs);
165 * Read glyph names from all AFM files in current directory
168 static void read_afms(FILE *f_c, FILE *f_h)
170 DIR *d = opendir(".");
174 " * Built-in font metrics\n"
177 "const AFM *const PSDRV_BuiltinAFMs[] =\n"
183 fprintf(stderr, "Error opening current directory\n");
187 while ((de = readdir(d)) != NULL)
190 char *cp, linebuf[256], font_family[128];
193 cp = strrchr(de->d_name, '.'); /* Does it end in */
194 if (cp == NULL || strcasecmp(cp, ".afm") != 0) /* .afm or .AFM? */
197 f = fopen(de->d_name, "r");
200 fprintf(stderr, "Error opening %s\n", de->d_name);
206 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
208 fprintf(stderr, "FontName not found in %s\n", de->d_name);
212 if (strncmp(linebuf, "FontName ", 9) == 0)
216 sscanf(linebuf, "FontName %[^\r\n]", font_family);
218 for (i = 0; font_family[i] != '\0'; ++i)
219 if (font_family[i] == '-')
220 font_family[i] = '_';
222 fprintf(f_h, "DECLSPEC_HIDDEN extern const AFM PSDRV_%s;\n", font_family);
223 fprintf(f_c, " &PSDRV_%s,\n", font_family);
227 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
229 fprintf(stderr, "FamilyName not found in %s\n", de->d_name);
233 if (strncmp(linebuf, "FamilyName ", 11) == 0)
237 sscanf(linebuf, "FamilyName %[^\r\n]", font_family);
241 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
243 fprintf(stderr, "StartCharMetrics not found in %s\n",
248 if (strncmp(linebuf, "StartCharMetrics ", 17) == 0)
252 sscanf(linebuf, "StartCharMetrics %i", &num_metrics);
254 for (i = 0; i < num_metrics; ++i)
258 if (fgets(linebuf, sizeof(linebuf), f) == NULL)
260 fprintf(stderr, "Unexpected EOF after %i glyphs in %s\n", i,
265 cp = strchr(linebuf, 'N');
266 if (cp == NULL || strlen(cp) < 3)
268 fprintf(stderr, "Parse error after %i glyphs in %s\n", i,
273 sscanf(cp, "N %s", namebuf);
274 if (search_by_name(namebuf) != NULL)
277 sprintf(linebuf, "FONT FAMILY;%s", font_family);
279 glyphs[num_glyphs].UV = -1;
280 glyphs[num_glyphs].name = strdup(namebuf);
281 glyphs[num_glyphs].comment = strdup(linebuf);
283 if (glyphs[num_glyphs].name == NULL ||
284 glyphs[num_glyphs].comment == NULL)
286 fprintf(stderr, "Memory allocation failure\n");
300 fputs(" NULL\n};\n", f_c);
305 * Write opening comments, etc.
308 static void write_header(FILE *f)
313 for (i = 0; i < 79; ++i)
317 " *\tFont and glyph data for the Wine PostScript driver\n"
319 " *\tCopyright 2001 Ian Pilcher\n"
322 " *\tThis data is derived from the Adobe Glyph list at\n"
325 "http://partners.adobe.com/asn/developer/type/glyphlist.txt\n"
327 " *\tand the Adobe Font Metrics files at\n"
330 "ftp://ftp.adobe.com/pub/adobe/type/win/all/afmfiles/base35/\n"
332 " *\twhich are Copyright 1985-1998 Adobe Systems Incorporated.\n"
336 "#include \"psdrv.h\"\n"
337 "#include \"data/agl.h\"\n", f);
341 * Write the array of glyph names (also populates indexes)
344 static void write_glyph_names(FILE *f_c, FILE *f_h)
346 int i, num_names = 0, index = 0;
348 for (i = 0; i < num_glyphs; ++i)
349 if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
353 " * Every glyph name in the AGL and the 35 core PostScript fonts\n"
357 fprintf(f_c, "const INT PSDRV_AGLGlyphNamesSize = %i;\n\n", num_names);
359 fprintf(f_c, "GLYPHNAME PSDRV_AGLGlyphNames[%i] =\n{\n", num_names);
361 for (i = 0; i < num_glyphs - 1; ++i)
365 if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
367 fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
368 fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
370 cp = fprintf(f_c, " { %4i, \"%s\" },", index, glyphs[i].name);
371 glyphs[i].index = index;
376 glyphs[i].index = glyphs[i - 1].index;
381 fprintf(f_c, "/* %s */\n", glyphs[i].comment);
384 fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
385 fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
387 glyphs[i].index = index;
388 fcpto(f_c, 40, fprintf(f_c, " { %4i, \"%s\" }", index, glyphs[i].name));
389 fprintf(f_c, "/* %s */\n};\n", glyphs[i].comment);
394 * Write the AGL encoding vector, sorted by glyph name
397 static void write_encoding_by_name(FILE *f)
399 int i, size = 0, even = 1;
401 for (i = 0; i < num_glyphs; ++i)
402 if (glyphs[i].UV != -1 &&
403 (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0))
404 ++size; /* should be 1039 */
407 " * The AGL encoding vector, sorted by glyph name - "
408 "duplicates omitted\n"
412 fprintf(f, "const INT PSDRV_AGLbyNameSize = %i;\n\n", size);
413 fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyName[%i] =\n{\n", size);
415 for (i = 0; i < num_glyphs - 1; ++i)
419 if (glyphs[i].UV == -1)
422 if (i != 0 && strcmp(glyphs[i - 1].name, glyphs[i].name) == 0)
425 cp = fprintf(f, " { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
434 fprintf(f, " { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
438 * Write the AGL encoding vector, sorted by Unicode value
441 static void write_encoding_by_UV(FILE *f)
443 int i, size = 0, even = 1;
445 for (i = 0; i < num_glyphs; ++i)
446 if (glyphs[i].UV != -1)
447 ++size; /* better be 1051! */
452 " * The AGL encoding vector, sorted by Unicode value - "
453 "duplicates included\n"
457 fprintf(f, "const INT PSDRV_AGLbyUVSize = %i;\n\n", size);
458 fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyUV[%i] =\n{\n", size);
460 for (i = 0; i < num_glyphs - 1; ++i)
464 if (glyphs[i].UV == -1)
467 cp = fprintf(f, " { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
476 fprintf(f, " { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
484 int main(int argc, char *argv[])
490 fprintf(stderr, "Usage: %s <C file> <header file>\n", argv[0]);
494 f_c = fopen(argv[1], "w");
497 fprintf(stderr, "Error opening %s for writing\n", argv[1]);
501 f_h = fopen(argv[2], "w");
504 fprintf(stderr, "Error opening %s for writing\n", argv[2]);
511 read_afms(f_c, f_h); /* also writes font list */
513 write_glyph_names(f_c, f_h);
515 write_encoding_by_name(f_c);
517 write_encoding_by_UV(f_c);