[12762] | 1 | using System;
|
---|
| 2 | using System.Xml;
|
---|
| 3 | using System.Text.RegularExpressions;
|
---|
| 4 |
|
---|
| 5 | namespace SharpVectors.Dom.Css
|
---|
| 6 | {
|
---|
| 7 | public class CssPrimitiveLengthValue : CssPrimitiveValue
|
---|
| 8 | {
|
---|
| 9 | //protected const double Dpi = 96;
|
---|
| 10 | protected const double Dpi = 90; // The common default value for this is 90, not 96
|
---|
| 11 | protected const double CmPerIn = 2.54;
|
---|
| 12 |
|
---|
| 13 | #region Constructors
|
---|
| 14 | public CssPrimitiveLengthValue(string number, string unit, bool readOnly) : base(number+unit, readOnly)
|
---|
| 15 | {
|
---|
| 16 | _setType(unit);
|
---|
| 17 | SetFloatValue(number);
|
---|
| 18 | }
|
---|
| 19 |
|
---|
| 20 | public CssPrimitiveLengthValue(string cssText, bool readOnly) : base(cssText, readOnly)
|
---|
| 21 | {
|
---|
| 22 | OnSetCssText(cssText);
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | public CssPrimitiveLengthValue(double number, string unit, bool readOnly) : base(number+unit, readOnly)
|
---|
| 26 | {
|
---|
| 27 | _setType(unit);
|
---|
| 28 | SetFloatValue(number);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | protected CssPrimitiveLengthValue() : base()
|
---|
| 32 | {
|
---|
| 33 | }
|
---|
| 34 | #endregion
|
---|
| 35 |
|
---|
| 36 | public override CssValue GetAbsoluteValue(string propertyName, XmlElement elm)
|
---|
| 37 | {
|
---|
| 38 | return new CssAbsPrimitiveLengthValue(this, propertyName, elm);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | protected override void OnSetCssText(string cssText)
|
---|
| 42 | {
|
---|
| 43 | Regex re = new Regex(CssValue.LengthPattern);
|
---|
| 44 | Match match = re.Match(cssText);
|
---|
| 45 | if(match.Success)
|
---|
| 46 | {
|
---|
| 47 | _setType(match.Groups["lengthUnit"].Value);
|
---|
| 48 | SetFloatValue(match.Groups["lengthNumber"].Value);
|
---|
| 49 | }
|
---|
| 50 | else
|
---|
| 51 | {
|
---|
| 52 | throw new DomException(DomExceptionType.SyntaxErr, "Unrecognized length format: " + cssText);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | private void _setType(string unit)
|
---|
| 56 | {
|
---|
| 57 | switch(unit)
|
---|
| 58 | {
|
---|
| 59 | case "cm":
|
---|
| 60 | SetPrimitiveType(CssPrimitiveType.Cm);
|
---|
| 61 | break;
|
---|
| 62 | case "mm":
|
---|
| 63 | SetPrimitiveType(CssPrimitiveType.Mm);
|
---|
| 64 | break;
|
---|
| 65 | case "px":
|
---|
| 66 | SetPrimitiveType(CssPrimitiveType.Px);
|
---|
| 67 | break;
|
---|
| 68 | case "em":
|
---|
| 69 | SetPrimitiveType(CssPrimitiveType.Ems);
|
---|
| 70 | break;
|
---|
| 71 | case "ex":
|
---|
| 72 | SetPrimitiveType(CssPrimitiveType.Exs);
|
---|
| 73 | break;
|
---|
| 74 | case "pc":
|
---|
| 75 | SetPrimitiveType(CssPrimitiveType.Pc);
|
---|
| 76 | break;
|
---|
| 77 | case "pt":
|
---|
| 78 | SetPrimitiveType(CssPrimitiveType.Pt);
|
---|
| 79 | break;
|
---|
| 80 | case "in":
|
---|
| 81 | SetPrimitiveType(CssPrimitiveType.In);
|
---|
| 82 | break;
|
---|
| 83 | case "%":
|
---|
| 84 | SetPrimitiveType(CssPrimitiveType.Percentage);
|
---|
| 85 | break;
|
---|
| 86 | case "":
|
---|
| 87 | SetPrimitiveType(CssPrimitiveType.Number);
|
---|
| 88 | break;
|
---|
| 89 | default:
|
---|
| 90 | throw new DomException(DomExceptionType.SyntaxErr, "Unknown length unit");
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | // only for absolute values
|
---|
| 95 | private double _getPxLength()
|
---|
| 96 | {
|
---|
| 97 | switch(PrimitiveType)
|
---|
| 98 | {
|
---|
| 99 | case CssPrimitiveType.In:
|
---|
| 100 | return floatValue * Dpi;
|
---|
| 101 | case CssPrimitiveType.Cm:
|
---|
| 102 | return floatValue / CmPerIn * Dpi;
|
---|
| 103 | case CssPrimitiveType.Mm:
|
---|
| 104 | return floatValue / 10 / CmPerIn * Dpi;
|
---|
| 105 | case CssPrimitiveType.Pt:
|
---|
| 106 | return floatValue / 72 * Dpi;
|
---|
| 107 | case CssPrimitiveType.Pc:
|
---|
| 108 | return floatValue / 6 * Dpi;
|
---|
| 109 | default:
|
---|
| 110 | return floatValue;
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | private double _getInLength()
|
---|
| 115 | {
|
---|
| 116 | return _getPxLength() / Dpi;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | public override double GetFloatValue(CssPrimitiveType unitType)
|
---|
| 120 | {
|
---|
| 121 | double ret = Double.NaN;
|
---|
| 122 | switch(PrimitiveType)
|
---|
| 123 | {
|
---|
| 124 | case CssPrimitiveType.Number:
|
---|
| 125 | case CssPrimitiveType.Px:
|
---|
| 126 | case CssPrimitiveType.Cm:
|
---|
| 127 | case CssPrimitiveType.Mm:
|
---|
| 128 | case CssPrimitiveType.In:
|
---|
| 129 | case CssPrimitiveType.Pt:
|
---|
| 130 | case CssPrimitiveType.Pc:
|
---|
| 131 | if(unitType == CssPrimitiveType.Px) ret = _getPxLength();
|
---|
| 132 | else if(unitType == CssPrimitiveType.Number) ret = _getPxLength();
|
---|
| 133 | else if(unitType == CssPrimitiveType.In) ret = _getInLength();
|
---|
| 134 | else if(unitType == CssPrimitiveType.Cm) ret = _getInLength() * CmPerIn;
|
---|
| 135 | else if(unitType == CssPrimitiveType.Mm) ret = _getInLength() * CmPerIn * 10;
|
---|
| 136 | else if(unitType == CssPrimitiveType.Pt) ret = _getInLength() * 72;
|
---|
| 137 | else if(unitType == CssPrimitiveType.Pc) ret = _getInLength() * 6;
|
---|
| 138 | break;
|
---|
| 139 | case CssPrimitiveType.Percentage:
|
---|
| 140 | if(unitType == CssPrimitiveType.Percentage) ret = floatValue;
|
---|
| 141 | break;
|
---|
| 142 | case CssPrimitiveType.Ems:
|
---|
| 143 | if(unitType == CssPrimitiveType.Ems) ret = floatValue;
|
---|
| 144 | break;
|
---|
| 145 | case CssPrimitiveType.Exs:
|
---|
| 146 | if(unitType == CssPrimitiveType.Exs) ret = floatValue;
|
---|
| 147 | break;
|
---|
| 148 | }
|
---|
| 149 | if(Double.IsNaN(ret))
|
---|
| 150 | {
|
---|
| 151 | throw new DomException(DomExceptionType.InvalidAccessErr);
|
---|
| 152 | }
|
---|
| 153 | else
|
---|
| 154 | {
|
---|
| 155 | return ret;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | public override string CssText
|
---|
| 160 | {
|
---|
| 161 | get
|
---|
| 162 | {
|
---|
| 163 | return GetFloatValue(PrimitiveType).ToString(CssNumber.Format) + PrimitiveTypeAsString;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 | }
|
---|