Skip to content

Commit

Permalink
Generic fix for #82 and #84, fixed problems with all overloads of Lib…
Browse files Browse the repository at this point in the history
…rary.SetProperty and Library.GetProperty in the process
  • Loading branch information
Robmaister committed Dec 8, 2016
1 parent f1d4a69 commit 3c51037
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Source/SharpFontShared/FT.Internal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ public static partial class FT
internal static extern Error FT_Property_Set(IntPtr library, string module_name, string property_name, IntPtr value);

[DllImport(FreetypeDll, CallingConvention = CallConvention, CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)]
internal static extern Error FT_Property_Get(IntPtr library, string module_name, string property_name, out IntPtr value);
internal static extern Error FT_Property_Get(IntPtr library, string module_name, string property_name, IntPtr value);

[DllImport(FreetypeDll, CallingConvention = CallConvention)]
internal static extern Error FT_Reference_Library(IntPtr library);
Expand Down
4 changes: 2 additions & 2 deletions Source/SharpFontShared/FTStream.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region MIT License
/*Copyright (c) 2012-2013 Robert Rouhani <[email protected]>
/*Copyright (c) 2012-2013, 2016 Robert Rouhani <[email protected]>
SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
Expand Down Expand Up @@ -55,7 +55,7 @@ namespace SharpFont
/// <summary>
/// A handle to an input stream.
/// </summary>
public sealed class FTStream: NativeObject
public sealed class FTStream : NativeObject
{
#region Fields

Expand Down
2 changes: 1 addition & 1 deletion Source/SharpFontShared/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public Face(Library library, string path, int faceIndex)
/// <param name="library">The parent library.</param>
/// <param name="file">The loaded file.</param>
/// <param name="faceIndex">The index of the face to take from the file.</param>
public unsafe Face(Library library, byte[] file, int faceIndex)
public Face(Library library, byte[] file, int faceIndex)
: this(library)
{
IntPtr reference;
Expand Down
133 changes: 75 additions & 58 deletions Source/SharpFontShared/Library.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#region MIT License
/*Copyright (c) 2012-2014 Robert Rouhani <[email protected]>
/*Copyright (c) 2012-2014, 2016 Robert Rouhani <[email protected]>
SharpFont based on Tao.FreeType, Copyright (c) 2003-2007 Tao Framework Team
Expand Down Expand Up @@ -447,21 +447,18 @@ public void PropertySet(string moduleName, string propertyName, IntPtr value)
/// <param name="value">A generic pointer to a variable or structure which gives the new value of the property.
/// The exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public unsafe void PropertySet<T>(string moduleName, string propertyName, T value)
public void PropertySet<T>(string moduleName, string propertyName, ref T value)
where T : struct
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

IntPtr ptr = IntPtr.Zero;
Marshal.StructureToPtr((object)value, ptr, false); //Should that last value be false? Any other way to get a pointer?

Error err = FT.FT_Property_Set(Reference, moduleName, propertyName, ptr);
GCHandle gch = GCHandle.Alloc(value, GCHandleType.Pinned);
PropertySet(moduleName, propertyName, gch.AddrOfPinnedObject());
gch.Free();
}

/// <summary>
/// Set a property for a given module.
/// </summary>
/// <typeparam name="T">The type of property to set.</typeparam>
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName"><para>The property name. Properties are described in the ‘Synopsis’ subsection
/// of the module's documentation.
Expand All @@ -470,16 +467,10 @@ public unsafe void PropertySet<T>(string moduleName, string propertyName, T valu
/// <param name="value">A generic pointer to a variable or structure which gives the new value of the property.
/// The exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public unsafe void PropertySet(string moduleName, string propertyName, GlyphToScriptMapProperty value)
public void PropertySet<T>(string moduleName, string propertyName, T value)
where T : struct
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

GlyphToScriptMapPropertyRec rec = value.Rec;
Error err = FT.FT_Property_Set(Reference, moduleName, propertyName, (IntPtr)(&rec));

if (err != Error.Ok)
throw new FreeTypeException(err);
PropertySet(moduleName, propertyName, ref value);
}

/// <summary>
Expand All @@ -493,16 +484,27 @@ public unsafe void PropertySet(string moduleName, string propertyName, GlyphToSc
/// <param name="value">A generic pointer to a variable or structure which gives the new value of the property.
/// The exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public unsafe void PropertySet(string moduleName, string propertyName, IncreaseXHeightProperty value)
public void PropertySet(string moduleName, string propertyName, GlyphToScriptMapProperty value)
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

IncreaseXHeightPropertyRec rec = value.Rec;
Error err = FT.FT_Property_Set(Reference, moduleName, propertyName, (IntPtr)(&rec));
var rec = value.Rec;
PropertySet(moduleName, propertyName, ref rec);
}

if (err != Error.Ok)
throw new FreeTypeException(err);
/// <summary>
/// Set a property for a given module.
/// </summary>
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName"><para>The property name. Properties are described in the ‘Synopsis’ subsection
/// of the module's documentation.
/// </para><para>
/// Note that only a few modules have properties.</para></param>
/// <param name="value">A generic pointer to a variable or structure which gives the new value of the property.
/// The exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public void PropertySet(string moduleName, string propertyName, IncreaseXHeightProperty value)
{
var rec = value.Rec;
PropertySet(moduleName, propertyName, ref rec);
}

/// <summary>
Expand All @@ -514,12 +516,12 @@ public unsafe void PropertySet(string moduleName, string propertyName, IncreaseX
/// <param name="value">A generic pointer to a variable or structure which gives the value of the property. The
/// exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public void PropertyGet(string moduleName, string propertyName, out IntPtr value)
public void PropertyGet(string moduleName, string propertyName, IntPtr value)
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

Error err = FT.FT_Property_Get(Reference, moduleName, propertyName, out value);
Error err = FT.FT_Property_Get(Reference, moduleName, propertyName, value);

if (err != Error.Ok)
throw new FreeTypeException(err);
Expand All @@ -532,21 +534,16 @@ public void PropertyGet(string moduleName, string propertyName, out IntPtr value
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName">The property name. Properties are described in the ‘Synopsis’ subsection of the
/// module's documentation.</param>
/// <param name="value">A generic pointer to a variable or structure which gives the value of the property. The
/// exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
/// <param name="value">The value read from the module.</param>
public void PropertyGet<T>(string moduleName, string propertyName, out T value)
where T : struct
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");
value = default(T);

IntPtr ptr;
Error err = FT.FT_Property_Get(Reference, moduleName, propertyName, out ptr);

if (err != Error.Ok)
throw new FreeTypeException(err);

value = PInvokeHelper.PtrToStructure<T>(ptr);
GCHandle gch = GCHandle.Alloc(value, GCHandleType.Pinned);
PropertyGet(moduleName, propertyName, gch.AddrOfPinnedObject());
value = PInvokeHelper.PtrToStructure<T>(gch.AddrOfPinnedObject());
gch.Free();
}

/// <summary>
Expand All @@ -555,42 +552,62 @@ public void PropertyGet<T>(string moduleName, string propertyName, out T value)
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName">The property name. Properties are described in the ‘Synopsis’ subsection of the
/// module's documentation.</param>
/// <param name="value">A generic pointer to a variable or structure which gives the value of the property. The
/// exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
/// <param name="value">The value read from the module.</param>
[Obsolete("Use PropertyGetGlyphToScriptMap instead")]
public void PropertyGet(string moduleName, string propertyName, out GlyphToScriptMapProperty value)
{
value = PropertyGetGlyphToScriptMap(moduleName, propertyName);
}

/// <summary>
/// Get a module's property value.
/// </summary>
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName">The property name. Properties are described in the ‘Synopsis’ subsection of the
/// module's documentation.</param>
/// <param name="value">The value read from the module.</param>
[Obsolete("Use PropertyGetIncreaseXHeight instead")]
public void PropertyGet(string moduleName, string propertyName, out IncreaseXHeightProperty value)
{
value = PropertyGetIncreaseXHeight(moduleName, propertyName);
}

/// <summary>
/// Gets a module's property value of the type <see cref="GlyphToScriptMapProperty"/>.
/// </summary>
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName">The property name. Properties are described in the ‘Synopsis’ subsection of the
/// module's documentation.</param>
/// <returns>The value read from the module.</returns>
public GlyphToScriptMapProperty PropertyGetGlyphToScriptMap(string moduleName, string propertyName)
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

IntPtr ptr;
Error err = FT.FT_Property_Get(Reference, moduleName, propertyName, out ptr);
GlyphToScriptMapPropertyRec rec;
PropertyGet(moduleName, propertyName, out rec);

GlyphToScriptMapPropertyRec ptrRec = PInvokeHelper.PtrToStructure<GlyphToScriptMapPropertyRec>(ptr);
Face face = childFaces.Find(f => f.Reference == ptrRec.face);
value = new GlyphToScriptMapProperty(ptrRec, face);
Face face = childFaces.Find(f => f.Reference == rec.face);
return new GlyphToScriptMapProperty(rec, face);
}

/// <summary>
/// Get a module's property value.
/// Gets a module's property value of the type <see cref="IncreaseXHeightProperty"/>.
/// </summary>
/// <param name="moduleName">The module name.</param>
/// <param name="propertyName">The property name. Properties are described in the ‘Synopsis’ subsection of the
/// module's documentation.</param>
/// <param name="value">A generic pointer to a variable or structure which gives the value of the property. The
/// exact definition of ‘value’ is dependent on the property; see the ‘Synopsis’ subsection of the module's
/// documentation.</param>
public void PropertyGet(string moduleName, string propertyName, out IncreaseXHeightProperty value)
/// <returns>The value read from the module.</returns>
public IncreaseXHeightProperty PropertyGetIncreaseXHeight(string moduleName, string propertyName)
{
if (disposed)
throw new ObjectDisposedException("Library", "Cannot access a disposed object.");

IntPtr ptr;
Error err = FT.FT_Property_Get(Reference, moduleName, propertyName, out ptr);
IncreaseXHeightPropertyRec rec;
PropertyGet(moduleName, propertyName, out rec);

IncreaseXHeightPropertyRec ptrRec = PInvokeHelper.PtrToStructure<IncreaseXHeightPropertyRec>(ptr);
Face face = childFaces.Find(f => f.Reference == ptrRec.face);
value = new IncreaseXHeightProperty(ptrRec, face);
Face face = childFaces.Find(f => f.Reference == rec.face);
return new IncreaseXHeightProperty(rec, face);
}

/// <summary>
Expand Down

0 comments on commit 3c51037

Please sign in to comment.