comment.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Functions: commentfile, do_comments
  2. **
  3. ** Function commentfile is an extended version of the p.map function.
  4. **
  5. ** Author: Paul W. Carlson April 1992
  6. */
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include "comment.h"
  10. #include "local_proto.h"
  11. #define KEY(x) (strcmp(key,x)==0)
  12. static char *help1[] = {
  13. "where x y",
  14. "font fontname",
  15. "fontsize fontsize",
  16. "color color",
  17. ""
  18. };
  19. static char *help2[] = {
  20. "enter comments, line by line",
  21. ""
  22. };
  23. int read_comment(char *name)
  24. {
  25. char buf[1024];
  26. char *key, *data;
  27. FILE *in, *out;
  28. int need_blank;
  29. int color, fontsize;
  30. double x, y;
  31. fontsize = 0;
  32. color = BLACK;
  33. x = y = 0.0;
  34. while (input(2, buf, help1)) {
  35. if (!key_data(buf, &key, &data))
  36. continue;
  37. if (KEY("where")) {
  38. if (sscanf(data, "%lf %lf", &x, &y) != 2) {
  39. x = y = 0.0;
  40. error(key, data, "illegal where request");
  41. }
  42. else
  43. continue;
  44. }
  45. if (KEY("fontsize")) {
  46. fontsize = atoi(data);
  47. if (fontsize < 4 || fontsize > 50)
  48. fontsize = 0;
  49. continue;
  50. }
  51. if (KEY("color")) {
  52. color = get_color_number(data);
  53. if (color < 0) {
  54. color = BLACK;
  55. error(key, data, "illegal color request");
  56. }
  57. continue;
  58. }
  59. if (KEY("font")) {
  60. get_font(data);
  61. cmt.font = G_store(data);
  62. continue;
  63. }
  64. error(key, data, "illegal comment sub-request");
  65. }
  66. cmt.x = x;
  67. cmt.y = y;
  68. cmt.color = color;
  69. if (fontsize)
  70. cmt.fontsize = fontsize;
  71. in = NULL;
  72. if (*name) {
  73. in = fopen(name, "r");
  74. if (in == NULL) {
  75. error("comment file", name, "can't open");
  76. return 1;
  77. }
  78. }
  79. if (PS.commentfile == NULL) {
  80. PS.commentfile = G_tempfile();
  81. need_blank = 0;
  82. if ((out = fopen(PS.commentfile, "w")) != NULL)
  83. fclose(out);
  84. }
  85. else
  86. need_blank = 1;
  87. out = fopen(PS.commentfile, "a");
  88. if (out == NULL) {
  89. error("can't create a comments file", "", "");
  90. if (in == NULL)
  91. gobble_input();
  92. else
  93. fclose(in);
  94. return 1;
  95. }
  96. if (in == NULL)
  97. while (input(2, buf, help2)) {
  98. if (need_blank) {
  99. fprintf(out, "\n");
  100. need_blank = 0;
  101. }
  102. /* G_strip(buf); */
  103. fprintf(out, "%s\n", buf);
  104. }
  105. else {
  106. while (G_getl2(buf, sizeof buf, in)) {
  107. if (need_blank) {
  108. fprintf(out, "\n");
  109. need_blank = 0;
  110. }
  111. /* G_strip(buf); */
  112. fprintf(out, "%s\n", buf);
  113. }
  114. fclose(in);
  115. }
  116. fclose(out);
  117. return 0;
  118. }
  119. int do_comment(void)
  120. {
  121. FILE *fp;
  122. char text[1024];
  123. double x, y, dy, fontsize;
  124. /* set font */
  125. fontsize = (double)cmt.fontsize;
  126. fprintf(PS.fp, "(%s) FN %.1f SF\n", cmt.font, fontsize);
  127. /* set start of first line */
  128. dy = 1.2 * fontsize;
  129. y = 72.0 * (PS.page_height - cmt.y);
  130. if (cmt.y > PS.page_height)
  131. y = PS.min_y - dy;
  132. x = 72.0 * PS.left_marg + 1.5;
  133. if (72.0 * cmt.x > x)
  134. x = 72.0 * cmt.x;
  135. /* read the comment file */
  136. if ((fp = fopen(PS.commentfile, "r")) == NULL) {
  137. error("comment file", PS.commentfile, "can't open");
  138. return 1;
  139. }
  140. while (G_getl2(text, sizeof text, fp)) {
  141. /* G_strip(text); */
  142. if (*text)
  143. show_text(x, y, text);
  144. y -= dy;
  145. }
  146. fclose(fp);
  147. y -= 0.25 * dy;
  148. if (PS.min_y > y)
  149. PS.min_y = y;
  150. return 0;
  151. }