PSOutputDev.cc 210 KB
Newer Older
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1 2 3 4
//========================================================================
//
// PSOutputDev.cc
//
Albert Astals Cid's avatar
Albert Astals Cid committed
5
// Copyright 1996-2013 Glyph & Cog, LLC
Kristian Høgsberg's avatar
Kristian Høgsberg committed
6 7 8
//
//========================================================================

9 10 11 12
//========================================================================
//
// Modified under the Poppler project - http://poppler.freedesktop.org
//
13 14 15
// All changes made under the Poppler project to this file are licensed
// under GPL version 2 or later
//
16 17
// Copyright (C) 2005 Martin Kretzschmar <martink@gnome.org>
// Copyright (C) 2005, 2006 Kristian Høgsberg <krh@redhat.com>
18
// Copyright (C) 2006-2009, 2011-2013, 2015-2019 Albert Astals Cid <aacid@kde.org>
19 20
// Copyright (C) 2006 Jeff Muizelaar <jeff@infidigm.net>
// Copyright (C) 2007, 2008 Brad Hards <bradh@kde.org>
21
// Copyright (C) 2008, 2009 Koji Otani <sho@bbr.jp>
Hib Eris's avatar
Hib Eris committed
22
// Copyright (C) 2008, 2010 Hib Eris <hib@hiberis.nl>
23
// Copyright (C) 2009-2013 Thomas Freitag <Thomas.Freitag@alfa.de>
24
// Copyright (C) 2009 Till Kamppeter <till.kamppeter@gmail.com>
25
// Copyright (C) 2009 Carlos Garcia Campos <carlosgc@gnome.org>
Albert Astals Cid's avatar
Albert Astals Cid committed
26
// Copyright (C) 2009, 2011, 2012, 2014-2017 William Bader <williambader@hotmail.com>
27
// Copyright (C) 2009 Kovid Goyal <kovid@kovidgoyal.net>
28
// Copyright (C) 2009-2011, 2013-2015, 2017 Adrian Johnson <ajohnson@redneon.com>
Fabio D'Urso's avatar
Fabio D'Urso committed
29
// Copyright (C) 2012, 2014 Fabio D'Urso <fabiodurso@hotmail.it>
30
// Copyright (C) 2012 Lu Wang <coolwanglu@gmail.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
31
// Copyright (C) 2014 Till Kamppeter <till.kamppeter@gmail.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
32
// Copyright (C) 2015 Marek Kasik <mkasik@redhat.com>
Albert Astals Cid's avatar
Albert Astals Cid committed
33
// Copyright (C) 2016 Caolán McNamara <caolanm@redhat.com>
34
// Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich
35
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
36
// Copyright (C) 2018 Philipp Knechtges <philipp-dev@knechtges.com>
37
// Copyright (C) 2019 Christian Persch <chpe@src.gnome.org>
38
// Copyright (C) 2019 Oliver Sander <oliver.sander@tu-dresden.de>
39 40 41 42 43 44
//
// To see a description of the changes please see the Changelog file that
// came with your tarball or type make ChangeLog if you are building from git
//
//========================================================================

Kristian Høgsberg's avatar
Kristian Høgsberg committed
45 46
#include <config.h>

47 48 49 50 51 52
#include <cstdio>
#include <cstddef>
#include <cstdarg>
#include <csignal>
#include <cmath>
#include <climits>
53
#include <algorithm>
54
#include <array>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include "goo/GooString.h"
#include "poppler-config.h"
#include "GlobalParams.h"
#include "Object.h"
#include "Error.h"
#include "Function.h"
#include "Gfx.h"
#include "GfxState.h"
#include "GfxFont.h"
#include "UnicodeMap.h"
#include <fofi/FoFiType1C.h>
#include <fofi/FoFiTrueType.h>
#include "Catalog.h"
#include "Page.h"
#include "Stream.h"
70
#ifdef ENABLE_ZLIB
71 72
#  include "FlateEncoder.h"
#endif
73
#ifdef ENABLE_ZLIB_UNCOMPRESS
74 75
#  include "FlateStream.h"
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
76
#include "Annot.h"
77 78
#include "XRef.h"
#include "PreScanOutputDev.h"
79
#include "FileSpec.h"
80
#include "CharCodeToUnicode.h"
81
#ifdef HAVE_SPLASH
82 83 84 85
#  include "splash/Splash.h"
#  include "splash/SplashBitmap.h"
#  include "SplashOutputDev.h"
#endif
Kristian Høgsberg's avatar
Kristian Høgsberg committed
86
#include "PSOutputDev.h"
Hib Eris's avatar
Hib Eris committed
87
#include "PDFDoc.h"
Kristian Høgsberg's avatar
Kristian Høgsberg committed
88

89 90 91 92 93 94 95
// the MSVC math.h doesn't define this
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

//------------------------------------------------------------------------

96 97
// Max size of a slice when rasterizing pages, in pixels.
#define rasterizationSliceSize 20000000
98

Kristian Høgsberg's avatar
Kristian Høgsberg committed
99 100 101 102
//------------------------------------------------------------------------
// PostScript prolog and setup
//------------------------------------------------------------------------

103 104 105 106 107 108 109
// The '~' escapes mark prolog code that is emitted only in certain
// levels:
//
//   ~[123][sn]
//      ^   ^----- s=psLevel*Sep, n=psLevel*
//      +----- 1=psLevel1*, 2=psLevel2*, 3=psLevel3*

110
static const char *prolog[] = {
Kristian Høgsberg's avatar
Kristian Høgsberg committed
111 112 113
  "/xpdf 75 dict def xpdf begin",
  "% PDF special state",
  "/pdfDictSize 15 def",
114
  "~1sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
115 116 117 118 119 120
  "/pdfStates 64 array def",
  "  0 1 63 {",
  "    pdfStates exch pdfDictSize dict",
  "    dup /pdfStateIdx 3 index put",
  "    put",
  "  } for",
121
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
122 123
  "/pdfSetup {",
  "  /setpagedevice where {",
124 125 126 127 128 129 130 131 132
  "    pop 2 dict begin",
  "      /Policies 1 dict dup begin /PageSize 6 def end def",
  "      { /Duplex true def } if",
  "    currentdict end setpagedevice",
  "  } {",
  "    pop",
  "  } ifelse",
  "} def",
  "/pdfSetupPaper {",
133 134 135
  "  % Change paper size, but only if different from previous paper size otherwise",
  "  % duplex fails. PLRM specifies a tolerance of 5 pts when matching paper size",
  "  % so we use the same when checking if the size changes.",
136
  "  /setpagedevice where {",
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  "    pop currentpagedevice",
  "    /PageSize known {",
  "      2 copy",
  "      currentpagedevice /PageSize get aload pop",
  "      exch 4 1 roll",
  "      sub abs 5 gt",
  "      3 1 roll",
  "      sub abs 5 gt",
  "      or",
  "    } {",
  "      true",
  "    } ifelse",
  "    {",
  "      2 array astore",
  "      2 dict begin",
  "        /PageSize exch def",
  "        /ImagingBBox null def",
  "      currentdict end",
  "      setpagedevice",
  "    } {",
  "      pop pop",
  "    } ifelse",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
159
  "  } {",
160
  "    pop",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
161 162
  "  } ifelse",
  "} def",
163
  "~1sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
164 165
  "/pdfOpNames [",
  "  /pdfFill /pdfStroke /pdfLastFill /pdfLastStroke",
166
  "  /pdfTextMat /pdfFontSize /pdfCharSpacing /pdfTextRender /pdfPatternCS",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
167 168
  "  /pdfTextRise /pdfWordSpacing /pdfHorizScaling /pdfTextClipPath",
  "] def",
169
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
170
  "/pdfStartPage {",
171
  "~1sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
172
  "  pdfStates 0 get begin",
173
  "~23sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
174
  "  pdfDictSize dict begin",
175 176 177 178 179 180 181 182 183 184 185 186
  "~23n",
  "  /pdfFillCS [] def",
  "  /pdfFillXform {} def",
  "  /pdfStrokeCS [] def",
  "  /pdfStrokeXform {} def",
  "~1n",
  "  /pdfFill 0 def",
  "  /pdfStroke 0 def",
  "~1s",
  "  /pdfFill [0 0 0 1] def",
  "  /pdfStroke [0 0 0 1] def",
  "~23sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
187 188
  "  /pdfFill [0] def",
  "  /pdfStroke [0] def",
189 190
  "  /pdfFillOP false def",
  "  /pdfStrokeOP false def",
191 192
  "~3sn",
  "  /pdfOPM false def",
193
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
194 195 196 197 198 199
  "  /pdfLastFill false def",
  "  /pdfLastStroke false def",
  "  /pdfTextMat [1 0 0 1 0 0] def",
  "  /pdfFontSize 0 def",
  "  /pdfCharSpacing 0 def",
  "  /pdfTextRender 0 def",
200
  "  /pdfPatternCS false def", 
Kristian Høgsberg's avatar
Kristian Høgsberg committed
201 202 203 204 205 206
  "  /pdfTextRise 0 def",
  "  /pdfWordSpacing 0 def",
  "  /pdfHorizScaling 1 def",
  "  /pdfTextClipPath [] def",
  "} def",
  "/pdfEndPage { end } def",
207
  "~23s",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
  "% separation convention operators",
  "/findcmykcustomcolor where {",
  "  pop",
  "}{",
  "  /findcmykcustomcolor { 5 array astore } def",
  "} ifelse",
  "/setcustomcolor where {",
  "  pop",
  "}{",
  "  /setcustomcolor {",
  "    exch",
  "    [ exch /Separation exch dup 4 get exch /DeviceCMYK exch",
  "      0 4 getinterval cvx",
  "      [ exch /dup load exch { mul exch dup } /forall load",
  "        /pop load dup ] cvx",
  "    ] setcolorspace setcolor",
  "  } def",
  "} ifelse",
  "/customcolorimage where {",
  "  pop",
  "}{",
  "  /customcolorimage {",
  "    gsave",
  "    [ exch /Separation exch dup 4 get exch /DeviceCMYK exch",
  "      0 4 getinterval",
  "      [ exch /dup load exch { mul exch dup } /forall load",
  "        /pop load dup ] cvx",
  "    ] setcolorspace",
  "    10 dict begin",
  "      /ImageType 1 def",
  "      /DataSource exch def",
  "      /ImageMatrix exch def",
  "      /BitsPerComponent exch def",
  "      /Height exch def",
  "      /Width exch def",
  "      /Decode [1 0] def",
  "    currentdict end",
  "    image",
  "    grestore",
  "  } def",
  "} ifelse",
249
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
250
  "% PDF color state",
251 252 253 254 255 256 257 258 259 260 261
  "~1n",
  "/g { dup /pdfFill exch def setgray",
  "     /pdfLastFill true def /pdfLastStroke false def } def",
  "/G { dup /pdfStroke exch def setgray",
  "     /pdfLastStroke true def /pdfLastFill false def } def",
  "/fCol {",
  "  pdfLastFill not {",
  "    pdfFill setgray",
  "    /pdfLastFill true def /pdfLastStroke false def",
  "  } if",
  "} def",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
262 263
  "/sCol {",
  "  pdfLastStroke not {",
264
  "    pdfStroke setgray",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
265 266 267
  "    /pdfLastStroke true def /pdfLastFill false def",
  "  } if",
  "} def",
268 269 270 271 272
  "~1s",
  "/k { 4 copy 4 array astore /pdfFill exch def setcmykcolor",
  "     /pdfLastFill true def /pdfLastStroke false def } def",
  "/K { 4 copy 4 array astore /pdfStroke exch def setcmykcolor",
  "     /pdfLastStroke true def /pdfLastFill false def } def",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
273 274
  "/fCol {",
  "  pdfLastFill not {",
275 276 277 278 279 280 281 282 283 284
  "    pdfFill aload pop setcmykcolor",
  "    /pdfLastFill true def /pdfLastStroke false def",
  "  } if",
  "} def",
  "/sCol {",
  "  pdfLastStroke not {",
  "    pdfStroke aload pop setcmykcolor",
  "    /pdfLastStroke true def /pdfLastFill false def",
  "  } if",
  "} def",
285 286
  "~3n",
  "/opm { dup /pdfOPM exch def",
287
  "      /setoverprintmode where{pop setoverprintmode}{pop}ifelse  } def",
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  "~23n",
  "/cs { /pdfFillXform exch def dup /pdfFillCS exch def",
  "      setcolorspace } def",
  "/CS { /pdfStrokeXform exch def dup /pdfStrokeCS exch def",
  "      setcolorspace } def",
  "/sc { pdfLastFill not { pdfFillCS setcolorspace } if",
  "      dup /pdfFill exch def aload pop pdfFillXform setcolor",
  "     /pdfLastFill true def /pdfLastStroke false def } def",
  "/SC { pdfLastStroke not { pdfStrokeCS setcolorspace } if",
  "      dup /pdfStroke exch def aload pop pdfStrokeXform setcolor",
  "     /pdfLastStroke true def /pdfLastFill false def } def",
  "/op { /pdfFillOP exch def",
  "      pdfLastFill { pdfFillOP setoverprint } if } def",
  "/OP { /pdfStrokeOP exch def",
  "      pdfLastStroke { pdfStrokeOP setoverprint } if } def",
  "/fCol {",
  "  pdfLastFill not {",
  "    pdfFillCS setcolorspace",
  "    pdfFill aload pop pdfFillXform setcolor",
  "    pdfFillOP setoverprint",
  "    /pdfLastFill true def /pdfLastStroke false def",
  "  } if",
  "} def",
  "/sCol {",
  "  pdfLastStroke not {",
  "    pdfStrokeCS setcolorspace",
  "    pdfStroke aload pop pdfStrokeXform setcolor",
  "    pdfStrokeOP setoverprint",
  "    /pdfLastStroke true def /pdfLastFill false def",
  "  } if",
  "} def",
319 320
  "~3s",
  "/opm { dup /pdfOPM exch def",
321
  "      /setoverprintmode where{pop setoverprintmode}{pop}ifelse } def",
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
  "~23s",
  "/k { 4 copy 4 array astore /pdfFill exch def setcmykcolor",
  "     /pdfLastFill true def /pdfLastStroke false def } def",
  "/K { 4 copy 4 array astore /pdfStroke exch def setcmykcolor",
  "     /pdfLastStroke true def /pdfLastFill false def } def",
  "/ck { 6 copy 6 array astore /pdfFill exch def",
  "      findcmykcustomcolor exch setcustomcolor",
  "      /pdfLastFill true def /pdfLastStroke false def } def",
  "/CK { 6 copy 6 array astore /pdfStroke exch def",
  "      findcmykcustomcolor exch setcustomcolor",
  "      /pdfLastStroke true def /pdfLastFill false def } def",
  "/op { /pdfFillOP exch def",
  "      pdfLastFill { pdfFillOP setoverprint } if } def",
  "/OP { /pdfStrokeOP exch def",
  "      pdfLastStroke { pdfStrokeOP setoverprint } if } def",
  "/fCol {",
  "  pdfLastFill not {",
  "    pdfFill aload length 4 eq {",
340 341 342 343
  "      setcmykcolor",
  "    }{",
  "      findcmykcustomcolor exch setcustomcolor",
  "    } ifelse",
344
  "    pdfFillOP setoverprint",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
345 346 347
  "    /pdfLastFill true def /pdfLastStroke false def",
  "  } if",
  "} def",
348 349 350 351 352 353 354 355 356 357 358 359
  "/sCol {",
  "  pdfLastStroke not {",
  "    pdfStroke aload length 4 eq {",
  "      setcmykcolor",
  "    }{",
  "      findcmykcustomcolor exch setcustomcolor",
  "    } ifelse",
  "    pdfStrokeOP setoverprint",
  "    /pdfLastStroke true def /pdfLastFill false def",
  "  } if",
  "} def",
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
  "% build a font",
  "/pdfMakeFont {",
  "  4 3 roll findfont",
  "  4 2 roll matrix scale makefont",
  "  dup length dict begin",
  "    { 1 index /FID ne { def } { pop pop } ifelse } forall",
  "    /Encoding exch def",
  "    currentdict",
  "  end",
  "  definefont pop",
  "} def",
  "/pdfMakeFont16 {",
  "  exch findfont",
  "  dup length dict begin",
  "    { 1 index /FID ne { def } { pop pop } ifelse } forall",
  "    /WMode exch def",
  "    currentdict",
  "  end",
  "  definefont pop",
  "} def",
380
  "~3sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
381 382 383 384 385 386 387 388 389 390 391 392 393
  "/pdfMakeFont16L3 {",
  "  1 index /CIDFont resourcestatus {",
  "    pop pop 1 index /CIDFont findresource /CIDFontType known",
  "  } {",
  "    false",
  "  } ifelse",
  "  {",
  "    0 eq { /Identity-H } { /Identity-V } ifelse",
  "    exch 1 array astore composefont pop",
  "  } {",
  "    pdfMakeFont16",
  "  } ifelse",
  "} def",
394
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
395
  "% graphics state operators",
396
  "~1sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
397 398 399 400 401 402 403
  "/q {",
  "  gsave",
  "  pdfOpNames length 1 sub -1 0 { pdfOpNames exch get load } for",
  "  pdfStates pdfStateIdx 1 add get begin",
  "  pdfOpNames { exch def } forall",
  "} def",
  "/Q { end grestore } def",
404 405 406 407 408 409 410 411 412 413 414 415
  "~23sn",
  "/q { gsave pdfDictSize dict begin } def",
  "/Q {",
  "  end grestore",
  "  /pdfLastFill where {",
  "    pop",
  "    pdfLastFill {",
  "      pdfFillOP setoverprint",
  "    } {",
  "      pdfStrokeOP setoverprint",
  "    } ifelse",
  "  } if",
416 417 418
  "~3sn",
  "  /pdfOPM where {",
  "    pop",
419
  "    pdfOPM /setoverprintmode where{pop setoverprintmode}{pop}ifelse ",
420 421
  "  } if",
  "~23sn",
422 423
  "} def",
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
  "/cm { concat } def",
  "/d { setdash } def",
  "/i { setflat } def",
  "/j { setlinejoin } def",
  "/J { setlinecap } def",
  "/M { setmiterlimit } def",
  "/w { setlinewidth } def",
  "% path segment operators",
  "/m { moveto } def",
  "/l { lineto } def",
  "/c { curveto } def",
  "/re { 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto",
  "      neg 0 rlineto closepath } def",
  "/h { closepath } def",
  "% path painting operators",
  "/S { sCol stroke } def",
  "/Sf { fCol stroke } def",
  "/f { fCol fill } def",
  "/f* { fCol eofill } def",
  "% clipping operators",
  "/W { clip newpath } def",
  "/W* { eoclip newpath } def",
446
  "/Ws { strokepath clip newpath } def",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
447 448 449 450 451 452 453
  "% text state operators",
  "/Tc { /pdfCharSpacing exch def } def",
  "/Tf { dup /pdfFontSize exch def",
  "      dup pdfHorizScaling mul exch matrix scale",
  "      pdfTextMat matrix concatmatrix dup 4 0 put dup 5 0 put",
  "      exch findfont exch makefont setfont } def",
  "/Tr { /pdfTextRender exch def } def",
454
  "/Tp { /pdfPatternCS exch def } def", 
Kristian Høgsberg's avatar
Kristian Høgsberg committed
455 456 457 458 459 460 461
  "/Ts { /pdfTextRise exch def } def",
  "/Tw { /pdfWordSpacing exch def } def",
  "/Tz { /pdfHorizScaling exch def } def",
  "% text positioning operators",
  "/Td { pdfTextMat transform moveto } def",
  "/Tm { /pdfTextMat exch def } def",
  "% text string operators",
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
  "/xyshow where {",
  "  pop",
  "  /xyshow2 {",
  "    dup length array",
  "    0 2 2 index length 1 sub {",
  "      2 index 1 index 2 copy get 3 1 roll 1 add get",
  "      pdfTextMat dtransform",
  "      4 2 roll 2 copy 6 5 roll put 1 add 3 1 roll dup 4 2 roll put",
  "    } for",
  "    exch pop",
  "    xyshow",
  "  } def",
  "}{",
  "  /xyshow2 {",
  "    currentfont /FontType get 0 eq {",
  "      0 2 3 index length 1 sub {",
  "        currentpoint 4 index 3 index 2 getinterval show moveto",
  "        2 copy get 2 index 3 2 roll 1 add get",
  "        pdfTextMat dtransform rmoveto",
  "      } for",
  "    } {",
  "      0 1 3 index length 1 sub {",
  "        currentpoint 4 index 3 index 1 getinterval show moveto",
  "        2 copy 2 mul get 2 index 3 2 roll 2 mul 1 add get",
  "        pdfTextMat dtransform rmoveto",
  "      } for",
  "    } ifelse",
  "    pop pop",
  "  } def",
  "} ifelse",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
492 493
  "/cshow where {",
  "  pop",
494 495 496 497 498 499 500 501
  "  /xycp {", // xycharpath
  "    0 3 2 roll",
  "    {",
  "      pop pop currentpoint 3 2 roll",
  "      1 string dup 0 4 3 roll put false charpath moveto",
  "      2 copy get 2 index 2 index 1 add get",
  "      pdfTextMat dtransform rmoveto",
  "      2 add",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
502 503 504 505
  "    } exch cshow",
  "    pop pop",
  "  } def",
  "}{",
506
  "  /xycp {", // xycharpath
Kristian Høgsberg's avatar
Kristian Høgsberg committed
507
  "    currentfont /FontType get 0 eq {",
508 509 510 511
  "      0 2 3 index length 1 sub {",
  "        currentpoint 4 index 3 index 2 getinterval false charpath moveto",
  "        2 copy get 2 index 3 2 roll 1 add get",
  "        pdfTextMat dtransform rmoveto",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
512 513
  "      } for",
  "    } {",
514 515 516 517 518
  "      0 1 3 index length 1 sub {",
  "        currentpoint 4 index 3 index 1 getinterval false charpath moveto",
  "        2 copy 2 mul get 2 index 3 2 roll 2 mul 1 add get",
  "        pdfTextMat dtransform rmoveto",
  "      } for",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
519 520 521 522 523 524 525
  "    } ifelse",
  "    pop pop",
  "  } def",
  "} ifelse",
  "/Tj {",
  "  fCol",  // because stringwidth has to draw Type 3 chars
  "  0 pdfTextRise pdfTextMat dtransform rmoveto",
526 527 528
  "  currentpoint 4 2 roll",
  "  pdfTextRender 1 and 0 eq {",
  "    2 copy xyshow2",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
529 530
  "  } if",
  "  pdfTextRender 3 and dup 1 eq exch 2 eq or {",
531 532
  "    3 index 3 index moveto",
  "    2 copy",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
533
  "    currentfont /FontType get 3 eq { fCol } { sCol } ifelse",
534
  "    xycp currentpoint stroke moveto",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
535
  "  } if",
536 537
  "  pdfTextRender 4 and 0 ne {",
  "    4 2 roll moveto xycp",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
538 539 540 541 542 543 544 545
  "    /pdfTextClipPath [ pdfTextClipPath aload pop",
  "      {/moveto cvx}",
  "      {/lineto cvx}",
  "      {/curveto cvx}",
  "      {/closepath cvx}",
  "    pathforall ] def",
  "    currentpoint newpath moveto",
  "  } {",
546
  "    pop pop pop pop",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
547 548 549
  "  } ifelse",
  "  0 pdfTextRise neg pdfTextMat dtransform rmoveto",
  "} def",
550
  "/TJm { 0.001 mul pdfFontSize mul pdfHorizScaling mul neg 0",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
551
  "       pdfTextMat dtransform rmoveto } def",
552
  "/TJmV { 0.001 mul pdfFontSize mul neg 0 exch",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
553 554 555
  "        pdfTextMat dtransform rmoveto } def",
  "/Tclip { pdfTextClipPath cvx exec clip newpath",
  "         /pdfTextClipPath [] def } def",
556 557
  "/Tclip* { pdfTextClipPath cvx exec eoclip newpath",
  "         /pdfTextClipPath [] def } def",
558
  "~1ns",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
559 560 561 562 563
  "% Level 1 image operators",
  "/pdfIm1 {",
  "  /pdfImBuf1 4 index string def",
  "  { currentfile pdfImBuf1 readhexstring pop } image",
  "} def",
564 565 566 567
  "/pdfIm1Bin {",
  "  /pdfImBuf1 4 index string def",
  "  { currentfile pdfImBuf1 readstring pop } image",
  "} def",
568
  "~1s",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
569 570 571 572 573 574 575 576 577 578 579
  "/pdfIm1Sep {",
  "  /pdfImBuf1 4 index string def",
  "  /pdfImBuf2 4 index string def",
  "  /pdfImBuf3 4 index string def",
  "  /pdfImBuf4 4 index string def",
  "  { currentfile pdfImBuf1 readhexstring pop }",
  "  { currentfile pdfImBuf2 readhexstring pop }",
  "  { currentfile pdfImBuf3 readhexstring pop }",
  "  { currentfile pdfImBuf4 readhexstring pop }",
  "  true 4 colorimage",
  "} def",
580 581 582 583 584 585 586 587 588 589 590
  "/pdfIm1SepBin {",
  "  /pdfImBuf1 4 index string def",
  "  /pdfImBuf2 4 index string def",
  "  /pdfImBuf3 4 index string def",
  "  /pdfImBuf4 4 index string def",
  "  { currentfile pdfImBuf1 readstring pop }",
  "  { currentfile pdfImBuf2 readstring pop }",
  "  { currentfile pdfImBuf3 readstring pop }",
  "  { currentfile pdfImBuf4 readstring pop }",
  "  true 4 colorimage",
  "} def",
591
  "~1ns",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
592
  "/pdfImM1 {",
593
  "  fCol /pdfImBuf1 4 index 7 add 8 idiv string def",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
594 595
  "  { currentfile pdfImBuf1 readhexstring pop } imagemask",
  "} def",
596 597 598 599
  "/pdfImM1Bin {",
  "  fCol /pdfImBuf1 4 index 7 add 8 idiv string def",
  "  { currentfile pdfImBuf1 readstring pop } imagemask",
  "} def",
600 601 602 603 604 605 606
  "/pdfImStr {",
  "  2 copy exch length lt {",
  "    2 copy get exch 1 add exch",
  "  } {",
  "    ()",
  "  } ifelse",
  "} def",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
607
  "/pdfImM1a {",
608
  "  { pdfImStr } imagemask",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
609 610
  "  pop pop",
  "} def",
611
  "~23sn",
612
  "% Level 2/3 image operators",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
613
  "/pdfImBuf 100 string def",
614 615 616 617 618 619 620 621
  "/pdfImStr {",
  "  2 copy exch length lt {",
  "    2 copy get exch 1 add exch",
  "  } {",
  "    ()",
  "  } ifelse",
  "} def",
  "/skipEOD {",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
622 623 624 625
  "  { currentfile pdfImBuf readline",
  "    not { pop exit } if",
  "    (%-EOD-) eq { exit } if } loop",
  "} def",
626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
  "/pdfIm { image skipEOD } def",
  "~3sn",
  "/pdfMask {",
  "  /ReusableStreamDecode filter",
  "  skipEOD",
  "  /maskStream exch def",
  "} def",
  "/pdfMaskEnd { maskStream closefile } def",
  "/pdfMaskInit {",
  "  /maskArray exch def",
  "  /maskIdx 0 def",
  "} def",
  "/pdfMaskSrc {",
  "  maskIdx maskArray length lt {",
  "    maskArray maskIdx get",
  "    /maskIdx maskIdx 1 add def",
  "  } {",
  "    ()",
  "  } ifelse",
  "} def",
646
  "~23s",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
647 648 649 650 651 652 653 654 655 656 657 658 659 660
  "/pdfImSep {",
  "  findcmykcustomcolor exch",
  "  dup /Width get /pdfImBuf1 exch string def",
  "  dup /Decode get aload pop 1 index sub /pdfImDecodeRange exch def",
  "  /pdfImDecodeLow exch def",
  "  begin Width Height BitsPerComponent ImageMatrix DataSource end",
  "  /pdfImData exch def",
  "  { pdfImData pdfImBuf1 readstring pop",
  "    0 1 2 index length 1 sub {",
  "      1 index exch 2 copy get",
  "      pdfImDecodeRange mul 255 div pdfImDecodeLow add round cvi",
  "      255 exch sub put",
  "    } for }",
  "  6 5 roll customcolorimage",
661
  "  skipEOD",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
662
  "} def",
663
  "~23sn",
664
  "/pdfImM { fCol imagemask skipEOD } def",
665
  "~123sn",
666
  "/pr { 2 index 2 index 3 2 roll putinterval 4 add } def",
667 668 669 670 671 672 673 674 675 676 677
  "/pdfImClip {",
  "  gsave",
  "  0 2 4 index length 1 sub {",
  "    dup 4 index exch 2 copy",
  "    get 5 index div put",
  "    1 add 3 index exch 2 copy",
  "    get 3 index div put",
  "  } for",
  "  pop pop rectclip",
  "} def",
  "/pdfImClipEnd { grestore } def",
678
  "~23sn",
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
  "% shading operators",
  "/colordelta {",
  "  false 0 1 3 index length 1 sub {",
  "    dup 4 index exch get 3 index 3 2 roll get sub abs 0.004 gt {",
  "      pop true",
  "    } if",
  "  } for",
  "  exch pop exch pop",
  "} def",
  "/funcCol { func n array astore } def",
  "/funcSH {",
  "  dup 0 eq {",
  "    true",
  "  } {",
  "    dup 6 eq {",
  "      false",
  "    } {",
  "      4 index 4 index funcCol dup",
  "      6 index 4 index funcCol dup",
  "      3 1 roll colordelta 3 1 roll",
  "      5 index 5 index funcCol dup",
  "      3 1 roll colordelta 3 1 roll",
  "      6 index 8 index funcCol dup",
  "      3 1 roll colordelta 3 1 roll",
  "      colordelta or or or",
  "    } ifelse",
  "  } ifelse",
  "  {",
  "    1 add",
  "    4 index 3 index add 0.5 mul exch 4 index 3 index add 0.5 mul exch",
  "    6 index 6 index 4 index 4 index 4 index funcSH",
  "    2 index 6 index 6 index 4 index 4 index funcSH",
  "    6 index 2 index 4 index 6 index 4 index funcSH",
  "    5 3 roll 3 2 roll funcSH pop pop",
  "  } {",
  "    pop 3 index 2 index add 0.5 mul 3 index  2 index add 0.5 mul",
715
  "~23n",
716
  "    funcCol sc",
717 718 719
  "~23s",
  "    funcCol aload pop k",
  "~23sn",
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
  "    dup 4 index exch mat transform m",
  "    3 index 3 index mat transform l",
  "    1 index 3 index mat transform l",
  "    mat transform l pop pop h f*",
  "  } ifelse",
  "} def",
  "/axialCol {",
  "  dup 0 lt {",
  "    pop t0",
  "  } {",
  "    dup 1 gt {",
  "      pop t1",
  "    } {",
  "      dt mul t0 add",
  "    } ifelse",
  "  } ifelse",
  "  func n array astore",
  "} def",
  "/axialSH {",
  "  dup 0 eq {",
  "    true",
  "  } {",
  "    dup 8 eq {",
  "      false",
  "    } {",
  "      2 index axialCol 2 index axialCol colordelta",
  "    } ifelse",
  "  } ifelse",
  "  {",
  "    1 add 3 1 roll 2 copy add 0.5 mul",
  "    dup 4 3 roll exch 4 index axialSH",
  "    exch 3 2 roll axialSH",
  "  } {",
753 754 755 756 757 758
  "    pop 2 copy add 0.5 mul",
  "~23n",
  "    axialCol sc",
  "~23s",
  "    axialCol aload pop k",
  "~23sn",
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
  "    exch dup dx mul x0 add exch dy mul y0 add",
  "    3 2 roll dup dx mul x0 add exch dy mul y0 add",
  "    dx abs dy abs ge {",
  "      2 copy yMin sub dy mul dx div add yMin m",
  "      yMax sub dy mul dx div add yMax l",
  "      2 copy yMax sub dy mul dx div add yMax l",
  "      yMin sub dy mul dx div add yMin l",
  "      h f*",
  "    } {",
  "      exch 2 copy xMin sub dx mul dy div add xMin exch m",
  "      xMax sub dx mul dy div add xMax exch l",
  "      exch 2 copy xMax sub dx mul dy div add xMax exch l",
  "      xMin sub dx mul dy div add xMin exch l",
  "      h f*",
  "    } ifelse",
  "  } ifelse",
  "} def",
  "/radialCol {",
  "  dup t0 lt {",
  "    pop t0",
  "  } {",
  "    dup t1 gt {",
  "      pop t1",
  "    } if",
  "  } ifelse",
  "  func n array astore",
  "} def",
  "/radialSH {",
  "  dup 0 eq {",
  "    true",
  "  } {",
  "    dup 8 eq {",
  "      false",
  "    } {",
  "      2 index dt mul t0 add radialCol",
  "      2 index dt mul t0 add radialCol colordelta",
  "    } ifelse",
  "  } ifelse",
  "  {",
  "    1 add 3 1 roll 2 copy add 0.5 mul",
  "    dup 4 3 roll exch 4 index radialSH",
  "    exch 3 2 roll radialSH",
  "  } {",
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  "    pop 2 copy add 0.5 mul dt mul t0 add",
  "~23n",
  "    radialCol sc",
  "~23s",
  "    radialCol aload pop k",
  "~23sn",
  "    encl {",
  "      exch dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      0 360 arc h",
  "      dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      360 0 arcn h f",
  "    } {",
  "      2 copy",
  "      dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      a1 a2 arcn",
  "      dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      a2 a1 arcn h",
  "      dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      a1 a2 arc",
  "      dup dx mul x0 add exch dup dy mul y0 add exch dr mul r0 add",
  "      a2 a1 arc h f",
  "    } ifelse",
824 825 826
  "  } ifelse",
  "} def",
  "~123sn",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
827
  "end",
828
  nullptr
Kristian Høgsberg's avatar
Kristian Høgsberg committed
829 830
};

831
static const char *cmapProlog[] = {
Kristian Høgsberg's avatar
Kristian Høgsberg committed
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872
  "/CIDInit /ProcSet findresource begin",
  "10 dict begin",
  "  begincmap",
  "  /CMapType 1 def",
  "  /CMapName /Identity-H def",
  "  /CIDSystemInfo 3 dict dup begin",
  "    /Registry (Adobe) def",
  "    /Ordering (Identity) def",
  "    /Supplement 0 def",
  "  end def",
  "  1 begincodespacerange",
  "    <0000> <ffff>",
  "  endcodespacerange",
  "  0 usefont",
  "  1 begincidrange",
  "    <0000> <ffff> 0",
  "  endcidrange",
  "  endcmap",
  "  currentdict CMapName exch /CMap defineresource pop",
  "end",
  "10 dict begin",
  "  begincmap",
  "  /CMapType 1 def",
  "  /CMapName /Identity-V def",
  "  /CIDSystemInfo 3 dict dup begin",
  "    /Registry (Adobe) def",
  "    /Ordering (Identity) def",
  "    /Supplement 0 def",
  "  end def",
  "  /WMode 1 def",
  "  1 begincodespacerange",
  "    <0000> <ffff>",
  "  endcodespacerange",
  "  0 usefont",
  "  1 begincidrange",
  "    <0000> <ffff> 0",
  "  endcidrange",
  "  endcmap",
  "  currentdict CMapName exch /CMap defineresource pop",
  "end",
  "end",
873
  nullptr
Kristian Høgsberg's avatar
Kristian Høgsberg committed
874 875 876 877 878 879 880
};

//------------------------------------------------------------------------
// Fonts
//------------------------------------------------------------------------

struct PSSubstFont {
881
  const char *psName;			// PostScript name
Kristian Høgsberg's avatar
Kristian Høgsberg committed
882 883 884
  double mWidth;		// width of 'm' character
};

885 886 887 888 889 890
// NB: must be in same order as base14SubstFonts in GfxFont.cc
static PSSubstFont psBase14SubstFonts[14] = {
  {"Courier",               0.600},
  {"Courier-Oblique",       0.600},
  {"Courier-Bold",          0.600},
  {"Courier-BoldOblique",   0.600},
Kristian Høgsberg's avatar
Kristian Høgsberg committed
891 892 893 894 895 896 897 898
  {"Helvetica",             0.833},
  {"Helvetica-Oblique",     0.833},
  {"Helvetica-Bold",        0.889},
  {"Helvetica-BoldOblique", 0.889},
  {"Times-Roman",           0.788},
  {"Times-Italic",          0.722},
  {"Times-Bold",            0.833},
  {"Times-BoldItalic",      0.778},
899 900 901 902 903 904 905 906 907 908
  // the last two are never used for substitution
  {"Symbol",                0},
  {"ZapfDingbats",          0}
};

// Mapping from Type 1/1C font file to PS font name.
struct PST1FontName {
  Ref fontFileID;
  GooString *psName;		// PostScript font name used for this
				//   embedded font file
Kristian Høgsberg's avatar
Kristian Høgsberg committed
909 910
};

911 912 913
// Info for 8-bit fonts
struct PSFont8Info {
  Ref fontID;
914
  int *codeToGID;		// code-to-GID mapping for TrueType fonts
915 916
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943
// Encoding info for substitute 16-bit font
struct PSFont16Enc {
  Ref fontID;
  GooString *enc;
};

//------------------------------------------------------------------------
// process colors
//------------------------------------------------------------------------

#define psProcessCyan     1
#define psProcessMagenta  2
#define psProcessYellow   4
#define psProcessBlack    8
#define psProcessCMYK    15

//------------------------------------------------------------------------
// PSOutCustomColor
//------------------------------------------------------------------------

class PSOutCustomColor {
public:

  PSOutCustomColor(double cA, double mA,
		   double yA, double kA, GooString *nameA);
  ~PSOutCustomColor();

944 945 946
  PSOutCustomColor(const PSOutCustomColor &) = delete;
  PSOutCustomColor& operator=(const PSOutCustomColor &) = delete;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
947 948 949 950 951 952 953 954 955 956 957 958
  double c, m, y, k;
  GooString *name;
  PSOutCustomColor *next;
};

PSOutCustomColor::PSOutCustomColor(double cA, double mA,
				   double yA, double kA, GooString *nameA) {
  c = cA;
  m = mA;
  y = yA;
  k = kA;
  name = nameA;
959
  next = nullptr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
960 961 962 963 964 965
}

PSOutCustomColor::~PSOutCustomColor() {
  delete name;
}

966 967 968 969 970 971
//------------------------------------------------------------------------

struct PSOutImgClipRect {
  int x0, x1, y0, y1;
};

972 973 974
//------------------------------------------------------------------------

struct PSOutPaperSize {
975 976
  PSOutPaperSize(GooString *nameA, int wA, int hA) { name = nameA; w = wA; h = hA; }
  ~PSOutPaperSize() { delete name; }
977 978
  PSOutPaperSize(const PSOutPaperSize &) = delete;
  PSOutPaperSize& operator=(const PSOutPaperSize &) = delete;
979
  GooString *name;
980 981 982
  int w, h;
};

Kristian Høgsberg's avatar
Kristian Høgsberg committed
983 984 985 986 987 988 989 990 991
//------------------------------------------------------------------------
// DeviceNRecoder
//------------------------------------------------------------------------

class DeviceNRecoder: public FilterStream {
public:

  DeviceNRecoder(Stream *strA, int widthA, int heightA,
		 GfxImageColorMap *colorMapA);
Albert Astals Cid's avatar
Albert Astals Cid committed
992
  ~DeviceNRecoder();
993
  StreamKind getKind() const override { return strWeird; }
Albert Astals Cid's avatar
Albert Astals Cid committed
994 995
  void reset() override;
  int getChar() override
Kristian Høgsberg's avatar
Kristian Høgsberg committed
996
    { return (bufIdx >= bufSize && !fillBuf()) ? EOF : buf[bufIdx++]; }
Albert Astals Cid's avatar
Albert Astals Cid committed
997
  int lookChar() override
Kristian Høgsberg's avatar
Kristian Høgsberg committed
998
    { return (bufIdx >= bufSize && !fillBuf()) ? EOF : buf[bufIdx]; }
999
  GooString *getPSFilter(int psLevel, const char *indent) override { return nullptr; }
1000 1001
  bool isBinary(bool last = true) override { return true; }
  bool isEncoder() override { return true; }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1002 1003 1004

private:

1005
  bool fillBuf();
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1006 1007 1008

  int width, height;
  GfxImageColorMap *colorMap;
1009
  const Function *func;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  ImageStream *imgStr;
  int buf[gfxColorMaxComps];
  int pixelIdx;
  int bufIdx;
  int bufSize;
};

DeviceNRecoder::DeviceNRecoder(Stream *strA, int widthA, int heightA,
			       GfxImageColorMap *colorMapA):
    FilterStream(strA) {
  width = widthA;
  height = heightA;
  colorMap = colorMapA;
1023
  imgStr = nullptr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
  pixelIdx = 0;
  bufIdx = gfxColorMaxComps;
  bufSize = ((GfxDeviceNColorSpace *)colorMap->getColorSpace())->
              getAlt()->getNComps();
  func = ((GfxDeviceNColorSpace *)colorMap->getColorSpace())->
           getTintTransformFunc();
}

DeviceNRecoder::~DeviceNRecoder() {
  if (imgStr) {
    delete imgStr;
  }
1036 1037 1038
  if (str->isEncoder()) {
    delete str;
  }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1039 1040 1041 1042 1043 1044 1045 1046
}

void DeviceNRecoder::reset() {
  imgStr = new ImageStream(str, width, colorMap->getNumPixelComps(),
			   colorMap->getBits());
  imgStr->reset();
}

1047
bool DeviceNRecoder::fillBuf() {
1048
  unsigned char pixBuf[gfxColorMaxComps];
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1049
  GfxColor color;
1050
  double x[gfxColorMaxComps], y[gfxColorMaxComps];
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1051 1052 1053
  int i;

  if (pixelIdx >= width * height) {
1054
    return false;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1055 1056 1057
  }
  imgStr->getPixel(pixBuf);
  colorMap->getColor(pixBuf, &color);
1058 1059 1060 1061 1062 1063
  for (i = 0;
       i < ((GfxDeviceNColorSpace *)colorMap->getColorSpace())->getNComps();
       ++i) {
    x[i] = colToDbl(color.c[i]);
  }
  func->transform(x, y);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1064 1065 1066 1067 1068
  for (i = 0; i < bufSize; ++i) {
    buf[i] = (int)(y[i] * 255 + 0.5);
  }
  bufIdx = 0;
  ++pixelIdx;
1069
  return true;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
}

//------------------------------------------------------------------------
// PSOutputDev
//------------------------------------------------------------------------

extern "C" {
typedef void (*SignalFunc)(int);
}

1080
static void outputToFile(void *stream, const char *data, int len) {
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1081 1082 1083
  fwrite(data, 1, len, (FILE *)stream);
}

1084
PSOutputDev::PSOutputDev(const char *fileName, PDFDoc *docA,
1085
			 char *psTitleA,
1086
			 const std::vector<int> &pagesA, PSOutMode modeA,
1087
			 int paperWidthA, int paperHeightA,
1088
                         bool noCropA, bool duplexA,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1089
			 int imgLLXA, int imgLLYA, int imgURXA, int imgURYA,
1090 1091
			 bool forceRasterizeA,
			 bool manualCtrlA,
1092 1093
			 PSOutCustomCodeCbk customCodeCbkA,
			 void *customCodeCbkDataA) {
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1094 1095 1096
  FILE *f;
  PSFileType fileTypeA;

1097 1098 1099 1100
  underlayCbk = nullptr;
  underlayCbkData = nullptr;
  overlayCbk = nullptr;
  overlayCbkData = nullptr;
1101 1102
  customCodeCbk = customCodeCbkA;
  customCodeCbkData = customCodeCbkDataA;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1103

1104 1105 1106 1107 1108 1109 1110 1111 1112
  fontIDs = nullptr;
  t1FontNames = nullptr;
  font8Info = nullptr;
  font16Enc = nullptr;
  imgIDs = nullptr;
  formIDs = nullptr;
  paperSizes = nullptr;
  embFontList = nullptr;
  customColors = nullptr;
1113
  haveTextClip = false;
1114
  t3String = nullptr;
1115
  forceRasterize = forceRasterizeA;
1116
  psTitle = nullptr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1117 1118 1119 1120 1121 1122 1123 1124

  // open file or pipe
  if (!strcmp(fileName, "-")) {
    fileTypeA = psStdout;
    f = stdout;
  } else if (fileName[0] == '|') {
    fileTypeA = psPipe;
#ifdef HAVE_POPEN
1125
#ifndef _WIN32
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1126 1127 1128
    signal(SIGPIPE, (SignalFunc)SIG_IGN);
#endif
    if (!(f = popen(fileName + 1, "w"))) {
1129
      error(errIO, -1, "Couldn't run print command '{0:s}'", fileName);
1130
      ok = false;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1131 1132 1133
      return;
    }
#else
1134
    error(errIO, -1, "Print commands are not supported ('{0:s}')", fileName);
1135
    ok = false;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1136 1137 1138 1139
    return;
#endif
  } else {
    fileTypeA = psFile;
1140
    if (!(f = openFile(fileName, "w"))) {
1141
      error(errIO, -1, "Couldn't open PostScript file '{0:s}'", fileName);
1142
      ok = false;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1143 1144 1145 1146
      return;
    }
  }

1147
  init(outputToFile, f, fileTypeA, psTitleA,
1148
       docA, pagesA, modeA,
1149
       imgLLXA, imgLLYA, imgURXA, imgURYA, manualCtrlA,
1150
       paperWidthA, paperHeightA, noCropA, duplexA);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1151 1152 1153
}

PSOutputDev::PSOutputDev(PSOutputFunc outputFuncA, void *outputStreamA,
1154
			 char *psTitleA,
1155 1156
			 PDFDoc *docA,
			 const std::vector<int> &pagesA, PSOutMode modeA,
1157
			 int paperWidthA, int paperHeightA,
1158
                         bool noCropA, bool duplexA,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1159
			 int imgLLXA, int imgLLYA, int imgURXA, int imgURYA,
1160 1161
			 bool forceRasterizeA,
			 bool manualCtrlA,
1162 1163
			 PSOutCustomCodeCbk customCodeCbkA,
			 void *customCodeCbkDataA) {
1164 1165 1166 1167
  underlayCbk = nullptr;
  underlayCbkData = nullptr;
  overlayCbk = nullptr;
  overlayCbkData = nullptr;
1168 1169
  customCodeCbk = customCodeCbkA;
  customCodeCbkData = customCodeCbkDataA;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1170

1171 1172 1173 1174 1175 1176 1177 1178 1179
  fontIDs = nullptr;
  t1FontNames = nullptr;
  font8Info = nullptr;
  font16Enc = nullptr;
  imgIDs = nullptr;
  formIDs = nullptr;
  paperSizes = nullptr;
  embFontList = nullptr;
  customColors = nullptr;
1180
  haveTextClip = false;
1181
  t3String = nullptr;
1182
  forceRasterize = forceRasterizeA;
1183
  psTitle = nullptr;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1184

1185
  init(outputFuncA, outputStreamA, psGeneric, psTitleA,
1186
       docA, pagesA, modeA,
1187
       imgLLXA, imgLLYA, imgURXA, imgURYA, manualCtrlA,
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
       paperWidthA, paperHeightA, noCropA, duplexA);
}

struct StandardMedia {
    const char *name;
    int width;
    int height;
};

static const StandardMedia standardMedia[] =
{
    { "A0",       2384, 3371 },
    { "A1",       1685, 2384 },
    { "A2",       1190, 1684 },
    { "A3",        842, 1190 },
    { "A4",        595,  842 },
    { "A5",        420,  595 },
    { "B4",        729, 1032 },
    { "B5",        516,  729 },
    { "Letter",    612,  792 },
    { "Tabloid",   792, 1224 },
    { "Ledger",   1224,  792 },
    { "Legal",     612, 1008 },
    { "Statement", 396,  612 },
    { "Executive", 540,  720 },
    { "Folio",     612,  936 },
    { "Quarto",    610,  780 },
    { "10x14",     720, 1008 },
1216
    { nullptr,          0,    0 }
1217 1218 1219 1220 1221
};

/* PLRM specifies a tolerance of 5 points when matching page sizes */
static bool pageDimensionEqual(int a, int b) {
  return (abs (a - b) < 5);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1222 1223
}

1224 1225 1226 1227
// Shared initialization of PSOutputDev members.
//   Store the values but do not process them so the function that
//   created the PSOutputDev can use the various setters to change defaults.

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1228
void PSOutputDev::init(PSOutputFunc outputFuncA, void *outputStreamA,
1229
		       PSFileType fileTypeA, char *psTitleA, PDFDoc *docA,
1230
		       const std::vector<int> &pagesA, PSOutMode modeA,
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1231
		       int imgLLXA, int imgLLYA, int imgURXA, int imgURYA,
1232 1233
		       bool manualCtrlA, int paperWidthA, int paperHeightA,
		       bool noCropA, bool duplexA) {
1234 1235

  if (pagesA.empty()) {
1236
    ok = false;
1237 1238
    return;
  }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1239 1240

  // initialize
1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
  postInitDone = false;
  embedType1 = true;
  embedTrueType = true;
  embedCIDPostScript = true;
  embedCIDTrueType = true;
  fontPassthrough = false;
  optimizeColorSpace = false;
  passLevel1CustomColor = false;
  preloadImagesForms = false;
  generateOPI = false;
  useASCIIHex = false;
  useBinary = false;
  enableLZW = true;
  enableFlate = true;
  rasterMono = false;
1256
  rasterResolution = 300;
1257 1258 1259 1260 1261
  uncompressPreloadedImages = false;
  psCenter = true;
  rasterAntialias = false;
  displayText = true;
  ok = true;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1262 1263 1264
  outputFunc = outputFuncA;
  outputStream = outputStreamA;
  fileType = fileTypeA;
1265
  psTitle = (psTitleA? strdup(psTitleA): nullptr);
1266
  doc = docA;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1267
  level = globalParams->getPSLevel();
1268
  pages = pagesA;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1269
  mode = modeA;
1270 1271
  paperWidth = paperWidthA;
  paperHeight = paperHeightA;
1272
  noCrop = noCropA;
1273
  duplex = duplexA;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1274 1275 1276 1277
  imgLLX = imgLLXA;
  imgLLY = imgLLYA;
  imgURX = imgURXA;
  imgURY = imgURYA;
1278 1279
  manualCtrl = manualCtrlA;

1280
  xref = nullptr;
1281 1282

  processColors = 0;
1283 1284 1285
  inType3Char = false;
  inUncoloredPattern = false;
  t3FillColorOnly = false;
1286

1287
#ifdef OPI_SUPPORT
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
  // initialize OPI nesting levels
  opi13Nest = 0;
  opi20Nest = 0;
#endif

  tx0 = ty0 = -1;
  xScale0 = yScale0 = 0;
  rotate0 = -1;
  clipLLX0 = clipLLY0 = 0;
  clipURX0 = clipURY0 = -1;

  // initialize sequential page number
  seqPage = 1;
}

// Complete the initialization after the function that created the PSOutputDev
//   has had a chance to modify default values with the various setters.

void PSOutputDev::postInit()
{
  Catalog *catalog;
  PDFRectangle *box;
  PSOutPaperSize *size;
  int w, h, i;

  if (postInitDone || !ok) {
    return;
  }

1317
  postInitDone = true;
1318 1319 1320 1321

  xref = doc->getXRef();
  catalog = doc->getCatalog();

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1322
  if (paperWidth < 0 || paperHeight < 0) {
1323
    paperMatch = true;
1324
  } else {
1325
    paperMatch = false;
1326
  }
1327

1328
  paperSizes = new std::vector<PSOutPaperSize*>();
1329 1330
  for (size_t pgi = 0; pgi < pages.size(); ++pgi) {
    const int pg = pages[pgi];
1331
    Page *page = catalog->getPage(pg);
1332
    if (page == nullptr)
1333
      paperMatch = false;
1334 1335 1336
    if (!paperMatch) {
      w = paperWidth;
      h = paperHeight;
1337 1338 1339 1340 1341 1342 1343
      if (w < 0 || h < 0) {
        // Unable to obtain a paper size from the document and no page size
        // specified. In this case use A4 as the page size to ensure the PS output is
        // valid. This will only occur if the PDF is very broken.
        w = 595;
        h = 842;
      }
1344
    } else if (noCrop) {
1345 1346
      w = (int)ceil(page->getMediaWidth());
      h = (int)ceil(page->getMediaHeight());
1347 1348 1349 1350 1351
    } else {
      w = (int)ceil(page->getCropWidth());
      h = (int)ceil(page->getCropHeight());
    }
    if (paperMatch) {
1352 1353
      const int pageRotate = page->getRotate();
      if (pageRotate == 90 || pageRotate == 270)
1354 1355 1356 1357 1358 1359
        std::swap(w, h);
    }
    if (w  > paperWidth)
      paperWidth = w;
    if (h  > paperHeight)
      paperHeight = h;
1360
    for (i = 0; i < (int)paperSizes->size(); ++i) {
1361
      size = (*paperSizes)[i];
1362 1363 1364
      if (pageDimensionEqual(w, size->w) && pageDimensionEqual(h, size->h))
        break;
    }
1365
    if (i == (int)paperSizes->size()) {
1366
      const StandardMedia *media = standardMedia;
1367
      GooString *name = nullptr;
1368 1369 1370 1371 1372 1373 1374 1375
      while (media->name) {
        if (pageDimensionEqual(w, media->width) && pageDimensionEqual(h, media->height)) {
          name = new GooString(media->name);
          w = media->width;
          h = media->height;
          break;
        }
        media++;
1376
      }
1377 1378
      if (!name)
        name = GooString::format("{0:d}x{1:d}mm", int(w*25.4/72), int(h*25.4/72));
1379
      paperSizes->push_back(new PSOutPaperSize(name, w, h));
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1380
    }
1381 1382 1383
    pagePaperSize.insert(std::pair<int,int>(pg, i));
    if (!paperMatch)
      break; // we only need one entry when all pages are the same size
1384
  }
1385 1386 1387 1388 1389
  if (imgLLX == 0 && imgURX == 0 && imgLLY == 0 && imgURY == 0) {
    imgLLX = imgLLY = 0;
    imgURX = paperWidth;
    imgURY = paperHeight;
  }
1390
  std::vector<int> pageList;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1391
  if (mode == psModeForm) {
1392
    pageList.push_back(pages[0]);
1393
  } else {
1394
    pageList = pages;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1395 1396 1397 1398 1399
  }

  // initialize fontIDs, fontFileIDs, and fontFileNames lists
  fontIDSize = 64;
  fontIDLen = 0;
1400
  fontIDs = (Ref *)gmallocn(fontIDSize, sizeof(Ref));
1401
  for (i = 0; i < 14; ++i) {
1402
    fontNames.emplace(psBase14SubstFonts[i].psName);
1403 1404 1405 1406
  }
  t1FontNameSize = 64;
  t1FontNameLen = 0;
  t1FontNames = (PST1FontName *)gmallocn(t1FontNameSize, sizeof(PST1FontName));
1407 1408
  font8InfoLen = 0;
  font8InfoSize = 0;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1409 1410
  font16EncLen = 0;
  font16EncSize = 0;
1411 1412 1413 1414
  imgIDLen = 0;
  imgIDSize = 0;
  formIDLen = 0;
  formIDSize = 0;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1415 1416

  numSaves = 0;
1417 1418
  numTilingPatterns = 0;
  nextFunc = 0;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1419 1420 1421 1422 1423

  // initialize embedded font resource comment list
  embFontList = new GooString();

  if (!manualCtrl) {
Hib Eris's avatar
Hib Eris committed
1424
    Page *page;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1425
    // this check is needed in case the document has zero pages
1426
    if ((page = doc->getPage(pageList[0]))) {
1427
      writeHeader(pageList.size(),
Hib Eris's avatar
Hib Eris committed
1428 1429 1430
		  page->getMediaBox(),
		  page->getCropBox(),
		  page->getRotate(),
1431
		  psTitle);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1432
    } else {
1433
      error(errSyntaxError, -1, "Invalid page {0:d}", pageList[0]);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1434
      box = new PDFRectangle(0, 0, 1, 1);
1435
      writeHeader(pageList.size(), box, box, 0, psTitle);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
      delete box;
    }
    if (mode != psModeForm) {
      writePS("%%BeginProlog\n");
    }
    writeXpdfProcset();
    if (mode != psModeForm) {
      writePS("%%EndProlog\n");
      writePS("%%BeginSetup\n");
    }
1446
    writeDocSetup(catalog, pageList, duplex);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457
    if (mode != psModeForm) {
      writePS("%%EndSetup\n");
    }
  }
}

PSOutputDev::~PSOutputDev() {
  PSOutCustomColor *cc;
  int i;

  if (ok) {
1458 1459 1460
    if (!postInitDone) {
      postInit();
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
    if (!manualCtrl) {
      writePS("%%Trailer\n");
      writeTrailer();
      if (mode != psModeForm) {
	writePS("%%EOF\n");
      }
    }
    if (fileType == psFile) {
      fclose((FILE *)outputStream);
    }
#ifdef HAVE_POPEN
    else if (fileType == psPipe) {
      pclose((FILE *)outputStream);
1474
#ifndef _WIN32
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1475 1476 1477 1478 1479
      signal(SIGPIPE, (SignalFunc)SIG_DFL);
#endif
    }
#endif
  }
1480
  if (paperSizes) {
1481 1482 1483 1484
    for (auto entry : *paperSizes) {
      delete entry;
    }
    delete paperSizes;
1485
  }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1486 1487 1488 1489 1490 1491
  if (embFontList) {
    delete embFontList;
  }
  if (fontIDs) {
    gfree(fontIDs);
  }
1492 1493 1494
  if (t1FontNames) {
    for (i = 0; i < t1FontNameLen; ++i) {
      delete t1FontNames[i].psName;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1495
    }
1496
    gfree(t1FontNames);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1497
  }
1498 1499 1500 1501 1502 1503
  if (font8Info) {
    for (i = 0; i < font8InfoLen; ++i) {
      gfree(font8Info[i].codeToGID);
    }
    gfree(font8Info);
  }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1504 1505
  if (font16Enc) {
    for (i = 0; i < font16EncLen; ++i) {
1506 1507 1508
      if (font16Enc[i].enc) {
	delete font16Enc[i].enc;
      }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1509 1510 1511
    }
    gfree(font16Enc);
  }
1512 1513
  gfree(imgIDs);
  gfree(formIDs);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1514 1515 1516 1517 1518
  while (customColors) {
    cc = customColors;
    customColors = cc->next;
    delete cc;
  }
1519
  gfree(psTitle);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1520 1521
}

1522
void PSOutputDev::writeHeader(int nPages,
Albert Astals Cid's avatar
Albert Astals Cid committed
1523
			      const PDFRectangle *mediaBox, const PDFRectangle *cropBox,
1524
			      int pageRotate, const char *title) {
1525 1526
  PSOutPaperSize *size;
  double x1, y1, x2, y2;
1527

Kristian Høgsberg's avatar
Kristian Høgsberg committed
1528 1529 1530
  switch (mode) {
  case psModePS:
    writePS("%!PS-Adobe-3.0\n");
1531 1532 1533 1534 1535 1536 1537 1538
    break;
  case psModeEPS:
    writePS("%!PS-Adobe-3.0 EPSF-3.0\n");
    break;
  case psModeForm:
    writePS("%!PS-Adobe-3.0 Resource-Form\n");
    break;
  }
1539
  writePSFmt("%Produced by poppler pdftops version: {0:s} (http://poppler.freedesktop.org)\n", PACKAGE_VERSION);
Albert Astals Cid's avatar
Albert Astals Cid committed
1540 1541 1542 1543 1544 1545 1546
  Object info = xref->getDocInfo();
  if (info.isDict()) {
    Object obj1 = info.dictLookup("Creator");
    if (obj1.isString()) {
        writePS("%%Creator: ");
        writePSTextLine(obj1.getString());
    }
1547
  }
1548 1549
  if(title) {
    char *sanitizedTitle = strdup(title);
1550
    for (size_t i = 0; i < strlen(sanitizedTitle); ++i) {
1551 1552
      if (sanitizedTitle[i] == '\n' || sanitizedTitle[i] == '\r') {
        sanitizedTitle[i] = ' ';
1553 1554
      }
    }
1555 1556
    writePSFmt("%%Title: {0:s}\n", sanitizedTitle);
    free(sanitizedTitle);
1557
  }
1558 1559 1560 1561 1562 1563 1564 1565
  writePSFmt("%%LanguageLevel: {0:d}\n",
	     (level == psLevel1 || level == psLevel1Sep) ? 1 :
	     (level == psLevel2 || level == psLevel2Sep) ? 2 : 3);
  if (level == psLevel1Sep || level == psLevel2Sep || level == psLevel3Sep) {
    writePS("%%DocumentProcessColors: (atend)\n");
    writePS("%%DocumentCustomColors: (atend)\n");
  }
  writePS("%%DocumentSuppliedResources: (atend)\n");
1566
  if ((level == psLevel1 || level == psLevel1Sep) && useBinary) {
1567 1568
    writePS("%%DocumentData: Binary\n");
  }
1569 1570 1571

  switch (mode) {
  case psModePS:
1572
    for (std::size_t i = 0; i < paperSizes->size(); ++i) {
1573
      size = (*paperSizes)[i];
1574 1575
      writePSFmt("%%{0:s} {1:t} {2:d} {3:d} 0 () ()\n",
                 i==0 ? "DocumentMedia:" : "+", size->name, size->w, size->h);
1576
    }
1577
    writePSFmt("%%BoundingBox: 0 0 {0:d} {1:d}\n", paperWidth, paperHeight);
1578
    writePSFmt("%%Pages: {0:d}\n", nPages);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1579
    writePS("%%EndComments\n");
1580
    if (!paperMatch) {
1581
      size = (*paperSizes)[0];
1582
      writePS("%%BeginDefaults\n");
1583
      writePSFmt("%%PageMedia: {0:t}\n", size->name);
1584 1585
      writePS("%%EndDefaults\n");
    }
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1586 1587
    break;
  case psModeEPS:
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
    epsX1 = cropBox->x1;
    epsY1 = cropBox->y1;
    epsX2 = cropBox->x2;
    epsY2 = cropBox->y2;
    if (pageRotate == 0 || pageRotate == 180) {
      x1 = epsX1;
      y1 = epsY1;
      x2 = epsX2;
      y2 = epsY2;
    } else { // pageRotate == 90 || pageRotate == 270
      x1 = 0;
      y1 = 0;
      x2 = epsY2 - epsY1;
      y2 = epsX2 - epsX1;
    }
1603
    writePSFmt("%%BoundingBox: {0:d} {1:d} {2:d} {3:d}\n",
1604
	       (int)floor(x1), (int)floor(y1), (int)ceil(x2), (int)ceil(y2));
1605
    writePSFmt("%%HiResBoundingBox: {0:.6g} {1:.6g} {2:.6g} {3:.6g}\n",
1606
		 x1, y1, x2, y2);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1607 1608 1609 1610 1611 1612
    writePS("%%DocumentSuppliedResources: (atend)\n");
    writePS("%%EndComments\n");
    break;
  case psModeForm:
    writePS("%%EndComments\n");
    writePS("32 dict dup begin\n");
1613
    writePSFmt("/BBox [{0:d} {1:d} {2:d} {3:d}] def\n",
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1614 1615 1616 1617 1618 1619 1620 1621 1622
	       (int)floor(mediaBox->x1), (int)floor(mediaBox->y1),
	       (int)ceil(mediaBox->x2), (int)ceil(mediaBox->y2));
    writePS("/FormType 1 def\n");
    writePS("/Matrix [1 0 0 1 0 0] def\n");
    break;
  }
}

void PSOutputDev::writeXpdfProcset() {
1623
  bool lev1, lev2, lev3, sep, nonSep;
1624 1625
  const char **p;
  const char *q;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
1626

1627
  writePSFmt("%%BeginResource: procset xpdf {0:s} 0\n", "3.00");