2013-02-13 68 views
1

爲什麼Ghostscript pswrite在輸出中編碼我的文本?考慮以下MWE:Ghostscript postscript pswrite是編碼文本

%!PS-Adobe-3.0 
%%Title: mwe.ps 
%%Pages: 001 
%%BoundingBox: 0 0 595 842 
%%EndComments 

%%Page: 1 1 
%%PageBoundingBox: 0 0 595 842 
0 0 1 setrgbcolor 
0 0 595 842 rectfill 
1 0 0 setrgbcolor 
247 371 100 100 rectfill 
/Times-Roman findfont 
72 scalefont 
setfont 
newpath 
247 300 moveto 
(Chris) show 
showpage 

保存此MWE到文件,並查看在GSView程序會顯示一個藍色的頁面,紅場和我的名字下面。現在使用以下命令行通過Ghostscript 9.06運行此文件:

"c:\Program Files\gs\gs9.06\bin\gswin64c.exe"^
    -dSAFER -dBATCH -dNOPAUSE^
    -sDEVICE=pswrite -sPAPERSIZE=a4 -r72 -sOutputFile=mwe_gs.ps mwe.ps 

請參閱下面的Ghostscript輸出。有人能解釋一下這裏發生了什麼嗎?雖然兩個rectfill命令仍然顯而易見,但我的文本(Chris)已被編碼並且不再可區分。

是否有替代後記設備,它會保留我的文本嗎?

<snip> 
%%Page: 1 1 
%%PageBoundingBox: 0 0 595 842 
%%BeginPageSetup 
GS_pswrite_2_0_1001 begin 
595 842 /a4 setpagesize 
/pagesave save store 197 dict begin 
1 1 scale 
%%EndPageSetup 
gsave mark 
255 0 r6 
0 0 595 842 rf 
255 0 r3 
247 371 100 100 rf 
Q q 
0 0 595 0 0 842^Y 
255 0 r3 
249 299 43 50 /5D 
$C 
,6CW56m1G"ZORNkWR*rB:!c2;9rlWTH="2^^[(q"h>cG<omZ2l^=qC[XbO:8_[?kji-8^"N#3q* 
jhL~> 
, 
289 300 41 49 /0P 
$C 
4r?0p$m<EkK3,0>s8W-!s8W-!s8W,u]<1irI=*p=<t0>[email protected]<)>Is8K6,aTi'$~> 
, 
325 300 30 33 /5I 
$C 
49S"pc4+Rhs8W-!s8W)oqdD:saRZq[4+k%):]~> 
, 
349 300 24 49 /0T 
$C 
4q%Ms%;PqCs8W-!s8W%1_qkn/K?*sYFSGd:5Q~> 
, 
377 299 23 34 /5M 
$C 
-TQR7$&O'!K+D:XribR9;$mr4#[email protected],dX=Y&Llg+F`d^HC#%$"]~> 
, 
cleartomark end end pagesave restore 
showpage 
%%PageTrailer 
%%Trailer 
%%Pages: 1 
%%EOF 

注:這似乎是一個奇怪的活動,但我用探索的Ghostscript來「清理」從MATLAB應用PostScript輸出的想法..

+0

正如你所看到的,你將很難得到任何清潔劑後記任何自動化的爲此,一旦輸入噪音。我認爲那裏唯一的選擇是,因爲tehere無法控制matlabe如何創建它的文件,所以完全爲了您的數學開發而切換到另一個棧。開源軟件可以讓你控制你的所有工作(包括mathematica算法本身,它們在matlab中是封閉的) – jsbueno 2013-02-14 11:05:45

回答

0

文本已轉換爲矢量路徑。 249 299 43 50/5D開始第一個字母「C」,然後289 300是「h」,289 300是「r」....

pswrite所做的是消除了對字體的需要,所以當您的原始代碼使用/ Times-Roman時,蒸餾代碼不需要任何字體,而是使用矢量繪製文本。

我不確定你究竟是什麼,但你可以嘗試「ps2write」或「epswrite」作爲「pswrite」的替代品。 pswrite用於寫入ps級別1標準,ps2write將寫入ps級別2輸出。沒有人需要ps等級1,所以等級2是可​​以接受的。 epswrite將寫入封裝後記(eps)。

+0

epswrite似乎產生了幾乎與pswrite相同的輸出。 ps2write甚至更多編碼。 我正在爲使用來自Matlab的postscript輸出的LaTeX創建PDF文檔作鬥爭。我需要解析postscript輸出以查找「塊」並創建超鏈接。目前,postscript參考點不是'左下角'的來源,這使得解析和計算圓形對象非常棘手。 我正在尋找方法來清理Matlab postscript輸出來幫助我的解析。 – Chris 2013-02-13 16:12:57

+0

對我來說,最簡單的答案是讓後記做這項工作。在postscript中,您可以重新定義運算符。 「show」操作符可以用記錄格式顯示文件(cvs或fixed fielded)輸出x,y,顏色,文本和rectfill來保存矩形rect,x1,y1,x2,y2,顏色postscript有一個「pathbbox」運算符將計算任何形狀的路徑的邊界框,因此,而不是Ghostscript也有pdfwrite,而不是讓文本只給出1 x/y,它可以定義文本位於show,x1,y1,x2,y2中的框,顏色,文字 – 2013-02-13 19:11:00

+0

除了pswrite,還有一個pdfwrite,它會輸出pdf的。使用上述技術,可以將.ps轉換爲.pdf並具有位置的數據文件。 ghostscript,pswrite,epswrite,pdfwrite都是開源代碼。如果你覺得真正冒險的話,你可以修改ghostscript來完成你以後的事情,bug ghostscript是一些難看的代碼,修改節目和rectfill會更容易一些。 – 2013-02-13 19:17:11

6

'文字'已被轉換爲圖片,而不是矢量路徑。這是pswrite設備的一個嚴重限制,也是其不推薦使用的原因之一,您應該使用ps2write設備。 pswrite設備仍然包含的唯一原因是使用它的epswrite(這就是pswrite和epswrite輸出看起來相同的原因)。在某些時候,將會有一個eps2write設備,並且pswrite將被分檔。

ps2write輸出默認情況下是壓縮的。如果您想要未壓縮的輸出,請在命令行上使用-dCompressPages = false開關。

如果你想要的只是文本的位置,你可以考慮txtwrite設備。這樣做的默認實現會創建輸入的純文本表示形式,但您可以讓它輸出僞裝的XML,其中包含文本的來源等內容。

1

下面是show操作符被重新定義以顯示關於節目的位置信息以及執行標準show操作的一個簡單示例。使用ghostscript你可以運行多個文件,所以頭文件將成爲另一個文件的前綴,這改變了標準行爲。

重新定義的展示可能包含字體名稱和大小。數據可能已寫入磁盤文件,而不是轉儲到控制檯。任何其他操作符也可能被重新定義,如rectfill,fill,stroke ...因爲原始操作符也被調用,您可以使用pdfwrite設備將.ps轉換爲.pdf,同時獲取位置信息。

gswin32c.exe -dBATCH -dNOPAUSE header.ps trash.ps 
gswin32c.exe -sDEVICE=pdfwrite -dCompressPages=false -sOutputFile=test.pdf header.ps trash.ps 

輸出

currentpoint x:247.0 y:300.0 pathbbox 249.015,298.992 400.066,349.184 text:Chris currentrgbcolor:1.0,0.0,0.0() 
currentpoint x:50.0 y:90.0 pathbbox 50.8682,89.2852 181.327,139.184 text:Fred currentrgbcolor:1.0,0.0,0.0() 
currentpoint x:150.0 y:200.0 pathbbox 150.867,184.298 304.154,247.673 text:Mary currentrgbcolor:1.0,0.0,0.0() 
currentpoint x:300.0 y:350.0 pathbbox 300.867,348.993 598.79,398.681 text:Mr. Green currentrgbcolor:0.0,1.0,0.0() 
currentpoint x:100.0 y:400.0 pathbbox 100.866,399.202 358.547,449.183 text:Mr. Blue currentrgbcolor:0.0,0.0,1.0() 

Header.ps

/mydict 5 dict def 
mydict begin 
/show 
    { 
    (currentpoint) print 
    currentpoint exch 10 string cvs (x:) print print 10 string cvs (y:) print print 
    gsave dup false charpath flattenpath 

    ( pathbbox) print 
    pathbbox 
    4 -1 roll 10 string cvs print (,) print 
    3 -1 roll 10 string cvs print () print 
    2 -1 roll 10 string cvs print (,) print 
    10 string cvs print () print 
    grestore 
    ( text:) 10 string cvs print 
    dup print () print 
    (currentrgbcolor:) print 
    currentrgbcolor 
    3 -1 roll 10 string cvs print (,) print 
    2 -1 roll 10 string cvs print (,) print 
    10 string cvs print () == 
    systemdict /show get exec 
} def 

trash.ps

%!PS-Adobe-3.0 
%%Title: mwe.ps 
%%Pages: 001 
%%BoundingBox: 0 0 595 842 
%%EndComments 
%%Page: 1 1 
%%PageBoundingBox: 0 0 595 842 
0 0 1 setrgbcolor 
0 0 595 842 rectfill 
1 0 0 setrgbcolor 
247 371 100 100 rectfill 
/Times-Roman findfont 
72 scalefont 
setfont 
newpath 
247 300 moveto (Chris) show 
50 90 moveto (Fred) show 
150 200 moveto (Mary) show 
0 1 0 setrgbcolor 
300 350 moveto (Mr. Green) show 
0 0 1 setrgbcolor 
100 400 moveto (Mr. Blue) show 
showpage