summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/metauml/util_picture.mp
blob: fc9133392d9cb3ac4cc05df0e772f913130504fe (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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
% MetaUtil, an easier MetaPost life
% Copyright (C) 2005 Ovidiu Gheorghies
%
% This program 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

if known _util_picture_mp:
  expandafter endinput
fi;
_util_picture_mp:=1;

input util_log;

input util_margins;
if not known _n_cur_:input boxes;fi;

vardef FontInfo@#(expr tname, tscale)=
  attributes(@#);
  var(string) className;
  var(string) name;
  var(numeric) scale;

  @#className := "FontInfo";

  @#name := tname;
  @#scale := tscale;
enddef;

vardef FontInfoCopy@#(text fontInfo)=
  FontInfo@#(fontInfo.name, fontInfo.scale);
  @#ignoreNegativeBase := fontInfo.ignoreNegativeBase;
enddef;

vardef FontInfo_toString@#=
  save ret; string ret;
  ret := "FontInfo(" & (str @#) & "): {" & (@#name) & ", " & (decimal @#scale) & "}";
  ret
enddef;

log "()()() Creating iFont";
FontInfo.iFont(defaultfont, defaultscale);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                 %
%                         PICTURE                                 %
%                                                                 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

vardef PictureInfo@#(expr pleft, pright, ptop, pbottom)(text pfont)=
  attributes(@#);
  var(string) className;
  @#className := "PictureInfo";

  var(color)   borderColor;
  var(numeric) boxed;

  var(numeric) ignoreNegativeBase;

  @#boxed := 0;
  @#borderColor := blue;

  @#ignoreNegativeBase := 0;

  Margins.@#(pleft, pright, ptop, pbottom);

  FontInfoCopy.@#iFont(pfont);
enddef;

vardef PictureInfoCopy@#(text src)=
  PictureInfo@#(src.left, src.right, src.top, src.bottom)(src.iFont);

  @#boxed := src.boxed;
  @#borderColor := src.borderColor;
  @#ignoreNegativeBase := src.ignoreNegativeBase;
enddef;

vardef PictureInfo_toString@#=
  save ret; string ret;
  ret := "PictureInfo(" & (str @#) & "): {" & (decimal @#left) & ", " & (decimal @#right) & ", " & (decimal @#top) & ", " & (decimal @#bottom) & "}" & " boxed: " & (decimal @#boxed);
  ret := ret & " Font: " & toString(@#iFont);
  ret
enddef;

PictureInfo.iPict(2, 2, 2, 2)(iFont);

PictureInfoCopy.iPictBoxed(iPict);
iPictBoxed.boxed := 1;

%%
%% Contructs a Picture object by wrapping around the
%% low-level picture thePict.
%%
vardef EPicture@#(text iPict)(expr thePict) =
  ObjectEquations(@#);
  @#className := "Picture";

  var(pair)    contentShift;
  var(picture) pict;
  var(numeric) negativeBase;
  var(string)  contentAsString;
  var(picture) contentAsPicture;

  PictureInfoCopy.@#info(iPict);

  if string thePict:
    log "Picture " & (str @#) & " is a text";
    @#contentAsString := thePict;
    log @#contentAsString;
  elseif picture thePict:
    log "Picture " & (str @#) & " is a native pict";
    @#contentAsPicture := thePict;
    log @#contentAsPicture;
  else:
    log "Picture " & (str @#) & " [error]";
  fi;

enddef;

%%
%% Contructs a Picture object by wrapping around the
%% low-level picture thePict.
%%
vardef Picture@#(expr thePict) =
  EPicture@#(iPict)(thePict);
enddef;

%%
%% Lays out the Picture.
%%
vardef Picture_layout@# =
  if @#layedout = 0:
    log "Laying out " & (str @#);

    @#layedout := 1;

    if known @#contentAsPicture:
      log "Content is known to be a picture";
      @#pict = @#contentAsPicture;
    elseif known @#contentAsString:
      log "Content is known to be a string";
      log @#contentAsString;
      @#pict = @#contentAsString infont @#info.iFont.name scaled @#info.iFont.scale;
    else:
      log "Show unknown parameter type in picture layout";
      2 = 3;
    fi;

    @#negativeBase = ypart llcorner @#pict;

    @#width = pictWidth(@#pict) + @#info.left + @#info.right;

    if @#info.ignoreNegativeBase = 0:
      @#height = pictHeight(@#pict) + @#info.top + @#info.bottom;
      @#contentShift = @#sw + (@#info.left, @#info.bottom - @#negativeBase) ;
    else:
      @#height = pictHeight(@#pict) + @#info.top + @#info.bottom + @#negativeBase;
      @#contentShift = @#sw + (@#info.left, @#info.bottom);
    fi;

  else:
    log "Picture " & str @# & " already layed out.";
  fi;
enddef;

%%
%% Draws the Picture.
%%
vardef Picture_draw@# =
  Picture_layout@#;
  objectEnsurePositioning.@#;
  
  draw @#pict shifted @#contentShift;

  if (@#info.boxed = 1):
    draw objectBox(@#) withcolor @#info.borderColor;
  fi;
enddef;

vardef Picture_border@#=
    objectBox(@#)
enddef;

vardef aitem(text pictInfo)(text thePict)(text equation)=
  save obj;
  EPicture.obj(pictInfo)(thePict);
  equation;
  drawObject(obj);
enddef;

vardef item@#(text iPict)(text thePict)(text equation)=
  save itemName;
  string itemName;
  itemName := str @#;

  log "Creating an item with name: '" & itemName & "'";

  if itemName = "":
    log "Name is empty, creating an anonymous item!";
    aitem(iPict)(thePict)(equation);
  else:
    EPicture@#(iPict)(thePict);
    equation;
    drawObject(@#);
  fi;
enddef;