Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
799 views
in Technique[技术] by (71.8m points)

linux - Overlay two postscript files (command line approach)?

I'm aware that similar questions have been answered here before:

... however, as they don't directly answer my specific problem, I'll try asking again.

 

I would like to know, how I can overlay two postscript files: one - a logo:

... on top of the other - a graphic background:

.... using a command line approach in Linux.

 

It is of importance here, that the graphic background preserves its original contents to the greatest extent possible. Therefore, I'd probably be happiest if I could somehow concatenate the two original postscript file contents unchanged (see below) - but other than that, I'd like to know how command-line tools like ghostscript could be used for the purpose.

 

The problem - logo on top of PCB .ps printout

Here is the problem in more detail - I want to add a logo on top of a PCB board print. I use kicad to develop the PCB print; once done, I export a postscript document with the individual layers. Usually, I want to have the front copper layer "mirrored"; I have often experimented with loading the .ps output into, say, inkscape, and then trying to do a mirroring there. Using inkscape was also convenient, because one can easily add a logo there (and then mirror the print and the logo together).

However, using inkscape to process postscript output is an expencive operation (because it takes a while for inkscape to load, and do the mirroring, and the saving)... Although, the main show stopper here is that: after processing, some of the dimensions may be slightly changed in the output file by inkscape (and it makes a lot of difference, when the target is printing of tracks less than a millimeter wide).

 

So, given that kicads "Plot" command has an option for mirroring - it is, of course, much better for me to "mirror" the layer print directly from kicads "Plot" command; the output is again a postscript file. tmp-Front.ps is an example of such a (mirrored) postscript file. Note that typically, the "copper" part is filled with black - and I'd basically like to have a white logo on top of it.

Now, since this print is already "mirrored" - if I want to add a logo on top of it, I better have the logo mirrored as well. So I fire up inkscape; and:

  • add a text for the logo;
  • set up via Document Properties:
    • that the document size matches the logo size, and
    • that the document should have a black background
  • Then change the logo text to have white fill;
  • do Object/Flip Horizontal on the logo text; and then
  • do File/Save a Copy;
    • choose PostScipt (*.ps) as output;
    • on the dialog choose PostScript level 2, and
    • select "Convert text to paths" before exporting.

This is how logo.ps linked above was generated (note that - for these kind of small logos: without 'text to paths', the .ps file (with fonts) can be up to 20 kB; as opposed to 5 kB for paths only).

One problem, though, is that postscript doesn't really support something like a "document background" color - apparently you need to define a filled rectangle yourself:

... therefore if you open logo.ps, you will not see anything - since it contains white letters on white background.

Viewers

If in logo.ps, the 1 g line is replaced with 0.5 g; the logo becomes gray (and visible), and here it is rendered under evince, gs raw viewer, and gv:

view-logo.png

Here is the same comparison for tmp-Front.ps:

view-tmp-Front.png

Notably, the raw ghostscript viewer ignores bounding box and page orientation - and even more problematic is, if you use Ubuntu Gnome on a netbook, then the top and bottom bar may cut a bit of the ghostscript viewer window - enough so that the logo in bottom left corner is invisible; and since "... the raw ghostscript viewer gs ... has no real user interface", a gs window which started up with the logo cropped, cannot be set to show it afterwards.

 

Concatenating .ps file contents for overlay

By looking at these references:

... I'm guessing that, in principle, it should be possible to concatenate the file contents of two postscript files; and if there is only a single showpage command in the merged file - then, in principle, one should obtain an overlay (?!) of the two. However, I tried that, and I cannot really get it to work.

If this is possible, I'd like to somehow just copy/paste the postscript file contents (and thereby avoid any problems with document dimensions/sizes/resolutions) - and then possibly use the translate postscript command (see Postscript Editing in Gnuplot) to adjust the position of the logo. Having the 'overlaid' (merged) .ps opened in evince, would refresh the document display each time the translate parameters are changed in a text editor - so it should be relatively easy to handle the logo positioning in that way.

 

However, I can foresee some problems here too: for one, the graphic (tmp-Front.ps) contains:

%%BoundingBox: 0 0 596 843
%%DocumentMedia: A4 595 842 0 () ()
...
0.0072 0.0072 scale
...
30085 54415 moveto
...

... while logo.ps contains:

%%BoundingBox: 0 0 31 13
...
%%DocumentMedia: 11x4mm 31 13 0 () ()
...
25.539 9.536 m(oveto) ...
...

... thus there are two main differences:

  • Document size is not the same for the files
  • Scale (or rather resolution/coordinate system) is not the same either

Since I couldn't get concatenation of file contents to work at all - I'm not really sure if these differences would have an influence on the overlay ... I'd certainly hope that they are somehow rendered independently; and the translate would just help to properly set up their relative position :)

 

So - how would I go about, if I wanted to achieve an overlay by concatenating postscript file contents? And if that is too troublesome - how could I use a tool such as ghostscript (or any other), in order to set up an overlay?

 

Many thanks in advance for any answers,
Cheers!

 


For reference, here are copies of the mentioned postscript files (as they are not too big):

tmp-Front.ps:

%!PS-Adobe-3.0
%%Creator: PCBNEW-PS
%%CreationDate: Thu Mar 22 09:50:52 2012
%%Title: /tmp-Front.ps
%%Pages: 1
%%PageOrder: Ascend
%%BoundingBox: 0 0 596 843
%%DocumentMedia: A4 595 842 0 () ()
%%Orientation: Landscape
%%EndComments
%%Page: 1 1
/line {
    newpath
    moveto
    lineto
    stroke
} bind def
/cir0 { newpath 0 360 arc stroke } bind def
/cir1 { newpath 0 360 arc gsave fill grestore stroke } bind def
/cir2 { newpath 0 360 arc gsave fill grestore stroke } bind def
/arc0 { newpath arc stroke } bind def
/arc1 { newpath 4 index 4 index moveto arc closepath gsave fill grestore stroke } bind def
/arc2 { newpath 4 index 4 index moveto arc closepath gsave fill grestore stroke } bind def
/poly0 { stroke } bind def
/poly1 { closepath gsave fill grestore stroke } bind def
/poly2 { closepath gsave fill grestore stroke } bind def
/rect0 { rectstroke } bind def
/rect1 { rectfill } bind def
/rect2 { rectfill } bind def
/linemode0 { 0 setlinecap 0 setlinejoin 0 setlinewidth } bind def
/linemode1 { 1 setlinecap 1 setlinejoin } bind def
/dashedline { [50 50] 0 setdash } bind def
/solidline { [] 0 setdash } bind def
gsave
0.0072 0.0072 scale
linemode1
82670 0 translate 90 rotate
60 setlinewidth
60 setlinewidth
newpath
30085 54415 moveto
30085 52585 lineto
31915 52585 lineto
31915 54415 lineto
30085 54415 lineto
poly1
newpath
87950 54930 moveto
32094 54930 lineto
32094 54474 lineto
32094 54415 lineto
32094 52525 lineto
32071 52470 lineto
32029 52428 lineto
31974 52406 lineto
31915 52406 lineto
30025 52406 lineto
29970 52429 lineto
29928 52471 lineto
29906 52526 lineto
29906 52585 lineto
29906 54475 lineto
29929 54530 lineto
29971 54572 lineto
30026 54594 lineto
30085 54594 lineto
31975 54594 lineto
32030 54571 lineto
32072 54529 lineto
32094 54474 lineto
32094 54930 lineto
28300 54930 lineto
28300 17800 lineto
87950 17800 lineto
87950 54930 lineto
poly1
100 setlinewidth
newpath
87950 54930 moveto
32094 54930 lineto
32094 54474 lineto
32094 54415 lineto
32094 52525 lineto
32071 52470 lineto
32029 52428 lineto
31974 52406 lineto
31915 52406 lineto
30025 52406 lineto
29970 52429 lineto
29928 52471 lineto
29906 52526 lineto
29906 52585 lineto
29906 54475 lineto
29929 54530 lineto
29971 54572 lineto
30026 54594 lineto
30085 54594 lineto
31975 54594 lineto
32030 54571 lineto
32072 54529 lineto
32094 54474 lineto
32094 54930 lineto
28300 54930 lineto
28300 17800 lineto
87950 17800 lineto
87950 54930 lineto
poly0
1 1 1 setrgbcolor
60 setlinewidth
31000 53500 170 cir1
0 0 0 setrgbcolor
showpage
grestore
%%EOF

logo.ps:

%!PS-Adobe-3.0
%%Creator: cairo 1.10.2 (http://cairographics.org)
%%CreationDate: Thu Mar 22 10:07:46 2012
%%Pages: 1
%%BoundingBox: 0 0 31 13
%%DocumentData: Clea

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In order to 'concatenate' the two files, you would need to disable the 'showpage' operator, as otherwise both files will emit their pages separately. But you need to maintain the original definition so that you can emit the final document. Somethign liek:

/Oldshowpage showpage load def
/showpage {} def

...
file1
...
...
file2
...
Oldshowpage

So if you saved the redefinitions in a file, and the final execution in another file you might cat 'prolog.ps file1.ps file2.ps epilog.ps'

Neither of your files executes a media request (you can ignore the %% lines, those are comments only).

However, as you rightly say you don't want scaling and transforms to persist between jobs. So you need to save and restore the graphics state. So in this case you also want a 'middle' file.

So prolog.ps contains:

/Oldshowpage showpage load def
/showpage {} def
gsave

middle.ps contains

grestore

and epilog.ps contains

Oldshowpage

cat all 5 files together and it ought to work. It does something sensible for me, but I should perhaps mention that your 'logo.ps' produces an empty page for me with Ghostscript and with Adobe Distiller.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...