home *** CD-ROM | disk | FTP | other *** search
/ PC Shareware 1999 March / PCShareware-3-99.iso / IMPLE / DJGPP.RAR / DJGPP2 / XLIB-SR0.ZIP / SRC / XLIBEMU / FONT.C < prev    next >
C/C++ Source or Header  |  1994-01-14  |  10KB  |  459 lines

  1. /* $Id: font.c 1.2 1994/01/15 02:09:03 ulrich Exp $ */
  2. /*
  3.  * font.c
  4.  *
  5.  * X library font handling functions.
  6.  */
  7. #include "Xlibemu.h"
  8. #include <stdio.h>
  9. #include <ctype.h>
  10.  
  11. extern GrTextOption _TextOpt;
  12.  
  13. #ifndef HAVE_STRICMP
  14. extern int strcasecmp (const char *, const char *);
  15. #define stricmp strcasecmp
  16. #endif
  17.  
  18. #include <grxfile.h>
  19. #include <grxfont.h>
  20.  
  21. typedef struct {
  22.   char file_name[20];
  23.   char *logical_name;
  24. } _WFontName;
  25.  
  26. _WFontName *_FontList = NULL;
  27. int _FontListSize = 0;
  28.  
  29. void
  30. _WInitFontList ()
  31. {
  32.   FILE *fp;
  33.   int n, list_size;
  34.   _WFontName *font_list;
  35.   char dir_name[120];
  36.   char buffer[200];
  37.   char *p;
  38.  
  39.   if (_FontList) return;
  40.  
  41.   /* scan xfonts.dir produced with \'grep "FONT.*$" *.fnt\' */
  42.  
  43.   GrSetFontPath (getenv ("GRXFONT"));
  44.   sprintf (dir_name,"%s/xfonts.dir", _GrFontPath);
  45.   fp = fopen (dir_name, "rt");
  46.   if (fp == NULL)
  47.     return;
  48.   
  49.   font_list = 0;
  50.   n = list_size = 0;
  51.  
  52.   while (fgets (buffer, 200, fp)) {
  53.     if (n >= list_size) {
  54.       list_size += list_size/2 + 1;
  55.       font_list = (_WFontName *)
  56.     Xrealloc (font_list, list_size * sizeof(_WFontName));
  57.     }
  58.     p = strchr (buffer, '\n');
  59.     if (p) *p = '\0';
  60.     p = strchr (buffer, ':');
  61.     if (! p) continue;
  62.     *p = 0;
  63.     strcpy (font_list[n].file_name, buffer);
  64.     font_list[n].logical_name = strdup (strlwr (p + strlen (":FONT ")));
  65. #if 0
  66.     fprintf (stderr, "%s: %s\n",
  67.          font_list[n].file_name,
  68.          font_list[n].logical_name);
  69. #endif
  70.     n++;
  71.   }
  72.   fclose (fp);
  73.   _FontList = font_list;
  74.   _FontListSize = n;
  75. }
  76.  
  77.  
  78. static int match_font(char *pattern, char *string)
  79. {
  80.   int nlit;
  81.   while (*pattern)
  82.   {
  83.     switch (*pattern)
  84.     {
  85.       case '*':
  86.         pattern++;
  87.         if (*pattern == 0)
  88.           return 1;
  89.         nlit=0;
  90.         while ((pattern[nlit] != 0)
  91.             && (pattern[nlit] != '*')
  92.             && (pattern[nlit] != '?') )
  93.           nlit++;
  94.         while (1)
  95.         {
  96.           if (strncmp(string, pattern, nlit) == 0)
  97.             break;
  98.           string++;
  99.           if (*string == 0)
  100.             return 0;
  101.         }
  102.         break;
  103.       case '?':
  104.         if (*string == 0)
  105.           return 0;
  106.         pattern++;
  107.         string++;
  108.         break;
  109.       default:
  110.         if (*pattern != *string)
  111.           return 0;
  112.         pattern++;
  113.         string++;
  114.         break;
  115.     }
  116.   }
  117.   if (*string)
  118.     return 0;
  119.   return 1;
  120. }
  121.  
  122. char **
  123. _WListFonts (const char *pattern, int max_count, int *count)
  124. {
  125.   int i, match_size, match_count;
  126.   char **match_list;
  127.   char *match_pattern;
  128.  
  129.   if (_FontListSize == 0)
  130.     _WInitFontList ();
  131.  
  132.   match_size = 0;
  133.   match_count = 0;
  134.   match_list = 0;
  135.  
  136. #if 0
  137.   fprintf (stderr, "list fonts %s\n", pattern);
  138. #endif
  139.  
  140.   match_pattern = (char *) alloca (strlen (pattern) + 1);
  141.   strcpy (match_pattern, pattern);
  142.   strlwr (match_pattern);
  143.  
  144.   for (i = 0; i < _FontListSize; i++) {
  145.     if (0 == match_font (match_pattern, _FontList[i].logical_name))
  146.       continue;
  147.     if (match_count >= max_count)
  148.       break;
  149.     if (match_count >= match_size) {
  150.       match_size += match_size/2 + 1;
  151.       match_list = (char **) 
  152.     Xrealloc (match_list, (match_size+1) * sizeof(char *));
  153.     }
  154.     match_list[match_count] = strdup (_FontList[i].file_name);
  155. #if 0
  156.     fprintf (stderr, "%d: %s\n", match_count, match_list[n]);
  157. #endif
  158.     match_count++;
  159.   }
  160.   if (match_count > 0)
  161.     match_list[match_count] = NULL;
  162.   *count = match_count;
  163.   return match_list;
  164. }
  165.  
  166. void
  167. _WFreeFontNames (char **name_table)
  168. {
  169.   char **p = name_table;
  170.  
  171.   if (! p) return;
  172.   while (*p) { free (*p); p++; }
  173.   free (name_table);
  174. }
  175.  
  176. Font
  177. XLoadFont(
  178.     Display*        display,
  179.     _Xconst char*    name)
  180. {
  181.   GrFont *font;
  182.   int width, height;
  183.   char pattern[200];
  184.  
  185.   if (isdigit (name[0])
  186.       && sscanf ((char *) name, "%dx%d", &width, &height) == 2) {
  187.     sprintf (pattern, "-*-clean-medium-r-normal--%d-*-*-*-c-%d-*-1", height, 10 * width);
  188.     name = pattern;
  189.   }
  190.   else if (! strcmp (name, "fixed")) {
  191.     width = 8;
  192.     height = 16;
  193.     sprintf (pattern, "-*-clean-medium-r-normal--%d-*-*-*-c-%d-*-1", height, 10 * width);
  194.     name = pattern;
  195.   }
  196.   if (strpbrk (name, "-*?")) {
  197.     int count;
  198.     char **list;
  199.  
  200.     list = _WListFonts (name, 1, &count);
  201.     if (count > 0) {
  202.       font = GrLoadFont (list[0]);
  203.       _WFreeFontNames (list);
  204.       if (font)
  205.     return font;
  206.     }
  207.   }
  208.  
  209.   font = GrLoadFont ((char *)name);
  210.   if (font == None) {
  211.     /* Load the default font */
  212.     font = GrLoadFont ("pc8x16");
  213.   }
  214.   if (font == None) {
  215.     _WError (display, None, BadName, 45 /* X_OpenFont */);
  216.   }
  217.   return font;
  218. }
  219.  
  220.  
  221.  
  222. XFontStruct *
  223. XLoadQueryFont(
  224.     Display*        display,
  225.     _Xconst char*    name)
  226. {
  227.   Font font;
  228.  
  229.   font = XLoadFont (display, name);
  230.   if (font == None)
  231.     return NULL;
  232.  
  233.   return XQueryFont (display, (XID) font);
  234. }
  235.  
  236. XFontStruct *
  237. XQueryFont(
  238.     Display*        display,
  239.     XID            font_ID)
  240. {
  241.   Font font = (Font) font_ID;
  242.   XFontStruct *fs;
  243.   int i, numchar, width, lbearing, rbearing, ascent, descent;
  244.  
  245.   if (font == None)
  246.     return NULL;
  247.  
  248.   fs = (XFontStruct *) Xmalloc (sizeof(XFontStruct));
  249.   if (fs == NULL) 
  250.     return NULL;
  251.  
  252.   fs->ext_data = NULL;
  253.   fs->fid = font;
  254.   fs->direction = 0;
  255.   fs->min_char_or_byte2 = font->fnt_minchar;
  256.   fs->max_char_or_byte2 = font->fnt_maxchar;
  257.   fs->min_byte1 = font->fnt_minchar;
  258.   fs->max_byte1 = font->fnt_maxchar;
  259.   fs->all_chars_exist = True;
  260.   fs->default_char = ' ';
  261.   fs->n_properties = 0;
  262.   fs->properties = NULL;
  263.  
  264.   width = font->fnt_width;
  265.   ascent = font->fnt_baseline;
  266.   descent = font->fnt_height - ascent;
  267.  
  268.   fs->ascent = ascent;
  269.   fs->descent = descent;
  270.  
  271.   if (font->fnt_isfixed) {
  272.     fs->min_bounds.lbearing = 0;
  273.     fs->min_bounds.rbearing = width;
  274.     fs->min_bounds.width = width;
  275.     fs->min_bounds.ascent = ascent;
  276.     fs->min_bounds.descent = descent;
  277.     fs->max_bounds = fs->min_bounds;
  278.     fs->per_char = NULL;
  279.   }
  280.   else {
  281.     numchar = font->fnt_maxchar - font->fnt_minchar + 1;
  282.     fs->per_char = (XCharStruct *) Xmalloc (numchar * sizeof (XCharStruct));
  283.  
  284.     fs->min_bounds.lbearing = 0;
  285.     fs->min_bounds.rbearing = width;
  286.     fs->min_bounds.width = width;
  287.     fs->min_bounds.ascent = ascent;
  288.     fs->min_bounds.descent = descent;
  289.     fs->max_bounds = fs->min_bounds;
  290.  
  291.     for (i = 0; i < numchar; i++) {
  292.       width = PFP(font)->pf_width[i];
  293.       lbearing = 0;
  294.       rbearing = width;
  295.       
  296.       fs->per_char[i].lbearing = lbearing;
  297.       fs->per_char[i].rbearing = rbearing;
  298.       fs->per_char[i].width = width;
  299.       fs->per_char[i].ascent = ascent;
  300.       fs->per_char[i].descent = descent;
  301.  
  302.       if (lbearing < fs->min_bounds.lbearing)
  303.     fs->min_bounds.lbearing = lbearing;
  304.       else if (lbearing > fs->max_bounds.lbearing)
  305.     fs->max_bounds.lbearing = lbearing;
  306.       if (rbearing < fs->min_bounds.rbearing)
  307.     fs->min_bounds.rbearing = rbearing;
  308.       else if (rbearing > fs->max_bounds.rbearing)
  309.     fs->max_bounds.lbearing = rbearing;
  310.       if (width < fs->min_bounds.width)
  311.     fs->min_bounds.width = width;
  312.       else if (width > fs->max_bounds.width)
  313.     fs->max_bounds.width = width;
  314.       if (ascent < fs->min_bounds.ascent)
  315.     fs->min_bounds.ascent = ascent;
  316.       else if (ascent > fs->max_bounds.ascent)
  317.     fs->max_bounds.ascent = ascent;
  318.       if (descent < fs->min_bounds.descent)
  319.     fs->min_bounds.ascent = descent;
  320.       else if (descent > fs->max_bounds.descent)
  321.     fs->max_bounds.ascent = descent;
  322.     }
  323.   }
  324.   return fs;
  325. }
  326.  
  327. int
  328. XFreeFont(
  329.     Display*        display,
  330.     XFontStruct*    font_struct)
  331. {
  332.   XFree (font_struct);
  333.   return 0;
  334. }
  335.  
  336. int
  337. XFreeFontInfo(
  338.     char**        names,
  339.     XFontStruct*    free_info,
  340.     int            actual_count)
  341. {
  342.   XFree (free_info);
  343.   return 0;
  344. }
  345.  
  346. int
  347. XTextExtents(
  348.     XFontStruct*    font_struct,
  349.     _Xconst char*    string,
  350.     int            nchars,
  351.     int*        direction_return,
  352.     int*        font_ascent_return,
  353.     int*        font_descent_return,
  354.     XCharStruct*    overall_return)
  355. {
  356.   int width, ascent, descent;
  357.   GrFont *font = font_struct->fid;
  358.  
  359.   ascent = font->fnt_baseline;
  360.   descent = font->fnt_height - ascent;
  361.  
  362.   *direction_return = font_struct->direction;
  363.   *font_ascent_return = ascent;
  364.   *font_descent_return = descent;
  365.   if (nchars > 0 && string != NULL) {
  366.     _TextOpt.txo_font = font;
  367.     width = GrStringWidth ((char *)string, nchars, &_TextOpt);
  368.     overall_return->lbearing = 0;
  369.     overall_return->rbearing = width;
  370.     overall_return->width = width;
  371.     overall_return->ascent = ascent;
  372.     overall_return->descent = descent;
  373.   }
  374.   return Success;
  375. }
  376.  
  377. int
  378. XTextExtents16(
  379.     XFontStruct*    font_struct,
  380.     _Xconst XChar2b*    string,
  381.     int            nchars,
  382.     int*        direction_return,
  383.     int*        font_ascent_return,
  384.     int*        font_descent_return,
  385.     XCharStruct*    overall_return)
  386. {
  387.   return 0;
  388. }
  389.  
  390. int
  391. XTextWidth(
  392.     XFontStruct*    font_struct,
  393.     _Xconst char*    string,
  394.     int            count
  395. )
  396. {
  397.   _TextOpt.txo_font = font_struct->fid;
  398.   return GrStringWidth ((char *)string, count, &_TextOpt);
  399. }
  400.  
  401. int
  402. XTextWidth16(
  403.     XFontStruct*    font_struct,
  404.     _Xconst XChar2b*    string,
  405.     int            count)
  406. {
  407.   return 0;
  408. }
  409.  
  410.  
  411. char **XListFonts(
  412.     Display*        display,
  413.     _Xconst char*    pattern,
  414.     int            maxnames,
  415.     int*        actual_count_return)
  416. {
  417.   char **list;
  418.   int count;
  419.  
  420.   list = _WListFonts (pattern, maxnames, &count);
  421.   *actual_count_return = count;
  422.   return list;
  423. }
  424.  
  425.  
  426. char **XListFontsWithInfo(
  427.     Display*        display,
  428.     _Xconst char*    pattern,
  429.     int            maxnames,
  430.     int*        count_return,
  431.     XFontStruct**    info_return)
  432. {
  433.   int i, count;
  434.   XFontStruct *font_array, *fs;
  435.   char **name_array;
  436.  
  437.   name_array = XListFonts (display, pattern, maxnames, count_return);
  438.   count = *count_return;
  439.   if (count > 0) {
  440.  
  441.     font_array = (XFontStruct *) Xmalloc (count * sizeof(XFontStruct));
  442.     for (i = 0; i < count; i++) {
  443.       fs = XLoadQueryFont (display, name_array[i]);
  444.       font_array[i] = *fs;
  445.       XFreeFont (display, fs);
  446.     }
  447.     *info_return = font_array;
  448.   }
  449.   return name_array;
  450. }
  451.  
  452. int XFreeFontNames(
  453.     char**        list)
  454. {
  455.   _WFreeFontNames (list);
  456.   return 0;
  457. }
  458.  
  459.