nethercote | 5912c81 | 2004-02-15 15:38:08 +0000 | [diff] [blame] | 1 | /* This file is part of hp2ps, a graph drawer for memory profiles. |
| 2 | Copyright (C) 2002 The University Court of the University of Glasgow. |
| 3 | This program is governed by the license contained in the file LICENSE. */ |
| 4 | |
nethercote | c9f3692 | 2004-02-14 16:40:02 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <string.h> |
| 7 | #include "Main.h" |
| 8 | #include "Defines.h" |
| 9 | #include "Dimensions.h" |
| 10 | #include "Curves.h" |
| 11 | #include "HpFile.h" |
| 12 | #include "Axes.h" |
| 13 | #include "Key.h" |
| 14 | #include "Marks.h" |
| 15 | #include "Utilities.h" |
| 16 | |
| 17 | /* own stuff */ |
| 18 | #include "PsFile.h" |
| 19 | |
| 20 | static void Prologue PROTO((void)); /* forward */ |
| 21 | static void Variables PROTO((void)); /* forward */ |
| 22 | static void BorderOutlineBox PROTO((void)); /* forward */ |
| 23 | static void BigTitleOutlineBox PROTO((void)); /* forward */ |
| 24 | static void TitleOutlineBox PROTO((void)); /* forward */ |
| 25 | static void BigTitleText PROTO((void)); /* forward */ |
| 26 | static void TitleText PROTO((void)); /* forward */ |
| 27 | |
| 28 | void |
| 29 | PutPsFile() |
| 30 | { |
| 31 | Prologue(); |
| 32 | Variables(); |
| 33 | BorderOutlineBox(); |
| 34 | |
| 35 | if (bflag) { |
| 36 | BigTitleOutlineBox(); |
| 37 | BigTitleText(); |
| 38 | } else { |
| 39 | TitleOutlineBox(); |
| 40 | TitleText(); |
| 41 | } |
| 42 | |
| 43 | CurvesInit(); |
| 44 | |
| 45 | Axes(); |
| 46 | |
| 47 | if (TWENTY) Key(); |
| 48 | |
| 49 | Curves(); |
| 50 | |
| 51 | if (!yflag) Marks(); |
| 52 | |
| 53 | fprintf(psfp, "showpage\n"); |
| 54 | } |
| 55 | |
| 56 | |
| 57 | static void StandardSpecialComments PROTO((void)); /* forward */ |
| 58 | static void EPSFSpecialComments PROTO((floatish)); /* forward */ |
| 59 | static void Landscape PROTO((void)); /* forward */ |
| 60 | static void Portrait PROTO((void)); /* forward */ |
| 61 | static void Scaling PROTO((floatish)); /* forward */ |
| 62 | |
| 63 | static void |
| 64 | Prologue() |
| 65 | { |
| 66 | if (eflag) { |
| 67 | floatish epsfscale = epsfwidth / (floatish) borderwidth; |
| 68 | EPSFSpecialComments(epsfscale); |
| 69 | Scaling(epsfscale); |
| 70 | } else { |
| 71 | StandardSpecialComments(); |
| 72 | if (gflag) Portrait(); else Landscape(); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | extern char *jobstring; |
| 77 | extern char *datestring; |
| 78 | |
| 79 | static void |
| 80 | StandardSpecialComments() |
| 81 | { |
| 82 | fprintf(psfp, "%%!PS-Adobe-2.0\n"); |
| 83 | fprintf(psfp, "%%%%Title: %s\n", jobstring); |
| 84 | fprintf(psfp, "%%%%Creator: %s (version %s)\n", programname, VERSION); |
| 85 | fprintf(psfp, "%%%%CreationDate: %s\n", datestring); |
| 86 | fprintf(psfp, "%%%%EndComments\n"); |
| 87 | } |
| 88 | |
| 89 | static void |
| 90 | EPSFSpecialComments(epsfscale) |
| 91 | floatish epsfscale; |
| 92 | { |
| 93 | fprintf(psfp, "%%!PS-Adobe-2.0\n"); |
| 94 | fprintf(psfp, "%%%%Title: %s\n", jobstring); |
| 95 | fprintf(psfp, "%%%%Creator: %s (version %s)\n", programname, VERSION); |
| 96 | fprintf(psfp, "%%%%CreationDate: %s\n", datestring); |
| 97 | fprintf(psfp, "%%%%BoundingBox: 0 0 %d %d\n", |
| 98 | (int) (borderwidth * epsfscale + 0.5), |
| 99 | (int) (borderheight * epsfscale + 0.5) ); |
| 100 | fprintf(psfp, "%%%%EndComments\n"); |
| 101 | } |
| 102 | |
| 103 | |
| 104 | |
| 105 | static void |
| 106 | Landscape() |
| 107 | { |
| 108 | fprintf(psfp, "-90 rotate\n"); |
| 109 | fprintf(psfp, "%f %f translate\n", -(borderwidth + (floatish) START_Y), |
| 110 | (floatish) START_X); |
| 111 | } |
| 112 | |
| 113 | static void |
| 114 | Portrait() |
| 115 | { |
| 116 | fprintf(psfp, "%f %f translate\n", (floatish) START_X, (floatish) START_Y); |
| 117 | } |
| 118 | |
| 119 | static void |
| 120 | Scaling(epsfscale) |
| 121 | floatish epsfscale; |
| 122 | { |
| 123 | fprintf(psfp, "%f %f scale\n", epsfscale, epsfscale); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static void |
| 128 | Variables() |
| 129 | { |
| 130 | fprintf(psfp, "/HE%d /Helvetica findfont %d scalefont def\n", |
| 131 | NORMAL_FONT, NORMAL_FONT); |
| 132 | |
| 133 | fprintf(psfp, "/HE%d /Helvetica findfont %d scalefont def\n", |
| 134 | LARGE_FONT, LARGE_FONT); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | static void |
| 139 | BorderOutlineBox() |
| 140 | { |
| 141 | fprintf(psfp, "newpath\n"); |
| 142 | fprintf(psfp, "0 0 moveto\n"); |
| 143 | fprintf(psfp, "0 %f rlineto\n", borderheight); |
| 144 | fprintf(psfp, "%f 0 rlineto\n", borderwidth); |
| 145 | fprintf(psfp, "0 %f rlineto\n", -borderheight); |
| 146 | fprintf(psfp, "closepath\n"); |
| 147 | fprintf(psfp, "%f setlinewidth\n", borderthick); |
| 148 | fprintf(psfp, "stroke\n"); |
| 149 | } |
| 150 | |
| 151 | static void |
| 152 | BigTitleOutlineBox() |
| 153 | { |
| 154 | fprintf(psfp, "newpath\n"); |
| 155 | fprintf(psfp, "%f %f moveto\n", borderspace, |
| 156 | borderheight - titleheight - borderspace); |
| 157 | fprintf(psfp, "0 %f rlineto\n", titleheight); |
| 158 | fprintf(psfp, "%f 0 rlineto\n", titlewidth); |
| 159 | fprintf(psfp, "0 %f rlineto\n", -titleheight); |
| 160 | fprintf(psfp, "closepath\n"); |
| 161 | fprintf(psfp, "%f setlinewidth\n", borderthick); |
| 162 | fprintf(psfp, "stroke\n"); |
| 163 | |
| 164 | fprintf(psfp, "%f %f moveto\n", borderspace, |
| 165 | borderheight - titleheight / 2 - borderspace); |
| 166 | fprintf(psfp, "%f 0 rlineto\n", titlewidth); |
| 167 | fprintf(psfp, "stroke\n"); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | static void |
| 172 | TitleOutlineBox() |
| 173 | { |
| 174 | fprintf(psfp, "newpath\n"); |
| 175 | fprintf(psfp, "%f %f moveto\n", borderspace, |
| 176 | borderheight - titleheight - borderspace); |
| 177 | fprintf(psfp, "0 %f rlineto\n", titleheight); |
| 178 | fprintf(psfp, "%f 0 rlineto\n", titlewidth); |
| 179 | fprintf(psfp, "0 %f rlineto\n", -titleheight); |
| 180 | fprintf(psfp, "closepath\n"); |
| 181 | fprintf(psfp, "%f setlinewidth\n", borderthick); |
| 182 | fprintf(psfp, "stroke\n"); |
| 183 | } |
| 184 | |
| 185 | static void EscapePrint PROTO((char *, int)); /* forward */ |
| 186 | |
| 187 | static void |
| 188 | BigTitleText() |
| 189 | { |
| 190 | floatish x, y; |
| 191 | |
| 192 | x = borderspace + titletextspace; |
| 193 | y = borderheight - titleheight / 2 - borderspace + titletextspace; |
| 194 | |
| 195 | /* job identifier goes on top at the far left */ |
| 196 | |
| 197 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 198 | fprintf(psfp, "%f %f moveto\n", x, y); |
| 199 | fputc('(', psfp); |
| 200 | EscapePrint(jobstring, BIG_JOB_STRING_WIDTH); |
| 201 | fprintf(psfp, ") show\n"); |
| 202 | |
| 203 | y = borderheight - titleheight - borderspace + titletextspace; |
| 204 | |
| 205 | /* area below curve gows at the botton, far left */ |
| 206 | |
| 207 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 208 | fprintf(psfp, "%f %f moveto\n", x, y); |
| 209 | fputc('(', psfp); |
| 210 | CommaPrint(psfp, (intish)areabelow); |
| 211 | fprintf(psfp, " %s x %s)\n", valueunitstring, sampleunitstring); |
| 212 | fprintf(psfp, "show\n"); |
| 213 | |
| 214 | /* date goes at far right */ |
| 215 | |
| 216 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 217 | fprintf(psfp, "(%s)\n", datestring); |
| 218 | fprintf(psfp, "dup stringwidth pop\n"); |
| 219 | fprintf(psfp, "%f\n", (titlewidth + borderspace) - titletextspace); |
| 220 | fprintf(psfp, "exch sub\n"); |
| 221 | fprintf(psfp, "%f moveto\n", y); |
| 222 | fprintf(psfp, "show\n"); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | static void |
| 227 | TitleText() |
| 228 | { |
| 229 | floatish x, y; |
| 230 | |
| 231 | x = borderspace + titletextspace; |
| 232 | y = borderheight - titleheight - borderspace + titletextspace; |
| 233 | |
| 234 | /* job identifier goes at far left */ |
| 235 | |
| 236 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 237 | fprintf(psfp, "%f %f moveto\n", x, y); |
| 238 | fputc('(', psfp); |
| 239 | EscapePrint(jobstring, SMALL_JOB_STRING_WIDTH); |
| 240 | fprintf(psfp, ") show\n"); |
| 241 | |
| 242 | /* area below curve is centered */ |
| 243 | |
| 244 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 245 | fputc('(', psfp); |
| 246 | CommaPrint(psfp, (intish) areabelow); |
| 247 | fprintf(psfp, " %s x %s)\n", valueunitstring, sampleunitstring); |
| 248 | |
| 249 | fprintf(psfp, "dup stringwidth pop\n"); |
| 250 | fprintf(psfp, "2 div\n"); |
| 251 | fprintf(psfp, "%f\n", titlewidth / 2); |
| 252 | fprintf(psfp, "exch sub\n"); |
| 253 | fprintf(psfp, "%f moveto\n", y); |
| 254 | fprintf(psfp, "show\n"); |
| 255 | |
| 256 | /* date goes at far right */ |
| 257 | |
| 258 | fprintf(psfp, "HE%d setfont\n", TITLE_TEXT_FONT); |
| 259 | fprintf(psfp, "(%s)\n", datestring); |
| 260 | fprintf(psfp, "dup stringwidth pop\n"); |
| 261 | fprintf(psfp, "%f\n", (titlewidth + borderspace) - titletextspace); |
| 262 | fprintf(psfp, "exch sub\n"); |
| 263 | fprintf(psfp, "%f moveto\n", y); |
| 264 | fprintf(psfp, "show\n"); |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * Print a string s in width w, escaping characters where necessary. |
| 269 | */ |
| 270 | |
| 271 | static void |
| 272 | EscapePrint(s,w) |
| 273 | char* s; int w; |
| 274 | { |
| 275 | for ( ; *s && w > 0; s++, w--) { |
| 276 | if (*s == '(') { /* escape required */ |
| 277 | fputc('\\', psfp); |
| 278 | } else if (*s == ')') { |
| 279 | fputc('\\', psfp); |
| 280 | } |
| 281 | |
| 282 | fputc(*s, psfp); |
| 283 | } |
| 284 | } |