#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 top position in a shape. /// public struct TopPosition : INullableValue { /// /// Initializes a new instance of TopPosition from Unit. /// private TopPosition(Unit value) { _shapePosition = ShapePosition.Undefined; _position = value; _notNull = !value.IsNull; } /// /// Initializes a new instance of TopPosition from ShapePosition. /// private TopPosition(ShapePosition value) { if (!(IsValid(value) || value == ShapePosition.Undefined)) throw new ArgumentException(DomSR.InvalidEnumForTopPosition); _shapePosition = value; _position = Unit.NullValue; _notNull = (value != ShapePosition.Undefined); } /// /// Indicates the given shapePosition is valid for TopPosition. /// private static bool IsValid(ShapePosition shapePosition) { return shapePosition == ShapePosition.Bottom || shapePosition == ShapePosition.Top || shapePosition == ShapePosition.Center; } /// /// Converts a ShapePosition to a TopPosition. /// public static implicit operator TopPosition(ShapePosition value) { return new TopPosition(value); } /// /// Converts a Unit to a TopPosition. /// public static implicit operator TopPosition(Unit val) { return new TopPosition(val); } /// /// Converts a string to a TopPosition. /// The string is interpreted as a Unit. /// public static implicit operator TopPosition(string value) { Unit unit = value; return new TopPosition(unit); } /// /// Converts a double to a TopPosition. /// The double is interpreted as a Unit in Point. /// public static implicit operator TopPosition(double value) { Unit unit = value; return new TopPosition(unit); } /// /// Converts an integer to a TopPosition. /// The integer is interpreted as a Unit in Point. /// public static implicit operator TopPosition(int value) { Unit unit = value; return new TopPosition(unit); } /// /// Sets shapeposition enum and resets position. /// private void SetFromEnum(ShapePosition shapePosition) { if (!IsValid(shapePosition)) throw new ArgumentException(DomSR.InvalidEnumForTopPosition); _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) { 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 Position as Unit or ShapePosition. /// 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 TopPosition(); } /// /// Determines whether this instance is null (not set). /// bool INullableValue.IsNull { get { return !_notNull; } } 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; /// /// Parses the specified value. /// public static TopPosition 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 TopPosition into DDL. /// public void Serialize(Serializer serializer) { if (_shapePosition == ShapePosition.Undefined) serializer.WriteSimpleAttribute("Top", Position); else serializer.WriteSimpleAttribute("Top", ShapePosition); } #endregion /// /// Represents the unitialized TopPosition object. /// public static readonly TopPosition NullValue = new TopPosition(); } }