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.
 
 
 
 
 

1820 lines
66 KiB

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using static System.Net.Mime.MediaTypeNames;
  8. namespace lts
  9. {
  10. using RenderRef = UIntPtr;
  11. using Handle = UIntPtr;
  12. using ContextHandle = UIntPtr;
  13. using RendererHandle = UIntPtr;
  14. using TextBlockHandle = UIntPtr;
  15. using FontCreatorHandle = UIntPtr;
  16. using FontHandle = UIntPtr;
  17. using PostProcessorHandle = UIntPtr;
  18. using ImageHandle = UIntPtr;
  19. using CharHandle = UIntPtr;
  20. using WideChar = UInt16;
  21. using System.Collections;
  22. using System.IO;
  23. public enum ErrorCode : Int32
  24. {
  25. Unknown = -1,
  26. None = 0,
  27. // misc
  28. NotInitialized = 1,
  29. InvalidEnum = 2,
  30. InvalidValue = 3,
  31. InvalidOperation = 4,
  32. InvalidType = 5,
  33. // invalid handles
  34. InvalidContextHandle = 100,
  35. InvalidRendererHandle = 101,
  36. InvalidTextBlockHandle = 102,
  37. InvalidFontHandle = 103,
  38. InvalidFontCreatorHandle = 104,
  39. InvalidImageHandle = 105,
  40. InvalidPostProcHandle = 106,
  41. // library
  42. InvalidLibName = 200,
  43. InvalidLibHandle = 201,
  44. InvalidMethodName = 202
  45. };
  46. public enum CodePage : UInt32
  47. {
  48. cpUTF8,
  49. cpISO_8859_1,
  50. cpISO_8859_2,
  51. cpISO_8859_3,
  52. cpISO_8859_4,
  53. cpISO_8859_5,
  54. cpISO_8859_6,
  55. cpISO_8859_7,
  56. cpISO_8859_8,
  57. cpISO_8859_9,
  58. cpISO_8859_10,
  59. cpISO_8859_11,
  60. cpISO_8859_13,
  61. cpISO_8859_14,
  62. cpISO_8859_15,
  63. cpISO_8859_16,
  64. cpISO_037,
  65. cpISO_437,
  66. cpISO_500,
  67. cpISO_737,
  68. cpISO_775,
  69. cpISO_850,
  70. cpISO_852,
  71. cpISO_855,
  72. cpISO_857,
  73. cpISO_860,
  74. cpISO_861,
  75. cpISO_862,
  76. cpISO_863,
  77. cpISO_864,
  78. cpISO_865,
  79. cpISO_866,
  80. cpISO_869,
  81. cpISO_874,
  82. cpISO_875,
  83. cpISO_1026,
  84. cpISO_1250,
  85. cpISO_1251,
  86. cpISO_1252,
  87. cpISO_1253,
  88. cpISO_1254,
  89. cpISO_1255,
  90. cpISO_1256,
  91. cpISO_1257,
  92. cpISO_1258
  93. };
  94. public enum Format : UInt32
  95. {
  96. Empty,
  97. RGBA8,
  98. LumAlpha8,
  99. Alpha8,
  100. Lum8
  101. };
  102. public enum RendererType : UInt32
  103. {
  104. Unknown,
  105. OpenGL,
  106. OpenGLES
  107. };
  108. public enum FontCreatorType : UInt32
  109. {
  110. Unknown,
  111. FreeType,
  112. GDI,
  113. Custom
  114. };
  115. public enum VertAlign : UInt32
  116. {
  117. Top,
  118. Center,
  119. Bottom
  120. };
  121. public enum HorzAlign : UInt32
  122. {
  123. Left,
  124. Center,
  125. Right,
  126. Justify
  127. };
  128. public enum Clipping : UInt32
  129. {
  130. None,
  131. WordBorder,
  132. CharBorder,
  133. WordComplete,
  134. CharComplete
  135. };
  136. public enum AntiAliasing : UInt32
  137. {
  138. None,
  139. Normal
  140. };
  141. public enum CharRangeUsage : UInt32
  142. {
  143. Include,
  144. Exclude
  145. };
  146. public enum ImageMode : UInt32
  147. {
  148. Ignore,
  149. Replace,
  150. Modulate
  151. };
  152. public enum StreamOrigin : UInt32
  153. {
  154. Begin = 0,
  155. Current = 1,
  156. End = 2
  157. };
  158. [Flags]
  159. public enum BlockFlags : UInt32
  160. {
  161. Empty = 0,
  162. WordWrap = (1 << 0)
  163. };
  164. [Flags]
  165. public enum FontStyles : UInt32
  166. {
  167. Empty = 0,
  168. Bold = (1 << 0),
  169. Italic = (1 << 1),
  170. Underline = (1 << 2),
  171. Strikeout = (1 << 3)
  172. };
  173. [Flags]
  174. public enum ColorChannels : UInt32
  175. {
  176. Empty = 0,
  177. Red = (1 << 0),
  178. Green = (1 << 1),
  179. Blue = (1 << 2),
  180. Alpha = (1 << 3)
  181. };
  182. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  183. public struct Position
  184. {
  185. public Int32 x;
  186. public Int32 y;
  187. };
  188. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  189. public struct GlyphMetric
  190. {
  191. Position GlyphOrigin;
  192. Rect GlyphRect;
  193. Int32 Advance;
  194. };
  195. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  196. public struct TextMetric
  197. {
  198. public UInt32 Ascent;
  199. public UInt32 Descent;
  200. public UInt32 ExternalLeading;
  201. public UInt32 BaseLineOffset;
  202. public UInt32 CharSpacing;
  203. public UInt32 LineHeight;
  204. public UInt32 LineSpacing;
  205. };
  206. [StructLayout(LayoutKind.Sequential, Pack = 4)]
  207. public struct FontMetric
  208. {
  209. public UInt32 Size;
  210. public FontStyles Styles;
  211. public AntiAliasing AntiAliasing;
  212. public WideChar defaultChar;
  213. //private fixed byte __reserved[2];
  214. public Int32 Ascent;
  215. public Int32 Descent;
  216. public Int32 ExternalLeading;
  217. public Int32 BaseLineOffset;
  218. public Int32 UnderlinePos;
  219. public Int32 UnderlineSize;
  220. public Int32 StrikeoutPos;
  221. public Int32 StrikeoutSize;
  222. }
  223. [StructLayout(LayoutKind.Explicit, Pack = 1)]
  224. public struct Rect
  225. {
  226. [FieldOffset(0)]
  227. public Position TopLeft;
  228. [FieldOffset(8)]
  229. public Position BottomRight;
  230. [FieldOffset(0)]
  231. public Int32 Left;
  232. [FieldOffset(4)]
  233. public Int32 Top;
  234. [FieldOffset(8)]
  235. public Int32 Right;
  236. [FieldOffset(12)]
  237. public Int32 Bottom;
  238. };
  239. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  240. public unsafe struct Color
  241. {
  242. public float R;
  243. public float G;
  244. public float B;
  245. public float A;
  246. };
  247. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  248. public struct ImageModes
  249. {
  250. public ImageMode R;
  251. public ImageMode G;
  252. public ImageMode B;
  253. public ImageMode A;
  254. };
  255. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  256. public struct Vector4f
  257. {
  258. public float X;
  259. public float Y;
  260. public float Z;
  261. public float W;
  262. };
  263. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  264. public struct Matrix4f
  265. {
  266. public Vector4f AxisX;
  267. public Vector4f AxisY;
  268. public Vector4f AxisZ;
  269. public Vector4f Pos;
  270. };
  271. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  272. public struct RendererCustomData
  273. {
  274. public IntPtr args;
  275. public IntPtr BeginRenderCallback;
  276. public IntPtr EndRenderCallback;
  277. public IntPtr GetDrawPosCallback;
  278. public IntPtr SetDrawPosCallback;
  279. public IntPtr MoveDrawPosCallback;
  280. public IntPtr SetColorCallback;
  281. public IntPtr RenderCallback;
  282. public IntPtr CreateRefCallback;
  283. public IntPtr FreeRefCallback;
  284. };
  285. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  286. public struct StreamData
  287. {
  288. public IntPtr args;
  289. public IntPtr ReadCallback;
  290. public IntPtr SeekCallback;
  291. };
  292. [StructLayout(LayoutKind.Sequential, Pack = 1)]
  293. public struct PostProcessorData
  294. {
  295. public IntPtr args;
  296. public IntPtr ExecuteCallback;
  297. };
  298. public class Constants
  299. {
  300. public static readonly ImageModes ImageModeReplaceAll = new ImageModes()
  301. {
  302. R = ImageMode.Replace,
  303. G = ImageMode.Replace,
  304. B = ImageMode.Replace,
  305. A = ImageMode.Replace
  306. };
  307. public static readonly ImageModes ImageModeModulateAlpha = new ImageModes()
  308. {
  309. R = ImageMode.Replace,
  310. G = ImageMode.Replace,
  311. B = ImageMode.Replace,
  312. A = ImageMode.Modulate
  313. };
  314. public static readonly ImageModes ImageModeModulateAll = new ImageModes()
  315. {
  316. R = ImageMode.Modulate,
  317. G = ImageMode.Modulate,
  318. B = ImageMode.Modulate,
  319. A = ImageMode.Modulate
  320. };
  321. public const ColorChannels ColorChannelsRGB = ColorChannels.Red | ColorChannels.Green | ColorChannels.Blue;
  322. public const ColorChannels ColorChannelsRGBA = ColorChannels.Red | ColorChannels.Green | ColorChannels.Blue | ColorChannels.Alpha;
  323. };
  324. public static unsafe class Imports
  325. {
  326. #region General
  327. [DllImport("libtextsuite.dll", EntryPoint = "ltsInitialize", CallingConvention = CallingConvention.StdCall)]
  328. public static extern ErrorCode Initialize();
  329. [DllImport("libtextsuite.dll", EntryPoint = "ltsGetVersion", CallingConvention = CallingConvention.StdCall)]
  330. public static extern IntPtr GetVersion();
  331. [DllImport("libtextsuite.dll", EntryPoint = "ltsGetLastErrorCode", CallingConvention = CallingConvention.StdCall)]
  332. public static extern ErrorCode GetLastErrorCode();
  333. [DllImport("libtextsuite.dll", EntryPoint = "ltsGetLastErrorMsg", CallingConvention = CallingConvention.StdCall)]
  334. public static extern IntPtr GetLastErrorMsg();
  335. [DllImport("libtextsuite.dll", EntryPoint = "ltsFinalize", CallingConvention = CallingConvention.StdCall)]
  336. public static extern ErrorCode Finalize();
  337. #endregion
  338. #region Context
  339. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextCreate", CallingConvention = CallingConvention.StdCall)]
  340. public static extern ContextHandle ContextCreate();
  341. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextGetCodePage", CallingConvention = CallingConvention.StdCall)]
  342. public static extern ErrorCode ContextGetCodePage(ContextHandle handle, out CodePage value);
  343. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextGetDefaultChar", CallingConvention = CallingConvention.StdCall)]
  344. public static extern ErrorCode ContextGetDefaultChar(ContextHandle handle, out WideChar value);
  345. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextSetCodePage", CallingConvention = CallingConvention.StdCall)]
  346. public static extern ErrorCode ContextSetCodePage(ContextHandle handle, CodePage value);
  347. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextSetDefaultChar", CallingConvention = CallingConvention.StdCall)]
  348. public static extern ErrorCode ContextSetDefaultChar(ContextHandle handle, WideChar value);
  349. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextAnsiToWide", CallingConvention = CallingConvention.StdCall)]
  350. public static extern IntPtr ContextAnsiToWide(ContextHandle handle, [MarshalAs(UnmanagedType.LPStr)] String text);
  351. [DllImport("libtextsuite.dll", EntryPoint = "ltsContextDestroy", CallingConvention = CallingConvention.StdCall)]
  352. public static extern ErrorCode ContextDestroy(ContextHandle handle);
  353. #endregion
  354. #region Renderer
  355. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererCreate", CallingConvention = CallingConvention.StdCall)]
  356. public static extern RendererHandle RendererCreate(ContextHandle handle, Int32 type, Int32 format);
  357. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererCreateCustom", CallingConvention = CallingConvention.StdCall)]
  358. public static extern RendererHandle RendererCreateCustom(ContextHandle handle, Format format, ref RendererCustomData data);
  359. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererBeginBlock", CallingConvention = CallingConvention.StdCall)]
  360. public static extern TextBlockHandle RendererBeginBlock(RendererHandle handle, Int32 top, Int32 left, Int32 width, Int32 height, BlockFlags flags);
  361. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererEndBlock", CallingConvention = CallingConvention.StdCall)]
  362. public static extern ErrorCode RendererEndBlock(RendererHandle handle, TextBlockHandle block);
  363. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererAbortBlock", CallingConvention = CallingConvention.StdCall)]
  364. public static extern ErrorCode RendererAbortBlock(RendererHandle handle, TextBlockHandle block);
  365. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererGetTextWidthA", CallingConvention = CallingConvention.StdCall)]
  366. public static extern Int32 RendererGetTextWidthA(RendererHandle handle, FontHandle font, [MarshalAs(UnmanagedType.LPStr)] String text);
  367. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererGetTextWidthW", CallingConvention = CallingConvention.StdCall)]
  368. public static extern Int32 RendererGetTextWidthW(RendererHandle handle, FontHandle font, [MarshalAs(UnmanagedType.LPWStr)] String text);
  369. [DllImport("libtextsuite.dll", EntryPoint = "ltsRendererDestroy", CallingConvention = CallingConvention.StdCall)]
  370. public static extern ErrorCode RendererDestroy(RendererHandle handle);
  371. #endregion
  372. #region FontCreator
  373. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  374. public delegate Int32 FontCreatorStreamReadHandler(IntPtr args, IntPtr buffer, Int32 count);
  375. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  376. public delegate Int32 FontCreatorStreamSeekHandler(IntPtr args, StreamOrigin origin, Int32 offset);
  377. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontCreatorCreate", CallingConvention = CallingConvention.StdCall)]
  378. public static extern FontCreatorHandle FontCreatorCreate(ContextHandle cHandle, FontCreatorType type);
  379. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontCreatorGetFontByName", CallingConvention = CallingConvention.StdCall)]
  380. public static extern FontHandle FontCreatorGetFontByName(FontCreatorHandle handle, [MarshalAs(UnmanagedType.LPStr)] String fontname, Int32 size, FontStyles style, AntiAliasing antiAliasing);
  381. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontCreatorGetFontByFile", CallingConvention = CallingConvention.StdCall)]
  382. public static extern FontHandle FontCreatorGetFontByFile(FontCreatorHandle handle, [MarshalAs(UnmanagedType.LPStr)] String filename, Int32 size, FontStyles style, AntiAliasing antiAliasing);
  383. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontCreatorGetFontByStream", CallingConvention = CallingConvention.StdCall)]
  384. public static extern FontHandle FontCreatorGetFontByStream(FontCreatorHandle handle, ref StreamData stream, Int32 size, FontStyles style, AntiAliasing antiAliasing);
  385. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontCreatorDestroy", CallingConvention = CallingConvention.StdCall)]
  386. public static extern ErrorCode FontCreatorDestory(FontCreatorHandle handle);
  387. #endregion
  388. #region Font
  389. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetPostProcessor", CallingConvention = CallingConvention.StdCall)]
  390. public static extern PostProcessorHandle FontGetPostProcessor(FontHandle handle);
  391. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetTabWidth", CallingConvention = CallingConvention.StdCall)]
  392. public static extern ErrorCode FontGetTabWidth(FontHandle handle, out Int32 value);
  393. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetCharSpacing", CallingConvention = CallingConvention.StdCall)]
  394. public static extern ErrorCode FontGetCharSpacing(FontHandle handle, out Int32 value);
  395. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetLineSpacing", CallingConvention = CallingConvention.StdCall)]
  396. public static extern ErrorCode FontGetLineSpacing(FontHandle handle, out Int32 value);
  397. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetMetric", CallingConvention = CallingConvention.StdCall)]
  398. public static extern ErrorCode FontGetMetric(FontHandle handle, out FontMetric value);
  399. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetFontName", CallingConvention = CallingConvention.StdCall)]
  400. public static extern IntPtr FontGetFontName(FontHandle handle);
  401. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetFaceName", CallingConvention = CallingConvention.StdCall)]
  402. public static extern IntPtr FontGetFaceName(FontHandle handle);
  403. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetStyleName", CallingConvention = CallingConvention.StdCall)]
  404. public static extern IntPtr FontGetStyleName(FontHandle handle);
  405. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetFullName", CallingConvention = CallingConvention.StdCall)]
  406. public static extern IntPtr FontGetFullName(FontHandle handle);
  407. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontGetCopyrightName", CallingConvention = CallingConvention.StdCall)]
  408. public static extern IntPtr FontGetCopyrightName(FontHandle handle);
  409. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontSetPostProcessor", CallingConvention = CallingConvention.StdCall)]
  410. public static extern ErrorCode FontSetPostProcessor(FontHandle handle, PostProcessorHandle pp);
  411. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontSetTabWidth", CallingConvention = CallingConvention.StdCall)]
  412. public static extern ErrorCode FontSetTabWidth(FontHandle handle, Int32 value);
  413. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontSetCharSpacing", CallingConvention = CallingConvention.StdCall)]
  414. public static extern ErrorCode FontSetCharSpacing(FontHandle handle, Int32 value);
  415. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontSetLineSpacing", CallingConvention = CallingConvention.StdCall)]
  416. public static extern ErrorCode FontSetLineSpacing(FontHandle handle, Int32 value);
  417. [DllImport("libtextsuite.dll", EntryPoint = "ltsFontDestroy", CallingConvention = CallingConvention.StdCall)]
  418. public static extern ErrorCode FontDestroy(FontHandle handle);
  419. #endregion
  420. #region TextBlock
  421. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetRect", CallingConvention = CallingConvention.StdCall)]
  422. public static extern ErrorCode TextBlockGetRect(TextBlockHandle handle, out Rect value);
  423. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetWidth", CallingConvention = CallingConvention.StdCall)]
  424. public static extern ErrorCode TextBlockGetWidth(TextBlockHandle handle, out Int32 value);
  425. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetHeight", CallingConvention = CallingConvention.StdCall)]
  426. public static extern ErrorCode TextBlockGetHeight(TextBlockHandle handle, out Int32 value);
  427. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetFlags", CallingConvention = CallingConvention.StdCall)]
  428. public static extern ErrorCode TextBlockGetFlags(TextBlockHandle handle, out BlockFlags value);
  429. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetTop", CallingConvention = CallingConvention.StdCall)]
  430. public static extern ErrorCode TextBlockGetTop(TextBlockHandle handle, out Int32 value);
  431. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetLeft", CallingConvention = CallingConvention.StdCall)]
  432. public static extern ErrorCode TextBlockGetLeft(TextBlockHandle handle, out Int32 value);
  433. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetVertAlign", CallingConvention = CallingConvention.StdCall)]
  434. public static extern ErrorCode TextBlockGetVertAlign(TextBlockHandle handle, out VertAlign value);
  435. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetHorzAlign", CallingConvention = CallingConvention.StdCall)]
  436. public static extern ErrorCode TextBlockGetHorzAlign(TextBlockHandle handle, out HorzAlign value);
  437. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetClipping", CallingConvention = CallingConvention.StdCall)]
  438. public static extern ErrorCode TextBlockGetClipping(TextBlockHandle handle, out Clipping value);
  439. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetColor", CallingConvention = CallingConvention.StdCall)]
  440. public static extern ErrorCode TextBlockGetColor(TextBlockHandle handle, out Color value);
  441. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetFont", CallingConvention = CallingConvention.StdCall)]
  442. public static extern ErrorCode TextBlockGetFont(TextBlockHandle handle, out FontHandle value);
  443. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetTop", CallingConvention = CallingConvention.StdCall)]
  444. public static extern ErrorCode TextBlockSetTop(TextBlockHandle handle, Int32 value);
  445. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetLeft", CallingConvention = CallingConvention.StdCall)]
  446. public static extern ErrorCode TextBlockSetLeft(TextBlockHandle handle, Int32 value);
  447. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetVertAlign", CallingConvention = CallingConvention.StdCall)]
  448. public static extern ErrorCode TextBlockSetVertAlign(TextBlockHandle handle, VertAlign value);
  449. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetHorzAlign", CallingConvention = CallingConvention.StdCall)]
  450. public static extern ErrorCode TextBlockSetHorzAlign(TextBlockHandle handle, HorzAlign value);
  451. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetClipping", CallingConvention = CallingConvention.StdCall)]
  452. public static extern ErrorCode TextBlockSetClipping(TextBlockHandle handle, Clipping value);
  453. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetColor", CallingConvention = CallingConvention.StdCall)]
  454. public static extern ErrorCode TextBlockSetColor(TextBlockHandle handle, Color value);
  455. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockSetFont", CallingConvention = CallingConvention.StdCall)]
  456. public static extern ErrorCode TextBlockSetFont(TextBlockHandle handle, FontHandle value);
  457. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetActualHeight", CallingConvention = CallingConvention.StdCall)]
  458. public static extern Int32 TextBlockGetActualHeight(TextBlockHandle handle);
  459. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetTextWidthA", CallingConvention = CallingConvention.StdCall)]
  460. public static extern Int32 TextBlockGetTextWidthA(TextBlockHandle handle, [MarshalAs(UnmanagedType.LPStr)] String text);
  461. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockGetTextWidthW", CallingConvention = CallingConvention.StdCall)]
  462. public static extern Int32 TextBlockGetTextWidthW(TextBlockHandle handle, [MarshalAs(UnmanagedType.LPWStr)] String text);
  463. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockTextOutA", CallingConvention = CallingConvention.StdCall)]
  464. public static extern ErrorCode TextBlockTextOutA(TextBlockHandle handle, [MarshalAs(UnmanagedType.LPStr)] String text);
  465. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockTextOutW", CallingConvention = CallingConvention.StdCall)]
  466. public static extern ErrorCode TextBlockTextOutW(TextBlockHandle handle, [MarshalAs(UnmanagedType.LPWStr)] String text);
  467. [DllImport("libtextsuite.dll", EntryPoint = "ltsTextBlockDestroy", CallingConvention = CallingConvention.StdCall)]
  468. public static extern ErrorCode TextBlockDestroy(TextBlockHandle handle);
  469. #endregion
  470. #region Image
  471. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  472. public delegate void LoadHandler(ImageHandle handle, Int32 x, Int32 y, ref Color pixel, IntPtr args);
  473. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  474. public delegate void BlendHandler(ImageHandle handle, Color src, Color dst, out Color merged, IntPtr args);
  475. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageCreate", CallingConvention = CallingConvention.StdCall)]
  476. public static extern ImageHandle ImageCreate(ContextHandle cHandle);
  477. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetIsEmpty", CallingConvention = CallingConvention.StdCall)]
  478. public static extern ErrorCode ImageGetIsEmpty(ImageHandle handle, out bool value);
  479. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetWidth", CallingConvention = CallingConvention.StdCall)]
  480. public static extern Int32 ImageGetWidth(ImageHandle handle);
  481. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetHeight", CallingConvention = CallingConvention.StdCall)]
  482. public static extern Int32 ImageGetHeight(ImageHandle handle);
  483. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetLineSize", CallingConvention = CallingConvention.StdCall)]
  484. public static extern Int32 ImageGetLineSize(ImageHandle handle);
  485. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetDataSize", CallingConvention = CallingConvention.StdCall)]
  486. public static extern Int32 ImageGetDataSize(ImageHandle handle);
  487. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetFormat", CallingConvention = CallingConvention.StdCall)]
  488. public static extern ErrorCode ImageGetFormat(ImageHandle handle, out Format format);
  489. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetData", CallingConvention = CallingConvention.StdCall)]
  490. public static extern IntPtr ImageGetData(ImageHandle handle);
  491. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetScanLine", CallingConvention = CallingConvention.StdCall)]
  492. public static extern IntPtr ImageGetScanLine(ImageHandle handle, Int32 index);
  493. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageGetPixelAt", CallingConvention = CallingConvention.StdCall)]
  494. public static extern ErrorCode ImageGetPixelAt(ImageHandle handle, Int32 x, Int32 y, out Color pixel);
  495. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageAssign", CallingConvention = CallingConvention.StdCall)]
  496. public static extern ErrorCode ImageAssign(ImageHandle handle, ImageHandle source);
  497. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageCreateEmpty", CallingConvention = CallingConvention.StdCall)]
  498. public static extern ErrorCode ImageCreateEmpty(ImageHandle handle, Format format, Int32 width, Int32 height);
  499. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageLoadFromFunc", CallingConvention = CallingConvention.StdCall)]
  500. public static extern ErrorCode ImageLoadFromFunc(ImageHandle handle, LoadHandler callback, IntPtr args);
  501. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageResize", CallingConvention = CallingConvention.StdCall)]
  502. public static extern ErrorCode ImageResize(ImageHandle handle, Int32 width, Int32 height, Int32 x, Int32 y);
  503. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageFillColor", CallingConvention = CallingConvention.StdCall)]
  504. public static extern ErrorCode ImageFillColor(ImageHandle handle, Color color, ColorChannels mask, ref ImageModes modes);
  505. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageFillPattern", CallingConvention = CallingConvention.StdCall)]
  506. public static extern ErrorCode ImageFillPattern(ImageHandle handle, ImageHandle patter, Int32 x, Int32 y, ColorChannels mask, ref ImageModes modes);
  507. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageBlend", CallingConvention = CallingConvention.StdCall)]
  508. public static extern ErrorCode ImageBlend(ImageHandle handle, ImageHandle source, Int32 x, Int32 y, BlendHandler callback, IntPtr args);
  509. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageBlur", CallingConvention = CallingConvention.StdCall)]
  510. public static extern ErrorCode ImageBlur(ImageHandle handle, float horzRad, float hortStr, float vertRad, float vertStr, ColorChannels mask);
  511. [DllImport("libtextsuite.dll", EntryPoint = "ltsImageDestroy", CallingConvention = CallingConvention.StdCall)]
  512. public static extern ErrorCode ImageDestroy(ImageHandle handle);
  513. #endregion
  514. #region PostProcessor
  515. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  516. public delegate void PostProcessorHandler(CharHandle charHandle, ImageHandle imageHandle, IntPtr args);
  517. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorAddRange", CallingConvention = CallingConvention.StdCall)]
  518. public static extern ErrorCode PostProcessorAddRange(PostProcessorHandle handle, CharRangeUsage usage, WideChar start, WideChar stop);
  519. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorAddChars", CallingConvention = CallingConvention.StdCall)]
  520. public static extern ErrorCode PostProcessorAddChars(PostProcessorHandle handle, CharRangeUsage usage, [MarshalAs(UnmanagedType.LPWStr)] String text);
  521. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorClearRanges", CallingConvention = CallingConvention.StdCall)]
  522. public static extern ErrorCode PostProcessorClearRanges(PostProcessorHandle handle);
  523. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorExecute", CallingConvention = CallingConvention.StdCall)]
  524. public static extern ErrorCode PostProcessorExecute(PostProcessorHandle handle, CharHandle charHandle, ImageHandle imageHandle);
  525. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorFillColorCreate", CallingConvention = CallingConvention.StdCall)]
  526. public static extern PostProcessorHandle PostProcessorFillColorCreate(PostProcessorHandle handle, Color color, ref ImageModes modes, ColorChannels channels);
  527. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorFillPatternCreate", CallingConvention = CallingConvention.StdCall)]
  528. public static extern PostProcessorHandle PostProcessorFillPatternCreate(ContextHandle handle, ImageHandle pattern, bool ownsPattern, Position position, ref ImageModes modes, ColorChannels channels);
  529. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorBorderCreate", CallingConvention = CallingConvention.StdCall)]
  530. public static extern PostProcessorHandle PostProcessorBorderCreate(PostProcessorHandle handle, float width, float strength, Color color, bool keepSize);
  531. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorShadowCreate", CallingConvention = CallingConvention.StdCall)]
  532. public static extern PostProcessorHandle PostProcessorShadowCreate(PostProcessorHandle handle, float raduis, float strength, Position position, Color color);
  533. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorCustomCreate", CallingConvention = CallingConvention.StdCall)]
  534. public static extern PostProcessorHandle PostProcessorCustomCreate(PostProcessorHandle handle, ref PostProcessorData data);
  535. [DllImport("libtextsuite.dll", EntryPoint = "ltsPostProcessorDestroy", CallingConvention = CallingConvention.StdCall)]
  536. public static extern ErrorCode PostProcessorDestroy(PostProcessorHandle handle);
  537. #endregion
  538. #region Char
  539. [DllImport("libtextsuite.dll", EntryPoint = "ltsCharGetCharCode", CallingConvention = CallingConvention.StdCall)]
  540. public static extern ErrorCode CharGetCharCode(CharHandle handle, out WideChar value);
  541. [DllImport("libtextsuite.dll", EntryPoint = "ltsCharGetGlyphMetric", CallingConvention = CallingConvention.StdCall)]
  542. public static extern ErrorCode CharGetGlyphMetric(CharHandle handle, out GlyphMetric value);
  543. [DllImport("libtextsuite.dll", EntryPoint = "ltsCharSetGlyphMetric", CallingConvention = CallingConvention.StdCall)]
  544. public static extern ErrorCode CharSetGlyphMetric(CharHandle handle, ref GlyphMetric value);
  545. #endregion
  546. };
  547. public class Exception : System.Exception
  548. {
  549. public ErrorCode ErrorCode { get; private set; }
  550. public string ErrorMessage { get; private set; }
  551. public static void ThrowError(ErrorCode err, string msg)
  552. {
  553. throw new Exception()
  554. {
  555. ErrorCode = err,
  556. ErrorMessage = msg,
  557. };
  558. }
  559. public static void ThrowLastError(ErrorCode err)
  560. {
  561. var pMsg = Imports.GetLastErrorMsg();
  562. var msg = Marshal.PtrToStringAnsi(pMsg);
  563. ThrowError(err, msg);
  564. }
  565. public static void ThrowLastError()
  566. {
  567. var err = Imports.GetLastErrorCode();
  568. ThrowLastError(err);
  569. }
  570. public static void HandleError(ErrorCode err)
  571. {
  572. if (err != ErrorCode.None)
  573. ThrowLastError(err);
  574. }
  575. };
  576. static class Impl
  577. {
  578. private static bool _isInitialized = false;
  579. private static int _refCount = 0;
  580. private static object _lock = new object();
  581. public static void Initialize()
  582. {
  583. lock (_lock)
  584. {
  585. ++_refCount;
  586. if (!_isInitialized)
  587. {
  588. var err = Imports.Initialize();
  589. if (err != ErrorCode.None)
  590. Exception.ThrowLastError();
  591. }
  592. }
  593. }
  594. public static void Finish()
  595. {
  596. lock (_lock)
  597. {
  598. --_refCount;
  599. if (_refCount == 0 && _isInitialized)
  600. {
  601. var err = Imports.Finalize();
  602. if (err != ErrorCode.None)
  603. Exception.ThrowLastError();
  604. }
  605. }
  606. }
  607. }
  608. public class Context : IDisposable
  609. {
  610. bool _initialized;
  611. public Context()
  612. {
  613. Impl.Initialize();
  614. _initialized = true;
  615. Handle = Imports.ContextCreate();
  616. if (Handle == ContextHandle.Zero)
  617. Exception.ThrowLastError();
  618. }
  619. ~Context()
  620. {
  621. Dispose(false);
  622. }
  623. public ContextHandle Handle { get; private set; }
  624. #region IDisposable Support
  625. protected virtual void Dispose(bool disposing)
  626. {
  627. if (Handle != UIntPtr.Zero)
  628. {
  629. Imports.ContextDestroy(Handle);
  630. Handle = UIntPtr.Zero;
  631. }
  632. if (_initialized)
  633. {
  634. Impl.Finish();
  635. _initialized = false;
  636. }
  637. }
  638. public void Dispose()
  639. {
  640. Dispose(true);
  641. GC.SuppressFinalize(this);
  642. }
  643. #endregion
  644. };
  645. public class Char
  646. {
  647. public Char(CharHandle handle)
  648. {
  649. Handle = handle;
  650. }
  651. public CharHandle Handle { get; private set; }
  652. public WideChar DefaultChar
  653. {
  654. get
  655. {
  656. WideChar value;
  657. Exception.HandleError(Imports.CharGetCharCode(Handle, out value));
  658. return value;
  659. }
  660. }
  661. public GlyphMetric GlyphMetriy
  662. {
  663. get
  664. {
  665. GlyphMetric value;
  666. Exception.HandleError(Imports.CharGetGlyphMetric(Handle, out value));
  667. return value;
  668. }
  669. set
  670. {
  671. Exception.HandleError(Imports.CharSetGlyphMetric(Handle, ref value));
  672. }
  673. }
  674. }
  675. public class Image : IDisposable
  676. {
  677. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  678. public delegate void LoadCallback(Image image, Int32 x, Int32 y, ref Color pixel);
  679. [UnmanagedFunctionPointer(CallingConvention.StdCall)]
  680. public delegate Color BlendCallback(Image image, Color src, Color dst);
  681. private struct LoadArgs
  682. {
  683. public LoadCallback callback;
  684. public Image image;
  685. };
  686. private static Imports.LoadHandler _loadHandler = new Imports.LoadHandler(LoadHandlerImpl);
  687. private static void LoadHandlerImpl(ImageHandle handle, Int32 x, Int32 y, ref Color pixel, IntPtr args)
  688. {
  689. var loadArgs = (LoadArgs)Marshal.PtrToStructure(args, typeof(LoadArgs));
  690. loadArgs.callback(loadArgs.image, x, y, ref pixel);
  691. }
  692. private struct BlendArgs
  693. {
  694. public BlendCallback callback;
  695. public Image image;
  696. };
  697. private static Imports.BlendHandler _blendHandler = new Imports.BlendHandler(BlendHandlerImpl);
  698. private static void BlendHandlerImpl(ImageHandle handle, Color src, Color dst, out Color merged, IntPtr args)
  699. {
  700. var blendArgs = (BlendArgs)Marshal.PtrToStructure(args, typeof(BlendArgs));
  701. merged = blendArgs.callback(blendArgs.image, src, dst);
  702. }
  703. bool _ownsHandle;
  704. byte[] _data;
  705. public Image(Context context)
  706. {
  707. _ownsHandle = true;
  708. Handle = Imports.ImageCreate(context.Handle);
  709. if (Handle == ImageHandle.Zero)
  710. Exception.ThrowLastError();
  711. }
  712. public Image(ImageHandle handle)
  713. {
  714. _ownsHandle = false;
  715. Handle = handle;
  716. }
  717. ~Image()
  718. {
  719. Dispose(false);
  720. }
  721. public ImageHandle Handle { get; private set; }
  722. public bool IsEmpty
  723. {
  724. get
  725. {
  726. bool value;
  727. Imports.ImageGetIsEmpty(Handle, out value);
  728. return value;
  729. }
  730. }
  731. public Int32 Width
  732. {
  733. get
  734. {
  735. Int32 value = Imports.ImageGetWidth(Handle);
  736. if (value < 0)
  737. Exception.ThrowLastError();
  738. return value;
  739. }
  740. }
  741. public Int32 Height
  742. {
  743. get
  744. {
  745. Int32 value = Imports.ImageGetHeight(Handle);
  746. if (value < 0)
  747. Exception.ThrowLastError();
  748. return value;
  749. }
  750. }
  751. public Int32 LineSize
  752. {
  753. get
  754. {
  755. Int32 value = Imports.ImageGetLineSize(Handle);
  756. if (value < 0)
  757. Exception.ThrowLastError();
  758. return value;
  759. }
  760. }
  761. public Int32 DataSize
  762. {
  763. get
  764. {
  765. Int32 value = Imports.ImageGetDataSize(Handle);
  766. if (value < 0)
  767. Exception.ThrowLastError();
  768. return value;
  769. }
  770. }
  771. public Format Format
  772. {
  773. get
  774. {
  775. Format value;
  776. Exception.HandleError(Imports.ImageGetFormat(Handle, out value));
  777. return value;
  778. }
  779. }
  780. public byte[] Data
  781. {
  782. get
  783. {
  784. if (_data == null)
  785. {
  786. IntPtr value = Imports.ImageGetData(Handle);
  787. if (value == IntPtr.Zero)
  788. Exception.ThrowLastError();
  789. var sz = DataSize;
  790. _data = new byte[sz];
  791. Marshal.Copy(value, _data, 0, sz);
  792. }
  793. return _data;
  794. }
  795. set
  796. {
  797. if (value == null)
  798. throw new ArgumentNullException("Data");
  799. var sz = DataSize;
  800. IntPtr ptr = Imports.ImageGetData(Handle);
  801. if (ptr == IntPtr.Zero)
  802. Exception.ThrowLastError();
  803. Marshal.Copy(value, 0, ptr, Math.Min(sz, value.Length));
  804. _data = null;
  805. }
  806. }
  807. public Color GetPixelAt(Int32 x, Int32 y)
  808. {
  809. Color value;
  810. Exception.HandleError(Imports.ImageGetPixelAt(Handle, x, y, out value));
  811. return value;
  812. }
  813. public void Assign(Image source)
  814. {
  815. _data = null;
  816. Exception.HandleError(Imports.ImageAssign(Handle, source.Handle));
  817. }
  818. public void CreateEmpty(Format format, Int32 width, Int32 height)
  819. {
  820. _data = null;
  821. Exception.HandleError(Imports.ImageCreateEmpty(Handle, format, width, height));
  822. }
  823. public void LoadFromFunc(LoadCallback callback)
  824. {
  825. _data = null;
  826. LoadArgs args = new LoadArgs()
  827. {
  828. callback = callback,
  829. image = this,
  830. };
  831. var argsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(args));
  832. Marshal.StructureToPtr(args, argsPtr, false);
  833. try
  834. {
  835. Exception.HandleError(Imports.ImageLoadFromFunc(Handle, _loadHandler, argsPtr));
  836. }
  837. finally
  838. {
  839. Marshal.FreeHGlobal(argsPtr);
  840. }
  841. }
  842. public void Resize(Int32 width, Int32 height, Int32 x, Int32 y)
  843. {
  844. _data = null;
  845. Exception.HandleError(Imports.ImageResize(Handle, width, height, x, y));
  846. }
  847. public void FillColor(Color color, ColorChannels mask, ImageModes modes)
  848. {
  849. _data = null;
  850. Exception.HandleError(Imports.ImageFillColor(Handle, color, mask, ref modes));
  851. }
  852. public void FillPattern(Image pattern, Int32 x, Int32 y, ColorChannels mask, ImageModes modes)
  853. {
  854. _data = null;
  855. Exception.HandleError(Imports.ImageFillPattern(Handle, pattern.Handle, x, y, mask, ref modes));
  856. }
  857. public void Blend(Image other, Int32 x, Int32 y, BlendCallback callback)
  858. {
  859. _data = null;
  860. BlendArgs args = new BlendArgs()
  861. {
  862. callback = callback,
  863. image = this,
  864. };
  865. var argsPtr = Marshal.AllocHGlobal(Marshal.SizeOf(args));
  866. Marshal.StructureToPtr(args, argsPtr, false);
  867. try
  868. {
  869. Exception.HandleError(Imports.ImageBlend(Handle, other.Handle, x, y, _blendHandler, argsPtr));
  870. }
  871. finally
  872. {
  873. Marshal.FreeHGlobal(argsPtr);
  874. }
  875. }
  876. public void Blur(float horzRad, float horzStr, float vertRad, float vertStr, ColorChannels mask)
  877. {
  878. _data = null;
  879. Exception.HandleError(Imports.ImageBlur(Handle, horzRad, horzStr, vertRad, vertStr, mask));
  880. }
  881. #region IDisposable Support
  882. protected virtual void Dispose(bool disposing)
  883. {
  884. if (Handle != UIntPtr.Zero && _ownsHandle)
  885. {
  886. Imports.ImageDestroy(Handle);
  887. Handle = UIntPtr.Zero;
  888. }
  889. }
  890. public void Dispose()
  891. {
  892. Dispose(true);
  893. GC.SuppressFinalize(this);
  894. }
  895. #endregion
  896. }
  897. public abstract class PostProcessor : IDisposable
  898. {
  899. ~PostProcessor()
  900. {
  901. Dispose(false);
  902. }
  903. public PostProcessorHandle Handle { get; protected set; }
  904. public void Execute(Char c, Image i)
  905. {
  906. Exception.HandleError(Imports.PostProcessorExecute(Handle, c.Handle, i.Handle));
  907. }
  908. public void AddRange(CharRangeUsage usage, WideChar start, WideChar stop)
  909. {
  910. Exception.HandleError(Imports.PostProcessorAddRange(Handle, usage, start, stop));
  911. }
  912. public void AddChars(CharRangeUsage usage, string s)
  913. {
  914. Exception.HandleError(Imports.PostProcessorAddChars(Handle, usage, s));
  915. }
  916. public void ClearRanges()
  917. {
  918. Exception.HandleError(Imports.PostProcessorClearRanges(Handle));
  919. }
  920. #region IDisposable Support
  921. protected virtual void Dispose(bool disposing)
  922. {
  923. if (Handle != UIntPtr.Zero)
  924. {
  925. Imports.PostProcessorDestroy(Handle);
  926. Handle = UIntPtr.Zero;
  927. }
  928. }
  929. public void Dispose()
  930. {
  931. Dispose(true);
  932. GC.SuppressFinalize(this);
  933. }
  934. #endregion
  935. }
  936. public class PostProcessorFillColor : PostProcessor
  937. {
  938. public PostProcessorFillColor(Context context, Color color, ImageModes modes, ColorChannels channels)
  939. {
  940. if (context == null)
  941. throw new ArgumentNullException("context");
  942. Color = color;
  943. Modes = modes;
  944. Channels = channels;
  945. Handle = Imports.PostProcessorFillColorCreate(context.Handle, color, ref modes, channels);
  946. if (Handle == UIntPtr.Zero)
  947. Exception.ThrowLastError();
  948. }
  949. public Color Color { get; private set; }
  950. public ImageModes Modes { get; private set; }
  951. public ColorChannels Channels { get; private set; }
  952. }
  953. public class PostProcessorFillPattern : PostProcessor
  954. {
  955. public PostProcessorFillPattern(Context context, Image pattern, Position pos, ImageModes modes, ColorChannels channels)
  956. {
  957. if (context == null)
  958. throw new ArgumentNullException("context");
  959. if (pattern == null)
  960. throw new ArgumentNullException("pattern");
  961. Pattern = pattern;
  962. Position = pos;
  963. Modes = modes;
  964. Channels = channels;
  965. Handle = Imports.PostProcessorFillPatternCreate(context.Handle, Pattern.Handle, false, Position, ref modes, Channels);
  966. if (Handle == UIntPtr.Zero)
  967. Exception.ThrowLastError();
  968. }
  969. public Image Pattern { get; private set; }
  970. public Position Position { get; private set; }
  971. public ImageModes Modes { get; private set; }
  972. public ColorChannels Channels { get; private set; }
  973. }
  974. public class PostProcessorBorder : PostProcessor
  975. {
  976. public PostProcessorBorder(Context context, float width, float strength, Color color, bool keepSize)
  977. {
  978. if (context == null)
  979. throw new ArgumentNullException("context");
  980. Width = width;
  981. Strength = strength;
  982. Color = color;
  983. KeepSize = keepSize;
  984. Handle = Imports.PostProcessorBorderCreate(context.Handle, Width, Strength, Color, KeepSize);
  985. if (Handle == UIntPtr.Zero)
  986. Exception.ThrowLastError();
  987. }
  988. public float Width { get; private set; }
  989. public float Strength { get; private set; }
  990. public Color Color { get; private set; }
  991. public bool KeepSize { get; private set; }
  992. }
  993. public class PostProcessorShadow : PostProcessor
  994. {
  995. public PostProcessorShadow(Context context, float radius, float strength, Position offset, Color color)
  996. {
  997. if (context == null)
  998. throw new ArgumentNullException("context");
  999. Radius = radius;
  1000. Strength = strength;
  1001. Offset = offset;
  1002. Color = color;
  1003. Handle = Imports.PostProcessorShadowCreate(context.Handle, Radius, Strength, Offset, Color);
  1004. if (Handle == UIntPtr.Zero)
  1005. Exception.ThrowLastError();
  1006. }
  1007. public float Radius { get; private set; }
  1008. public float Strength { get; private set; }
  1009. public Position Offset { get; private set; }
  1010. public Color Color { get; private set; }
  1011. }
  1012. public abstract class PostProcessorCustom : PostProcessor
  1013. {
  1014. private GCHandle _self;
  1015. private static Imports.PostProcessorHandler _executeHandler = new Imports.PostProcessorHandler(ExecuteHandler);
  1016. private static void ExecuteHandler(CharHandle charHandle, ImageHandle imageHandle, IntPtr args)
  1017. {
  1018. GCHandle self = (GCHandle)args;
  1019. Char c = new Char(charHandle);
  1020. Image i = new Image(imageHandle);
  1021. (self.Target as PostProcessorCustom).doExecute(c, i);
  1022. }
  1023. public PostProcessorCustom(Context context)
  1024. {
  1025. if (context == null)
  1026. throw new ArgumentNullException("context");
  1027. _self = GCHandle.Alloc(this);
  1028. PostProcessorData data = new PostProcessorData()
  1029. {
  1030. args = (IntPtr)_self,
  1031. ExecuteCallback = Marshal.GetFunctionPointerForDelegate(_executeHandler),
  1032. };
  1033. Handle = Imports.PostProcessorCustomCreate(context.Handle, ref data);
  1034. if (Handle == UIntPtr.Zero)
  1035. Exception.ThrowLastError();
  1036. }
  1037. protected abstract void doExecute(Char c, Image i);
  1038. protected override void Dispose(bool disposing)
  1039. {
  1040. base.Dispose(disposing);
  1041. _self.Free();
  1042. }
  1043. }
  1044. public class PostProcessorList : PostProcessorCustom, IList<PostProcessor>
  1045. {
  1046. private List<PostProcessor> _items;
  1047. public PostProcessorList(Context context) :
  1048. base(context)
  1049. {
  1050. _items = new List<PostProcessor>();
  1051. }
  1052. protected override void doExecute(Char c, Image i)
  1053. {
  1054. foreach (var item in _items)
  1055. item.Execute(c, i);
  1056. }
  1057. #region IList
  1058. public PostProcessor this[int index]
  1059. {
  1060. get { return _items[index]; }
  1061. set { _items[index] = value; }
  1062. }
  1063. public int Count { get { return _items.Count; } }
  1064. public bool IsReadOnly { get { return false; } }
  1065. public void Add(PostProcessor item)
  1066. { _items.Add(item); }
  1067. public void Clear()
  1068. { _items.Clear(); }
  1069. public bool Contains(PostProcessor item)
  1070. { return _items.Contains(item); }
  1071. public void CopyTo(PostProcessor[] array, int arrayIndex)
  1072. { _items.CopyTo(array, arrayIndex); }
  1073. public IEnumerator<PostProcessor> GetEnumerator()
  1074. { return _items.GetEnumerator(); }
  1075. public int IndexOf(PostProcessor item)
  1076. { return _items.IndexOf(item); }
  1077. public void Insert(int index, PostProcessor item)
  1078. { _items.Insert(index, item); }
  1079. public bool Remove(PostProcessor item)
  1080. { return _items.Remove(item); }
  1081. public void RemoveAt(int index)
  1082. { _items.RemoveAt(index); }
  1083. IEnumerator IEnumerable.GetEnumerator()
  1084. { return _items.GetEnumerator(); }
  1085. #endregion
  1086. }
  1087. public class Font : IDisposable
  1088. {
  1089. private readonly bool _ownsHandle;
  1090. private PostProcessor _postProcessor;
  1091. public Font(FontHandle handle, bool ownsHandle)
  1092. {
  1093. Handle = handle;
  1094. _ownsHandle = ownsHandle;
  1095. }
  1096. ~Font()
  1097. {
  1098. Dispose(false);
  1099. }
  1100. public FontHandle Handle { get; private set; }
  1101. public string Fontname
  1102. {
  1103. get
  1104. {
  1105. var s = Imports.FontGetFontName(Handle);
  1106. if (s == IntPtr.Zero)
  1107. Exception.ThrowLastError();
  1108. return Marshal.PtrToStringUni(s);
  1109. }
  1110. }
  1111. public string Facename
  1112. {
  1113. get
  1114. {
  1115. var s = Imports.FontGetFaceName(Handle);
  1116. if (s == IntPtr.Zero)
  1117. Exception.ThrowLastError();
  1118. return Marshal.PtrToStringUni(s);
  1119. }
  1120. }
  1121. public string Stylename
  1122. {
  1123. get
  1124. {
  1125. var s = Imports.FontGetStyleName(Handle);
  1126. if (s == IntPtr.Zero)
  1127. Exception.ThrowLastError();
  1128. return Marshal.PtrToStringUni(s);
  1129. }
  1130. }
  1131. public string Fullname
  1132. {
  1133. get
  1134. {
  1135. var s = Imports.FontGetFullName(Handle);
  1136. if (s == IntPtr.Zero)
  1137. Exception.ThrowLastError();
  1138. return Marshal.PtrToStringUni(s);
  1139. }
  1140. }
  1141. public FontMetric Metric
  1142. {
  1143. get
  1144. {
  1145. FontMetric value;
  1146. Exception.HandleError(Imports.FontGetMetric(Handle, out value));
  1147. return value;
  1148. }
  1149. }
  1150. public PostProcessor PostProcessor
  1151. {
  1152. get
  1153. {
  1154. return _postProcessor;
  1155. }
  1156. set
  1157. {
  1158. _postProcessor = value;
  1159. Exception.HandleError(Imports.FontSetPostProcessor(Handle, _postProcessor != null ? _postProcessor.Handle : PostProcessorHandle.Zero));
  1160. }
  1161. }
  1162. public Int32 TabWidth
  1163. {
  1164. get
  1165. {
  1166. Int32 value;
  1167. Exception.HandleError(Imports.FontGetTabWidth(Handle, out value));
  1168. return value;
  1169. }
  1170. set
  1171. {
  1172. Exception.HandleError(Imports.FontSetTabWidth(Handle, value));
  1173. }
  1174. }
  1175. public Int32 CharSpacing
  1176. {
  1177. get
  1178. {
  1179. Int32 value;
  1180. Exception.HandleError(Imports.FontGetCharSpacing(Handle, out value));
  1181. return value;
  1182. }
  1183. set
  1184. {
  1185. Exception.HandleError(Imports.FontSetCharSpacing(Handle, value));
  1186. }
  1187. }
  1188. public Int32 LineSpacing
  1189. {
  1190. get
  1191. {
  1192. Int32 value;
  1193. Exception.HandleError(Imports.FontGetLineSpacing(Handle, out value));
  1194. return value;
  1195. }
  1196. set
  1197. {
  1198. Exception.HandleError(Imports.FontSetLineSpacing(Handle, value));
  1199. }
  1200. }
  1201. #region IDisposable Support
  1202. protected virtual void Dispose(bool disposing)
  1203. {
  1204. if (Handle != UIntPtr.Zero && _ownsHandle)
  1205. {
  1206. Imports.FontDestroy(Handle);
  1207. Handle = UIntPtr.Zero;
  1208. }
  1209. }
  1210. public void Dispose()
  1211. {
  1212. Dispose(true);
  1213. GC.SuppressFinalize(this);
  1214. }
  1215. #endregion
  1216. }
  1217. public abstract class FontCreator : IDisposable
  1218. {
  1219. private static readonly Imports.FontCreatorStreamReadHandler _readHandler = new Imports.FontCreatorStreamReadHandler(ReadHandler);
  1220. private static Int32 ReadHandler(IntPtr args, IntPtr buffer, int count)
  1221. {
  1222. GCHandle sHandle = (GCHandle)args;
  1223. var stream = (sHandle.Target as Stream);
  1224. var buf = new byte[count];
  1225. var read = stream.Read(buf, 0, buf.Length);
  1226. Marshal.Copy(buf, 0, buffer, read);
  1227. return read;
  1228. }
  1229. private static readonly Imports.FontCreatorStreamSeekHandler _seekHandler = new Imports.FontCreatorStreamSeekHandler(SeekHandler);
  1230. private static Int32 SeekHandler(IntPtr args, StreamOrigin origin, int offset)
  1231. {
  1232. GCHandle sHandle = (GCHandle)args;
  1233. var stream = (sHandle.Target as Stream);
  1234. switch (origin)
  1235. {
  1236. case StreamOrigin.Begin:
  1237. return (Int32)stream.Seek(offset, SeekOrigin.Begin);
  1238. case StreamOrigin.Current:
  1239. return (Int32)stream.Seek(offset, SeekOrigin.Current);
  1240. case StreamOrigin.End:
  1241. return (Int32)stream.Seek(offset, SeekOrigin.End);
  1242. }
  1243. throw new ArgumentException("invalid stream origin" + origin);
  1244. }
  1245. ~FontCreator()
  1246. {
  1247. Dispose(false);
  1248. }
  1249. public FontCreatorHandle Handle { get; protected set; }
  1250. public Font GetFontByName(string name, Int32 size, FontStyles style, AntiAliasing antiAliasing)
  1251. {
  1252. var handle = Imports.FontCreatorGetFontByName(Handle, name, size, style, antiAliasing);
  1253. if (handle == FontHandle.Zero)
  1254. Exception.ThrowLastError();
  1255. return new Font(handle, true);
  1256. }
  1257. public Font GetFontByFile(string filename, Int32 size, FontStyles style, AntiAliasing antiAliasing)
  1258. {
  1259. var handle = Imports.FontCreatorGetFontByFile(Handle, filename, size, style, antiAliasing);
  1260. if (handle == FontHandle.Zero)
  1261. Exception.ThrowLastError();
  1262. return new Font(handle, true);
  1263. }
  1264. public Font GetFontByStream(Stream stream, Int32 size, FontStyles style, AntiAliasing antiAliasing)
  1265. {
  1266. var streamHandle = GCHandle.Alloc(stream);
  1267. try
  1268. {
  1269. var data = new StreamData()
  1270. {
  1271. args = (IntPtr)streamHandle,
  1272. ReadCallback = Marshal.GetFunctionPointerForDelegate(_readHandler),
  1273. SeekCallback = Marshal.GetFunctionPointerForDelegate(_seekHandler),
  1274. };
  1275. var handle = Imports.FontCreatorGetFontByStream(Handle, ref data, size, style, antiAliasing);
  1276. if (handle == FontHandle.Zero)
  1277. Exception.ThrowLastError();
  1278. return new Font(handle, true);
  1279. }
  1280. finally
  1281. {
  1282. streamHandle.Free();
  1283. }
  1284. }
  1285. #region IDisposable Support
  1286. protected virtual void Dispose(bool disposing)
  1287. {
  1288. if (Handle != UIntPtr.Zero)
  1289. {
  1290. Imports.FontCreatorDestory(Handle);
  1291. Handle = UIntPtr.Zero;
  1292. }
  1293. }
  1294. public void Dispose()
  1295. {
  1296. Dispose(true);
  1297. GC.SuppressFinalize(this);
  1298. }
  1299. #endregion
  1300. }
  1301. public class FontCreatorGdi : FontCreator
  1302. {
  1303. public FontCreatorGdi(Context context) :
  1304. base()
  1305. {
  1306. if (context == null)
  1307. throw new ArgumentNullException("context");
  1308. Handle = Imports.FontCreatorCreate(context.Handle, FontCreatorType.GDI);
  1309. if (Handle == FontHandle.Zero)
  1310. Exception.ThrowLastError();
  1311. }
  1312. }
  1313. public class FontCreatorFreeType : FontCreator
  1314. {
  1315. public FontCreatorFreeType(Context context) :
  1316. base()
  1317. {
  1318. if (context == null)
  1319. throw new ArgumentNullException("context");
  1320. Handle = Imports.FontCreatorCreate(context.Handle, FontCreatorType.FreeType);
  1321. if (Handle == FontHandle.Zero)
  1322. Exception.ThrowLastError();
  1323. }
  1324. }
  1325. public class TextBlock : IDisposable
  1326. {
  1327. Font _font = null;
  1328. private readonly bool _ownsHandle;
  1329. public TextBlock(TextBlockHandle handle, bool ownsHandle)
  1330. {
  1331. Handle = handle;
  1332. _ownsHandle = ownsHandle;
  1333. }
  1334. ~TextBlock()
  1335. {
  1336. Dispose(false);
  1337. }
  1338. public TextBlockHandle Handle { get; private set; }
  1339. public Rect Rect
  1340. {
  1341. get
  1342. {
  1343. Rect value;
  1344. Exception.HandleError(Imports.TextBlockGetRect(Handle, out value));
  1345. return value;
  1346. }
  1347. }
  1348. public Int32 Width
  1349. {
  1350. get
  1351. {
  1352. Int32 value;
  1353. Exception.HandleError(Imports.TextBlockGetWidth(Handle, out value));
  1354. return value;
  1355. }
  1356. }
  1357. public Int32 Height
  1358. {
  1359. get
  1360. {
  1361. Int32 value;
  1362. Exception.HandleError(Imports.TextBlockGetHeight(Handle, out value));
  1363. return value;
  1364. }
  1365. }
  1366. public BlockFlags Flags
  1367. {
  1368. get
  1369. {
  1370. BlockFlags value;
  1371. Exception.HandleError(Imports.TextBlockGetFlags(Handle, out value));
  1372. return value;
  1373. }
  1374. }
  1375. public Int32 Top
  1376. {
  1377. get
  1378. {
  1379. Int32 value;
  1380. Exception.HandleError(Imports.TextBlockGetTop(Handle, out value));
  1381. return value;
  1382. }
  1383. set
  1384. {
  1385. Exception.HandleError(Imports.TextBlockSetTop(Handle, value));
  1386. }
  1387. }
  1388. public Int32 Left
  1389. {
  1390. get
  1391. {
  1392. Int32 value;
  1393. Exception.HandleError(Imports.TextBlockGetLeft(Handle, out value));
  1394. return value;
  1395. }
  1396. set
  1397. {
  1398. Exception.HandleError(Imports.TextBlockSetLeft(Handle, value));
  1399. }
  1400. }
  1401. public VertAlign VertAlign
  1402. {
  1403. get
  1404. {
  1405. VertAlign value;
  1406. Exception.HandleError(Imports.TextBlockGetVertAlign(Handle, out value));
  1407. return value;
  1408. }
  1409. set
  1410. {
  1411. Exception.HandleError(Imports.TextBlockSetVertAlign(Handle, value));
  1412. }
  1413. }
  1414. public HorzAlign HorzAlign
  1415. {
  1416. get
  1417. {
  1418. HorzAlign value;
  1419. Exception.HandleError(Imports.TextBlockGetHorzAlign(Handle, out value));
  1420. return value;
  1421. }
  1422. set
  1423. {
  1424. Exception.HandleError(Imports.TextBlockSetHorzAlign(Handle, value));
  1425. }
  1426. }
  1427. public Clipping Clipping
  1428. {
  1429. get
  1430. {
  1431. Clipping value;
  1432. Exception.HandleError(Imports.TextBlockGetClipping(Handle, out value));
  1433. return value;
  1434. }
  1435. set
  1436. {
  1437. Exception.HandleError(Imports.TextBlockSetClipping(Handle, value));
  1438. }
  1439. }
  1440. public Color Color
  1441. {
  1442. get
  1443. {
  1444. Color value;
  1445. Exception.HandleError(Imports.TextBlockGetColor(Handle, out value));
  1446. return value;
  1447. }
  1448. set
  1449. {
  1450. Exception.HandleError(Imports.TextBlockSetColor(Handle, value));
  1451. }
  1452. }
  1453. public Font Font
  1454. {
  1455. get
  1456. {
  1457. return _font;
  1458. }
  1459. set
  1460. {
  1461. _font = value;
  1462. Exception.HandleError(Imports.TextBlockSetFont(Handle, _font == null ? FontHandle.Zero : _font.Handle));
  1463. }
  1464. }
  1465. public Int32 ActualHeight
  1466. {
  1467. get
  1468. {
  1469. Int32 value = Imports.TextBlockGetActualHeight(Handle);
  1470. if (value < 0)
  1471. Exception.ThrowLastError();
  1472. return value;
  1473. }
  1474. }
  1475. public void TextOut(string text)
  1476. {
  1477. if (text == null)
  1478. throw new ArgumentNullException("text");
  1479. Exception.HandleError(Imports.TextBlockTextOutA(Handle, text));
  1480. }
  1481. Int32 GetTextWidth(string text)
  1482. {
  1483. Int32 value = Imports.TextBlockGetTextWidthA(Handle, text);
  1484. if (value < 0)
  1485. Exception.ThrowLastError();
  1486. return value;
  1487. }
  1488. #region IDisposable Support
  1489. protected virtual void Dispose(bool disposing)
  1490. {
  1491. if (Handle != UIntPtr.Zero && _ownsHandle)
  1492. {
  1493. Imports.TextBlockDestroy(Handle);
  1494. Handle = UIntPtr.Zero;
  1495. }
  1496. }
  1497. public void Dispose()
  1498. {
  1499. Dispose(true);
  1500. GC.SuppressFinalize(this);
  1501. }
  1502. #endregion
  1503. }
  1504. public abstract class Renderer : IDisposable
  1505. {
  1506. ~Renderer()
  1507. {
  1508. Dispose(false);
  1509. }
  1510. public RendererHandle Handle { get; protected set; }
  1511. public TextBlock BeginBlock(Int32 top, Int32 left, Int32 width, Int32 height, BlockFlags flags)
  1512. {
  1513. var tb = Imports.RendererBeginBlock(Handle, top, left, width, height, flags);
  1514. if (tb == TextBlockHandle.Zero)
  1515. Exception.ThrowLastError();
  1516. return new TextBlock(tb, false);
  1517. }
  1518. public void EndBlock(TextBlock block)
  1519. {
  1520. if (block == null)
  1521. return;
  1522. Exception.HandleError(Imports.RendererEndBlock(Handle, block.Handle));
  1523. block = null;
  1524. }
  1525. public void AbortBlock(TextBlock block)
  1526. {
  1527. if (block == null)
  1528. return;
  1529. Exception.HandleError(Imports.RendererAbortBlock(Handle, block.Handle));
  1530. block = null;
  1531. }
  1532. public Int32 GetTextWidth(Font font, string text)
  1533. {
  1534. if (font == null)
  1535. throw new ArgumentNullException("font");
  1536. if (text == null)
  1537. throw new ArgumentNullException("text");
  1538. Int32 value = Imports.RendererGetTextWidthA(Handle, font.Handle, text);
  1539. if (value < 0)
  1540. Exception.ThrowLastError();
  1541. return value;
  1542. }
  1543. #region IDisposable Support
  1544. protected virtual void Dispose(bool disposing)
  1545. {
  1546. if (Handle != UIntPtr.Zero)
  1547. {
  1548. Imports.RendererDestroy(Handle);
  1549. Handle = UIntPtr.Zero;
  1550. }
  1551. }
  1552. public void Dispose()
  1553. {
  1554. Dispose(true);
  1555. GC.SuppressFinalize(this);
  1556. }
  1557. #endregion
  1558. }
  1559. public class RendererOpenGl : Renderer
  1560. {
  1561. public RendererOpenGl(Context context, Format format)
  1562. {
  1563. Handle = Imports.RendererCreate(context.Handle, (Int32)RendererType.OpenGL, (Int32)format);
  1564. if (Handle == RendererHandle.Zero)
  1565. Exception.ThrowLastError();
  1566. }
  1567. }
  1568. }