1 | /* |
---|
2 | * libcaca .NET bindings for libcaca |
---|
3 | * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
4 | * 2007 Sam Hocevar <sam@hocevar.net> |
---|
5 | * All Rights Reserved |
---|
6 | * |
---|
7 | * $Id$ |
---|
8 | * |
---|
9 | * This library is free software. It comes without any warranty, to |
---|
10 | * the extent permitted by applicable law. You can redistribute it |
---|
11 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
12 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
13 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
14 | */ |
---|
15 | |
---|
16 | using System; |
---|
17 | using System.Runtime.InteropServices; |
---|
18 | using System.Security; |
---|
19 | using System.Drawing; |
---|
20 | |
---|
21 | namespace Caca |
---|
22 | { |
---|
23 | public class Font : IDisposable |
---|
24 | { |
---|
25 | private IntPtr _font; |
---|
26 | private GCHandle _gch; |
---|
27 | |
---|
28 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
29 | SuppressUnmanagedCodeSecurity] |
---|
30 | private static extern IntPtr caca_load_font(IntPtr data, uint len); |
---|
31 | public Font(string s) |
---|
32 | { |
---|
33 | IntPtr name = Marshal.StringToHGlobalAnsi(s); |
---|
34 | _font = caca_load_font(name, 0); |
---|
35 | Marshal.FreeHGlobal(name); |
---|
36 | } |
---|
37 | |
---|
38 | public Font(byte[] buf) |
---|
39 | { |
---|
40 | GCHandle _gch = GCHandle.Alloc(buf, GCHandleType.Pinned); |
---|
41 | _font = caca_load_font(_gch.AddrOfPinnedObject(), |
---|
42 | (uint)buf.Length); |
---|
43 | } |
---|
44 | |
---|
45 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
46 | SuppressUnmanagedCodeSecurity] |
---|
47 | private static extern int caca_free_font(IntPtr d); |
---|
48 | public void Dispose() |
---|
49 | { |
---|
50 | caca_free_font(_font); |
---|
51 | _gch.Free(); |
---|
52 | GC.SuppressFinalize(this); |
---|
53 | } |
---|
54 | |
---|
55 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
56 | SuppressUnmanagedCodeSecurity] |
---|
57 | private static extern IntPtr caca_get_font_list(); |
---|
58 | public static string[] getList() |
---|
59 | { |
---|
60 | IntPtr l = caca_get_font_list(); |
---|
61 | |
---|
62 | int size; |
---|
63 | for(size = 0; true; size++) |
---|
64 | if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero) |
---|
65 | break; |
---|
66 | |
---|
67 | string[] ret = new string[size]; |
---|
68 | for(int i = 0; i < size; i++) |
---|
69 | { |
---|
70 | IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i); |
---|
71 | ret[i] = Marshal.PtrToStringAnsi(s); |
---|
72 | } |
---|
73 | |
---|
74 | return ret; |
---|
75 | } |
---|
76 | |
---|
77 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
78 | SuppressUnmanagedCodeSecurity] |
---|
79 | private static extern int caca_get_font_width(IntPtr font); |
---|
80 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
81 | SuppressUnmanagedCodeSecurity] |
---|
82 | private static extern int caca_get_font_height(IntPtr font); |
---|
83 | public Size Size |
---|
84 | { |
---|
85 | get { return new Size(caca_get_font_width(_font), |
---|
86 | caca_get_font_height(_font)); } |
---|
87 | } |
---|
88 | |
---|
89 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
90 | SuppressUnmanagedCodeSecurity] |
---|
91 | private static extern IntPtr caca_get_font_blocks(IntPtr font); |
---|
92 | public int[,] getBlocks() |
---|
93 | { |
---|
94 | IntPtr l = caca_get_font_blocks(_font); |
---|
95 | |
---|
96 | int size; |
---|
97 | for(size = 1; true; size += 2) |
---|
98 | if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero) |
---|
99 | break; |
---|
100 | |
---|
101 | int[,] ret = new int[size,2]; |
---|
102 | for(int i = 0; i < size; i++) |
---|
103 | { |
---|
104 | ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2); |
---|
105 | ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1); |
---|
106 | } |
---|
107 | |
---|
108 | return ret; |
---|
109 | } |
---|
110 | |
---|
111 | [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl), |
---|
112 | SuppressUnmanagedCodeSecurity] |
---|
113 | private static extern int caca_render_canvas(IntPtr cv, IntPtr f, |
---|
114 | IntPtr buf, int w, int h, |
---|
115 | int pitch); |
---|
116 | public int Render(Canvas cv, uint[,] buf, int pitch) |
---|
117 | { |
---|
118 | GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned); |
---|
119 | int ret = caca_render_canvas(cv._c_cv, _font, |
---|
120 | gch.AddrOfPinnedObject(), |
---|
121 | buf.GetLength(0), buf.GetLength(1), |
---|
122 | pitch); |
---|
123 | gch.Free(); |
---|
124 | return ret; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|