File: gfont_pxtogdf.c

package info (click to toggle)
gfont 1.0.3-1
  • links: PTS
  • area: non-free
  • in suites: sarge, woody
  • size: 504 kB
  • ctags: 513
  • sloc: ansic: 6,465; sh: 823; makefile: 284; perl: 156
file content (580 lines) | stat: -rw-r--r-- 15,611 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*         _____ ___  _   _ _____ 
**    __ _|  ___/ _ \| \ | |_   _|
**   / _` | |_ | | | |  \| | | |  
**  | (_| |  _|| |_| | |\  | | |  
**   \__, |_|   \___/|_| \_| |_|  
**   |___/                        
**  
**  gFONT -- Create GIF image rendered with TeX-available Font
**
**  The gFONT program creates a GIF image for a given ASCII string
**  by the use of an arbitrary TeX-available font (Postscript or
**  METAFONT). The used font is converted from TeX's PK format to
**  gFONT's own GdF format (Gd Font) and rendered into the
**  resulting GIF image by the use of its own enhanced Gd library.
**  The result is intended to be included into HTML pages with an
**  IMG tag.
**
**  ======================================================================
**
**  Copyright (c) 1997 Ralf S. Engelschall, All rights reserved.
**
**  This program is free software; it may be redistributed and/or modified
**  only under the terms of either the Artistic License or the GNU General
**  Public License, which may be found in the ePerl source distribution.
**  Look at the files ARTISTIC and COPYING or run ``eperl -l'' to receive
**  a built-in copy of both license files.
**
**  This program 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 either the
**  Artistic License or the GNU General Public License for more details.
**
**  ======================================================================
**
**  gfont_pxtogdf.c -- PX (pixel) to GdF (Gd Font) converter
*/

/*
**  pxtogdf -- Convert PX fontfile into GDF fontfile format
**  Copyright (c) Ralf S. Engelschall, All Rights Reserved.
**
**  Usage: pxtogdf pxfile.px gdffile.gdf gdifile.gdi
**
**      METAFONT  
**  .mf ----------+ 
** (vector)        \            gftopk         pktopx         pxtogdf            
**                  +--> .gf -----------> .pk ---------> .px ----------> .gdf    
**      Ghostscript/  (rendered)      (packed)        (as pixels)     (for gd)
**  .pfb ---------+
** (vector)     
*/

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <sys/types.h>
#include <time.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#else /* HAVE_STRINGS_H */
#include <string.h>
#endif /* HAVE_STRINGS_H */

#include "lib/xalloc.h"

/* OK and FAIL exits should ALLWAYS exists */
#undef  EX_OK
#define EX_OK   0
#undef  EX_FAIL
#define EX_FAIL 1

/*
 *  CU() -- CleanUp Makro
 *  implemented in a safety way
 */
#define DECL_EXRC int rc
#define ZERO 0
#define STMT(stuff) do { stuff } while (ZERO)
#define CU(returncode) \
            STMT( \
            rc = returncode; goto CUS; )
#define CU_E1(returncode, errstr) \
            STMT( \
            fprintf(stderr, "ERROR: "); \
            fprintf(stderr, errstr); \
            fprintf(stderr, "\n"); \
            rc = returncode; goto CUS; )
#define CU_E2(returncode, errstr1, errstr2) \
            STMT( \
            fprintf(stderr, "ERROR: "); \
            fprintf(stderr, errstr1, errstr2); \
            fprintf(stderr, "\n"); \
            rc = returncode; goto CUS; )
#define CU_E3(returncode, errstr1, errstr2, errstr3) \
            STMT( \
            fprintf(stderr, "ERROR: "); \
            fprintf(stderr, errstr1, errstr2, errstr3); \
            fprintf(stderr, "\n"); \
            rc = returncode; goto CUS; )
#define START_OF_CUS CUS:
#define RETURN_EXRC return (rc)


/*
 *  Yeah, here it comes...all ASCII control codes.
 *  Defined as their correct acronyms.
 */

#define ASC_NUL '\x00'
#define ASC_SOH '\x01'
#define ASC_STX '\x02'
#define ASC_ETX '\x03'
#define ASC_EOT '\x04'
#define ASC_ENQ '\x05'
#define ASC_ACK '\x06'
#define ASC_BEL '\x07'
#define ASC_BS  '\x08'
#define ASC_HT  '\x09'
#define ASC_LF  '\x0a'
#define ASC_VT  '\x0b'
#define ASC_FF  '\x0c'
#define ASC_CR  '\x0d'
#define ASC_SO  '\x0e'
#define ASC_SI  '\x0f'
#define ASC_DLE '\x10'
#define ASC_DC1 '\x11'
#define ASC_DC2 '\x12'
#define ASC_DC3 '\x13'
#define ASC_DC4 '\x14'
#define ASC_NAK '\x15'
#define ASC_SYN '\x16'
#define ASC_ETB '\x17'
#define ASC_CAN '\x18'
#define ASC_EM  '\x19'
#define ASC_SUB '\x1a'
#define ASC_ESC '\x1b'
#define ASC_FS  '\x1c'
#define ASC_GS  '\x1d'
#define ASC_RS  '\x1e'
#define ASC_US  '\x1f'
#define ASC_SP  '\x20'
#define ASC_DEL '\x7f'
#ifndef NUL
#define NUL ASC_NUL
#endif

#define ASC_QUOTE '\x22'
#define ASC_NL    ASC_LF


/*   define the boolean values in a general and
 *   portable way.
 */
#define TRUE  (0 || !(0))
#define FALSE (!(TRUE))
typedef enum { false = FALSE, true = TRUE } bool;

/*   some other names for true and false */
#define YES   TRUE
#define NO    FALSE

#define GOOD  TRUE
#define WRONG FALSE

#define OK    TRUE
#define BAD   FALSE

#define SOME  TRUE
#define NONE  FALSE

/*
 *   define NULL if not already defined
 */
#ifndef NULL
#define NULL (void *)0
#endif


/*   define some handy str* functions
 */
#define   strNE(s1, s2)          (strcmp(s1, s2))
#define   strEQ(s1, s2)         (!strcmp(s1, s2))
#define  strnNE(s1, s2, l)      (strncmp(s1, s2, l))
#define  strnEQ(s1, s2, l)     (!strncmp(s1, s2, l))
#define strniNE(s1, s2, l)  (strncasecmp(s1, s2, l))
#define strniEQ(s1, s2, l) (!strncasecmp(s1, s2, l))

/*  Needed by lib/error.c */
const char program_name[] = "gfont_pxtogdf";

int fDebug = 0;

void Debug(char *str, ...)
{
    va_list ap;
    char buf[1024];

    va_start(ap, str);
    if (fDebug) {
        vsprintf(buf, str, ap);
        fprintf(stderr, "Debug: %s\n", buf);
    }
    va_end(ap);
    return;
}

char int2binstrBuf[32];

char *int2binstr(unsigned int c)
{
    int i;

    for (i = 0; i < 8*4; i++) {
        if ((c & 0x80000000) == 0)
            int2binstrBuf[i] = '0';
        else
            int2binstrBuf[i] = '1';
        c = c << 1;
    }
    int2binstrBuf[i] = 0;
    return int2binstrBuf;
}

unsigned int Get32(FILE *fp)
{
    unsigned int i;
    unsigned char c;

    i = 0;
    c = fgetc(fp);
    i = (i | c) << 8;
    c = fgetc(fp);
    i = (i | c) << 8;
    c = fgetc(fp);
    i = (i | c) << 8;
    c = fgetc(fp);
    i = (i | c);
  
    return i;
}

void Put32(FILE *fp, int i)
{
#ifdef WORDS_BIGENDIAN /* { */
    i = (((i & 0xff000000L) >> 24) | 
         ((i & 0x00ff0000L) >>  8) |
         ((i & 0x0000ff00L) <<  8) |
         ((i & 0x000000ffL) << 24));
#endif /* } */
    fwrite(&i, sizeof(int), 1, fp);
    return;
}

void Put8(FILE *fp, unsigned char c)
{
    fwrite(&c, sizeof(unsigned char), 1, fp);
    return;
}

#define NUM_CHARS_IN_PK 256

int 
main(int argc, char *argv[])
{
    DECL_EXRC;
    time_t tm;

    char pxfile[512];   /* font data in PX format */
    char gdffile[512];  /* font data in GDF format */
    char gdifile[512];  /* font data in GDF format */
    char fontname[512]; /* font name */

    FILE *fppx  = NULL;
    FILE *fpgdf = NULL;
    FILE *fpgdi = NULL;

    char *gdfiobuf;
    char *gdiiobuf;

    int int32;

    int sizes_w[NUM_CHARS_IN_PK];
    int sizes_h[NUM_CHARS_IN_PK];
    int offsets_x[NUM_CHARS_IN_PK];
    int offsets_y[NUM_CHARS_IN_PK];
    int rasterpointer[NUM_CHARS_IN_PK];
    int tfmwidth_w[NUM_CHARS_IN_PK];
    int tfmwidth_h[NUM_CHARS_IN_PK];
    int sizes_wMax;
    int sizes_hMax;
    int abs_wMax;
    int abs_wMin;
    int abs_hMax;
    int abs_hMin;

    int xdim, ydim, ybase;
    int xoffs;

    int checksum;
    int magnification;
    int designsize;
    int designsize_w;
    int designsize_h;
    int dirptr;

    int i, k, l;
    char buf[1000];
    char *cp;


    if (argc != 4) {
        fprintf(stderr, "Usage: pxtogdf <pxfile>.px <gdfile>.gdf <gdifile>.gdi\n");
        exit(1);
    }
    strcpy(pxfile,   argv[1]);
    strcpy(gdffile,  argv[2]);
    strcpy(gdifile,  argv[3]);
    strcpy(fontname, argv[2]);
    cp = strchr(fontname, '.');
    if (cp != NULL)
        *cp = '\0';

    tm = time(NULL);

    if ((fppx = fopen(pxfile, "r")) == NULL)
        CU_E2(EX_FAIL, "cannot open input file '%s'", pxfile);
    if ((fpgdf = fopen(gdffile, "w")) == NULL)
        CU_E2(EX_FAIL, "cannot open output file '%s'", gdffile);
    if ((fpgdi = fopen(gdifile, "w")) == NULL)
        CU_E2(EX_FAIL, "cannot open output file '%s'", gdifile);

    gdfiobuf = (char *) xmalloc(16384);
    gdiiobuf = (char *) xmalloc(16384);
    setvbuf(fpgdf, gdfiobuf, _IOFBF, 16384);
    setvbuf(fpgdi, gdiiobuf, _IOFBF, 16384);

    /*
     *
     *  READ PART 1: HEADER
     *
     */

    int32 = Get32(fppx);
    if (int32 != 1001)
        CU_E2(EX_FAIL, "input file '%s' not in PX format", pxfile);
    Debug("read: START_ID=1001");


    /*
     *
     *  READ PART 3: CHARACTER INFORMATION
     *
     */

    fseek(fppx, -(sizeof(int)*(NUM_CHARS_IN_PK*4+5)), SEEK_END);

    sizes_hMax = 0;
    sizes_wMax = 0;
    abs_wMin = abs_wMax = abs_hMin = abs_hMax = 0;

    for (i = 0; i < NUM_CHARS_IN_PK; i++) {

        /*  read size information of char and decode
            it into a width (=X) and height (=Y) parts          */
        int32 = Get32(fppx);
        sizes_w[i] = int32 / 65536;
        sizes_h[i] = int32 % 65536;
        /*  ajust max sizes  */
        sizes_wMax = sizes_w[i] > sizes_wMax ? sizes_w[i] : sizes_wMax;
        sizes_hMax = sizes_h[i] > sizes_hMax ? sizes_h[i] : sizes_hMax;
      
        /*  read offset information of char and decode
            it into a X and Y parts          */
        int32 = Get32(fppx);
        offsets_x[i] = int32 / 65536;
        if (offsets_x[i] >= 128) 
            offsets_x[i] -= 256;
        offsets_y[i] = int32 % 65536;
        if (offsets_y[i] >= 128) 
            offsets_y[i] -= 256;
        /*  special "if" because there exists offsets = 2 in cmr...  */
        /*
        if (offsets_y[i] < -100) {
            offsets_y[i] = offsets_y[i] & 0x0000ffff;
        }
        */

        /*  ajust absolute max sizes  */
        abs_wMin = -offsets_x[i] < abs_wMin ? -offsets_x[i] : abs_wMin;
        abs_wMax = -offsets_x[i]+sizes_w[i] > abs_wMax ?  -offsets_x[i]+sizes_w[i] : abs_wMax;
        abs_hMin = -offsets_y[i] < abs_hMin ? -offsets_y[i] : abs_hMin;
        abs_hMax = -offsets_y[i]+sizes_h[i] > abs_hMax ?  -offsets_y[i]+sizes_h[i] : abs_hMax;

        /*  read in direct fseek() offset of character,
            calculated as sizeof(int) blocks, i.e. 4 chars usually */
        rasterpointer[i] = Get32(fppx);

        /*  read in TeX Font Metirc width and decode it  */
        int32 = Get32(fppx);
        tfmwidth_w[i] = int32 / 65536 - 1;
        tfmwidth_h[i] = int32 % 65536;

        Debug("read: INFO of char #%03d: size = %2dx%2d, offset = (x=%3d,y=%3d), rasterpointer=%6d",
            i, sizes_w[i], sizes_h[i], offsets_x[i], offsets_y[i], rasterpointer[i]);
    }
    Debug("sizes_wMax=%d, sizes_hMax=%d", sizes_wMax, sizes_hMax);
    Debug("abs_wMin = %d, abs_wMax = %d", abs_wMin, abs_wMax);
    Debug("abs_hMin = %d, abs_hMax = %d", abs_hMin, abs_hMax);

    xdim  = abs(abs_wMin)+abs_wMax;
    ybase = abs(abs_hMin);
    ydim  = abs(abs_hMin)+abs_hMax;

    Debug("Char Block = %d x %d (%d base)", xdim, ydim, ybase); 


    /*
     *
     *  READ PART 4: TOTAL INFORMATION
     *
     */

    checksum = Get32(fppx);
    Debug("checksum=%d", checksum);

    magnification = Get32(fppx);
    Debug("magnification=%d", magnification);

    designsize = Get32(fppx);
    designsize_w = designsize / 65536;
    designsize_h = designsize % 65536;
    Debug("designsize=%d (%dx%d)", designsize, designsize_w, designsize_h);

    dirptr = Get32(fppx);
    Debug("dirptr=%d", dirptr);


    /*
     *
     *  READ PART 5: FOOTER
     *
     */

    Debug("END_ID = %d", Get32(fppx));


    /*
     *
     *  WRITE HEADER
     *
     */

    Put8(fpgdf, 'G');
    Put8(fpgdf, 'D');
    Put8(fpgdf, 'F');
    Put8(fpgdf, '\n');

    Put32(fpgdf, NUM_CHARS_IN_PK); /* num of chars */
    Put32(fpgdf, 0);               /* offset of first char */
    Put32(fpgdf, xdim);            /* character width */
    Put32(fpgdf, ydim);            /* character height */

    for (i = 0; i < NUM_CHARS_IN_PK; i++) {
        Put32(fpgdf, -offsets_x[i]+sizes_w[i]);
    }

    fprintf(fpgdi, "______________________________________________________________\n");
    fprintf(fpgdi, "\n");
    fprintf(fpgdi, "  Gd Font Information (GDI)\n");
    fprintf(fpgdi, "\n");
    fprintf(fpgdi, "  Font Name: %s\n", fontname);
    fprintf(fpgdi, "  Font Dims: %d x %d (%d base)\n", xdim, ydim, ybase); 
    fprintf(fpgdi, "\n");
    fprintf(fpgdi, "  Created on %s", ctime(&tm));
    fprintf(fpgdi, "  via ``pxtogdf %s %s %s''\n", pxfile, gdffile, gdifile);
    fprintf(fpgdi, "______________________________________________________________\n");
    fprintf(fpgdi, "\n");
    fprintf(fpgdi, "\n");


    /*
     *
     *  READ PART 2: CHARACTER DATA
     *  WRITE GDI CHARACTER INFO
     *  WRITE GDF CHARACTER DATA
     *
     */


    for (i = 0; i < NUM_CHARS_IN_PK; i++) {
        fseek(fppx, sizeof(int)*rasterpointer[i], SEEK_SET);

        fprintf(fpgdi, " char: # %-3d    design size: %2d x %2d\n", i, sizes_w[i], sizes_h[i]);
        fprintf(fpgdi, " width: %-3d          offset: x = %d, y = %d\n", -offsets_x[i]+sizes_w[i], offsets_x[i], offsets_y[i]);
        fprintf(fpgdi, "\n");
        fprintf(fpgdi, "     ");
        for (k = 1; k <= xdim; k++) {
            if (k % 10 == 0)
                fprintf(fpgdi, "%d", k / 10);
            else
                fprintf(fpgdi, " ");
        }
        fprintf(fpgdi, "\n");
        fprintf(fpgdi, "     ");
        for (k = 1; k <= xdim; k++) {
            fprintf(fpgdi, "%d", k % 10);
        }
        fprintf(fpgdi, "\n");
 

        /*  first zero bytes up to line where real data starts  */
        for (l = 0; l < ybase-offsets_y[i]; l++) {
            fprintf(fpgdi, " %3d ", l+1);
            for (k = 0; k < xdim; k++) {
                fprintf(fpgdi, ".");
                Put8(fpgdf, 0);
            }
            fprintf(fpgdi, "\n");
        }

        /*  real data lines  */
        for (l = ybase-offsets_y[i]; l < ybase+(-offsets_y[i])+sizes_h[i]; l++) {
            fprintf(fpgdi, " %3d ", l+1);
            xoffs = (-offsets_x[i] < 0 ? 0 : -offsets_x[i]);

            /* before real data in the line */
            for (k = 0; k < xoffs; k++) {
                fprintf(fpgdi, ".");
                Put8(fpgdf, 0);
            }

            /* the real data in the line */
            strcpy(buf, "");
            for (k = 0; k < (sizes_w[i] + 31) / 32; k++) {
                int32 = Get32(fppx);
                sprintf(buf+strlen(buf), "%s", int2binstr(int32));
            }
            buf[sizes_w[i]] = '\0';
            for (k = 0; buf[k] != '\0'; k++) {
                fprintf(fpgdi, "%c", buf[k] == '0' ? '.' : '*');
                Put8(fpgdf, buf[k] == '0' ? 0 : 1);
            }

            /* after real data in the line */
            for (k = xoffs+sizes_w[i]; k < xdim; k++) {
                fprintf(fpgdi, ".");
                Put8(fpgdf, 0);
            }

            fprintf(fpgdi, "\n");
        }
        
        /*  from the last real data line up to the last line */
        for (l = ybase+-offsets_y[i]+sizes_h[i]; l < ydim; l++) {
            fprintf(fpgdi, " %3d ", l+1);
            for (k = 0; k < xdim; k++) {
                fprintf(fpgdi, ".");
                Put8(fpgdf, 0);
            }
            fprintf(fpgdi, "\n");
        }
        fprintf(fpgdi, "\n\n");

    }
    CU(EX_OK);

    START_OF_CUS
    if (fppx)
        fclose(fppx);
    if (fpgdf)
        fclose(fpgdf);
    if (fpgdi)
        fclose(fpgdi);

    RETURN_EXRC;
}

/*EOF*/