You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Font.cs 4.4 KiB

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