File: gfont_main.c

package info (click to toggle)
gfont 1.0.2-5
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 640 kB
  • ctags: 457
  • sloc: ansic: 6,265; sh: 452; makefile: 225; perl: 179
file content (517 lines) | stat: -rw-r--r-- 16,026 bytes parent folder | download | duplicates (2)
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
/*         _____ ___  _   _ _____ 
**    __ _|  ___/ _ \| \ | |_   _|
**   / _` | |_ | | | |  \| | | |  
**  | (_| |  _|| |_| | |\  | | |  
**   \__, |_|   \___/|_| \_| |_|  
**   |___/                        
**  
**  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_main.c -- main program
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "gfont_gd.h"
#include "gfont_gdfontl.h"
#include "gfont_getopt.h"

#include "config_sc.h"

int fVerbose = 0;

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

    va_start(ap, str);
    if (fVerbose) {
        vsprintf(buf, str, ap);
        fprintf(stderr, "%s", buf);
    }
    va_end(ap);
    return;
}

int hex2int(char c)
{
    if (c >= 'a' && c <= 'f')
        return (c - 'a' + 10);
    else if (c >= 'A' && c <= 'F')
        return (c - 'A' + 10);
    else
        return (c - '0');
}

void hextriple2gdcolor(char *str, int *r, int *g, int *b)
{
    *r = (hex2int(str[0]) * 16 + hex2int(str[1]));
    *g = (hex2int(str[2]) * 16 + hex2int(str[3]));
    *b = (hex2int(str[4]) * 16 + hex2int(str[5]));
}

#define ALIGN_L 0
#define ALIGN_C 1
#define ALIGN_R 2
#define ALIGN_T 3
#define ALIGN_M 4
#define ALIGN_B 5

int parsesizespec(char *str, int *xrel, int *xsize, int *xalign,
                              int *yrel, int *ysize, int *yalign)
{
    char spec[128];
    char *xspec;
    char *yspec;
    char *cp;

    strcpy(spec, str);
    if ((cp = index(spec, 'x')) == NULL)
        return 1;
    *cp++ = '\0';
    xspec = spec;
    yspec = cp;

    *xrel = 0;
    if (*xspec == '+') {
        *xrel = 1;
        xspec++;
    }
    *xalign = ALIGN_L;
    if (*(xspec+(strlen(xspec)-1)) == 'L') {
        *xalign = ALIGN_L;
        *(xspec+(strlen(xspec)-1)) = '\0';
    }
    else if (*(xspec+(strlen(xspec)-1)) == 'C') {
        *xalign = ALIGN_C;
        *(xspec+(strlen(xspec)-1)) = '\0';
    }
    else if (*(xspec+(strlen(xspec)-1)) == 'R') {
        *xalign = ALIGN_R;
        *(xspec+(strlen(xspec)-1)) = '\0';
    }
    *xsize = atoi(xspec);
    if (*xsize < 0) {
        *xsize = 0;
        *xrel  = 1;
    }

    *yrel = 0;
    if (*yspec == '+') {
        *yrel = 1;
        yspec++;
    }
    *yalign = ALIGN_T;
    if (*(yspec+(strlen(yspec)-1)) == 'T') {
        *yalign = ALIGN_T;
        *(yspec+(strlen(yspec)-1)) = '\0';
    }
    else if (*(yspec+(strlen(yspec)-1)) == 'M') {
        *yalign = ALIGN_M;
        *(yspec+(strlen(yspec)-1)) = '\0';
    }
    else if (*(yspec+(strlen(yspec)-1)) == 'B') {
        *yalign = ALIGN_B;
        *(yspec+(strlen(yspec)-1)) = '\0';
    }
    *ysize = atoi(yspec);
    if (*ysize < 0) {
        *ysize = 0;
        *yrel  = 1;
    }

    return 0; 
}

int gdImageAntiAlias(gdImage *ip, 
                     int x0, int y0, int x1, int y1, 
                     int antialias, int bgcolor)
{
    int x, y;
    int p;
    int p_u, p_d, p_l, p_r;
    int r_u, r_d, r_l, r_r;
    int g_u, g_d, g_l, g_r;
    int b_u, b_d, b_l, b_r;
    int r_q, g_q, b_q;
    int q;

    if (!gdImageBoundsSafe(ip, x0, y0) || !gdImageBoundsSafe(ip, x1, y1))
        return 1;

    for (x = x0+1; x <= x1-1; x++) {
        for (y = y0+1; y <= y1-1; y++) {
            p = gdImageGetPixel(ip, x, y);
            if (p != bgcolor)
                continue;
            p_l = gdImageGetPixel(ip, x-1, y);
            r_l = ip->red[p_l]; g_l = ip->blue[p_l]; b_l = ip->blue[p_l];
            p_u = gdImageGetPixel(ip, x, y-1);
            r_u = ip->red[p_u]; g_u = ip->blue[p_u]; b_u = ip->blue[p_u];
            p_r = gdImageGetPixel(ip, x+1, y);
            r_r = ip->red[p_r]; g_r = ip->blue[p_r]; b_r = ip->blue[p_r];
            p_d = gdImageGetPixel(ip, x, y+1);
            r_d = ip->red[p_d]; g_d = ip->blue[p_d]; b_d = ip->blue[p_d];

            r_q = (unsigned char)((r_l + r_u + r_r + r_d) / 4);
            g_q = (unsigned char)((g_l + g_u + g_r + g_d) / 4);
            b_q = (unsigned char)((b_l + b_u + b_r + b_d) / 4);

            q = gdImageColorClosest(ip, r_q+64, g_q+64, b_q+64);
            gdImageSetPixel(ip, x, y, q);
        }
    }
    return 0;
}

extern char gFONT_Hello[];

void give_version(void)
{
    fprintf(stderr, "%s\n", gFONT_Hello);
    fprintf(stdout, "\n");
    fprintf(stdout, "Copyright (c) 1997 Ralf S. Engelschall, All rights reserved.\n");
    fprintf(stdout, "\n");
    fprintf(stdout, "This program is distributed in the hope that it will be useful,\n");
    fprintf(stdout, "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
    fprintf(stdout, "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
    fprintf(stdout, "the GNU General Public License for more details.\n");
    fprintf(stdout, "\n");
}


void give_usage(char *name)
{
    fprintf(stderr, "Usage: %s [options] string\n", name);
    fprintf(stderr, "\n");
    fprintf(stderr, "Rendering Parameters:\n");
    fprintf(stderr, "   -b, --background=RRGGBB  set background color (default: `ffffff')\n");
    fprintf(stderr, "   -f, --foreground=RRGGBB  set foreground color (default: `000000')\n");
    fprintf(stderr, "   -F, --fontname=NAME      set specific font (default `CMBoldface-V')\n");
    fprintf(stderr, "   -G, --gdf-path=DIR       add DIR to the path where GdF files are searched\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Output Parameters:\n");
    fprintf(stderr, "   -a, --anti-alias=N       apply anti-alias filter with N colors\n");
    fprintf(stderr, "   -c, --crop               crop image to real edges after rendering\n");
    fprintf(stderr, "   -r, --resize=SPEC        resize the image to a particular result size\n");
    fprintf(stderr, "   -i, --interlace          enable GIF Interlacing feature\n");
    fprintf(stderr, "   -o, --output-file=FILE   set the output file (default `out.gif')\n");
    fprintf(stderr, "\n");
    fprintf(stderr, "Giving Feedback:\n");
    fprintf(stderr, "   -v, --verbose            enable verbose processing\n");
    fprintf(stderr, "   -V, --version            display version string\n");
    fprintf(stderr, "   -h, --help               display this page\n");
    fprintf(stderr, "\n");
}

struct option options[] = {
    { "background",  1, NULL, 'b' },
    { "foreground",  1, NULL, 'f' },
    { "fontname",    1, NULL, 'F' },
    { "gdf-path",    1, NULL, 'G' },
    { "anti-alias",  0, NULL, 'a' },
    { "crop",        0, NULL, 'c' },
    { "resize",      0, NULL, 'r' },
    { "output-file", 1, NULL, 'o' },
    { "verbose",     0, NULL, 'v' },
    { "version",     0, NULL, 'V' },
    { "help",        0, NULL, 'h' }
};

int
main(int argc, char *argv[]) 
{
    FILE *fp;
    gdImage *ip;
    gdImage *ipn;
    int cbg, cfg;
    gdFontPtr gdfp;
    int x, y;
    int i, h, w;
    int nX, nY, nW, nH;
    char buffer[20];
    char ca[1024];
    char cmd[1024];
    int antialias = 0;
    int fCrop = 0;
    int fResize = 0;
    int fInterlace = 0;
    char *outputfile = "out.gif";
    int bg_r, bg_g, bg_b;
    int fg_r, fg_g, fg_b;
    char c;
    char *string;
    char *font = "CMBoldface-V";
    int r, g, b;
    int d_r, d_g, d_b;
    char *gdfpath = FONTPATH;
    int bgnondefault = 0;
    char *cp;
    int cont;
    int ss_xrel, ss_xsize, ss_xalign;
    int ss_yrel, ss_ysize, ss_yalign;

    hextriple2gdcolor("ffffff", &bg_r, &bg_g, &bg_b);
    hextriple2gdcolor("000000", &fg_r, &fg_g, &fg_b);

    /*  parse the option arguments */
    opterr = 0;
    while ((c = getopt_long(argc, argv, ":b:f:a:F:G:cir:o:vVh", options, NULL)) != -1) {
        if (optarg == NULL) 
            optarg = "(null)";
        switch (c) {
            case 'b':
                hextriple2gdcolor(optarg, &bg_r, &bg_g, &bg_b);
                bgnondefault = 1;
                break;
            case 'f':
                hextriple2gdcolor(optarg, &fg_r, &fg_g, &fg_b);
                break;
            case 'a':
                antialias = atoi(optarg);
                break;
            case 'F':
                font = strdup(optarg);
                break;
            case 'G':
                sprintf(ca, "%s:%s", optarg, gdfpath);
                gdfpath = strdup(ca);
                break;
            case 'c':
                fCrop = 1;
                break;
            case 'i':
                fInterlace = 1;
                break;
            case 'r':
                if (parsesizespec(optarg, &ss_xrel, &ss_xsize, &ss_xalign, &ss_yrel, &ss_ysize, &ss_yalign)) {
                    fprintf(stderr, "** gFONT::Error: bad size spec: `%s'\n", optarg);
                    exit(1);
                }
                fResize = 1;
                break;
            case 'o':
                outputfile = strdup(optarg);
                break;
            case 'v':
                fVerbose = 1;
                break;
            case 'V':
                give_version();
                exit(0);
                break;
            case 'h':
                give_usage(argv[0]);
                exit(0);
                break;
            default:
				fprintf(stderr, "gFONT: invalid option `%c'\n", optopt);
		        fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
                exit(1);
        }
    }

    /* determine source filename and runtime mode */
    if (optind != argc-1) {
		fprintf(stderr, "gFONT: wrong number of arguments\n");
		fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]);
        exit(1);
    }
    string = argv[optind];

    Verbose("Loading font: ");
    if ((cp = getenv("GDFPATH")) != NULL) {
        sprintf(ca, "%s:%s", gdfpath, cp);
        gdfpath = strdup(ca);
    }
    gdFontPathSet(gdfpath);
    if ((gdfp = gdFontLoad(font)) == NULL) {
        sprintf(cmd, "%s/exec/gfont_mkgdf %s", libdir, font);
        fprintf(stderr, "Running `%s'\n", cmd);
        system(cmd);
        if ((gdfp = gdFontLoad(font)) == NULL) {
            fprintf(stderr, "Still cannot load font '%s'\n", font);
            exit(1);
        }
    }
    Verbose("%s (%dx%d)\n", gdfp->name, gdfp->w, gdfp->h);

    Verbose("Calculating image size: ");
    w = gdImageStringWidth(gdfp, string) + 2;
    h = gdfp->h + 2;
    Verbose("%d x %d\n", w, h);

    Verbose("Allocating image: ");
    ip = gdImageCreate(w, h);
    cbg = gdImageColorAllocate(ip, bg_r, bg_g, bg_b);  
    cfg = gdImageColorAllocate(ip, fg_r, fg_g, fg_b);         
    if (fInterlace) 
        gdImageInterlace(ip, 1);
    if (!bgnondefault)
       gdImageColorTransparent(ip, cbg);
    Verbose("Done\n");

    Verbose("Drawing image: ");
    gdImageString(ip, gdfp, 1, 1, string, cfg);
    Verbose("Done\n");

    if (antialias > 0) {
        Verbose("Anti-Aliasing image: ");
        r = (bg_r > fg_r ? fg_r : bg_r);
        g = (bg_g > fg_g ? fg_g : bg_g);
        b = (bg_b > fg_b ? fg_b : bg_b);
        d_r = abs(bg_r - fg_r) / (antialias+1);
        d_g = abs(bg_g - fg_g) / (antialias+1);
        d_b = abs(bg_b - fg_b) / (antialias+1);
        for (i = 0; i < antialias; i++) {
            r += d_r;
            g += d_g;
            b += d_b;
            Verbose("r=%d,g=%d,b=%d\n", r+20, g+20, b+20);
            gdImageColorAllocate(ip, r, g, b);         
        }
        gdImageAntiAlias(ip, 1, 1, w-1, h-1, antialias, cbg);
        gdImageAntiAlias(ip, 1, 1, w-1, h-1, antialias, cbg);
        Verbose("Done\n");
    }

    if (fCrop) {
        Verbose("Cropping image to: ");
        nX = 0;
        nW = w;
        cont = 1;
        for (y = 0; y < gdImageSY(ip) && cont; y++) 
            for (x = 0; x < gdImageSX(ip) && cont; x++) 
                if (gdImageGetPixel(ip, x, y) != cbg) 
                    cont = 0;
        y = (y > 0 ? y-1 : y);
        nY = y;
        nH = gdImageSY(ip) - y;
        cont = 1;
        for (y = gdImageSY(ip) - 1; y >= nY && cont; y--) 
            for (x = 0; x < gdImageSX(ip) && cont; x++) 
                if (gdImageGetPixel(ip, x, y) != cbg) 
                    cont = 0;
        y = (y < gdImageSY(ip) ? y+1 : y);
        nH = nH - ((gdImageSY(ip) - 1) - y);
        Verbose("%d x %d, origin: (%d,%d)\n", nW, nH, nX, nY);

        Verbose("Allocating new image: ");
        ipn = gdImageCreate(nW, nH);
        cbg = gdImageColorAllocate(ipn, bg_r, bg_g, bg_b);  
        cfg = gdImageColorAllocate(ipn, fg_r, fg_g, fg_b);         
        if (fInterlace) 
            gdImageInterlace(ipn, 1);
        if (!bgnondefault)
            gdImageColorTransparent(ipn, cbg);
        Verbose("Done\n");

        Verbose("Copying image: ");
        gdImageCopy(ipn, ip, 0, 0, nX, nY, nW, nH);
        Verbose("Done\n");

        Verbose("Freeing old image: ");
        gdImageDestroy(ip);
        ip = ipn;
        Verbose("Done\n");
    }

    if (fResize) {
        Verbose("Adjusting image size: ");
        Verbose("x=%s%d y=%s%d, xalign=%d, yalign=%d\n", 
                (ss_xrel ? "+" : ""), ss_xsize, 
                (ss_yrel ? "+" : ""), ss_ysize,
                ss_xalign,
                ss_yalign
        );

        if (ss_xrel) 
            nW = gdImageSX(ip)+ss_xsize;
        else
            nW = ss_xsize;
        if (ss_yrel) 
            nH = gdImageSY(ip)+ss_ysize;
        else
            nH = ss_ysize;
        if (ss_xalign == ALIGN_L)
            nX = 0;
        else if (ss_xalign == ALIGN_C)
            nX = ((int)((nW/2))-1) - (int)(gdImageSX(ip)/2);
        else if (ss_xalign == ALIGN_R)
            nX = nW-gdImageSX(ip);
        if (ss_yalign == ALIGN_T)
            nY = 0;
        else if (ss_yalign == ALIGN_M)
            nY = ((int)((nH/2))-1) - (int)(gdImageSY(ip)/2);
        else if (ss_yalign == ALIGN_B)
            nY = nH-gdImageSY(ip);
        Verbose("Adjusting parameters: %d x %d, origin: (%d,%d)\n", nW, nH, nX, nY);

        if (nW < gdImageSX(ip) || nH < gdImageSY(ip)) {
            fprintf(stderr, "** gFONT:Error: new image size too small\n");
            exit(1);
        }

        Verbose("Allocating new image: ");
        ipn = gdImageCreate(nW, nH);
        cbg = gdImageColorAllocate(ipn, bg_r, bg_g, bg_b);  
        cfg = gdImageColorAllocate(ipn, fg_r, fg_g, fg_b);         
        if (fInterlace) 
            gdImageInterlace(ipn, 1);
        if (!bgnondefault)
            gdImageColorTransparent(ipn, cbg);
        Verbose("Done\n");

        Verbose("Copying image: ");
        gdImageCopy(ipn, ip, nX, nY, 0, 0, nW, nH);
        Verbose("Done\n");

        Verbose("Freeing old image: ");
        gdImageDestroy(ip);
        ip = ipn;
        Verbose("Done\n");
    }

    Verbose("Exporting image to GIF file: ");
    fp = fopen(outputfile, "w");
    gdImageGif(ip, fp);
    fclose(fp);
    Verbose("Done\n");

    Verbose("Freeing image: ");
    gdImageDestroy(ip);
    Verbose("Done\n");

    return 0;
}

/*EOF*/