home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / graphics-0.17 / xplot / alabel.c next >
Encoding:
C/C++ Source or Header  |  1991-03-11  |  7.2 KB  |  308 lines

  1. /* Copyright (C) 1989 Free Software Foundation, Inc.
  2.  
  3.    plot is distributed in the hope that it will be useful, but WITHOUT
  4.    ANY WARRANTY.  No author or distributor accepts responsibility to
  5.    anyone for the consequences of using it or for whether it serves any
  6.    particular purpose or works at all, unless he says so in writing.
  7.    Refer to the GNU General Public License for full details.
  8.  
  9.    Everyone is granted permission to copy, modify and redistribute
  10.    plot, but only under the conditions described in the GNU General
  11.    Public License.  A copy of this license is supposed to have been
  12.    given to you along with plot so you can know your rights and
  13.    responsibilities.  It should be in a file named COPYING.  Among
  14.    other things, the copyright notice and this notice must be preserved
  15.    on all copies.  */
  16.  
  17. #include "sys-defines.h"
  18.  
  19. #include <X11/Xlib.h>
  20. #include <X11/Intrinsic.h>
  21. #include <X11/StringDefs.h>
  22. #include <X11/Xaw/Label.h>
  23.  
  24. #include "libplot.h"
  25.  
  26. extern Display *dpy;
  27. extern Pixmap pixmap;
  28. extern GC gc;
  29.  
  30. #ifdef DEBUG
  31. extern int debug_flag;        /* nonzero means output debugging info. */
  32. #endif
  33. extern FILE *ostrm;        /* stream for error messages */
  34.  
  35. extern int xmin, xmax, ymin, ymax;
  36. extern double xfactor, yfactor;
  37.  
  38. #define MAPX(x) ((int) (((x) - xmin) * xfactor))
  39. #define MAPY(y) ((int) ((ymax - (y)) * yfactor))
  40.  
  41. double font_size = 14.;
  42. char *font_name = NULL;
  43. int font_name_length = 0;
  44. char *user_font_name = NULL;    /* user specified font name */
  45. int user_font_name_length = 0;
  46. char *x_name = NULL;        /* x font name */
  47. int x_name_length = 0;
  48.  
  49.  
  50. struct name_info_struct
  51. {
  52.   char *plot;
  53.   char *x;
  54. };
  55.  
  56. struct name_info_struct name_info[] = {
  57.   {
  58.     "courier-bold",
  59.     "courier-bold-r"},
  60.   {
  61.     "courier-boldoblique",
  62.     "courier-bold-o"},
  63.   {
  64.     "courier-oblique",
  65.     "courier-medium-o"},
  66.   {
  67.     "courier",
  68.     "courier-medium-r"},
  69.   {
  70.     "helvetica-bold",
  71.     "helvetica-bold-r"},
  72.   {
  73.     "helvetica-boldoblique",
  74.     "helvetica-bold-o"},
  75.   {
  76.     "helvetica-oblique",
  77.     "helvetica-medium-o"},
  78.   {
  79.     "helvetica",
  80.     "helvetica-medium-r"},
  81.   {
  82.     "symbol",
  83.     "symbol-medium-r"},
  84.   {
  85.     "times-bold",
  86.     "times-bold-r"},
  87.   {
  88.     "times-bolditalic",
  89.     "times-bold-i"},
  90.   {
  91.     "times-italic",
  92.     "times-medium-i"},
  93.   {
  94.     "times-roman",
  95.     "times-medium-r"},
  96.   {
  97.     NULL,
  98.     NULL}
  99. };
  100.  
  101. /* font structures necessary for finding the bounding boxes */
  102. XFontStruct *font_struct = NULL;
  103. int direction_return;
  104. int font_ascent_return, font_descent_return;
  105. XCharStruct overall_return;
  106.  
  107. int
  108. alabel (x_justify, y_justify, s)
  109.      char x_justify, y_justify;
  110.      char *s;
  111. {
  112.   int alabel_width, alabel_height, alabel_decent;
  113.   double x_char_offset, y_char_offset;
  114.  
  115.   switch (x_justify)        /* placement of label with respect
  116.                    to x coordinate */
  117.     {
  118.     case 'l':            /* left justified */
  119.       x_char_offset = 0.0;
  120.       break;
  121.  
  122.     case 'c':            /* centered */
  123.       x_char_offset = -0.5;
  124.       break;
  125.  
  126.     case 'r':            /* right justified */
  127.       x_char_offset = -1.0;
  128.       break;
  129.     }
  130.  
  131.   switch (y_justify)        /* placement of label with respect
  132.                    to y coordinate */
  133.     {
  134.     case 'b':            /* bottom */
  135.       y_char_offset = 0.0;
  136.       break;
  137.  
  138.     case 'c':            /* centered */
  139.       y_char_offset = 0.5;
  140.       break;
  141.  
  142.     case 't':            /* top */
  143.       y_char_offset = 1.0;
  144.       break;
  145.     }
  146.  
  147.   if (font_struct == NULL)
  148.     {
  149. #ifdef DEBUG
  150.       if (debug_flag)
  151.     fprintf(ostrm, "warning: null font_struct\n");
  152. #endif
  153.       alabel_width = alabel_height = alabel_decent = 0;
  154.     }
  155.   else
  156.     {
  157.       alabel_width = XTextWidth (font_struct, s, strlen (s));
  158.       XTextExtents (font_struct, s, strlen (s), &direction_return,
  159.             &font_ascent_return, &font_descent_return, &overall_return);
  160.       alabel_height = font_ascent_return + font_descent_return;
  161.       alabel_decent = font_descent_return;
  162.     }
  163.  
  164.   /* `+ .5' encourages round off rather than truncate by (int) cast. */
  165.   {
  166.     int x, y;
  167.     x = (int) (MAPX (last_x) + x_char_offset * alabel_width + .5);
  168.     y = (int) (MAPY (last_y) + y_char_offset * alabel_height - alabel_decent + .5);
  169. #ifdef DEBUG
  170.     if (debug_flag)
  171.       fprintf(ostrm, "drawstring %d %d %d %s\n", gc, x, y, s);
  172. #endif
  173.     XDrawString (dpy, pixmap, gc, x, y, s, strlen (s));
  174.   }
  175.   return 0;
  176. }
  177.  
  178. int set_font_and_size (name, size)
  179.      char *name;
  180.      int size;
  181. {
  182.   int name_size;
  183.   int i;
  184.  
  185. #ifdef DEBUG
  186.   if (debug_flag)
  187.     fprintf(ostrm, "fontname `%s' size %d\n", name ? name : "", size);
  188. #endif
  189.  
  190.   /* If the name is null or zero length, use the default: */
  191.  
  192.   if ((name == NULL)
  193.       || (strlen(name) == 0))
  194.     return set_font_and_size (XGetDefault (dpy, "xplot", "font"), 0);
  195.  
  196.   if (size == 0)
  197.     {
  198.       if (font_struct)
  199.     XFreeFont (dpy, font_struct);
  200.       font_struct = XLoadQueryFont (dpy, name);
  201.       if (font_struct)        /* if the font does exist */
  202.     {
  203.       XSetFont (dpy, gc, font_struct->fid);
  204. #ifdef DEBUG
  205.       if (debug_flag)
  206.         fprintf(ostrm, "Font `%s' %d\n", name, font_struct);
  207. #endif
  208.       return 1;        /* font found */
  209.     }
  210.       return 0;            /* font not found */
  211.     }
  212.  
  213.   /* Try buildin an x font name from the name and size: */
  214.  
  215.   /* save the user specified name for later use */
  216.   if (user_font_name_length < strlen (name) + 256)
  217.     {
  218.       user_font_name_length = strlen (name) + 256;
  219.       if (user_font_name)
  220.     user_font_name = (char *) realloc (user_font_name,
  221.                        user_font_name_length);
  222.       else
  223.     user_font_name = (char *) malloc (user_font_name_length);
  224.     }
  225.   strcpy (user_font_name, name);
  226.  
  227.  
  228.   /* search for the X font name correxponding to the requested name */
  229.   i = -1;
  230.   while (name_info[++i].plot)
  231.     {
  232.       if (strcmp (name_info[i].plot, name) == 0)
  233.     break;
  234.     }
  235.  
  236.   /* if the X font name is found, use it.  Otherwise, assume the supplied
  237.      name is an X font name itself. */
  238.   if (name_info[i].plot)
  239.     {
  240.       if (!x_name)
  241.     x_name = malloc (256);
  242.       sprintf (x_name, "*-%s-*-%d-*", name_info[i].x, (int) font_size);
  243.       if (set_font_and_size (x_name, 0))
  244.     return 1;
  245.     }
  246.   else
  247.     {
  248.       /* Try using the name as an X font name: */
  249.       if (set_font_and_size (name, 0))
  250.     return 1;
  251.     }
  252.  
  253.   set_font_and_size (0, 0);    /* last resort: punt and use the default. */
  254.   return 0;
  255. }
  256.  
  257. int
  258. fontname (name)
  259.      char *name;
  260. {
  261.   if (!set_font_and_size (name, (int) font_size))
  262.     fprintf(ostrm, "warning: font `%s' not found.\n", user_font_name);
  263.   return 0;
  264. }
  265.  
  266. int
  267. fontsize (points)
  268.      int points;
  269. {
  270.   font_size = points;
  271.   if (!set_font_and_size (user_font_name, (int) font_size))
  272.     fprintf(ostrm, "warning: font `%s' at size %d not found.\n",
  273.         user_font_name, points);
  274.   return 0;
  275. }
  276.  
  277. /* We cannot implement fill untill we implement saving at least
  278.    the last contiguous line drawn.  If we save the whole plot file
  279.    in a buffer, we could find the path by looking back after each 
  280.    move command.  Then we could implement fill by using XFillPolygon
  281.    on the last contiguous set of continue commands. */
  282.  
  283. int
  284. fill (pattern)
  285.      int pattern;
  286. {
  287.   return 0;
  288. }
  289.  
  290. /* Untill we have fill, color wont make much difference. */
  291.  
  292. int
  293. color (red, green, blue)
  294.      int red, green, blue;
  295. {
  296.   return 0;
  297. }
  298.  
  299. /* I don't know how we could rotate text yet.  Using InterViews is probably
  300.    an easier way of getting this implemented in a reasonable ammount of time. */
  301.  
  302. int
  303. rotate (angle, width, height)
  304.      int angle, width, height;
  305. {
  306.   return 0;
  307. }
  308.