summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/exteps/exteps.mp
blob: 0ec3626f26be63becb2ceeb994ceac71f85d9b50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
% Copyright 2005 by Palle Jørgensen
%
% This file is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 2 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 59 Temple Place - Suite 330, Boston,
% MA 02111-1307, USA.
%
% This is version 0.1 Wed Aug 10 2005
%
% The module is documentet in exteps.pdf

picture epspicture; 

%% String handling tool
string string_split[];
def splitstring expr S =
  begingroup
    save __splitctr; numeric __splitctr; __splitctr = 0;
    save __prevchar; string __prevchar, __currentchar; __prevchar = " ";
    for i = 0 upto infinity:
      __currentchar := substring(i, i+1) of S;
      if (__currentchar = " ") and (__prevchar = " "):
	relax;
      elseif (__currentchar <> " ") and (__prevchar = " "):
	string_split[__splitctr] := __currentchar;
      elseif (__currentchar <> " ") and (__prevchar <> " "):
	string_split[__splitctr] := string_split[__splitctr] & __currentchar;
      elseif (__currentchar = " ") and (__prevchar <> " "):
	__splitctr := __splitctr+1;
      fi
      __prevchar := __currentchar;
    endfor
  endgroup;
enddef;
%% End string handling tool

def begineps text F =
  begingroup;
    save file;                string file;        file = F;    
    save angle;               numeric angle;      angle = 0;
    save clip;                boolean clip;       clip = false;
    save scale;               pair scale;         scale = (1,1);
    save base;                pair base;          base = origin;
    save __bbxfound;          boolean __bbxfound; __bbxfound = false;
    save grid;                boolean grid;       grid = false;
    save gridstep;            numeric gridstep;   gridstep = 10;
    save __base;              pair __base;
    save  __eps__currentline; string __eps__currentline;
    save __bbxline;           string __bbxline;
    save llx, lly, urx, ury;  numeric llx, lly, urx, ury;
    save pct;                 numeric pct;
    save width;               numeric width;
    save height;              numeric height;
%% Finding the bounding box
    forever:
      __eps__currentline := readfrom F;
      if substring(0,14) of __eps__currentline = "%%BoundingBox:":
	__bbxline := substring(14, infinity) of __eps__currentline;
	__bbxfound := true;
	splitstring __bbxline;
	llx = scantokens string_split[0];
	lly = scantokens string_split[1];
	urx = scantokens string_split[2];
	ury = scantokens string_split[3];
      fi
      exitif __bbxfound;
    endfor
    closefrom F;
    __base = -(llx,lly);
    pct = (urx - llx)/100;
%% To ensure the right bounding box of the output file
%% a picture with the same size as the eps figure is added.
    epspicture := nullpicture;
    setbounds epspicture to ((0,0)--(0,ury-lly)--(urx-llx,ury-lly)--(urx-llx,0)--cycle);
enddef;
  
def endeps =
%% Drawing the grid
  if grid:
    for i = 0 step gridstep*pct until (urx - llx):
      epsdraw (i,0)--(i,ury - lly);
      epslabel.bot(((decimal.(i/pct) & "%") infont defaultfont) rotated -90, (i,0));
    endfor
    for i = 0 step gridstep*pct until (epsilon + ury - lly):
      epsdraw (0,i)--(urx - llx,i);
      epslabel.lft(((decimal.(i/pct) & "%") infont defaultfont), (0,i));
    endfor
  fi
%% Calculating scale if width and/or height is known
  if (known width) and (known height):
    scale := (width/(urx - llx),height/(ury - lly));
  elseif known width:
    scale := (width/(urx - llx),width/(urx - llx));
  elseif known height:
    scale := (height/(ury - lly),height/(ury - lly));
  fi
%% The graphics inclusion commands
  special "gsave";
  if base <> origin:
    special decimal.xpart.base & " " & decimal.ypart.base & " translate";
  fi
  if scale <> (1,1):
    special decimal xpart.scale & " " & decimal ypart.scale & " scale";
  fi
  if angle <> 0:
    special decimal angle & " rotate";
    epspicture := epspicture rotatedaround(origin)(angle);
  fi
    if __base <> origin:
    special decimal.xpart.__base & " " & decimal.ypart.__base & " translate";
  fi
  if scale <> (1,1):
    epspicture := epspicture scaled xpart.scale
    if xpart.scale <> ypart.scale:
      yscaled (ypart.scale/xpart.scale)
    fi;
  fi
  if clip:
    special "newpath ";
    special decimal llx & " " & decimal lly & " moveto";
    special decimal llx & " " & decimal ury & " lineto";
    special decimal urx & " " & decimal ury & " lineto";
    special decimal urx & " " & decimal lly & " lineto";
    special decimal llx & " " & decimal lly & " lineto";
    special "closepath clip";
  fi
  special "%%BeginDocument: " & file;
  forever:
    __eps__currentline := readfrom file;
    exitunless __eps__currentline <> EOF;      
    special __eps__currentline;
  endfor
  special "%%EndDocument: " & file;
  special "grestore";
  closefrom file;
  if base <> (0,0):
    epspicture :=  epspicture shifted base;
  fi
  addto currentpicture also epspicture;
endgroup;
enddef;

%% Special drawing commands
def epsfill expr c = addto epspicture contour c _op_ enddef;

def epsdraw expr p =
  addto epspicture
  if picture p:
    also p
  else:
    doublepath p withpen currentpen
  fi
  _op_
enddef;

def epsfilldraw expr c =
  addto epspicture contour c withpen currentpen
  _op_ enddef;

def epsdrawdot expr z =
  addto epspicture contour makepath currentpen shifted z
  _op_ enddef;

def epslabel = epsdraw thelabel enddef;

endinput