| 1 |
% Copyright 2005 2015 Ovidiu Gheorghies |
| 2 |
% Licensed under the Apache License, Version 2.0. |
| 3 |
|
| 4 |
if known _util_picture_mp: |
| 5 |
expandafter endinput |
| 6 |
fi; |
| 7 |
_util_picture_mp:=1; |
| 8 |
|
| 9 |
% Sadly, this copy of the macro is needed to prevent multiple file loads being shown by MetaPost. |
| 10 |
% The guard values (such as _metauml_mp) do ensure that the file isn't loaded multiple times, |
| 11 |
% but this macro makes sure that MetaPost won't try to load the file and display a message for that. |
| 12 |
def inputonce text libraryFile= |
| 13 |
if not known scantokens ("_" & str libraryFile & "_mp"): |
| 14 |
%includeonce% show "Loading " & str libraryFile; |
| 15 |
scantokens ("input " & str libraryFile); |
| 16 |
else: |
| 17 |
%includeonce% show str libraryFile & " already loaded."; |
| 18 |
fi; |
| 19 |
enddef; |
| 20 |
|
| 21 |
inputonce util_log; |
| 22 |
inputonce util_margins; |
| 23 |
|
| 24 |
if not known _n_cur_:input boxes;fi; |
| 25 |
|
| 26 |
vardef FontInfo@#(expr tname, tscale)= |
| 27 |
attributes(@#); |
| 28 |
var(string) className; |
| 29 |
var(string) name; |
| 30 |
var(numeric) scale; |
| 31 |
|
| 32 |
@#className := "FontInfo"; |
| 33 |
|
| 34 |
@#name := tname; |
| 35 |
@#scale := tscale; |
| 36 |
enddef; |
| 37 |
|
| 38 |
vardef FontInfoCopy@#(text fontInfo)= |
| 39 |
FontInfo@#(fontInfo.name, fontInfo.scale); |
| 40 |
@#ignoreNegativeBase := fontInfo.ignoreNegativeBase; |
| 41 |
enddef; |
| 42 |
|
| 43 |
vardef FontInfo_toString@#= |
| 44 |
save ret; string ret; |
| 45 |
ret := "FontInfo(" & (str @#) & "): {" & (@#name) & ", " & (decimal @#scale) & "}"; |
| 46 |
ret |
| 47 |
enddef; |
| 48 |
|
| 49 |
log "()()() Creating iFont"; |
| 50 |
FontInfo.iFont(defaultfont, defaultscale); |
| 51 |
|
| 52 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 53 |
% % |
| 54 |
% PICTURE % |
| 55 |
% % |
| 56 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 57 |
|
| 58 |
vardef PictureInfo@#(expr pleft, pright, ptop, pbottom)(text pfont)= |
| 59 |
attributes(@#); |
| 60 |
var(string) className; |
| 61 |
@#className := "PictureInfo"; |
| 62 |
|
| 63 |
var(color) borderColor; |
| 64 |
var(numeric) boxed; |
| 65 |
|
| 66 |
%% Used to set the base of a text to the natural base of the letters. |
| 67 |
%% If set to '1', 'y' and 'a' will have the same base line, the tail |
| 68 |
%% of 'y' being rendered below that line |
| 69 |
%% |
| 70 |
var(numeric) ignoreNegativeBase; |
| 71 |
|
| 72 |
%% If these attributes are set to -1, the height and width of the |
| 73 |
%% Picture object is deduced from the dimensions of the contained |
| 74 |
%% object (picture or text). If one of these parameters is set to |
| 75 |
%% a positive value, then the given value is used, regardless of the |
| 76 |
%% size of the contents. |
| 77 |
%% |
| 78 |
var(numeric) fixedWidth, fixedHeight; |
| 79 |
|
| 80 |
%% Controls how the contents of the picture is to be aligned relatively |
| 81 |
%% to the picture margins. Can be one of "left", "centered", and defaults to |
| 82 |
%% "left". The "centered" setting is useful for Picture-s whose fixedWidth |
| 83 |
%% is set to a given value. |
| 84 |
%% |
| 85 |
var(string) halign, valign; |
| 86 |
|
| 87 |
var(string) textDecoration; |
| 88 |
|
| 89 |
@#boxed := 0; |
| 90 |
@#borderColor := blue; |
| 91 |
|
| 92 |
@#ignoreNegativeBase := 0; |
| 93 |
|
| 94 |
@#fixedWidth := -1; |
| 95 |
@#fixedHeight := -1; |
| 96 |
|
| 97 |
@#halign := "left"; |
| 98 |
@#valign := "bottom"; |
| 99 |
|
| 100 |
@#textDecoration := "none"; |
| 101 |
|
| 102 |
Margins.@#(pleft, pright, ptop, pbottom); |
| 103 |
|
| 104 |
FontInfoCopy.@#iFont(pfont); |
| 105 |
enddef; |
| 106 |
|
| 107 |
vardef PictureInfoCopy@#(text src)= |
| 108 |
PictureInfo@#(src.left, src.right, src.top, src.bottom)(src.iFont); |
| 109 |
|
| 110 |
@#boxed := src.boxed; |
| 111 |
@#borderColor := src.borderColor; |
| 112 |
@#ignoreNegativeBase := src.ignoreNegativeBase; |
| 113 |
@#textDecoration := src.textDecoration; |
| 114 |
|
| 115 |
@#fixedWidth := src.fixedWidth; |
| 116 |
@#fixedHeight := src.fixedHeight; |
| 117 |
|
| 118 |
@#halign := src.halign; |
| 119 |
@#valign := src.valign; |
| 120 |
enddef; |
| 121 |
|
| 122 |
vardef PictureInfo_toString@#= |
| 123 |
save ret; string ret; |
| 124 |
ret := "PictureInfo(" & (str @#) & "): {" & (decimal @#left) & ", " & (decimal @#right) & ", " & (decimal @#top) & ", " & (decimal @#bottom) & "}" & " boxed: " & (decimal @#boxed); |
| 125 |
ret := ret & " Font: " & toString(@#iFont); |
| 126 |
ret |
| 127 |
enddef; |
| 128 |
|
| 129 |
PictureInfo.iPict(2, 2, 2, 2)(iFont); |
| 130 |
PictureInfo.iPictNoMargins(0, 0, 0, 0)(iFont); |
| 131 |
|
| 132 |
PictureInfoCopy.iPictBoxed(iPict); |
| 133 |
iPictBoxed.boxed := 1; |
| 134 |
|
| 135 |
%% |
| 136 |
%% Contructs a Picture object by wrapping around the |
| 137 |
%% low-level picture thePict. |
| 138 |
%% |
| 139 |
vardef EPicture@#(text iPict)(expr thePict) = |
| 140 |
ObjectEquations(@#); |
| 141 |
@#className := "Picture"; |
| 142 |
|
| 143 |
var(pair) contentShift; |
| 144 |
var(picture) pict; |
| 145 |
var(numeric) negativeBase; |
| 146 |
var(string) contentAsString; |
| 147 |
var(picture) contentAsPicture; |
| 148 |
|
| 149 |
PictureInfoCopy.@#info(iPict); |
| 150 |
|
| 151 |
if string thePict: |
| 152 |
log "Picture " & (str @#) & " is a text"; |
| 153 |
@#contentAsString := thePict; |
| 154 |
log @#contentAsString; |
| 155 |
elseif picture thePict: |
| 156 |
log "Picture " & (str @#) & " is a native pict"; |
| 157 |
@#contentAsPicture := thePict; |
| 158 |
log @#contentAsPicture; |
| 159 |
else: |
| 160 |
log "Picture " & (str @#) & " [error]"; |
| 161 |
fi; |
| 162 |
|
| 163 |
enddef; |
| 164 |
|
| 165 |
%% |
| 166 |
%% Contructs a Picture object by wrapping around the |
| 167 |
%% low-level picture thePict. |
| 168 |
%% |
| 169 |
vardef Picture@#(expr thePict) = |
| 170 |
EPicture@#(iPict)(thePict); |
| 171 |
enddef; |
| 172 |
|
| 173 |
%% |
| 174 |
%% Lays out the Picture. |
| 175 |
%% |
| 176 |
vardef Picture_layout@# = |
| 177 |
if @#laidout = 1: |
| 178 |
log "Picture " & str @# & " already laid out."; |
| 179 |
else: |
| 180 |
log "Laying out " & (str @#); |
| 181 |
|
| 182 |
@#laidout := 1; |
| 183 |
|
| 184 |
if known @#contentAsPicture: |
| 185 |
log "Content is known to be a picture"; |
| 186 |
@#pict = @#contentAsPicture; |
| 187 |
elseif known @#contentAsString: |
| 188 |
log "Content is known to be a string: " & @#contentAsString; |
| 189 |
@#pict = @#contentAsString infont @#info.iFont.name scaled @#info.iFont.scale; |
| 190 |
else: |
| 191 |
log "Show unknown parameter type in picture layout"; |
| 192 |
2 = 3; % force an error |
| 193 |
fi; |
| 194 |
|
| 195 |
@#negativeBase = ypart llcorner @#pict; |
| 196 |
|
| 197 |
if @#info.ignoreNegativeBase = 0: |
| 198 |
negativeBaseAdjustement__ := 0; |
| 199 |
else: |
| 200 |
negativeBaseAdjustement__ := @#negativeBase; |
| 201 |
fi; |
| 202 |
|
| 203 |
if @#info.fixedWidth = -1: |
| 204 |
@#width = pictWidth(@#pict) + @#info.left + @#info.right; |
| 205 |
else: |
| 206 |
@#width = @#info.fixedWidth; |
| 207 |
fi; |
| 208 |
|
| 209 |
if @#info.fixedHeight = -1: |
| 210 |
@#height = pictHeight(@#pict) + @#info.top + @#info.bottom + negativeBaseAdjustement__; |
| 211 |
else: |
| 212 |
@#height = @#info.fixedHeight; |
| 213 |
fi; |
| 214 |
|
| 215 |
if @#info.halign = "left": |
| 216 |
xdelta__ := @#info.left; |
| 217 |
elseif @#info.halign = "center": |
| 218 |
xdelta__ := (@#width-pictWidth(@#pict))/2; |
| 219 |
else: |
| 220 |
2 = 3; % throw exception illegal value for @#info.halign |
| 221 |
fi; |
| 222 |
|
| 223 |
if @#info.valign = "bottom": |
| 224 |
ydelta__ := @#info.bottom - @#negativeBase + negativeBaseAdjustement__; |
| 225 |
elseif @#info.valign = "center": |
| 226 |
ydelta__ := (@#height-pictHeight(@#pict))/2 - @#negativeBase + negativeBaseAdjustement__; |
| 227 |
else: |
| 228 |
2 = 3; % throw exception illegal value for @#info.valign |
| 229 |
fi; |
| 230 |
|
| 231 |
@#contentShift = @#sw + (xdelta__, ydelta__); |
| 232 |
fi; |
| 233 |
enddef; |
| 234 |
|
| 235 |
%% |
| 236 |
%% Draws the Picture. |
| 237 |
%% |
| 238 |
vardef Picture_draw@# = |
| 239 |
Picture_layout@#; |
| 240 |
objectEnsurePositioning.@#; |
| 241 |
|
| 242 |
draw @#pict shifted @#contentShift; |
| 243 |
|
| 244 |
if (@#info.textDecoration = "underline"): |
| 245 |
save ydelta, height; |
| 246 |
height := pictHeight(@#pict) - @#negativeBase; |
| 247 |
|
| 248 |
ydelta := @#info.bottom - @#negativeBase - 0.8; |
| 249 |
|
| 250 |
draw @#sw + (@#info.left, ydelta) -- @#se + (-@#info.right, ydelta) withcolor black withpen pencircle scaled 0.2bp; |
| 251 |
fi; |
| 252 |
|
| 253 |
if (@#info.boxed = 1): |
| 254 |
draw objectBox(@#) withcolor @#info.borderColor; |
| 255 |
fi; |
| 256 |
enddef; |
| 257 |
|
| 258 |
vardef Picture_border@#= |
| 259 |
objectBox(@#) |
| 260 |
enddef; |
| 261 |
|
| 262 |
vardef aitem(text pictInfo)(text thePict)(text equation)= |
| 263 |
save obj; |
| 264 |
EPicture.obj(pictInfo)(thePict); |
| 265 |
equation; |
| 266 |
drawObject(obj); |
| 267 |
enddef; |
| 268 |
|
| 269 |
vardef item@#(text iPict)(text thePict)(text equation)= |
| 270 |
save itemName; |
| 271 |
string itemName; |
| 272 |
itemName := str @#; |
| 273 |
|
| 274 |
log "Creating an item with name: '" & itemName & "'"; |
| 275 |
|
| 276 |
if itemName = "": |
| 277 |
log "Name is empty, creating an anonymous item!"; |
| 278 |
aitem(iPict)(thePict)(equation); |
| 279 |
else: |
| 280 |
EPicture@#(iPict)(thePict); |
| 281 |
equation; |
| 282 |
drawObject(@#); |
| 283 |
fi; |
| 284 |
enddef; |
| 285 |
|