Added Cairo.Path properties

New properties on Path is lazy evaluated. Only reading is supported.
This commit is contained in:
dmg 2021-07-17 17:05:21 +03:00
parent ad0420b118
commit fbe180d742
3 changed files with 103 additions and 0 deletions

View file

@ -36,7 +36,17 @@ namespace Cairo {
public class Path : IDisposable
{
[StructLayout(LayoutKind.Sequential)]
struct PathStruct
{
public Status Status;
public IntPtr Data;
public int NumData;
}
IntPtr handle = IntPtr.Zero;
Status status;
PathData[] data;
internal Path (IntPtr handle)
{
@ -72,5 +82,57 @@ namespace Cairo {
NativeMethods.cairo_path_destroy (handle);
handle = IntPtr.Zero;
}
void CheckDisposed ()
{
if (handle == IntPtr.Zero)
throw new ObjectDisposedException ("Object has already been disposed");
}
void MarshalData ()
{
if (data == null)
{
var rawStruct = Marshal.PtrToStructure<PathStruct>(handle);
status = rawStruct.Status;
data = new PathData[rawStruct.NumData];
int oneDataSize = Marshal.SizeOf<PathData>();
for (int i = 0; i < rawStruct.NumData; i++)
{
IntPtr iPtr = new IntPtr(rawStruct.Data.ToInt64() + i * oneDataSize);
data[i] = Marshal.PtrToStructure<PathData>(iPtr);
}
}
}
public Status Status
{
get
{
CheckDisposed ();
MarshalData ();
return status;
}
}
public int NumData
{
get
{
CheckDisposed ();
MarshalData ();
return data.Length;
}
}
public PathData[] Data
{
get
{
CheckDisposed ();
MarshalData ();
return data;
}
}
}
}

View file

@ -0,0 +1,28 @@
using System;
using System.Runtime.InteropServices;
namespace Cairo
{
[StructLayout(LayoutKind.Explicit)]
public struct PathData
{
[FieldOffset(0)]
public PathDataHeader Header;
[FieldOffset(0)]
public PathDataPoint Point;
}
[StructLayout(LayoutKind.Sequential)]
public struct PathDataHeader
{
public PathDataType Type;
public int Length;
}
[StructLayout(LayoutKind.Sequential)]
public struct PathDataPoint
{
public double X;
public double Y;
}
}

View file

@ -0,0 +1,13 @@
using System;
namespace Cairo
{
[Serializable]
public enum PathDataType
{
MoveTo,
LineTo,
CurveTo,
ClosePath
}
}