
有什麼方法可以用Metapost
/產生完全遵循的 PS 和 SVG 程式碼Metafun
?不幸的是,unfill
這對我不起作用,因為結果頁面中有背景。這個例子被精簡了,因為真實的路徑要複雜得多。
所需的後記輸出:
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 0 0 100 100
%%BeginProlog
%%EndProlog%%Page: 1 1
newpath 0 100 moveto 100 100 lineto 100 0 lineto 0 0 lineto 0 100 lineto closepath
10 90 moveto 10 10 lineto 90 10 lineto 90 90 lineto 10 90 lineto closepath
fill
%%EOF
所需的 SVG 輸出:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="125" height="125">
<g transform="matrix(1.25,0,0,-1.25,0,125)">
<g transform="scale(0.1,0.1)">
<path d="m 0,1000 1000,0 L 1000,0 0,0 0,1000 z m 100,-100 0,-800 800,0 0,800 -800,0" id="3"
style="fill:#000000;fill-opacity:1;fill-rule:nonzero;stroke:none" />
</g>
</g>
</svg>
答案1
MetaPost 中有一個標準技巧可以做到這一點。我決定包含該解決方案,因為 MetaPost 的取消填充實際上不會填充任何內容,而是填充背景顏色。
下面展示了解決方案。
beginfig( 0 )
path clockwisepath, anticlockwisepath;
clockwisepath := (0,100) -- (100,100) -- (100,0) -- (0,0) -- (0,100) -- cycle;
anticlockwisepath := (10,90) -- (10,10) -- (90,10) -- (90,90) -- (10,90) -- cycle;
fill clockwisepath -- anticlockwisepath -- cycle;
endfig;
end.
以下是製作的eps:
%!PS
%%BoundingBox: 0 0 100 100
%%HiResBoundingBox: 0 0 100 100
%%Creator: MetaPost 1.902
%%CreationDate: 2014.12.19:1511
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
0 0 0 setrgbcolor
newpath 0 100 moveto 100 100 lineto 100 0 lineto 0 0 lineto 0 100 lineto
0 100 lineto 10 90 lineto 10 10 lineto 90 10 lineto 90 90 lineto
10 90 lineto 10 90 lineto
closepath fill
showpage
%%EOF
請注意,程式碼不會產生完全相同的 eps,但填充它實際上並不重要。
答案2
填充和未填充:
%% naid.mp
u:=1bp;
beginfig(1);
fill (0,0)--(100u,0)--(100u,100u)--(0,100u)--cycle;
unfill (10u,10u)--(90u,10u)--(90u,90u)--(10u,90u)--cycle;
endfig;
end.
這是輸出mpost naid.mp
:
%!PS
%%BoundingBox: 0 0 100 100
%%HiResBoundingBox: 0 0 100 100
%%Creator: MetaPost 1.803
%%CreationDate: 2013.12.23:1356
%%Pages: 1
%%BeginProlog
%%EndProlog
%%Page: 1 1
0 0 0 setrgbcolor
newpath 0 0 moveto
100 0 lineto
100 100 lineto
0 100 lineto
closepath fill
1 1 1 setrgbcolor
newpath 10 10 moveto
90 10 lineto
90 90 lineto
10 90 lineto
closepath fill
showpage
%%EOF
這是輸出
mpost '\outputformat:="svg";outputtemplate:="%j-%c.svg";input naid.mp'
在文件中naid-1.svg
<?xml version="1.0"?>
<!-- Created by MetaPost 1.803 on 2013.12.23:1445 -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100.000000" height="100.000000" viewBox="0 0 100.000000 100.000000">
<!-- Original BoundingBox: 0.000000 0.000000 100.000000 100.000000 -->
<path d="M0.000000 100.000000L100.000000 100.000000L100.000000 -0.000000L0.000000 -0.000000Z" style="fill: rgb(0.000000%,0.000000%,0.000000%);stroke: none;"></path>
<path d="M10.000000 90.000000L90.000000 90.000000L90.000000 10.000000L10.000000 10.000000Z" style="fill: rgb(100.000000%,100.000000%,100.000000%);stroke: none;"></path>
</svg>
答案3
我從未使用過 Metapost,但由於 Asymptote 基於它,Asymptote 答案可能會幫助某人找出 Metapost 答案。
foo.asy
建立一個包含以下內容的文件:
fill((0,100)--(100,100)--(100,0)--(0,0)--cycle ^^ (10,90)--(10,10)--(90,10)--(90,90)--cycle);
從命令列運行會給出一個包含以下內容的asy foo.asy
eps 檔案(對一些明顯的差異進行取模,例如建立時間):foo.eps
%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 255 345 356 446
%%HiResBoundingBox: 255.5 345.5 355.5 445.5
%%Creator: Asymptote 2.23
%%CreationDate: 2013.12.23 10:10:49
%%Pages: 1
%%Page: 1 1
/Setlinewidth {0 exch dtransform dup abs 1 lt {pop 0}{round} ifelse
idtransform setlinewidth pop} bind def
gsave
255.5 345.5 translate
newpath 0 100 moveto
100 100 lineto
100 0 lineto
0 0 lineto
0 100 lineto
closepath
10 90 moveto
10 10 lineto
90 10 lineto
90 90 lineto
10 90 lineto
closepath
0 setgray
0.5 Setlinewidth
1 setlinecap
1 setlinejoin
10 setmiterlimit
fill
grestore
showpage
%%EOF
結果如圖所示: