Debian Codegraph @

texmf-dist/metapost/mpattern/mpattern.mp

raw
1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2 % This file is part of the MPATTERN package
3 %
4 % mpattern.mp: MetaPost macros for pattern defining and filling
5 %
6 % Author: Piotr Bolek
7 % version 0.5: (Jun 25, 2001)
8 %
9 % $Id: mpattern.mp,v 1.3 2001/06/25 07:28:14 piotrek Exp $
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12 boolean P_dict_;
13 boolean P_used_already_;
14 % Record all numbers (charcode) of pictures
15 extra_beginfig := extra_beginfig
16 & "numeric Pwritten_[]; Pcharcode_[charcode]=1; P_used_already_:=false; P_dict_:=false;";
17
18 % Working variables to store fragments of Pattern definition
19 string PBBox_, PHead_, PTail_, PPaintProc_, Pname_, Pdefs_;
20 string Pmatrix_;
21
22 PHead_="<< /PaintType 1 /PatternType 1 /TilingType 1 ";
23 % 11.08.98 PPaintProc_="/PaintProc { begin ";
24 PPaintProc_="/PaintProc { pop ";
25 % 11.08.98 PTail_=" end } >>"; % matrix makepattern";
26 PTail_=" } >>"; % matrix makepattern";
27 Pdefs_="";
28
29 % New line string
30 string Pnl_; Pnl_=char 13;
31
32 % Number of defined patterns
33 numeric Pnum_; Pnum_=0;
34
35 % Split string defining pattern on new line chars
36 % and use each piece as argument to special
37 vardef Psplit_and_write_(expr s)=
38 save Pfirst_, Plast_;
39 Pfirst_:=0; Plast_:=0;
40 for i=0 upto length(s):
41 if ( substring (i,i+1) of s = Pnl_ ):
42 Plast_:=i;
43 special substring (Pfirst_,Plast_) of s;
44 Pfirst_:=i+1;
45 fi;
46 endfor;
47 enddef;
48
49 % Find maximum charcode not used for picture yet
50 vardef Pmax_charcode_=
51 save Pn_;
52 for i=999 downto 1:
53 Pn_:=i;
54 exitif unknown Pcharcode_[i];
55 endfor;
56 Pn_
57 enddef;
58
59 def PmakeBB_=
60 PBBox_ := "/BBox [" & decimal Plft_ & " " & decimal Plow_ & " "
61 & decimal Prt_ & " " & decimal Pup_ & "]" & Pnl_;
62 enddef;
63
64 def Psteps_=
65 PBBox_ := PBBox_ & "/XStep " & decimal
66 if unknown PXStep_: (Prt_-Plft_) else: PXStep_ fi & Pnl_
67 & "/YStep " & decimal
68 if unknown PYStep_: (Pup_-Plow_) else: PYStep_ fi;
69 enddef;
70
71 vardef Pfindbounds_=
72 % save Plow_, Plft_, Pup_, Prt_;
73 % numeric Plow_, Plft_, Pup_, Prt_;
74
75 Plow_ = ypart (llcorner currentpicture);
76 Plft_ = xpart (llcorner currentpicture);
77 Pup_ = ypart (urcorner currentpicture);
78 Prt_ = xpart (urcorner currentpicture);
79
80 PmakeBB_;
81 enddef;
82
83 % Read file in which the body of patterns PaintProc is stored
84 vardef PReadFile_=
85 save Pfile_, Pline_, Pall_;
86 string Pfile_, Pline_, Pall_;
87 Pline_=Pall_="";
88 Pfile_:=jobname & "." & decimal charcode;
89 forever:
90 Pline_ := readfrom Pfile_;
91 exitif Pline_ = EOF;
92 if (substring(length Pline_-3,length Pline_) of Pline_ = "def")
93 and (substring (0,6) of Pline_ <> "/fshow"):
94 Pdefs_ := Pdefs_ & Pline_ & Pnl_;
95 else:
96 if (substring (0,2) of Pline_ = "%%")
97 or (substring (0,8) of Pline_ = "showpage")
98 or (substring (0,4) of Pline_ = "%!PS")
99 or (substring (0,6) of Pline_ = "/fshow"):
100 else:
101 Pall_:=Pall_ & Pline_ & Pnl_;
102 fi;
103 fi;
104 endfor;
105 Pall_
106 enddef;
107
108 % Join all parts of pattern definition
109 vardef PmakePattern_(expr name)=
110 save Pread_;
111 string Pread_;
112
113 Pread_ = PReadFile_;
114
115 % Write string used later to identify pattern
116 % batchmode;
117 % message "Defining pattern " & name & "->" & decimal (Pnum_*epsilon);
118 % errorstopmode;
119 "% Pattern:" &
120 if (not known PColor_):
121 decimal (Pnum_*epsilon)
122 else:
123 decimal (PColor_)
124 fi
125 & ":" & name & Pnl_
126 & PHead_ & Pnl_
127 & PBBox_ & Pnl_
128 & Pdefs_
129 & PPaintProc_ & Pnl_
130 & Pread_
131 & PTail_ & Pnl_
132 & Pmatrix_ & " makepattern" & Pnl_
133 & "/" & name & " exch def" & Pnl_
134 & "MPP " &
135 if (not known PColor_):
136 decimal (Pnum_*epsilon)
137 else:
138 decimal (PColor_)
139 fi
140 & " " & name & " put" & Pnl_
141 enddef;
142
143 % find PatternColor -- color which will be replaced by pattern
144 vardef Pfindcolor_(expr s)=
145 save Pfirst_, Plast_;
146 numeric Pfirst_, Plast_;
147 Pfirst_=Plast_=0;
148 for i=0 upto 255:
149 if substring(i,i+1) of s=":":
150 if Pfirst_=0:
151 Pfirst_:=i+1;
152 else:
153 Plast_:=i;
154 fi;
155 fi;
156 exitif Plast_<>0;
157 endfor;
158 scantokens (substring (Pfirst_,Plast_) of s)
159 enddef;
160
161 %%%%%%%%%%%%%% User interface macros
162
163 % Define BoundingBox of pattern
164 vardef patternbbox(expr a)(text b)=
165 % save Plft_, Plow_, Prt_, Pup_, Pi_, Pz_;
166 % numeric Plft_, Plow_, Prt_, Pup_, Pi_, Pz_[];
167 save Pi_, Pz_;
168 numeric Pi_, Pz_[];
169 if pair a:
170 Plft_:=min(xpart(a),xpart(b)); Plow_:=min(ypart(a),ypart(b));
171 Prt_:=max(xpart(a),xpart(b)); Pup_:=max(ypart(a),ypart(b));
172 else:
173 Pi_=1;
174 for t=b:
175 Pz_[Pi_]=t;
176 Pi_:=Pi_+1;
177 endfor;
178 Plft_:=min(a,Pz_2); Plow_:=min(Pz_1,Pz_3);
179 Prt_:=max(a,Pz_2); Pup_:=max(Pz_1,Pz_3);
180 fi;
181
182 PmakeBB_;
183 enddef;
184
185 def beginpattern(suffix name)=
186 numeric PXStep_, PYStep_;
187 numeric Plow_, Plft_, Pup_, Prt_;
188 numeric PColor_;
189 Pmatrix_:=" matrix ";
190
191 % Declare variable in which the pattern definition will be stored
192 string name;
193 Pname_:=str name;
194
195 Pnum_:=Pnum_+1;
196
197 % Use the largest available picture number (charcode)
198 % for storing the body od patten PaintProc
199 beginfig(Pmax_charcode_);
200 enddef;
201
202 def endpattern=
203 if unknown PBBox_:
204 Pfindbounds_;
205 fi;
206 endfig;
207 Psteps_;
208
209 begingroup;
210 save Punknown_, Ppattern_;
211 string Punknown_, Ppattern_;
212
213 Ppattern_=PmakePattern_(Pname_);
214 % free used charcode
215 Pcharcode_[charcode]:=whatever;
216 PBBox_:=Punknown_;
217 Pdefs_:="";
218 % Assign pattern to string variable
219 scantokens(Pname_ & "=Ppattern_;");
220 endgroup;
221 enddef;
222
223 primarydef p withpattern s=
224 hide(
225 if not P_dict_:
226 special "/MPP 100 dict def";
227 special "/MPPshow {exch findfont exch scalefont setfont show}bind def";
228 special "/fill { MPP currentgray known " & char 10
229 & " { MPP currentgray get setpattern fill } { fill } " & char 10
230 & " ifelse } bind def";
231 special "/fshow { MPP currentgray known " & char 10
232 & " { MPP currentgray get setpattern MPPshow } { MPPshow } " & char 10
233 & " ifelse } bind def";
234 P_dict_:=true;
235 fi;
236 Pc_:=Pfindcolor_(s);
237 % write definition of pattern used in picture
238 % but not yet written to output file;
239 if unknown Pwritten_[Pc_]:
240 Psplit_and_write_(s);
241 Pwritten_[Pc_]:=1;
242 fi;
243 if not P_used_already_:
244 batchmode;
245 message "Pattern:" & decimal charcode;
246 errorstopmode;
247 P_used_already_:=true;
248 fi;
249 %show p;
250 )
251 p withcolor Pc_*white
252 enddef;
253
254 def patterntransform expr t=
255 Pmatrix_ := "[ " & decimal xxpart t
256 & " " & decimal yxpart t
257 & " " & decimal xypart t
258 & " " & decimal yypart t
259 & " " & decimal xpart t
260 & " " & decimal ypart t & " ]";
261 % show Pmatrix_;
262 enddef;
263
264 def patternxstep expr t=
265 show t;
266 PXStep_ = t;
267 enddef;
268
269 def patternystep expr t=
270 show t;
271 PYStep_ = t;
272 enddef;
273
274 def patternstep text t=
275 show t;
276 if pair t:
277 PXStep_ = xpart t;
278 PYStep_ = ypart t;
279 else:
280 (PXStep_,PYStep_)=t;
281 fi;
282 enddef;
283
284 def patterncolor expr t=
285 show t;
286 PColor_ = t;
287 enddef;