| 1 |
/* GraphicsPostscript.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 1992-2007,2009,2011,2012,2014-2017,2020-2022,2025 Paul Boersma |
| 4 |
* |
| 5 |
* This code 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; either version 3 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
* See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "GraphicsP.h" |
| 20 |
#include <time.h> /* For creation date. */ |
| 21 |
#include "Printer.h" |
| 22 |
|
| 23 |
Thing_implement (GraphicsPostscript, Graphics, 0); |
| 24 |
|
| 25 |
static void downloadPrologAndSetUp (GraphicsPostscript me) { |
| 26 |
/* |
| 27 |
* Procedures section of the document prolog: procedure definitions valid for all pages. |
| 28 |
*/ |
| 29 |
my d_printf (my d_file, "%%%%BeginProlog\n"); |
| 30 |
my d_printf (my d_file, "%%%%BeginResource: procset (ppgb GraphicsPostscript procs) 1.0 0\n"); |
| 31 |
my d_printf (my d_file, "/N { newpath } bind def /M { newpath moveto } bind def /L { rlineto } bind def\n"); |
| 32 |
my d_printf (my d_file, "/F { 10 { 1 exch rlineto } repeat } bind def\n"); |
| 33 |
my d_printf (my d_file, "/C { 0 360 arc stroke } bind def /FC { 0 360 arc fill } bind def\n"); |
| 34 |
my d_printf (my d_file, |
| 35 |
"/PraatEncoding [\n" |
| 36 |
" /dotlessi/Aogonek/aogonek/Cacute/cacute/Ccaron/ccaron/Dcaron/dcaron/Dbar\n" |
| 37 |
" /dbar/Ecaron/ecaron/Eogonek/eogonek/Gcaron/gcaron/Lslash/lslash/Nacute\n" |
| 38 |
" /nacute/Ncaron/ncaron/Ohungarumlaut/ohungarumlaut/Rcaron/rcaron/Sacute/sacute/Scaron\n" |
| 39 |
" /scaron/Tcaron/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quotesinglright\n" |
| 40 |
" /parenleft/parenright/asterisk/plus/comma/hyphen/period/slash/zero/one\n" |
| 41 |
" /two/three/four/five/six/seven/eight/nine/colon/semicolon\n" |
| 42 |
" /less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N/O/P/Q/R/S/T/U/V/W/X/Y/Z\n" |
| 43 |
" /bracketleft/backslash/bracketright/asciicircum/underscore/quotesinglleft\n" |
| 44 |
" /a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde\n" |
| 45 |
" /Zdot/Adieresis/Aring/Ccedilla/Eacute/Ntilde/Odieresis/Udieresis/aacute/agrave/acircumflex\n" |
| 46 |
" /adieresis/atilde/aring/ccedilla/eacute/egrave/ecircumflex/edieresis/iacute/igrave/icircumflex\n"); |
| 47 |
my d_printf (my d_file, |
| 48 |
" /idieresis/ntilde/oacute/ograve/ocircumflex/odieresis/otilde/uacute/ugrave/ucircumflex/udieresis\n" |
| 49 |
" /dagger/degree/cent/sterling/section/bullet/paragraph/germandbls/registered/copyright/trademark\n" |
| 50 |
" /acute/dieresis/notequal/AE/Oslash/infinity/plusminus/lessequal/greaterequal/yen/mu\n" |
| 51 |
" /partialdiff/summation/product/pi/integral/ordfeminine/ordmasculine/Omega/ae/oslash\n" |
| 52 |
" /questiondown/exclamdown/logicalnot/radical/florin/approxequal/Delta/guillemotleft/guillemotright\n" |
| 53 |
" /ellipsis/zdot/Agrave/Atilde/Otilde/OE/oe/endash/emdash/quotedblleft/quotedblright\n" |
| 54 |
" /quoteleft/quoteright/divide/lozenge/ydieresis/Ydieresis/fraction/currency\n" |
| 55 |
" /guilsinglleft/guilsinglright/fi/fl/daggerdbl/periodcentered/quotesinglbase/quotedblbase\n" |
| 56 |
" /perthousand/Acircumflex/Ecircumflex/Aacute/Edieresis/Egrave/Iacute/Icircumflex/Idieresis\n" |
| 57 |
" /Igrave/Oacute/Ocircumflex/apple/Ograve/Uacute/Ucircumflex/Ugrave/tcaron\n" |
| 58 |
" /Uhungarumlaut/uhungarumlaut/Uring/uring/Yacute/yacute/Zacute/zacute/Zcaron/zcaron ] def\n"); |
| 59 |
my d_printf (my d_file, |
| 60 |
"/PraatEncode { /font exch def /base exch def\n" |
| 61 |
" /basedict base findfont def /new basedict maxlength dict def\n" |
| 62 |
" basedict { exch dup dup /FID ne exch /Encoding ne and\n" |
| 63 |
" { exch new 3 1 roll put } { pop pop } ifelse } forall\n" |
| 64 |
" new /FontName font put new /Encoding PraatEncoding put font new definefont pop } def\n"); |
| 65 |
my d_printf (my d_file, "%%%%EndResource\n"); |
| 66 |
my d_printf (my d_file, "%%%%EndProlog\n"); |
| 67 |
|
| 68 |
/* |
| 69 |
* Document setup: graphics state changes persistent across showpage calls. |
| 70 |
*/ |
| 71 |
my d_printf (my d_file, "%%%%BeginSetup\n"); |
| 72 |
//my d_printf (my d_file, "%d %d { dup mul exch dup mul add 1.0 exch sub } setscreen\n", my spotsDensity, my spotsAngle); |
| 73 |
my d_printf (my d_file, "true setstrokeadjust\n"); |
| 74 |
my d_printf (my d_file, "%%%%EndSetup\n"); |
| 75 |
} |
| 76 |
|
| 77 |
static void initPage (GraphicsPostscript me) { |
| 78 |
++ my pageNumber; |
| 79 |
if (my printer && my pageNumber > 1) downloadPrologAndSetUp (me); // has to be repeated for every page |
| 80 |
if (my job) { |
| 81 |
my d_printf (my d_file, "%%%%Page: %d %d\n", my pageNumber, my pageNumber); |
| 82 |
/*my d_printf (my d_file, "save\n");*/ |
| 83 |
my d_printf (my d_file, "%%%%BeginPageSetup\n"); |
| 84 |
} |
| 85 |
my d_printf (my d_file, "%g setlinewidth 2 setlinejoin\n", my resolution / 192.0); /* 0.375 point */ |
| 86 |
if (my job || my printer) { |
| 87 |
if (my landscape) |
| 88 |
my d_printf (my d_file, "%d 0 translate 90 rotate ", (int) (my paperHeight * 72 * my magnification)); |
| 89 |
} |
| 90 |
my d_printf (my d_file, "%.6g dup scale\n", 72.0 * my magnification / my resolution); |
| 91 |
if (my job) my d_printf (my d_file, "%%%%EndPageSetup\n"); |
| 92 |
my lastFid = nullptr; |
| 93 |
} |
| 94 |
|
| 95 |
static void exitPage (GraphicsPostscript me) { |
| 96 |
int font, style; |
| 97 |
/* |
| 98 |
* A showpage is only needed if we are printing to a file. |
| 99 |
* If we are printing directly to a printer, the page will be ejected |
| 100 |
* by EndPage (Windows NT/2000/XP/Vista) or PMSessionEndPage (MacOSX). |
| 101 |
*/ |
| 102 |
if (my d_file) { |
| 103 |
if (my job) { |
| 104 |
/*my d_printf (my d_file, "restore\n");*/ |
| 105 |
my d_printf (my d_file, "showpage\n"); |
| 106 |
} else if (my eps) { |
| 107 |
my d_printf (my d_file, "showpage %% redefined by encapsulating program\n"); |
| 108 |
} |
| 109 |
} |
| 110 |
for (font = 0; font <= kGraphics_font_DINGBATS; font ++) |
| 111 |
for (style = 0; style <= Graphics_BOLD_ITALIC; style ++) |
| 112 |
Melder_free (my fontInfos [font] [style]); |
| 113 |
my loadedXipa = false; // FIXME: include this because of the unpredictable page order with DSC? |
| 114 |
} |
| 115 |
|
| 116 |
void structGraphicsPostscript :: v9_destroy () noexcept { |
| 117 |
exitPage (this); |
| 118 |
if (our d_file) { |
| 119 |
if (our job) { |
| 120 |
our d_printf (our d_file, "%%%%Trailer\n"); |
| 121 |
our d_printf (our d_file, "%%%%Pages: %d\n", our pageNumber); |
| 122 |
} |
| 123 |
our d_printf (our d_file, "%%%%EOF\n"); // BUG. Correct according to DSC. But not good in EPS files? |
| 124 |
fclose (our d_file); |
| 125 |
} |
| 126 |
GraphicsPostscript_Parent :: v9_destroy (); |
| 127 |
} |
| 128 |
|
| 129 |
autoGraphics Graphics_create_postscriptjob (MelderFile file, int resolution, kGraphicsPostscript_spots spots, |
| 130 |
kGraphicsPostscript_paperSize paperSize, kGraphicsPostscript_orientation rotation, double magnification) |
| 131 |
{ |
| 132 |
autoGraphicsPostscript me = Thing_new (GraphicsPostscript); |
| 133 |
time_t today; |
| 134 |
my postScript = true, my yIsZeroAtTheTop = false, my languageLevel = 2; |
| 135 |
my job = true, my eps = false, my printer = false; |
| 136 |
my d_printf = (int (*)(void *, const char*, ...)) fprintf; |
| 137 |
Graphics_init (me.get(), resolution); // virtual resolution; may differ from that of the printer; OK if always 600 dpi |
| 138 |
my photocopyable = spots == kGraphicsPostscript_spots::PHOTOCOPYABLE; |
| 139 |
if (my photocopyable) { my spotsDensity = 85; my spotsAngle = 35; } |
| 140 |
else { my spotsDensity = 106; my spotsAngle = 46; } |
| 141 |
if (paperSize == kGraphicsPostscript_paperSize::A3) my paperWidth = 842 / 72.0, my paperHeight = 1191 / 72.0; |
| 142 |
else if (paperSize == kGraphicsPostscript_paperSize::US_LETTER) my paperWidth = 612 / 72.0, my paperHeight = 792 / 72.0; |
| 143 |
else my paperWidth = 595 / 72.0, my paperHeight = 842 / 72.0; |
| 144 |
my landscape = rotation == kGraphicsPostscript_orientation::LANDSCAPE; |
| 145 |
my magnification = magnification; |
| 146 |
my includeFonts = true; |
| 147 |
my d_file = Melder_fopen (file, "w"); |
| 148 |
/* |
| 149 |
* The Device Coordinates are the PostScript user coordinates. |
| 150 |
* They are chosen in such a way that a distance of 1 in device coordinates |
| 151 |
* equals one dot if the printer's resolution is 'resolution' dots per inch. |
| 152 |
* Take a sensible default margin: half an inch on all sides. |
| 153 |
*/ |
| 154 |
my d_x1DC = my d_x1DCmin = resolution / 2; |
| 155 |
my d_x2DC = my d_x2DCmax = (my paperWidth - 0.5) * resolution; |
| 156 |
my d_y1DC = my d_y1DCmin = resolution / 2; |
| 157 |
my d_y2DC = my d_y2DCmax = (my paperHeight - 0.5) * resolution; |
| 158 |
/* |
| 159 |
* Now don't just set x1wNDC etc, but force computation of the scaling as well. |
| 160 |
*/ |
| 161 |
Graphics_setWsWindow (me.get(), 0, my paperWidth - 1.0, 12.000 - (my paperHeight - 1.0), 12.000); |
| 162 |
/* |
| 163 |
* We will adhere to version 3.0 of the Document Structuring Conventions for print jobs. |
| 164 |
*/ |
| 165 |
my d_printf (my d_file, "%%!PS-Adobe-3.0\n"); |
| 166 |
my d_printf (my d_file, "%%%%Creator: Praat Shell 4.2\n"); |
| 167 |
my d_printf (my d_file, "%%%%Title: %s\n", Melder_peek32to8_fileSystem (MelderFile_name (file))); |
| 168 |
today = time (nullptr); |
| 169 |
my d_printf (my d_file, "%%%%CreationDate: %s", ctime (& today)); // contains newline symbol |
| 170 |
my d_printf (my d_file, "%%%%PageOrder: Special\n"); |
| 171 |
my d_printf (my d_file, "%%%%Pages: (atend)\n"); |
| 172 |
my d_printf (my d_file, "%%%%EndComments\n"); |
| 173 |
downloadPrologAndSetUp (me.get()); |
| 174 |
initPage (me.get()); |
| 175 |
return me.move(); |
| 176 |
} |
| 177 |
|
| 178 |
#if defined (macintosh) |
| 179 |
static int Eps_postScript_printf (void *stream, const char *format, ... ) { |
| 180 |
constexpr integer bufferSize = 3002; |
| 181 |
static char theLine [bufferSize]; |
| 182 |
char *p; |
| 183 |
va_list args; |
| 184 |
va_start (args, format); |
| 185 |
vsnprintf (theLine, bufferSize, format, args); |
| 186 |
theLine [bufferSize - 1] = '\0'; |
| 187 |
va_end (args); |
| 188 |
for (p = theLine; *p != '\0'; p ++) |
| 189 |
if (*p == '\n') |
| 190 |
*p = '\r'; |
| 191 |
return (int) fwrite (theLine, sizeof (char), strlen (theLine), reinterpret_cast <FILE *> (stream)); |
| 192 |
} |
| 193 |
#endif |
| 194 |
|
| 195 |
autoGraphics Graphics_create_epsfile (MelderFile file, int resolution, enum kGraphicsPostscript_spots spots, |
| 196 |
double x1inches, double x2inches, double y1inches, double y2inches, bool includeFonts, bool useSilipaPS) |
| 197 |
{ |
| 198 |
autoGraphicsPostscript me = Thing_new (GraphicsPostscript); |
| 199 |
time_t today; |
| 200 |
integer left, right, top, bottom; |
| 201 |
my postScript = true, my languageLevel = 2; |
| 202 |
my job = false, my eps = true, my printer = false; |
| 203 |
#if defined (macintosh) |
| 204 |
/* Replace newlines with carriage returns to be compatible with MS Word 5.1. */ |
| 205 |
my d_printf = Eps_postScript_printf; |
| 206 |
#else |
| 207 |
my d_printf = (int (*)(void *, const char*, ...)) fprintf; |
| 208 |
#endif |
| 209 |
Graphics_init (me.get(), resolution); // virtual resolution; may differ from that of the printer; OK if always 600 dpi |
| 210 |
my photocopyable = spots == kGraphicsPostscript_spots::PHOTOCOPYABLE; |
| 211 |
if (my photocopyable) { my spotsDensity = 85; my spotsAngle = 35; } |
| 212 |
else { my spotsDensity = 106; my spotsAngle = 46; } |
| 213 |
my paperWidth = 7.5, my paperHeight = 11.0; |
| 214 |
my landscape = false; |
| 215 |
my magnification = 1.0; |
| 216 |
my includeFonts = includeFonts; |
| 217 |
my useSilipaPS = useSilipaPS; |
| 218 |
my d_file = Melder_fopen (file, "w"); |
| 219 |
my d_x1DC = my d_x1DCmin = 0; |
| 220 |
my d_x2DC = my d_x2DCmax = my paperWidth * resolution; // 600 dpi -> 4500 virtual dots |
| 221 |
my d_y1DC = my d_y1DCmin = 0; |
| 222 |
my d_y2DC = my d_y2DCmax = my paperHeight * resolution; // 600 dpi -> 6600 virtual dots |
| 223 |
Graphics_setWsWindow (me.get(), 0, my paperWidth, 12.0 - my paperHeight, 12.0); // force scaling |
| 224 |
/* |
| 225 |
* We will honour version 3.0 of the DSC for Encapsulated PostScript files, |
| 226 |
* which includes supplying the bounding box information. |
| 227 |
*/ |
| 228 |
left = Melder_ifloor (x1inches * 72); |
| 229 |
right = Melder_iceiling (x2inches * 72); |
| 230 |
top = Melder_iceiling ((y2inches - my d_y1wNDC) * 72); |
| 231 |
bottom = Melder_ifloor ((y1inches - my d_y1wNDC) * 72); |
| 232 |
my d_printf (my d_file, "%%!PS-Adobe-3.0 EPSF-3.0\n"); |
| 233 |
my d_printf (my d_file, "%%%%BoundingBox: %d %d %d %d\n", left, bottom, right, top); |
| 234 |
my d_printf (my d_file, "%%%%Creator: Praat Shell 5.1\n"); |
| 235 |
/* |
| 236 |
* In an EPS file without screen preview, the file name will be visible anyway. |
| 237 |
* This leaves us room to show a warning that should keep users from thinking anything is wrong. |
| 238 |
*/ |
| 239 |
my d_printf (my d_file, "%%%%Title: NO SCREEN PREVIEW, BUT WILL PRINT CORRECTLY\n"); |
| 240 |
today = time (nullptr); |
| 241 |
my d_printf (my d_file, "%%%%CreationDate: %s", ctime (& today)); /* Contains newline symbol. */ |
| 242 |
my d_printf (my d_file, "%%%%EndComments\n"); |
| 243 |
downloadPrologAndSetUp (me.get()); |
| 244 |
initPage (me.get()); |
| 245 |
return me.move(); |
| 246 |
} |
| 247 |
|
| 248 |
#if defined (_WIN32) |
| 249 |
autoGraphics Graphics_create_postscriptprinter () { |
| 250 |
autoGraphicsPostscript me = Thing_new (GraphicsPostscript); |
| 251 |
my postScript = true, my languageLevel = 2; |
| 252 |
my job = false, my eps = false, my printer = true; |
| 253 |
my d_printf = Printer_postScript_printf; |
| 254 |
Graphics_init (me.get(), thePrinter. resolution); // virtual resolution |
| 255 |
my photocopyable = thePrinter. spots == kGraphicsPostscript_spots::PHOTOCOPYABLE; |
| 256 |
if (my photocopyable) { my spotsDensity = 85; my spotsAngle = 35; } |
| 257 |
else { my spotsDensity = 106; my spotsAngle = 46; } |
| 258 |
my paperWidth = (double) thePrinter. paperWidth / my resolution; |
| 259 |
my paperHeight = (double) thePrinter. paperHeight / my resolution; |
| 260 |
my landscape = thePrinter. orientation == kGraphicsPostscript_orientation::LANDSCAPE; |
| 261 |
my magnification = thePrinter. magnification; |
| 262 |
my includeFonts = true; |
| 263 |
my d_x1DC = my d_x1DCmin = my resolution / 2; |
| 264 |
my d_x2DC = my d_x2DCmax = (my paperWidth - 0.5) * my resolution; |
| 265 |
my d_y1DC = my d_y1DCmin = my resolution / 2; |
| 266 |
my d_y2DC = my d_y2DCmax = (my paperHeight - 0.5) * my resolution; |
| 267 |
Graphics_setWsWindow (me.get(), 0, my paperWidth - 1.0, 13.0 - my paperHeight, 12.0); |
| 268 |
downloadPrologAndSetUp (me.get()); |
| 269 |
initPage (me.get()); |
| 270 |
return me.move(); |
| 271 |
} |
| 272 |
#endif |
| 273 |
|
| 274 |
void Graphics_nextSheetOfPaper (Graphics graphics) { |
| 275 |
if (graphics -> postScript) { |
| 276 |
GraphicsPostscript me = static_cast <GraphicsPostscript> (graphics); |
| 277 |
exitPage (me); |
| 278 |
if (my printer) Printer_nextPage (); |
| 279 |
initPage (me); |
| 280 |
} else if (graphics -> printer) { |
| 281 |
Printer_nextPage (); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
/* End of file GraphicsPostscript.cpp */ |