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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 inline static void sort_by_UV()
60 qsort(glyphs
, num_glyphs
, sizeof(GLYPHINFO
), cmp_by_UV
);
63 inline static void sort_by_name()
65 qsort(glyphs
, num_glyphs
, sizeof(GLYPHINFO
), cmp_by_name
);
68 inline static 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 inline static 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
)
102 inline static void cpto(int newpos
, int curpos
)
104 fcpto(stdout
, newpos
, curpos
);
109 * Make main() look "purty"
112 inline static void double_space(FILE *f
)
117 inline static void triple_space(FILE *f
)
119 fputc('\n', f
); fputc('\n', f
);
124 * Read the Adobe Glyph List from 'glyphlist.txt'
127 static void read_agl()
129 FILE *f
= fopen("glyphlist.txt", "r");
130 char linebuf
[256], namebuf
[128], commbuf
[128];
134 fprintf(stderr
, "Error opening glyphlist.txt\n");
138 while (fgets(linebuf
, sizeof(linebuf
), f
) != NULL
)
142 if (linebuf
[0] == '#')
145 sscanf(linebuf
, "%X;%[^;];%[^\n]", &UV
, namebuf
, commbuf
);
147 glyphs
[num_glyphs
].UV
= (int)UV
;
148 glyphs
[num_glyphs
].name
= strdup(namebuf
);
149 glyphs
[num_glyphs
].comment
= strdup(commbuf
);
151 if (glyphs
[num_glyphs
].name
== NULL
||
152 glyphs
[num_glyphs
].comment
== NULL
)
154 fprintf(stderr
, "Memory allocation failure\n");
163 if (num_glyphs
!= 1051)
165 fprintf(stderr
, "Read %i glyphs\n", num_glyphs
);
172 * Read glyph names from all AFM files in current directory
175 static void read_afms(FILE *f_c
, FILE *f_h
)
177 DIR *d
= opendir(".");
181 " * Built-in font metrics\n"
184 "const AFM *const PSDRV_BuiltinAFMs[] =\n"
190 fprintf(stderr
, "Error opening current directory\n");
194 while ((de
= readdir(d
)) != NULL
)
197 char *cp
, linebuf
[256], font_family
[128];
200 cp
= strrchr(de
->d_name
, '.'); /* Does it end in */
201 if (cp
== NULL
|| strcasecmp(cp
, ".afm") != 0) /* .afm or .AFM? */
204 f
= fopen(de
->d_name
, "r");
207 fprintf(stderr
, "Error opening %s\n", de
->d_name
);
213 if (fgets(linebuf
, sizeof(linebuf
), f
) == NULL
)
215 fprintf(stderr
, "FontName not found in %s\n", de
->d_name
);
219 if (strncmp(linebuf
, "FontName ", 9) == 0)
223 sscanf(linebuf
, "FontName %[^\r\n]", font_family
);
225 for (i
= 0; font_family
[i
] != '\0'; ++i
)
226 if (font_family
[i
] == '-')
227 font_family
[i
] = '_';
229 fprintf(f_h
, "extern const AFM PSDRV_%s;\n", font_family
);
230 fprintf(f_c
, " &PSDRV_%s,\n", font_family
);
234 if (fgets(linebuf
, sizeof(linebuf
), f
) == NULL
)
236 fprintf(stderr
, "FamilyName not found in %s\n", de
->d_name
);
240 if (strncmp(linebuf
, "FamilyName ", 11) == 0)
244 sscanf(linebuf
, "FamilyName %[^\r\n]", font_family
);
248 if (fgets(linebuf
, sizeof(linebuf
), f
) == NULL
)
250 fprintf(stderr
, "StartCharMetrics not found in %s\n",
255 if (strncmp(linebuf
, "StartCharMetrics ", 17) == 0)
259 sscanf(linebuf
, "StartCharMetrics %i", &num_metrics
);
261 for (i
= 0; i
< num_metrics
; ++i
)
265 if (fgets(linebuf
, sizeof(linebuf
), f
) == NULL
)
267 fprintf(stderr
, "Unexpected EOF after %i glyphs in %s\n", i
,
272 cp
= strchr(linebuf
, 'N');
273 if (cp
== NULL
|| strlen(cp
) < 3)
275 fprintf(stderr
, "Parse error after %i glyphs in %s\n", i
,
280 sscanf(cp
, "N %s", namebuf
);
281 if (search_by_name(namebuf
) != NULL
)
284 sprintf(linebuf
, "FONT FAMILY;%s", font_family
);
286 glyphs
[num_glyphs
].UV
= -1;
287 glyphs
[num_glyphs
].name
= strdup(namebuf
);
288 glyphs
[num_glyphs
].comment
= strdup(linebuf
);
290 if (glyphs
[num_glyphs
].name
== NULL
||
291 glyphs
[num_glyphs
].comment
== NULL
)
293 fprintf(stderr
, "Memory allocation failure\n");
307 fputs(" NULL\n};\n", f_c
);
312 * Write opening comments, etc.
315 static void write_header(FILE *f
)
320 for (i
= 0; i
< 79; ++i
)
324 " *\tFont and glyph data for the Wine PostScript driver\n"
326 " *\tCopyright 2001 Ian Pilcher\n"
329 " *\tThis data is derived from the Adobe Glyph list at\n"
332 "http://partners.adobe.com/asn/developer/type/glyphlist.txt\n"
334 " *\tand the Adobe Font Metrics files at\n"
337 "ftp://ftp.adobe.com/pub/adobe/type/win/all/afmfiles/base35/\n"
339 " *\twhich are Copyright 1985-1998 Adobe Systems Incorporated.\n"
343 "#include \"psdrv.h\"\n"
344 "#include \"data/agl.h\"\n", f
);
348 * Write the array of glyph names (also populates indexes)
351 static void write_glyph_names(FILE *f_c
, FILE *f_h
)
353 int i
, num_names
= 0, index
= 0;
355 for (i
= 0; i
< num_glyphs
; ++i
)
356 if (i
== 0 || strcmp(glyphs
[i
- 1].name
, glyphs
[i
].name
) != 0)
360 " * Every glyph name in the AGL and the 35 core PostScript fonts\n"
364 fprintf(f_c
, "const INT PSDRV_AGLGlyphNamesSize = %i;\n\n", num_names
);
366 fprintf(f_c
, "GLYPHNAME PSDRV_AGLGlyphNames[%i] =\n{\n", num_names
);
368 for (i
= 0; i
< num_glyphs
- 1; ++i
)
372 if (i
== 0 || strcmp(glyphs
[i
- 1].name
, glyphs
[i
].name
) != 0)
374 fcpto(f_h
, 32, fprintf(f_h
, "#define GN_%s", glyphs
[i
].name
));
375 fprintf(f_h
, "(PSDRV_AGLGlyphNames + %i)\n", index
);
377 cp
= fprintf(f_c
, " { %4i, \"%s\" },", index
, glyphs
[i
].name
);
378 glyphs
[i
].index
= index
;
383 glyphs
[i
].index
= glyphs
[i
- 1].index
;
388 fprintf(f_c
, "/* %s */\n", glyphs
[i
].comment
);
391 fcpto(f_h
, 32, fprintf(f_h
, "#define GN_%s", glyphs
[i
].name
));
392 fprintf(f_h
, "(PSDRV_AGLGlyphNames + %i)\n", index
);
394 glyphs
[i
].index
= index
;
395 fcpto(f_c
, 40, fprintf(f_c
, " { %4i, \"%s\" }", index
, glyphs
[i
].name
));
396 fprintf(f_c
, "/* %s */\n};\n", glyphs
[i
].comment
);
401 * Write the AGL encoding vector, sorted by glyph name
404 static void write_encoding_by_name(FILE *f
)
406 int i
, size
= 0, even
= 1;
408 for (i
= 0; i
< num_glyphs
; ++i
)
409 if (glyphs
[i
].UV
!= -1 &&
410 (i
== 0 || strcmp(glyphs
[i
- 1].name
, glyphs
[i
].name
) != 0))
411 ++size
; /* should be 1039 */
414 " * The AGL encoding vector, sorted by glyph name - "
415 "duplicates omitted\n"
419 fprintf(f
, "const INT PSDRV_AGLbyNameSize = %i;\n\n", size
);
420 fprintf(f
, "const UNICODEGLYPH PSDRV_AGLbyName[%i] =\n{\n", size
);
422 for (i
= 0; i
< num_glyphs
- 1; ++i
)
426 if (glyphs
[i
].UV
== -1)
429 if (i
!= 0 && strcmp(glyphs
[i
- 1].name
, glyphs
[i
].name
) == 0)
432 cp
= fprintf(f
, " { 0x%.4x, GN_%s },", glyphs
[i
].UV
, glyphs
[i
].name
);
441 fprintf(f
, " { 0x%.4x, GN_%s }\n};\n", glyphs
[i
].UV
, glyphs
[i
].name
);
445 * Write the AGL encoding vector, sorted by Unicode value
448 static void write_encoding_by_UV(FILE *f
)
450 int i
, size
= 0, even
= 1;
452 for (i
= 0; i
< num_glyphs
; ++i
)
453 if (glyphs
[i
].UV
!= -1)
454 ++size
; /* better be 1051! */
459 " * The AGL encoding vector, sorted by Unicode value - "
460 "duplicates included\n"
464 fprintf(f
, "const INT PSDRV_AGLbyUVSize = %i;\n\n", size
);
465 fprintf(f
, "const UNICODEGLYPH PSDRV_AGLbyUV[%i] =\n{\n", size
);
467 for (i
= 0; i
< num_glyphs
- 1; ++i
)
471 if (glyphs
[i
].UV
== -1)
474 cp
= fprintf(f
, " { 0x%.4x, GN_%s },", glyphs
[i
].UV
, glyphs
[i
].name
);
483 fprintf(f
, " { 0x%.4x, GN_%s }\n};\n", glyphs
[i
].UV
, glyphs
[i
].name
);
491 int main(int argc
, char *argv
[])
497 fprintf(stderr
, "Usage: %s <C file> <header file>\n", argv
[0]);
501 f_c
= fopen(argv
[1], "w");
504 fprintf(stderr
, "Error opening %s for writing\n", argv
[1]);
508 f_h
= fopen(argv
[2], "w");
511 fprintf(stderr
, "Error opening %s for writing\n", argv
[2]);
518 read_afms(f_c
, f_h
); /* also writes font list */
520 write_glyph_names(f_c
, f_h
);
522 write_encoding_by_name(f_c
);
524 write_encoding_by_UV(f_c
);