#region MIT License /*Copyright (c) 2012-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; using SharpFont.Internal; namespace SharpFont { /// /// A simple structure used to store a 2x2 matrix. Coefficients are in 16.16 fixed float format. The computation /// performed is: /// /// x' = x*xx + y*xy /// y' = x*yx + y*yy /// /// [StructLayout(LayoutKind.Sequential)] public struct FTMatrix : IEquatable { #region Fields private IntPtr xx, xy; private IntPtr yx, yy; #endregion #region Constructors /// /// Initializes a new instance of the struct. /// /// Matrix coefficient XX. /// Matrix coefficient XY. /// Matrix coefficient YX. /// Matrix coefficient YY. public FTMatrix(int xx, int xy, int yx, int yy) : this() { this.xx = (IntPtr)xx; this.xy = (IntPtr)xy; this.yx = (IntPtr)yx; this.yy = (IntPtr)yy; } /// /// Initializes a new instance of the struct. /// /// Matrix coefficients XX, XY. /// Matrix coefficients YX, YY. public FTMatrix(FTVector row0, FTVector row1) : this(row0.X.Value, row0.Y.Value, row1.X.Value, row1.Y.Value) { } #endregion #region Properties /// /// Gets or sets the matrix coefficient. /// public Fixed16Dot16 XX { get { return Fixed16Dot16.FromRawValue((int)xx); } set { xx = (IntPtr)value.Value; } } /// /// Gets or sets the matrix coefficient. /// public Fixed16Dot16 XY { get { return Fixed16Dot16.FromRawValue((int)xy); } set { xy = (IntPtr)value.Value; } } /// /// Gets or sets the matrix coefficient. /// public Fixed16Dot16 YX { get { return Fixed16Dot16.FromRawValue((int)yx); } set { yx = (IntPtr)value.Value; } } /// /// Gets or sets the matrix coefficient. /// public Fixed16Dot16 YY { get { return Fixed16Dot16.FromRawValue((int)yy); } set { yy = (IntPtr)value.Value; } } #endregion #region Operators /// /// Compares two instances of for equality. /// /// A . /// Another . /// A value indicating equality. public static bool operator ==(FTMatrix left, FTMatrix right) { return left.Equals(right); } /// /// Compares two instances of for inequality. /// /// A . /// Another . /// A value indicating inequality. public static bool operator !=(FTMatrix left, FTMatrix right) { return !left.Equals(right); } #endregion #region Methods /// /// Perform the matrix operation ‘b = a*b’. /// /// /// The result is undefined if either ‘a’ or ‘b’ is zero. /// /// A pointer to matrix ‘a’. /// A pointer to matrix ‘b’. public static void Multiply(FTMatrix a, FTMatrix b) { FT.FT_Matrix_Multiply(ref a, ref b); } /// /// Perform the matrix operation ‘b = a*b’. /// /// /// The result is undefined if either ‘a’ or ‘b’ is zero. /// /// A pointer to matrix ‘b’. public void Multiply(FTMatrix b) { FT.FT_Matrix_Multiply(ref this, ref b); } /// /// Invert a 2x2 matrix. Return an error if it can't be inverted. /// public void Invert() { Error err = FT.FT_Matrix_Invert(ref this); if (err != Error.Ok) throw new FreeTypeException(err); } /// /// Compares this instance of to another for equality. /// /// A . /// A value indicating equality. public bool Equals(FTMatrix other) { return xx == other.xx && xy == other.xy && yx == other.yx && yy == other.yy; } /// /// Compares this instance of to an object for equality. /// /// An object. /// A value indicating equality. public override bool Equals(object obj) { if (obj is FTMatrix) return this.Equals((FTMatrix)obj); else return false; } /// /// Gets a unique hash code for this instance. /// /// A hash code. public override int GetHashCode() { return xx.GetHashCode() ^ xy.GetHashCode() ^ yx.GetHashCode() ^ yy.GetHashCode(); } #endregion } }