src/gui/painting/qpsprinter.ps
changeset 0 1918ee327afb
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 % the postscript header we use for our qpsprinter in uncompressed and commented form.
       
     2 % use the makepsheader perl script to generate a compressed version of this header
       
     3 % you can then paste into qpsprinter.cpp
       
     4 %
       
     5 % some compression of the code is done by the makepsheader script, so we don't need to 
       
     6 % write too criptically here. 
       
     7 
       
     8 /BD  {bind def} bind def
       
     9 /d2 {dup dup} BD
       
    10 /ED {exch def} BD
       
    11 /D0 {0 ED} BD
       
    12 
       
    13 /F  {setfont} BD
       
    14 /RL {rlineto} BD
       
    15 /CM {currentmatrix} BD
       
    16 /SM {setmatrix} BD
       
    17 /TR {translate} BD
       
    18 /SD {setdash} BD
       
    19 /SC {aload pop setrgbcolor} BD
       
    20 /CR {currentfile read pop} BD
       
    21 /i  {index} BD
       
    22 /scs {setcolorspace} BD
       
    23 /DB {dict dup begin} BD
       
    24 /DE {end def} BD
       
    25 /ie {ifelse} BD
       
    26 /gs {gsave} BD
       
    27 /gr {grestore} BD
       
    28 
       
    29 % these use PDF syntax
       
    30 /w {setlinewidth} BD
       
    31 /d {setdash} BD
       
    32 /J {setlinecap} BD
       
    33 /j {setlinejoin} BD
       
    34 /scn {3 array astore /BCol exch def} BD
       
    35 /SCN {3 array astore /PCol exch def} BD
       
    36 /cm {6 array astore concat} BD
       
    37 
       
    38 /m  {moveto} BD
       
    39 /l  {lineto} BD
       
    40 /c  {curveto} BD
       
    41 /h  {closepath} BD
       
    42 
       
    43 /W  {clip} BD
       
    44 /W* {eoclip} BD
       
    45 /n {newpath} BD
       
    46 % ENDUNCOMPRESSED: Warning: leave this line in. 
       
    47 % Everything before this line will be left untouched by the compression
       
    48 
       
    49 /q  {gsave 10 dict begin} BD
       
    50 /Q  {end grestore} BD
       
    51 
       
    52 % PDF operators
       
    53 /re { % PDF re operator
       
    54   4 2 roll  % w h x y
       
    55   moveto % w h
       
    56   dup % w h h
       
    57   0 exch rlineto % w h
       
    58   exch 0 rlineto % h
       
    59   0 exch neg rlineto
       
    60   closepath
       
    61 } bind def
       
    62 
       
    63 /S {
       
    64   gsave
       
    65     PCol SC stroke
       
    66   grestore
       
    67   newpath
       
    68 } BD
       
    69 
       
    70 % PDF text operators
       
    71 /BT {gsave 10 dict begin /_m matrix currentmatrix def BCol SC} BD
       
    72 /ET {end grestore} BD
       
    73 /Tf {
       
    74   /_fs exch def
       
    75   findfont
       
    76   [ _fs 0 0 _fs 0 0 ]  
       
    77   makefont
       
    78   setfont
       
    79 } BD
       
    80 /Tm {6 array astore concat} BD
       
    81 /Td {translate} BD
       
    82 /Tj {0 0 moveto show} BD
       
    83 /BDC {pop pop} BD
       
    84 /EMC {} BD
       
    85 
       
    86 % old operators
       
    87 
       
    88 /BSt 0 def                             % brush style
       
    89 /WFi false def                 % winding fill
       
    90 
       
    91 /BCol  [ 1 1 1 ] def                   % brush color
       
    92 /PCol  [ 0 0 0 ] def                   % pen color
       
    93 /BDArr [                             % Brush dense patterns
       
    94   0.94
       
    95   0.88
       
    96   0.63
       
    97   0.50
       
    98   0.37 
       
    99   0.12 
       
   100   0.06 
       
   101 ] def
       
   102 
       
   103 % -- level3 true/false
       
   104 /level3 {
       
   105   /languagelevel where {
       
   106     pop
       
   107     languagelevel 3 ge 
       
   108   } { false } ifelse
       
   109 } bind def
       
   110 
       
   111 
       
   112 %% image drawing routines
       
   113 
       
   114 % defines for QCI
       
   115 /QCIgray D0 /QCIcolor D0 /QCIindex D0
       
   116 
       
   117 % this method prints color images if colorimage is available, otherwise
       
   118 % converts the string to a grayscale image and uses the reular postscript image
       
   119 % operator for printing.
       
   120 % Arguments are the same as for the image operator:
       
   121 % 
       
   122 %     width height bits/sample matrix datasrc QCI -
       
   123 /QCI {
       
   124   /colorimage where {
       
   125     pop
       
   126     false 3 colorimage
       
   127   }{  % the hard way, based on PD code by John Walker <kelvin@autodesk.com>
       
   128     exec /QCIcolor exch def
       
   129     /QCIgray QCIcolor length 3 idiv string def
       
   130     0 1 QCIcolor length 3 idiv 1 sub
       
   131     { /QCIindex exch def
       
   132       /_x QCIindex 3 mul def
       
   133       QCIgray QCIindex
       
   134       QCIcolor _x       get 0.30 mul
       
   135       QCIcolor _x 1 add get 0.59 mul
       
   136       QCIcolor _x 2 add get 0.11 mul
       
   137       add add cvi
       
   138       put
       
   139     } for
       
   140     QCIgray image
       
   141   } ifelse
       
   142 } bind def
       
   143 
       
   144 % general image drawing routine, used from the postscript driver
       
   145 %
       
   146 % Draws images with and without mask with 1, 8 and 24(rgb) bits depth.
       
   147 %
       
   148 %     width height matrix image 1|8|24 mask|false x y di
       
   149 %
       
   150 % width and height specify the width/height of the image,
       
   151 % matrix a transformation matrix, image a procedure holding the image data
       
   152 % (same for mask) and x/y an additional translation.
       
   153 %
       
   154 % ### should move the translation into the matrix!!!
       
   155 /di 
       
   156 {
       
   157   gsave
       
   158     translate
       
   159     1 index 1 eq { % bitmap
       
   160       pop pop % get rid of mask and depth
       
   161       false 3 1 roll % width height false matrix image
       
   162       BCol SC
       
   163       imagemask
       
   164     } { 
       
   165       dup false ne { 
       
   166 	% have a mask, see if we can use it
       
   167 	level3
       
   168       } { 
       
   169 	false 
       
   170       } ifelse
       
   171 
       
   172       {
       
   173 	% languagelevel3, we can use image mask and dicts
       
   174 
       
   175 	% store the image mask
       
   176 	/_ma exch def
       
   177 	% select colorspace according to 8|24 bit depth and set the decode array /dc
       
   178 	8 eq {
       
   179 	  /_dc [0 1] def
       
   180 	  /DeviceGray
       
   181 	} {
       
   182 	  /_dc [0 1 0 1 0 1] def
       
   183 	  /DeviceRGB
       
   184 	} ifelse
       
   185 	setcolorspace
       
   186 	% the image data
       
   187 	/_im exch def
       
   188 	% transformation matrix
       
   189 	/_mt exch def
       
   190 	% width and height
       
   191 	/_h exch def
       
   192 	/_w exch def
       
   193 	% and the combined image dict
       
   194 	<<
       
   195 	  /ImageType 3
       
   196 	  % the image dict
       
   197 	  /DataDict <<
       
   198               /ImageType 1
       
   199 	      /Width _w
       
   200 	      /Height _h
       
   201 	      /ImageMatrix _mt
       
   202 	      /DataSource _im
       
   203 	      /BitsPerComponent 8
       
   204 	      /Decode _dc
       
   205 	  >> 
       
   206 	  % the mask dictionary
       
   207 	  /MaskDict <<
       
   208              /ImageType 1
       
   209 	     /Width _w
       
   210 	     /Height _h
       
   211 	     /ImageMatrix _mt
       
   212 	     /DataSource _ma
       
   213 	     /BitsPerComponent 1
       
   214 	     /Decode [0 1]
       
   215 	  >> 
       
   216 	  /InterleaveType 3
       
   217 	>> 
       
   218 	image
       
   219       } {
       
   220 	pop % no mask or can't use it, get rid of it
       
   221 	8 % width height image 8|24 8 matrix
       
   222 	4 1 roll
       
   223 	8 eq { % grayscale
       
   224 	  image
       
   225 	} { %color
       
   226 	  QCI
       
   227 	} ifelse
       
   228       } ifelse
       
   229     } ifelse
       
   230   grestore    
       
   231 } bind def
       
   232 
       
   233 
       
   234 /BF {                                % brush fill
       
   235   gsave
       
   236     BSt 1 eq                          % solid brush?
       
   237     {
       
   238       BCol SC
       
   239       WFi { fill } { eofill } ifelse
       
   240     } if
       
   241     BSt 2 ge BSt 8 le and             % dense pattern?
       
   242     {
       
   243       BDArr BSt 2 sub get /_sc exch def 
       
   244       % the following line scales the brush color according to the pattern. the higher the pattern the lighter the color.
       
   245       BCol 
       
   246       { 
       
   247 	1. exch sub _sc mul 1. exch sub 
       
   248       } forall 
       
   249       3 array astore
       
   250       SC 
       
   251       WFi { fill } { eofill } ifelse
       
   252     } if
       
   253     BSt 9 ge BSt 14 le and            % brush pattern?
       
   254     {
       
   255       WFi { clip } { eoclip } ifelse
       
   256       pathbbox                        % left upper right lower
       
   257       3 index 3 index translate
       
   258       4 2 roll                        % right lower left upper
       
   259       3 2 roll                        % right left upper lower
       
   260       exch                            % left right lower upper
       
   261       sub /_h exch def
       
   262       sub /_w exch def
       
   263       BCol SC
       
   264       0.3 setlinewidth
       
   265       newpath
       
   266       BSt 9 eq BSt 11 eq or           % horiz or cross pattern
       
   267       { 0 4 _h
       
   268 	{ dup 0 exch moveto _w exch lineto } for
       
   269       } if
       
   270       BSt 10 eq BSt 11 eq or          % vert or cross pattern
       
   271       { 0 4 _w
       
   272 	{ dup 0 moveto _h lineto } for
       
   273       } if
       
   274       BSt 12 eq BSt 14 eq or          % F-diag or diag cross
       
   275       { _w _h gt
       
   276 	{ 0 6 _w _h add
       
   277 	  { dup 0 moveto _h sub _h lineto } for
       
   278 	} { 0 6 _w _h add
       
   279 	  { dup 0 exch moveto _w sub _w exch lineto } for
       
   280 	} ifelse
       
   281       } if
       
   282       BSt 13 eq BSt 14 eq or          % B-diag or diag cross
       
   283       { _w _h gt
       
   284 	{ 0 6 _w _h add
       
   285 	  { dup _h moveto _h sub 0 lineto } for
       
   286 	} { 0 6 _w _h add
       
   287 	  { dup _w exch moveto _w sub 0 exch lineto } for
       
   288 	} ifelse
       
   289       } if
       
   290       stroke
       
   291     } if
       
   292     BSt 15 eq
       
   293     {
       
   294     } if
       
   295     BSt 24 eq                         % TexturePattern
       
   296     {
       
   297     } if
       
   298   grestore
       
   299 } bind def
       
   300 
       
   301 % more PDF operators
       
   302 /f { /WFi true def BF newpath } bind def
       
   303 /f* { /WFi false def BF newpath } bind def
       
   304 /B { /WFi true def BF S newpath } bind def
       
   305 /B* { /WFi false def BF S newpath } bind def
       
   306 
       
   307 %% start of page
       
   308 /QI {
       
   309   /C save def
       
   310   pageinit
       
   311   q
       
   312   newpath
       
   313 } bind def
       
   314 
       
   315 %% end of page
       
   316 /QP {
       
   317   Q                                % show page
       
   318   C restore
       
   319   showpage
       
   320 } bind def
       
   321 
       
   322 % merges one key value pair into the page device dict
       
   323 %
       
   324 %    key value SPD -
       
   325 /SPD {
       
   326   /setpagedevice where {
       
   327     << 3 1 roll >>
       
   328     setpagedevice
       
   329   } { pop pop } ifelse
       
   330 } bind def
       
   331 
       
   332 
       
   333 % font handling
       
   334 
       
   335 /T1AddMapping { % basefont [glyphname ...] T1AddMapping -
       
   336   10 dict begin
       
   337     /glyphs exch def
       
   338     /fnt exch def
       
   339     /current fnt /NumGlyphs get def
       
   340     /CMap fnt /CMap get def
       
   341 
       
   342     0 1 glyphs length 1 sub % 0 1 (num glyphs - 1)
       
   343     {
       
   344       glyphs exch get /gn exch def
       
   345 
       
   346       current dup                                 % glyph_index glyph_index
       
   347       256 mod /min exch def                       % glyph_index
       
   348       256 idiv /maj exch def                      % -
       
   349       CMap dup maj get dup                        % cmap cmap_maj cmap_maj
       
   350       null eq {
       
   351 	pop 256 array 
       
   352 	0 1 255 {1 index exch /.notdef put} for
       
   353       } if
       
   354       dup                                         % cmap cmap_maj cmap_maj
       
   355       min gn put                                  % cmap cmap_maj
       
   356       maj exch put                                % -
       
   357 
       
   358       /current current 1 add def
       
   359     } for
       
   360 
       
   361     fnt /CMap CMap put
       
   362     fnt /NumGlyphs current put
       
   363   end 
       
   364 } def
       
   365 
       
   366 /T1AddGlyphs { % basefont [glyphname charstring ...] T1AddGlyphs -
       
   367   10 dict begin
       
   368     /glyphs exch def
       
   369     /fnt exch def
       
   370     /current fnt /NumGlyphs get def
       
   371     /CMap fnt /CMap get def
       
   372     /CharStrings fnt /CharStrings get def
       
   373 
       
   374     0 1 glyphs length 2 idiv 1 sub % 0 1 (num glyphs - 1)
       
   375     {
       
   376       2 mul dup
       
   377       glyphs exch get /gn exch def
       
   378       1 add
       
   379       glyphs exch get /cs exch def
       
   380 
       
   381       current dup                                 % glyph_index glyph_index
       
   382       256 mod /min exch def                       % glyph_index
       
   383       256 idiv /maj exch def                      % -
       
   384       CMap dup maj get dup                        % cmap cmap_maj cmap_maj
       
   385       null eq {
       
   386 	pop 256 array 
       
   387 	0 1 255 {1 index exch /.notdef put} for
       
   388       } if
       
   389       dup                                         % cmap cmap_maj cmap_maj
       
   390       min gn put                                  % cmap cmap_maj
       
   391       maj exch put                                % -
       
   392 
       
   393       CharStrings gn cs put
       
   394       /current current 1 add def
       
   395     } for
       
   396 
       
   397     fnt /CharStrings CharStrings put
       
   398     fnt /CMap CMap put
       
   399     fnt /NumGlyphs current put
       
   400   end 
       
   401 } def
       
   402 
       
   403 
       
   404 
       
   405 /StringAdd { % string1 string2 stringadd result
       
   406   1 index length 1 index length add
       
   407   string
       
   408   3 1 roll
       
   409   2 index 0 3 index putinterval
       
   410   2 index 2 index length 2 index putinterval 
       
   411   pop pop
       
   412 } def
       
   413 
       
   414 
       
   415 /T1Setup { % fontname T1Setup -
       
   416 10 dict begin
       
   417   dup /FontName exch def
       
   418   (-Base) StringAdd cvx cvn /Font exch def
       
   419   /MaxPage Font /NumGlyphs get 1 sub 256 idiv def
       
   420 
       
   421   /FDepVector MaxPage 1 add array def
       
   422   /Encoding MaxPage 1 add array def
       
   423 
       
   424   0 1 MaxPage {
       
   425     dup Encoding exch dup put
       
   426  
       
   427 
       
   428     dup /Page exch def
       
   429     FontName (-) StringAdd 
       
   430     exch 
       
   431     20 string cvs StringAdd % page fontname
       
   432     cvn
       
   433 
       
   434     Font 0 dict copy dup dup /CMap get 
       
   435     Page get 
       
   436     /Encoding exch put definefont 
       
   437     FDepVector exch Page exch put
       
   438   } for 
       
   439 
       
   440   FontName cvn <<
       
   441     /FontType 0
       
   442     /FMapType 2
       
   443     /FontMatrix[1 0 0 1 0 0]
       
   444     /Encoding Encoding
       
   445     /FDepVector FDepVector
       
   446     >> definefont pop
       
   447   end
       
   448 } def
       
   449