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.
 
 
 
 
 

169 lines
6.2 KiB

  1. using SharpGL;
  2. using SharpGL.SceneGraph;
  3. using System;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Runtime.InteropServices;
  7. using System.Windows;
  8. namespace libTextSuiteExample
  9. {
  10. public partial class MainWindow : Window
  11. {
  12. private static readonly string TEST_TEXT = @"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
  13. private lts.Context _context;
  14. private lts.FontCreator _fontCreator;
  15. private lts.Renderer _renderer;
  16. private lts.Font _font;
  17. private lts.Image _pattern;
  18. private lts.PostProcessorList _ppList;
  19. private static readonly string LibTextSuiteDllName = "libtextsuite.dll";
  20. [DllImport("kernel32.dll")]
  21. private static extern IntPtr LoadLibrary(string dllToLoad);
  22. public MainWindow()
  23. {
  24. InitializeComponent();
  25. // load libtextsuite depending on architecture
  26. string exeDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  27. string archDir = Environment.Is64BitProcess ? "x64" : "x86";
  28. string dllPath = Path.Combine(exeDir, "resources", archDir, LibTextSuiteDllName);
  29. var handle = LoadLibrary(dllPath);
  30. if (handle == IntPtr.Zero)
  31. {
  32. MessageBox.Show("Unable to load libtextsuite.dll", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
  33. Application.Current.Shutdown();
  34. }
  35. }
  36. private void oglControl_OpenGLInitialized(object sender, OpenGLEventArgs args)
  37. {
  38. var gl = args.OpenGL;
  39. gl.Disable(OpenGL.GL_DEPTH_TEST);
  40. gl.Disable(OpenGL.GLU_CULLING);
  41. gl.Enable(OpenGL.GL_BLEND);
  42. gl.ClearColor(1.0f, 1.0f, 1.0f, 1.0f);
  43. gl.MakeCurrent();
  44. string exeDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
  45. string fontPath = Path.Combine(exeDir, "resources", "Prototype.ttf");
  46. _context = new lts.Context();
  47. _renderer = new lts.RendererOpenGl(_context, lts.Format.RGBA8);
  48. _fontCreator = new lts.FontCreatorGdi(_context);
  49. _font = _fontCreator.GetFontByFile(fontPath, 40, lts.FontStyles.Empty, lts.AntiAliasing.Normal);
  50. _ppList = new lts.PostProcessorList(_context);
  51. _font.PostProcessor = _ppList;
  52. _pattern = new lts.Image(_context);
  53. _pattern.CreateEmpty(lts.Format.Alpha8, 4, 4);
  54. _pattern.Data = new byte[] {
  55. 0xFF, 0xBF, 0x7F, 0xBF,
  56. 0xBF, 0xFF, 0xBF, 0x7F,
  57. 0x7F, 0xBF, 0xFF, 0xBF,
  58. 0xBF, 0x7F, 0xBF, 0xFF
  59. };
  60. lts.PostProcessor pp = new lts.PostProcessorFillPattern(
  61. _context,
  62. _pattern,
  63. new lts.Position() { x = 0, y = 0 },
  64. lts.Constants.ImageModeModulateAll,
  65. lts.Constants.ColorChannelsRGBA);
  66. pp.AddChars(lts.CharRangeUsage.Include, "Lorem");
  67. _ppList.Add(pp);
  68. pp = new lts.PostProcessorFillColor(
  69. _context,
  70. new lts.Color() { R = 0.0f, G = 0.0f, B = 0.5f, A = 1.0f },
  71. lts.Constants.ImageModeReplaceAll,
  72. lts.Constants.ColorChannelsRGB);
  73. pp.AddChars(lts.CharRangeUsage.Exclude, "e");
  74. _ppList.Add(pp);
  75. pp = new lts.PostProcessorBorder(
  76. _context,
  77. 3.0f,
  78. 0.5f,
  79. new lts.Color() { R = 0.0f, G = 0.5f, B = 0.0f, A = 1.0f },
  80. true);
  81. pp.AddChars(lts.CharRangeUsage.Include, "e");
  82. _ppList.Add(pp);
  83. }
  84. private void oglControl_OpenGLDraw(object sender, OpenGLEventArgs args)
  85. {
  86. var gl = args.OpenGL;
  87. gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);
  88. gl.BlendFunc(OpenGL.GL_SRC_ALPHA, OpenGL.GL_ONE_MINUS_SRC_ALPHA);
  89. using (var textBlock = _renderer.BeginBlock(10, 10, (Int32)oglControl.ActualWidth - 20, (Int32)oglControl.ActualHeight - 20, lts.BlockFlags.WordWrap))
  90. {
  91. textBlock.HorzAlign = lts.HorzAlign.Justify;
  92. textBlock.Font = _font;
  93. textBlock.Color = new lts.Color() { R = 1.0f, G = 1.0f, B = 1.0f, A = 1.0f };
  94. textBlock.TextOut(TEST_TEXT);
  95. _renderer.EndBlock(textBlock);
  96. }
  97. gl.Flush();
  98. }
  99. private void oglControl_Resized(object sender, OpenGLEventArgs args)
  100. {
  101. var gl = args.OpenGL;
  102. gl.MatrixMode(OpenGL.GL_PROJECTION);
  103. gl.LoadIdentity();
  104. gl.Ortho(0, gl.RenderContextProvider.Width, gl.RenderContextProvider.Height, 0, 10, -10);
  105. gl.MatrixMode(OpenGL.GL_MODELVIEW);
  106. gl.LoadIdentity();
  107. }
  108. private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  109. {
  110. if (_ppList != null)
  111. {
  112. foreach (var pp in _ppList)
  113. pp.Dispose();
  114. _ppList.Dispose();
  115. _ppList.Clear();
  116. _ppList = null;
  117. }
  118. if (_pattern != null)
  119. {
  120. _pattern.Dispose();
  121. _pattern = null;
  122. }
  123. if (_font != null)
  124. {
  125. _font.Dispose();
  126. _font = null;
  127. }
  128. if (_fontCreator != null)
  129. {
  130. _fontCreator.Dispose();
  131. _fontCreator = null;
  132. }
  133. if (_renderer != null)
  134. {
  135. _renderer.Dispose();
  136. _renderer = null;
  137. }
  138. if (_context != null)
  139. {
  140. _context.Dispose();
  141. _context = null;
  142. }
  143. }
  144. }
  145. }