home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / X / mit / lib / X / XFontInfo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-29  |  6.8 KB  |  236 lines

  1. /* $XConsortium: XFontInfo.c,v 11.21 91/01/29 08:40:25 rws Exp $ */
  2. /* Copyright    Massachusetts Institute of Technology    1986    */
  3.  
  4. /*
  5. Permission to use, copy, modify, distribute, and sell this software and its
  6. documentation for any purpose is hereby granted without fee, provided that
  7. the above copyright notice appear in all copies and that both that
  8. copyright notice and this permission notice appear in supporting
  9. documentation, and that the name of M.I.T. not be used in advertising or
  10. publicity pertaining to distribution of the software without specific,
  11. written prior permission.  M.I.T. makes no representations about the
  12. suitability of this software for any purpose.  It is provided "as is"
  13. without express or implied warranty.
  14. */
  15.  
  16. #define NEED_REPLIES
  17. #include "Xlibint.h"
  18.  
  19. #if NeedFunctionPrototypes
  20. char **XListFontsWithInfo(
  21. register Display *dpy,
  22. _Xconst char *pattern,  /* null-terminated */
  23. int maxNames,
  24. int *actualCount,    /* RETURN */
  25. XFontStruct **info)    /* RETURN */
  26. #else
  27. char **XListFontsWithInfo(dpy, pattern, maxNames, actualCount, info)
  28. register Display *dpy;
  29. char *pattern;  /* null-terminated */
  30. int maxNames;
  31. int *actualCount;    /* RETURN */
  32. XFontStruct **info;    /* RETURN */
  33. #endif
  34. {       
  35.     register long nbytes;
  36.     register int i;
  37.     register XFontStruct *fs;
  38.     register int size = 0;
  39.     XFontStruct *finfo = NULL;
  40.     char **flist = NULL;
  41.     xListFontsWithInfoReply reply;
  42.     register xListFontsReq *req;
  43.     int j;
  44.  
  45.     LockDisplay(dpy);
  46.     GetReq(ListFontsWithInfo, req);
  47.     req->maxNames = maxNames;
  48.     nbytes = req->nbytes = pattern ? strlen (pattern) : 0;
  49.     req->length += (nbytes + 3) >> 2;
  50.     _XSend (dpy, pattern, nbytes);
  51.     /* use _XSend instead of Data, since subsequent _XReply will flush buffer */
  52.  
  53.     for (i = 0; ; i++) {
  54.     if (!_XReply (dpy, (xReply *) &reply,
  55.               ((SIZEOF(xListFontsWithInfoReply) - 
  56.             SIZEOF(xGenericReply)) >> 2), xFalse)) {
  57.         for (j=(i-1); (j >= 0); j--) {
  58.         Xfree(flist[j]);
  59.         if (finfo[j].properties) Xfree((char *) finfo[j].properties);
  60.         }
  61.         if (flist) Xfree((char *) flist);
  62.         if (finfo) Xfree((char *) finfo);
  63.         UnlockDisplay(dpy);
  64.         SyncHandle();
  65.         return ((char **) NULL);
  66.     }
  67.     if (reply.nameLength == 0)
  68.         break;
  69.     if ((i + reply.nReplies) >= size) {
  70.         size = i + reply.nReplies + 1;
  71.  
  72.         if (finfo) {
  73.         XFontStruct * tmp_finfo = (XFontStruct *) 
  74.             Xrealloc ((char *) finfo,
  75.                   (unsigned) (sizeof(XFontStruct) * size));
  76.         char ** tmp_flist = (char **)
  77.             Xrealloc ((char *) flist,
  78.                   (unsigned) (sizeof(char *) * (size+1)));
  79.  
  80.         if ((! tmp_finfo) || (! tmp_flist)) {
  81.             /* free all the memory that we allocated */
  82.             for (j=(i-1); (j >= 0); j--) {
  83.             Xfree(flist[j]);
  84.             if (finfo[j].properties)
  85.                 Xfree((char *) finfo[j].properties);
  86.             }
  87.             if (tmp_flist) Xfree((char *) tmp_flist);
  88.             else Xfree((char *) flist);
  89.             if (tmp_finfo) Xfree((char *) tmp_finfo);
  90.             else Xfree((char *) finfo);
  91.             goto clearwire;
  92.         }
  93.         finfo = tmp_finfo;
  94.         flist = tmp_flist;
  95.         }
  96.         else {
  97.         if (! (finfo = (XFontStruct *)
  98.                Xmalloc((unsigned) (sizeof(XFontStruct) * size))))
  99.             goto clearwire;
  100.         if (! (flist = (char **)
  101.                Xmalloc((unsigned) (sizeof(char *) * (size+1))))) {
  102.             Xfree((char *) finfo);
  103.             goto clearwire;
  104.         }
  105.         }
  106.     }
  107.     fs = &finfo[i];
  108.  
  109.     fs->ext_data         = NULL;
  110.     fs->per_char        = NULL;
  111.     fs->fid         = None;
  112.     fs->direction         = reply.drawDirection;
  113.     fs->min_char_or_byte2    = reply.minCharOrByte2;
  114.     fs->max_char_or_byte2     = reply.maxCharOrByte2;
  115.     fs->min_byte1         = reply.minByte1;
  116.     fs->max_byte1         = reply.maxByte1;
  117.     fs->default_char    = reply.defaultChar;
  118.     fs->all_chars_exist     = reply.allCharsExist;
  119.     fs->ascent         = cvtINT16toInt (reply.fontAscent);
  120.     fs->descent         = cvtINT16toInt (reply.fontDescent);
  121.     
  122. #ifdef MUSTCOPY
  123.     {
  124.         xCharInfo *xcip;
  125.  
  126.         xcip = (xCharInfo *) &reply.minBounds;
  127.         fs->min_bounds.lbearing = xcip->leftSideBearing;
  128.         fs->min_bounds.rbearing = xcip->rightSideBearing;
  129.         fs->min_bounds.width = xcip->characterWidth;
  130.         fs->min_bounds.ascent = xcip->ascent;
  131.         fs->min_bounds.descent = xcip->descent;
  132.         fs->min_bounds.attributes = xcip->attributes;
  133.  
  134.         xcip = (xCharInfo *) &reply.maxBounds;
  135.         fs->max_bounds.lbearing = xcip->leftSideBearing;
  136.         fs->max_bounds.rbearing = xcip->rightSideBearing;
  137.         fs->max_bounds.width = xcip->characterWidth;
  138.         fs->max_bounds.ascent = xcip->ascent;
  139.         fs->max_bounds.descent = xcip->descent;
  140.         fs->max_bounds.attributes = xcip->attributes;
  141.     }
  142. #else
  143.     /* XXX the next two statements won't work if short isn't 16 bits */
  144.     fs->min_bounds = * (XCharStruct *) &reply.minBounds;
  145.     fs->max_bounds = * (XCharStruct *) &reply.maxBounds;
  146. #endif /* MUSTCOPY */
  147.  
  148.     fs->n_properties = reply.nFontProps;
  149.     if (fs->n_properties > 0) {
  150.         nbytes = reply.nFontProps * sizeof(XFontProp);
  151.         if (! (fs->properties = (XFontProp *) Xmalloc((unsigned) nbytes)))
  152.         goto badmem;
  153.         nbytes = reply.nFontProps * SIZEOF(xFontProp);
  154.         _XRead32 (dpy, (char *)fs->properties, nbytes);
  155.  
  156.     } else
  157.         fs->properties = NULL;
  158.  
  159.     j = reply.nameLength + 1;
  160.     if (!i)
  161.         j++; /* make first string 1 byte longer, to match XListFonts */
  162.     flist[i] = (char *) Xmalloc ((unsigned int) j);
  163.     if (! flist[i]) {
  164.         if (finfo[i].properties) Xfree((char *) finfo[i].properties);
  165.         nbytes = reply.nameLength + 3 & ~3;
  166.         _XEatData(dpy, (unsigned long) nbytes);
  167.         goto badmem;
  168.     }
  169.     if (!i) {
  170.         *flist[0] = 0; /* zero to distinguish from XListFonts */
  171.         flist[0]++;
  172.     }
  173.     flist[i][reply.nameLength] = '\0';
  174.     _XReadPad (dpy, flist[i], (long) reply.nameLength);
  175.     }
  176.     *info = finfo;
  177.     *actualCount = i;
  178.     if (flist)
  179.     flist[i] = NULL; /* required in case XFreeFontNames is called */
  180.     UnlockDisplay(dpy);
  181.     SyncHandle();
  182.     return (flist);
  183.  
  184.  
  185.   badmem:
  186.     /* Free all memory allocated by this function. */
  187.     for (j=(i-1); (j >= 0); j--) {
  188.     Xfree(flist[j]);
  189.     if (finfo[j].properties) Xfree((char *) finfo[j].properties);
  190.     }
  191.     if (flist) Xfree((char *) flist);
  192.     if (finfo) Xfree((char *) finfo);
  193.  
  194.   clearwire:
  195.     /* Clear the wire. */
  196.     do {
  197.     if (reply.nFontProps)
  198.         _XEatData(dpy, (unsigned long)
  199.               (reply.nFontProps * SIZEOF(xFontProp)));
  200.     nbytes = (reply.nameLength + 3) & ~3;
  201.     _XEatData(dpy, (unsigned long) nbytes);
  202.     }
  203.     while (_XReply(dpy,(xReply *) &reply, ((SIZEOF(xListFontsWithInfoReply) -
  204.                         SIZEOF(xGenericReply)) >> 2),
  205.            xFalse) && (reply.nameLength != 0));
  206.  
  207.     UnlockDisplay(dpy);
  208.     SyncHandle();
  209.     return (char **) NULL;
  210. }
  211.  
  212.  
  213. XFreeFontInfo (names, info, actualCount)
  214. char **names;
  215. XFontStruct *info;
  216. int actualCount;
  217. {
  218.     register int i;
  219.     if (names) {
  220.         Xfree (names[0]-1);
  221.         for (i = 1; i < actualCount; i++) {
  222.             Xfree (names[i]);
  223.         }
  224.         Xfree((char *) names);
  225.     }
  226.     if (info) {
  227.         for (i = 0; i < actualCount; i++) {
  228.             if (info[i].per_char)
  229.                 Xfree ((char *) info[i].per_char);
  230.             if (info[i].properties)
  231.                 Xfree ((char *) info[i].properties);
  232.             }
  233.         Xfree((char *) info);
  234.     }
  235. }
  236.