URI:
       tlabel.c - plan9port - [fork] Plan 9 from user space
  HTML git clone git://src.adamsgaard.dk/plan9port
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tlabel.c (3043B)
       ---
            1 #include <stdio.h>
            2 #include <string.h>
            3 #include "grap.h"
            4 #include "y.tab.h"
            5 
            6 int        pointsize        = 10;        /* assumed pointsize to start */
            7 int        ps_set                = 0;        /* someone has set pointsize explicitly */
            8 
            9 double        textht        = 1.0/6.0;        /* 6 lines/inch */
           10 double        textwid = 1;                /* width of text box for vertical */
           11 
           12 double        lab_up        = 0.0;                /* extra motion for label */
           13 double        lab_rt        = 0.0;                /* extra motion for label */
           14 double        lab_wid        = 0.0;                /* override default width computation */
           15 
           16 void labelwid(double amt)
           17 {
           18         lab_wid = amt + .00001;
           19 }
           20 
           21 void labelmove(int dir, double amt)        /* record direction & motion of position corr */
           22 {
           23         switch (dir) {
           24         case UP:        lab_up += amt; break;
           25         case DOWN:        lab_up -= amt; break;
           26         case LEFT:        lab_rt -= amt; break;
           27         case RIGHT:        lab_rt += amt; break;
           28         }
           29 }
           30 
           31 void label(int label_side, Attr *stringlist)        /* stick label on label_side */
           32 {
           33         int m;
           34         Attr *ap;
           35 
           36         fprintf(tfd, "\ttextht = %g\n", textht);
           37         if (lab_wid != 0.0) {
           38                 fprintf(tfd, "\ttextwid = %g\n", lab_wid);
           39                 lab_wid = 0;
           40         } else if (label_side == LEFT || label_side == RIGHT) {
           41                 textwid = 0;
           42                 for (ap = stringlist; ap != NULL; ap = ap->next)
           43                         if ((m = strlen(ap->sval)) > textwid)
           44                                 textwid = m;
           45                 textwid /= 15;        /* estimate width at 15 chars/inch */
           46                 fprintf(tfd, "\ttextwid = %g\n", textwid);
           47         }
           48         fprintf(tfd, "Label:\t%s", slprint(stringlist));
           49         freeattr(stringlist);
           50         switch (label_side) {
           51         case BOT:
           52         case 0:
           53                 fprintf(tfd, " with .n at Frame.s - (0,2 * textht)");
           54                 break;
           55         case LEFT:
           56                 fprintf(tfd, " wid textwid with .e at Frame.w - (0.2,0)");
           57                 break;
           58         case RIGHT:
           59                 fprintf(tfd, " wid textwid with .w at Frame.e + (0.2,0)");
           60                 break;
           61         case TOP:
           62                 fprintf(tfd, " with .s at Frame.n + (0,2 * textht)");
           63                 break;
           64         }
           65         lab_adjust();
           66         fprintf(tfd, "\n");
           67         label_side = BOT;
           68 }
           69 
           70 void lab_adjust(void)        /* add a string to adjust labels, ticks, etc. */
           71 {
           72         if (lab_up != 0.0 || lab_rt != 0.0)
           73                 fprintf(tfd, " + (%g,%g)", lab_rt, lab_up);
           74 }
           75 
           76 char *sizeit(Attr *ap)                /* add \s..\s to ap->sval */
           77 {
           78         int n;
           79         static char buf[1000];
           80 
           81         if (!ap->op) {        /* no explicit size command */
           82                 if (ps_set) {
           83                         sprintf(buf, "\\s%d%s\\s0", pointsize, ap->sval);
           84                         return buf;
           85                 } else
           86                         return ap->sval;
           87         } else if (!ps_set) {        /* explicit size but no global size */
           88                 n = (int) ap->fval;
           89                 switch (ap->op) {
           90                 case ' ':        /* absolute size */
           91                         sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
           92                         break;
           93                 case '+':        /* better be only one digit! */
           94                         sprintf(buf, "\\s+%d%s\\s-%d", n, ap->sval, n);
           95                         break;
           96                 case '-':
           97                         sprintf(buf, "\\s-%d%s\\s+%d", n, ap->sval, n);
           98                         break;
           99                 case '*':
          100                 case '/':
          101                         return ap->sval;        /* ignore for now */
          102                 }
          103                 return buf;
          104         } else {
          105                 /* explicit size and a global background size */
          106                 n = (int) ap->fval;
          107                 switch (ap->op) {
          108                 case ' ':        /* absolute size */
          109                         sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
          110                         break;
          111                 case '+':
          112                         sprintf(buf, "\\s%d%s\\s0", pointsize+n, ap->sval);
          113                         break;
          114                 case '-':
          115                         sprintf(buf, "\\s%d%s\\s0", pointsize-n, ap->sval);
          116                         break;
          117                 case '*':
          118                 case '/':
          119                         return ap->sval;        /* ignore for now */
          120                 }
          121                 return buf;
          122         }
          123 }