about summary refs log tree commit diff
path: root/generator/pbmtext.c
blob: a4566d12b977fa7eac06e2776df093ec3db05090 (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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
/* pbmtext.c - render text into a bitmap
**
** Copyright (C) 1991 by Jef Poskanzer.
**
** 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.
*/

#define _DEFAULT_SOURCE 1  /* New name for SVID & BSD source defines */
#define _BSD_SOURCE 1      /* Make sure strdup() is in string.h */
#define _XOPEN_SOURCE 500  /* Make sure strdup() is in string.h */

#include <string.h>
#include <math.h>
#include <limits.h>
#include <assert.h>
#include <setjmp.h>
#include <locale.h>
#include <wchar.h>

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


/* Max length of input text.  Valid for text which is part of the
   command line and also for text fed from standard input.
   Note that newline is counted as a character.
*/
#define  MAXLINECHARS 4999

/* We add one slot for the terminating NULL charter
   and another slot as a margin to detect overruns.
*/
#define  LINEBUFSIZE  (MAXLINECHARS + 2)

struct CmdlineInfo {
    /* All the information the user supplied in the command line,
       in a form easy for the program to use.
    */
    const PM_WCHAR * text; /* text from command line or NULL if none */
    const char * font;    /* -font option value or NULL if none */
    const char * builtin; /* -builtin option value or NULL if none */
    float space;          /* -space option value or default */
    int lspace;           /* -lspace option value or default */
    unsigned int width;   /* -width option value or zero */
    unsigned int wchar;   /* -wchar option specified  */
    unsigned int nomargins;  /* -nomargins option specified  */
    unsigned int dryrun;     /* -dry-run option specified */
    unsigned int textdump;   /* -text-dump option specified */
    unsigned int entirefont; /* -load-entire-font option specified */
    unsigned int verbose;    /* -verbose option specified */
        /* undocumented option */
    unsigned int dumpsheet; /* font data sheet in PBM format for -font */
};



static const PM_WCHAR *
textFmCmdLine(int argc, const char ** argv) {

    char * text;
    PM_WCHAR * wtext;
    unsigned int i;
    unsigned int totaltextsize;

    MALLOCARRAY(text, LINEBUFSIZE);

    if (!text)
        pm_error("Unable to allocate memory for a buffer of up to %u "
                 "characters of text", MAXLINECHARS);

    text[0] = '\0';

    for (i = 1, totaltextsize = 0; i < argc; ++i) {
        if (i > 1) {
            strcat(text, " ");
        }
        totaltextsize += strlen(argv[i]) + (i > 1 ? 1 : 0);
        if (totaltextsize > MAXLINECHARS)
           pm_error("Input text is %u characters.  "
                    "Cannot process longer than %u",
                    totaltextsize, (unsigned int) MAXLINECHARS);
        strcat(text, argv[i]);
    }
    MALLOCARRAY(wtext, totaltextsize * sizeof(PM_WCHAR));

    if (!wtext)
        pm_error("Unable to allocate memory for a buffer of up to %u "
                 "wide characters of text", totaltextsize);

    for (i = 0; i < totaltextsize + 1; ++i)
        wtext[i] = (PM_WCHAR) text[i];

    free(text);

    return wtext;
}



static void
parseCommandLine(int argc, const char ** argv,
                 struct CmdlineInfo * const 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 OptParseOptions3 on how to parse our options.
         */
    optStruct3 opt;

    unsigned int option_def_index;

    MALLOCARRAY_NOFAIL(option_def, 100);

    option_def_index = 0;   /* incremented by OPTENTRY */
    OPTENT3(0, "font",       OPT_STRING, &cmdlineP->font,    NULL,   0);
    OPTENT3(0, "builtin",    OPT_STRING, &cmdlineP->builtin, NULL,   0);
    OPTENT3(0, "space",      OPT_FLOAT,  &cmdlineP->space,   NULL,   0);
    OPTENT3(0, "lspace",     OPT_INT,    &cmdlineP->lspace,  NULL,   0);
    OPTENT3(0, "width",      OPT_UINT,   &cmdlineP->width,   NULL,   0);
    OPTENT3(0, "nomargins",  OPT_FLAG,   NULL, &cmdlineP->nomargins, 0);
    OPTENT3(0, "wchar",      OPT_FLAG,   NULL, &cmdlineP->wchar,     0);
    OPTENT3(0, "verbose",    OPT_FLAG,   NULL, &cmdlineP->verbose,   0);
    OPTENT3(0, "dry-run",    OPT_FLAG,   NULL, &cmdlineP->dryrun,    0);
    OPTENT3(0, "text-dump",  OPT_FLAG,   NULL, &cmdlineP->textdump,  0);
    OPTENT3(0, "dump-sheet", OPT_FLAG,   NULL, &cmdlineP->dumpsheet, 0);
    OPTENT3(0, "load-entire-font", OPT_FLAG,   NULL, &cmdlineP->entirefont, 0);

    /* Set the defaults */
    cmdlineP->font    = NULL;
    cmdlineP->builtin = NULL;
    cmdlineP->space   = 0.0;
    cmdlineP->width   = 0;
    cmdlineP->lspace  = 0;

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

    pm_optParseOptions3(&argc, (char **)argv, opt, sizeof(opt), 0);
    /* Uses and sets argc, argv, and some of *cmdlineP and others. */

    if (cmdlineP->width > 0 && cmdlineP->nomargins) {
        pm_message("-nomargins has no effect when -width is specified");
        cmdlineP->nomargins = FALSE;
    } else if (cmdlineP->width > INT_MAX-10)
        pm_error("-width value too large");

    if (cmdlineP->space > pbm_maxfontwidth())
        pm_error("-space value too large");
    else if (cmdlineP->space < -pbm_maxfontwidth())
        pm_error("negative -space value too large");

    if (cmdlineP->lspace > pbm_maxfontheight())
        pm_error("-lspace value too large");
    else if (cmdlineP->lspace < -pbm_maxfontheight())
        pm_error("negative -lspace value too large");

    if (cmdlineP->font != NULL && cmdlineP->builtin != NULL)
        pm_error("You cannot specify both -font and -builtin");
    else if (cmdlineP->font == NULL && cmdlineP->entirefont)
        pm_error("You cannot specify -load-entire-font without -font");

    if (cmdlineP->textdump) {
        if (cmdlineP->dryrun)
            pm_error("You cannot specify both -dry-run and -text-dump");
        else if (cmdlineP->dumpsheet)
            pm_error("You cannot specify both -dump-sheet and -text-dump");
    }

    if (cmdlineP->dryrun && cmdlineP->dumpsheet)
        pm_error("You cannot specify both -dry-run and -dump-sheet");

    if (argc-1 == 0)
        cmdlineP->text = NULL;
    else {  /* Text to render is part of command line */
        if (cmdlineP->wchar)
            pm_error("-wchar is not valid when text is from command line");

        cmdlineP->text = textFmCmdLine(argc, argv);
    }

    free(option_def);
}



static void
reportFont(const struct font2 * const fontP) {

    pm_message("FONT:");
    pm_message("  Name: %s", fontP->name);
    pm_message("  Encoding: %s", fontP->charset_string);
    pm_message("  Origin: %s", pbmFontOrigin[fontP->load_fn]);
    pm_message("  Character dimensions: %uw x %uh",
               fontP->maxwidth, fontP->maxheight);
    pm_message("  Additional vert white space: %d pixels", fontP->y);
    pm_message("  # characters loaded: %u", fontP->chars);
}



static struct font2 *
font2FromFile(const char *               const fileName,
              PM_WCHAR                   const maxmaxglyph,
              const struct pm_selector * const selectorP) {

    struct font2 * font2P;

    jmp_buf jmpbuf;
    int rc;

    rc = setjmp(jmpbuf);

    if (rc == 0) {
        /* This is the normal program flow */
        pm_setjmpbuf(&jmpbuf);

        font2P = pbm_loadfont2select(fileName, maxmaxglyph, selectorP);

        pm_setjmpbuf(NULL);
    } else {
        /* This is the second pass, after pbm_loadbdffont2 does a longjmp
           because it fails.
        */
        pm_setjmpbuf(NULL);

        pm_error("Failed to load font from file '%s'", fileName);
    }

    return font2P;
}



static bool
codepointIsValid(struct font2 * const fontP,
                 PM_WCHAR       const codepoint) {
/*----------------------------------------------------------------------------
  'codepoint' is a valid entry in the font indicated by 'fontP'.
-----------------------------------------------------------------------------*/
    bool retval;

    assert(pm_selector_is_marked(fontP->selectorP, codepoint));

    if (codepoint > fontP->maxglyph || fontP->glyph[codepoint] == NULL)
        retval = false;
    else retval = true;

    return (retval);

}



static const char *
charDescription(PM_WCHAR const codepoint) {
/*----------------------------------------------------------------------------
   Descriptive string for codepoint 'codepoint'.

   Certain codepoints appear frequently in text files and cause problems when
   missing in the font set, so we give those descriptions.  For other
   codepoint, we just return a null string.
-----------------------------------------------------------------------------*/

  const char * name;

  switch (codepoint) {
  case '\r' : name="carriage return";  break;
  case '\n' : name="line feed";        break; /* for future use */
  case '\t' : name="tab";              break; /* for future use */
  case ' '  : name="space";            break;
  case 0xFEFF: name="byte order mark"; break;
  default : name=""; break;
  }

  return name;
}



enum FixMode {SILENT, /* convert silently */
              WARN,   /* output message to stderr */
              QUIT    /* abort */ };



static void
reportAbsentGlyphs(bool                       const wchar,
                   struct font2 *             const fontP,
                   const struct pm_selector * const textSelectorP,
                   unsigned int *             const missingCharCtP) {
/*----------------------------------------------------------------------------
   Compare the glyph entries in *fontP with the requests in *textSelectorP.

   Note that we may need the space character as a substitute for missing
   glyphs while the input text has no spaces.  In rare cases the font may not
   have a space character.

   Currently, this program reads the font file only once.  A future version
   may opt to read it a second time to load the substitute glyph.
-----------------------------------------------------------------------------*/
    PM_WCHAR     codepoint;
    unsigned int missingCharCt;

    for (codepoint = textSelectorP->min, missingCharCt = 0;
         codepoint <= textSelectorP->max; ++codepoint) {

        if (pm_selector_is_marked(textSelectorP, codepoint) &&
            !codepointIsValid(fontP, codepoint)) {
            ++missingCharCt;
            if (missingCharCt == 1)  { /* initial */
                pm_message("failed to load glyph data for these code points "
                           "in input:");
            }

            pm_message(wchar ? "+%05X %s" : "%02X %s",
                       (unsigned int) codepoint,
                       charDescription(codepoint));
        }
    }

    *missingCharCtP = missingCharCt;
}



static void
validateFont(bool                       const wchar,
             struct font2 *             const fontP,
             const struct pm_selector * const textSelectorP,
             enum   FixMode             const fixmode,
             bool                       const verbose,
             bool *                     const hasAllCharsP) {
/*----------------------------------------------------------------------------
   If any glyphs required by the text indicated by *textSelectorP are missing
   from font *fontP, issue a warning message or abort the program according to
   'fixmode'.

   Abort the program if one or more characters are missing and the space
   character is one of them.

   Return (if we return) as *hasAllCharsP whether the font has all the glyphs.
-----------------------------------------------------------------------------*/
    unsigned int missingCharCt;

    assert (textSelectorP != NULL);
    assert(pm_selector_marked_ct(textSelectorP) >= 0);

    reportAbsentGlyphs(wchar, fontP, textSelectorP, &missingCharCt);

    if (missingCharCt > 0) {
        if (verbose)
            pm_message("%u characters absent in font", missingCharCt);

        if (fixmode == QUIT)
            pm_error("aborting");
        else if (!codepointIsValid(fontP, L' '))
            pm_error ("replacement character (space) absent; aborting");
        else
            pm_message("undefined code points will be converted to space");
    }

    *hasAllCharsP = (missingCharCt == 0);
}



static void
computeFont(struct CmdlineInfo         const cmdline,
            struct font2 **            const fontPP,
            const struct pm_selector * const textSelectorP,
            enum   FixMode             const fixmode,
            bool *                     const fontHasAllCharsP) {

    struct font2 *       font2P;
    struct pm_selector * fontSelectorP;

    if (cmdline.font) {
        if(cmdline.entirefont)
            fontSelectorP = NULL;
        else if(!pm_selector_is_marked(textSelectorP, L' ')) {
            pm_selector_copy(MAX(textSelectorP->max, L' '),
                             textSelectorP, &fontSelectorP);
            pm_selector_mark(fontSelectorP, L' ');
        } else
            fontSelectorP = (struct pm_selector *) textSelectorP;

        font2P = font2FromFile(cmdline.font, cmdline.wchar ?
                               PM_FONT2_MAXGLYPH : PM_FONT_MAXGLYPH,
                               fontSelectorP);
    } else if (cmdline.builtin)
        font2P = pbm_defaultfont2(cmdline.builtin);
    else
        font2P = pbm_defaultfont2(cmdline.wchar ? "bdf" : "bdf");

    if (cmdline.verbose) {
        reportFont(font2P);
        pm_message("%u code points found in text",
                   pm_selector_marked_ct(textSelectorP));
    }

    validateFont(cmdline.wchar, font2P, textSelectorP, fixmode,
                 cmdline.verbose,
                 fontHasAllCharsP);

    *fontPP = font2P;
}



struct Text {
    PM_WCHAR **  textArray;  /* malloc'ed */
    unsigned int allocatedLineCount;
    unsigned int lineCount;
};



static void
allocTextArray(struct Text * const textP,
               unsigned int  const maxLineCount,
               unsigned int  const maxColumnCount) {

    unsigned int line;

    textP->allocatedLineCount = maxColumnCount > 0 ? maxLineCount : 0;
    MALLOCARRAY_NOFAIL(textP->textArray, maxLineCount);

    for (line = 0; line < maxLineCount; ++line) {
        if (maxColumnCount > 0)
            MALLOCARRAY_NOFAIL(textP->textArray[line], maxColumnCount+1);
    else
        textP->textArray[line] = NULL;
    }
    textP->lineCount = 0;
}



static void
freeTextArray(struct Text const text) {

    unsigned int line;

    for (line = 0; line < text.allocatedLineCount; ++line)
        free((PM_WCHAR **)text.textArray[line]);

    free(text.textArray);
}




static void
setupSelector(const PM_WCHAR *     const input,
              const PM_WCHAR **    const outputP,
              struct pm_selector * const selectorP) {
/*----------------------------------------------------------------------------
   Read through input[] and record the codepoints encountered.  Return it as
   newly malloced *outputP.

   Expand tabs to spaces.

   Remove any trailing newline.  (But leave intermediate ones as line
   delimiters).
-----------------------------------------------------------------------------*/
    /* We don't know in advance how big the output will be because of the
       tab expansions.  So we make sure before processing each input
       character that there is space in the output buffer for a worst
       case tab expansion, plus a terminating NUL, reallocating as
       necessary.  And we originally allocate enough for the entire line
       assuming no tabs.
    */

    unsigned int const tabSize = 8;

    unsigned int inCursor, outCursor;
    PM_WCHAR * output;      /* Output buffer.  Malloced */
    size_t outputSize;  /* Currently allocated size of 'output' */

    outputSize = wcslen(input) + 1 + tabSize;
        /* Leave room for one worst case tab expansion and NUL terminator */
    MALLOCARRAY(output, outputSize);

    if (output == NULL)
        pm_error("Couldn't allocate %u bytes for a line of text.",
                 (unsigned)outputSize);

    for (inCursor = 0, outCursor = 0; input[inCursor] != L'\0'; ++inCursor) {
        PM_WCHAR const currentChar = input[inCursor];
        if (outCursor + 1 + tabSize > outputSize) {
            outputSize = outCursor + 1 + 4 * tabSize;
            REALLOCARRAY(output, outputSize);
            if (output == NULL)
                pm_error("Couldn't allocate %u bytes for a line of text.",
                         (unsigned)outputSize);
        }
        if (currentChar == L'\n' && input[inCursor+1] == L'\0') {
            /* This is a terminating newline.  We don't do those. */
        } else if (currentChar == L'\t') {
            /* Expand this tab into the right number of spaces. */
            unsigned int const nextTabStop =
                (outCursor + tabSize) / tabSize * tabSize;

            while (outCursor < nextTabStop)
                output[outCursor++] = L' ';

            pm_selector_mark(selectorP, L' ');

        } else if (currentChar > PM_FONT2_MAXGLYPH)
                pm_message("code point %X is beyond what this program "
                           "can handle.  Max=%X",
                           (unsigned int)currentChar, PM_FONT2_MAXGLYPH);
        else {
            output[outCursor++] = input[inCursor];
            pm_selector_mark(selectorP, currentChar);
        }
        assert(outCursor <= outputSize);
    }
    output[outCursor++] = L'\0';

    assert(outCursor <= outputSize);

    *outputP = output;
}



static void
clearBackground(bit ** const bits,
                int    const cols,
                int    const rows) {

    unsigned int row;

    for (row = 0; row < rows; ++row) {
        unsigned int colChar;
        for (colChar = 0; colChar < pbm_packed_bytes(cols); ++colChar)
            bits[row][colChar] = 0x00;
    }
}



static void
getEdges(double               const currentPosition,
         PM_WCHAR             const currentChar,
         const struct glyph * const glyphP,
         int                  const currLeftEdge,
         double               const currRightEdge,
         int                * const newLeftEdgeP,
         double             * const newRightEdgeP) {

    int leftEdge;
    double rightEdge;

    if (glyphP == NULL)
        pm_error("encountered unrenderable char: %04X",
                  (unsigned int) currentChar);
    else {
        leftEdge  =  (int) MIN(currentPosition + glyphP->x, currLeftEdge);
        rightEdge =  MAX(currentPosition + glyphP->x + glyphP->width,
                         currRightEdge);
    }
    *newLeftEdgeP  = leftEdge;
    *newRightEdgeP = rightEdge;
}



static void
advancePosition(double               const currentPosition,
                PM_WCHAR             const currentChar,
                const struct glyph * const glyphP,
                float                const space,
                double               const accumulatedSpace,
                double             * const newPositionP,
                double             * const newAccumulatedSpaceP) {
/*----------------------------------------------------------------------------
  Advance position according to value for glyph.
  Add extra intercharacter space if -space option was used.

  The advance value must be zero or positive.
----------------------------------------------------------------------------*/

    /* Start position of next character */
    /* Must not move left from current position */
    int const fullPixels = (int) (accumulatedSpace + space);
        /* round toward 0 */
    int const advance    = (int) glyphP->xadd + fullPixels;

    if (advance < 0) {
        if (space < 0)
            pm_error("Negative -space value too large");
        else
            pm_error("Abnormal horizontal advance value %d "
                     "for code point +%05X",
                     glyphP->xadd, (unsigned int) currentChar);
    }
    else if (currentPosition + advance > INT_MAX)
        pm_error("Image is too wide");
    else {
        *newPositionP = currentPosition + advance;
        *newAccumulatedSpaceP = accumulatedSpace + space
            - (double) fullPixels;
    }
}



static void
getLineDimensions(PM_WCHAR             const line[],
                  const struct font2 * const fontP,
                  float                const intercharacterSpace,
                  double *             const rightEdgeP,
                  int    *             const leftEdgeP) {
/*----------------------------------------------------------------------------
   Determine the left edge and right edge in pixels of the line of text
   line[] in the font *fontP, and return them as *leftEdgeP and *rightEdgeP.
   *leftEdgeP will be negative if the leftmost character in the line has a
   "backup" distance.

   Note that the right (left) edge may not belong to the last (first)
   character in the text line.  This happens when the font is slanted
   (xadd is smaller than width) and/or intercharacter space is negative.
   This is illustrated by the following:

     pbmtext -nomargin "ART." | pnmshear -30 -noantialias

   Also note that there may be no black pixels on what is reported as an edge.
   This often happens with fixed-width font in which the white areas on the
   sides are not trimmed.
-----------------------------------------------------------------------------*/
    unsigned int cursor;  /* cursor into the line of text */
    double currentPosition;
        /* sum of xadd values and intercharacter space so far in line.  this
           is never negative.
        */
    double accumulatedIcs;
        /* accumulated intercharacter space so far in the line we are stepping
           through.  Because the intercharacter space might not be an integer,
           we accumulate it here and realize full pixels whenever we have more
           than one pixel.  Note that this can be negative (which means were
           crowding, rather than spreading, text).
        */
    int leftEdge;
    double rightEdge;

    currentPosition = 0;  /* initial value */
    accumulatedIcs  = 0.0;  /* initial value */

    leftEdge  = INT_MAX;  /* initial value */
    rightEdge = INT_MIN;  /* initial value */

    for (cursor = 0; line[cursor] != L'\0'; ++cursor) {
        PM_WCHAR          const currentChar = line[cursor];
        unsigned int      const index       = (unsigned int) currentChar;
        struct glyph *    const glyphP      = fontP->glyph[index];

        getEdges(currentPosition, currentChar, glyphP, leftEdge, rightEdge,
                 &leftEdge, &rightEdge);

        advancePosition(currentPosition, currentChar, glyphP,
                        intercharacterSpace, accumulatedIcs,
                        &currentPosition, &accumulatedIcs);
    }

    if (line[0] == L'\0') {     /* Empty line */
        leftEdge  = 0;
        rightEdge = 0.0;
    }

    *leftEdgeP  = leftEdge;
    *rightEdgeP = rightEdge;
}



static void
getCharsWithinWidth(PM_WCHAR             const line[],
                    const struct font2 * const fontP,
                    float                const intercharacter_space,
                    unsigned int         const targetWidth,
                    unsigned int       * const charCountP,
                    int                * const leftEdgeP) {
/*----------------------------------------------------------------------------
   Determine how many characters of text line[] fit into an image of target
   width targetWidth.

   *leftEdgeP will be negative if the leftmost character in the line has a
   "backup" distance and zero if it does not.
-----------------------------------------------------------------------------*/
    if (line[0] == L'\0') {
        /* Empty line */
        *leftEdgeP = 0;
        *charCountP = 0;
    } else {
        unsigned int cursor;  /* cursor into the line of text */
        double currentPosition;
        double accumulatedIcs;
        int leftEdge;
        double rightEdge;
        unsigned int currentWidth;

        currentPosition = 0;    /* initial value */
        accumulatedIcs  = 0.0;  /* initial value */

        leftEdge     = INT_MAX;  /* initial value */
        rightEdge    = INT_MIN;  /* initial value */

        for (cursor = 0, currentWidth = 0;
             currentWidth <= targetWidth && line[cursor] != L'\0';
             ++cursor) {
            PM_WCHAR const currentChar = line[cursor];
            unsigned int const index = (unsigned int) currentChar;
            struct glyph * const glyphP = fontP->glyph[index];

            getEdges(currentPosition, currentChar, glyphP, leftEdge, rightEdge,
                     &leftEdge, &rightEdge);

            advancePosition(currentPosition, currentChar, glyphP,
                            intercharacter_space, accumulatedIcs,
                            &currentPosition, &accumulatedIcs);

            currentWidth = rightEdge - ((leftEdge > 0 ) ? 0 : leftEdge);
        }

        if (currentWidth > targetWidth) {
            if (cursor == 1)
                pm_error("-width value too small "
                         "to accommodate single character");
            else
                *charCountP = cursor - 1;
        } else
            *charCountP = cursor;

        *leftEdgeP  = leftEdge;
    }
}



static void
insertCharacter(const struct glyph * const glyphP,
                int                  const toprow,
                int                  const leftcol,
                unsigned int         const cols,
                unsigned int         const rows,
                bit **               const bits) {
/*----------------------------------------------------------------------------
   Insert one character (whose glyph is 'glyph') into the image bits[].
   Its top left corner shall be row 'toprow', column 'leftcol'.
-----------------------------------------------------------------------------*/
    if (glyphP->width == 0 && glyphP->height == 0) {
        /* No bitmap data.  Some BDF files code space this way */
    } else {
        unsigned int glyph_y;  /* Y position within the glyph */

        if (leftcol + glyphP->x < 0 ||
            leftcol + glyphP->x + glyphP->width > cols ||
            toprow < 0 ||
            toprow + glyphP->height >rows )
            pm_error("internal error.  Rendering out of bounds");

        for (glyph_y = 0; glyph_y < glyphP->height; ++glyph_y) {
            unsigned int glyph_x;  /* position within the glyph */

            for (glyph_x = 0; glyph_x < glyphP->width; ++glyph_x) {
                if (glyphP->bmap[glyph_y * glyphP->width + glyph_x]) {
                    unsigned int const col = leftcol + glyphP->x + glyph_x;
                    bits[toprow+glyph_y][col/8] |= PBM_BLACK << (7-col%8);
                }
            }
        }
    }
}



static void
insertCharacters(bit **         const bits,
                 struct Text    const lp,
                 struct font2 * const fontP,
                 int            const topmargin,
                 int            const leftmargin,
                 float          const intercharacter_space,
                 unsigned int   const cols,
                 unsigned int   const rows,
                 int            const lspace,
                 bool           const fixedAdvance) {
/*----------------------------------------------------------------------------
   Render the text 'lp' into the image 'bits' using font *fontP and
   putting 'intercharacter_space' pixels between characters and
   'lspace' pixels between the lines.
-----------------------------------------------------------------------------*/
    unsigned int line;  /* Line number in input text */

    for (line = 0; line < lp.lineCount; ++line) {
        unsigned int row;  /* row in image of top of current typeline */
        double leftcol;  /* Column in image of left edge of current glyph */
        unsigned int cursor;  /* cursor into a line of input text */
        double accumulatedIcs;
            /* accumulated intercharacter space so far in the line we
               are building.  Because the intercharacter space might
               not be an integer, we accumulate it here and realize
               full pixels whenever we have more than one pixel.
            */

        row = topmargin + line * (fontP->maxheight + lspace);
        leftcol = leftmargin;
        accumulatedIcs = 0.0;  /* initial value */

        for (cursor = 0; lp.textArray[line][cursor] != '\0'; ++cursor) {
            PM_WCHAR const currentChar = lp.textArray[line][cursor];
            unsigned int const index = (unsigned int) currentChar;
            struct glyph * const glyphP = fontP->glyph[index];
            int const toprow =
                row + fontP->maxheight + fontP->y - glyphP->height - glyphP->y;
                /* row number in image of top row in glyph */

            assert(glyphP != NULL);

            insertCharacter(glyphP, toprow, leftcol, cols, rows, bits);

        if (fixedAdvance)
            leftcol += fontP->maxwidth;
        else
            advancePosition(leftcol, currentChar, glyphP,
                            intercharacter_space, accumulatedIcs,
                            &leftcol, &accumulatedIcs);
        }
    }
}



static void
flowText(struct Text    const inputText,
         int            const targetWidth,
         struct font2 * const fontP,
         float          const intercharacterSpace,
         struct Text  * const outputTextP,
         unsigned int * const maxleftbP) {

    unsigned int outputLineNum;
    unsigned int incursor;   /* cursor into the line we are reading */
    unsigned int const maxLineCount = 50; /* max output lines */
    int leftEdge;
    int leftExtreme = 0;
    unsigned int charCount;

    allocTextArray(outputTextP, maxLineCount, 0);

    for (incursor = 0, outputLineNum = 0;
         inputText.textArray[0][incursor] != L'\0'; ) {

        unsigned int outcursor;

        getCharsWithinWidth(&inputText.textArray[0][incursor], fontP,
                            intercharacterSpace, targetWidth,
                            &charCount, &leftEdge);

        MALLOCARRAY(outputTextP->textArray[outputLineNum], charCount+1);

        if (!outputTextP->textArray[outputLineNum])
            pm_error("Unable to allocate memory for the text of line %u, "
                     "%u characters long", outputLineNum, charCount);

        ++outputTextP->allocatedLineCount;

        for (outcursor = 0; outcursor < charCount; ++outcursor, ++incursor)
            outputTextP->textArray[outputLineNum][outcursor] =
                inputText.textArray[0][incursor];

        outputTextP->textArray[outputLineNum][charCount] = L'\0';
        ++outputLineNum;
        if (outputLineNum >= maxLineCount)
            pm_error("-width too small.  too many output lines");

        leftExtreme = MIN(leftEdge, leftExtreme);
    }
    outputTextP->lineCount = outputLineNum;
    *maxleftbP = (unsigned int) -leftExtreme;
}



static void
truncateText(struct Text    const inputText,
             unsigned int   const targetWidth,
             struct font2 * const fontP,
             float          const intercharacterSpace,
             unsigned int * const maxleftbP) {

    unsigned int lineNum;  /* Line number on which we are currently working */
    int leftEdge;
    int leftExtreme = 0;

    for (lineNum = 0; lineNum < inputText.lineCount; ++lineNum) {
        PM_WCHAR * const currentLine = inputText.textArray[lineNum];

        unsigned int charCount;

        getCharsWithinWidth(currentLine, fontP,
                            intercharacterSpace, targetWidth,
                            &charCount, &leftEdge);

        if (currentLine[charCount] != L'\0') {
            pm_message("truncating line %u from %u to %u characters",
                       lineNum, (unsigned) wcslen(currentLine), charCount);
            currentLine[charCount] = L'\0';
        }

        leftExtreme = MIN(leftEdge, leftExtreme);
    }
    *maxleftbP = (unsigned int) - leftExtreme;
}



static void
fgetWideString(PM_WCHAR *    const widestring,
               unsigned int  const size,
               FILE *        const ifP,
               bool *        const eofP,
               const char ** const errorP) {

    wchar_t * rc;

    assert(widestring);
    assert(size > 1);

    rc = fgetws(widestring, size, ifP);

    if (rc == NULL) {
        if (feof(ifP)) {
            *eofP   = true;
            *errorP = NULL;
        } else if (ferror(ifP) && errno == EILSEQ)
            pm_asprintf(errorP,
                        "fgetws(): conversion error: sequence is "
                        "invalid for locale '%s'",
                        setlocale(LC_CTYPE, NULL));
        else
            pm_asprintf(errorP,
                        "fgetws() of max %u bytes failed",
                        size);
    } else {
        *eofP   = false;
        *errorP = NULL;
    }
}



static void
fgetNarrowString(PM_WCHAR *    const widestring,
                 unsigned int  const size,
                 FILE *        const ifP,
                 bool *        const eofP,
                 const char ** const errorP) {

    char * bufNarrow;
    char * rc;

    assert(widestring);
    assert(size > 0);

    MALLOCARRAY_NOFAIL(bufNarrow, LINEBUFSIZE);

    rc = fgets(bufNarrow, size, ifP);

    if (rc == NULL) {
        if (feof(ifP)) {
            *eofP   = true;
            *errorP = NULL;
        } else
            pm_asprintf(errorP, "Error reading file");
    } else {
        size_t cnt;

        for (cnt = 0; cnt < size && bufNarrow[cnt] != '\0'; ++cnt)
            widestring[cnt] = (PM_WCHAR)(unsigned char) bufNarrow[cnt];

        widestring[cnt] = L'\0';

        *eofP   = false;
        *errorP = NULL;
    }
    free(bufNarrow);
}



static void
fgetNarrowWideString(PM_WCHAR *    const widestring,
                     unsigned int  const size,
                     FILE *        const ifP,
                     bool *        const eofP,
                     const char ** const errorP) {
/*----------------------------------------------------------------------------
  Return the next line from file *ifP, as *widestring, a buffer 'size'
  characters long.

  Lines are delimited by newline characters and EOF.

  'size' is the size in characters of the buffer at *widestring.  If the line
  to which the file is positioned is longer than that minus 1, we consider it
  to be only that long and consider the next character of the actual line to
  be the first character of the next line.  We leave the file positioned
  to that character.

  Return *eofP == true iff we encounter end of file (and therefore don't read
  a line).

  If we can't read the file (or sense EOF), return as *errorP a text
  explanation of why; otherwise, return *errorP = NULL.

  The line we return is null-terminated.  But it also includes any embedded
  null characters that are within the line in the file.  It is not strictly
  possible for Caller to tell whether a null character in *widestring comes
  from the file or is the one we put there, so Caller should just ignore any
  null character and anything after it.  It is also not possible for Caller to
  tell if we trunctaed the actual line because of 'size' if there is a null
  character in the line.  This means there just isn't any way to get
  reasonable behavior from this function if the input file contains null
  characters (but at least the damage is limited to presenting arbitrary text
  as the contents of the file - the program won't crash).

  Null characters never appear within normal text (including wide-character
  text).  If there is one in the input file, it is probably because the input
  is corrupted.

  The line we return may or may not end in a newline character.  It ends in a
  newline character unless it doesn't fit in 'size' characters or it is the
  last line in the file and doesn't end in newline.
-----------------------------------------------------------------------------*/
    /* The limitations described above with respect to null characters in
       *ifP are derived from the same limitations in POSIX 'fgets' and
       'fgetws'.  To avoid them, we would have to read *ifP one character
       at a time with 'fgetc' and 'fgetwc'.
    */

    int const wideCode = fwide(ifP, 0);
        /* Width orientation for *ifP: positive means wide, negative means
           byte, zero means undecided.
        */

    assert(widestring);
    assert(size > 0);

    if (wideCode > 0)
        /* *ifP is wide-oriented */
        fgetWideString(widestring, size, ifP, eofP, errorP);
    else
        fgetNarrowString(widestring, size, ifP, eofP, errorP);
}




static void
getText(PM_WCHAR             const cmdlineText[],
        struct Text *        const inputTextP,
        struct pm_selector * const selectorP) {
/*----------------------------------------------------------------------------
   Get as *inputTextP the text to format, given that the text on the
   command line (one word per command line argument, separated by spaces),
   is 'cmdlineText'.

   If 'cmdlineText' is null, that means to get the text from Standard Input.
   Otherwise, 'cmdlineText' is that text.

   But we return text as only renderable characters - characters in *fontP -
   with control characters interpreted or otherwise fixed, according to
   'fixMode'.

   If *inputTextP indicates Standard Input and Standard Input contains null
   characters, we will truncate lines or consider a single line to be multiple
   lines.
-----------------------------------------------------------------------------*/
    struct Text inputText;

    if (cmdlineText) {
        MALLOCARRAY_NOFAIL(inputText.textArray, 1);
        inputText.allocatedLineCount = 1;
        inputText.lineCount = 1;
        setupSelector(cmdlineText, (const PM_WCHAR**) &inputText.textArray[0],
                      selectorP);
        free((void *) cmdlineText);
    } else {
        /* Read text from stdin. */

        unsigned int const lineBufTerm = LINEBUFSIZE - 1;

        unsigned int maxlines;
            /* Maximum number of lines for which we currently have space in
               the text array
            */
        PM_WCHAR *   buf;
        PM_WCHAR **  textArray;
        unsigned int lineCount;
        bool         eof;

        MALLOCARRAY(buf, LINEBUFSIZE);

        if (!buf)
            pm_error("Unable to allocate memory for up to %u characters of "
                     "text", MAXLINECHARS);
        buf[lineBufTerm] = L'\1';  /* Initialize to non-zero value */
                                   /* to detect input overrun */

        maxlines = 50;  /* initial value */
        MALLOCARRAY(textArray, maxlines);

        if (!textArray)
            pm_error("Unable to allocate memory for a buffer for up to %u "
                     "lines of text", maxlines);

        for (lineCount = 0, eof = false; !eof; ) {
            const char * error;
            fgetNarrowWideString(buf, LINEBUFSIZE, stdin, &eof, &error);
            if (error)
                pm_error("Unable to read line %u from file.  %s",
                         lineCount, error);
            else {
                if (!eof) {
                    if (buf[lineBufTerm] == L'\0') /* overrun */
                        pm_error(
                            "Line %u (starting at zero) of input text "
                            "is longer than %u characters. "
                            "Cannot process",
                            lineCount, (unsigned int) MAXLINECHARS);
                    if (lineCount >= maxlines) {
                        maxlines *= 2;
                        REALLOCARRAY(textArray, maxlines);
                        if (textArray == NULL)
                            pm_error("out of memory");
                    }
                    setupSelector(buf,
                                  (const PM_WCHAR **) &textArray[lineCount],
                                  selectorP);
                    if (textArray[lineCount] == NULL)
                        pm_error("out of memory");
                    ++lineCount;
                }
            }
        }
        inputText.textArray = textArray;
        inputText.lineCount = lineCount;
        inputText.allocatedLineCount = lineCount;
        free(buf);
    }
    *inputTextP = inputText;
}



static void
computeMargins(struct CmdlineInfo const cmdline,
               struct Text        const inputText,
               struct font2 *     const fontP,
               unsigned int *     const vmarginP,
               unsigned int *     const hmarginP) {

    if (cmdline.nomargins) {
        *vmarginP = 0;
        *hmarginP = 0;
    } else {
        if (inputText.lineCount == 1) {
            *vmarginP = fontP->maxheight / 2;
            *hmarginP = fontP->maxwidth;
        } else {
            *vmarginP = fontP->maxheight;
            *hmarginP = 2 * fontP->maxwidth;
        }
    }
}



static void
refineText(struct Text        const inputText,
           struct font2 *     const fontP) {
/*----------------------------------------------------------------------------
   Replace missing characters with space

   A future version of this program may provide various alternatives
   here including simply deleting the offending character, based on a
   command-line option
-----------------------------------------------------------------------------*/
    PM_WCHAR ** const textArray = inputText.textArray;

    unsigned int lineNum;

    for (lineNum = 0; lineNum < inputText.lineCount; ++lineNum) {
        PM_WCHAR * const line = textArray[lineNum];

        unsigned int cursor;

        for (cursor = 0; line[cursor] != L'\0'; ++cursor)
            if ( !codepointIsValid(fontP, line[cursor]) )
                line[cursor] = L' ';
    }
}



static void
formatText(struct CmdlineInfo const cmdline,
           struct Text        const inputText,
           struct font2 *     const fontP,
           unsigned int       const hmargin,
           struct Text *      const formattedTextP,
           unsigned int *     const maxleftb0P) {
/*----------------------------------------------------------------------------
  Flow or truncate lines to meet user's width request.
-----------------------------------------------------------------------------*/
    if (cmdline.width > 0) {
        unsigned int const fontMargin = fontP->x < 0 ? -fontP->x : 0;

        if (cmdline.width > INT_MAX -10)
            pm_error("-width value too large: %u", cmdline.width);
        else if (cmdline.width < 2 * hmargin)
            pm_error("-width value too small: %u", cmdline.width);
        else if (inputText.lineCount == 1) {
            flowText(inputText, cmdline.width - fontMargin,
                     fontP, cmdline.space, formattedTextP, maxleftb0P);
            freeTextArray(inputText);
        } else {
            truncateText(inputText, cmdline.width - fontMargin,
                         fontP, cmdline.space, maxleftb0P);
            *formattedTextP = inputText;
        }
    } else
        *formattedTextP = inputText;
}



static void
computeImageHeight(struct Text          const formattedText,
                   const struct font2 * const fontP,
                   int                  const interlineSpace,
                   unsigned int         const vmargin,
                   unsigned int       * const rowsP) {

    if (interlineSpace < 0 && fontP->maxheight < -interlineSpace)
        pm_error("-lspace value (%d) negative and exceeds font height.",
                 interlineSpace);
    else {
        double const rowsD = 2 * (double) vmargin +
            (double) formattedText.lineCount * fontP->maxheight +
            (double) (formattedText.lineCount-1) * interlineSpace;

        if (rowsD > INT_MAX-10)
            pm_error("Image height too large.");
        else
            *rowsP = (unsigned int) rowsD;
    }
}



static void
computeImageWidth(struct Text          const formattedText,
                  const struct font2 * const fontP,
                  float                const intercharacterSpace,
                  unsigned int         const hmargin,
                  unsigned int *       const colsP,
                  unsigned int *       const maxleftbP) {

    assert (pbm_maxfontwidth() < (INT_MAX - 10) / LINEBUFSIZE);

    if (intercharacterSpace < 0 && fontP->maxwidth < -intercharacterSpace)
        pm_error("negative -space value %.2f exceeds font width",
                 intercharacterSpace);
    else {
        /* Find the widest line, and the one that backs up the most past
           the nominal start of the line.
        */

        unsigned int lineNum;
        double rightExtreme;
        int leftExtreme;
        double colsD;

        rightExtreme = 0.0;  /* initial value */
        leftExtreme = 0;     /* initial value */

        for (lineNum = 0; lineNum < formattedText.lineCount;  ++lineNum) {
            double rightEdge;
            int leftEdge;

            getLineDimensions(formattedText.textArray[lineNum], fontP,
                              intercharacterSpace,
                              &rightEdge, &leftEdge);
            rightExtreme = MAX(rightExtreme, rightEdge);
            leftExtreme  = MIN(leftExtreme,  leftEdge);
        }
        leftExtreme = MIN(leftExtreme, 0);

        colsD = (double) (-leftExtreme) + rightExtreme + 2 * hmargin;

        if (colsD > INT_MAX-10)
            pm_error("Image width too large.");
        else
            *colsP = (unsigned int) colsD;

        *maxleftbP = (unsigned int) - leftExtreme;
    }
}



static void
renderText(unsigned int   const cols,
           unsigned int   const rows,
           struct font2 * const fontP,
           unsigned int   const hmargin,
           unsigned int   const vmargin,
           struct Text    const formattedText,
           unsigned int   const maxleftb,
           float          const space,
           int            const lspace,
           bool           const fixedAdvance,
           FILE *         const ofP) {

    bit ** const bits = pbm_allocarray(pbm_packed_bytes(cols), rows);

    /* Fill background with white */
    clearBackground(bits, cols, rows);

    /* Put the text in  */
    insertCharacters(bits, formattedText, fontP, vmargin, hmargin + maxleftb,
                     space, cols, rows, lspace, fixedAdvance);

    /* Free all font data */
    pbm_destroybdffont2(fontP);

    {
        unsigned int row;

        pbm_writepbminit(ofP, cols, rows, 0);

        for (row = 0; row < rows; ++row)
            pbm_writepbmrow_packed(ofP, bits[row], cols, 0);
    }

    pbm_freearray(bits, rows);
}



static PM_WCHAR const * sheetTextArray[] = {
L"M \",/^_[`jpqy| M",
L"                ",
L"/  !\"#$%&'()*+ /",
L"< ,-./01234567 <",
L"> 89:;<=>?@ABC >",
L"@ DEFGHIJKLMNO @",
L"_ PQRSTUVWXYZ[ _",
L"{ \\]^_`abcdefg {",
L"} hijklmnopqrs }",
L"~ tuvwxyz{|}~  ~",
L"                ",
L"M \",/^_[`jpqy| M" };



static void
renderSheet(struct CmdlineInfo const cmdline,
            FILE *             const ofP) {

    struct Text const sheetText =
        { (PM_WCHAR ** const) sheetTextArray, 12, 12};
    static unsigned char const sheetRequestArray[16] = {
         0x00, 0x00, 0x00, 0x00,  0xff, 0xff, 0xff, 0xff,
         0xff, 0xff, 0xff, 0xff,  0xff, 0xff, 0xff, 0xfe};

    struct pm_selector * selectorP;
    struct font2 *       fontP;
    bool                 fontIsComplete;

    pm_selector_create_fixed(sheetRequestArray, 32, 126,95, &selectorP);

    computeFont(cmdline, &fontP, selectorP, QUIT, &fontIsComplete);

    {
        unsigned int const cols  = fontP->maxwidth  * 16;
        unsigned int const rows  = fontP->maxheight * 12;

        renderText(cols, rows, fontP, 0, 0, sheetText, MAX(-(fontP->x),0),
                   0.0, 0, TRUE, ofP);
    }
    pm_selector_destroy(selectorP);
}



static void
dryrunOutput(unsigned int const cols,
             unsigned int const rows,
             FILE *       const ofP) {

    fprintf(ofP, "%u %u\n", cols, rows);
}



static void
textDumpOutput(struct Text   const lp,
               FILE *        const ofP) {
/*----------------------------------------------------------------------------
   Output the text 'lp' as characters.  (Do not render.)

   Note that the output stream is wide-oriented; it cannot be mixed with
   narrow-oriented output.  The libnetpbm library functions are
   narrow-oriented.  Thus, when this output is specified, it must not be mixed
   with any output from the library; it should be the sole output.
-----------------------------------------------------------------------------*/
    int rc;

    rc = fwide(ofP, 1);
    if (rc != 1) {
        /* This occurs when narrow-oriented output to ofP happens before we
           get here.
        */
        pm_error("Failed to set output stream to wide "
                 "(fwide() returned %d.  Maybe the output file "
                 "was written in narrow mode before this program was invoked?",
                 rc);
    } else {
        unsigned int line;  /* Line number in input text */

        for (line = 0; line < lp.lineCount; ++line) {
            fputws(lp.textArray[line], ofP);
            fputwc(L'\n', ofP);
        }
    }
}



static void
pbmtext(struct CmdlineInfo const cmdline,
        FILE *             const ofP,
        bool               const wchar) {

    unsigned int rows, cols;
        /* Dimensions in pixels of the output image */
    unsigned int cols0;
    unsigned int vmargin, hmargin;
        /* Margins in pixels we add to the output image */
    unsigned int hmargin0;
    struct Text inputText;
    struct Text formattedText;
    struct font2 * fontP;
    struct pm_selector * selectorP;
    unsigned int maxleftb, maxleftb0;
    bool fontIsComplete;

    pm_selector_create(wchar ? PM_FONT2_MAXGLYPH : PM_FONT_MAXGLYPH,
                       &selectorP);

    getText(cmdline.text, &inputText, selectorP);

    if (pm_selector_marked_ct(selectorP) == 0)
        pm_error("No input text.  Aborting.");

    computeFont(cmdline, &fontP, selectorP, cmdline.verbose ? WARN : SILENT,
                &fontIsComplete);

    computeMargins(cmdline, inputText, fontP, &vmargin, &hmargin0);

    if (!fontIsComplete)
        refineText(inputText, fontP);

    formatText(cmdline, inputText, fontP, hmargin0,
               &formattedText, &maxleftb0);

    computeImageHeight(formattedText, fontP, cmdline.lspace, vmargin, &rows);

    computeImageWidth(formattedText, fontP, cmdline.space,
                      cmdline.width > 0 ? 0 : hmargin0, &cols0, &maxleftb);

    if (cols0 == 0 || rows == 0)
        pm_error("Input is all whitespace and/or non-renderable characters.");

    if (cmdline.width == 0) {
        cols    = cols0;
        hmargin = hmargin0;
    } else {
        if (cmdline.width < cols0)
            pm_error("internal error: calculated image width (%u) exceeds "
                     "specified -width value: %u",
                     cols0, cmdline.width);
        else if (maxleftb0 != maxleftb)
            pm_error("internal error: contradicting backup values");
        else {
            hmargin = MIN(hmargin0, (cmdline.width - cols0) / 2);
            cols = cmdline.width;
        }
    }

    if (cmdline.dryrun)
        dryrunOutput(cols, rows, ofP);
    else if (cmdline.textdump)
        textDumpOutput(formattedText, ofP);
    else
        renderText(cols, rows, fontP, hmargin, vmargin, formattedText,
                   maxleftb, cmdline.space, cmdline.lspace, FALSE, ofP);

    freeTextArray(formattedText);

    pm_selector_destroy(selectorP);
}



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

    struct CmdlineInfo cmdline;

    pm_proginit(&argc, argv);

    parseCommandLine(argc, argv, &cmdline);

    if (cmdline.wchar) {
        char * newLocale;
        newLocale = setlocale(LC_ALL, "");
        if (!newLocale)
            pm_error("Failed to set locale (LC_ALL) from environment");

        /* Orient standard input stream to wide */
        fwide(stdin,  1);
    } else
        fwide(stdin, -1);

    if (cmdline.verbose)
        pm_message("LC_CTYPE is set to '%s'", setlocale(LC_CTYPE, NULL) );

    if (cmdline.dumpsheet)
        renderSheet(cmdline, stdout);
    else
        pbmtext(cmdline, stdout, cmdline.wchar);

    pm_close(stdout);

    return 0;
}