-
Notifications
You must be signed in to change notification settings - Fork 3
/
FigLabel.m
225 lines (144 loc) · 8.13 KB
/
FigLabel.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
(* ::Package:: *)
(*Header comments*)
(* :Title: FigLabel *)
(* :Context: SciDraw` *)
(* :Summary: Label object *)
(* :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 package information file. *)
(*Begin package*)
(*Package context definition*)
BeginPackage["SciDraw`",SciDraw`Private`$ExternalContexts];
Unprotect[Evaluate[$Context<>"*"]];
(*Object usage*)
FigLabel::usage="FIGURE OBJECT: FigLabel[point,text] generates a label at the given point. If point is given as an anchor object, the default text offset and orientation information is taken from the anchor. FigLabel[object,name,text] or FigLabel[object,name,arg,text] attaches the label to an anchor extracted from the given object. FigLabel[text] takes its position from the option Position.";
(*Other usage*)
Displacement::usage="Option name for use with figure objects.";
DisplacedPoint::usage="Option name for use with figure objects.";
IntermediatePoints::usage="Option name for use with figure objects.";
(*Begin private context*)
Begin["`Private`"];
(*Dependencies*)
(*FigLabel*)
(*Object declaration*)
DeclareFigClass[
FigLabel,
{"TheAnchor","TextElement","Points"},
{},
{}
];
DefineFigClassOptions[
FigLabel,
{ShowLine->False}, (* callout is off by default *)
{
(* given position *)
Point->{0,0},
(* anchor position *)
Position->Automatic,
(* label displacement *)
DisplacedPoint->None,
Displacement->None,
(* callout *)
IntermediatePoints->None,
(* curve/arrowhead *)
FigArrowheadOptions[False,True],
FigCurveOptions
}
];
(*Constructor*)
(*Note: Because of the optional first argument, syntactic ambiguity occurs in the very unlikely scenario in which the text art expression also matches the criteria for an option list, e.g., is a rule expression, a list of rule expressions, or (more plausibly) the null list.*)
BasicLabel[Self_Object][GivenAnchor_Object,TextArt_]:=Module[
{
RelativeAnchor,Anchor,UsedIntermediatePoints,Curve,CanvasPoints,TextElement,GivenDisplacedPoint
},
(* validate options *)
FigCheckOption[Self,DisplacedPoint,None|FigPointPattern,FigOptions];
FigCheckOption[Self,Displacement,FigDisplacementPattern,FigOptions];
FigCheckOption[Self,IntermediatePoints,None|{FigCurvePointPattern...},FigOptions];
FigCheckArrowheadOptions[Self];
FigCheckCurveOptions[Self];
(* generate anchor point for text *)
RelativeAnchor=DisplacePoint[FigAnchor[GivenAnchor],(Displacement/.FigOptions)];
GivenDisplacedPoint=ResolveOption[DisplacedPoint,{None->RelativeAnchor},FigOptions];
Anchor=FigAnchor[GivenDisplacedPoint];
(* construct callout curve specification *)
UsedIntermediatePoints=ResolveOption[IntermediatePoints,{None->{}},FigOptions];
Curve=Join[{Anchor},UsedIntermediatePoints,{GivenAnchor}];
CanvasPoints=FigResolveCurve[Self,Curve,FigOptions];
(* save data needed for anchor generation on callout curve *)
Self@SetPoints[CanvasPoints];
(* make graphics elements *)
(* short circuit if no displaced point -- else have divide-by-zero errors on arrowheads *)
If[((Displacement/.FigOptions)=!=None)||((DisplacedPoint/.FigOptions)=!=None),
(* curve *)
FigLineElement[
{Line[CanvasPoints]},
FigOptions
];
(* arrowheads *)
FigLineElement[
{Line[FigCurveArrowheadPoints[
Self@MakeAnchor[Tail,None],
Self@MakeAnchor[Head,None],
FigOptions
]]},
Flatten[{LineDashing->None,FigOptions}]
];
];
(* make text *)
TextElement=FigTextElement[
Anchor,
TextArt,
FigOptions
];
(* save references to spawned objects *)
Self@SetTheAnchor[Anchor];
Self@SetTextElement[TextElement];
];
Constructor[Class:FigLabel,Self_Object][TextArt_,Opts___?OptionQ]:=FigObjectWrapper[Class,Self,{Opts},
(* validate special option *)
FigCheckOption[Self,Point,FigPointPattern,FigOptions];
BasicLabel[Self][FigAnchor[(Point/.FigOptions)],TextArt]
];
Constructor[Class:FigLabel,Self_Object][p:FigPointPattern,TextArt_,Opts___?OptionQ]:=FigObjectWrapper[Class,Self,{Opts},
BasicLabel[Self][FigAnchor[p],TextArt]
];
(*With argument pattern*)
(* FigLabel[n,Name,Arg:_:None,Text_,Opts___]*)
(*unfortunately*)
(* FigLabel[obj,name,arg,text,opt->value]*)
(*matches n=obj, Name=name, Arg=text, Text=(opt->value), Opts={}...*)
(*Overcome by forbidding Text from matching OptionQ, at cost of possibly misinterpreting text in the form of a rule or list rules, including the null list.*)
(*Note that use of Longest on Opts empirically fails to accomplish goal.*)
Constructor[Class:FigLabel,Self_Object][(n:ObjectNamePattern[FigObject]),Name_,AnchorArg:_:None,TextArt:Except[_?OptionQ],Opts___?OptionQ]:=FigObjectWrapper[Class,Self,{Opts},
(*Print[n," ",Name," ",TextArt," ",{Opts}];*)
BasicLabel[Self][
FigAnchor[n,Name,ResolveOption[Position,{Automatic->AnchorArg},FigOptions]],
TextArt
]
];
(*Methods*)
(* rectangle-style anchor points, deduced from text element *)
MakeAnchor[Class:FigLabel,Self_Object][Name:Except[(Head|Tail)],Arg_]:=FigMakeAnchorWrapper[Class,Self,Name,Arg,
FigRectangleAnchor[
(Self@GetTextElement[])@GetCenter[],(Self@GetTextElement[])@GetRadius[],(Self@GetTextElement[])@GetPivot[],(Self@GetTextElement[])@GetRotation[],
Name,Arg
]
];
(* curve-style anchor points, from callout curve -- needed for arrowhead generation -- only define Head/Tail*)
MakeAnchor[Class:FigLabel,Self_Object][Name:(Head|Tail),Arg:_]:=FigMakeAnchorWrapper[Class,Self,Name,Arg,
FigCurveAnchorFromPoints[Self@GetPoints[],Name,Arg]
];
MakeBoundingBox[Class:FigLabel,Self_Object][]:=FigRectangleBoundingBox[
(Self@GetTextElement[])@GetCenter[],(Self@GetTextElement[])@GetRadius[],(Self@GetTextElement[])@GetPivot[],(Self@GetTextElement[])@GetRotation[]
];
(*End package*)
(*Exit private context*)
End[];
(*Exit package context*)
Protect[Evaluate[$Context<>"*"]];
Unprotect[Evaluate[$Context<>"$*"]];
EndPackage[];