Length
Represents an absolute length value.
using AngleSharp.Extensions;
using System;
namespace AngleSharp.Css.Values
{
public struct Length : IEquatable<Length>, IComparable<Length>, IFormattable
{
public enum Unit : byte
{
None,
Px,
Em,
Ex,
Cm,
Mm,
In,
Pt,
Pc,
Ch,
Rem,
Vw,
Vh,
Vmin,
Vmax,
Percent
}
public static readonly Length Zero = new Length(0, Unit.Px);
public static readonly Length Half = new Length(50, Unit.Percent);
public static readonly Length Full = new Length(100, Unit.Percent);
public static readonly Length Thin = new Length(1, Unit.Px);
public static readonly Length Medium = new Length(3, Unit.Px);
public static readonly Length Thick = new Length(5, Unit.Px);
public static readonly Length Missing = new Length(-1, Unit.Ch);
private readonly float _value;
private readonly Unit _unit;
public bool IsAbsolute {
get {
if (_unit != Unit.In && _unit != Unit.Mm && _unit != Unit.Pc && _unit != Unit.Px && _unit != Unit.Pt)
return _unit == Unit.Cm;
return true;
}
}
public bool IsRelative => !IsAbsolute;
public Unit Type => _unit;
public float Value => _value;
public string UnitString {
get {
switch (_unit) {
case Unit.Px:
return UnitNames.Px;
case Unit.Em:
return UnitNames.Em;
case Unit.Ex:
return UnitNames.Ex;
case Unit.Cm:
return UnitNames.Cm;
case Unit.Mm:
return UnitNames.Mm;
case Unit.In:
return UnitNames.In;
case Unit.Pt:
return UnitNames.Pt;
case Unit.Pc:
return UnitNames.Pc;
case Unit.Ch:
return UnitNames.Ch;
case Unit.Rem:
return UnitNames.Rem;
case Unit.Vw:
return UnitNames.Vw;
case Unit.Vh:
return UnitNames.Vh;
case Unit.Vmin:
return UnitNames.Vmin;
case Unit.Vmax:
return UnitNames.Vmax;
case Unit.Percent:
return UnitNames.Percent;
default:
return string.Empty;
}
}
}
public Length(float value, Unit unit)
{
_value = value;
_unit = unit;
}
public static bool operator >=(Length a, Length b)
{
int num = a.CompareTo(b);
if (num != 0)
return num == 1;
return true;
}
public static bool operator >(Length a, Length b)
{
return a.CompareTo(b) == 1;
}
public static bool operator <=(Length a, Length b)
{
int num = a.CompareTo(b);
if (num != 0)
return num == -1;
return true;
}
public static bool operator <(Length a, Length b)
{
return a.CompareTo(b) == -1;
}
public int CompareTo(Length other)
{
float num;
if (_unit == other._unit) {
num = _value;
return num.CompareTo(other._value);
}
if (IsAbsolute && other.IsAbsolute) {
num = ToPixel();
return num.CompareTo(other.ToPixel());
}
return 0;
}
public static bool TryParse(string s, out Length result)
{
float result2 = 0;
Unit unit = GetUnit(s.CssUnit(out result2));
if (unit != 0) {
result = new Length(result2, unit);
return true;
}
if (result2 == 0) {
result = Zero;
return true;
}
result = default(Length);
return false;
}
public static Unit GetUnit(string s)
{
switch (s) {
case "ch":
return Unit.Ch;
case "cm":
return Unit.Cm;
case "em":
return Unit.Em;
case "ex":
return Unit.Ex;
case "in":
return Unit.In;
case "mm":
return Unit.Mm;
case "pc":
return Unit.Pc;
case "pt":
return Unit.Pt;
case "px":
return Unit.Px;
case "rem":
return Unit.Rem;
case "vh":
return Unit.Vh;
case "vmax":
return Unit.Vmax;
case "vmin":
return Unit.Vmin;
case "vw":
return Unit.Vw;
case "%":
return Unit.Percent;
default:
return Unit.None;
}
}
public float ToPixel()
{
switch (_unit) {
case Unit.In:
return _value * 96;
case Unit.Mm:
return _value * 5 * 96 / 127;
case Unit.Pc:
return _value * 12 * 96 / 72;
case Unit.Pt:
return _value * 96 / 72;
case Unit.Cm:
return _value * 50 * 96 / 127;
case Unit.Px:
return _value;
default:
throw new InvalidOperationException("A relative unit cannot be converted.");
}
}
public float To(Unit unit)
{
float num = ToPixel();
switch (unit) {
case Unit.In:
return num / 96;
case Unit.Mm:
return num * 127 / 480;
case Unit.Pc:
return num * 72 / 1152;
case Unit.Pt:
return num * 72 / 96;
case Unit.Cm:
return num * 127 / 4800;
case Unit.Px:
return num;
default:
throw new InvalidOperationException("An absolute unit cannot be converted to a relative one.");
}
}
public bool Equals(Length other)
{
if (_value == other._value)
return _unit == other._unit;
return false;
}
public static bool operator ==(Length a, Length b)
{
return a.Equals(b);
}
public static bool operator !=(Length a, Length b)
{
return !a.Equals(b);
}
public override bool Equals(object obj)
{
Length? nullable = obj as Length?;
if (nullable.HasValue)
return Equals(nullable.Value);
return false;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public override string ToString()
{
string str = (_value == 0) ? string.Empty : UnitString;
return _value.ToString() + str;
}
public string ToString(string format, IFormatProvider formatProvider)
{
string str = (_value == 0) ? string.Empty : UnitString;
return _value.ToString(format, formatProvider) + str;
}
}
}