Debian Codegraph @

caca-sharp/Font.cs

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