#region MIT License /*Copyright (c) 2012-2013, 2015 Robert Rouhani SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team 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 System.Runtime.InteropServices; namespace SharpFont { /// /// Represents a fixed-point decimal value with 16 bits of decimal precision. /// [StructLayout(LayoutKind.Sequential, Pack = 1)] public struct Fixed16Dot16 : IEquatable, IComparable { #region Fields /// /// The angle pi expressed in FT_Angle units. /// public static readonly Fixed16Dot16 AnglePI = new Fixed16Dot16(180); /// /// The angle 2*pi expressed in FT_Angle units. /// public static readonly Fixed16Dot16 Angle2PI = new Fixed16Dot16(360); /// /// The angle pi/2 expressed in FT_Angle units. /// public static readonly Fixed16Dot16 AnglePI2 = new Fixed16Dot16(90); /// /// The angle pi/4 expressed in FT_Angle units. /// public static readonly Fixed16Dot16 AnglePI4 = new Fixed16Dot16(45); /// /// The raw 16.16 integer. /// private int value; #endregion #region Constructors /// /// Initializes a new instance of the struct. /// /// An integer value. public Fixed16Dot16(int value) { this.value = value << 16; } /// /// Initializes a new instance of the struct. /// /// A floating point value. public Fixed16Dot16(float value) { this.value = (int)(value * 65536); } /// /// Initializes a new instance of the struct. /// /// A floating point value. public Fixed16Dot16(double value) { this.value = (int)(value * 65536); } /// /// Initializes a new instance of the struct. /// /// A floating point value. public Fixed16Dot16(decimal value) { this.value = (int)(value * 65536); } #endregion #region Properties /// /// Gets the raw 16.16 integer. /// public int Value { get { return value; } } #endregion #region Methods #region Static /// /// Creates a from an int containing a 16.16 value. /// /// A 16.16 value. /// An instance of . public static Fixed16Dot16 FromRawValue(int value) { Fixed16Dot16 f = new Fixed16Dot16(); f.value = value; return f; } /// /// Creates a new from a /// /// A value. /// The equivalent value. public static Fixed16Dot16 FromInt32(int value) { return new Fixed16Dot16(value); } /// /// Creates a new from . /// /// A floating-point value. /// A fixed 16.16 value. public static Fixed16Dot16 FromSingle(float value) { return new Fixed16Dot16(value); } /// /// Creates a new from a . /// /// A floating-point value. /// A fixed 16.16 value. public static Fixed16Dot16 FromDouble(double value) { return new Fixed16Dot16(value); } /// /// Creates a new from a . /// /// A floating-point value. /// A fixed 16.16 value. public static Fixed16Dot16 FromDecimal(decimal value) { return new Fixed16Dot16(value); } /// /// Adds two 16.16 values together. /// /// The left operand. /// The right operand. /// The result of the addition. public static Fixed16Dot16 Add(Fixed16Dot16 left, Fixed16Dot16 right) { return Fixed16Dot16.FromRawValue(left.value + right.value); } /// /// Subtacts one 16.16 values from another. /// /// The left operand. /// The right operand. /// The result of the subtraction. public static Fixed16Dot16 Subtract(Fixed16Dot16 left, Fixed16Dot16 right) { return Fixed16Dot16.FromRawValue(left.value - right.value); } /// /// Multiplies two 16.16 values together. /// /// The left operand. /// The right operand. /// The result of the multiplication. public static Fixed16Dot16 Multiply(Fixed16Dot16 left, Fixed16Dot16 right) { long mul = (long)left.value * (long)right.value; Fixed16Dot16 ans = new Fixed16Dot16(); ans.value = (int)(mul >> 16); return ans; } /// /// A very simple function used to perform the computation ‘(a*b)/0x10000’ with maximal accuracy. Most of the /// time this is used to multiply a given value by a 16.16 fixed float factor. /// /// /// NOTE: This is a native FreeType function. /// /// This function has been optimized for the case where the absolute value of ‘a’ is less than 2048, and ‘b’ is /// a 16.16 scaling factor. As this happens mainly when scaling from notional units to fractional pixels in /// FreeType, it resulted in noticeable speed improvements between versions 2.x and 1.x. /// /// As a conclusion, always try to place a 16.16 factor as the second argument of this function; this can make /// a great difference. /// /// The first multiplier. /// The second multiplier. Use a 16.16 factor here whenever possible (see note below). /// The result of ‘(a*b)/0x10000’. public static Fixed16Dot16 MultiplyFix(int a, Fixed16Dot16 b) { return Fixed16Dot16.FromRawValue((int)FT.FT_MulFix((IntPtr)a, (IntPtr)b.Value)); } /// /// Divides one 16.16 values from another. /// /// The left operand. /// The right operand. /// The result of the division. public static Fixed16Dot16 Divide(Fixed16Dot16 left, Fixed16Dot16 right) { long div = ((long)left.Value << 16) / right.value; Fixed16Dot16 ans = new Fixed16Dot16(); ans.value = (int)div; return ans; } /// /// A very simple function used to perform the computation ‘(a*0x10000)/b’ with maximal accuracy. Most of the /// time, this is used to divide a given value by a 16.16 fixed float factor. /// /// /// NOTE: This is a native FreeType function. /// /// The optimization for is simple: If (a << 16) fits in 32 bits, then the division /// is computed directly. Otherwise, we use a specialized version of . /// /// The first multiplier. /// The second multiplier. Use a 16.16 factor here whenever possible (see note below). /// The result of ‘(a*0x10000)/b’. public static Fixed16Dot16 DivideFix(int a, Fixed16Dot16 b) { return Fixed16Dot16.FromRawValue((int)FT.FT_DivFix((IntPtr)a, (IntPtr)b.Value)); } /// /// A very simple function used to perform the computation ‘(a*b)/c’ with maximal accuracy (it uses a 64-bit /// intermediate integer whenever necessary). /// /// This function isn't necessarily as fast as some processor specific operations, but is at least completely /// portable. /// /// This is a native FreeType function. /// The first multiplier. /// The second multiplier. /// The divisor. /// /// The result of ‘(a*b)/c’. This function never traps when trying to divide by zero; it simply returns /// ‘MaxInt’ or ‘MinInt’ depending on the signs of ‘a’ and ‘b’. /// public static Fixed16Dot16 MultiplyDivide(Fixed16Dot16 a, Fixed16Dot16 b, Fixed16Dot16 c) { return Fixed16Dot16.FromRawValue((int)FT.FT_MulDiv((IntPtr)a.Value, (IntPtr)b.Value, (IntPtr)c.Value)); } /// /// Return the arc-tangent corresponding to a given vector (x,y) in the 2d plane. /// /// This is a native FreeType function. /// The horizontal vector coordinate. /// The vertical vector coordinate. /// The arc-tangent value (i.e. angle). public static Fixed16Dot16 Atan2(Fixed16Dot16 x, Fixed16Dot16 y) { return Fixed16Dot16.FromRawValue((int)FT.FT_Atan2((IntPtr)x.Value, (IntPtr)y.Value)); } /// /// Return the difference between two angles. The result is always constrained to the [-PI..PI] interval. /// /// This is a native FreeType function. /// First angle. /// Second angle. /// Constrained value of ‘value2-value1’. public static Fixed16Dot16 AngleDiff(Fixed16Dot16 angle1, Fixed16Dot16 angle2) { return Fixed16Dot16.FromRawValue((int)FT.FT_Angle_Diff((IntPtr)angle1.Value, (IntPtr)angle2.Value)); } #endregion #region Operators /// /// Casts a to a . /// /// A value. /// The equivalent value. public static implicit operator Fixed16Dot16(short value) { return new Fixed16Dot16(value); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static explicit operator Fixed16Dot16(int value) { return new Fixed16Dot16(value); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static explicit operator Fixed16Dot16(float value) { return new Fixed16Dot16(value); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static explicit operator Fixed16Dot16(double value) { return new Fixed16Dot16(value); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static explicit operator Fixed16Dot16(decimal value) { return new Fixed16Dot16(value); } /// /// Casts a to a . /// /// /// This operation can result in a loss of data. /// /// A value. /// The equivalent value. public static explicit operator int(Fixed16Dot16 value) { return value.ToInt32(); } /// /// Casts a to a . /// /// /// This operation can result in a loss of data. /// /// A value. /// The equivalent value. public static explicit operator float(Fixed16Dot16 value) { return value.ToSingle(); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static implicit operator double(Fixed16Dot16 value) { return value.ToDouble(); } /// /// Casts a to a . /// /// A value. /// The equivalent value. public static implicit operator decimal(Fixed16Dot16 value) { return value.ToDecimal(); } /// /// Adds two 16.16 values together. /// /// The left operand. /// The right operand. /// The result of the addition. public static Fixed16Dot16 operator +(Fixed16Dot16 left, Fixed16Dot16 right) { return Add(left, right); } /// /// Subtacts one 16.16 values from another. /// /// The left operand. /// The right operand. /// The result of the subtraction. public static Fixed16Dot16 operator -(Fixed16Dot16 left, Fixed16Dot16 right) { return Subtract(left, right); } /// /// Multiplies two 16.16 values together. /// /// The left operand. /// The right operand. /// The result of the multiplication. public static Fixed16Dot16 operator *(Fixed16Dot16 left, Fixed16Dot16 right) { return Multiply(left, right); } /// /// Divides one 16.16 values from another. /// /// The left operand. /// The right operand. /// The result of the division. public static Fixed16Dot16 operator /(Fixed16Dot16 left, Fixed16Dot16 right) { return Divide(left, right); } /// /// Compares two instances of for equality. /// /// The left operand. /// The right operand. /// A value indicating whether the two instances are equal. public static bool operator ==(Fixed16Dot16 left, Fixed16Dot16 right) { return left.Equals(right); } /// /// Compares two instances of for inequality. /// /// The left operand. /// The right operand. /// A value indicating whether the two instances are not equal. public static bool operator !=(Fixed16Dot16 left, Fixed16Dot16 right) { return !(left == right); } /// /// Checks if the left operand is less than the right operand. /// /// The left operand. /// The right operand. /// A value indicating whether left is less than right. public static bool operator <(Fixed16Dot16 left, Fixed16Dot16 right) { return left.CompareTo(right) < 0; } /// /// Checks if the left operand is less than or equal to the right operand. /// /// The left operand. /// The right operand. /// A value indicating whether left is less than or equal to right. public static bool operator <=(Fixed16Dot16 left, Fixed16Dot16 right) { return left.CompareTo(right) <= 0; } /// /// Checks if the left operand is greater than the right operand. /// /// The left operand. /// The right operand. /// A value indicating whether left is greater than right. public static bool operator >(Fixed16Dot16 left, Fixed16Dot16 right) { return left.CompareTo(right) > 0; } /// /// Checks if the left operand is greater than or equal to the right operand. /// /// The left operand. /// The right operand. /// A value indicating whether left is greater than or equal to right. public static bool operator >=(Fixed16Dot16 left, Fixed16Dot16 right) { return left.CompareTo(right) >= 0; } #endregion #region Instance /// /// Removes the decimal part of the value. /// /// The truncated number. public int Floor() { return value >> 16; } /// /// A very simple function used to compute the floor function of a 16.16 fixed number. /// /// This is a native FreeType function. /// The result of ‘a & -0x10000’. public Fixed16Dot16 FloorFix() { //TODO does the P/Invoke overhead make this slower than re-implementing in C#? Test it return FromRawValue((int)FT.FT_FloorFix((IntPtr)this.Value)); } /// /// Rounds to the nearest whole number. /// /// The nearest whole number. public int Round() { //add 2^15, rounds the integer part up if the decimal value is >= 0.5 return (value + 32768) >> 16; } /// /// A very simple function used to round a 16.16 fixed number. /// /// This is a native FreeType function. /// The result of ‘(a + 0x8000) & -0x10000’. public Fixed16Dot16 RoundFix() { return FromRawValue((int)FT.FT_RoundFix((IntPtr)this.Value)); } /// /// Rounds up to the next whole number. /// /// The next whole number. public int Ceiling() { //add 2^16 - 1, rounds the integer part up if there's any decimal value return (value + 65535) >> 16; } /// /// A very simple function used to compute the ceiling function of a 16.16 fixed number. /// /// This is a native FreeType function. /// The result of ‘(a + 0x10000 - 1) & -0x10000’. public Fixed16Dot16 CeilingFix() { return FromRawValue((int)FT.FT_CeilFix((IntPtr)this.Value)); } /// /// Return the sinus of a given angle in fixed point format. /// /// /// NOTE: This is a native FreeType function. /// /// If you need both the sinus and cosinus for a given angle, use the function . /// /// The sinus value. public Fixed16Dot16 Sin() { return Fixed16Dot16.FromRawValue((int)FT.FT_Sin((IntPtr)this.Value)); } /// /// Return the cosinus of a given angle in fixed point format. /// /// /// NOTE: This is a native FreeType function. /// /// If you need both the sinus and cosinus for a given angle, use the function . /// /// The cosinus value. public Fixed16Dot16 Cos() { return Fixed16Dot16.FromRawValue((int)FT.FT_Cos((IntPtr)this.Value)); } /// /// Return the tangent of a given angle in fixed point format. /// /// This is a native FreeType function. /// The tangent value. public Fixed16Dot16 Tan() { return Fixed16Dot16.FromRawValue((int)FT.FT_Tan((IntPtr)this.Value)); } /// /// Converts the value to a . The value is floored. /// /// An integer value. public int ToInt32() { return Floor(); } /// /// Converts the value to a . /// /// A floating-point value. public float ToSingle() { return value / 65536f; } /// /// Converts the value to a . /// /// A floating-point value. public double ToDouble() { return value / 65536d; } /// /// Converts the value to a . /// /// A decimal value. public decimal ToDecimal() { return value / 65536m; } /// /// Compares this instance to another for equality. /// /// A . /// A value indicating whether the two instances are equal. public bool Equals(Fixed16Dot16 other) { return value == other.value; } /// /// Compares this instnace with another and returns an integer that indicates /// whether the current instance precedes, follows, or occurs in the same position in the sort order as the /// other . /// /// A . /// A value indicating the relative order of the instances. public int CompareTo(Fixed16Dot16 other) { return value.CompareTo(other.value); } #endregion #region Overrides /// /// Returns a string that represents the current object. /// /// An object that supplies culture-specific formatting information. /// A string that represents the current object. public string ToString(IFormatProvider provider) { return ToDecimal().ToString(provider); } /// /// Returns a string that represents the current object. /// /// A numeric format string. /// A string that represents the current object. public string ToString(string format) { return ToDecimal().ToString(format); } /// /// Returns a string that represents the current object. /// /// A numeric format string. /// An object that supplies culture-specific formatting information. /// A string that represents the current object. public string ToString(string format, IFormatProvider provider) { return ToDecimal().ToString(format, provider); } /// /// Returns a string that represents the current object. /// /// A string that represents the current object. public override string ToString() { return ToDecimal().ToString(); } /// /// Calculates a hash code for the current object. /// /// A hash code for the current object. public override int GetHashCode() { return value.GetHashCode(); } /// /// Determines whether the specified object isequal to the current object. /// /// The object to compare with the current object. /// A value indicating equality between the two objects. public override bool Equals(object obj) { if (obj is Fixed16Dot16) return this.Equals((Fixed16Dot16)obj); else if (obj is int) return value == ((Fixed16Dot16)obj).value; else return false; } #endregion #endregion } }