mkagl.c 10.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2001 Ian Pilcher
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
17 18
 */

19 20 21 22 23 24 25 26 27 28 29
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>


/*
 *  The array of glyph information
 */
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
typedef struct
{
    int     	UV;
    int     	index;	    	/* in PSDRV_AGLGlyphNames */
    const char	*name;
    const char	*comment;

} GLYPHINFO;

static GLYPHINFO    glyphs[1500];
static int  	    num_glyphs = 0;


/*
 *  Functions to search and sort the array
 */
47

48 49 50 51 52 53 54 55 56 57
static int cmp_by_UV(const void *a, const void *b)
{
    return ((const GLYPHINFO *)a)->UV - ((const GLYPHINFO *)b)->UV;
}

static int cmp_by_name(const void *a, const void *b)
{
    return strcmp(((const GLYPHINFO *)a)->name, ((const GLYPHINFO *)b)->name);
}

58
static inline void sort_by_UV(void)
59 60 61 62
{
    qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_UV);
}

63
static inline void sort_by_name(void)
64 65 66 67
{
    qsort(glyphs, num_glyphs, sizeof(GLYPHINFO), cmp_by_name);
}

68
static inline GLYPHINFO *search_by_name(const char *name)
69 70
{
    GLYPHINFO	gi;
71

72
    gi.name = name;
73

74 75 76 77 78 79 80 81
    return (GLYPHINFO *)bsearch(&gi, glyphs, num_glyphs, sizeof(GLYPHINFO),
    	    cmp_by_name);
}


/*
 *  Use the 'optimal' combination of tabs and spaces to position the cursor
 */
82

83
static inline void fcpto(FILE *f, int newpos, int curpos)
84 85 86
{
    int newtpos = newpos & ~7;
    int curtpos = curpos & ~7;
87

88 89 90 91 92 93
    while (curtpos < newtpos)
    {
    	fputc('\t', f);
	curtpos += 8;
	curpos = curtpos;
    }
94

95 96 97 98 99 100 101 102 103 104
    while (curpos < newpos)
    {
    	fputc(' ', f);
	++curpos;
    }
}

/*
 *  Make main() look "purty"
 */
105

106
static inline void triple_space(FILE *f)
107 108 109 110 111 112 113 114
{
    fputc('\n', f);  fputc('\n', f);
}


/*
 *  Read the Adobe Glyph List from 'glyphlist.txt'
 */
115

116
static void read_agl(void)
117 118 119
{
    FILE    *f = fopen("glyphlist.txt", "r");
    char    linebuf[256], namebuf[128], commbuf[128];
120

121 122 123 124 125
    if (f == NULL)
    {
    	fprintf(stderr, "Error opening glyphlist.txt\n");
	exit(__LINE__);
    }
126

127 128 129
    while (fgets(linebuf, sizeof(linebuf), f) != NULL)
    {
    	unsigned int	UV;
130

131 132
    	if (linebuf[0] == '#')
	    continue;
133

134
    	sscanf(linebuf, "%X;%[^;];%[^\n]", &UV, namebuf, commbuf);
135

136 137 138
	glyphs[num_glyphs].UV = (int)UV;
	glyphs[num_glyphs].name = strdup(namebuf);
	glyphs[num_glyphs].comment = strdup(commbuf);
139

140 141 142 143 144 145
	if (glyphs[num_glyphs].name == NULL ||
	    	glyphs[num_glyphs].comment == NULL)
	{
	    fprintf(stderr, "Memory allocation failure\n");
	    exit(__LINE__);
	}
146

147 148
	++num_glyphs;
    }
149

150
    fclose(f);
151

152 153 154 155 156 157 158 159 160 161 162
    if (num_glyphs != 1051)
    {
    	fprintf(stderr, "Read %i glyphs\n", num_glyphs);
	exit(__LINE__);
    }
}


/*
 *  Read glyph names from all AFM files in current directory
 */
163

164
static void read_afms(FILE *f_c, FILE *f_h)
165 166 167
{
    DIR     	    *d = opendir(".");
    struct dirent   *de;
168

169 170 171 172
    fputs(  "/*\n"
    	    " *  Built-in font metrics\n"
	    " */\n"
	    "\n"
173
	    "const AFM *const PSDRV_BuiltinAFMs[] =\n"
174
	    "{\n", f_c);
175 176


177 178 179 180 181
    if (d == NULL)
    {
    	fprintf(stderr, "Error opening current directory\n");
	exit(__LINE__);
    }
182

183 184 185 186 187
    while ((de = readdir(d)) != NULL)
    {
    	FILE   	*f;
	char   	*cp, linebuf[256], font_family[128];
	int	i, num_metrics;
188

189 190 191
	cp = strrchr(de->d_name, '.');	    	    	/* Does it end in   */
	if (cp == NULL || strcasecmp(cp, ".afm") != 0)	/*   .afm or .AFM?  */
	    continue;
192

193 194 195 196 197 198
	f = fopen(de->d_name, "r");
	if (f == NULL)
	{
	    fprintf(stderr, "Error opening %s\n", de->d_name);
	    exit(__LINE__);
	}
199

200 201 202 203 204 205 206
	while (1)
	{
	    if (fgets(linebuf, sizeof(linebuf), f) == NULL)
	    {
	    	fprintf(stderr, "FontName not found in %s\n", de->d_name);
		exit(__LINE__);
	    }
207

208 209 210
	    if (strncmp(linebuf, "FontName ", 9) == 0)
	    	break;
	}
211

212
	sscanf(linebuf, "FontName %[^\r\n]", font_family);
213

214 215 216
	for (i = 0; font_family[i] != '\0'; ++i)
	    if (font_family[i] == '-')
	    	font_family[i] = '_';
217

218
	fprintf(f_h, "extern const AFM PSDRV_%s;\n", font_family);
219
	fprintf(f_c, "    &PSDRV_%s,\n", font_family);
220

221 222 223 224 225 226 227
	while (1)
	{
	    if (fgets(linebuf, sizeof(linebuf), f) == NULL)
	    {
	    	fprintf(stderr, "FamilyName not found in %s\n", de->d_name);
		exit(__LINE__);
	    }
228

229 230 231
	    if (strncmp(linebuf, "FamilyName ", 11) == 0)
	    	break;
	}
232

233
	sscanf(linebuf, "FamilyName %[^\r\n]", font_family);
234

235 236 237 238 239 240 241 242
	while (1)
	{
	    if (fgets(linebuf, sizeof(linebuf), f) == NULL)
	    {
	    	fprintf(stderr, "StartCharMetrics not found in %s\n",
		    	de->d_name);
		exit(__LINE__);
	    }
243

244 245 246
	    if (strncmp(linebuf, "StartCharMetrics ", 17) == 0)
	    	break;
	}
247

248
	sscanf(linebuf, "StartCharMetrics %i", &num_metrics);
249

250 251 252
	for (i = 0; i < num_metrics; ++i)
	{
	    char    namebuf[128];
253

254 255 256 257 258 259
	    if (fgets(linebuf, sizeof(linebuf), f) == NULL)
	    {
	    	fprintf(stderr, "Unexpected EOF after %i glyphs in %s\n", i,
		    	de->d_name);
		exit(__LINE__);
	    }
260

261 262 263 264 265 266 267
	    cp = strchr(linebuf, 'N');
	    if (cp == NULL || strlen(cp) < 3)
	    {
	    	fprintf(stderr, "Parse error after %i glyphs in %s\n", i,
		    	de->d_name);
		exit(__LINE__);
	    }
268

269 270 271
	    sscanf(cp, "N %s", namebuf);
	    if (search_by_name(namebuf) != NULL)
	    	continue;
272

273
	    sprintf(linebuf, "FONT FAMILY;%s", font_family);
274

275 276 277
	    glyphs[num_glyphs].UV = -1;
	    glyphs[num_glyphs].name = strdup(namebuf);
	    glyphs[num_glyphs].comment = strdup(linebuf);
278

279 280 281 282 283 284
	    if (glyphs[num_glyphs].name == NULL ||
	    	    glyphs[num_glyphs].comment == NULL)
	    {
	    	fprintf(stderr, "Memory allocation failure\n");
		exit(__LINE__);
	    }
285

286
	    ++num_glyphs;
287

288 289
	    sort_by_name();
	}
290

291 292
	fclose(f);
    }
293

294
    closedir(d);
295

296
    fputs("    NULL\n};\n", f_c);
297 298 299 300 301 302
}


/*
 *  Write opening comments, etc.
 */
303

304 305 306 307 308 309 310 311 312
static void write_header(FILE *f)
{
    int i;

    fputc('/', f);
    for (i = 0; i < 79; ++i)
    	fputc('*', f);
    fputs("\n"
    	    " *\n"
313
	    " *\tFont and glyph data for the Wine PostScript driver\n"
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    	    " *\n"
	    " *\tCopyright 2001 Ian Pilcher\n"
	    " *\n"
	    " *\n"
	    " *\tThis data is derived from the Adobe Glyph list at\n"
    	    " *\n"
	    " *\t    "
	    "http://partners.adobe.com/asn/developer/type/glyphlist.txt\n"
	    " *\n"
	    " *\tand the Adobe Font Metrics files at\n"
	    " *\n"
	    " *\t    "
	    "ftp://ftp.adobe.com/pub/adobe/type/win/all/afmfiles/base35/\n"
	    " *\n"
	    " *\twhich are Copyright 1985-1998 Adobe Systems Incorporated.\n"
	    " *\n"
	    " */\n"
	    "\n"
332 333
	    "#include \"psdrv.h\"\n"
	    "#include \"data/agl.h\"\n", f);
334 335 336
}

/*
337
 *  Write the array of glyph names (also populates indexes)
338
 */
339

340
static void write_glyph_names(FILE *f_c, FILE *f_h)
341
{
342
    int i, num_names = 0, index = 0;
343

344 345 346
    for (i = 0; i < num_glyphs; ++i)
    	if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
	    ++num_names;
347

348
    fputs(  "/*\n"
349
    	    " *  Every glyph name in the AGL and the 35 core PostScript fonts\n"
350
	    " */\n"
351
	    "\n", f_c);
352

353
    fprintf(f_c, "const INT PSDRV_AGLGlyphNamesSize = %i;\n\n", num_names);
354

355
    fprintf(f_c, "GLYPHNAME PSDRV_AGLGlyphNames[%i] =\n{\n", num_names);
356

357 358 359
    for (i = 0; i < num_glyphs - 1; ++i)
    {
    	int cp = 0;
360

361 362
	if (i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0)
	{
363 364
	    fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
	    fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
365

366
	    cp = fprintf(f_c, "    { %4i, \"%s\" },", index, glyphs[i].name);
367 368 369 370 371
	    glyphs[i].index = index;
	    ++index;
	}
	else
	{
372
	    glyphs[i].index = glyphs[i - 1].index;
373
	}
374

375
	fcpto(f_c, 40, cp);
376

377
	fprintf(f_c, "/* %s */\n", glyphs[i].comment);
378
    }
379

380 381
    fcpto(f_h, 32, fprintf(f_h, "#define GN_%s", glyphs[i].name));
    fprintf(f_h, "(PSDRV_AGLGlyphNames + %i)\n", index);
382

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);
386 387 388 389
}


/*
390 391 392 393 394 395
 *  Write the AGL encoding vector, sorted by glyph name
 */

static void write_encoding_by_name(FILE *f)
{
    int i, size = 0, even = 1;
396

397 398 399 400
    for (i = 0; i < num_glyphs; ++i)
    	if (glyphs[i].UV != -1 &&
	    	(i == 0 || strcmp(glyphs[i - 1].name, glyphs[i].name) != 0))
	    ++size; 	    	    /* should be 1039 */
401

402 403 404 405 406
    fputs(  "/*\n"
    	    " *  The AGL encoding vector, sorted by glyph name - "
	    	    "duplicates omitted\n"
	    " */\n"
	    "\n", f);
407

408
    fprintf(f, "const INT PSDRV_AGLbyNameSize = %i;\n\n", size);
409 410
    fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyName[%i] =\n{\n", size);

411 412 413
    for (i = 0; i < num_glyphs - 1; ++i)
    {
    	int cp;
414

415 416
    	if (glyphs[i].UV == -1)
	    continue;
417

418 419
	if (i != 0 && strcmp(glyphs[i - 1].name, glyphs[i].name) == 0)
	    continue;
420

421
    	cp = fprintf(f, "    { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
422

423 424 425 426 427 428
	even = !even;
	if (even)
	    fputc('\n', f);
	else
	    fcpto(f, 40, cp);
    }
429

430 431 432 433 434
    fprintf(f, "    { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
}

/*
 *  Write the AGL encoding vector, sorted by Unicode value
435
 */
436

437
static void write_encoding_by_UV(FILE *f)
438
{
439
    int i, size = 0, even = 1;
440

441 442 443
    for (i = 0; i < num_glyphs; ++i)
    	if (glyphs[i].UV != -1)
	    ++size; 	    	    	/* better be 1051! */
444

445
    sort_by_UV();
446

447
    fputs(  "/*\n"
448 449
    	    " *  The AGL encoding vector, sorted by Unicode value - "
	    	    "duplicates included\n"
450 451
	    " */\n"
	    "\n", f);
452 453

    fprintf(f, "const INT PSDRV_AGLbyUVSize = %i;\n\n", size);
454 455
    fprintf(f, "const UNICODEGLYPH PSDRV_AGLbyUV[%i] =\n{\n", size);

456 457
    for (i = 0; i < num_glyphs - 1; ++i)
    {
458
    	int cp;
459

460 461
    	if (glyphs[i].UV == -1)
	    continue;
462

463
	cp = fprintf(f, "    { 0x%.4x, GN_%s },", glyphs[i].UV, glyphs[i].name);
464

465 466 467 468 469
	even = !even;
	if (even)
	    fputc('\n', f);
	else
	    fcpto(f, 40, cp);
470
    }
471

472
    fprintf(f, "    { 0x%.4x, GN_%s }\n};\n", glyphs[i].UV, glyphs[i].name);
473
}
474

475 476 477 478

/*
 *  Do it!
 */
479

480 481
int main(int argc, char *argv[])
{
482
    FILE    *f_c, *f_h;
483

484
    if (argc < 3)
485
    {
486 487
    	fprintf(stderr, "Usage: %s <C file> <header file>\n", argv[0]);
	exit(__LINE__);
488
    }
489 490 491

    f_c = fopen(argv[1], "w");
    if (f_c == NULL)
492
    {
493 494 495
	fprintf(stderr, "Error opening %s for writing\n", argv[1]);
	exit(__LINE__);
    }
496

497 498 499 500 501
    f_h = fopen(argv[2], "w");
    if (f_h == NULL)
    {
    	fprintf(stderr, "Error opening %s for writing\n", argv[2]);
	exit(__LINE__);
502
    }
503

504 505 506 507 508 509 510 511 512 513
    write_header(f_c);
    triple_space(f_c);
    read_agl();
    read_afms(f_c, f_h);    	    /* also writes font list */
    triple_space(f_c);
    write_glyph_names(f_c, f_h);
    triple_space(f_c);
    write_encoding_by_name(f_c);
    triple_space(f_c);
    write_encoding_by_UV(f_c);
514

515 516
    return 0;
}