#region MigraDoc - Creating Documents on the Fly // // Authors: // Stefan Lange // Klaus Potzesny // David Stephensen // // Copyright (c) 2001-2017 empira Software GmbH, Cologne Area (Germany) // // http://www.pdfsharp.com // http://www.migradoc.com // http://sourceforge.net/projects/pdfsharp // // Permission is hereby granted, free of charge, to any person obtaining a // copy of this software and associated documentation files (the "Software"), // to deal in the Software without restriction, including without limitation // the rights to use, copy, modify, merge, publish, distribute, sublicense, // and/or sell copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included // in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. #endregion using System; using MigraDoc.DocumentObjectModel.publics; namespace MigraDoc.DocumentObjectModel.Shapes { /// /// Represents the left position in a shape. /// public struct LeftPosition : INullableValue { /// /// Initializes a new instance of the LeftPosition class from Unit. /// private LeftPosition(Unit value) { _shapePosition = ShapePosition.Undefined; _position = value; _notNull = !value.IsNull; } /// /// Initializes a new instance of the LeftPosition class from ShapePosition. /// private LeftPosition(ShapePosition value) { if (!(value == ShapePosition.Undefined || IsValid(value))) throw new ArgumentException(DomSR.InvalidEnumForLeftPosition); _shapePosition = value; _position = Unit.NullValue; _notNull = (value != ShapePosition.Undefined); } /// /// Sets shapeposition enum and resets position. /// private void SetFromEnum(ShapePosition shapePosition) { if (!IsValid(shapePosition)) throw new ArgumentException(DomSR.InvalidEnumForLeftPosition); _shapePosition = shapePosition; _position = Unit.NullValue; } /// /// Sets the Position from a Unit. /// private void SetFromUnit(Unit unit) { _shapePosition = ShapePosition.Undefined; _position = unit; } /// /// Sets the Position from an object. /// void INullableValue.SetValue(object value) { //REVIEW: Same code in TopPosition/LeftPosition. if (value == null) throw new ArgumentNullException("value"); if (value is ShapePosition) SetFromEnum((ShapePosition)value); else if (value is string && Enum.IsDefined(typeof(ShapePosition), value)) SetFromEnum((ShapePosition)Enum.Parse(typeof(ShapePosition), (string)value, true)); else SetFromUnit(value.ToString()); _notNull = true; } /// /// Gets the value of the position. /// object INullableValue.GetValue() { if (_shapePosition == ShapePosition.Undefined) return _position; return _shapePosition; } /// /// Resets this instance, i.e. IsNull() will return true afterwards. /// void INullableValue.SetNull() { this = new LeftPosition(); } /// /// Determines whether this instance is null (not set). /// bool INullableValue.IsNull { get { return !_notNull; } } private bool _notNull; /// /// Gets the value of the position in unit. /// public Unit Position { get { return _position; } } public Unit _position; /// /// Gets the value of the position. /// public ShapePosition ShapePosition { get { return _shapePosition; } } public ShapePosition _shapePosition; /// /// Indicates the given shapePosition is valid for LeftPosition. /// private static bool IsValid(ShapePosition shapePosition) { return shapePosition == ShapePosition.Left || shapePosition == ShapePosition.Center || shapePosition == ShapePosition.Right || shapePosition == ShapePosition.Inside || shapePosition == ShapePosition.Outside; } /// /// Converts a ShapePosition to a LeftPosition. /// public static implicit operator LeftPosition(ShapePosition value) { return new LeftPosition(value); } /// /// Converts a Unit to a LeftPosition. /// public static implicit operator LeftPosition(Unit value) { return new LeftPosition(value); } /// /// Converts a string to a LeftPosition. /// The string is interpreted as a Unit. /// public static implicit operator LeftPosition(string value) { Unit unit = value; return new LeftPosition(unit); } /// /// Converts a double to a LeftPosition. /// The double is interpreted as a Unit in Point. /// public static implicit operator LeftPosition(double value) { Unit unit = value; return new LeftPosition(unit); } /// /// Converts an integer to a LeftPosition. /// The integer is interpreted as a Unit in Point. /// public static implicit operator LeftPosition(int value) { Unit unit = value; return new LeftPosition(unit); } /// /// Parses the specified value. /// public static LeftPosition Parse(string value) { if (String.IsNullOrEmpty(value)) throw new ArgumentNullException("value"); value = value.Trim(); char ch = value[0]; if (ch == '+' || ch == '-' || Char.IsNumber(ch)) return Unit.Parse(value); return (ShapePosition)Enum.Parse(typeof(ShapePosition), value, true); } #region public /// /// Converts LeftPosition into DDL. /// public void Serialize(Serializer serializer) { if (_shapePosition == ShapePosition.Undefined) serializer.WriteSimpleAttribute("Left", Position); else serializer.WriteSimpleAttribute("Left", ShapePosition); } #endregion /// /// Returns the uninitialized LeftPosition object. /// public static readonly LeftPosition NullValue = new LeftPosition(); } }