mtextbox.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Function: multi_text_box_path, multi_lines
  2. **
  3. ** Author: Paul W. Carlson March 1992
  4. */
  5. #include "ps_info.h"
  6. #define LEFT 0
  7. #define RIGHT 1
  8. #define LOWER 0
  9. #define UPPER 1
  10. #define CENTER 2
  11. int multi_text_box_path(double x, double y,
  12. int xref, int yref, char *text, int fontsize,
  13. float rotate)
  14. {
  15. int numlines;
  16. char *ptr;
  17. static int firsttime = 1;
  18. /* write procs if first time called */
  19. if (firsttime) {
  20. firsttime = 0;
  21. /* procs to get box for multiple text lines */
  22. fprintf(PS.fp, "/CMX {l sub r l sub D2 sub} BD\n");
  23. fprintf(PS.fp, "/CMY {t sub t b sub D2 add} BD\n");
  24. fprintf(PS.fp, "/LMX {l sub} BD\n");
  25. fprintf(PS.fp, "/LMY {b sub} BD\n");
  26. fprintf(PS.fp, "/RMX {r sub} BD\n");
  27. fprintf(PS.fp, "/UMY {t sub} BD\n");
  28. fprintf(PS.fp, "/MTBX {/y dely def\n");
  29. fprintf(PS.fp, "0 1 nlm1 { /i exch def\n");
  30. fprintf(PS.fp, "newpath /y y dely sub def\n");
  31. fprintf(PS.fp, "0 y moveto ta i get\n");
  32. fprintf(PS.fp, "false charpath flattenpath pathbbox\n");
  33. fprintf(PS.fp, "/tt XD /rr XD /bb XD /ll XD\n");
  34. fprintf(PS.fp, "tt t gt {/t tt def} if rr r gt {/r rr def} if\n");
  35. fprintf(PS.fp, "bb b lt {/b bb def} if ll l lt {/l ll def} if\n");
  36. fprintf(PS.fp, "} for\n");
  37. fprintf(PS.fp, "/t t mg add def /r r mg add def \n");
  38. fprintf(PS.fp, "/b b mg sub def /l l mg sub def} BD\n");
  39. fprintf(PS.fp, "/TBM {l b r t B} BD\n");
  40. /* proc to draw multiple text lines in text color */
  41. fprintf(PS.fp, "/DMT {/y dely def 0 1 nlm1 {\n");
  42. fprintf(PS.fp, "/i exch def /y y dely sub def\n");
  43. fprintf(PS.fp, "0 y moveto ta i get show } for grestore} BD\n");
  44. /* proc to draw multiple text lines in highlight color */
  45. fprintf(PS.fp, "/DMH {/y dely def 0 1 nlm1 {\n");
  46. fprintf(PS.fp, "/i exch def /y y dely sub def\n");
  47. fprintf(PS.fp, "newpath 0 y moveto ta i get\n");
  48. fprintf(PS.fp, "false charpath stroke} for} BD\n");
  49. }
  50. /* put text into array */
  51. numlines = 0;
  52. ptr = text;
  53. fprintf(PS.fp, "/ta [ (");
  54. while (*ptr) {
  55. if (*ptr == '\\' && *(ptr + 1) == 'n') {
  56. fprintf(PS.fp, ")\n(");
  57. ptr++;
  58. numlines++;
  59. }
  60. else
  61. fprintf(PS.fp, "%c", *ptr);
  62. ptr++;
  63. }
  64. fprintf(PS.fp, ") ] def\n");
  65. numlines++;
  66. /*initialize PostScript variables */
  67. fprintf(PS.fp, "/t -9999 def /r -9999 def /b 9999 def /l 9999 def\n");
  68. fprintf(PS.fp, "/dely %d def /nlm1 %d def\n", fontsize, numlines - 1);
  69. /* get relative box coordinates */
  70. fprintf(PS.fp, "MTBX\n");
  71. /* set box x coordinate */
  72. fprintf(PS.fp, "%.2f ", x);
  73. /* set box y coordinate */
  74. fprintf(PS.fp, " %.2f ", y);
  75. fprintf(PS.fp, "gsave TR %.2f rotate ", rotate);
  76. fprintf(PS.fp, " 0 ");
  77. switch (xref) {
  78. case LEFT:
  79. fprintf(PS.fp, "LMX");
  80. break;
  81. case RIGHT:
  82. fprintf(PS.fp, "RMX");
  83. break;
  84. case CENTER:
  85. default:
  86. fprintf(PS.fp, "CMX");
  87. break;
  88. }
  89. fprintf(PS.fp, " 0 ");
  90. switch (yref) {
  91. case UPPER:
  92. fprintf(PS.fp, "UMY");
  93. break;
  94. case LOWER:
  95. fprintf(PS.fp, "LMY");
  96. break;
  97. case CENTER:
  98. default:
  99. fprintf(PS.fp, "CMY");
  100. break;
  101. }
  102. fprintf(PS.fp, " TR TBM\n");
  103. return 0;
  104. }
  105. int multi_lines(char *text)
  106. {
  107. char *ptr;
  108. ptr = text;
  109. while (*ptr) {
  110. if (*ptr == '\\' && *(ptr + 1) == 'n')
  111. return 1;
  112. ptr++;
  113. }
  114. return 0;
  115. }