Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright 2000 Computing Research Labs, New Mexico State University
  3.  * Copyright 2001-2013
  4.  *   Francesco Zappa Nardelli
  5.  *
  6.  * Permission is hereby granted, free of charge, to any person obtaining a
  7.  * copy of this software and associated documentation files (the "Software"),
  8.  * to deal in the Software without restriction, including without limitation
  9.  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10.  * and/or sell copies of the Software, and to permit persons to whom the
  11.  * Software is furnished to do so, subject to the following conditions:
  12.  *
  13.  * The above copyright notice and this permission notice shall be included in
  14.  * all copies or substantial portions of the Software.
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
  19.  * THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
  20.  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
  21.  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
  22.  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23.  */
  24.  
  25.   /*************************************************************************/
  26.   /*                                                                       */
  27.   /*  This file is based on bdf.c,v 1.22 2000/03/16 20:08:50               */
  28.   /*                                                                       */
  29.   /*  taken from Mark Leisher's xmbdfed package                            */
  30.   /*                                                                       */
  31.   /*************************************************************************/
  32.  
  33.  
  34. #include <ft2build.h>
  35.  
  36. #include FT_FREETYPE_H
  37. #include FT_INTERNAL_DEBUG_H
  38. #include FT_INTERNAL_STREAM_H
  39. #include FT_INTERNAL_OBJECTS_H
  40.  
  41. #include "bdf.h"
  42. #include "bdferror.h"
  43.  
  44.  
  45.   /*************************************************************************/
  46.   /*                                                                       */
  47.   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  48.   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  49.   /* messages during execution.                                            */
  50.   /*                                                                       */
  51. #undef  FT_COMPONENT
  52. #define FT_COMPONENT  trace_bdflib
  53.  
  54.  
  55.   /*************************************************************************/
  56.   /*                                                                       */
  57.   /* Default BDF font options.                                             */
  58.   /*                                                                       */
  59.   /*************************************************************************/
  60.  
  61.  
  62.   static const bdf_options_t  _bdf_opts =
  63.   {
  64.     1,                /* Correct metrics.               */
  65.     1,                /* Preserve unencoded glyphs.     */
  66.     0,                /* Preserve comments.             */
  67.     BDF_PROPORTIONAL  /* Default spacing.               */
  68.   };
  69.  
  70.  
  71.   /*************************************************************************/
  72.   /*                                                                       */
  73.   /* Builtin BDF font properties.                                          */
  74.   /*                                                                       */
  75.   /*************************************************************************/
  76.  
  77.   /* List of most properties that might appear in a font.  Doesn't include */
  78.   /* the RAW_* and AXIS_* properties in X11R6 polymorphic fonts.           */
  79.  
  80.   static const bdf_property_t  _bdf_properties[] =
  81.   {
  82.     { (char *)"ADD_STYLE_NAME",          BDF_ATOM,     1, { 0 } },
  83.     { (char *)"AVERAGE_WIDTH",           BDF_INTEGER,  1, { 0 } },
  84.     { (char *)"AVG_CAPITAL_WIDTH",       BDF_INTEGER,  1, { 0 } },
  85.     { (char *)"AVG_LOWERCASE_WIDTH",     BDF_INTEGER,  1, { 0 } },
  86.     { (char *)"CAP_HEIGHT",              BDF_INTEGER,  1, { 0 } },
  87.     { (char *)"CHARSET_COLLECTIONS",     BDF_ATOM,     1, { 0 } },
  88.     { (char *)"CHARSET_ENCODING",        BDF_ATOM,     1, { 0 } },
  89.     { (char *)"CHARSET_REGISTRY",        BDF_ATOM,     1, { 0 } },
  90.     { (char *)"COMMENT",                 BDF_ATOM,     1, { 0 } },
  91.     { (char *)"COPYRIGHT",               BDF_ATOM,     1, { 0 } },
  92.     { (char *)"DEFAULT_CHAR",            BDF_CARDINAL, 1, { 0 } },
  93.     { (char *)"DESTINATION",             BDF_CARDINAL, 1, { 0 } },
  94.     { (char *)"DEVICE_FONT_NAME",        BDF_ATOM,     1, { 0 } },
  95.     { (char *)"END_SPACE",               BDF_INTEGER,  1, { 0 } },
  96.     { (char *)"FACE_NAME",               BDF_ATOM,     1, { 0 } },
  97.     { (char *)"FAMILY_NAME",             BDF_ATOM,     1, { 0 } },
  98.     { (char *)"FIGURE_WIDTH",            BDF_INTEGER,  1, { 0 } },
  99.     { (char *)"FONT",                    BDF_ATOM,     1, { 0 } },
  100.     { (char *)"FONTNAME_REGISTRY",       BDF_ATOM,     1, { 0 } },
  101.     { (char *)"FONT_ASCENT",             BDF_INTEGER,  1, { 0 } },
  102.     { (char *)"FONT_DESCENT",            BDF_INTEGER,  1, { 0 } },
  103.     { (char *)"FOUNDRY",                 BDF_ATOM,     1, { 0 } },
  104.     { (char *)"FULL_NAME",               BDF_ATOM,     1, { 0 } },
  105.     { (char *)"ITALIC_ANGLE",            BDF_INTEGER,  1, { 0 } },
  106.     { (char *)"MAX_SPACE",               BDF_INTEGER,  1, { 0 } },
  107.     { (char *)"MIN_SPACE",               BDF_INTEGER,  1, { 0 } },
  108.     { (char *)"NORM_SPACE",              BDF_INTEGER,  1, { 0 } },
  109.     { (char *)"NOTICE",                  BDF_ATOM,     1, { 0 } },
  110.     { (char *)"PIXEL_SIZE",              BDF_INTEGER,  1, { 0 } },
  111.     { (char *)"POINT_SIZE",              BDF_INTEGER,  1, { 0 } },
  112.     { (char *)"QUAD_WIDTH",              BDF_INTEGER,  1, { 0 } },
  113.     { (char *)"RAW_ASCENT",              BDF_INTEGER,  1, { 0 } },
  114.     { (char *)"RAW_AVERAGE_WIDTH",       BDF_INTEGER,  1, { 0 } },
  115.     { (char *)"RAW_AVG_CAPITAL_WIDTH",   BDF_INTEGER,  1, { 0 } },
  116.     { (char *)"RAW_AVG_LOWERCASE_WIDTH", BDF_INTEGER,  1, { 0 } },
  117.     { (char *)"RAW_CAP_HEIGHT",          BDF_INTEGER,  1, { 0 } },
  118.     { (char *)"RAW_DESCENT",             BDF_INTEGER,  1, { 0 } },
  119.     { (char *)"RAW_END_SPACE",           BDF_INTEGER,  1, { 0 } },
  120.     { (char *)"RAW_FIGURE_WIDTH",        BDF_INTEGER,  1, { 0 } },
  121.     { (char *)"RAW_MAX_SPACE",           BDF_INTEGER,  1, { 0 } },
  122.     { (char *)"RAW_MIN_SPACE",           BDF_INTEGER,  1, { 0 } },
  123.     { (char *)"RAW_NORM_SPACE",          BDF_INTEGER,  1, { 0 } },
  124.     { (char *)"RAW_PIXEL_SIZE",          BDF_INTEGER,  1, { 0 } },
  125.     { (char *)"RAW_POINT_SIZE",          BDF_INTEGER,  1, { 0 } },
  126.     { (char *)"RAW_PIXELSIZE",           BDF_INTEGER,  1, { 0 } },
  127.     { (char *)"RAW_POINTSIZE",           BDF_INTEGER,  1, { 0 } },
  128.     { (char *)"RAW_QUAD_WIDTH",          BDF_INTEGER,  1, { 0 } },
  129.     { (char *)"RAW_SMALL_CAP_SIZE",      BDF_INTEGER,  1, { 0 } },
  130.     { (char *)"RAW_STRIKEOUT_ASCENT",    BDF_INTEGER,  1, { 0 } },
  131.     { (char *)"RAW_STRIKEOUT_DESCENT",   BDF_INTEGER,  1, { 0 } },
  132.     { (char *)"RAW_SUBSCRIPT_SIZE",      BDF_INTEGER,  1, { 0 } },
  133.     { (char *)"RAW_SUBSCRIPT_X",         BDF_INTEGER,  1, { 0 } },
  134.     { (char *)"RAW_SUBSCRIPT_Y",         BDF_INTEGER,  1, { 0 } },
  135.     { (char *)"RAW_SUPERSCRIPT_SIZE",    BDF_INTEGER,  1, { 0 } },
  136.     { (char *)"RAW_SUPERSCRIPT_X",       BDF_INTEGER,  1, { 0 } },
  137.     { (char *)"RAW_SUPERSCRIPT_Y",       BDF_INTEGER,  1, { 0 } },
  138.     { (char *)"RAW_UNDERLINE_POSITION",  BDF_INTEGER,  1, { 0 } },
  139.     { (char *)"RAW_UNDERLINE_THICKNESS", BDF_INTEGER,  1, { 0 } },
  140.     { (char *)"RAW_X_HEIGHT",            BDF_INTEGER,  1, { 0 } },
  141.     { (char *)"RELATIVE_SETWIDTH",       BDF_CARDINAL, 1, { 0 } },
  142.     { (char *)"RELATIVE_WEIGHT",         BDF_CARDINAL, 1, { 0 } },
  143.     { (char *)"RESOLUTION",              BDF_INTEGER,  1, { 0 } },
  144.     { (char *)"RESOLUTION_X",            BDF_CARDINAL, 1, { 0 } },
  145.     { (char *)"RESOLUTION_Y",            BDF_CARDINAL, 1, { 0 } },
  146.     { (char *)"SETWIDTH_NAME",           BDF_ATOM,     1, { 0 } },
  147.     { (char *)"SLANT",                   BDF_ATOM,     1, { 0 } },
  148.     { (char *)"SMALL_CAP_SIZE",          BDF_INTEGER,  1, { 0 } },
  149.     { (char *)"SPACING",                 BDF_ATOM,     1, { 0 } },
  150.     { (char *)"STRIKEOUT_ASCENT",        BDF_INTEGER,  1, { 0 } },
  151.     { (char *)"STRIKEOUT_DESCENT",       BDF_INTEGER,  1, { 0 } },
  152.     { (char *)"SUBSCRIPT_SIZE",          BDF_INTEGER,  1, { 0 } },
  153.     { (char *)"SUBSCRIPT_X",             BDF_INTEGER,  1, { 0 } },
  154.     { (char *)"SUBSCRIPT_Y",             BDF_INTEGER,  1, { 0 } },
  155.     { (char *)"SUPERSCRIPT_SIZE",        BDF_INTEGER,  1, { 0 } },
  156.     { (char *)"SUPERSCRIPT_X",           BDF_INTEGER,  1, { 0 } },
  157.     { (char *)"SUPERSCRIPT_Y",           BDF_INTEGER,  1, { 0 } },
  158.     { (char *)"UNDERLINE_POSITION",      BDF_INTEGER,  1, { 0 } },
  159.     { (char *)"UNDERLINE_THICKNESS",     BDF_INTEGER,  1, { 0 } },
  160.     { (char *)"WEIGHT",                  BDF_CARDINAL, 1, { 0 } },
  161.     { (char *)"WEIGHT_NAME",             BDF_ATOM,     1, { 0 } },
  162.     { (char *)"X_HEIGHT",                BDF_INTEGER,  1, { 0 } },
  163.     { (char *)"_MULE_BASELINE_OFFSET",   BDF_INTEGER,  1, { 0 } },
  164.     { (char *)"_MULE_RELATIVE_COMPOSE",  BDF_INTEGER,  1, { 0 } },
  165.   };
  166.  
  167.   static const unsigned long
  168.   _num_bdf_properties = sizeof ( _bdf_properties ) /
  169.                         sizeof ( _bdf_properties[0] );
  170.  
  171.  
  172.   /* Auto correction messages. */
  173. #define ACMSG1   "FONT_ASCENT property missing.  " \
  174.                  "Added `FONT_ASCENT %hd'.\n"
  175. #define ACMSG2   "FONT_DESCENT property missing.  " \
  176.                  "Added `FONT_DESCENT %hd'.\n"
  177. #define ACMSG3   "Font width != actual width.  Old: %hd New: %hd.\n"
  178. #define ACMSG4   "Font left bearing != actual left bearing.  " \
  179.                  "Old: %hd New: %hd.\n"
  180. #define ACMSG5   "Font ascent != actual ascent.  Old: %hd New: %hd.\n"
  181. #define ACMSG6   "Font descent != actual descent.  Old: %hd New: %hd.\n"
  182. #define ACMSG7   "Font height != actual height. Old: %hd New: %hd.\n"
  183. #define ACMSG8   "Glyph scalable width (SWIDTH) adjustments made.\n"
  184. #define ACMSG9   "SWIDTH field missing at line %ld.  Set automatically.\n"
  185. #define ACMSG10  "DWIDTH field missing at line %ld.  Set to glyph width.\n"
  186. #define ACMSG11  "SIZE bits per pixel field adjusted to %hd.\n"
  187. #define ACMSG12  "Duplicate encoding %ld (%s) changed to unencoded.\n"
  188. #define ACMSG13  "Glyph %ld extra rows removed.\n"
  189. #define ACMSG14  "Glyph %ld extra columns removed.\n"
  190. #define ACMSG15  "Incorrect glyph count: %ld indicated but %ld found.\n"
  191. #define ACMSG16  "Glyph %ld missing columns padded with zero bits.\n"
  192.  
  193.   /* Error messages. */
  194. #define ERRMSG1  "[line %ld] Missing `%s' line.\n"
  195. #define ERRMSG2  "[line %ld] Font header corrupted or missing fields.\n"
  196. #define ERRMSG3  "[line %ld] Font glyphs corrupted or missing fields.\n"
  197. #define ERRMSG4  "[line %ld] BBX too big.\n"
  198. #define ERRMSG5  "[line %ld] `%s' value too big.\n"
  199. #define ERRMSG6  "[line %ld] Input line too long.\n"
  200. #define ERRMSG7  "[line %ld] Font name too long.\n"
  201. #define ERRMSG8  "[line %ld] Invalid `%s' value.\n"
  202. #define ERRMSG9  "[line %ld] Invalid keyword.\n"
  203.  
  204.   /* Debug messages. */
  205. #define DBGMSG1  "  [%6ld] %s" /* no \n */
  206. #define DBGMSG2  " (0x%lX)\n"
  207.  
  208.  
  209.   /*************************************************************************/
  210.   /*                                                                       */
  211.   /* Hash table utilities for the properties.                              */
  212.   /*                                                                       */
  213.   /*************************************************************************/
  214.  
  215.   /* XXX: Replace this with FreeType's hash functions */
  216.  
  217.  
  218. #define INITIAL_HT_SIZE  241
  219.  
  220.   typedef void
  221.   (*hash_free_func)( hashnode  node );
  222.  
  223.   static hashnode*
  224.   hash_bucket( const char*  key,
  225.                hashtable*   ht )
  226.   {
  227.     const char*    kp  = key;
  228.     unsigned long  res = 0;
  229.     hashnode*      bp  = ht->table, *ndp;
  230.  
  231.  
  232.     /* Mocklisp hash function. */
  233.     while ( *kp )
  234.       res = ( res << 5 ) - res + *kp++;
  235.  
  236.     ndp = bp + ( res % ht->size );
  237.     while ( *ndp )
  238.     {
  239.       kp = (*ndp)->key;
  240.       if ( kp[0] == key[0] && ft_strcmp( kp, key ) == 0 )
  241.         break;
  242.       ndp--;
  243.       if ( ndp < bp )
  244.         ndp = bp + ( ht->size - 1 );
  245.     }
  246.  
  247.     return ndp;
  248.   }
  249.  
  250.  
  251.   static FT_Error
  252.   hash_rehash( hashtable*  ht,
  253.                FT_Memory   memory )
  254.   {
  255.     hashnode*  obp = ht->table, *bp, *nbp;
  256.     int        i, sz = ht->size;
  257.     FT_Error   error = FT_Err_Ok;
  258.  
  259.  
  260.     ht->size <<= 1;
  261.     ht->limit  = ht->size / 3;
  262.  
  263.     if ( FT_NEW_ARRAY( ht->table, ht->size ) )
  264.       goto Exit;
  265.  
  266.     for ( i = 0, bp = obp; i < sz; i++, bp++ )
  267.     {
  268.       if ( *bp )
  269.       {
  270.         nbp = hash_bucket( (*bp)->key, ht );
  271.         *nbp = *bp;
  272.       }
  273.     }
  274.     FT_FREE( obp );
  275.  
  276.   Exit:
  277.     return error;
  278.   }
  279.  
  280.  
  281.   static FT_Error
  282.   hash_init( hashtable*  ht,
  283.              FT_Memory   memory )
  284.   {
  285.     int       sz    = INITIAL_HT_SIZE;
  286.     FT_Error  error = FT_Err_Ok;
  287.  
  288.  
  289.     ht->size  = sz;
  290.     ht->limit = sz / 3;
  291.     ht->used  = 0;
  292.  
  293.     if ( FT_NEW_ARRAY( ht->table, sz ) )
  294.       goto Exit;
  295.  
  296.   Exit:
  297.     return error;
  298.   }
  299.  
  300.  
  301.   static void
  302.   hash_free( hashtable*  ht,
  303.              FT_Memory   memory )
  304.   {
  305.     if ( ht != 0 )
  306.     {
  307.       int        i, sz = ht->size;
  308.       hashnode*  bp = ht->table;
  309.  
  310.  
  311.       for ( i = 0; i < sz; i++, bp++ )
  312.         FT_FREE( *bp );
  313.  
  314.       FT_FREE( ht->table );
  315.     }
  316.   }
  317.  
  318.  
  319.   static FT_Error
  320.   hash_insert( char*       key,
  321.                size_t      data,
  322.                hashtable*  ht,
  323.                FT_Memory   memory )
  324.   {
  325.     hashnode   nn;
  326.     hashnode*  bp      = hash_bucket( key, ht );
  327.     FT_Error   error   = FT_Err_Ok;
  328.  
  329.  
  330.     nn = *bp;
  331.     if ( !nn )
  332.     {
  333.       if ( FT_NEW( nn ) )
  334.         goto Exit;
  335.       *bp = nn;
  336.  
  337.       nn->key  = key;
  338.       nn->data = data;
  339.  
  340.       if ( ht->used >= ht->limit )
  341.       {
  342.         error = hash_rehash( ht, memory );
  343.         if ( error )
  344.           goto Exit;
  345.       }
  346.       ht->used++;
  347.     }
  348.     else
  349.       nn->data = data;
  350.  
  351.   Exit:
  352.     return error;
  353.   }
  354.  
  355.  
  356.   static hashnode
  357.   hash_lookup( const char* key,
  358.                hashtable*  ht )
  359.   {
  360.     hashnode *np = hash_bucket( key, ht );
  361.  
  362.  
  363.     return *np;
  364.   }
  365.  
  366.  
  367.   /*************************************************************************/
  368.   /*                                                                       */
  369.   /* Utility types and functions.                                          */
  370.   /*                                                                       */
  371.   /*************************************************************************/
  372.  
  373.  
  374.   /* Function type for parsing lines of a BDF font. */
  375.  
  376.   typedef FT_Error
  377.   (*_bdf_line_func_t)( char*          line,
  378.                        unsigned long  linelen,
  379.                        unsigned long  lineno,
  380.                        void*          call_data,
  381.                        void*          client_data );
  382.  
  383.  
  384.   /* List structure for splitting lines into fields. */
  385.  
  386.   typedef struct  _bdf_list_t_
  387.   {
  388.     char**         field;
  389.     unsigned long  size;
  390.     unsigned long  used;
  391.     FT_Memory      memory;
  392.  
  393.   } _bdf_list_t;
  394.  
  395.  
  396.   /* Structure used while loading BDF fonts. */
  397.  
  398.   typedef struct  _bdf_parse_t_
  399.   {
  400.     unsigned long   flags;
  401.     unsigned long   cnt;
  402.     unsigned long   row;
  403.  
  404.     short           minlb;
  405.     short           maxlb;
  406.     short           maxrb;
  407.     short           maxas;
  408.     short           maxds;
  409.  
  410.     short           rbearing;
  411.  
  412.     char*           glyph_name;
  413.     long            glyph_enc;
  414.  
  415.     bdf_font_t*     font;
  416.     bdf_options_t*  opts;
  417.  
  418.     unsigned long   have[34816]; /* must be in sync with `nmod' and `umod' */
  419.                                  /* arrays from `bdf_font_t' structure     */
  420.     _bdf_list_t     list;
  421.  
  422.     FT_Memory       memory;
  423.  
  424.   } _bdf_parse_t;
  425.  
  426.  
  427. #define setsbit( m, cc ) \
  428.           ( m[(FT_Byte)(cc) >> 3] |= (FT_Byte)( 1 << ( (cc) & 7 ) ) )
  429. #define sbitset( m, cc ) \
  430.           ( m[(FT_Byte)(cc) >> 3]  & ( 1 << ( (cc) & 7 ) ) )
  431.  
  432.  
  433.   static void
  434.   _bdf_list_init( _bdf_list_t*  list,
  435.                   FT_Memory     memory )
  436.   {
  437.     FT_ZERO( list );
  438.     list->memory = memory;
  439.   }
  440.  
  441.  
  442.   static void
  443.   _bdf_list_done( _bdf_list_t*  list )
  444.   {
  445.     FT_Memory  memory = list->memory;
  446.  
  447.  
  448.     if ( memory )
  449.     {
  450.       FT_FREE( list->field );
  451.       FT_ZERO( list );
  452.     }
  453.   }
  454.  
  455.  
  456.   static FT_Error
  457.   _bdf_list_ensure( _bdf_list_t*   list,
  458.                     unsigned long  num_items ) /* same as _bdf_list_t.used */
  459.   {
  460.     FT_Error  error = FT_Err_Ok;
  461.  
  462.  
  463.     if ( num_items > list->size )
  464.     {
  465.       unsigned long  oldsize = list->size; /* same as _bdf_list_t.size */
  466.       unsigned long  newsize = oldsize + ( oldsize >> 1 ) + 5;
  467.       unsigned long  bigsize = (unsigned long)( FT_INT_MAX / sizeof ( char* ) );
  468.       FT_Memory      memory  = list->memory;
  469.  
  470.  
  471.       if ( oldsize == bigsize )
  472.       {
  473.         error = FT_THROW( Out_Of_Memory );
  474.         goto Exit;
  475.       }
  476.       else if ( newsize < oldsize || newsize > bigsize )
  477.         newsize = bigsize;
  478.  
  479.       if ( FT_RENEW_ARRAY( list->field, oldsize, newsize ) )
  480.         goto Exit;
  481.  
  482.       list->size = newsize;
  483.     }
  484.  
  485.   Exit:
  486.     return error;
  487.   }
  488.  
  489.  
  490.   static void
  491.   _bdf_list_shift( _bdf_list_t*   list,
  492.                    unsigned long  n )
  493.   {
  494.     unsigned long  i, u;
  495.  
  496.  
  497.     if ( list == 0 || list->used == 0 || n == 0 )
  498.       return;
  499.  
  500.     if ( n >= list->used )
  501.     {
  502.       list->used = 0;
  503.       return;
  504.     }
  505.  
  506.     for ( u = n, i = 0; u < list->used; i++, u++ )
  507.       list->field[i] = list->field[u];
  508.     list->used -= n;
  509.   }
  510.  
  511.  
  512.   /* An empty string for empty fields. */
  513.  
  514.   static const char  empty[1] = { 0 };      /* XXX eliminate this */
  515.  
  516.  
  517.   static char *
  518.   _bdf_list_join( _bdf_list_t*    list,
  519.                   int             c,
  520.                   unsigned long  *alen )
  521.   {
  522.     unsigned long  i, j;
  523.     char*          dp;
  524.  
  525.  
  526.     *alen = 0;
  527.  
  528.     if ( list == 0 || list->used == 0 )
  529.       return 0;
  530.  
  531.     dp = list->field[0];
  532.     for ( i = j = 0; i < list->used; i++ )
  533.     {
  534.       char*  fp = list->field[i];
  535.  
  536.  
  537.       while ( *fp )
  538.         dp[j++] = *fp++;
  539.  
  540.       if ( i + 1 < list->used )
  541.         dp[j++] = (char)c;
  542.     }
  543.     if ( dp != empty )
  544.       dp[j] = 0;
  545.  
  546.     *alen = j;
  547.     return dp;
  548.   }
  549.  
  550.  
  551.   /* The code below ensures that we have at least 4 + 1 `field' */
  552.   /* elements in `list' (which are possibly NULL) so that we    */
  553.   /* don't have to check the number of fields in most cases.    */
  554.  
  555.   static FT_Error
  556.   _bdf_list_split( _bdf_list_t*   list,
  557.                    char*          separators,
  558.                    char*          line,
  559.                    unsigned long  linelen )
  560.   {
  561.     int       mult, final_empty;
  562.     char      *sp, *ep, *end;
  563.     char      seps[32];
  564.     FT_Error  error = FT_Err_Ok;
  565.  
  566.  
  567.     /* Initialize the list. */
  568.     list->used = 0;
  569.     if ( list->size )
  570.     {
  571.       list->field[0] = (char*)empty;
  572.       list->field[1] = (char*)empty;
  573.       list->field[2] = (char*)empty;
  574.       list->field[3] = (char*)empty;
  575.       list->field[4] = (char*)empty;
  576.     }
  577.  
  578.     /* If the line is empty, then simply return. */
  579.     if ( linelen == 0 || line[0] == 0 )
  580.       goto Exit;
  581.  
  582.     /* In the original code, if the `separators' parameter is NULL or */
  583.     /* empty, the list is split into individual bytes.  We don't need */
  584.     /* this, so an error is signaled.                                 */
  585.     if ( separators == 0 || *separators == 0 )
  586.     {
  587.       error = FT_THROW( Invalid_Argument );
  588.       goto Exit;
  589.     }
  590.  
  591.     /* Prepare the separator bitmap. */
  592.     FT_MEM_ZERO( seps, 32 );
  593.  
  594.     /* If the very last character of the separator string is a plus, then */
  595.     /* set the `mult' flag to indicate that multiple separators should be */
  596.     /* collapsed into one.                                                */
  597.     for ( mult = 0, sp = separators; sp && *sp; sp++ )
  598.     {
  599.       if ( *sp == '+' && *( sp + 1 ) == 0 )
  600.         mult = 1;
  601.       else
  602.         setsbit( seps, *sp );
  603.     }
  604.  
  605.     /* Break the line up into fields. */
  606.     for ( final_empty = 0, sp = ep = line, end = sp + linelen;
  607.           sp < end && *sp; )
  608.     {
  609.       /* Collect everything that is not a separator. */
  610.       for ( ; *ep && !sbitset( seps, *ep ); ep++ )
  611.         ;
  612.  
  613.       /* Resize the list if necessary. */
  614.       if ( list->used == list->size )
  615.       {
  616.         error = _bdf_list_ensure( list, list->used + 1 );
  617.         if ( error )
  618.           goto Exit;
  619.       }
  620.  
  621.       /* Assign the field appropriately. */
  622.       list->field[list->used++] = ( ep > sp ) ? sp : (char*)empty;
  623.  
  624.       sp = ep;
  625.  
  626.       if ( mult )
  627.       {
  628.         /* If multiple separators should be collapsed, do it now by */
  629.         /* setting all the separator characters to 0.               */
  630.         for ( ; *ep && sbitset( seps, *ep ); ep++ )
  631.           *ep = 0;
  632.       }
  633.       else if ( *ep != 0 )
  634.         /* Don't collapse multiple separators by making them 0, so just */
  635.         /* make the one encountered 0.                                  */
  636.         *ep++ = 0;
  637.  
  638.       final_empty = ( ep > sp && *ep == 0 );
  639.       sp = ep;
  640.     }
  641.  
  642.     /* Finally, NULL-terminate the list. */
  643.     if ( list->used + final_empty >= list->size )
  644.     {
  645.       error = _bdf_list_ensure( list, list->used + final_empty + 1 );
  646.       if ( error )
  647.         goto Exit;
  648.     }
  649.  
  650.     if ( final_empty )
  651.       list->field[list->used++] = (char*)empty;
  652.  
  653.     list->field[list->used] = 0;
  654.  
  655.   Exit:
  656.     return error;
  657.   }
  658.  
  659.  
  660. #define NO_SKIP  256  /* this value cannot be stored in a 'char' */
  661.  
  662.  
  663.   static FT_Error
  664.   _bdf_readstream( FT_Stream         stream,
  665.                    _bdf_line_func_t  callback,
  666.                    void*             client_data,
  667.                    unsigned long    *lno )
  668.   {
  669.     _bdf_line_func_t  cb;
  670.     unsigned long     lineno, buf_size;
  671.     int               refill, hold, to_skip;
  672.     ptrdiff_t         bytes, start, end, cursor, avail;
  673.     char*             buf    = 0;
  674.     FT_Memory         memory = stream->memory;
  675.     FT_Error          error  = FT_Err_Ok;
  676.  
  677.  
  678.     if ( callback == 0 )
  679.     {
  680.       error = FT_THROW( Invalid_Argument );
  681.       goto Exit;
  682.     }
  683.  
  684.     /* initial size and allocation of the input buffer */
  685.     buf_size = 1024;
  686.  
  687.     if ( FT_NEW_ARRAY( buf, buf_size ) )
  688.       goto Exit;
  689.  
  690.     cb      = callback;
  691.     lineno  = 1;
  692.     buf[0]  = 0;
  693.     start   = 0;
  694.     end     = 0;
  695.     avail   = 0;
  696.     cursor  = 0;
  697.     refill  = 1;
  698.     to_skip = NO_SKIP;
  699.     bytes   = 0;        /* make compiler happy */
  700.  
  701.     for (;;)
  702.     {
  703.       if ( refill )
  704.       {
  705.         bytes  = (ptrdiff_t)FT_Stream_TryRead(
  706.                    stream, (FT_Byte*)buf + cursor,
  707.                    (FT_ULong)( buf_size - cursor ) );
  708.         avail  = cursor + bytes;
  709.         cursor = 0;
  710.         refill = 0;
  711.       }
  712.  
  713.       end = start;
  714.  
  715.       /* should we skip an optional character like \n or \r? */
  716.       if ( start < avail && buf[start] == to_skip )
  717.       {
  718.         start  += 1;
  719.         to_skip = NO_SKIP;
  720.         continue;
  721.       }
  722.  
  723.       /* try to find the end of the line */
  724.       while ( end < avail && buf[end] != '\n' && buf[end] != '\r' )
  725.         end++;
  726.  
  727.       /* if we hit the end of the buffer, try shifting its content */
  728.       /* or even resizing it                                       */
  729.       if ( end >= avail )
  730.       {
  731.         if ( bytes == 0 )  /* last line in file doesn't end in \r or \n */
  732.           break;           /* ignore it then exit                       */
  733.  
  734.         if ( start == 0 )
  735.         {
  736.           /* this line is definitely too long; try resizing the input */
  737.           /* buffer a bit to handle it.                               */
  738.           FT_ULong  new_size;
  739.  
  740.  
  741.           if ( buf_size >= 65536UL )  /* limit ourselves to 64KByte */
  742.           {
  743.             FT_ERROR(( "_bdf_readstream: " ERRMSG6, lineno ));
  744.             error = FT_THROW( Invalid_Argument );
  745.             goto Exit;
  746.           }
  747.  
  748.           new_size = buf_size * 2;
  749.           if ( FT_RENEW_ARRAY( buf, buf_size, new_size ) )
  750.             goto Exit;
  751.  
  752.           cursor   = buf_size;
  753.           buf_size = new_size;
  754.         }
  755.         else
  756.         {
  757.           bytes = avail - start;
  758.  
  759.           FT_MEM_MOVE( buf, buf + start, bytes );
  760.  
  761.           cursor = bytes;
  762.           avail -= bytes;
  763.           start  = 0;
  764.         }
  765.         refill = 1;
  766.         continue;
  767.       }
  768.  
  769.       /* Temporarily NUL-terminate the line. */
  770.       hold     = buf[end];
  771.       buf[end] = 0;
  772.  
  773.       /* XXX: Use encoding independent value for 0x1a */
  774.       if ( buf[start] != '#' && buf[start] != 0x1a && end > start )
  775.       {
  776.         error = (*cb)( buf + start, (unsigned long)( end - start ), lineno,
  777.                        (void*)&cb, client_data );
  778.         /* Redo if we have encountered CHARS without properties. */
  779.         if ( error == -1 )
  780.           error = (*cb)( buf + start, (unsigned long)( end - start ), lineno,
  781.                          (void*)&cb, client_data );
  782.         if ( error )
  783.           break;
  784.       }
  785.  
  786.       lineno  += 1;
  787.       buf[end] = (char)hold;
  788.       start    = end + 1;
  789.  
  790.       if ( hold == '\n' )
  791.         to_skip = '\r';
  792.       else if ( hold == '\r' )
  793.         to_skip = '\n';
  794.       else
  795.         to_skip = NO_SKIP;
  796.     }
  797.  
  798.     *lno = lineno;
  799.  
  800.   Exit:
  801.     FT_FREE( buf );
  802.     return error;
  803.   }
  804.  
  805.  
  806.   /* XXX: make this work with EBCDIC also */
  807.  
  808.   static const unsigned char  a2i[128] =
  809.   {
  810.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  811.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  812.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  813.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  814.     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x00, 0x00,
  815.     0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00,
  816.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  817.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  818.     0x00, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x00, 0x00, 0x00, 0x00, 0x00,
  819.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  820.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  821.   };
  822.  
  823.   static const unsigned char  odigits[32] =
  824.   {
  825.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x00,
  826.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  827.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  828.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  829.   };
  830.  
  831.   static const unsigned char  ddigits[32] =
  832.   {
  833.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
  834.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  835.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  836.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  837.   };
  838.  
  839.   static const unsigned char  hdigits[32] =
  840.   {
  841.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0x03,
  842.     0x7e, 0x00, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00,
  843.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  844.     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  845.   };
  846.  
  847.  
  848.   /* Routine to convert an ASCII string into an unsigned long integer. */
  849.   static unsigned long
  850.   _bdf_atoul( char*   s,
  851.               char**  end,
  852.               int     base )
  853.   {
  854.     unsigned long         v;
  855.     const unsigned char*  dmap;
  856.  
  857.  
  858.     if ( s == 0 || *s == 0 )
  859.       return 0;
  860.  
  861.     /* Make sure the radix is something recognizable.  Default to 10. */
  862.     switch ( base )
  863.     {
  864.     case 8:
  865.       dmap = odigits;
  866.       break;
  867.     case 16:
  868.       dmap = hdigits;
  869.       break;
  870.     default:
  871.       base = 10;
  872.       dmap = ddigits;
  873.       break;
  874.     }
  875.  
  876.     /* Check for the special hex prefix. */
  877.     if ( *s == '0'                                  &&
  878.          ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
  879.     {
  880.       base = 16;
  881.       dmap = hdigits;
  882.       s   += 2;
  883.     }
  884.  
  885.     for ( v = 0; sbitset( dmap, *s ); s++ )
  886.       v = v * base + a2i[(int)*s];
  887.  
  888.     if ( end != 0 )
  889.       *end = s;
  890.  
  891.     return v;
  892.   }
  893.  
  894.  
  895.   /* Routine to convert an ASCII string into an signed long integer. */
  896.   static long
  897.   _bdf_atol( char*   s,
  898.              char**  end,
  899.              int     base )
  900.   {
  901.     long                  v, neg;
  902.     const unsigned char*  dmap;
  903.  
  904.  
  905.     if ( s == 0 || *s == 0 )
  906.       return 0;
  907.  
  908.     /* Make sure the radix is something recognizable.  Default to 10. */
  909.     switch ( base )
  910.     {
  911.     case 8:
  912.       dmap = odigits;
  913.       break;
  914.     case 16:
  915.       dmap = hdigits;
  916.       break;
  917.     default:
  918.       base = 10;
  919.       dmap = ddigits;
  920.       break;
  921.     }
  922.  
  923.     /* Check for a minus sign. */
  924.     neg = 0;
  925.     if ( *s == '-' )
  926.     {
  927.       s++;
  928.       neg = 1;
  929.     }
  930.  
  931.     /* Check for the special hex prefix. */
  932.     if ( *s == '0'                                  &&
  933.          ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
  934.     {
  935.       base = 16;
  936.       dmap = hdigits;
  937.       s   += 2;
  938.     }
  939.  
  940.     for ( v = 0; sbitset( dmap, *s ); s++ )
  941.       v = v * base + a2i[(int)*s];
  942.  
  943.     if ( end != 0 )
  944.       *end = s;
  945.  
  946.     return ( !neg ) ? v : -v;
  947.   }
  948.  
  949.  
  950.   /* Routine to convert an ASCII string into an signed short integer. */
  951.   static short
  952.   _bdf_atos( char*   s,
  953.              char**  end,
  954.              int     base )
  955.   {
  956.     short                 v, neg;
  957.     const unsigned char*  dmap;
  958.  
  959.  
  960.     if ( s == 0 || *s == 0 )
  961.       return 0;
  962.  
  963.     /* Make sure the radix is something recognizable.  Default to 10. */
  964.     switch ( base )
  965.     {
  966.     case 8:
  967.       dmap = odigits;
  968.       break;
  969.     case 16:
  970.       dmap = hdigits;
  971.       break;
  972.     default:
  973.       base = 10;
  974.       dmap = ddigits;
  975.       break;
  976.     }
  977.  
  978.     /* Check for a minus. */
  979.     neg = 0;
  980.     if ( *s == '-' )
  981.     {
  982.       s++;
  983.       neg = 1;
  984.     }
  985.  
  986.     /* Check for the special hex prefix. */
  987.     if ( *s == '0'                                  &&
  988.          ( *( s + 1 ) == 'x' || *( s + 1 ) == 'X' ) )
  989.     {
  990.       base = 16;
  991.       dmap = hdigits;
  992.       s   += 2;
  993.     }
  994.  
  995.     for ( v = 0; sbitset( dmap, *s ); s++ )
  996.       v = (short)( v * base + a2i[(int)*s] );
  997.  
  998.     if ( end != 0 )
  999.       *end = s;
  1000.  
  1001.     return (short)( ( !neg ) ? v : -v );
  1002.   }
  1003.  
  1004.  
  1005.   /* Routine to compare two glyphs by encoding so they can be sorted. */
  1006.   static int
  1007.   by_encoding( const void*  a,
  1008.                const void*  b )
  1009.   {
  1010.     bdf_glyph_t  *c1, *c2;
  1011.  
  1012.  
  1013.     c1 = (bdf_glyph_t *)a;
  1014.     c2 = (bdf_glyph_t *)b;
  1015.  
  1016.     if ( c1->encoding < c2->encoding )
  1017.       return -1;
  1018.  
  1019.     if ( c1->encoding > c2->encoding )
  1020.       return 1;
  1021.  
  1022.     return 0;
  1023.   }
  1024.  
  1025.  
  1026.   static FT_Error
  1027.   bdf_create_property( char*        name,
  1028.                        int          format,
  1029.                        bdf_font_t*  font )
  1030.   {
  1031.     size_t           n;
  1032.     bdf_property_t*  p;
  1033.     FT_Memory        memory = font->memory;
  1034.     FT_Error         error  = FT_Err_Ok;
  1035.  
  1036.  
  1037.     /* First check whether the property has        */
  1038.     /* already been added or not.  If it has, then */
  1039.     /* simply ignore it.                           */
  1040.     if ( hash_lookup( name, &(font->proptbl) ) )
  1041.       goto Exit;
  1042.  
  1043.     if ( FT_RENEW_ARRAY( font->user_props,
  1044.                          font->nuser_props,
  1045.                          font->nuser_props + 1 ) )
  1046.       goto Exit;
  1047.  
  1048.     p = font->user_props + font->nuser_props;
  1049.     FT_ZERO( p );
  1050.  
  1051.     n = ft_strlen( name ) + 1;
  1052.     if ( n > FT_ULONG_MAX )
  1053.       return FT_THROW( Invalid_Argument );
  1054.  
  1055.     if ( FT_NEW_ARRAY( p->name, n ) )
  1056.       goto Exit;
  1057.  
  1058.     FT_MEM_COPY( (char *)p->name, name, n );
  1059.  
  1060.     p->format  = format;
  1061.     p->builtin = 0;
  1062.  
  1063.     n = _num_bdf_properties + font->nuser_props;
  1064.  
  1065.     error = hash_insert( p->name, n, &(font->proptbl), memory );
  1066.     if ( error )
  1067.       goto Exit;
  1068.  
  1069.     font->nuser_props++;
  1070.  
  1071.   Exit:
  1072.     return error;
  1073.   }
  1074.  
  1075.  
  1076.   FT_LOCAL_DEF( bdf_property_t * )
  1077.   bdf_get_property( char*        name,
  1078.                     bdf_font_t*  font )
  1079.   {
  1080.     hashnode  hn;
  1081.     size_t    propid;
  1082.  
  1083.  
  1084.     if ( name == 0 || *name == 0 )
  1085.       return 0;
  1086.  
  1087.     if ( ( hn = hash_lookup( name, &(font->proptbl) ) ) == 0 )
  1088.       return 0;
  1089.  
  1090.     propid = hn->data;
  1091.     if ( propid >= _num_bdf_properties )
  1092.       return font->user_props + ( propid - _num_bdf_properties );
  1093.  
  1094.     return (bdf_property_t*)_bdf_properties + propid;
  1095.   }
  1096.  
  1097.  
  1098.   /*************************************************************************/
  1099.   /*                                                                       */
  1100.   /* BDF font file parsing flags and functions.                            */
  1101.   /*                                                                       */
  1102.   /*************************************************************************/
  1103.  
  1104.  
  1105.   /* Parse flags. */
  1106.  
  1107. #define _BDF_START      0x0001
  1108. #define _BDF_FONT_NAME  0x0002
  1109. #define _BDF_SIZE       0x0004
  1110. #define _BDF_FONT_BBX   0x0008
  1111. #define _BDF_PROPS      0x0010
  1112. #define _BDF_GLYPHS     0x0020
  1113. #define _BDF_GLYPH      0x0040
  1114. #define _BDF_ENCODING   0x0080
  1115. #define _BDF_SWIDTH     0x0100
  1116. #define _BDF_DWIDTH     0x0200
  1117. #define _BDF_BBX        0x0400
  1118. #define _BDF_BITMAP     0x0800
  1119.  
  1120. #define _BDF_SWIDTH_ADJ  0x1000
  1121.  
  1122. #define _BDF_GLYPH_BITS ( _BDF_GLYPH    | \
  1123.                           _BDF_ENCODING | \
  1124.                           _BDF_SWIDTH   | \
  1125.                           _BDF_DWIDTH   | \
  1126.                           _BDF_BBX      | \
  1127.                           _BDF_BITMAP   )
  1128.  
  1129. #define _BDF_GLYPH_WIDTH_CHECK   0x40000000UL
  1130. #define _BDF_GLYPH_HEIGHT_CHECK  0x80000000UL
  1131.  
  1132.  
  1133.   static FT_Error
  1134.   _bdf_add_comment( bdf_font_t*    font,
  1135.                     char*          comment,
  1136.                     unsigned long  len )
  1137.   {
  1138.     char*      cp;
  1139.     FT_Memory  memory = font->memory;
  1140.     FT_Error   error  = FT_Err_Ok;
  1141.  
  1142.  
  1143.     if ( FT_RENEW_ARRAY( font->comments,
  1144.                          font->comments_len,
  1145.                          font->comments_len + len + 1 ) )
  1146.       goto Exit;
  1147.  
  1148.     cp = font->comments + font->comments_len;
  1149.  
  1150.     FT_MEM_COPY( cp, comment, len );
  1151.     cp[len] = '\n';
  1152.  
  1153.     font->comments_len += len + 1;
  1154.  
  1155.   Exit:
  1156.     return error;
  1157.   }
  1158.  
  1159.  
  1160.   /* Set the spacing from the font name if it exists, or set it to the */
  1161.   /* default specified in the options.                                 */
  1162.   static FT_Error
  1163.   _bdf_set_default_spacing( bdf_font_t*     font,
  1164.                             bdf_options_t*  opts,
  1165.                             unsigned long   lineno )
  1166.   {
  1167.     size_t       len;
  1168.     char         name[256];
  1169.     _bdf_list_t  list;
  1170.     FT_Memory    memory;
  1171.     FT_Error     error = FT_Err_Ok;
  1172.  
  1173.     FT_UNUSED( lineno );        /* only used in debug mode */
  1174.  
  1175.  
  1176.     if ( font == 0 || font->name == 0 || font->name[0] == 0 )
  1177.     {
  1178.       error = FT_THROW( Invalid_Argument );
  1179.       goto Exit;
  1180.     }
  1181.  
  1182.     memory = font->memory;
  1183.  
  1184.     _bdf_list_init( &list, memory );
  1185.  
  1186.     font->spacing = opts->font_spacing;
  1187.  
  1188.     len = ft_strlen( font->name ) + 1;
  1189.     /* Limit ourselves to 256 characters in the font name. */
  1190.     if ( len >= 256 )
  1191.     {
  1192.       FT_ERROR(( "_bdf_set_default_spacing: " ERRMSG7, lineno ));
  1193.       error = FT_THROW( Invalid_Argument );
  1194.       goto Exit;
  1195.     }
  1196.  
  1197.     FT_MEM_COPY( name, font->name, len );
  1198.  
  1199.     error = _bdf_list_split( &list, (char *)"-", name, (unsigned long)len );
  1200.     if ( error )
  1201.       goto Fail;
  1202.  
  1203.     if ( list.used == 15 )
  1204.     {
  1205.       switch ( list.field[11][0] )
  1206.       {
  1207.       case 'C':
  1208.       case 'c':
  1209.         font->spacing = BDF_CHARCELL;
  1210.         break;
  1211.       case 'M':
  1212.       case 'm':
  1213.         font->spacing = BDF_MONOWIDTH;
  1214.         break;
  1215.       case 'P':
  1216.       case 'p':
  1217.         font->spacing = BDF_PROPORTIONAL;
  1218.         break;
  1219.       }
  1220.     }
  1221.  
  1222.   Fail:
  1223.     _bdf_list_done( &list );
  1224.  
  1225.   Exit:
  1226.     return error;
  1227.   }
  1228.  
  1229.  
  1230.   /* Determine whether the property is an atom or not.  If it is, then */
  1231.   /* clean it up so the double quotes are removed if they exist.       */
  1232.   static int
  1233.   _bdf_is_atom( char*          line,
  1234.                 unsigned long  linelen,
  1235.                 char**         name,
  1236.                 char**         value,
  1237.                 bdf_font_t*    font )
  1238.   {
  1239.     int              hold;
  1240.     char             *sp, *ep;
  1241.     bdf_property_t*  p;
  1242.  
  1243.  
  1244.     *name = sp = ep = line;
  1245.  
  1246.     while ( *ep && *ep != ' ' && *ep != '\t' )
  1247.       ep++;
  1248.  
  1249.     hold = -1;
  1250.     if ( *ep )
  1251.     {
  1252.       hold = *ep;
  1253.       *ep  = 0;
  1254.     }
  1255.  
  1256.     p = bdf_get_property( sp, font );
  1257.  
  1258.     /* Restore the character that was saved before any return can happen. */
  1259.     if ( hold != -1 )
  1260.       *ep = (char)hold;
  1261.  
  1262.     /* If the property exists and is not an atom, just return here. */
  1263.     if ( p && p->format != BDF_ATOM )
  1264.       return 0;
  1265.  
  1266.     /* The property is an atom.  Trim all leading and trailing whitespace */
  1267.     /* and double quotes for the atom value.                              */
  1268.     sp = ep;
  1269.     ep = line + linelen;
  1270.  
  1271.     /* Trim the leading whitespace if it exists. */
  1272.     if ( *sp )
  1273.       *sp++ = 0;
  1274.     while ( *sp                           &&
  1275.             ( *sp == ' ' || *sp == '\t' ) )
  1276.       sp++;
  1277.  
  1278.     /* Trim the leading double quote if it exists. */
  1279.     if ( *sp == '"' )
  1280.       sp++;
  1281.     *value = sp;
  1282.  
  1283.     /* Trim the trailing whitespace if it exists. */
  1284.     while ( ep > sp                                       &&
  1285.             ( *( ep - 1 ) == ' ' || *( ep - 1 ) == '\t' ) )
  1286.       *--ep = 0;
  1287.  
  1288.     /* Trim the trailing double quote if it exists. */
  1289.     if ( ep > sp && *( ep - 1 ) == '"' )
  1290.       *--ep = 0;
  1291.  
  1292.     return 1;
  1293.   }
  1294.  
  1295.  
  1296.   static FT_Error
  1297.   _bdf_add_property( bdf_font_t*    font,
  1298.                      char*          name,
  1299.                      char*          value,
  1300.                      unsigned long  lineno )
  1301.   {
  1302.     size_t          propid;
  1303.     hashnode        hn;
  1304.     bdf_property_t  *prop, *fp;
  1305.     FT_Memory       memory = font->memory;
  1306.     FT_Error        error  = FT_Err_Ok;
  1307.  
  1308.     FT_UNUSED( lineno );        /* only used in debug mode */
  1309.  
  1310.  
  1311.     /* First, check whether the property already exists in the font. */
  1312.     if ( ( hn = hash_lookup( name, (hashtable *)font->internal ) ) != 0 )
  1313.     {
  1314.       /* The property already exists in the font, so simply replace */
  1315.       /* the value of the property with the current value.          */
  1316.       fp = font->props + hn->data;
  1317.  
  1318.       switch ( fp->format )
  1319.       {
  1320.       case BDF_ATOM:
  1321.         /* Delete the current atom if it exists. */
  1322.         FT_FREE( fp->value.atom );
  1323.  
  1324.         if ( value && value[0] != 0 )
  1325.         {
  1326.           if ( FT_STRDUP( fp->value.atom, value ) )
  1327.             goto Exit;
  1328.         }
  1329.         break;
  1330.  
  1331.       case BDF_INTEGER:
  1332.         fp->value.l = _bdf_atol( value, 0, 10 );
  1333.         break;
  1334.  
  1335.       case BDF_CARDINAL:
  1336.         fp->value.ul = _bdf_atoul( value, 0, 10 );
  1337.         break;
  1338.  
  1339.       default:
  1340.         ;
  1341.       }
  1342.  
  1343.       goto Exit;
  1344.     }
  1345.  
  1346.     /* See whether this property type exists yet or not. */
  1347.     /* If not, create it.                                */
  1348.     hn = hash_lookup( name, &(font->proptbl) );
  1349.     if ( hn == 0 )
  1350.     {
  1351.       error = bdf_create_property( name, BDF_ATOM, font );
  1352.       if ( error )
  1353.         goto Exit;
  1354.       hn = hash_lookup( name, &(font->proptbl) );
  1355.     }
  1356.  
  1357.     /* Allocate another property if this is overflow. */
  1358.     if ( font->props_used == font->props_size )
  1359.     {
  1360.       if ( font->props_size == 0 )
  1361.       {
  1362.         if ( FT_NEW_ARRAY( font->props, 1 ) )
  1363.           goto Exit;
  1364.       }
  1365.       else
  1366.       {
  1367.         if ( FT_RENEW_ARRAY( font->props,
  1368.                              font->props_size,
  1369.                              font->props_size + 1 ) )
  1370.           goto Exit;
  1371.       }
  1372.  
  1373.       fp = font->props + font->props_size;
  1374.       FT_MEM_ZERO( fp, sizeof ( bdf_property_t ) );
  1375.       font->props_size++;
  1376.     }
  1377.  
  1378.     propid = hn->data;
  1379.     if ( propid >= _num_bdf_properties )
  1380.       prop = font->user_props + ( propid - _num_bdf_properties );
  1381.     else
  1382.       prop = (bdf_property_t*)_bdf_properties + propid;
  1383.  
  1384.     fp = font->props + font->props_used;
  1385.  
  1386.     fp->name    = prop->name;
  1387.     fp->format  = prop->format;
  1388.     fp->builtin = prop->builtin;
  1389.  
  1390.     switch ( prop->format )
  1391.     {
  1392.     case BDF_ATOM:
  1393.       fp->value.atom = 0;
  1394.       if ( value != 0 && value[0] )
  1395.       {
  1396.         if ( FT_STRDUP( fp->value.atom, value ) )
  1397.           goto Exit;
  1398.       }
  1399.       break;
  1400.  
  1401.     case BDF_INTEGER:
  1402.       fp->value.l = _bdf_atol( value, 0, 10 );
  1403.       break;
  1404.  
  1405.     case BDF_CARDINAL:
  1406.       fp->value.ul = _bdf_atoul( value, 0, 10 );
  1407.       break;
  1408.     }
  1409.  
  1410.     /* If the property happens to be a comment, then it doesn't need */
  1411.     /* to be added to the internal hash table.                       */
  1412.     if ( ft_memcmp( name, "COMMENT", 7 ) != 0 )
  1413.     {
  1414.       /* Add the property to the font property table. */
  1415.       error = hash_insert( fp->name,
  1416.                            font->props_used,
  1417.                            (hashtable *)font->internal,
  1418.                            memory );
  1419.       if ( error )
  1420.         goto Exit;
  1421.     }
  1422.  
  1423.     font->props_used++;
  1424.  
  1425.     /* Some special cases need to be handled here.  The DEFAULT_CHAR       */
  1426.     /* property needs to be located if it exists in the property list, the */
  1427.     /* FONT_ASCENT and FONT_DESCENT need to be assigned if they are        */
  1428.     /* present, and the SPACING property should override the default       */
  1429.     /* spacing.                                                            */
  1430.     if ( ft_memcmp( name, "DEFAULT_CHAR", 12 ) == 0 )
  1431.       font->default_char = fp->value.l;
  1432.     else if ( ft_memcmp( name, "FONT_ASCENT", 11 ) == 0 )
  1433.       font->font_ascent = fp->value.l;
  1434.     else if ( ft_memcmp( name, "FONT_DESCENT", 12 ) == 0 )
  1435.       font->font_descent = fp->value.l;
  1436.     else if ( ft_memcmp( name, "SPACING", 7 ) == 0 )
  1437.     {
  1438.       if ( !fp->value.atom )
  1439.       {
  1440.         FT_ERROR(( "_bdf_add_property: " ERRMSG8, lineno, "SPACING" ));
  1441.         error = FT_THROW( Invalid_File_Format );
  1442.         goto Exit;
  1443.       }
  1444.  
  1445.       if ( fp->value.atom[0] == 'p' || fp->value.atom[0] == 'P' )
  1446.         font->spacing = BDF_PROPORTIONAL;
  1447.       else if ( fp->value.atom[0] == 'm' || fp->value.atom[0] == 'M' )
  1448.         font->spacing = BDF_MONOWIDTH;
  1449.       else if ( fp->value.atom[0] == 'c' || fp->value.atom[0] == 'C' )
  1450.         font->spacing = BDF_CHARCELL;
  1451.     }
  1452.  
  1453.   Exit:
  1454.     return error;
  1455.   }
  1456.  
  1457.  
  1458.   static const unsigned char nibble_mask[8] =
  1459.   {
  1460.     0xFF, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE
  1461.   };
  1462.  
  1463.  
  1464.   /* Actually parse the glyph info and bitmaps. */
  1465.   static FT_Error
  1466.   _bdf_parse_glyphs( char*          line,
  1467.                      unsigned long  linelen,
  1468.                      unsigned long  lineno,
  1469.                      void*          call_data,
  1470.                      void*          client_data )
  1471.   {
  1472.     int                c, mask_index;
  1473.     char*              s;
  1474.     unsigned char*     bp;
  1475.     unsigned long      i, slen, nibbles;
  1476.  
  1477.     _bdf_parse_t*      p;
  1478.     bdf_glyph_t*       glyph;
  1479.     bdf_font_t*        font;
  1480.  
  1481.     FT_Memory          memory;
  1482.     FT_Error           error = FT_Err_Ok;
  1483.  
  1484.     FT_UNUSED( call_data );
  1485.     FT_UNUSED( lineno );        /* only used in debug mode */
  1486.  
  1487.  
  1488.     p = (_bdf_parse_t *)client_data;
  1489.  
  1490.     font   = p->font;
  1491.     memory = font->memory;
  1492.  
  1493.     /* Check for a comment. */
  1494.     if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
  1495.     {
  1496.       linelen -= 7;
  1497.  
  1498.       s = line + 7;
  1499.       if ( *s != 0 )
  1500.       {
  1501.         s++;
  1502.         linelen--;
  1503.       }
  1504.       error = _bdf_add_comment( p->font, s, linelen );
  1505.       goto Exit;
  1506.     }
  1507.  
  1508.     /* The very first thing expected is the number of glyphs. */
  1509.     if ( !( p->flags & _BDF_GLYPHS ) )
  1510.     {
  1511.       if ( ft_memcmp( line, "CHARS", 5 ) != 0 )
  1512.       {
  1513.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "CHARS" ));
  1514.         error = FT_THROW( Missing_Chars_Field );
  1515.         goto Exit;
  1516.       }
  1517.  
  1518.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1519.       if ( error )
  1520.         goto Exit;
  1521.       p->cnt = font->glyphs_size = _bdf_atoul( p->list.field[1], 0, 10 );
  1522.  
  1523.       /* Make sure the number of glyphs is non-zero. */
  1524.       if ( p->cnt == 0 )
  1525.         font->glyphs_size = 64;
  1526.  
  1527.       /* Limit ourselves to 1,114,112 glyphs in the font (this is the */
  1528.       /* number of code points available in Unicode).                 */
  1529.       if ( p->cnt >= 0x110000UL )
  1530.       {
  1531.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG5, lineno, "CHARS" ));
  1532.         error = FT_THROW( Invalid_Argument );
  1533.         goto Exit;
  1534.       }
  1535.  
  1536.       if ( FT_NEW_ARRAY( font->glyphs, font->glyphs_size ) )
  1537.         goto Exit;
  1538.  
  1539.       p->flags |= _BDF_GLYPHS;
  1540.  
  1541.       goto Exit;
  1542.     }
  1543.  
  1544.     /* Check for the ENDFONT field. */
  1545.     if ( ft_memcmp( line, "ENDFONT", 7 ) == 0 )
  1546.     {
  1547.       /* Sort the glyphs by encoding. */
  1548.       ft_qsort( (char *)font->glyphs,
  1549.                 font->glyphs_used,
  1550.                 sizeof ( bdf_glyph_t ),
  1551.                 by_encoding );
  1552.  
  1553.       p->flags &= ~_BDF_START;
  1554.  
  1555.       goto Exit;
  1556.     }
  1557.  
  1558.     /* Check for the ENDCHAR field. */
  1559.     if ( ft_memcmp( line, "ENDCHAR", 7 ) == 0 )
  1560.     {
  1561.       p->glyph_enc = 0;
  1562.       p->flags    &= ~_BDF_GLYPH_BITS;
  1563.  
  1564.       goto Exit;
  1565.     }
  1566.  
  1567.     /* Check whether a glyph is being scanned but should be */
  1568.     /* ignored because it is an unencoded glyph.            */
  1569.     if ( ( p->flags & _BDF_GLYPH )     &&
  1570.          p->glyph_enc            == -1 &&
  1571.          p->opts->keep_unencoded == 0  )
  1572.       goto Exit;
  1573.  
  1574.     /* Check for the STARTCHAR field. */
  1575.     if ( ft_memcmp( line, "STARTCHAR", 9 ) == 0 )
  1576.     {
  1577.       /* Set the character name in the parse info first until the */
  1578.       /* encoding can be checked for an unencoded character.      */
  1579.       FT_FREE( p->glyph_name );
  1580.  
  1581.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1582.       if ( error )
  1583.         goto Exit;
  1584.  
  1585.       _bdf_list_shift( &p->list, 1 );
  1586.  
  1587.       s = _bdf_list_join( &p->list, ' ', &slen );
  1588.  
  1589.       if ( !s )
  1590.       {
  1591.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG8, lineno, "STARTCHAR" ));
  1592.         error = FT_THROW( Invalid_File_Format );
  1593.         goto Exit;
  1594.       }
  1595.  
  1596.       if ( FT_NEW_ARRAY( p->glyph_name, slen + 1 ) )
  1597.         goto Exit;
  1598.  
  1599.       FT_MEM_COPY( p->glyph_name, s, slen + 1 );
  1600.  
  1601.       p->flags |= _BDF_GLYPH;
  1602.  
  1603.       FT_TRACE4(( DBGMSG1, lineno, s ));
  1604.  
  1605.       goto Exit;
  1606.     }
  1607.  
  1608.     /* Check for the ENCODING field. */
  1609.     if ( ft_memcmp( line, "ENCODING", 8 ) == 0 )
  1610.     {
  1611.       if ( !( p->flags & _BDF_GLYPH ) )
  1612.       {
  1613.         /* Missing STARTCHAR field. */
  1614.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "STARTCHAR" ));
  1615.         error = FT_THROW( Missing_Startchar_Field );
  1616.         goto Exit;
  1617.       }
  1618.  
  1619.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1620.       if ( error )
  1621.         goto Exit;
  1622.  
  1623.       p->glyph_enc = _bdf_atol( p->list.field[1], 0, 10 );
  1624.  
  1625.       /* Normalize negative encoding values.  The specification only */
  1626.       /* allows -1, but we can be more generous here.                */
  1627.       if ( p->glyph_enc < -1 )
  1628.         p->glyph_enc = -1;
  1629.  
  1630.       /* Check for alternative encoding format. */
  1631.       if ( p->glyph_enc == -1 && p->list.used > 2 )
  1632.         p->glyph_enc = _bdf_atol( p->list.field[2], 0, 10 );
  1633.  
  1634.       if ( p->glyph_enc < -1 )
  1635.         p->glyph_enc = -1;
  1636.  
  1637.       FT_TRACE4(( DBGMSG2, p->glyph_enc ));
  1638.  
  1639.       /* Check that the encoding is in the Unicode range because  */
  1640.       /* otherwise p->have (a bitmap with static size) overflows. */
  1641.       if ( p->glyph_enc > 0                                      &&
  1642.            (size_t)p->glyph_enc >= sizeof ( p->have ) /
  1643.                                    sizeof ( unsigned long ) * 32 )
  1644.       {
  1645.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG5, lineno, "ENCODING" ));
  1646.         error = FT_THROW( Invalid_File_Format );
  1647.         goto Exit;
  1648.       }
  1649.  
  1650.       /* Check whether this encoding has already been encountered. */
  1651.       /* If it has then change it to unencoded so it gets added if */
  1652.       /* indicated.                                                */
  1653.       if ( p->glyph_enc >= 0 )
  1654.       {
  1655.         if ( _bdf_glyph_modified( p->have, p->glyph_enc ) )
  1656.         {
  1657.           /* Emit a message saying a glyph has been moved to the */
  1658.           /* unencoded area.                                     */
  1659.           FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG12,
  1660.                       p->glyph_enc, p->glyph_name ));
  1661.           p->glyph_enc = -1;
  1662.           font->modified = 1;
  1663.         }
  1664.         else
  1665.           _bdf_set_glyph_modified( p->have, p->glyph_enc );
  1666.       }
  1667.  
  1668.       if ( p->glyph_enc >= 0 )
  1669.       {
  1670.         /* Make sure there are enough glyphs allocated in case the */
  1671.         /* number of characters happen to be wrong.                */
  1672.         if ( font->glyphs_used == font->glyphs_size )
  1673.         {
  1674.           if ( FT_RENEW_ARRAY( font->glyphs,
  1675.                                font->glyphs_size,
  1676.                                font->glyphs_size + 64 ) )
  1677.             goto Exit;
  1678.  
  1679.           font->glyphs_size += 64;
  1680.         }
  1681.  
  1682.         glyph           = font->glyphs + font->glyphs_used++;
  1683.         glyph->name     = p->glyph_name;
  1684.         glyph->encoding = p->glyph_enc;
  1685.  
  1686.         /* Reset the initial glyph info. */
  1687.         p->glyph_name = 0;
  1688.       }
  1689.       else
  1690.       {
  1691.         /* Unencoded glyph.  Check whether it should */
  1692.         /* be added or not.                          */
  1693.         if ( p->opts->keep_unencoded != 0 )
  1694.         {
  1695.           /* Allocate the next unencoded glyph. */
  1696.           if ( font->unencoded_used == font->unencoded_size )
  1697.           {
  1698.             if ( FT_RENEW_ARRAY( font->unencoded ,
  1699.                                  font->unencoded_size,
  1700.                                  font->unencoded_size + 4 ) )
  1701.               goto Exit;
  1702.  
  1703.             font->unencoded_size += 4;
  1704.           }
  1705.  
  1706.           glyph           = font->unencoded + font->unencoded_used;
  1707.           glyph->name     = p->glyph_name;
  1708.           glyph->encoding = font->unencoded_used++;
  1709.         }
  1710.         else
  1711.           /* Free up the glyph name if the unencoded shouldn't be */
  1712.           /* kept.                                                */
  1713.           FT_FREE( p->glyph_name );
  1714.  
  1715.         p->glyph_name = 0;
  1716.       }
  1717.  
  1718.       /* Clear the flags that might be added when width and height are */
  1719.       /* checked for consistency.                                      */
  1720.       p->flags &= ~( _BDF_GLYPH_WIDTH_CHECK | _BDF_GLYPH_HEIGHT_CHECK );
  1721.  
  1722.       p->flags |= _BDF_ENCODING;
  1723.  
  1724.       goto Exit;
  1725.     }
  1726.  
  1727.     /* Point at the glyph being constructed. */
  1728.     if ( p->glyph_enc == -1 )
  1729.       glyph = font->unencoded + ( font->unencoded_used - 1 );
  1730.     else
  1731.       glyph = font->glyphs + ( font->glyphs_used - 1 );
  1732.  
  1733.     /* Check whether a bitmap is being constructed. */
  1734.     if ( p->flags & _BDF_BITMAP )
  1735.     {
  1736.       /* If there are more rows than are specified in the glyph metrics, */
  1737.       /* ignore the remaining lines.                                     */
  1738.       if ( p->row >= (unsigned long)glyph->bbx.height )
  1739.       {
  1740.         if ( !( p->flags & _BDF_GLYPH_HEIGHT_CHECK ) )
  1741.         {
  1742.           FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG13, glyph->encoding ));
  1743.           p->flags |= _BDF_GLYPH_HEIGHT_CHECK;
  1744.           font->modified = 1;
  1745.         }
  1746.  
  1747.         goto Exit;
  1748.       }
  1749.  
  1750.       /* Only collect the number of nibbles indicated by the glyph     */
  1751.       /* metrics.  If there are more columns, they are simply ignored. */
  1752.       nibbles = glyph->bpr << 1;
  1753.       bp      = glyph->bitmap + p->row * glyph->bpr;
  1754.  
  1755.       for ( i = 0; i < nibbles; i++ )
  1756.       {
  1757.         c = line[i];
  1758.         if ( !sbitset( hdigits, c ) )
  1759.           break;
  1760.         *bp = (FT_Byte)( ( *bp << 4 ) + a2i[c] );
  1761.         if ( i + 1 < nibbles && ( i & 1 ) )
  1762.           *++bp = 0;
  1763.       }
  1764.  
  1765.       /* If any line has not enough columns,            */
  1766.       /* indicate they have been padded with zero bits. */
  1767.       if ( i < nibbles                            &&
  1768.            !( p->flags & _BDF_GLYPH_WIDTH_CHECK ) )
  1769.       {
  1770.         FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG16, glyph->encoding ));
  1771.         p->flags       |= _BDF_GLYPH_WIDTH_CHECK;
  1772.         font->modified  = 1;
  1773.       }
  1774.  
  1775.       /* Remove possible garbage at the right. */
  1776.       mask_index = ( glyph->bbx.width * p->font->bpp ) & 7;
  1777.       if ( glyph->bbx.width )
  1778.         *bp &= nibble_mask[mask_index];
  1779.  
  1780.       /* If any line has extra columns, indicate they have been removed. */
  1781.       if ( i == nibbles                           &&
  1782.            sbitset( hdigits, line[nibbles] )      &&
  1783.            !( p->flags & _BDF_GLYPH_WIDTH_CHECK ) )
  1784.       {
  1785.         FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG14, glyph->encoding ));
  1786.         p->flags       |= _BDF_GLYPH_WIDTH_CHECK;
  1787.         font->modified  = 1;
  1788.       }
  1789.  
  1790.       p->row++;
  1791.       goto Exit;
  1792.     }
  1793.  
  1794.     /* Expect the SWIDTH (scalable width) field next. */
  1795.     if ( ft_memcmp( line, "SWIDTH", 6 ) == 0 )
  1796.     {
  1797.       if ( !( p->flags & _BDF_ENCODING ) )
  1798.         goto Missing_Encoding;
  1799.  
  1800.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1801.       if ( error )
  1802.         goto Exit;
  1803.  
  1804.       glyph->swidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 );
  1805.       p->flags |= _BDF_SWIDTH;
  1806.  
  1807.       goto Exit;
  1808.     }
  1809.  
  1810.     /* Expect the DWIDTH (scalable width) field next. */
  1811.     if ( ft_memcmp( line, "DWIDTH", 6 ) == 0 )
  1812.     {
  1813.       if ( !( p->flags & _BDF_ENCODING ) )
  1814.         goto Missing_Encoding;
  1815.  
  1816.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1817.       if ( error )
  1818.         goto Exit;
  1819.  
  1820.       glyph->dwidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 );
  1821.  
  1822.       if ( !( p->flags & _BDF_SWIDTH ) )
  1823.       {
  1824.         /* Missing SWIDTH field.  Emit an auto correction message and set */
  1825.         /* the scalable width from the device width.                      */
  1826.         FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG9, lineno ));
  1827.  
  1828.         glyph->swidth = (unsigned short)FT_MulDiv(
  1829.                           glyph->dwidth, 72000L,
  1830.                           (FT_Long)( font->point_size *
  1831.                                      font->resolution_x ) );
  1832.       }
  1833.  
  1834.       p->flags |= _BDF_DWIDTH;
  1835.       goto Exit;
  1836.     }
  1837.  
  1838.     /* Expect the BBX field next. */
  1839.     if ( ft_memcmp( line, "BBX", 3 ) == 0 )
  1840.     {
  1841.       if ( !( p->flags & _BDF_ENCODING ) )
  1842.         goto Missing_Encoding;
  1843.  
  1844.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  1845.       if ( error )
  1846.         goto Exit;
  1847.  
  1848.       glyph->bbx.width    = _bdf_atos( p->list.field[1], 0, 10 );
  1849.       glyph->bbx.height   = _bdf_atos( p->list.field[2], 0, 10 );
  1850.       glyph->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 );
  1851.       glyph->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 );
  1852.  
  1853.       /* Generate the ascent and descent of the character. */
  1854.       glyph->bbx.ascent  = (short)( glyph->bbx.height + glyph->bbx.y_offset );
  1855.       glyph->bbx.descent = (short)( -glyph->bbx.y_offset );
  1856.  
  1857.       /* Determine the overall font bounding box as the characters are */
  1858.       /* loaded so corrections can be done later if indicated.         */
  1859.       p->maxas    = (short)FT_MAX( glyph->bbx.ascent, p->maxas );
  1860.       p->maxds    = (short)FT_MAX( glyph->bbx.descent, p->maxds );
  1861.  
  1862.       p->rbearing = (short)( glyph->bbx.width + glyph->bbx.x_offset );
  1863.  
  1864.       p->maxrb    = (short)FT_MAX( p->rbearing, p->maxrb );
  1865.       p->minlb    = (short)FT_MIN( glyph->bbx.x_offset, p->minlb );
  1866.       p->maxlb    = (short)FT_MAX( glyph->bbx.x_offset, p->maxlb );
  1867.  
  1868.       if ( !( p->flags & _BDF_DWIDTH ) )
  1869.       {
  1870.         /* Missing DWIDTH field.  Emit an auto correction message and set */
  1871.         /* the device width to the glyph width.                           */
  1872.         FT_TRACE2(( "_bdf_parse_glyphs: " ACMSG10, lineno ));
  1873.         glyph->dwidth = glyph->bbx.width;
  1874.       }
  1875.  
  1876.       /* If the BDF_CORRECT_METRICS flag is set, then adjust the SWIDTH */
  1877.       /* value if necessary.                                            */
  1878.       if ( p->opts->correct_metrics != 0 )
  1879.       {
  1880.         /* Determine the point size of the glyph. */
  1881.         unsigned short  sw = (unsigned short)FT_MulDiv(
  1882.                                glyph->dwidth, 72000L,
  1883.                                (FT_Long)( font->point_size *
  1884.                                           font->resolution_x ) );
  1885.  
  1886.  
  1887.         if ( sw != glyph->swidth )
  1888.         {
  1889.           glyph->swidth = sw;
  1890.  
  1891.           if ( p->glyph_enc == -1 )
  1892.             _bdf_set_glyph_modified( font->umod,
  1893.                                      font->unencoded_used - 1 );
  1894.           else
  1895.             _bdf_set_glyph_modified( font->nmod, glyph->encoding );
  1896.  
  1897.           p->flags       |= _BDF_SWIDTH_ADJ;
  1898.           font->modified  = 1;
  1899.         }
  1900.       }
  1901.  
  1902.       p->flags |= _BDF_BBX;
  1903.       goto Exit;
  1904.     }
  1905.  
  1906.     /* And finally, gather up the bitmap. */
  1907.     if ( ft_memcmp( line, "BITMAP", 6 ) == 0 )
  1908.     {
  1909.       unsigned long  bitmap_size;
  1910.  
  1911.  
  1912.       if ( !( p->flags & _BDF_BBX ) )
  1913.       {
  1914.         /* Missing BBX field. */
  1915.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "BBX" ));
  1916.         error = FT_THROW( Missing_Bbx_Field );
  1917.         goto Exit;
  1918.       }
  1919.  
  1920.       /* Allocate enough space for the bitmap. */
  1921.       glyph->bpr = ( glyph->bbx.width * p->font->bpp + 7 ) >> 3;
  1922.  
  1923.       bitmap_size = glyph->bpr * glyph->bbx.height;
  1924.       if ( glyph->bpr > 0xFFFFU || bitmap_size > 0xFFFFU )
  1925.       {
  1926.         FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG4, lineno ));
  1927.         error = FT_THROW( Bbx_Too_Big );
  1928.         goto Exit;
  1929.       }
  1930.       else
  1931.         glyph->bytes = (unsigned short)bitmap_size;
  1932.  
  1933.       if ( FT_NEW_ARRAY( glyph->bitmap, glyph->bytes ) )
  1934.         goto Exit;
  1935.  
  1936.       p->row    = 0;
  1937.       p->flags |= _BDF_BITMAP;
  1938.  
  1939.       goto Exit;
  1940.     }
  1941.  
  1942.     FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG9, lineno ));
  1943.     error = FT_THROW( Invalid_File_Format );
  1944.     goto Exit;
  1945.  
  1946.   Missing_Encoding:
  1947.     /* Missing ENCODING field. */
  1948.     FT_ERROR(( "_bdf_parse_glyphs: " ERRMSG1, lineno, "ENCODING" ));
  1949.     error = FT_THROW( Missing_Encoding_Field );
  1950.  
  1951.   Exit:
  1952.     if ( error && ( p->flags & _BDF_GLYPH ) )
  1953.       FT_FREE( p->glyph_name );
  1954.  
  1955.     return error;
  1956.   }
  1957.  
  1958.  
  1959.   /* Load the font properties. */
  1960.   static FT_Error
  1961.   _bdf_parse_properties( char*          line,
  1962.                          unsigned long  linelen,
  1963.                          unsigned long  lineno,
  1964.                          void*          call_data,
  1965.                          void*          client_data )
  1966.   {
  1967.     unsigned long      vlen;
  1968.     _bdf_line_func_t*  next;
  1969.     _bdf_parse_t*      p;
  1970.     char*              name;
  1971.     char*              value;
  1972.     char               nbuf[128];
  1973.     FT_Error           error = FT_Err_Ok;
  1974.  
  1975.     FT_UNUSED( lineno );
  1976.  
  1977.  
  1978.     next = (_bdf_line_func_t *)call_data;
  1979.     p    = (_bdf_parse_t *)    client_data;
  1980.  
  1981.     /* Check for the end of the properties. */
  1982.     if ( ft_memcmp( line, "ENDPROPERTIES", 13 ) == 0 )
  1983.     {
  1984.       /* If the FONT_ASCENT or FONT_DESCENT properties have not been      */
  1985.       /* encountered yet, then make sure they are added as properties and */
  1986.       /* make sure they are set from the font bounding box info.          */
  1987.       /*                                                                  */
  1988.       /* This is *always* done regardless of the options, because X11     */
  1989.       /* requires these two fields to compile fonts.                      */
  1990.       if ( bdf_get_font_property( p->font, "FONT_ASCENT" ) == 0 )
  1991.       {
  1992.         p->font->font_ascent = p->font->bbx.ascent;
  1993.         ft_sprintf( nbuf, "%hd", p->font->bbx.ascent );
  1994.         error = _bdf_add_property( p->font, (char *)"FONT_ASCENT",
  1995.                                    nbuf, lineno );
  1996.         if ( error )
  1997.           goto Exit;
  1998.  
  1999.         FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent ));
  2000.         p->font->modified = 1;
  2001.       }
  2002.  
  2003.       if ( bdf_get_font_property( p->font, "FONT_DESCENT" ) == 0 )
  2004.       {
  2005.         p->font->font_descent = p->font->bbx.descent;
  2006.         ft_sprintf( nbuf, "%hd", p->font->bbx.descent );
  2007.         error = _bdf_add_property( p->font, (char *)"FONT_DESCENT",
  2008.                                    nbuf, lineno );
  2009.         if ( error )
  2010.           goto Exit;
  2011.  
  2012.         FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent ));
  2013.         p->font->modified = 1;
  2014.       }
  2015.  
  2016.       p->flags &= ~_BDF_PROPS;
  2017.       *next     = _bdf_parse_glyphs;
  2018.  
  2019.       goto Exit;
  2020.     }
  2021.  
  2022.     /* Ignore the _XFREE86_GLYPH_RANGES properties. */
  2023.     if ( ft_memcmp( line, "_XFREE86_GLYPH_RANGES", 21 ) == 0 )
  2024.       goto Exit;
  2025.  
  2026.     /* Handle COMMENT fields and properties in a special way to preserve */
  2027.     /* the spacing.                                                      */
  2028.     if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
  2029.     {
  2030.       name = value = line;
  2031.       value += 7;
  2032.       if ( *value )
  2033.         *value++ = 0;
  2034.       error = _bdf_add_property( p->font, name, value, lineno );
  2035.       if ( error )
  2036.         goto Exit;
  2037.     }
  2038.     else if ( _bdf_is_atom( line, linelen, &name, &value, p->font ) )
  2039.     {
  2040.       error = _bdf_add_property( p->font, name, value, lineno );
  2041.       if ( error )
  2042.         goto Exit;
  2043.     }
  2044.     else
  2045.     {
  2046.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  2047.       if ( error )
  2048.         goto Exit;
  2049.       name = p->list.field[0];
  2050.  
  2051.       _bdf_list_shift( &p->list, 1 );
  2052.       value = _bdf_list_join( &p->list, ' ', &vlen );
  2053.  
  2054.       error = _bdf_add_property( p->font, name, value, lineno );
  2055.       if ( error )
  2056.         goto Exit;
  2057.     }
  2058.  
  2059.   Exit:
  2060.     return error;
  2061.   }
  2062.  
  2063.  
  2064.   /* Load the font header. */
  2065.   static FT_Error
  2066.   _bdf_parse_start( char*          line,
  2067.                     unsigned long  linelen,
  2068.                     unsigned long  lineno,
  2069.                     void*          call_data,
  2070.                     void*          client_data )
  2071.   {
  2072.     unsigned long      slen;
  2073.     _bdf_line_func_t*  next;
  2074.     _bdf_parse_t*      p;
  2075.     bdf_font_t*        font;
  2076.     char               *s;
  2077.  
  2078.     FT_Memory          memory = NULL;
  2079.     FT_Error           error  = FT_Err_Ok;
  2080.  
  2081.     FT_UNUSED( lineno );            /* only used in debug mode */
  2082.  
  2083.  
  2084.     next = (_bdf_line_func_t *)call_data;
  2085.     p    = (_bdf_parse_t *)    client_data;
  2086.  
  2087.     if ( p->font )
  2088.       memory = p->font->memory;
  2089.  
  2090.     /* Check for a comment.  This is done to handle those fonts that have */
  2091.     /* comments before the STARTFONT line for some reason.                */
  2092.     if ( ft_memcmp( line, "COMMENT", 7 ) == 0 )
  2093.     {
  2094.       if ( p->opts->keep_comments != 0 && p->font != 0 )
  2095.       {
  2096.         linelen -= 7;
  2097.  
  2098.         s = line + 7;
  2099.         if ( *s != 0 )
  2100.         {
  2101.           s++;
  2102.           linelen--;
  2103.         }
  2104.  
  2105.         error = _bdf_add_comment( p->font, s, linelen );
  2106.         if ( error )
  2107.           goto Exit;
  2108.         /* here font is not defined! */
  2109.       }
  2110.  
  2111.       goto Exit;
  2112.     }
  2113.  
  2114.     if ( !( p->flags & _BDF_START ) )
  2115.     {
  2116.       memory = p->memory;
  2117.  
  2118.       if ( ft_memcmp( line, "STARTFONT", 9 ) != 0 )
  2119.       {
  2120.         /* we don't emit an error message since this code gets */
  2121.         /* explicitly caught one level higher                  */
  2122.         error = FT_THROW( Missing_Startfont_Field );
  2123.         goto Exit;
  2124.       }
  2125.  
  2126.       p->flags = _BDF_START;
  2127.       font = p->font = 0;
  2128.  
  2129.       if ( FT_NEW( font ) )
  2130.         goto Exit;
  2131.       p->font = font;
  2132.  
  2133.       font->memory = p->memory;
  2134.       p->memory    = 0;
  2135.  
  2136.       { /* setup */
  2137.         size_t           i;
  2138.         bdf_property_t*  prop;
  2139.  
  2140.  
  2141.         error = hash_init( &(font->proptbl), memory );
  2142.         if ( error )
  2143.           goto Exit;
  2144.         for ( i = 0, prop = (bdf_property_t*)_bdf_properties;
  2145.               i < _num_bdf_properties; i++, prop++ )
  2146.         {
  2147.           error = hash_insert( prop->name, i,
  2148.                                &(font->proptbl), memory );
  2149.           if ( error )
  2150.             goto Exit;
  2151.         }
  2152.       }
  2153.  
  2154.       if ( FT_ALLOC( p->font->internal, sizeof ( hashtable ) ) )
  2155.         goto Exit;
  2156.       error = hash_init( (hashtable *)p->font->internal,memory );
  2157.       if ( error )
  2158.         goto Exit;
  2159.       p->font->spacing      = p->opts->font_spacing;
  2160.       p->font->default_char = -1;
  2161.  
  2162.       goto Exit;
  2163.     }
  2164.  
  2165.     /* Check for the start of the properties. */
  2166.     if ( ft_memcmp( line, "STARTPROPERTIES", 15 ) == 0 )
  2167.     {
  2168.       if ( !( p->flags & _BDF_FONT_BBX ) )
  2169.       {
  2170.         /* Missing the FONTBOUNDINGBOX field. */
  2171.         FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONTBOUNDINGBOX" ));
  2172.         error = FT_THROW( Missing_Fontboundingbox_Field );
  2173.         goto Exit;
  2174.       }
  2175.  
  2176.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  2177.       if ( error )
  2178.         goto Exit;
  2179.       /* at this point, `p->font' can't be NULL */
  2180.       p->cnt = p->font->props_size = _bdf_atoul( p->list.field[1], 0, 10 );
  2181.  
  2182.       if ( FT_NEW_ARRAY( p->font->props, p->cnt ) )
  2183.       {
  2184.         p->font->props_size = 0;
  2185.         goto Exit;
  2186.       }
  2187.  
  2188.       p->flags |= _BDF_PROPS;
  2189.       *next     = _bdf_parse_properties;
  2190.  
  2191.       goto Exit;
  2192.     }
  2193.  
  2194.     /* Check for the FONTBOUNDINGBOX field. */
  2195.     if ( ft_memcmp( line, "FONTBOUNDINGBOX", 15 ) == 0 )
  2196.     {
  2197.       if ( !( p->flags & _BDF_SIZE ) )
  2198.       {
  2199.         /* Missing the SIZE field. */
  2200.         FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "SIZE" ));
  2201.         error = FT_THROW( Missing_Size_Field );
  2202.         goto Exit;
  2203.       }
  2204.  
  2205.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  2206.       if ( error )
  2207.         goto Exit;
  2208.  
  2209.       p->font->bbx.width  = _bdf_atos( p->list.field[1], 0, 10 );
  2210.       p->font->bbx.height = _bdf_atos( p->list.field[2], 0, 10 );
  2211.  
  2212.       p->font->bbx.x_offset = _bdf_atos( p->list.field[3], 0, 10 );
  2213.       p->font->bbx.y_offset = _bdf_atos( p->list.field[4], 0, 10 );
  2214.  
  2215.       p->font->bbx.ascent  = (short)( p->font->bbx.height +
  2216.                                       p->font->bbx.y_offset );
  2217.  
  2218.       p->font->bbx.descent = (short)( -p->font->bbx.y_offset );
  2219.  
  2220.       p->flags |= _BDF_FONT_BBX;
  2221.  
  2222.       goto Exit;
  2223.     }
  2224.  
  2225.     /* The next thing to check for is the FONT field. */
  2226.     if ( ft_memcmp( line, "FONT", 4 ) == 0 )
  2227.     {
  2228.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  2229.       if ( error )
  2230.         goto Exit;
  2231.       _bdf_list_shift( &p->list, 1 );
  2232.  
  2233.       s = _bdf_list_join( &p->list, ' ', &slen );
  2234.  
  2235.       if ( !s )
  2236.       {
  2237.         FT_ERROR(( "_bdf_parse_start: " ERRMSG8, lineno, "FONT" ));
  2238.         error = FT_THROW( Invalid_File_Format );
  2239.         goto Exit;
  2240.       }
  2241.  
  2242.       /* Allowing multiple `FONT' lines (which is invalid) doesn't hurt... */
  2243.       FT_FREE( p->font->name );
  2244.  
  2245.       if ( FT_NEW_ARRAY( p->font->name, slen + 1 ) )
  2246.         goto Exit;
  2247.       FT_MEM_COPY( p->font->name, s, slen + 1 );
  2248.  
  2249.       /* If the font name is an XLFD name, set the spacing to the one in  */
  2250.       /* the font name.  If there is no spacing fall back on the default. */
  2251.       error = _bdf_set_default_spacing( p->font, p->opts, lineno );
  2252.       if ( error )
  2253.         goto Exit;
  2254.  
  2255.       p->flags |= _BDF_FONT_NAME;
  2256.  
  2257.       goto Exit;
  2258.     }
  2259.  
  2260.     /* Check for the SIZE field. */
  2261.     if ( ft_memcmp( line, "SIZE", 4 ) == 0 )
  2262.     {
  2263.       if ( !( p->flags & _BDF_FONT_NAME ) )
  2264.       {
  2265.         /* Missing the FONT field. */
  2266.         FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONT" ));
  2267.         error = FT_THROW( Missing_Font_Field );
  2268.         goto Exit;
  2269.       }
  2270.  
  2271.       error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
  2272.       if ( error )
  2273.         goto Exit;
  2274.  
  2275.       p->font->point_size   = _bdf_atoul( p->list.field[1], 0, 10 );
  2276.       p->font->resolution_x = _bdf_atoul( p->list.field[2], 0, 10 );
  2277.       p->font->resolution_y = _bdf_atoul( p->list.field[3], 0, 10 );
  2278.  
  2279.       /* Check for the bits per pixel field. */
  2280.       if ( p->list.used == 5 )
  2281.       {
  2282.         unsigned short bitcount, i, shift;
  2283.  
  2284.  
  2285.         p->font->bpp = (unsigned short)_bdf_atos( p->list.field[4], 0, 10 );
  2286.  
  2287.         /* Only values 1, 2, 4, 8 are allowed. */
  2288.         shift = p->font->bpp;
  2289.         bitcount = 0;
  2290.         for ( i = 0; shift > 0; i++ )
  2291.         {
  2292.           if ( shift & 1 )
  2293.             bitcount = i;
  2294.           shift >>= 1;
  2295.         }
  2296.  
  2297.         shift = (short)( ( bitcount > 3 ) ? 8 : ( 1 << bitcount ) );
  2298.  
  2299.         if ( p->font->bpp > shift || p->font->bpp != shift )
  2300.         {
  2301.           /* select next higher value */
  2302.           p->font->bpp = (unsigned short)( shift << 1 );
  2303.           FT_TRACE2(( "_bdf_parse_start: " ACMSG11, p->font->bpp ));
  2304.         }
  2305.       }
  2306.       else
  2307.         p->font->bpp = 1;
  2308.  
  2309.       p->flags |= _BDF_SIZE;
  2310.  
  2311.       goto Exit;
  2312.     }
  2313.  
  2314.     /* Check for the CHARS field -- font properties are optional */
  2315.     if ( ft_memcmp( line, "CHARS", 5 ) == 0 )
  2316.     {
  2317.       char  nbuf[128];
  2318.  
  2319.  
  2320.       if ( !( p->flags & _BDF_FONT_BBX ) )
  2321.       {
  2322.         /* Missing the FONTBOUNDINGBOX field. */
  2323.         FT_ERROR(( "_bdf_parse_start: " ERRMSG1, lineno, "FONTBOUNDINGBOX" ));
  2324.         error = FT_THROW( Missing_Fontboundingbox_Field );
  2325.         goto Exit;
  2326.       }
  2327.  
  2328.       /* Add the two standard X11 properties which are required */
  2329.       /* for compiling fonts.                                   */
  2330.       p->font->font_ascent = p->font->bbx.ascent;
  2331.       ft_sprintf( nbuf, "%hd", p->font->bbx.ascent );
  2332.       error = _bdf_add_property( p->font, (char *)"FONT_ASCENT",
  2333.                                  nbuf, lineno );
  2334.       if ( error )
  2335.         goto Exit;
  2336.       FT_TRACE2(( "_bdf_parse_properties: " ACMSG1, p->font->bbx.ascent ));
  2337.  
  2338.       p->font->font_descent = p->font->bbx.descent;
  2339.       ft_sprintf( nbuf, "%hd", p->font->bbx.descent );
  2340.       error = _bdf_add_property( p->font, (char *)"FONT_DESCENT",
  2341.                                  nbuf, lineno );
  2342.       if ( error )
  2343.         goto Exit;
  2344.       FT_TRACE2(( "_bdf_parse_properties: " ACMSG2, p->font->bbx.descent ));
  2345.  
  2346.       p->font->modified = 1;
  2347.  
  2348.       *next = _bdf_parse_glyphs;
  2349.  
  2350.       /* A special return value. */
  2351.       error = -1;
  2352.       goto Exit;
  2353.     }
  2354.  
  2355.     FT_ERROR(( "_bdf_parse_start: " ERRMSG9, lineno ));
  2356.     error = FT_THROW( Invalid_File_Format );
  2357.  
  2358.   Exit:
  2359.     return error;
  2360.   }
  2361.  
  2362.  
  2363.   /*************************************************************************/
  2364.   /*                                                                       */
  2365.   /* API.                                                                  */
  2366.   /*                                                                       */
  2367.   /*************************************************************************/
  2368.  
  2369.  
  2370.   FT_LOCAL_DEF( FT_Error )
  2371.   bdf_load_font( FT_Stream       stream,
  2372.                  FT_Memory       extmemory,
  2373.                  bdf_options_t*  opts,
  2374.                  bdf_font_t*    *font )
  2375.   {
  2376.     unsigned long  lineno = 0; /* make compiler happy */
  2377.     _bdf_parse_t   *p     = NULL;
  2378.  
  2379.     FT_Memory      memory = extmemory;
  2380.     FT_Error       error  = FT_Err_Ok;
  2381.  
  2382.  
  2383.     if ( FT_NEW( p ) )
  2384.       goto Exit;
  2385.  
  2386.     memory    = NULL;
  2387.     p->opts   = (bdf_options_t*)( ( opts != 0 ) ? opts : &_bdf_opts );
  2388.     p->minlb  = 32767;
  2389.     p->memory = extmemory;  /* only during font creation */
  2390.  
  2391.     _bdf_list_init( &p->list, extmemory );
  2392.  
  2393.     error = _bdf_readstream( stream, _bdf_parse_start,
  2394.                              (void *)p, &lineno );
  2395.     if ( error )
  2396.       goto Fail;
  2397.  
  2398.     if ( p->font != 0 )
  2399.     {
  2400.       /* If the font is not proportional, set the font's monowidth */
  2401.       /* field to the width of the font bounding box.              */
  2402.       memory = p->font->memory;
  2403.  
  2404.       if ( p->font->spacing != BDF_PROPORTIONAL )
  2405.         p->font->monowidth = p->font->bbx.width;
  2406.  
  2407.       /* If the number of glyphs loaded is not that of the original count, */
  2408.       /* indicate the difference.                                          */
  2409.       if ( p->cnt != p->font->glyphs_used + p->font->unencoded_used )
  2410.       {
  2411.         FT_TRACE2(( "bdf_load_font: " ACMSG15, p->cnt,
  2412.                     p->font->glyphs_used + p->font->unencoded_used ));
  2413.         p->font->modified = 1;
  2414.       }
  2415.  
  2416.       /* Once the font has been loaded, adjust the overall font metrics if */
  2417.       /* necessary.                                                        */
  2418.       if ( p->opts->correct_metrics != 0 &&
  2419.            ( p->font->glyphs_used > 0 || p->font->unencoded_used > 0 ) )
  2420.       {
  2421.         if ( p->maxrb - p->minlb != p->font->bbx.width )
  2422.         {
  2423.           FT_TRACE2(( "bdf_load_font: " ACMSG3,
  2424.                       p->font->bbx.width, p->maxrb - p->minlb ));
  2425.           p->font->bbx.width = (unsigned short)( p->maxrb - p->minlb );
  2426.           p->font->modified  = 1;
  2427.         }
  2428.  
  2429.         if ( p->font->bbx.x_offset != p->minlb )
  2430.         {
  2431.           FT_TRACE2(( "bdf_load_font: " ACMSG4,
  2432.                       p->font->bbx.x_offset, p->minlb ));
  2433.           p->font->bbx.x_offset = p->minlb;
  2434.           p->font->modified     = 1;
  2435.         }
  2436.  
  2437.         if ( p->font->bbx.ascent != p->maxas )
  2438.         {
  2439.           FT_TRACE2(( "bdf_load_font: " ACMSG5,
  2440.                       p->font->bbx.ascent, p->maxas ));
  2441.           p->font->bbx.ascent = p->maxas;
  2442.           p->font->modified   = 1;
  2443.         }
  2444.  
  2445.         if ( p->font->bbx.descent != p->maxds )
  2446.         {
  2447.           FT_TRACE2(( "bdf_load_font: " ACMSG6,
  2448.                       p->font->bbx.descent, p->maxds ));
  2449.           p->font->bbx.descent  = p->maxds;
  2450.           p->font->bbx.y_offset = (short)( -p->maxds );
  2451.           p->font->modified     = 1;
  2452.         }
  2453.  
  2454.         if ( p->maxas + p->maxds != p->font->bbx.height )
  2455.         {
  2456.           FT_TRACE2(( "bdf_load_font: " ACMSG7,
  2457.                       p->font->bbx.height, p->maxas + p->maxds ));
  2458.           p->font->bbx.height = (unsigned short)( p->maxas + p->maxds );
  2459.         }
  2460.  
  2461.         if ( p->flags & _BDF_SWIDTH_ADJ )
  2462.           FT_TRACE2(( "bdf_load_font: " ACMSG8 ));
  2463.       }
  2464.     }
  2465.  
  2466.     if ( p->flags & _BDF_START )
  2467.     {
  2468.       /* The ENDFONT field was never reached or did not exist. */
  2469.       if ( !( p->flags & _BDF_GLYPHS ) )
  2470.       {
  2471.         /* Error happened while parsing header. */
  2472.         FT_ERROR(( "bdf_load_font: " ERRMSG2, lineno ));
  2473.         error = FT_THROW( Corrupted_Font_Header );
  2474.         goto Exit;
  2475.       }
  2476.       else
  2477.       {
  2478.         /* Error happened when parsing glyphs. */
  2479.         FT_ERROR(( "bdf_load_font: " ERRMSG3, lineno ));
  2480.         error = FT_THROW( Corrupted_Font_Glyphs );
  2481.         goto Exit;
  2482.       }
  2483.     }
  2484.  
  2485.     if ( p->font != 0 )
  2486.     {
  2487.       /* Make sure the comments are NULL terminated if they exist. */
  2488.       memory = p->font->memory;
  2489.  
  2490.       if ( p->font->comments_len > 0 )
  2491.       {
  2492.         if ( FT_RENEW_ARRAY( p->font->comments,
  2493.                              p->font->comments_len,
  2494.                              p->font->comments_len + 1 ) )
  2495.           goto Fail;
  2496.  
  2497.         p->font->comments[p->font->comments_len] = 0;
  2498.       }
  2499.     }
  2500.     else if ( error == FT_Err_Ok )
  2501.       error = FT_THROW( Invalid_File_Format );
  2502.  
  2503.     *font = p->font;
  2504.  
  2505.   Exit:
  2506.     if ( p )
  2507.     {
  2508.       _bdf_list_done( &p->list );
  2509.  
  2510.       memory = extmemory;
  2511.  
  2512.       FT_FREE( p );
  2513.     }
  2514.  
  2515.     return error;
  2516.  
  2517.   Fail:
  2518.     bdf_free_font( p->font );
  2519.  
  2520.     memory = extmemory;
  2521.  
  2522.     FT_FREE( p->font );
  2523.  
  2524.     goto Exit;
  2525.   }
  2526.  
  2527.  
  2528.   FT_LOCAL_DEF( void )
  2529.   bdf_free_font( bdf_font_t*  font )
  2530.   {
  2531.     bdf_property_t*  prop;
  2532.     unsigned long    i;
  2533.     bdf_glyph_t*     glyphs;
  2534.     FT_Memory        memory;
  2535.  
  2536.  
  2537.     if ( font == 0 )
  2538.       return;
  2539.  
  2540.     memory = font->memory;
  2541.  
  2542.     FT_FREE( font->name );
  2543.  
  2544.     /* Free up the internal hash table of property names. */
  2545.     if ( font->internal )
  2546.     {
  2547.       hash_free( (hashtable *)font->internal, memory );
  2548.       FT_FREE( font->internal );
  2549.     }
  2550.  
  2551.     /* Free up the comment info. */
  2552.     FT_FREE( font->comments );
  2553.  
  2554.     /* Free up the properties. */
  2555.     for ( i = 0; i < font->props_size; i++ )
  2556.     {
  2557.       if ( font->props[i].format == BDF_ATOM )
  2558.         FT_FREE( font->props[i].value.atom );
  2559.     }
  2560.  
  2561.     FT_FREE( font->props );
  2562.  
  2563.     /* Free up the character info. */
  2564.     for ( i = 0, glyphs = font->glyphs;
  2565.           i < font->glyphs_used; i++, glyphs++ )
  2566.     {
  2567.       FT_FREE( glyphs->name );
  2568.       FT_FREE( glyphs->bitmap );
  2569.     }
  2570.  
  2571.     for ( i = 0, glyphs = font->unencoded; i < font->unencoded_used;
  2572.           i++, glyphs++ )
  2573.     {
  2574.       FT_FREE( glyphs->name );
  2575.       FT_FREE( glyphs->bitmap );
  2576.     }
  2577.  
  2578.     FT_FREE( font->glyphs );
  2579.     FT_FREE( font->unencoded );
  2580.  
  2581.     /* Free up the overflow storage if it was used. */
  2582.     for ( i = 0, glyphs = font->overflow.glyphs;
  2583.           i < font->overflow.glyphs_used; i++, glyphs++ )
  2584.     {
  2585.       FT_FREE( glyphs->name );
  2586.       FT_FREE( glyphs->bitmap );
  2587.     }
  2588.  
  2589.     FT_FREE( font->overflow.glyphs );
  2590.  
  2591.     /* bdf_cleanup */
  2592.     hash_free( &(font->proptbl), memory );
  2593.  
  2594.     /* Free up the user defined properties. */
  2595.     for ( prop = font->user_props, i = 0;
  2596.           i < font->nuser_props; i++, prop++ )
  2597.     {
  2598.       FT_FREE( prop->name );
  2599.       if ( prop->format == BDF_ATOM )
  2600.         FT_FREE( prop->value.atom );
  2601.     }
  2602.  
  2603.     FT_FREE( font->user_props );
  2604.  
  2605.     /* FREE( font ); */ /* XXX Fixme */
  2606.   }
  2607.  
  2608.  
  2609.   FT_LOCAL_DEF( bdf_property_t * )
  2610.   bdf_get_font_property( bdf_font_t*  font,
  2611.                          const char*  name )
  2612.   {
  2613.     hashnode  hn;
  2614.  
  2615.  
  2616.     if ( font == 0 || font->props_size == 0 || name == 0 || *name == 0 )
  2617.       return 0;
  2618.  
  2619.     hn = hash_lookup( name, (hashtable *)font->internal );
  2620.  
  2621.     return hn ? ( font->props + hn->data ) : 0;
  2622.   }
  2623.  
  2624.  
  2625. /* END */
  2626.