-
Notifications
You must be signed in to change notification settings - Fork 3
/
FigAnchor.m
294 lines (177 loc) · 8.44 KB
/
FigAnchor.m
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
286
287
288
289
290
291
292
293
294
(* ::Package:: *)
(*Header comments*)
(* :Title: FigAnchor *)
(* :Context: SciDraw` *)
(* :Summary: Declares the figure anchor object class *)
(* :Author: Mark A. Caprio, Department of Physics, University of Notre Dame *)
(* :Copyright: Copyright FIGYEAR, Mark A. Caprio *)
(* :Package Version: FIGVERSION *)
(* :Mathematica Version: MATHVERSION *)
(* :Discussion: FIGDISCUSSION *)
(* :History: See main package file. *)
(*Begin package*)
(*Package context definition*)
BeginPackage["SciDraw`",SciDraw`Private`$ExternalContexts];
Unprotect[Evaluate[$Context<>"*"]];
(*Begin private context*)
Begin["`Private`"];
(*Dependencies*)
(*FigAnchor*)
(*Constructors*)
(* From coordinate point -- FigAnchor[{x,y}|etc.] with optional arguments FigAnchor[{x,y}|etc.,{xo,yo},theta]*)
(* returns anchor with point {x,y}, text offset {xo,yo} (default {0,0}), and tangent angle theta (default 0).*)
(* DEPRECATED: Direct access to FigAnchor is deprecated in favor of the wrapper functions Anchor and GetAnchor, in anticipation to revisions to the SciDraw internal representation of anchors. *)
Constructor[Class:FigAnchor,Self_Object][p:FigCoordinatePointPattern,Offset:FigOffsetPattern:Center,theta:(_?NumericQ):0]:=Module[
{},
Self@SetPoint[FigResolvePoint[p]];
Self@SetOffset[FigResolveOffset[Offset]];
Self@SetAngle[theta];
];
(*Copy from another anchor -- FigAnchor[anchorobject|anchorname] with optional arguments FigAnchor[anchorobject|anchorname,{xo,yo},theta]*)
(* anchorobject -- which is how it would be returned from an internal calculation*)
(* anchorname -- which is how the user would specify it*)
(* *)
(* returns anchor with canvas point taken from given anchor, text offset {xo,yo} (default from given anchor), and tangent angle theta (default from given anchor).*)
Constructor[Class:FigAnchor,Self_Object][
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor])
]:=Module[
{},
SetObjectData[Self,Object[n]];
];
Constructor[Class:FigAnchor,Self_Object][
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor]),
Offset:FigOffsetPattern:Center
]:=Module[
{},
SetObjectData[Self,Object[n]];
Self@SetOffset[FigResolveOffset[Offset]];
];
Constructor[Class:FigAnchor,Self_Object][
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor]),
Offset:FigOffsetPattern,theta:(_?NumericQ)
]:=Module[
{},
SetObjectData[Self,Object[n]];
Self@SetOffset[FigResolveOffset[Offset]];
Self@SetAngle[theta];
];
(*
Matching fails when optional arguments omitted (under Mathematica 8):
Constructor[Class:FigAnchor,Self_Object][
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor]),
Offset:{_?NumericQ,_?NumericQ}:Automatic,theta:(_?NumericQ):Automatic
]:=Module[
{},
Self@SetPoint[Object[n]@GetPoint[]];
Self@SetOffset[ReplaceSequential[Offset,{Automatic:>Object[n]@GetOffset[]}]];
Self@SetAngle[ReplaceSequential[theta,{Automatic:>Object[n]@GetAngle[]}]];
];
*)
(*From figure object MakeAnchor method -- FigAnchor[objectname,arg]*)
General::figmakeanchor="Unable to resolve anchor specification `1`.\n(Diagnostic information: `2`)";
Constructor[Class:FigAnchor,Self_Object][p:(n:ObjectNamePattern[FigObject]),Name_,Arg_:None]:=Module[
{TemporaryAnchor},
TemporaryAnchor=Object[n]@MakeAnchor[Name,Arg];
(* verify success of MakeAnchor call *)
If[
Head[TemporaryAnchor]=!=Object,
FigError[Self,"figmakeanchor",p,TemporaryAnchor]
];
(* copy temporary anchor to self *)
SetObjectData[Self,TemporaryAnchor];
];
(* advanced anchor wrapper -- UNDER CONSTRUCTION *)
(*
Constructor[Class:FigAnchor,Self_Object][p:(n:ObjectNamePattern[FigObject]),Name_,Opts___?OptionsPattern]:=Module[
{OptionsList=Flatten[{Opts}],TemporaryAnchor},
TemporaryAnchor=Object[n]@MakeAnchor[Name,OptionsList];
(* verify success of MakeAnchor call *)
If[
Head[TemporaryAnchor]=!=Object,
FigError[Self,"figmakeanchor",p,TemporaryAnchor]
];
(* copy temporary anchor to self *)
SetObjectData[Self,TemporaryAnchor];
];
*)
(*Set hook so that error in creation (duplicate name or syntax error) triggers Abort.*)
(*Note: Use symbol Figanchor in error message rather than Self, since no Self was created.*)
OnCreationFailure[FigAnchor,Self_Object][___]:=FigError[FigAnchor,"nocreate"];
(*Anchor interface functions*)
(*Anchor*)
(* From coordinate point -- Anchor[{x,y}|etc.] with optional arguments Anchor[{x,y}|etc.,{xo,yo},theta]*)
(* returns anchor with point {x,y}, text offset {xo,yo} (default {0,0}), and tangent angle theta (default 0).*)
Anchor[p:FigCoordinatePointPattern,Offset:FigOffsetPattern:Center,theta:(_?NumericQ):0]:=Module[
{CanvasPoint,UsedOffset,UsedAngle},
FigCheckInFigure[Anchor];
(* pass through *)
FigAnchor[p,Offset,theta]
];
(*Copy from another anchor -- Anchor[anchorobject|anchorname] with optional arguments Anchor[anchorobject|anchorname,{xo,yo},theta]*)
(* anchorobject -- which is how it would be returned from an internal calculation*)
(* anchorname -- which is how the user would specify it*)
(* *)
(* returns anchor with canvas point taken from given anchor, text offset {xo,yo} (default from given anchor), and tangent angle theta (default from given anchor).*)
Anchor[
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor])
]:=Module[
{},
FigCheckInFigure[Anchor];
(* pass through *)
FigAnchor[Object[n]]
];
Anchor[
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor]),
Offset:FigOffsetPattern:Center
]:=Module[
{},
FigCheckInFigure[Anchor];
(* pass through *)
FigAnchor[Object[n],Offset]
];
Anchor[
Object[n:ObjectNamePattern[FigAnchor]]|(n:ObjectNamePattern[FigAnchor]),
Offset:FigOffsetPattern,theta:(_?NumericQ)
]:=Module[
{},
FigCheckInFigure[Anchor];
(* pass through *)
FigAnchor[Object[n],Offset,theta]
];
DeclareFigFallThroughError[Anchor];
(*GetAnchor*)
(*From figure object MakeAnchor method -- GetAnchor[objectname,arg]*)
(**)
(*Note that GetAnchor can support Object[n] whereas FigAnchor direct access could not, for confusion with an anchor object representing a point.*)
GetAnchor::figmakeanchor="Unable to resolve anchor specification for object `1`, anchor name `2`, with argument `3`.\n(Diagnostic information: `4`)";
GetAnchor[Object[n:ObjectNamePattern[FigObject]]|(n:ObjectNamePattern[FigObject]),Name_,Arg_:None]:=Module[
{TemporaryAnchor},
FigCheckInFigure[GetAnchor];
(* attempt anchor retrieval *)
TemporaryAnchor=Object[n]@MakeAnchor[Name,Arg];
(* verify success of MakeAnchor call *)
(* note that this error trap is presently superfluous -- the actual error trap will occur earlier, in MakeAnchor, if MakeAnchor uses FigMakeAnchorWrapper interface *)
If[
Head[TemporaryAnchor]=!=Object,
FigError[GetAnchor,"figmakeanchor",n,Name,Arg,TemporaryAnchor]
];
(* return temporary anchor *)
TemporaryAnchor
];
DeclareFigFallThroughError[GetAnchor];
(*Diagnostic*)
(*Printed output of anchor parameters*)
ShowAnchor[p:FigPointPattern]:=Module[
{Anchor},
Anchor=FigAnchor[p];
Print["Anchor: canvas point ",Anchor@GetPoint[],", text offset ",Anchor@GetOffset[],", tilt angle ",Row[{Anchor@GetAngle[]/Degree,Degree}]];
];
(*Syntax fallthrough*)
DeclareFigFallThroughError[ShowAnchor];
(*End package*)
(*Exit private context*)
End[];
(*Exit package context*)
Protect[Evaluate[$Context<>"*"]];
Unprotect[Evaluate[$Context<>"$*"]];
EndPackage[];