do_header.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /* Functions: do_map_header, read_header_file
  2. **
  3. ** Author: Paul W. Carlson April 1992
  4. */
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <grass/raster.h>
  8. #include "header.h"
  9. #include "local_proto.h"
  10. static double x, y, dy, fontsize;
  11. static void append(char, char *);
  12. static void apply(const char *, const char *, char *);
  13. static char *get_format(char *, char *);
  14. static int output(char *, const char *);
  15. int do_map_header(const char *date)
  16. {
  17. char temp[100];
  18. /* set color and font */
  19. set_ps_color(&hdr.color);
  20. fontsize = (double)hdr.fontsize;
  21. fprintf(PS.fp, "(%s) FN %.1f SF\n", hdr.font, fontsize);
  22. /* set start of first line */
  23. dy = 1.2 * fontsize;
  24. y = 72.0 * (PS.page_height - PS.top_marg) - fontsize - 1.5;
  25. if (hdr.fp == NULL) {
  26. if (PS.celltitle[0]) {
  27. fprintf(PS.fp, "/t (TITLE: %s) def\n", PS.celltitle);
  28. fprintf(PS.fp, "t SW pop %.1f XS D2 t exch %.1f MS\n",
  29. 72 * PS.page_width, y);
  30. y -= dy;
  31. }
  32. strcpy(temp, G_myname());
  33. G_strip(temp);
  34. if (*temp == 0)
  35. strcpy(temp, G_location());
  36. fprintf(PS.fp, "/t (LOCATION: %s) def\n", temp);
  37. fprintf(PS.fp, "t SW pop %.1f XS D2 t exch %.1f MS\n",
  38. 72 * PS.page_width, y);
  39. y -= 0.25 * dy;
  40. if (PS.min_y > y)
  41. PS.min_y = y;
  42. return 0;
  43. }
  44. x = 72.0 * PS.left_marg + 1.5;
  45. read_header_file(date);
  46. y -= 0.25 * dy;
  47. if (PS.min_y > y)
  48. PS.min_y = y;
  49. return 0;
  50. }
  51. int read_header_file(const char *date)
  52. {
  53. char buf[1024];
  54. while (G_getl2(buf, sizeof buf, hdr.fp))
  55. output(buf, date);
  56. fclose(hdr.fp);
  57. return 0;
  58. }
  59. static int output(char *line, const char *date)
  60. {
  61. char text[1024];
  62. char fmt[30];
  63. char *buf;
  64. buf = line;
  65. *text = 0;
  66. while (*buf) {
  67. if (*buf == '%') {
  68. buf++;
  69. if (*buf == '%')
  70. strcat(text, "%");
  71. else if (*buf == 'n') {
  72. if (*text)
  73. show_text(x, y, text);
  74. y -= dy;
  75. return 0;
  76. }
  77. else if (*buf == '_') {
  78. fprintf(PS.fp, "BW ");
  79. draw_line(x, y + 0.2 * fontsize,
  80. 72.0 * (PS.page_width - PS.right_marg),
  81. y + 0.2 * fontsize);
  82. y -= dy;
  83. set_ps_color(&hdr.color);
  84. return 0;
  85. }
  86. else {
  87. buf = get_format(buf, fmt);
  88. append('s', fmt);
  89. switch (*buf) {
  90. case 'd':
  91. apply(date, fmt, text);
  92. break;
  93. case 'l':
  94. apply(G_location(), fmt, text);
  95. break;
  96. case 'L':
  97. apply(G_myname(), fmt, text);
  98. break;
  99. case 'c':
  100. if (PS.cell_fd >= 0) {
  101. char name[100];
  102. sprintf(name, "<%s> in mapset <%s>",
  103. PS.cell_name, PS.cell_mapset);
  104. apply(name, fmt, text);
  105. }
  106. else
  107. apply("none", fmt, text);
  108. break;
  109. case 'm':
  110. apply(G_mapset(), fmt, text);
  111. break;
  112. case 'u':
  113. apply(G_whoami(), fmt, text);
  114. break;
  115. case 'x':
  116. apply(Rast_mask_info(), fmt, text);
  117. break;
  118. case 0:
  119. continue;
  120. }
  121. }
  122. }
  123. else
  124. append(*buf, text);
  125. buf++;
  126. }
  127. if (*text)
  128. show_text(x, y, text);
  129. y -= dy;
  130. return 0;
  131. }
  132. static char *get_format(char *buf, char *fmt)
  133. {
  134. strcpy(fmt, "%");
  135. if (*buf == '-') {
  136. append(*buf++, fmt);
  137. if (*buf < '1' || *buf > '9')
  138. return (buf - 1);
  139. }
  140. while (*buf >= '0' && *buf <= '9')
  141. append(*buf++, fmt);
  142. return buf;
  143. }
  144. static void append(char c, char *buf)
  145. {
  146. while (*buf)
  147. buf++;
  148. *buf++ = c;
  149. *buf = 0;
  150. }
  151. static void apply(const char *buf, const char *fmt, char *text)
  152. {
  153. char temp[300];
  154. sprintf(temp, fmt, buf);
  155. strcat(text, temp);
  156. }