about summary refs log tree commit diff
path: root/generator/pbmtextps.c
blob: eab6780ac273f133b57e014a301d1aee898991f9 (plain) (blame)
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
/*
 * pbmtextps.c -  render text into a bitmap using a postscript interpreter
 *
 * Copyright (C) 2002 by James McCann.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose and without fee is hereby granted, provided
 * that the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  This software is provided "as is" without express or
 * implied warranty.
 *
 * PostScript is a registered trademark of Adobe Systems International.
 *
 * Additions by Bryan Henderson contributed to public domain by author.
 *
 */
#define _XOPEN_SOURCE   /* Make sure popen() is in stdio.h */
#define _BSD_SOURCE     /* Make sure stdrup() is in string.h */
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "pm_c_util.h"
#include "mallocvar.h"
#include "nstring.h"
#include "shhopt.h"
#include "pbm.h"


#define BUFFER_SIZE 2048

struct cmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    unsigned int res;         /* resolution, DPI */
    unsigned int fontsize;    /* Size of font in points */
    const char * font;      /* Name of postscript font */
    float        stroke;
        /* Width of stroke in points (only for outline font) */
    unsigned int verbose;
    const char * text;
};



static void
writeFileToStdout(const char * const fileName){
    /* simple pbmtopbm */

    FILE * ifP;
    int format;
    int cols, rows, row ;
    unsigned char * bitrow; 
    
    ifP = pm_openr(fileName);
    pbm_readpbminit(ifP, &cols, &rows, &format);

    if (cols==0 || rows==0 || cols>INT_MAX-10 || rows>INT_MAX-10)
      pm_error("Abnormal output from gs program.  "
               "width x height = %u x %u", cols, rows);
               
    pbm_writepbminit(stdout, cols, rows, 0);           
               
    bitrow = pbm_allocrow_packed(cols);
    
    for (row = 0; row < rows; ++row) {
        pbm_readpbmrow_packed(ifP, bitrow, cols, format);
        pbm_writepbmrow_packed(stdout, bitrow, cols, 0);
    }
    pbm_freerow_packed(bitrow);
}



static void
validateFontName(const char * const name) {
/*-----------------------------------------------------------------------------
  Validate font name string.

  Abort with error message if it contains anything other than the printable
  characters in the ASCII 7-bit range, or any character with a special meaning
  in PostScript.
-----------------------------------------------------------------------------*/
    unsigned int idx;

    for (idx = 0; name[idx] != '\0'; ++idx) {
        char const c = name[idx]; 

        if (c < 32 || c > 125)
            pm_error("Invalid character in font name");
        else 
            switch (c) {
              case '[':   case ']':   case '(':   case ')':
              case '{':   case '}':   case '/':   case '\\':
              case '<':   case '>':   case '%':   case ' ':
              case '@':
                pm_error("Invalid character in font name");
            }
    }
}



static void
asciiHexEncode(char *          const inbuff,
               char *          const outbuff) {
/*-----------------------------------------------------------------------------
  Convert the input text string to ASCII-Hex encoding.

  Examples: "ABC abc 123" -> <4142432061626320313233>
            "FOO(BAR)FOO" -> <464f4f2842415229464f4f>
-----------------------------------------------------------------------------*/
    char const hexits[16] = "0123456789abcdef";

    unsigned int idx;

    for (idx = 0; inbuff[idx] != '\0'; ++idx) {
        unsigned int const item = (unsigned char) inbuff[idx]; 

        outbuff[idx*2]   = hexits[item >> 4];
        outbuff[idx*2+1] = hexits[item & 0xF];
    }

    outbuff[idx * 2] = '\0';
}



static void
buildTextFromArgs(int           const argc,
                  char **       const argv,
                  const char ** const asciiHexTextP) {
/*----------------------------------------------------------------------------
   Build the array of text to be included in the Postscript program to
   be rendered, from the arguments of this program.

   We encode it in ASCII-Hex notation as opposed to using the plain text from
   the command line because 1) the command line might have Postscript control
   characters in it; and 2) the command line might have text in 8-bit or
   multibyte code, but a Postscript program is supposed to be entirely
   printable ASCII characters.
-----------------------------------------------------------------------------*/
    char * text;
    unsigned int totalTextSize;
    unsigned int i;

    text = strdup("");
    totalTextSize = 1;

    if (argc-1 < 1)
        pm_error("No text");

    for (i = 1; i < argc; ++i) {
        if (i > 1) {
            totalTextSize += 1;
            text = realloc(text, totalTextSize);
            if (text == NULL)
                pm_error("out of memory");
            strcat(text, " ");
        } 
        totalTextSize += strlen(argv[i]);
        text = realloc(text, totalTextSize);
        if (text == NULL)
            pm_error("out of memory");
        strcat(text, argv[i]);
    }

    { 
        char * asciiHexText;

        MALLOCARRAY(asciiHexText, totalTextSize * 2);

        if (!asciiHexText)
            pm_error("Unable to allocate memory for hex encoding of %u "
                     "characters of text", totalTextSize);

        asciiHexEncode(text, asciiHexText);
        *asciiHexTextP = asciiHexText;
    }
    strfree(text);
}



static void
parseCommandLine(int argc, char ** argv,
                 struct cmdlineInfo *cmdlineP) {
/*---------------------------------------------------------------------------
  Note that the file spec array we return is stored in the storage that
  was passed to us as the argv array.
---------------------------------------------------------------------------*/
    optEntry * option_def;
    /* Instructions to OptParseOptions2 on how to parse our options.
   */
    optStruct3 opt;

    unsigned int option_def_index;

    MALLOCARRAY(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "resolution", OPT_UINT,   &cmdlineP->res,            NULL,  0);
    OPTENT3(0, "font",       OPT_STRING, &cmdlineP->font,           NULL,  0);
    OPTENT3(0, "fontsize",   OPT_UINT,   &cmdlineP->fontsize,       NULL,  0);
    OPTENT3(0, "stroke",     OPT_FLOAT,  &cmdlineP->stroke,         NULL,  0);
    OPTENT3(0, "verbose",    OPT_FLAG,   NULL, &cmdlineP->verbose,         0);

    /* Set the defaults */
    cmdlineP->res = 150;
    cmdlineP->fontsize = 24;
    cmdlineP->font = "Times-Roman";
    cmdlineP->stroke = -1;

    opt.opt_table = option_def;
    opt.short_allowed = FALSE;  /* We have no short (old-fashioned) options */
    opt.allowNegNum = FALSE;

    optParseOptions3(&argc, argv, opt, sizeof(opt), 0);

    validateFontName(cmdlineP->font);

    buildTextFromArgs(argc, argv, &cmdlineP->text);
}



static void
termCmdline(struct cmdlineInfo const cmdline) {

    strfree(cmdline.text);
}



static const char *
construct_postscript(struct cmdlineInfo const cmdline) {

    const char * retval;
    const char * template;

    if (cmdline.stroke < 0) 
        template =
            "/%s findfont\n"
            "%d scalefont\n"
            "setfont\n"
            "12 36 moveto\n"
            "<%s> show\n"
            "showpage\n";
    else 
        template =
            "/%s findfont\n"
            "%d scalefont\n"
            "setfont\n"
            "12 36 moveto\n"
            "%f setlinewidth\n"
            "0 setgray\n"
            "<%s> true charpath\n"
            "stroke\n"
            "showpage\n";

    if (cmdline.stroke < 0)
        asprintfN(&retval, template, cmdline.font, cmdline.fontsize, 
                  cmdline.text);
    else
        asprintfN(&retval, template, cmdline.font, cmdline.fontsize, 
                  cmdline.stroke, cmdline.text);

    return retval;
}



static const char *
gsExecutableName() {

    const char * const which = "which gs";

    static char buffer[BUFFER_SIZE];

    FILE * f;

    memset(buffer, 0, BUFFER_SIZE);

    f = popen(which, "r");
    if (!f)
        pm_error("Can't find ghostscript");

    fread(buffer, 1, BUFFER_SIZE, f);
    if (buffer[strlen(buffer) - 1] == '\n')
        buffer[strlen(buffer) - 1] = '\0';
    pclose(f);
    
    if (buffer[0] != '/' && buffer[0] != '.')
        pm_error("Can't find ghostscript");

    return buffer;
}



static const char *
cropExecutableName(void) {

    const char * const which = "which pnmcrop";

    static char buffer[BUFFER_SIZE];
    const char * retval;

    FILE * f;

    memset(buffer, 0, BUFFER_SIZE);

    f = popen(which, "r");
    if (!f)
        retval = NULL;
    else {
        fread(buffer, 1, BUFFER_SIZE, f);
        if (buffer[strlen(buffer) - 1] == '\n')
            buffer[strlen(buffer) - 1] = 0;
        pclose(f);
            
        if (buffer[0] != '/' && buffer[0] != '.') {
            retval = NULL;
            pm_message("Can't find pnmcrop");
        } else
            retval = buffer;
    }
    return retval;
}



static const char *
gsCommand(const char *       const psFname,
          const char *       const outputFilename, 
          struct cmdlineInfo const cmdline) {

    const char * retval;
    double const x = (double) cmdline.res * 11;
    double const y = (double) cmdline.res * 
                     ((double) cmdline.fontsize * 2 + 72)  / 72;
    
    if (cmdline.res <= 0)
         pm_error("Resolution (dpi) must be positive.");
    
    if (cmdline.fontsize <= 0)
         pm_error("Font size must be positive.");
    
    /* The following checks are for guarding against overflows in this 
       function.  Huge x,y values that pass these checks may be
       rejected by the 'gs' program.
    */
    
    if (x > (double) INT_MAX-10)
         pm_error("Absurdly fine resolution: %u. Output width too large.",
                   cmdline.res );
    if (y > (double) INT_MAX-10)
         pm_error("Absurdly fine resolution (%u) and/or huge font size (%u). "
                  "Output height too large.", cmdline.res, cmdline.fontsize);
         
    asprintfN(&retval, "%s -g%dx%d -r%d -sDEVICE=pbm "
              "-sOutputFile=%s -q -dBATCH -dNOPAUSE %s </dev/null >/dev/null", 
              gsExecutableName(), (int) x, (int) y, cmdline.res, 
              outputFilename, psFname);

    return retval;
}



static const char *
cropCommand(const char * const inputFileName) {

    const char * retval;
    const char * plainOpt = pm_plain_output ? "-plain" : "" ;
    
    if (cropExecutableName()) {
        asprintfN(&retval, "%s -top -right %s %s", 
                  cropExecutableName(), plainOpt, inputFileName);
        if (retval == strsol)
            pm_error("Unable to allocate memory");
    } else
        retval = NULL;

    return retval;
}



static void
writeProgram(const char *       const psFname,
             struct cmdlineInfo const cmdline) {

    const char * ps;
    FILE * psfile;

    psfile = fopen(psFname, "w");
    if (psfile == NULL)
        pm_error("Can't open temp file '%s'.  Errno=%d (%s)",
                 psFname, errno, strerror(errno));

    ps = construct_postscript(cmdline);

    if (cmdline.verbose)
        pm_message("Postscript program = '%s'", ps);
        
    if (fwrite(ps, 1, strlen(ps), psfile) != strlen(ps))
        pm_error("Can't write postscript to temp file");

    fclose(psfile);

    strfree(ps);
}



static void
executeProgram(const char *       const psFname, 
               const char *       const outputFname,
               struct cmdlineInfo const cmdline) {

    const char * com;
    int rc;

    com = gsCommand(psFname, outputFname, cmdline);
    if (com == NULL)
        pm_error("Can't allocate memory for a 'ghostscript' command");
    
    if (cmdline.verbose)
        pm_message("Running Postscript interpreter '%s'", com);

    rc = system(com);
    if (rc != 0)
        pm_error("Failed to run Ghostscript process.  rc=%d", rc);

    strfree(com);
}



static void
cropToStdout(const char * const inputFileName,
             bool         const verbose) {

    const char * com;

    com = cropCommand(inputFileName);

    if (com == NULL) {
        /* No pnmcrop.  So don't crop. */
        pm_message("Can't find pnmcrop command, image will be large");
        writeFileToStdout(inputFileName);
    } else {
        FILE * pnmcrop;

        if (verbose)
            pm_message("Running crop command '%s'", com);
        
        pnmcrop = popen(com, "r");
        if (pnmcrop == NULL)
            pm_error("Can't run pnmcrop process");
        else {
            char buf[2048];
            bool eof;

            eof = FALSE;
            
            while (!eof) {
                int bytesRead;

                bytesRead = fread(buf, 1, sizeof(buf), pnmcrop);
                if (bytesRead > 0) {
                    int rc;
                    rc = fwrite(buf, 1, bytesRead, stdout);
                    if (rc != bytesRead)
                        pm_error("Can't write to stdout");
                } else if (bytesRead == 0)
                    eof = TRUE;
                else
                    pm_error("Failed to read output of Pnmcrop process.  "
                             "Errno=%d (%s)", errno, strerror(errno));
            }
            fclose(pnmcrop);
        }
        strfree(com);
    }
}



static void
createOutputFile(struct cmdlineInfo const cmdline) {

    const char * const template = "./pstextpbm.%d.tmp.%s";
    
    const char * psFname;
    const char * uncroppedPbmFname;

    asprintfN(&psFname, template, getpid(), "ps");
    if (psFname == NULL)
        pm_error("Unable to allocate memory");
 
    writeProgram(psFname, cmdline);

    asprintfN(&uncroppedPbmFname, template, getpid(), "pbm");
    if (uncroppedPbmFname == NULL)
        pm_error("Unable to allocate memory");
 
    executeProgram(psFname, uncroppedPbmFname, cmdline);

    unlink(psFname);
    strfree(psFname);

    cropToStdout(uncroppedPbmFname, cmdline.verbose);

    unlink(uncroppedPbmFname);
    strfree(uncroppedPbmFname);
}



int 
main(int argc, char *argv[]) {

    struct cmdlineInfo cmdline;

    pbm_init(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    createOutputFile(cmdline);

    termCmdline(cmdline);

    return 0;
}