summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/metapost/metauml/util_picture.mp
blob: 85ce68f7d4133773163f0022549f5521a3183046 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
% Copyright 2005 2015 Ovidiu Gheorghies
% Licensed under the Apache License, Version 2.0.

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

% Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost.
% The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times, 
% but this macro makes sure that MetaPost won't try to load the file and display a message for that.
def inputonce text libraryFile=
	if not known scantokens ("_" & str libraryFile & "_mp"):
		%includeonce% show "Loading " & str libraryFile;
		scantokens ("input " & str libraryFile);
	else:
		%includeonce% show str libraryFile & " already loaded.";
	fi;
enddef;

inputonce util_log;
inputonce 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;

  %% Used to set the base of a text to the natural base of the letters.
  %% If set to '1', 'y' and 'a' will have the same base line, the tail
  %% of 'y' being rendered below that line
  %%
  var(numeric) ignoreNegativeBase;

  %% If these attributes are set to -1, the height and width of the
  %% Picture object is deduced from the dimensions of the contained
  %% object (picture or text). If one of these parameters is set to
  %% a positive value, then the given value is used, regardless of the
  %% size of the contents.
  %%
  var(numeric) fixedWidth, fixedHeight;

  %% Controls how the contents of the picture is to be aligned relatively
  %% to the picture margins. Can be one of "left", "centered", and defaults to
  %% "left". The "centered" setting is useful for Picture-s whose fixedWidth
  %% is set to a given value.
  %%
  var(string) halign, valign;

  var(string) textDecoration;

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

  @#ignoreNegativeBase := 0;

  @#fixedWidth  := -1;
  @#fixedHeight := -1;

  @#halign := "left";
  @#valign := "bottom";

  @#textDecoration := "none";

  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;
  @#textDecoration := src.textDecoration;

  @#fixedWidth := src.fixedWidth;
  @#fixedHeight := src.fixedHeight;
  
  @#halign := src.halign;
  @#valign := src.valign;
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);
PictureInfo.iPictNoMargins(0, 0, 0, 0)(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 @#laidout = 1:
    log "Picture " & str @# & " already laid out.";
  else:
    log "Laying out " & (str @#);

    @#laidout := 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: " & @#contentAsString;
      @#pict = @#contentAsString infont @#info.iFont.name scaled @#info.iFont.scale;
    else:
      log "Show unknown parameter type in picture layout";
      2 = 3; % force an error
    fi;

    @#negativeBase = ypart llcorner @#pict;

    if @#info.ignoreNegativeBase = 0:
      negativeBaseAdjustement__ := 0;
    else:
      negativeBaseAdjustement__ := @#negativeBase;
    fi; 

    if @#info.fixedWidth = -1:
      @#width = pictWidth(@#pict) + @#info.left + @#info.right;
    else:
      @#width = @#info.fixedWidth;
    fi;

    if @#info.fixedHeight = -1:
      @#height = pictHeight(@#pict) + @#info.top + @#info.bottom + negativeBaseAdjustement__;
    else:
      @#height = @#info.fixedHeight;
    fi;

    if @#info.halign = "left":
      xdelta__ := @#info.left;
    elseif @#info.halign = "center":
      xdelta__ := (@#width-pictWidth(@#pict))/2;
    else:
      2 = 3; % throw exception illegal value for @#info.halign
    fi;

    if @#info.valign = "bottom":
      ydelta__ := @#info.bottom - @#negativeBase + negativeBaseAdjustement__;
    elseif @#info.valign = "center":
      ydelta__ := (@#height-pictHeight(@#pict))/2 - @#negativeBase + negativeBaseAdjustement__;
    else:
      2 = 3; % throw exception illegal value for @#info.valign
    fi;

    @#contentShift = @#sw + (xdelta__, ydelta__);
  fi;
enddef;

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

  if (@#info.textDecoration = "underline"):
    save ydelta, height;
    height := pictHeight(@#pict) - @#negativeBase;

    ydelta := @#info.bottom - @#negativeBase - 0.8;

    draw @#sw + (@#info.left, ydelta) -- @#se + (-@#info.right, ydelta) withcolor black withpen pencircle scaled 0.2bp;
  fi;

  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;