Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
LengthAttribute.cs
1using System;
2using System.Xml;
3using Waher.Content;
5using Waher.Script;
8
10{
14 public class LengthAttribute : Attribute<Length>
15 {
22 public LengthAttribute(string AttributeName, Length Value, Layout2DDocument Document)
23 : base(AttributeName, Value, Document)
24 {
25 }
26
33 public LengthAttribute(XmlElement E, string AttributeName, Layout2DDocument Document)
34 : base(E, AttributeName, true, Document)
35 {
36 }
37
44 public LengthAttribute(string AttributeName, Expression Expression, Layout2DDocument Document)
45 : base(AttributeName, Expression, Document)
46 {
47 }
48
55 public override bool TryConvert(object Result, out Length Value)
56 {
57 if (Result is double d)
58 {
59 Value = new Length((float)(d * 100), LengthUnit.Percent);
60 return true;
61 }
62 else if (Result is IPhysicalQuantity PQ)
63 {
65
66 switch (Q.Unit.ToString().ToLower())
67 {
68 case "px":
69 Value = new Length((float)Q.Magnitude, LengthUnit.Px);
70 return true;
71
72 case "pt":
73 Value = new Length((float)Q.Magnitude, LengthUnit.Pt);
74 return true;
75
76 case "pc":
77 Value = new Length((float)Q.Magnitude, LengthUnit.Pc);
78 return true;
79
80 case "cm":
81 Value = new Length((float)Q.Magnitude, LengthUnit.Cm);
82 return true;
83
84 case "in":
85 Value = new Length((float)Q.Magnitude, LengthUnit.In);
86 return true;
87
88 case "mm":
89 Value = new Length((float)Q.Magnitude, LengthUnit.Mm);
90 return true;
91
92 case "em":
93 Value = new Length((float)Q.Magnitude, LengthUnit.Em);
94 return true;
95
96 case "ex":
97 Value = new Length((float)Q.Magnitude, LengthUnit.Ex);
98 return true;
99
100 case "ch":
101 Value = new Length((float)Q.Magnitude, LengthUnit.Ch);
102 return true;
103
104 case "rem":
105 Value = new Length((float)Q.Magnitude, LengthUnit.Rem);
106 return true;
107
108 case "vw":
109 Value = new Length((float)Q.Magnitude, LengthUnit.Vw);
110 return true;
111
112 case "vh":
113 Value = new Length((float)Q.Magnitude, LengthUnit.Vh);
114 return true;
115
116 case "vmin":
117 Value = new Length((float)Q.Magnitude, LengthUnit.Vmin);
118 return true;
119
120 case "vmax":
121 Value = new Length((float)Q.Magnitude, LengthUnit.Vmax);
122 return true;
123
124 default:
125 if (Unit.TryConvert(Q.Magnitude, Q.Unit, pixels, out double Pixels))
126 {
127 Value = new Length((float)Pixels, LengthUnit.Px);
128 return true;
129 }
130 else
131 {
132 Value = null;
133 return false;
134 }
135 }
136 }
137 else
138 return base.TryConvert(Result, out Value);
139 }
140
141 private static readonly Unit pixels = new Unit("px");
142
149 public override bool TryParse(string StringValue, out Length Value)
150 {
152 int c = StringValue.Length;
153 int i = c;
154 char ch = StringValue[--i];
155
156 if (ch == '%')
157 Unit = LengthUnit.Percent;
158 else
159 {
160 char ch2 = StringValue[--i];
161
162 switch (ch)
163 {
164 case 'x':
165 switch (ch2)
166 {
167 case 'p':
168 Unit = LengthUnit.Px;
169 break;
170
171 case 'e':
172 Unit = LengthUnit.Ex;
173 break;
174
175 case 'a':
176 if (c < 4 ||
177 StringValue[--i] != 'm' ||
178 StringValue[--i] != 'v')
179 {
180 throw InvalidLengthException(StringValue);
181 }
182
183 Unit = LengthUnit.Vmax;
184 break;
185
186 default:
187 throw InvalidLengthException(StringValue);
188 }
189 break;
190
191 case 't':
192 if (ch2 != 'p')
193 throw InvalidLengthException(StringValue);
194
195 Unit = LengthUnit.Pt;
196 break;
197
198 case 'c':
199 if (ch2 != 'p')
200 throw InvalidLengthException(StringValue);
201
202 Unit = LengthUnit.Pc;
203 break;
204
205 case 'm':
206 switch (ch2)
207 {
208 case 'c':
209 Unit = LengthUnit.Cm;
210 break;
211
212 case 'm':
213 Unit = LengthUnit.Mm;
214 break;
215
216 case 'e':
217 if (c >= 3 && StringValue[i - 1] == 'r')
218 {
219 Unit = LengthUnit.Rem;
220 i--;
221 }
222 else
223 Unit = LengthUnit.Em;
224 break;
225
226 default:
227 throw InvalidLengthException(StringValue);
228 }
229 break;
230
231 case 'n':
232 if (c >= 4 && StringValue[i - 1] == 'm')
233 {
234 i--;
235
236 if (StringValue[--i] == 'v')
237 Unit = LengthUnit.Vmin;
238 else
239 throw InvalidLengthException(StringValue);
240 }
241 else
242 Unit = LengthUnit.In;
243 break;
244
245 case 'h':
246 switch (ch2)
247 {
248 case 'c':
249 Unit = LengthUnit.Ch;
250 break;
251
252 case 'v':
253 Unit = LengthUnit.Vh;
254 break;
255
256 default:
257 throw InvalidLengthException(StringValue);
258 }
259 break;
260
261 case 'w':
262 if (ch2 != 'v')
263 throw InvalidLengthException(StringValue);
264
265 Unit = LengthUnit.Vw;
266 break;
267
268 default:
269 throw InvalidLengthException(StringValue);
270 }
271 }
272
273 while (i >= 0 && ((ch = StringValue[i]) <= ' ' || ch == 160))
274 i--;
275
276 StringValue = StringValue.Substring(0, i);
277
278 if (!CommonTypes.TryParse(StringValue, out float d))
279 throw InvalidLengthException(StringValue);
280
281 Value = new Length(d, Unit);
282
283 return true;
284 }
285
286 private static Exception InvalidLengthException(string AttributeValue)
287 {
288 return new LayoutSyntaxException("Invalid length: " + AttributeValue);
289 }
290
296 public override string ToString(Length Value)
297 {
298 string s = CommonTypes.Encode(Value.Value);
299
300 switch (Value.Unit)
301 {
302 case LengthUnit.Px: return s + " px";
303 case LengthUnit.Pt: return s + " pt";
304 case LengthUnit.Pc: return s + " pc";
305 case LengthUnit.Cm: return s + " cm";
306 case LengthUnit.In: return s + " in";
307 case LengthUnit.Mm: return s + " mm";
308 case LengthUnit.Em: return s + " em";
309 case LengthUnit.Ex: return s + " ex";
310 case LengthUnit.Ch: return s + " ch";
311 case LengthUnit.Rem: return s + " rem";
312 case LengthUnit.Vw: return s + " vw";
313 case LengthUnit.Vh: return s + " vh";
314 case LengthUnit.Vmin: return s + " vmin";
315 case LengthUnit.Vmax: return s + " vmax";
316 case LengthUnit.Percent: return s + " %";
317 default: return s;
318 }
319 }
320
328 {
329 if (this.HasPresetValue)
330 return this;
331 else
332 return new LengthAttribute(this.Name, this.Expression, ForDocument);
333 }
334
335 }
336}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static string Encode(bool x)
Encodes a Boolean for use in XML and other formats.
Definition: CommonTypes.cs:594
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Contains a 2D layout document.
Manages an attribute value or expression.
Definition: Attribute.cs:14
bool HasPresetValue
If the attribute has a preset value.
Definition: Attribute.cs:129
LengthAttribute(XmlElement E, string AttributeName, Layout2DDocument Document)
Length attribute
LengthAttribute(string AttributeName, Length Value, Layout2DDocument Document)
Length attribute
override bool TryParse(string StringValue, out Length Value)
Tries to parse a string value
LengthAttribute(string AttributeName, Expression Expression, Layout2DDocument Document)
Length attribute
LengthAttribute CopyIfNotPreset(Layout2DDocument ForDocument)
Copies the attribute object if undefined, or defined by an expression. Returns a reference to itself,...
override string ToString(Length Value)
Converts a value to a string.
override bool TryConvert(object Result, out Length Value)
Tries to convert script result to a value of type float.
LengthUnit Unit
Unit of length.
Definition: Length.cs:113
float Value
Value of length
Definition: Length.cs:104
Class managing a script expression.
Definition: Expression.cs:39
PhysicalQuantity ToPhysicalQuantity()
Converts underlying object to a physical quantity.
Represents a unit.
Definition: Unit.cs:15
static bool TryConvert(double From, Unit FromUnit, Unit ToUnit, out double To)
Tries to convert a magnitude in one unit to a magnitude in another.
Definition: Unit.cs:1201
override string ToString()
Definition: Unit.cs:517
Interface for objects that can be represented as a physical quantity.
LengthUnit
Unit of length
Definition: Length.cs:7