diff --git a/csharp/Cucul.cs b/csharp/Cucul.cs index 8a8c661..990ca6b 100644 --- a/csharp/Cucul.cs +++ b/csharp/Cucul.cs @@ -717,5 +717,93 @@ namespace Cucul /* */ } + + public class CuculFont : IDisposable + { + private IntPtr _font; + private GCHandle _gch; + + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern IntPtr cucul_load_font(IntPtr data, int len); + public CuculFont(string s) + { + IntPtr name = Marshal.StringToHGlobalAnsi(s); + _font = cucul_load_font(name, 0); + Marshal.FreeHGlobal(name); + } + + public CuculFont(byte[] buf) + { + GCHandle _gch = GCHandle.Alloc(buf, GCHandleType.Pinned); + _font = cucul_load_font(_gch.AddrOfPinnedObject(), buf.Length); + } + + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern int cucul_free_font(IntPtr d); + public void Dispose() + { + cucul_free_font(_font); + _gch.Free(); + GC.SuppressFinalize(this); + } + + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern IntPtr cucul_get_font_list(); + public static string[] getList() + { + IntPtr l = cucul_get_font_list(); + + int size; + for(size = 0; true; size++) + if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero) + break; + + string[] ret = new string[size]; + for(int i = 0; i < size; i++) + { + IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i); + ret[i] = Marshal.PtrToStringAnsi(s); + } + + return ret; + } + + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern int cucul_get_font_width(IntPtr font); + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern int cucul_get_font_height(IntPtr font); + public Size Size + { + get { return new Size(cucul_get_font_width(_font), + cucul_get_font_height(_font)); } + } + + [DllImport("libcucul.dll", CallingConvention=CallingConvention.Cdecl), + SuppressUnmanagedCodeSecurity] + private static extern IntPtr cucul_get_font_blocks(IntPtr font); + public int[,] getBlocks() + { + IntPtr l = cucul_get_font_blocks(_font); + + int size; + for(size = 1; true; size += 2) + if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero) + break; + + int[,] ret = new int[size,2]; + for(int i = 0; i < size; i++) + { + ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2); + ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1); + } + + return ret; + } + } } diff --git a/csharp/test.cs b/csharp/test.cs index 18dcc84..8e1e2a3 100644 --- a/csharp/test.cs +++ b/csharp/test.cs @@ -27,6 +27,7 @@ class DemoCanvas : CuculCanvas private DateTime startTime; private CuculDither d; + private CuculFont f; public DemoCanvas() { @@ -35,6 +36,8 @@ class DemoCanvas : CuculCanvas image = new uint[16,16]; d = new CuculDither(32, new Size(16, 16), 16 * 4, 0xff0000, 0xff00, 0xff, 0x0); + f = new CuculFont(CuculFont.getList()[0]); + f.getBlocks(); } public void Draw()