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.
 
 
 
 
 

179 lines
5.9 KiB

  1. #include <stdio.h>
  2. #include <GL/gl.h>
  3. #include "example.h"
  4. #include "helper.h"
  5. #ifdef LINUX
  6. # include "../../libTextSuite.h"
  7. #else
  8. # include "..\..\libTextSuite.h"
  9. #endif
  10. #if _WIN64
  11. # define LIB_NAME "..\\..\\..\\bin\\x86_64-win64\\libtextsuite.dll"
  12. #elif _WIN32
  13. # define LIB_NAME "..\\..\\..\\bin\\i386-win32\\libtextsuite.dll"
  14. #elif __linux__ && (__amd64 || __x86_64 || _M_AMD64 || __ppc64__)
  15. # define LIB_NAME "../../../bin/x86_64-linux/libtextsuite.so"
  16. #elif __linux__ && (__i386 || _X86_)
  17. # define LIB_NAME "../../../bin/i386-linux/libtextsuite.so"
  18. #else
  19. # error 'unknown operation system'
  20. #endif
  21. #ifdef LINUX
  22. # define FontCreator ltsFontCreatorFreeType
  23. #else
  24. # define FontCreator ltsFontCreatorGDI
  25. #endif
  26. lts_context_handle_t context;
  27. lts_font_creator_handle_t creator;
  28. lts_renderer_handle_t renderer;
  29. lts_font_handle_t font;
  30. lts_post_processor_handle_t pp;
  31. lts_post_processor_handle_t ppFillPattern;
  32. lts_post_processor_handle_t ppFillColor;
  33. lts_post_processor_handle_t ppBorder;
  34. const char* 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.";
  35. const lts_wchar_t CHAR_RANGE_LOREM[] = { 'L', 'o', 'r', 'e', 'm', 0 };
  36. const lts_wchar_t CHAR_RANGE_E[] = { 'e', 0 };
  37. const uint8_t PATTER_DATA[] = {
  38. 0xFF, 0xBF, 0x7F, 0xBF,
  39. 0xBF, 0xFF, 0xBF, 0x7F,
  40. 0x7F, 0xBF, 0xFF, 0xBF,
  41. 0xBF, 0x7F, 0xBF, 0xFF
  42. };
  43. void WINAPI execute_post_proc(lts_char_handle_t charHandle, lts_image_handle_t imageHandle)
  44. {
  45. lts_post_processor_execute(ppFillPattern, charHandle, imageHandle);
  46. lts_post_processor_execute(ppFillColor, charHandle, imageHandle);
  47. lts_post_processor_execute(ppBorder, charHandle, imageHandle);
  48. }
  49. bool exampleInit()
  50. {
  51. if (lts_initialize(LIB_NAME) != ltsErrNone)
  52. {
  53. showMessage("unable to initialize library");
  54. return false;
  55. }
  56. context = lts_context_create();
  57. if (!context)
  58. {
  59. showMessage("unable to create text suite context");
  60. return false;
  61. }
  62. renderer = lts_renderer_create(context, ltsRendererOpenGL, ltsFormatRGBA8);
  63. if (!renderer)
  64. {
  65. showMessage("unable to create text suite renderer");
  66. return false;
  67. }
  68. creator = lts_font_creator_create(context, FontCreator);
  69. if (!creator)
  70. {
  71. showMessage("unable to create text suite creator");
  72. return false;
  73. }
  74. lts_post_processor_custom_data_t ppData;
  75. ppData.args = NULL;
  76. ppData.execute = &execute_post_proc;
  77. pp = lts_post_processor_custom_create(context, &ppData);
  78. if (!pp)
  79. {
  80. showMessage("unable to create custom post processor");
  81. return false;
  82. }
  83. lts_image_handle_t img = lts_image_create(context);
  84. lts_image_create_empty(img, ltsFormatAlpha8, 4, 4);
  85. void* imgData = lts_image_get_data(img);
  86. if (img) memcpy(imgData, &PATTER_DATA[0], 16);
  87. ppFillPattern = lts_post_processor_fill_pattern_create(context, img, true, ltsPosition(0, 0), LTS_IMAGE_MODES_MODULATE_ALL, LTS_COLOR_CHANNELS_RGBA);
  88. lts_post_processor_add_chars(ppFillPattern, ltsUsageInclude, &CHAR_RANGE_LOREM[0]);
  89. ppFillColor = lts_post_processor_fill_color_create(context, ltsColor4f(0.0, 0.0, 0.5, 1.0), LTS_IMAGE_MODES_REPLACE_ALL, LTS_COLOR_CHANNELS_RGB);
  90. lts_post_processor_add_chars(ppFillColor, ltsUsageExclude, &CHAR_RANGE_E[0]);
  91. ppBorder = lts_post_processor_border_create(context, 3.0, 0.5, ltsColor4f(0.0, 0.5, 0.0, 1.0), true);
  92. lts_post_processor_add_chars(ppBorder, ltsUsageInclude, &CHAR_RANGE_E[0]);
  93. font = lts_font_creator_get_font_by_file(creator, "../Prototype.ttf", 40, 0, ltsAANormal);
  94. if (!font)
  95. {
  96. showMessage("unable to create text suite font");
  97. return false;
  98. }
  99. if (lts_font_set_post_processor(font, pp) != ltsErrNone)
  100. {
  101. showMessage("unable to set post processor");
  102. return false;
  103. }
  104. return true;
  105. }
  106. bool exampleRender(int width, int height)
  107. {
  108. const lts_color4f_t color = { 1.0, 1.0, 1.0, 1.0 };
  109. glEnable(GL_BLEND);
  110. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  111. lts_block_flags_t flags = 0;
  112. FLAGS_SET(flags, ltsBlockFlagWordWrap);
  113. lts_text_block_handle_t textBlock = lts_renderer_begin_block(renderer, 10, 10, width-20, height-20, flags);
  114. if (!textBlock)
  115. {
  116. showMessage(lts_get_last_error_msg());
  117. return false;
  118. }
  119. if (lts_text_block_set_horz_align(textBlock, ltsHorzAlignJustify) != ltsErrNone)
  120. {
  121. showMessage("unable to set horizontal alignment");
  122. return false;
  123. }
  124. if (lts_text_block_set_font(textBlock, font) != ltsErrNone)
  125. {
  126. showMessage("unable to set font");
  127. return false;
  128. }
  129. if (lts_text_block_set_color(textBlock, color) != ltsErrNone)
  130. {
  131. showMessage("unable to set font");
  132. return false;
  133. }
  134. if (lts_text_block_text_out_a(textBlock, TEST_TEXT) != ltsErrNone)
  135. {
  136. showMessage("unable to print text");
  137. return false;
  138. }
  139. if (lts_renderer_end_block(renderer, textBlock) != ltsErrNone)
  140. {
  141. showMessage("unable to finish text block");
  142. return false;
  143. }
  144. //lts_text_block_destroy(textBlock);
  145. glDisable(GL_BLEND);
  146. return true;
  147. }
  148. void exampleFinish()
  149. {
  150. lts_font_destroy(font);
  151. lts_post_processor_destroy(ppBorder);
  152. lts_post_processor_destroy(ppFillColor);
  153. lts_post_processor_destroy(ppFillPattern);
  154. lts_post_processor_destroy(pp);
  155. lts_font_creator_destroy(creator);
  156. lts_renderer_destroy(creator);
  157. lts_context_destroy(context);
  158. lts_finalize();
  159. }