about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--doc/HISTORY2
-rw-r--r--generator/pbmtext.c531
-rw-r--r--lib/libpbmfont.c211
-rw-r--r--lib/pbmfont.h53
4 files changed, 581 insertions, 216 deletions
diff --git a/doc/HISTORY b/doc/HISTORY
index ce5f74b4..06e35482 100644
--- a/doc/HISTORY
+++ b/doc/HISTORY
@@ -6,6 +6,8 @@ CHANGE HISTORY
 
 not yet  BJH  Release 10.82.00
 
+              pbmtext: Add -wchar.
+
               pbmtext: Add -text-dump option.
 
               ppmhist: Add color summary to top of output, (except with
diff --git a/generator/pbmtext.c b/generator/pbmtext.c
index 9908eef3..488d8329 100644
--- a/generator/pbmtext.c
+++ b/generator/pbmtext.c
@@ -19,6 +19,8 @@
 #include <limits.h>
 #include <assert.h>
 #include <setjmp.h>
+#include <locale.h>
+#include <wchar.h>
 
 #include "pm_c_util.h"
 #include "mallocvar.h"
@@ -34,12 +36,13 @@ struct CmdlineInfo {
     /* All the information the user supplied in the command line,
        in a form easy for the program to use.
     */
-    const char * text;    /* text from command line or NULL if none */
+    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 */
@@ -50,6 +53,47 @@ struct CmdlineInfo {
 
 
 
+static const PM_WCHAR *
+textFmCmdLine(int argc, const char ** argv) {
+
+    char * text;
+    PM_WCHAR * wtext;
+    unsigned int i;
+    unsigned int totaltextsize;
+
+    MALLOCARRAY(text, MAXLINECHARS+1);
+
+    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 = 1; i < argc; ++i) {
+        if (i > 1) {
+            strcat(text, " ");
+        }
+        totaltextsize += strlen(argv[i]) + 1;
+        if (totaltextsize > MAXLINECHARS)
+            pm_error("input text too long");
+        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; ++i)
+        wtext[i] = (PM_WCHAR) text[i];
+
+    free(text);
+
+    return wtext;
+}
+
+
+
 static void
 parseCommandLine(int argc, const char ** argv,
                  struct CmdlineInfo * const cmdlineP) {
@@ -73,6 +117,7 @@ parseCommandLine(int argc, const char ** argv,
     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);
@@ -92,7 +137,7 @@ parseCommandLine(int argc, const char ** argv,
     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 == TRUE) {
+    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)
@@ -120,31 +165,13 @@ parseCommandLine(int argc, const char ** argv,
 
     if (argc-1 == 0)
         cmdlineP->text = NULL;
-    else {
-        char *text;
-        int i;
-        int totaltextsize;
-
-        totaltextsize = 1;  /* initial value */
-
-        MALLOCARRAY(text, MAXLINECHARS+1);
+    else {  /* Text to render is part of command line */
+        if (cmdlineP->wchar)
+            pm_error("-wchar is not valid when text is from command line");
 
-        if (!text)
-            pm_error("Unable to allocate memory for a buffer of up to %u "
-                     "characters of text", MAXLINECHARS);
+        cmdlineP->text = textFmCmdLine(argc, argv);
 
-        text[0] = '\0';
 
-        for (i = 1; i < argc; ++i) {
-            if (i > 1) {
-                strcat(text, " ");
-            }
-            totaltextsize += strlen(argv[i]) + 1;
-            if (totaltextsize > MAXLINECHARS)
-                pm_error("input text too long");
-            strcat(text, argv[i]);
-        }
-        cmdlineP->text = text;
     }
     free(option_def);
 }
@@ -152,7 +179,7 @@ parseCommandLine(int argc, const char ** argv,
 
 
 static void
-reportFont(struct font * const fontP) {
+reportFont(struct font2 * const fontP) {
 
     unsigned int n;
     unsigned int c;
@@ -162,9 +189,10 @@ reportFont(struct font * const fontP) {
                fontP->maxwidth, fontP->maxheight);
     pm_message("  Additional vert white space: %d pixels", fontP->y);
 
-    for (c = 0, n = 0; c < ARRAY_SIZE(fontP->glyph); ++c)
+    for (c = 0, n = 0; c <= fontP->maxglyph; ++c) {
         if (fontP->glyph[c])
             ++n;
+    }
 
     pm_message("  # characters: %u", n);
 }
@@ -174,7 +202,7 @@ reportFont(struct font * const fontP) {
 static struct font *
 fontFromFile(const char * const fileName) {
 
-    struct font * retval;
+    struct font  * retval;
 
     jmp_buf jmpbuf;
     int rc;
@@ -202,31 +230,69 @@ fontFromFile(const char * const fileName) {
 
 
 
+static struct font2 *
+font2FromFile(const char * const fileName) {
+
+    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_loadbdffont2(fileName, PM_FONT2_MAXGLYPH);
+
+        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 void
 computeFont(struct CmdlineInfo const cmdline,
-            struct font **     const fontPP) {
+            struct font2 **    const fontPP) {
 
-    struct font * fontP;
+    struct font2 * font2P;
 
-    if (cmdline.font)
-        fontP = fontFromFile(cmdline.font);
+    if (cmdline.wchar && cmdline.font)
+        font2P = font2FromFile(cmdline.font);
     else {
-        if (cmdline.builtin)
-            fontP = pbm_defaultfont(cmdline.builtin);
-        else
-            fontP = pbm_defaultfont("bdf");
+        struct font  * fontP;
+
+        if (cmdline.font)
+            fontP = fontFromFile(cmdline.font);
+        else {
+            if (cmdline.builtin)
+                fontP = pbm_defaultfont(cmdline.builtin);
+            else
+                fontP = pbm_defaultfont("bdf");
+        }
+        font2P = pbm_expandbdffont(fontP);
     }
 
     if (cmdline.verbose)
-        reportFont(fontP);
+        reportFont(font2P);
 
-    *fontPP = fontP;
+    *fontPP = font2P;
 }
 
 
 
 struct Text {
-    char **      textArray;  /* malloc'ed */
+    PM_WCHAR **     textArray;  /* malloc'ed */
         /* This is strictly characters that are in user's font - no control
            characters, no undefined code points.
         */
@@ -263,7 +329,7 @@ freeTextArray(struct Text const text) {
     unsigned int line;
 
     for (line = 0; line < text.allocatedLineCount; ++line)
-        free((char **)text.textArray[line]);
+        free((PM_WCHAR **)text.textArray[line]);
 
     free(text.textArray);
 }
@@ -276,10 +342,10 @@ enum FixMode {SILENT, /* convert silently */
 
 
 static void
-fixControlChars(const char *  const input,
-                struct font * const fontP,
-                const char ** const outputP,
-                enum FixMode  const fixMode) {
+fixControlChars(const PM_WCHAR  * const input,
+                struct font2    * const fontP,
+                const PM_WCHAR ** const outputP,
+                enum FixMode      const fixMode) {
 /*----------------------------------------------------------------------------
    Return a translation of input[] that can be rendered as glyphs in
    the font 'fontP'.  Return it as newly malloced *outputP.
@@ -304,10 +370,10 @@ fixControlChars(const char *  const input,
     unsigned int const tabSize = 8;
 
     unsigned int inCursor, outCursor;
-    char * output;      /* Output buffer.  Malloced */
+    PM_WCHAR * output;      /* Output buffer.  Malloced */
     size_t outputSize;  /* Currently allocated size of 'output' */
 
-    outputSize = strlen(input) + 1 + tabSize;
+    outputSize = wcslen(input) + 1 + tabSize;
         /* Leave room for one worst case tab expansion and NUL terminator */
     MALLOCARRAY(output, outputSize);
 
@@ -315,7 +381,8 @@ fixControlChars(const char *  const input,
         pm_error("Couldn't allocate %u bytes for a line of text.",
                  (unsigned)outputSize);
 
-    for (inCursor = 0, outCursor = 0; input[inCursor] != '\0'; ++inCursor) {
+    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);
@@ -323,29 +390,34 @@ fixControlChars(const char *  const input,
                 pm_error("Couldn't allocate %u bytes for a line of text.",
                          (unsigned)outputSize);
         }
-        if (input[inCursor] == '\n' && input[inCursor+1] == '\0') {
+        if (currentChar == L'\n' && input[inCursor+1] == L'\0') {
             /* This is a terminating newline.  We don't do those. */
-        } else if (input[inCursor] == '\t') {
+        } else if (currentChar == L'\t') {
             /* Expand this tab into the right number of spaces. */
             unsigned int const nextTabStop =
                 (outCursor + tabSize) / tabSize * tabSize;
 
-        if (fontP->glyph[(unsigned char)' '] == NULL)
-            pm_error("space character not defined in font");
+            if (fontP->glyph[L' '] == NULL)
+                pm_error("space character not defined in font");
 
             while (outCursor < nextTabStop)
-                output[outCursor++] = ' ';
-        } else if (!fontP->glyph[(unsigned char)input[inCursor]]) {
+                output[outCursor++] = L' ';
+        } else if (currentChar > fontP->maxglyph ||
+                   !fontP->glyph[currentChar]) {
+        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);
             /* Turn this unknown char into a single space. */
-            if (fontP->glyph[(unsigned char) ' '] == NULL)
+            if (fontP->glyph[L' '] == NULL)
                 pm_error("space character not defined in font");
             else if (fixMode == QUIT)
-                pm_error("character %d not defined in font",
-                         (unsigned int )input[inCursor] );
+                pm_error("code point %X not defined in font",
+                         (unsigned int) currentChar );
             else {
                 if (fixMode == WARN)
-                    pm_message("converting character %d to space",
-                               (unsigned int) input[inCursor] );
+                    pm_message("converting code point %X to space",
+                               (unsigned int) currentChar );
                 output[outCursor++] = ' ';
             }
         } else
@@ -353,7 +425,7 @@ fixControlChars(const char *  const input,
 
         assert(outCursor <= outputSize);
     }
-    output[outCursor++] = '\0';
+    output[outCursor++] = L'\0';
 
     assert(outCursor <= outputSize);
 
@@ -380,7 +452,7 @@ clearBackground(bit ** const bits,
 
 static void
 getEdges(double               const currentPosition,
-         char                 const currentChar,
+         PM_WCHAR             const currentChar,
          const struct glyph * const glyphP,
          int                  const currLeftEdge,
          double               const currRightEdge,
@@ -391,7 +463,7 @@ getEdges(double               const currentPosition,
     double rightEdge;
 
     if (glyphP == NULL)
-        pm_error("Unrenderable char: %c", currentChar);
+        pm_error("Unrenderable char: %04X", (unsigned int) currentChar);
     else {
         leftEdge  =  (int) MIN(currentPosition + glyphP->x, currLeftEdge);
         rightEdge =  MAX(currentPosition + glyphP->x + glyphP->width,
@@ -405,7 +477,7 @@ getEdges(double               const currentPosition,
 
 static void
 advancePosition(double               const currentPosition,
-                char                 const currentChar,
+                PM_WCHAR             const currentChar,
                 const struct glyph * const glyphP,
                 float                const space,
                 double               const accumulatedSpace,
@@ -429,8 +501,8 @@ advancePosition(double               const currentPosition,
             pm_error("Negative -space value too large");
         else
             pm_error("Abnormal horizontal advance value %d "
-                     "for char '%c' 0x%x.",
-                     glyphP->xadd, currentChar, (unsigned int) currentChar);
+                     "for code point 0x%lx.",
+                     glyphP->xadd, (unsigned long int) currentChar);
     }
     else if (currentPosition + advance > INT_MAX)
         pm_error("Image is too wide");
@@ -444,11 +516,11 @@ advancePosition(double               const currentPosition,
 
 
 static void
-getLineDimensions(char                const line[],
-                  const struct font * const fontP,
-                  float               const intercharacterSpace,
-                  double *            const rightEdgeP,
-                  int    *            const leftEdgeP) {
+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.
@@ -466,7 +538,7 @@ getLineDimensions(char                const line[],
    This often happens with fixed-width font in which the white areas on the
    sides are not trimmed.
 -----------------------------------------------------------------------------*/
-    int cursor;  /* cursor into the line of text */
+    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.
@@ -487,10 +559,10 @@ getLineDimensions(char                const line[],
     leftEdge  = INT_MAX;  /* initial value */
     rightEdge = INT_MIN;  /* initial value */
 
-    for (cursor = 0; line[cursor] != '\0'; ++cursor) {
-        char const currentChar = line[cursor];
-        struct glyph * const glyphP =
-            fontP->glyph[(unsigned char) currentChar];
+    for (cursor = 0; line[cursor] != L'\0'; ++cursor) {
+        PM_WCHAR          const currentChar = line[cursor];
+        unsigned long int const glyphIndex  = (unsigned long int) currentChar;
+        struct glyph *    const glyphP      = fontP->glyph[glyphIndex];
 
         getEdges(currentPosition, currentChar, glyphP, leftEdge, rightEdge,
                  &leftEdge, &rightEdge);
@@ -500,7 +572,7 @@ getLineDimensions(char                const line[],
                         &currentPosition, &accumulatedIcs);
     }
 
-    if (line[0] == '\0') {     /* Empty line */
+    if (line[0] == L'\0') {     /* Empty line */
         leftEdge  = 0;
         rightEdge = 0.0;
     }
@@ -512,12 +584,12 @@ getLineDimensions(char                const line[],
 
 
 static void
-getCharsWithinWidth(char                const line[],
-                    const struct font * const fontP,
-                    float               const intercharacter_space,
-                    unsigned int        const targetWidth,
-                    unsigned int      * const charCountP,
-                    int               * const leftEdgeP) {
+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.
@@ -525,7 +597,7 @@ getCharsWithinWidth(char                const line[],
    *leftEdgeP will be negative if the leftmost character in the line has a
    "backup" distance and zero if it does not.
 -----------------------------------------------------------------------------*/
-    if (line[0] == '\0') {
+    if (line[0] == L'\0') {
         /* Empty line */
         *leftEdgeP = 0;
         *charCountP = 0;
@@ -544,11 +616,12 @@ getCharsWithinWidth(char                const line[],
         rightEdge    = INT_MIN;  /* initial value */
 
         for (cursor = 0, currentWidth = 0;
-             currentWidth <= targetWidth && line[cursor] != '\0';
+             currentWidth <= targetWidth && line[cursor] != L'\0';
              ++cursor) {
-            char const currentChar = line[cursor];
-            struct glyph * const glyphP =
-                fontP->glyph[(unsigned char) currentChar];
+            PM_WCHAR const currentChar = line[cursor];
+            unsigned long int const glyphIndex =
+              (unsigned long int) currentChar;
+            struct glyph * const glyphP = fontP->glyph[glyphIndex];
 
             getEdges(currentPosition, currentChar, glyphP, leftEdge, rightEdge,
                      &leftEdge, &rightEdge);
@@ -613,16 +686,16 @@ insertCharacter(const struct glyph * const glyphP,
 
 
 static void
-insertCharacters(bit **        const bits,
-                 struct Text   const lp,
-                 struct font * 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) {
+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
@@ -646,8 +719,9 @@ insertCharacters(bit **        const bits,
         accumulatedIcs = 0.0;  /* initial value */
 
         for (cursor = 0; lp.textArray[line][cursor] != '\0'; ++cursor) {
-            char const currentChar = lp.textArray[line][cursor];
-            unsigned int const glyphIndex = (unsigned char) currentChar;
+            PM_WCHAR const currentChar = lp.textArray[line][cursor];
+            unsigned long int const glyphIndex =
+                (unsigned long int)currentChar;
             struct glyph * const glyphP = fontP->glyph[glyphIndex];
             int const toprow =
                 row + fontP->maxheight + fontP->y - glyphP->height - glyphP->y;
@@ -672,7 +746,7 @@ insertCharacters(bit **        const bits,
 static void
 flowText(struct Text    const inputText,
          int            const targetWidth,
-         struct font  * const fontP,
+         struct font2 * const fontP,
          float          const intercharacterSpace,
          struct Text  * const outputTextP,
          unsigned int * const maxleftbP) {
@@ -687,7 +761,7 @@ flowText(struct Text    const inputText,
     allocTextArray(outputTextP, maxLineCount, 0);
 
     for (incursor = 0, outputLineNum = 0;
-         inputText.textArray[0][incursor] != '\0'; ) {
+         inputText.textArray[0][incursor] != L'\0'; ) {
 
         unsigned int outcursor;
 
@@ -707,7 +781,7 @@ flowText(struct Text    const inputText,
             outputTextP->textArray[outputLineNum][outcursor] =
                 inputText.textArray[0][incursor];
 
-        outputTextP->textArray[outputLineNum][charCount] = '\0';
+        outputTextP->textArray[outputLineNum][charCount] = L'\0';
         ++outputLineNum;
         if (outputLineNum >= maxLineCount)
             pm_error("-width too small.  too many output lines");
@@ -723,7 +797,7 @@ flowText(struct Text    const inputText,
 static void
 truncateText(struct Text    const inputText,
              unsigned int   const targetWidth,
-             struct font  * const fontP,
+             struct font2 * const fontP,
              float          const intercharacterSpace,
              unsigned int * const maxleftbP) {
 
@@ -732,7 +806,7 @@ truncateText(struct Text    const inputText,
     int leftExtreme = 0;
 
     for (lineNum = 0; lineNum < inputText.lineCount; ++lineNum) {
-        char * const currentLine = inputText.textArray[lineNum];
+        PM_WCHAR * const currentLine = inputText.textArray[lineNum];
 
         unsigned int charCount;
 
@@ -740,10 +814,10 @@ truncateText(struct Text    const inputText,
                             intercharacterSpace, targetWidth,
                             &charCount, &leftEdge);
 
-        if (currentLine[charCount] != '\0') {
+        if (currentLine[charCount] != L'\0') {
             pm_message("truncating line %u from %u to %u characters",
-                       lineNum, (unsigned) strlen(currentLine), charCount);
-            currentLine[charCount] = '\0';
+                       lineNum, (unsigned) wcslen(currentLine), charCount);
+            currentLine[charCount] = L'\0';
         }
 
         leftExtreme = MIN(leftEdge, leftExtreme);
@@ -754,10 +828,66 @@ truncateText(struct Text    const inputText,
 
 
 static void
-getText(char          const cmdlineText[],
-        struct font * const fontP,
-        struct Text * const inputTextP,
-        enum FixMode  const fixMode) {
+fgetNarrowWideString(PM_WCHAR *    const widestring,
+                     unsigned int  const size,
+                     FILE *        const ifP,
+                     const char ** const errorP) {
+/*----------------------------------------------------------------------------
+  Return the next line from file *ifP, up to 'size' characters, as
+  *widestring.
+
+  Return error if we can't read the file, file is at EOF, or next thing in
+  file is an empty line.
+-----------------------------------------------------------------------------*/
+    int wideCode;
+        /* Width orientation for *ifP: positive means wide, negative means
+           byte, zero means undecided.
+        */
+
+    assert(widestring);
+    assert(size > 0);
+
+    wideCode = fwide(ifP, 0);
+    if (wideCode > 0) {
+        /* *ifP is wide-oriented */
+        wchar_t * rc;
+        rc = fgetws(widestring, size, ifP);
+        if (rc == NULL)
+            pm_asprintf(errorP,
+                        "fgetws() of max %u bytes failed or end of stream",
+                        size);
+        else
+            *errorP = NULL;
+    } else {
+        char * bufNarrow;
+        char * rc;
+
+        MALLOCARRAY_NOFAIL(bufNarrow, MAXLINECHARS+1);
+
+        rc = fgets(bufNarrow, size, ifP);
+        if (rc == NULL)
+            pm_asprintf(errorP, "EOF or 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';
+            *errorP = NULL;
+        }
+        free(bufNarrow);
+    }
+}
+
+
+
+
+static void
+getText(PM_WCHAR       const cmdlineText[],
+        struct font2 * const fontP,
+        struct Text  * const inputTextP,
+        enum FixMode   const fixMode) {
 /*----------------------------------------------------------------------------
    Get as *inputTextP the text to format, given that the text on the
    command line (one word per command line argument, separated by spaces),
@@ -777,7 +907,7 @@ getText(char          const cmdlineText[],
         inputText.allocatedLineCount = 1;
         inputText.lineCount = 1;
         fixControlChars(cmdlineText, fontP,
-                        (const char**)&inputText.textArray[0], fixMode);
+                        (const PM_WCHAR**)&inputText.textArray[0], fixMode);
     } else {
         /* Read text from stdin. */
 
@@ -785,9 +915,10 @@ getText(char          const cmdlineText[],
             /* Maximum number of lines for which we presently have space in
                the text array
             */
-        char * buf;
-        char ** textArray;
+        PM_WCHAR *   buf;
+        PM_WCHAR **  textArray;
         unsigned int lineCount;
+        bool         eof;
 
         MALLOCARRAY(buf, MAXLINECHARS+1);
 
@@ -802,22 +933,33 @@ getText(char          const cmdlineText[],
             pm_error("Unable to allocate memory for a buffer for up to %u "
                      "lines of text", maxlines);
 
-        lineCount = 0;  /* initial value */
-        while (fgets(buf, MAXLINECHARS, stdin) != NULL) {
-            if (strlen(buf) + 1 >= MAXLINECHARS)
-                pm_error("A line of input text is longer than %u characters."
-                         "Cannot process", (unsigned int) MAXLINECHARS-1);
-            if (lineCount >= maxlines) {
-                maxlines *= 2;
-                REALLOCARRAY(textArray, maxlines);
-                if (textArray == NULL)
+        for (lineCount = 0, eof = false; !eof; ) {
+            const char * error;
+            fgetNarrowWideString(buf, MAXLINECHARS, stdin, &error);
+            if (error) {
+                /* We're lazy, so we treat any error as EOF */
+                pm_strfree(error);
+                eof = true;
+            } else {
+                if (wcslen(buf) + 1 >= MAXLINECHARS)
+                    pm_error(
+                        "Line %u (starting at zero) of input text "
+                        "is longer than %u characters."
+                        "Cannot process",
+                        lineCount, (unsigned int) MAXLINECHARS-1);
+                if (lineCount >= maxlines) {
+                    maxlines *= 2;
+                    REALLOCARRAY(textArray, maxlines);
+                    if (textArray == NULL)
+                        pm_error("out of memory");
+                }
+                fixControlChars(buf, fontP,
+                                (const PM_WCHAR **)&textArray[lineCount],
+                                fixMode);
+                if (textArray[lineCount] == NULL)
                     pm_error("out of memory");
+                ++lineCount;
             }
-            fixControlChars(buf, fontP,
-                            (const char **)&textArray[lineCount], fixMode);
-            if (textArray[lineCount] == NULL)
-                pm_error("out of memory");
-            ++lineCount;
         }
         inputText.textArray = textArray;
         inputText.lineCount = lineCount;
@@ -831,7 +973,7 @@ getText(char          const cmdlineText[],
 static void
 computeMargins(struct CmdlineInfo const cmdline,
                struct Text        const inputText,
-               struct font *      const fontP,
+               struct font2 *     const fontP,
                unsigned int *     const vmarginP,
                unsigned int *     const hmarginP) {
 
@@ -854,7 +996,7 @@ computeMargins(struct CmdlineInfo const cmdline,
 static void
 formatText(struct CmdlineInfo const cmdline,
            struct Text        const inputText,
-           struct font *      const fontP,
+           struct font2 *     const fontP,
            unsigned int       const hmargin,
            struct Text *      const formattedTextP,
            unsigned int *     const maxleftb0P) {
@@ -884,11 +1026,11 @@ formatText(struct CmdlineInfo const cmdline,
 
 
 static void
-computeImageHeight(struct Text         const formattedText,
-                   const struct font * const fontP,
-                   int                 const interlineSpace,
-                   unsigned int        const vmargin,
-                   unsigned int      * const rowsP) {
+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.",
@@ -908,12 +1050,12 @@ computeImageHeight(struct Text         const formattedText,
 
 
 static void
-computeImageWidth(struct Text         const formattedText,
-                  const struct font * const fontP,
-                  float               const intercharacterSpace,
-                  unsigned int        const hmargin,
-                  unsigned int *      const colsP,
-                  unsigned int *      const maxleftbP) {
+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) {
 
     if (intercharacterSpace < 0 && fontP->maxwidth < -intercharacterSpace)
         pm_error("negative -space value %.2f exceeds font width",
@@ -957,17 +1099,17 @@ computeImageWidth(struct Text         const formattedText,
 
 
 static void
-renderText(unsigned int  const cols,
-           unsigned int  const rows,
-           struct font * 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) {
+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);
 
@@ -992,47 +1134,48 @@ renderText(unsigned int  const cols,
 
 
 
-static char const * sheetTextArray[] = {
-"M \",/^_[`jpqy| M",
-"                ",
-"/  !\"#$%&'()*+ /",
-"< ,-./01234567 <",
-"> 89:;<=>?@ABC >",
-"@ DEFGHIJKLMNO @",
-"_ PQRSTUVWXYZ[ _",
-"{ \\]^_`abcdefg {",
-"} hijklmnopqrs }",
-"~ tuvwxyz{|}~  ~",
-"                ",
-"M \",/^_[`jpqy| M" };
+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
-validateText(const char ** const textArray,
-             struct font * const fontP) {
+validateText(const PM_WCHAR ** const textArray,
+             struct font2    * const fontP) {
 /*----------------------------------------------------------------------------
    Abort the program if there are characters in 'textArray' which cannot be
    rendered in font *fontP.
 -----------------------------------------------------------------------------*/
-    const char * output;
+    const PM_WCHAR * output;
     unsigned int textRow;
 
     for (textRow = 0; textRow < 12; ++textRow)
         fixControlChars(textArray[textRow], fontP, &output, QUIT);
 
-    pm_strfree(output);
+    free((PM_WCHAR *)output);
 }
 
 
 
 static void
-renderSheet(struct font * const fontP,
-            FILE *        const ofP) {
+renderSheet(struct font2 * const fontP,
+            FILE *         const ofP) {
 
     int const cols  = fontP->maxwidth  * 16;
     int const rows  = fontP->maxheight * 12;
-    struct Text const sheetText = { (char ** const) sheetTextArray, 12, 12};
+    struct Text const sheetText =
+        { (PM_WCHAR ** const) sheetTextArray, 12, 12};
 
     validateText(sheetTextArray, fontP);
 
@@ -1057,12 +1200,30 @@ 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.
 -----------------------------------------------------------------------------*/
-    unsigned int line;  /* Line number in input text */
+    int rc;
 
-    for (line = 0; line < lp.lineCount; ++line) {
-        fputs(lp.textArray[line], ofP);
-        fputc('\n', ofP);
+    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);
+        }
     }
 }
 
@@ -1070,7 +1231,7 @@ textDumpOutput(struct Text   const lp,
 
 static void
 pbmtext(struct CmdlineInfo const cmdline,
-        struct font *      const fontP,
+        struct font2 *     const fontP,
         FILE *             const ofP) {
 
     unsigned int rows, cols;
@@ -1135,12 +1296,26 @@ int
 main(int argc, const char *argv[]) {
 
     struct CmdlineInfo cmdline;
-    struct font * fontP;
+    struct font2 * fontP;
 
     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 environemt");
+
+        /* 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) );
+
     computeFont(cmdline, &fontP);
 
     if (cmdline.dumpsheet)
@@ -1148,6 +1323,10 @@ main(int argc, const char *argv[]) {
     else
         pbmtext(cmdline, fontP, stdout);
 
+    /* Note that *fontP is unfreeable.  See pbm_loadbdffont2,
+       pbm_expandbdffont
+    */
+
     pm_close(stdout);
 
     return 0;
diff --git a/lib/libpbmfont.c b/lib/libpbmfont.c
index 8e854aa6..8f308eda 100644
--- a/lib/libpbmfont.c
+++ b/lib/libpbmfont.c
@@ -256,7 +256,7 @@ pbm_dissectfont(const bit ** const font,
     /* Initialize all character positions to "undefined."  Those that
        are defined in the font will be filled in below.
     */
-    for (i = 0; i < 256; i++)
+    for (i = 0; i < PM_FONT_MAXGLYPH + 1; i++)
         fn->glyph[i] = NULL;
 
     MALLOCARRAY(glyph, nCharsInFont);
@@ -294,7 +294,7 @@ pbm_dissectfont(const bit ** const font,
             row += cellHeight;
         }
     }
-    for (i = firstCodePoint + nCharsInFont; i < 256; ++i)
+    for (i = firstCodePoint + nCharsInFont; i < PM_FONT_MAXGLYPH + 1; ++i)
         fn->glyph[i] = NULL;
     
     return fn;
@@ -377,14 +377,14 @@ pbm_dumpfont(struct font * const fontP,
         pm_message("Netpbm no longer has the capability to generate "
                    "a font in long hexadecimal data format");
 
-    for (i = 0, ng = 0; i < 256; ++i) {
+    for (i = 0, ng = 0; i < PM_FONT_MAXGLYPH +1; ++i) {
         if (fontP->glyph[i])
             ++ng;
     }
 
     printf("static struct glyph _g[%d] = {\n", ng);
 
-    for (i = 0; i < 256; ++i) {
+    for (i = 0; i < PM_FONT_MAXGLYPH + 1; ++i) {
         struct glyph * const glyphP = fontP->glyph[i];
         if (glyphP) {
             unsigned int j;
@@ -409,13 +409,13 @@ pbm_dumpfont(struct font * const fontP,
     {
         unsigned int i;
 
-        for (i = 0; i < 256; ++i) {
+        for (i = 0; i < PM_FONT_MAXGLYPH + 1; ++i) {
             if (fontP->glyph[i])
                 printf(" _g + %d", ng++);
             else
                 printf(" NULL");
         
-            if (i != 255) printf(",");
+            if (i != PM_FONT_MAXGLYPH) printf(",");
             printf("\n");
         }
     }
@@ -738,7 +738,8 @@ skipCharacter(Readline * const readlineP) {
 static void
 interpEncoding(const char **  const arg,
                unsigned int * const codepointP,
-               bool *         const badCodepointP) {
+               bool *         const badCodepointP,
+               PM_WCHAR       const maxglyph) {
 /*----------------------------------------------------------------------------
    With arg[] being the ENCODING statement from the font, return as
    *codepointP the codepoint that it indicates (code point is the character
@@ -746,8 +747,9 @@ interpEncoding(const char **  const arg,
 
    But if the statement doesn't give an acceptable codepoint return
    *badCodepointP == TRUE.
------------------------------------------------------------------------------*/
 
+   'maxglyph' is the maximum codepoint in the font.
+-----------------------------------------------------------------------------*/
     bool gotCodepoint;
     bool badCodepoint;
     unsigned int codepoint;
@@ -763,7 +765,7 @@ interpEncoding(const char **  const arg,
             gotCodepoint = false;
     }
     if (gotCodepoint) {
-        if (codepoint > 255)
+        if (codepoint > maxglyph)
             badCodepoint = true;
         else
             badCodepoint = false;
@@ -779,17 +781,18 @@ interpEncoding(const char **  const arg,
 static void
 readEncoding(Readline *     const readlineP,
              unsigned int * const codepointP,
-             bool *         const badCodepointP) {
+             bool *         const badCodepointP,
+             PM_WCHAR       const maxglyph) {
 
     readExpectedStatement(readlineP, "ENCODING");
     
-    interpEncoding(readlineP->arg, codepointP, badCodepointP);
+    interpEncoding(readlineP->arg, codepointP, badCodepointP, maxglyph);
 }
 
 
 
 static void
-validateFontLimits(const struct font * const fontP) {
+validateFontLimits(const struct font2 * const fontP) {
 
     assert(pbm_maxfontheight() > 0 && pbm_maxfontwidth() > 0);
 
@@ -797,22 +800,27 @@ validateFontLimits(const struct font * const fontP) {
         fontP->maxheight <= 0 ||
         fontP->maxwidth  > pbm_maxfontwidth()  ||
         fontP->maxheight > pbm_maxfontheight() ||
-        fontP->x < - fontP->maxwidth  +1 ||
-        fontP->y < - fontP->maxheight +1 ||
+        -fontP->x + 1 > fontP->maxwidth ||
+        -fontP->y + 1 > fontP->maxheight ||
         fontP->x > fontP->maxwidth  ||
         fontP->y > fontP->maxheight ||
         fontP->x + fontP->maxwidth  > pbm_maxfontwidth() || 
         fontP->y + fontP->maxheight > pbm_maxfontheight()
         ) {
 
-        pm_error("Global font metric(s) out of bounds.\n"); 
+        pm_error("Global font metric(s) out of bounds."); 
     }
+
+    if (fontP->maxglyph > PM_FONT2_MAXGLYPH)
+        pm_error("Internal error.  Glyph table too large: %u glyphs; "
+                 "Maximum possible in Netpbm is %u",
+                 fontP->maxglyph, PM_FONT2_MAXGLYPH);
 }
 
 
 
 static void
-validateGlyphLimits(const struct font  * const fontP,
+validateGlyphLimits(const struct font2 * const fontP,
                     const struct glyph * const glyphP,
                     const char *         const charName) {
 
@@ -842,7 +850,8 @@ validateGlyphLimits(const struct font  * const fontP,
 
 static void
 processChars(Readline *     const readlineP,
-             struct font  * const fontP) {
+             struct font2 * const fontP,
+             PM_WCHAR       const maxglyph ) {
 /*----------------------------------------------------------------------------
    Process the CHARS block in a BDF font file, assuming the file is positioned
    just after the CHARS line.  Read the rest of the block and apply its
@@ -880,7 +889,7 @@ processChars(Readline *     const readlineP,
                 pm_error("no memory for font glyph for '%s' character",
                          charName);
 
-            readEncoding(readlineP, &codepoint, &badCodepoint);
+            readEncoding(readlineP, &codepoint, &badCodepoint, maxglyph);
 
             if (badCodepoint)
                 skipCharacter(readlineP);
@@ -907,7 +916,7 @@ processChars(Readline *     const readlineP,
 
                 readExpectedStatement(readlineP, "ENDCHAR");
 
-                assert(codepoint < 256); /* Ensured by readEncoding() */
+                assert(codepoint <= maxglyph); /* Ensured by readEncoding() */
 
                 fontP->glyph[codepoint] = glyphP;
                 pm_strfree(charName);
@@ -921,8 +930,9 @@ processChars(Readline *     const readlineP,
 
 static void
 processBdfFontLine(Readline     * const readlineP,
-                   struct font  * const fontP,
-                   bool         * const endOfFontP) {
+                   struct font2 * const fontP,
+                   bool         * const endOfFontP,
+                   PM_WCHAR       const maxglyph) {
 /*----------------------------------------------------------------------------
    Process a nonblank line just read from a BDF font file.
 
@@ -964,7 +974,7 @@ processBdfFontLine(Readline     * const readlineP,
       pm_error("Encountered CHARS before FONTBOUNDINGBOX " 
                    "in BDF font file");
       else
-        processChars(readlineP, fontP);
+        processChars(readlineP, fontP, maxglyph);
     } else {
         /* ignore */
     }
@@ -972,36 +982,57 @@ processBdfFontLine(Readline     * const readlineP,
 
 
 
-struct font *
-pbm_loadbdffont(const char * const name) {
+struct font2 *
+pbm_loadbdffont2(const char * const filename,
+                 PM_WCHAR     const maxglyph) {
+/*----------------------------------------------------------------------------
+   Read a BDF font file "filename" as a 'font2' structure.  A 'font2'
+   structure is more expressive than a 'font' structure, most notably in that
+   it can handle wide code points and many more glyphs.
 
-    FILE * ifP;
-    Readline readline;
-    struct font * fontP;
-    bool endOfFont;
+   Codepoints up to maxglyph inclusive are valid in the file.
 
-    ifP = fopen(name, "rb");
+   The returned object is in new malloc'ed storage, in many pieces, and
+   cannot be destroyed.
+-----------------------------------------------------------------------------*/
+    /* For our return object to be destroyable, we need to supply a destroy
+       function, and it needs to return glyph and raster memory, and
+       struct font needs to manage glyph memory separately from mapping
+       code points to glyphs.
+    */
+    FILE *         ifP;
+    Readline       readline;
+    struct font2 * font2P;
+    bool           endOfFont;
+
+    ifP = fopen(filename, "rb");
     if (!ifP)
         pm_error("Unable to open BDF font file name '%s'.  errno=%d (%s)",
-                 name, errno, strerror(errno));
+                 filename, errno, strerror(errno));
 
     readline_init(&readline, ifP);
 
-    MALLOCVAR(fontP);
-    if (fontP == NULL)
+    MALLOCVAR(font2P);
+    if (font2P == NULL)
         pm_error("no memory for font");
 
-    fontP->oldfont = 0;
-    { 
+    MALLOCARRAY(font2P->glyph, maxglyph + 1);
+    if (font2P->glyph == NULL)
+        pm_error("no memory for font glyphs");
+
+    font2P->maxglyph = maxglyph;
+
+    font2P->oldfont = NULL;
+    {
         /* Initialize all characters to nonexistent; we will fill the ones we
            find in the bdf file later.
         */
-        unsigned int i;
-        for (i = 0; i < 256; ++i) 
-            fontP->glyph[i] = NULL;
+        PM_WCHAR i;
+        for (i = 0; i <= maxglyph; ++i)
+            font2P->glyph[i] = NULL;
     }
 
-    fontP->maxwidth = fontP->maxheight = fontP->x = fontP->y = 0;
+    font2P->maxwidth = font2P->maxheight = font2P->x = font2P->y = 0;
 
     readExpectedStatement(&readline, "STARTFONT");
 
@@ -1013,10 +1044,112 @@ pbm_loadbdffont(const char * const name) {
         if (eof)
             pm_error("End of file before ENDFONT statement in BDF font file");
 
-        processBdfFontLine(&readline, fontP, &endOfFont);
+        processBdfFontLine(&readline, font2P, &endOfFont, maxglyph);
     }
+    return font2P;
+}
+
+
+
+struct font *
+pbm_loadbdffont(const char * const filename) {
+/*----------------------------------------------------------------------------
+   Read a BDF font file "filename" into a traditional font structure.
+
+   Codepoints up to 255 (PM_FONT_MAXGLYPH) are valid.
+
+   Can handle ASCII, ISO-8859-1, ISO-8859-2, ISO-8859-15, etc.
+
+   The returned object is in new malloc'ed storage, in many pieces, and
+   cannot be destroyed.
+-----------------------------------------------------------------------------*/
+    /* For our return object to deep copy the glyphs and fonts from
+       the struct font2be destroyable, we need to supply a destroy
+       function, and it needs to return glyph and raster memory, and
+       struct font needs to manage glyph memory separately from mapping
+       code points to glyphs.
+    */
+    struct font  * fontP;
+    struct font2 * font2P;
+    unsigned int   codePoint;
+
+    MALLOCVAR(fontP);
+    if (fontP == NULL)
+        pm_error("no memory for font");
+
+    font2P = pbm_loadbdffont2(filename, PM_FONT_MAXGLYPH);
+
+    fontP->maxwidth  = font2P->maxwidth;
+    fontP->maxheight = font2P->maxheight;
+
+    fontP->x = font2P->x;
+    fontP->y = font2P->y;
+
+    for (codePoint = 0; codePoint < PM_FONT_MAXGLYPH + 1; ++codePoint)
+        fontP->glyph[codePoint] = font2P->glyph[codePoint];
+
+    fontP->oldfont = NULL;
+
+    fontP->fcols = 0;
+    fontP->frows = 0;
+
+    /* Note that *fontP2 is unfreeable.  See pbm_loadbdffont2.  And even if it
+       were, we hooked *fontP into it above, so that would have to turn into a
+       deep copy before we could free *fontP2.
+    */
+
     return fontP;
 }
 
 
 
+struct font2 *
+pbm_expandbdffont(const struct font * const fontP) {
+/*----------------------------------------------------------------------------
+  Convert a traditional font structure into an expanded font2 structure.
+
+  This function depends upon the fact that *fontP, like any struct font,
+  cannot be destroyed.  The returned object refers to memory that belongs
+  to *fontP.
+
+  The returned object is in new malloc'ed storage, in many pieces, and
+  cannot be destroyed.
+-----------------------------------------------------------------------------*/
+    /* If we ever make struct font destroyable, this function needs to
+       copy the glyphs and rasters, and struct font and struct font2 need
+       to manage glyph memory separately from mapping code points to the
+       glyphs.
+    */
+    PM_WCHAR const maxglyph = PM_FONT_MAXGLYPH;
+
+    struct font2 * font2P;
+    unsigned int   codePoint;
+
+    MALLOCVAR(font2P);
+    if (font2P == NULL)
+        pm_error("no memory for font");
+
+    MALLOCARRAY(font2P->glyph, maxglyph + 1);
+    if (font2P->glyph == NULL)
+        pm_error("no memory for font glyphs");
+
+    font2P->maxwidth  = fontP->maxwidth;
+    font2P->maxheight = fontP->maxheight;
+
+    font2P->x = fontP->x;
+    font2P->y = fontP->y;
+
+    font2P->maxglyph = maxglyph;
+
+    for (codePoint = 0; codePoint < maxglyph + 1; ++codePoint)
+        font2P->glyph[codePoint] = fontP->glyph[codePoint];
+
+    font2P->oldfont = fontP->oldfont;
+
+    font2P->fcols = fontP->fcols;
+    font2P->frows = fontP->frows;
+
+    return font2P;
+}
+
+
diff --git a/lib/pbmfont.h b/lib/pbmfont.h
index 5111a075..ad5d3acf 100644
--- a/lib/pbmfont.h
+++ b/lib/pbmfont.h
@@ -20,6 +20,23 @@ extern "C" {
        a 65536 x 65536 glyph occupies 4G pixels. 
     */
 
+typedef wchar_t PM_WCHAR;
+    /* Precaution to make adjustments, if necessary, for systems with
+       unique wchar_t.
+    */
+
+#define PM_FONT_MAXGLYPH 255
+
+#define PM_FONT2_MAXGLYPH 65535
+    /* Upper limit of codepoint value.
+
+       This is large enough to handle Unicode Plane 0 (Basic Multilingual
+       Plane: BMP) which defines the great majority of characters used in
+       modern languages.
+
+       This can be set to a higher value at some cost to efficiency.
+       As of Unicode v. 11.0.0 planes up to 16 are defined.
+    */
 
 struct glyph {
     /* A glyph consists of white borders and the "central glyph" which
@@ -60,7 +77,7 @@ struct font {
        an code point in the range 0..255, this structure describes the
        glyph for that character.
     */
-    int maxwidth, maxheight;
+    unsigned int maxwidth, maxheight;
     int x;
         /* The minimum value of glyph.font.  The left edge of the glyph
            in the glyph set which advances furthest to the left. */
@@ -76,6 +93,33 @@ struct font {
     int fcols, frows;
 };
 
+
+struct font2 {
+    /* Font structure for expanded character set.  Code point is in the
+       range 0..maxglyph .
+     */
+    int maxwidth, maxheight;
+
+    int x;
+        /* The minimum value of glyph.font.  The left edge of the glyph
+           in the glyph set which advances furthest to the left. */
+    int y;
+        /* Amount of white space that should be added between lines of
+           this font.  Can be negative.
+        */
+    struct glyph ** glyph;
+        /* glyph[i] is the glyph for code point i */
+
+    PM_WCHAR maxglyph;
+        /* max code point for glyphs, including vacant slots */
+
+    const bit ** oldfont;
+        /* for compatibility with old pbmtext routines */
+        /* oldfont is NULL if the font is BDF derived */
+
+    unsigned int fcols, frows;
+};
+
 struct font *
 pbm_defaultfont(const char* const which);
 
@@ -93,6 +137,13 @@ pbm_loadpbmfont(const char * const filename);
 struct font *
 pbm_loadbdffont(const char * const filename);
 
+struct font2 *
+pbm_loadbdffont2(const char * const filename,
+                 PM_WCHAR     const maxglyph);
+
+struct font2 *
+pbm_expandbdffont(const struct font * const font);
+
 void
 pbm_dumpfont(struct font * const fontP,
              FILE *        const ofP);