home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / fonts / lib / font / bitmap / bdfutils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-21  |  7.4 KB  |  332 lines

  1. /************************************************************************
  2. Copyright 1989 by Digital Equipment Corporation, Maynard, Massachusetts,
  3. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Digital or MIT not be
  12. used in advertising or publicity pertaining to distribution of the
  13. software without specific, written prior permission.
  14.  
  15. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  16. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  17. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  18. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  19. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  20. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  21. SOFTWARE.
  22.  
  23. ************************************************************************/
  24.  
  25. /* $XConsortium: bdfutils.c,v 1.6 92/04/16 09:20:57 gildea Exp $ */
  26.  
  27. #include <ctype.h>
  28.  
  29. #include <stdio.h>
  30. #include "fontfilest.h"
  31. #include "fontstruct.h"
  32. /* use bitmap structure */
  33. #include "bitmap.h"
  34. #include "bdfint.h"
  35.  
  36. /***====================================================================***/
  37.  
  38. void
  39. bdfError(message, a0, a1, a2, a3, a4, a5)
  40.     char       *message;
  41.     pointer     a0,
  42.                 a1,
  43.                 a2,
  44.                 a3,
  45.                 a4,
  46.                 a5;
  47. {
  48.     fprintf(stderr, "BDF Error: ");
  49.     fprintf(stderr, message, a0, a1, a2, a3, a4, a5);
  50. }
  51.  
  52. /***====================================================================***/
  53.  
  54. void
  55. bdfWarning(message, a0, a1, a2, a3, a4, a5)
  56.     char       *message;
  57.     pointer     a0,
  58.                 a1,
  59.                 a2,
  60.                 a3,
  61.                 a4,
  62.                 a5;
  63. {
  64.     fprintf(stderr, "BDF Warning: ");
  65.     fprintf(stderr, message, a0, a1, a2, a3, a4, a5);
  66. }
  67.  
  68. /***====================================================================***/
  69.  
  70. /*ARGSUSED*/
  71. void
  72. bdfInformation(pFile, message, a0, a1, a2, a3, a4, a5)
  73.     char       *message;
  74.     pointer     a0,
  75.                 a1,
  76.                 a2,
  77.                 a3,
  78.                 a4,
  79.                 a5;
  80. {
  81.     fprintf(stderr, "BDF Information: ");
  82.     fprintf(stderr, message, a0, a1, a2, a3, a4, a5);
  83. }
  84.  
  85. /***====================================================================***/
  86.  
  87. /*
  88.  * read the next (non-comment) line and keep a count for error messages.
  89.  * Returns buf, or NULL if EOF.
  90.  */
  91.  
  92. unsigned char *
  93. bdfGetLine(file, buf, len)
  94.     FontFilePtr file;
  95.     unsigned char *buf;
  96.     int         len;
  97. {
  98.     int         c;
  99.     unsigned char *b;
  100.  
  101.     for (;;) {
  102.     b = buf;
  103.     while ((c = FontFileGetc(file)) != FontFileEOF) {
  104.         if (c == '\r')
  105.         continue;
  106.         if (c == '\n')
  107.         break;
  108.         if (b - buf >= (len - 1))
  109.         break;
  110.         *b++ = c;
  111.     }
  112.     *b = '\0';
  113.     if (c == FontFileEOF)
  114.         return NULL;
  115.     if (b != buf && !bdfIsPrefix(buf, "COMMENT"))
  116.         break;
  117.     }
  118.     return buf;
  119. }
  120.  
  121. /***====================================================================***/
  122.  
  123. Atom
  124. bdfForceMakeAtom(str, size)
  125.     register char *str;
  126.     register int *size;
  127. {
  128.     register int len = strlen(str);
  129.     extern Atom    MakeAtom();
  130.  
  131.     if (size != NULL)
  132.     *size += len + 1;
  133.     return MakeAtom(str, len, TRUE);
  134. }
  135.  
  136. /***====================================================================***/
  137.  
  138. /*
  139.  * Handle quoted strings.
  140.  */
  141.  
  142. Atom
  143. bdfGetPropertyValue(s)
  144.     char       *s;
  145. {
  146.     register char *p,
  147.                *pp;
  148.     char *orig_s = s;
  149.     Atom        atom;
  150.  
  151.     /* strip leading white space */
  152.     while (*s && (*s == ' ' || *s == '\t'))
  153.     s++;
  154.     if (*s == 0) {
  155.     return bdfForceMakeAtom(s, NULL);
  156.     }
  157.     if (*s != '"') {
  158.     pp = s;
  159.     /* no white space in value */
  160.     for (pp = s; *pp; pp++)
  161.         if (*pp == ' ' || *pp == '\t' || *pp == '\015' || *pp == '\n') {
  162.         *pp = 0;
  163.         break;
  164.         }
  165.     return bdfForceMakeAtom(s, NULL);
  166.     }
  167.     /* quoted string: strip outer quotes and undouble inner quotes */
  168.     s++;
  169.     pp = p = (char *) xalloc((unsigned) strlen(s) + 1);
  170.     while (*s) {
  171.     if (*s == '"') {
  172.         if (*(s + 1) != '"') {
  173.         *p++ = 0;
  174.         atom = bdfForceMakeAtom(pp, NULL);
  175.         xfree(pp);
  176.         return atom;
  177.         } else {
  178.         s++;
  179.         }
  180.     }
  181.     *p++ = *s++;
  182.     }
  183.     xfree (pp);
  184.     bdfError("unterminated quoted string property: %s\n", orig_s);
  185.     return None;
  186. }
  187.  
  188. /***====================================================================***/
  189.  
  190. /*
  191.  * return TRUE if string is a valid integer
  192.  */
  193. int
  194. bdfIsInteger(str)
  195.     char       *str;
  196. {
  197.     char        c;
  198.  
  199.     c = *str++;
  200.     if (!(isdigit(c) || c == '-' || c == '+'))
  201.     return (FALSE);
  202.  
  203.     while (c = *str++)
  204.     if (!isdigit(c))
  205.         return (FALSE);
  206.  
  207.     return (TRUE);
  208. }
  209.  
  210. /***====================================================================***/
  211.  
  212. /*
  213.  * make a byte from the first two hex characters in glyph picture
  214.  */
  215.  
  216. unsigned char
  217. bdfHexByte(s)
  218.     char       *s;
  219. {
  220.     unsigned char b = 0;
  221.     register char c;
  222.     int         i;
  223.  
  224.     for (i = 2; i; i--) {
  225.     c = *s++;
  226.     if ((c >= '0') && (c <= '9'))
  227.         b = (b << 4) + (c - '0');
  228.     else if ((c >= 'A') && (c <= 'F'))
  229.         b = (b << 4) + 10 + (c - 'A');
  230.     else if ((c >= 'a') && (c <= 'f'))
  231.         b = (b << 4) + 10 + (c - 'a');
  232.     else
  233.         bdfError("bad hex char '%c'", c);
  234.     }
  235.     return b;
  236. }
  237.  
  238. /***====================================================================***/
  239.  
  240. /*
  241.  * check for known special property values
  242.  */
  243.  
  244. static char *SpecialAtoms[] = {
  245.     "FONT_ASCENT",
  246. #define BDF_FONT_ASCENT    0
  247.     "FONT_DESCENT",
  248. #define BDF_FONT_DESCENT 1
  249.     "DEFAULT_CHAR",
  250. #define BDF_DEFAULT_CHAR 2
  251.     "POINT_SIZE",
  252. #define BDF_POINT_SIZE 3
  253.     "RESOLUTION",
  254. #define BDF_RESOLUTION 4
  255.     "X_HEIGHT",
  256. #define BDF_X_HEIGHT 5
  257.     "WEIGHT",
  258. #define BDF_WEIGHT 6
  259.     "QUAD_WIDTH",
  260. #define BDF_QUAD_WIDTH 7
  261.     "FONT",
  262. #define BDF_FONT 8
  263.     "RESOLUTION_X",
  264. #define BDF_RESOLUTION_X 9
  265.     "RESOLUTION_Y",
  266. #define BDF_RESOLUTION_Y 10
  267.     0,
  268. };
  269.  
  270. Bool
  271. bdfSpecialProperty(pFont, prop, isString, bdfState)
  272.     FontPtr     pFont;
  273.     FontPropPtr prop;
  274.     char        isString;
  275.     bdfFileState *bdfState;
  276. {
  277.     char      **special;
  278.     char       *name;
  279.  
  280.     name = NameForAtom(prop->name);
  281.     for (special = SpecialAtoms; *special; special++)
  282.     if (!strcmp(name, *special))
  283.         break;
  284.  
  285.     switch (special - SpecialAtoms) {
  286.     case BDF_FONT_ASCENT:
  287.     if (!isString) {
  288.         pFont->info.fontAscent = prop->value;
  289.         bdfState->haveFontAscent = TRUE;
  290.     }
  291.     return TRUE;
  292.     case BDF_FONT_DESCENT:
  293.     if (!isString) {
  294.         pFont->info.fontDescent = prop->value;
  295.         bdfState->haveFontDescent = TRUE;
  296.     }
  297.     return TRUE;
  298.     case BDF_DEFAULT_CHAR:
  299.     if (!isString) {
  300.         pFont->info.defaultCh = prop->value;
  301.         bdfState->haveDefaultCh = TRUE;
  302.     }
  303.     return TRUE;
  304.     case BDF_POINT_SIZE:
  305.     bdfState->pointSizeProp = prop;
  306.     return FALSE;
  307.     case BDF_RESOLUTION:
  308.     bdfState->resolutionProp = prop;
  309.     return FALSE;
  310.     case BDF_X_HEIGHT:
  311.     bdfState->xHeightProp = prop;
  312.     return FALSE;
  313.     case BDF_WEIGHT:
  314.     bdfState->weightProp = prop;
  315.     return FALSE;
  316.     case BDF_QUAD_WIDTH:
  317.     bdfState->quadWidthProp = prop;
  318.     return FALSE;
  319.     case BDF_FONT:
  320.     bdfState->fontProp = prop;
  321.     return FALSE;
  322.     case BDF_RESOLUTION_X:
  323.     bdfState->resolutionXProp = prop;
  324.     return FALSE;
  325.     case BDF_RESOLUTION_Y:
  326.     bdfState->resolutionYProp = prop;
  327.     return FALSE;
  328.     default:
  329.     return FALSE;
  330.     }
  331. }
  332.