label.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include "grap.h"
  12. #include "y.tab.h"
  13. int pointsize = 10; /* assumed pointsize to start */
  14. int ps_set = 0; /* someone has set pointsize explicitly */
  15. double textht = 1.0/6.0; /* 6 lines/inch */
  16. double textwid = 1; /* width of text box for vertical */
  17. double lab_up = 0.0; /* extra motion for label */
  18. double lab_rt = 0.0; /* extra motion for label */
  19. double lab_wid = 0.0; /* override default width computation */
  20. void labelwid(double amt)
  21. {
  22. lab_wid = amt + .00001;
  23. }
  24. void labelmove(int dir, double amt) /* record direction & motion of position corr */
  25. {
  26. switch (dir) {
  27. case UP: lab_up += amt; break;
  28. case DOWN: lab_up -= amt; break;
  29. case LEFT: lab_rt -= amt; break;
  30. case RIGHT: lab_rt += amt; break;
  31. }
  32. }
  33. void label(int label_side, Attr *stringlist) /* stick label on label_side */
  34. {
  35. int m;
  36. Attr *ap;
  37. fprintf(tfd, "\ttextht = %g\n", textht);
  38. if (lab_wid != 0.0) {
  39. fprintf(tfd, "\ttextwid = %g\n", lab_wid);
  40. lab_wid = 0;
  41. } else if (label_side == LEFT || label_side == RIGHT) {
  42. textwid = 0;
  43. for (ap = stringlist; ap != NULL; ap = ap->next)
  44. if ((m = strlen(ap->sval)) > textwid)
  45. textwid = m;
  46. textwid /= 15; /* estimate width at 15 chars/inch */
  47. fprintf(tfd, "\ttextwid = %g\n", textwid);
  48. }
  49. fprintf(tfd, "Label:\t%s", slprint(stringlist));
  50. freeattr(stringlist);
  51. switch (label_side) {
  52. case BOT:
  53. case 0:
  54. fprintf(tfd, " with .n at Frame.s - (0,2 * textht)");
  55. break;
  56. case LEFT:
  57. fprintf(tfd, " wid textwid with .e at Frame.w - (0.2,0)");
  58. break;
  59. case RIGHT:
  60. fprintf(tfd, " wid textwid with .w at Frame.e + (0.2,0)");
  61. break;
  62. case TOP:
  63. fprintf(tfd, " with .s at Frame.n + (0,2 * textht)");
  64. break;
  65. }
  66. lab_adjust();
  67. fprintf(tfd, "\n");
  68. label_side = BOT;
  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. char *sizeit(Attr *ap) /* add \s..\s to ap->sval */
  76. {
  77. int n;
  78. static char buf[1000];
  79. if (!ap->op) { /* no explicit size command */
  80. if (ps_set) {
  81. sprintf(buf, "\\s%d%s\\s0", pointsize, ap->sval);
  82. return buf;
  83. } else
  84. return ap->sval;
  85. } else if (!ps_set) { /* explicit size but no global size */
  86. n = (int) ap->fval;
  87. switch (ap->op) {
  88. case ' ': /* absolute size */
  89. sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
  90. break;
  91. case '+': /* better be only one digit! */
  92. sprintf(buf, "\\s+%d%s\\s-%d", n, ap->sval, n);
  93. break;
  94. case '-':
  95. sprintf(buf, "\\s-%d%s\\s+%d", n, ap->sval, n);
  96. break;
  97. case '*':
  98. case '/':
  99. return ap->sval; /* ignore for now */
  100. }
  101. return buf;
  102. } else {
  103. /* explicit size and a global background size */
  104. n = (int) ap->fval;
  105. switch (ap->op) {
  106. case ' ': /* absolute size */
  107. sprintf(buf, "\\s%d%s\\s0", n, ap->sval);
  108. break;
  109. case '+':
  110. sprintf(buf, "\\s%d%s\\s0", pointsize+n, ap->sval);
  111. break;
  112. case '-':
  113. sprintf(buf, "\\s%d%s\\s0", pointsize-n, ap->sval);
  114. break;
  115. case '*':
  116. case '/':
  117. return ap->sval; /* ignore for now */
  118. }
  119. return buf;
  120. }
  121. }