paren.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 "e.h"
  10. #define abs(x) ((x) > 0 ? (x) : (-(x)))
  11. extern void brack(int, char *, char *, char *);
  12. void paren(int leftc, int p1, int rightc)
  13. {
  14. int n, m, j;
  15. double h1, b1;
  16. double v, bv; /* v = shift of inside, bv = shift of brackets */
  17. extern double Parenbase, Parenshift, Parenheight;
  18. bv = ttype == DEVPOST ? Parenshift : 0; /* move brackets down this much */
  19. h1 = eht[p1];
  20. b1 = ebase[p1];
  21. yyval = p1;
  22. lfont[yyval] = rfont[yyval] = 0;
  23. n = REL(h1,ps) + 0.99; /* ceiling */
  24. if (n < 2)
  25. n = 1;
  26. m = n - 2;
  27. if (leftc == '{' || rightc == '}') {
  28. n = n%2 ? n : ++n;
  29. if (n < 3)
  30. n = 3;
  31. m = n-3;
  32. }
  33. eht[yyval] = EM((double) n + Parenheight, ps);
  34. ebase[yyval] = eht[yyval]/2 - EM(Parenbase, ps);
  35. /* try to cope with things that are badly centered */
  36. /* (top heavy or bottom heavy) */
  37. if (abs(h1/2 - b1) >= EM(0.5, ps))
  38. v = REL(-ebase[yyval] + (eht[yyval]-h1)/2 + b1, ps);
  39. else
  40. v = 0; /* don't shift it at all */
  41. printf(".ds %d \\^", yyval); /* was \| */
  42. if (bv)
  43. printf("\\v'%gm'", bv);
  44. switch (leftc) {
  45. case 'n': /* nothing */
  46. case '\0':
  47. break;
  48. case 'f': /* floor */
  49. if (n <= 1)
  50. printf("\\(lf");
  51. else
  52. brack(m, "\\(bv", "\\(bv", "\\(lf");
  53. break;
  54. case 'c': /* ceiling */
  55. if (n <= 1)
  56. printf("\\(lc");
  57. else
  58. brack(m, "\\(lc", "\\(bv", "\\(bv");
  59. break;
  60. case '{':
  61. printf("\\b'\\(lt");
  62. for(j = 0; j < m; j += 2) printf("\\(bv");
  63. printf("\\(lk");
  64. for(j = 0; j < m; j += 2) printf("\\(bv");
  65. printf("\\(lb'");
  66. break;
  67. case '(':
  68. brack(m, "\\(lt", "\\(bv", "\\(lb");
  69. break;
  70. case '[':
  71. brack(m, "\\(lc", "\\(bv", "\\(lf");
  72. break;
  73. case '|':
  74. brack(m, "|", "|", "|");
  75. break;
  76. default:
  77. brack(m, (char *) &leftc, (char *) &leftc,
  78. (char *) &leftc);
  79. break;
  80. }
  81. if (bv)
  82. printf("\\v'%gm'", -bv);
  83. if (v)
  84. printf("\\v'%gm'\\*(%d\\v'%gm'", -v, p1, v);
  85. else
  86. printf("\\*(%d", p1);
  87. if (rightc) {
  88. if (bv)
  89. printf("\\v'%gm'", bv);
  90. switch (rightc) {
  91. case 'f': /* floor */
  92. if (n <= 1)
  93. printf("\\(rf");
  94. else
  95. brack(m, "\\(bv", "\\(bv", "\\(rf");
  96. break;
  97. case 'c': /* ceiling */
  98. if (n <= 1)
  99. printf("\\(rc");
  100. else
  101. brack(m, "\\(rc", "\\(bv", "\\(bv");
  102. break;
  103. case '}':
  104. printf("\\b'\\(rt");
  105. for(j = 0; j < m; j += 2) printf("\\(bv");
  106. printf("\\(rk");
  107. for(j = 0; j < m; j += 2) printf("\\(bv");
  108. printf("\\(rb'");
  109. break;
  110. case ']':
  111. brack(m, "\\(rc", "\\(bv", "\\(rf");
  112. break;
  113. case ')':
  114. brack(m, "\\(rt", "\\(bv", "\\(rb");
  115. break;
  116. case '|':
  117. brack(m, "|", "|", "|");
  118. break;
  119. default:
  120. brack(m, (char *) &rightc, (char *) &rightc,
  121. (char *) &rightc);
  122. break;
  123. }
  124. if (bv)
  125. printf("\\v'%gm'", -bv);
  126. }
  127. printf("\n");
  128. dprintf(".\tcurly: h=%g b=%g n=%d v=%g l=%c, r=%c\n",
  129. eht[yyval], ebase[yyval], n, v, leftc, rightc);
  130. }
  131. void brack(int m, char *t, char *c, char *b)
  132. {
  133. int j;
  134. printf("\\b'%s", t);
  135. for( j=0; j < m; j++)
  136. printf("%s", c);
  137. printf("%s'", b);
  138. }