1 /*
2     Gri - A language for scientific graphics programming
3     Copyright (C) 2008 Daniel Kelley
4 
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; version 3 of the License, or
8     (at your option) any later version.
9 
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14 
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19 
20 // BUG: change the "n" and "S" in symbol drawing later.  gr.cc:gr_drawsymbol
21 #if !defined(_postscpt_)
22 
23 #define _postscpt_
24 
25 #define PS_break_path           "stroke newpath\n%.1f %.1f m\n"
26 #define	PS_closepath		"h\n"
27 #define	PS_comment		"%%%s\n"
28 #define	PS_fill			"F\n"
29 #define	PS_fillrect		"%.1f %.1f %.1f %.1f fr\n"
30 #define	PS_lineto		"%.1f %.1f l\n"
31 #define	PS_rlineto		"%.1f %.1f rl\n"
32 #define	PS_moveto		"%.1f %.1f m\n"
33 #define	PS_rmoveto		"%.1f %.1f rm\n"
34 #define	PS_weak_newpath		"n\n"
35 #define	PS_newpath		"newpath\n"
36 #define	PS_setgray		"%.3f g\n"
37 #define	PS_setlinewidth		"%.3f w\n"
38 #define	PS_showc		"(%c) sh\n"
39 #define	PS_showpage		"showpage\n"
40 #define	PS_stroke		"S\n"
41 
42 // Commands to retire, when get a chance to recode next:
43 // rl rm ALL TEXT COMMANDS
44 const static char *PS_dict[] =
45 {
46 	"% NOTE: The Gri postscript dictionary is being converted to the Adobe",
47 	"% Illustrator 3.0 dialect of PostScript, as described in the Adobe",
48 	"% documents stored at URL",
49 	"%   http://www.adobe.com/Support/TechNotes.html",
50 	"% (as of Jan 1996, this doc is number 5007).  When the conversion",
51 	"% is complete, the Adobe Illustrator drawing program -- and any",
52 	"% program compatible with AI -- will be able to edit Gri output.",
53 	"%",
54 	"% The IslandDraw (TM) program is able to read Gri output",
55 	"% at this time; remarkably, it can read/edit arbitrary PostScript.",
56 	"%",
57 	"% The definitions below are presented in the same order as the Adobe",
58 	"% manual.  The stack configuration before and after is shown in curly",
59 	"% brackets.  All the operators are listed, but only some are defined",
60 	"% here. Most things are faithful, except that no distinction is made",
61 	"% between colors for stroking and filling paths.  The string 'WRONGLY'",
62 	"% appears with commands that are approximations.",
63 	"%",
64 	"% PDF-style abbreviations:",
65 	"/rg {setrgbcolor} def   % {red green blue}            {-} set RGB color",
66 	"/RG {setrgbcolor} def   % {red green blue}            {-} set RGB color",
67 	"/q {gsave} def",
68 	"/Q {grestore} def",
69 	"/W {clip} def",
70 	"/W* {eoclip} def",
71 	"%",
72 	"% Gri-specific abbreviations:",
73 	"/hsb {sethsbcolor} def  % {hue saturation brightness} {-} set HSB color",
74 	"%",
75 	"% Following all try to mimic Adobe Illustrator",
76 	"%",
77 	"% Mimicking section 5.1 of Adobe manual:",
78 	"%A                      % {flag A}          {-} Determine whether",
79 	"                        %                       following object can",
80 	"                        %                       be selected. Flag=1",
81 	"                        %                       prevents selection;",
82 	"                        %                       flag=0 allows it.",
83 	"",
84 	"% Mimicking section 5.2 of Adobe manual:",
85 	"%u                      % {u}               {-} start group",
86 	"%U                      % end group",
87 	"%q                      % as 'u' but first item is a clip path",
88 	"%Q                      % as 'U' but first item is a clip path",
89 	"",
90 	"% Mimicking section 5.3 of Adobe manual:",
91 	"/g {setgray} def        % {gray g}          {-} Set gray for fill",
92 	"                        %                       path, WRONGLY used",
93 	"                        %                       for stroking also. ",
94 	"/G {setgray} def        % As 'g', but for filling path.",
95 	"%k                      % Set cmyk color for filling path.",
96 	"%K                      % As 'k', but for stroking path.",
97 	"%x                      % Set cmyk custom color for filling path.",
98 	"%X                      % As 'x' but for stroking path.",
99 	"%p                      % Define pattern for filling path.",
100 	"%P                      % As 'p' but for stroking path.",
101 	"%O                      % Specify whether overprinting for fill paths",
102 	"%R                      % As 'O' but for stroking path.",
103 	"",
104 	"% Mimicking section 5.4 of Adobe manual:",
105 	"/d {setdash} def        % {[array] phase d} {-} Set dash.",
106 	"/i {setflat} def        % {flatness i}      {-} Set flatness.",
107 	"/j {setlinejoin} def    % {linejoin j}      {-} Set line join.",
108 	"/J {setlinecap} def     % {linecap J}       {-} Set line cap.",
109 	"/M {setmiterlimit} def  % {miterlimit M}    {-} Set miter limit.",
110 	"/w {setlinewidth} def   % {linewidth w}     {-} Set line width.",
111 	"",
112 	"% Mimicking section 5.5 of Adobe manual:",
113 	"/m {moveto} def         % {x y m}           {-} Move to locn",
114 	"/l {lineto} def         % {x y l}           {-} Draw line to locn",
115 	"                        %                       not a smooth point.",
116 	"                        %                       WRONGLY, no",
117 	"                        %                       distinction is made",
118 	"                        %                       between smooth and",
119 	"                        %                       corner. ",
120 	"%L                      % {x y L}           {-} As 'l' but a corner",
121 	"%c                      % Bezier curve to smooth point.",
122 	"%C                      % As 'c' but to corner point.",
123 	"%v                      % Something else to do with Bezier.",
124 	"%V                      % ",
125 	"%y                      % ",
126 	"%Y                      % ",
127 	"",
128 	"% Mimicking section 5.6 of Adobe manual:",
129 	"%N                      % {N}               {-} As 'n' for nondrawn stuff",
130 	"/n {newpath} def        % {n}               {-} WRONGLY interpreted",
131 	"                        %                       as path constructor",
132 	"/F {fill} def           % {F}               {-} Fill current path.",
133 	"%f                      % {f}               {-} 'F' but close first",
134 	"/S {stroke} def         % {S}               {-} Stroke current path.",
135 	"%s                      % {s}               {-} 'S' but close first",
136 	"%B                      % {B}               {-} As 's' but don't empty path.",
137 	"%b                      % {b}               {-} As 'f' but don't empty path.",
138 	"%H                      % no-op (weird huh?)",
139 	"/h {closepath} def      % {h}               {-} Close  current path",
140 	"%W                      % Used to create masks.",
141 	"",
142 	"% Mimicking section 5.7 of Adobe manual:",
143 	"%a                      % Begin text block ...",
144 	"%e                      % Similar to 'a' but ...",
145 	"%I                      % Similar to 'a' but ...",
146 	"%o                      % Similar to 'a' but ...",
147 	"%r                      % Similar to 'a' but ...",
148 	"%t                      % {len (string) t}  {-} Render string.",
149 	"%T                      % End block of text",
150 	"",
151 	"% That's the end of the Illustrator stuff. Following are some Gri",
152 	"% definitions which provide a temporary way of handling fonts. ",
153 	"/sf {setfont} def       % {fontname sf}     {-} Set font name.",
154 	"/sh {show} def          % {(text) sh}       {-} Show text.",
155 	"/sc {scalefont} def     % {size sc}         {-} Scale font.",
156 	"% Gri items which should be translated to Illustrator format:",
157 	"/rl {rlineto} def",
158 	"/rm {rmoveto} def",
159 
160 	"% Procedures",
161 	"/cimdict 7 dict def",
162 	"/cim {",
163 	" cimdict begin",
164 	" /cl exch def",
165 	" /rw exch def",
166 	" /yur exch def",
167 	" /xur exch def",
168 	" /yll exch def",
169 	" /xll exch def",
170 	" q",
171 	" xll yll translate",
172 	" xur xll sub yur yll sub scale",
173 	" /do cl 3 mul string def",
174 	" cl rw 8 [cl 0 0 rw neg 0 rw] {currentfile do readhexstring pop} false 3 colorimage",
175 	" Q",
176 	" end",
177 	"} def",
178 
179 #if 1				// suggested 2001-mar-01 as workaround for ps2pdf bug
180 	"/imdict 14 dict def",
181 	"/im {",
182 	" imdict begin",
183 	" /cl exch def",
184 	" /rw exch def",
185 	" /yur exch def",
186 	" /xur exch def",
187 	" /yll exch def",
188 	" /xll exch def",
189 	" /imagemap exch def",
190 	" q",
191 	" % Until version 2.6.0 used a 'settransfer' here, but that",
192 	" % triggers a bug in ps2pdf",
193 	" xll yll translate",
194 	" xur xll sub yur yll sub scale",
195 	" /do cl string def",
196 	" cl rw 8 [cl 0 0 rw neg 0 rw]",
197 	" {currentfile do readhexstring pop",
198 	"  dup length 1 sub 0 1 3 -1 roll",
199 	"    { 1 index exch",
200 	"      2 copy get imagemap exch get 255 mul cvi put",
201 	"    }",
202 	"  for",
203 	" }image",
204 	" Q",
205 	" end",
206 	"} bind def",
207 #else
208 	"/imdict 14 dict def",
209 	"/im {",
210 	" imdict begin",
211 	" /cl exch def",
212 	" /rw exch def",
213 	" /yur exch def",
214 	" /xur exch def",
215 	" /yll exch def",
216 	" /xll exch def",
217 	" /imagemap exch def",
218 	" q",
219 	" % Add the mapping to the transfer function (ref: white book, p 743.",
220 	" [{255 mul cvi imagemap exch get} /exec load currenttransfer /exec load]",
221 	"   cvx settransfer",
222 	" xll yll translate",
223 	" xur xll sub yur yll sub scale",
224 	" /do cl string def",
225 	" cl rw 8 [cl 0 0 rw neg 0 rw] {currentfile do readhexstring pop}image",
226 	" Q",
227 	" end",
228 	"} def",
229 #endif
230 
231 	"/frdict  5 dict def",
232 	"/fr {",
233 	" frdict begin",
234 	" /yt exch def",
235 	" /xr exch def",
236 	" /yb exch def",
237 	" /xl exch def",
238 	" n",
239 	" xl yb m",
240 	" xl yt l",
241 	" xr yt l",
242 	" xr yb l",
243 	" h",
244 	" F",
245 	" n",
246 	" end",
247 	"} def",
248 	"/plusdict 3 dict def",
249 	"/_plus {",
250 	" plusdict begin",
251 	" dup 0.5 mul /t0 exch def",
252 	" /t1 exch def",
253 	" 0 t0 rm",
254 	" 0 t1 neg rl",
255 	" t0 neg t0 rm",
256 	" t1 0 rl",
257 	" t0 neg 0 rm",
258 	" end",
259 	"} def",
260 	"/timesdict 3 dict def",
261 	"/_times {",
262 	" timesdict begin",
263 	" dup 0.353553 mul /t0 exch def",
264 	" 0.707106 mul /t1 exch def",
265 	" t0 neg t0 rm",		// upper left
266 	" t1 dup neg rl",		// lower right
267 	" t1 neg 0 rm",		// lower left
268 	" t1 dup rl",		// upper right
269 	" t0 neg dup rm",		// centre
270 	" end",
271 	"} def",
272 	"/boxdict 3 dict def",
273 	"/_box {",
274 	" boxdict begin",
275 	" dup 0.5 mul /t0 exch def",
276 	" 1 mul /t1 exch def",
277 	" t0 neg t0 rm",		// top left
278 	" t1 0 rl",			// top right
279 	" 0 t1 neg rl",		// bottom right
280 	" t1 neg 0 rl",		// bottom left
281 	" h",			// back to top left
282 	" t0 dup neg rm",		// end at centre
283 	" end",
284 	"} def",
285 	"/filledboxdict 3 dict def",
286 	"/_filledbox {",
287 	" filledboxdict begin",
288 	" dup 0.5 mul /t0 exch def",
289 	" 1 mul /t1 exch def",
290 	" t0 neg t0 rm",		// top left
291 	" t1 0 rl",			// top right
292 	" 0 t1 neg rl",		// bottom right
293 	" t1 neg 0 rl",		// bottom left
294 	" h",			// back to top left
295 	" t0 dup neg rm",		// end at centre
296 	" F end",
297 	"} def",
298 	"/diamonddict 2 dict def",
299 	"/_diamond {",
300 	" diamonddict begin",
301 	" 0.5 mul /t0 exch def",
302 	" t0 neg 0 rm",		// left
303 	" t0 dup rl",		// top
304 	" t0 dup neg rl",		// right
305 	" t0 neg dup rl",		// bottom
306 	" h",			// back to left
307 	" t0 0 rm",			// end at centre
308 	" end",
309 	"} def",
310 	"/filleddiamonddict 2 dict def",
311 	"/_filleddiamond {",
312 	" filleddiamonddict begin",
313 	" 0.5 mul /t0 exch def",
314 	" t0 neg 0 rm",		// left
315 	" t0 dup rl",		// top
316 	" t0 dup neg rl",		// right
317 	" t0 neg dup rl",		// bottom
318 	" h",			// back to left
319 	" t0 0 rm",			// end at centre
320 	" F end",
321 	"} def",
322 	"/triangleupdict 5 dict def",
323 	"/_triangleup {",
324 	" triangleupdict begin",
325 	" dup 0.25 mul /t0 exch def",
326 	" dup 0.433013 mul /t1 exch def",
327 	" dup 0.75 mul /t2 exch def",
328 	" 0.866026 mul /t3 exch def",
329 	" t1 neg t0 neg rm",	// bottom left
330 	" t1 t2 rl",		// top
331 	" t1 t2 neg rl",		// bottom right
332 	" h",			// back to bottom left
333 	" t1 t0 rm",		// end at centre
334 	" end",
335 	"} def",
336 
337 	"/filledtriangleupdict 5 dict def",
338 	"/_filledtriangleup {",
339 	" filledtriangleupdict begin",
340 	" dup 0.25 mul /t0 exch def",
341 	" dup 0.433013 mul /t1 exch def",
342 	" dup 0.75 mul /t2 exch def",
343 	" 0.866026 mul /t3 exch def",
344 	" t1 neg t0 neg rm",	// bottom left
345 	" t1 t2 rl",		// top
346 	" t1 t2 neg rl",		// bottom right
347 	" h",			// back to bottom left
348 	" t1 t0 rm",		// end at centre
349 	" F end",
350 	"} def",
351 
352 	"/trianglerightdict 5 dict def",
353 	"/_triangleright {",
354 	" trianglerightdict begin",
355 	" dup 0.25 mul /t0 exch def",
356 	" dup 0.433013 mul /t1 exch def",
357 	" dup 0.75 mul /t2 exch def",
358 	" 0.866026 mul /t3 exch def",
359 	" t0 neg t1 rm",		// top
360 	" t2 t1 neg rl",		// right
361 	" t2 neg t1 neg rl",	// bottom
362 	" h",			// back to top
363 	" t0 t1 neg rm",		// end at centre
364 	" end",
365 	"} def",
366 
367 	"/filledtrianglerightdict 5 dict def",
368 	"/_filledtriangleright {",
369 	" filledtrianglerightdict begin",
370 	" dup 0.25 mul /t0 exch def",
371 	" dup 0.433013 mul /t1 exch def",
372 	" dup 0.75 mul /t2 exch def",
373 	" 0.866026 mul /t3 exch def",
374 	" t0 neg t1 rm",		// top
375 	" t2 t1 neg rl",		// right
376 	" t2 neg t1 neg rl",	// bottom
377 	" h",			// back to top
378 	" t0 t1 neg rm",		// end at centre
379 	" F end",
380 	"} def",
381 
382 	"/triangledowndict 5 dict def",
383 	"/_triangledown {",
384 	" triangledowndict begin",
385 	" dup 0.25 mul /t0 exch def",
386 	" dup 0.433013 mul /t1 exch def",
387 	" dup 0.75 mul /t2 exch def",
388 	" 0.866026 mul /t3 exch def",
389 	" t1 neg t0 rm",		// top left
390 	" t3 0 rl",			// top right
391 	" t1 neg t2 neg rl",	// bottom
392 	" h",			// back to top left
393 	" t1 t0 neg rm",		// end at centre
394 	" end",
395 	"} def",
396 
397 	"/filledtriangledowndict 5 dict def",
398 	"/_filledtriangledown {",
399 	" filledtriangledowndict begin",
400 	" dup 0.25 mul /t0 exch def",
401 	" dup 0.433013 mul /t1 exch def",
402 	" dup 0.75 mul /t2 exch def",
403 	" 0.866026 mul /t3 exch def",
404 	" t1 neg t0 rm",		// top left
405 	" t3 0 rl",			// top right
406 	" t1 neg t2 neg rl",	// bottom
407 	" h",			// back to top left
408 	" t1 t0 neg rm",		// end at centre
409 	" F end",
410 	"} def",
411 
412 	"/triangleleftdict 5 dict def",
413 	"/_triangleleft {",
414 	" triangleleftdict begin",
415 	" dup 0.25 mul /t0 exch def",
416 	" dup 0.433013 mul /t1 exch def",
417 	" dup 0.75 mul /t2 exch def",
418 	" 0.866026 mul /t3 exch def",
419 	" t0 t1 rm",		// top
420 	" 0 t3 neg rl",		// bottom
421 	" t2 neg t1 rl",		// left
422 	" h",			// back to top
423 	" t0 neg t1 neg rm",	// end at centre
424 	" end",
425 	"} def",
426 
427 	"/filledtriangleleftdict 5 dict def",
428 	"/_filledtriangleleft {",
429 	" filledtriangleleftdict begin",
430 	" dup 0.25 mul /t0 exch def",
431 	" dup 0.433013 mul /t1 exch def",
432 	" dup 0.75 mul /t2 exch def",
433 	" 0.866026 mul /t3 exch def",
434 	" t0 t1 rm",		// top
435 	" 0 t3 neg rl",		// bottom
436 	" t2 neg t1 rl",		// left
437 	" h",			// back to top
438 	" t0 neg t1 neg rm",	// end at centre
439 	" F end",
440 	"} def",
441 
442 	"/circdict 5 dict def",
443 	"/_circ {",
444 	" circdict begin",
445 	" 0.5 mul /t0 exch def",
446 	" currentpoint",
447 	" /t2 exch def",
448 	" /t1 exch def",
449 	" S n t1 t2 t0 0 360 arc",
450 	" t1 t2 m",
451 	" end",
452 	"} def",
453 
454 	"/bulldict 3 dict def",	// stack: diameter
455 	"/_bull {",
456 	" bulldict begin",
457 	" 0.5 mul /r exch def",
458 	" currentpoint /y exch def /x exch def",
459 	" S n x y r 0 360 arc h F S",
460 	" end",
461 	"} def",
462 
463 	"/filledhalfmoonupdict 3 dict def",	// stack: diameter
464 	"/_filledhalfmoonup {",
465 	" bulldict begin",
466 	" 0.5 mul /r exch def",
467 	" currentpoint /y exch def /x exch def",
468 	" S n x y r 0 180 arc h F S",
469 	" end",
470 	"} def",
471 
472 	"/filledhalfmoondowndict 3 dict def",	// stack: diameter
473 	"/_filledhalfmoondown {",
474 	" bulldict begin",
475 	" 0.5 mul /r exch def",
476 	" currentpoint /y exch def /x exch def",
477 	" S n x y r 180 360 arc h F S",
478 	" end",
479 	"} def",
480 	NULL
481 };
482 
483 #endif				// _postscpt_
484