Merge pull request #267 from zii-dmg/CairoPathData

Added Cairo.Path properties
This commit is contained in:
lytico 2021-07-19 15:19:03 +02:00 committed by GitHub
commit b3fd3f91bf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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
}
}