Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * libcaca .NET bindings for libcaca
  3. * Copyright (c) 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  4. * 2007 Sam Hocevar <sam@zoy.org>
  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. using System;
  16. using System.Runtime.InteropServices;
  17. using System.Security;
  18. using System.Drawing;
  19. namespace Caca
  20. {
  21. public class Font : IDisposable
  22. {
  23. private IntPtr _font;
  24. private GCHandle _gch;
  25. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  26. SuppressUnmanagedCodeSecurity]
  27. private static extern IntPtr caca_load_font(IntPtr data, uint len);
  28. public Font(string s)
  29. {
  30. IntPtr name = Marshal.StringToHGlobalAnsi(s);
  31. _font = caca_load_font(name, 0);
  32. Marshal.FreeHGlobal(name);
  33. }
  34. public Font(byte[] buf)
  35. {
  36. GCHandle _gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  37. _font = caca_load_font(_gch.AddrOfPinnedObject(),
  38. (uint)buf.Length);
  39. }
  40. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  41. SuppressUnmanagedCodeSecurity]
  42. private static extern int caca_free_font(IntPtr d);
  43. public void Dispose()
  44. {
  45. caca_free_font(_font);
  46. _gch.Free();
  47. GC.SuppressFinalize(this);
  48. }
  49. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  50. SuppressUnmanagedCodeSecurity]
  51. private static extern IntPtr caca_get_font_list();
  52. public static string[] getList()
  53. {
  54. IntPtr l = caca_get_font_list();
  55. int size;
  56. for(size = 0; true; size++)
  57. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  58. break;
  59. string[] ret = new string[size];
  60. for(int i = 0; i < size; i++)
  61. {
  62. IntPtr s = Marshal.ReadIntPtr(l, IntPtr.Size * i);
  63. ret[i] = Marshal.PtrToStringAnsi(s);
  64. }
  65. return ret;
  66. }
  67. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  68. SuppressUnmanagedCodeSecurity]
  69. private static extern int caca_get_font_width(IntPtr font);
  70. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  71. SuppressUnmanagedCodeSecurity]
  72. private static extern int caca_get_font_height(IntPtr font);
  73. public Size Size
  74. {
  75. get { return new Size(caca_get_font_width(_font),
  76. caca_get_font_height(_font)); }
  77. }
  78. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  79. SuppressUnmanagedCodeSecurity]
  80. private static extern IntPtr caca_get_font_blocks(IntPtr font);
  81. public int[,] getBlocks()
  82. {
  83. IntPtr l = caca_get_font_blocks(_font);
  84. int size;
  85. for(size = 1; true; size += 2)
  86. if(Marshal.ReadIntPtr(l, IntPtr.Size * size) == IntPtr.Zero)
  87. break;
  88. int[,] ret = new int[size,2];
  89. for(int i = 0; i < size; i++)
  90. {
  91. ret[i,0] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2);
  92. ret[i,1] = (int)Marshal.ReadIntPtr(l, IntPtr.Size * i * 2 + 1);
  93. }
  94. return ret;
  95. }
  96. [DllImport("libcaca.dll", CallingConvention=CallingConvention.Cdecl),
  97. SuppressUnmanagedCodeSecurity]
  98. private static extern int caca_render_canvas(IntPtr cv, IntPtr f,
  99. IntPtr buf, int w, int h,
  100. int pitch);
  101. public int Render(Canvas cv, uint[,] buf, int pitch)
  102. {
  103. GCHandle gch = GCHandle.Alloc(buf, GCHandleType.Pinned);
  104. int ret = caca_render_canvas(cv._c_cv, _font,
  105. gch.AddrOfPinnedObject(),
  106. buf.GetLength(0), buf.GetLength(1),
  107. pitch);
  108. gch.Free();
  109. return ret;
  110. }
  111. }
  112. }