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.
 
 
 
 
 

941 rivejä
46 KiB

  1. #ifndef LIB_TEXT_SUITE_HPP
  2. #define LIB_TEXT_SUITE_HPP
  3. #include <string>
  4. #include <stdint.h>
  5. #include <initializer_list>
  6. #if defined(WIN32) || defined(WIN64) || defined(_WIN32) || defined(_WIN64)
  7. # define LTS_WINDOWS
  8. # include <windows.h>
  9. #elif LINUX || __linux__
  10. # define LTS_LINUX
  11. # include <dlfcn.h>
  12. #else
  13. # error "unknown operation system"
  14. #endif
  15. #if __MINGW32__
  16. # define WINAPI __stdcall
  17. #else
  18. # define WINAPI
  19. #endif
  20. /**********************************************************************************************************************************/
  21. /* public interface */
  22. /**********************************************************************************************************************************/
  23. namespace lts
  24. {
  25. /* enumerations *******************************************************************************************************************/
  26. enum class ErrorCode : int32_t
  27. {
  28. Unknown = -1,
  29. None = 0,
  30. // misc
  31. NotInitialized = 1,
  32. InvalidEnum = 2,
  33. InvalidValue = 3,
  34. InvalidOperation = 4,
  35. InvalidType = 5,
  36. // invalid handles
  37. InvalidContextHandle = 100,
  38. InvalidRendererHandle = 101,
  39. InvalidTextBlockHandle = 102,
  40. InvalidFontHandle = 103,
  41. InvalidFontCreatorHandle = 104,
  42. InvalidImageHandle = 105,
  43. InvalidPostProcHandle = 106,
  44. // library
  45. InvalidLibName = 200,
  46. InvalidLibHandle = 201,
  47. InvalidMethodName = 202
  48. };
  49. static_assert(sizeof(ErrorCode) == 4, "size of lts::ErrorCode should be 4");
  50. enum class CodePage : uint32_t
  51. {
  52. cpUTF8,
  53. cpISO_8859_1,
  54. cpISO_8859_2,
  55. cpISO_8859_3,
  56. cpISO_8859_4,
  57. cpISO_8859_5,
  58. cpISO_8859_6,
  59. cpISO_8859_7,
  60. cpISO_8859_8,
  61. cpISO_8859_9,
  62. cpISO_8859_10,
  63. cpISO_8859_11,
  64. cpISO_8859_13,
  65. cpISO_8859_14,
  66. cpISO_8859_15,
  67. cpISO_8859_16,
  68. cpISO_037,
  69. cpISO_437,
  70. cpISO_500,
  71. cpISO_737,
  72. cpISO_775,
  73. cpISO_850,
  74. cpISO_852,
  75. cpISO_855,
  76. cpISO_857,
  77. cpISO_860,
  78. cpISO_861,
  79. cpISO_862,
  80. cpISO_863,
  81. cpISO_864,
  82. cpISO_865,
  83. cpISO_866,
  84. cpISO_869,
  85. cpISO_874,
  86. cpISO_875,
  87. cpISO_1026,
  88. cpISO_1250,
  89. cpISO_1251,
  90. cpISO_1252,
  91. cpISO_1253,
  92. cpISO_1254,
  93. cpISO_1255,
  94. cpISO_1256,
  95. cpISO_1257,
  96. cpISO_1258
  97. };
  98. static_assert(sizeof(CodePage) == 4, "size of lts::CodePage should be 4");
  99. enum class Format : uint32_t
  100. {
  101. Empty,
  102. RGBA8,
  103. LumAlpha8,
  104. Alpha8,
  105. Lum8
  106. };
  107. static_assert(sizeof(Format) == 4, "size of lts::Format should be 4");
  108. enum class RendererType : uint32_t
  109. {
  110. Unknown,
  111. OpenGL,
  112. OpenGLES
  113. };
  114. static_assert(sizeof(RendererType) == 4, "size of lts::RendererType should be 4");
  115. enum class FontCreatorType : uint32_t
  116. {
  117. Unknown,
  118. FreeType,
  119. GDI,
  120. Custom
  121. };
  122. static_assert(sizeof(FontCreatorType) == 4, "size of lts::FontCreatorType should be 4");
  123. enum class VertAlign : uint32_t
  124. {
  125. Top,
  126. Center,
  127. Bottom
  128. };
  129. static_assert(sizeof(VertAlign) == 4, "size of lts::VertAlign should be 4");
  130. enum class HorzAlign : uint32_t
  131. {
  132. Left,
  133. Center,
  134. Right,
  135. Justify
  136. };
  137. static_assert(sizeof(HorzAlign) == 4, "size of lts::HorzAlign should be 4");
  138. enum class Clipping : uint32_t
  139. {
  140. None,
  141. WordBorder,
  142. CharBorder,
  143. WordComplete,
  144. CharComplete
  145. };
  146. static_assert(sizeof(Clipping) == 4, "size of lts::Clipping should be 4");
  147. enum class AntiAliasing : uint32_t
  148. {
  149. None,
  150. Normal
  151. };
  152. static_assert(sizeof(AntiAliasing) == 4, "size of lts::AntiAliasing should be 4");
  153. enum class CharRangeUsage : uint32_t
  154. {
  155. Include,
  156. Exclude
  157. };
  158. static_assert(sizeof(CharRangeUsage) == 4, "size of lts::CharRangeUsage should be 4");
  159. enum class ImageMode : uint32_t
  160. {
  161. Ignore,
  162. Replace,
  163. Modulate
  164. };
  165. static_assert(sizeof(ImageMode) == 4, "size of lts::ImageMode should be 4");
  166. /* flags **************************************************************************************************************************/
  167. template <typename TEnum, typename TBase>
  168. class Flags
  169. {
  170. private:
  171. TBase _value;
  172. public:
  173. inline bool isSet(const TEnum& e) const
  174. { return (_value & (1 << e)); };
  175. inline void set(const TEnum& e)
  176. { _value |= (1 << e); };
  177. inline void unset(const TEnum& e)
  178. { _value &= ~(1 << e); };
  179. inline void clear()
  180. { _value = 0; };
  181. Flags() :
  182. _value(0)
  183. { };
  184. Flags(const TEnum& e) :
  185. _value(1 << e)
  186. { };
  187. Flags(const std::initializer_list<TEnum>& vec) :
  188. _value(0)
  189. {
  190. for (const auto& e : vec)
  191. set(e);
  192. };
  193. template <typename T>
  194. Flags(T begIt, T endIt) :
  195. _value(0)
  196. {
  197. for (auto i = begIt; i != endIt; ++i)
  198. set(*i);
  199. };
  200. };
  201. enum class BlockFlag
  202. {
  203. WordWrap
  204. };
  205. typedef Flags<BlockFlag, uint32_t> BlockFlags;
  206. enum class FontStyle
  207. {
  208. Bold,
  209. Italic,
  210. Underline,
  211. Strikeout,
  212. };
  213. typedef Flags<FontStyle, uint32_t> FontStyles;
  214. enum class ColorChannel
  215. {
  216. Red,
  217. Green,
  218. Blue,
  219. Alpha
  220. };
  221. typedef Flags<ColorChannel, uint32_t> ColorChannels;
  222. /* structures *********************************************************************************************************************/
  223. struct Position
  224. {
  225. int x;
  226. int y;
  227. };
  228. union Rect
  229. {
  230. struct { Position top_left, bottom_right; };
  231. struct { int32_t left, top, right, bottom; };
  232. };
  233. union Color4f
  234. {
  235. struct { float r, g, b, a; };
  236. float arr[4];
  237. };
  238. struct GlyphMetric
  239. {
  240. Position glyphOrigin;
  241. Rect glyphRect;
  242. int32_t advance;
  243. };
  244. struct TextMetric
  245. {
  246. int32_t ascent;
  247. int32_t descent;
  248. int32_t externalLeading;
  249. int32_t baseLineOffset;
  250. int32_t charSpacing;
  251. int32_t lineHeight;
  252. int32_t lineSpacing;
  253. };
  254. struct FontMetric
  255. {
  256. int32_t size;
  257. FontStyles styles;
  258. AntiAliasing antiAliasing;
  259. wchar_t defaultChar;
  260. uint8_t __reserved[2];
  261. int32_t ascent;
  262. int32_t descent;
  263. int32_t externalLeading;
  264. int32_t baseLineOffset;
  265. int32_t underlinePos;
  266. int32_t underlineSize;
  267. int32_t strikeoutPos;
  268. int32_t strikeoutSize;
  269. };
  270. typedef float Vector4f[4];
  271. typedef Vector4f Matrix4f[4];
  272. typedef ImageMode ImageModes[4];
  273. typedef void* Handle;
  274. typedef Handle ContextHandle;
  275. typedef Handle RendererHandle;
  276. typedef Handle TextBlockHandle;
  277. typedef Handle FontCreatorHandle;
  278. typedef Handle FontHandle;
  279. typedef Handle PostProcessorHandle;
  280. typedef Handle ImageHandle;
  281. typedef Handle CharHandle;
  282. class Exception : public std::exception
  283. {
  284. private:
  285. std::string _message;
  286. ErrorCode _errorCode;
  287. public:
  288. ErrorCode getErrorCode() const;
  289. std::string getMessage() const;
  290. virtual const char* what() const throw();
  291. Exception(std::string message, ErrorCode errorCode = ErrorCode::Unknown);
  292. ~Exception() throw();
  293. };
  294. class Library
  295. {
  296. public:
  297. #if defined(LTS_WINDOWS)
  298. typedef HMODULE Handle; //!< shader file handle
  299. #elif defined(LTS_LINUX)
  300. typedef void* Handle; //!< shader file handle
  301. #else
  302. #error "unknown operation system"
  303. #endif
  304. private:
  305. struct Impl;
  306. Impl* _impl;
  307. Handle _handle;
  308. public:
  309. ErrorCode getLastErrorCode() const;
  310. std::string getLastErrorMsg() const;
  311. Library(const std::string& libName);
  312. ~Library();
  313. private:
  314. Library(const Library& that) { };
  315. };
  316. }
  317. /**********************************************************************************************************************************/
  318. /* private implementation */
  319. /**********************************************************************************************************************************/
  320. #if defined(LTS_WINDOWS)
  321. lts::Library::Handle libOpen(const char* name)
  322. { return LoadLibrary(name); };
  323. template <typename T>
  324. T getAddr(lts::Library::Handle handle, const char* name)
  325. { return reinterpret_cast<T>(GetProcAddress(handle, name)); };
  326. int libClose(lts::Library::Handle handle)
  327. { return FreeLibrary(handle); };
  328. #elif defined(LTS_LINUX)
  329. lts::Library::Handle libOpen(const char* name)
  330. { return dlopen(name, RTLD_LAZY); };
  331. template <typename T>
  332. T getAddr(lts::Library::Handle handle, const char* name)
  333. { return reinterpret_cast<T>(dlsym(handle, name)); };
  334. int libClose(lts::Library::Handle handle)
  335. { return !dlclose(handle); };
  336. #else
  337. # error "unknown operation system"
  338. #endif
  339. /* Library::Impl ******************************************************************************************************************/
  340. struct lts::Library::Impl
  341. {
  342. public: /* stream */
  343. struct StreamData
  344. {
  345. enum class Origin : uint32_t
  346. {
  347. Begin = 0,
  348. Current = 1,
  349. End = 2
  350. };
  351. static_assert(sizeof(Origin) == 4, "size of lts::StreamOrigin should be 4");
  352. typedef int (WINAPI *stream_read_t)(void* args, void* buffer, int count);
  353. typedef int (WINAPI *stream_seek_t)(void* args, Origin origin, int offset);
  354. void* args;
  355. stream_read_t read;
  356. stream_seek_t seek;
  357. };
  358. public: /* custom post processor */
  359. struct PostProcessorData
  360. {
  361. typedef void (WINAPI *post_processor_execute_t)(lts::CharHandle charHandle, lts::ImageHandle imageHandle);
  362. void* args;
  363. post_processor_execute_t execute;
  364. };
  365. public: /* custom renderer */
  366. struct RendererCustomData
  367. {
  368. typedef void (WINAPI *renderer_begin_render_t) (void* args);
  369. typedef void (WINAPI *renderer_end_render_t) (void* args);
  370. typedef lts::Position (WINAPI *renderer_get_draw_pos_t) (void* args);
  371. typedef void (WINAPI *renderer_set_draw_pos_t) (lts::Position value, void* args);
  372. typedef void (WINAPI *renderer_move_draw_pos_t) (lts::Position value, void* args);
  373. typedef void (WINAPI *renderer_set_color_t) (lts::Color4f value, void* args);
  374. typedef void (WINAPI *renderer_render) (void* ref, int forcedWidth, void* args);
  375. typedef void* (WINAPI *renderer_create_ref) (lts::CharHandle charHandle, lts::ImageHandle imageHandle, void* args);
  376. typedef void (WINAPI *renderer_free_ref) (void* ref, void* args);
  377. void* args;
  378. renderer_begin_render_t beginRender;
  379. renderer_end_render_t endRender;
  380. renderer_get_draw_pos_t getDrawPos;
  381. renderer_set_draw_pos_t setDrawPos;
  382. renderer_move_draw_pos_t moveDrawPos;
  383. renderer_set_color_t setColor;
  384. renderer_render render;
  385. renderer_create_ref createRef;
  386. renderer_free_ref freeRef;
  387. };
  388. public: /* callbacks */
  389. typedef void (WINAPI *image_load_callback_t) (lts::ImageHandle handle, int x, int y, lts::Color4f pixel, void* args);
  390. typedef void (WINAPI *image_blend_callback_t)(lts::ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args);
  391. typedef lts::ContextHandle (WINAPI *context_create_t) ();
  392. typedef lts::ErrorCode (WINAPI *context_get_code_page_t) (lts::ContextHandle handle, lts::CodePage* value);
  393. typedef lts::ErrorCode (WINAPI *context_get_default_char_t) (lts::ContextHandle handle, wchar_t& value);
  394. typedef lts::ErrorCode (WINAPI *context_set_code_page_t) (lts::ContextHandle handle, lts::CodePage value);
  395. typedef lts::ErrorCode (WINAPI *context_set_default_char_t) (lts::ContextHandle handle, wchar_t value);
  396. typedef wchar_t* (WINAPI *context_ansi_to_wide_t) (lts::ContextHandle handle, const char* text);
  397. typedef lts::ErrorCode (WINAPI *context_destroy_t) (lts::ContextHandle handle);
  398. typedef lts::RendererHandle (WINAPI *renderer_create_t) (lts::ContextHandle handle, lts::RendererType type, lts::Format format);
  399. typedef lts::RendererHandle (WINAPI *renderer_create_custom_t) (lts::ContextHandle handle, lts::Format format, const RendererCustomData& data);
  400. typedef lts::TextBlockHandle (WINAPI *renderer_begin_block_t) (lts::RendererHandle handle, int top, int left, int width, int height, lts::BlockFlags flags);
  401. typedef lts::ErrorCode (WINAPI *renderer_end_block_t) (lts::RendererHandle handle, lts::TextBlockHandle block);
  402. typedef lts::ErrorCode (WINAPI *renderer_abort_block_t) (lts::RendererHandle handle, lts::TextBlockHandle block);
  403. typedef int (WINAPI *renderer_get_text_width_a_t) (lts::RendererHandle handle, lts::FontHandle font, const char* text);
  404. typedef int (WINAPI *renderer_get_text_width_w_t) (lts::RendererHandle handle, lts::FontHandle font, const wchar_t* text);
  405. typedef lts::ErrorCode (WINAPI *renderer_destroy_t) (lts::RendererHandle handle);
  406. typedef lts::FontCreatorHandle (WINAPI *font_creator_create_t) (lts::ContextHandle handle, lts::FontCreatorType type);
  407. typedef lts::FontHandle (WINAPI *font_creator_get_font_by_name_t) (lts::FontCreatorHandle handle, const char* fontname, int size, lts::FontStyles style, lts::AntiAliasing antialiasing);
  408. typedef lts::FontHandle (WINAPI *font_creator_get_font_by_file_t) (lts::FontCreatorHandle handle, const char* filename, int size, lts::FontStyles style, lts::AntiAliasing antialiasing);
  409. typedef lts::FontHandle (WINAPI *font_creator_get_font_by_stream_t) (lts::FontCreatorHandle handle, StreamData* stream, int size, lts::FontStyles style, lts::AntiAliasing antialiasing);
  410. typedef lts::ErrorCode (WINAPI *font_creator_destroy_t) (lts::FontCreatorHandle handle);
  411. typedef lts::PostProcessorHandle(WINAPI *font_get_post_processor_t) (lts::FontHandle handle);
  412. typedef lts::ErrorCode (WINAPI *font_get_tab_width_t) (lts::FontHandle handle, int& value);
  413. typedef lts::ErrorCode (WINAPI *font_get_char_spacing_t) (lts::FontHandle handle, int& value);
  414. typedef lts::ErrorCode (WINAPI *font_get_line_spacing_t) (lts::FontHandle handle, float& value);
  415. typedef lts::ErrorCode (WINAPI *font_get_metric_t) (lts::FontHandle handle, lts::FontMetric& value);
  416. typedef const char* (WINAPI *font_get_fontname_t) (lts::FontHandle handle);
  417. typedef const char* (WINAPI *font_get_facename_t) (lts::FontHandle handle);
  418. typedef const char* (WINAPI *font_get_stylename_t) (lts::FontHandle handle);
  419. typedef const char* (WINAPI *font_get_fillname_t) (lts::FontHandle handle);
  420. typedef const char* (WINAPI *font_get_copyright_t) (lts::FontHandle handle);
  421. typedef lts::ErrorCode (WINAPI *font_set_post_processor_t) (lts::FontHandle handle, lts::PostProcessorHandle pp);
  422. typedef lts::ErrorCode (WINAPI *font_set_tab_width_t) (lts::FontHandle handle, int value);
  423. typedef lts::ErrorCode (WINAPI *font_set_char_spacing_t) (lts::FontHandle handle, int value);
  424. typedef lts::ErrorCode (WINAPI *font_set_line_spacing_t) (lts::FontHandle handle, float value);
  425. typedef lts::ErrorCode (WINAPI *font_destroy_t) (lts::FontHandle handle);
  426. typedef lts::ErrorCode (WINAPI *text_block_get_rect_t) (lts::TextBlockHandle handle, lts::Rect& value);
  427. typedef lts::ErrorCode (WINAPI *text_block_get_width_t) (lts::TextBlockHandle handle, int& value);
  428. typedef lts::ErrorCode (WINAPI *text_block_get_height_t) (lts::TextBlockHandle handle, int& value);
  429. typedef lts::ErrorCode (WINAPI *text_block_get_flags_t) (lts::TextBlockHandle handle, lts::BlockFlags& value);
  430. typedef lts::ErrorCode (WINAPI *text_block_get_top_t) (lts::TextBlockHandle handle, int& value);
  431. typedef lts::ErrorCode (WINAPI *text_block_get_left_t) (lts::TextBlockHandle handle, int& value);
  432. typedef lts::ErrorCode (WINAPI *text_block_get_vert_align_t) (lts::TextBlockHandle handle, lts::VertAlign& value);
  433. typedef lts::ErrorCode (WINAPI *text_block_get_horz_align_t) (lts::TextBlockHandle handle, lts::HorzAlign& value);
  434. typedef lts::ErrorCode (WINAPI *text_block_get_clipping_t) (lts::TextBlockHandle handle, lts::Clipping& value);
  435. typedef lts::ErrorCode (WINAPI *text_block_get_color_t) (lts::TextBlockHandle handle, lts::Color4f& value);
  436. typedef lts::ErrorCode (WINAPI *text_block_get_font_t) (lts::TextBlockHandle handle, lts::FontHandle& value);
  437. typedef lts::ErrorCode (WINAPI *text_block_set_top_t) (lts::TextBlockHandle handle, int value);
  438. typedef lts::ErrorCode (WINAPI *text_block_set_left_t) (lts::TextBlockHandle handle, int value);
  439. typedef lts::ErrorCode (WINAPI *text_block_set_vert_align_t) (lts::TextBlockHandle handle, lts::VertAlign value);
  440. typedef lts::ErrorCode (WINAPI *text_block_set_horz_align_t) (lts::TextBlockHandle handle, lts::HorzAlign value);
  441. typedef lts::ErrorCode (WINAPI *text_block_set_clipping_t) (lts::TextBlockHandle handle, lts::Clipping value);
  442. typedef lts::ErrorCode (WINAPI *text_block_set_color_t) (lts::TextBlockHandle handle, lts::Color4f value);
  443. typedef lts::ErrorCode (WINAPI *text_block_set_font_t) (lts::TextBlockHandle handle, lts::FontHandle value);
  444. typedef int (WINAPI *text_block_get_actual_height_t) (lts::TextBlockHandle handle);
  445. typedef int (WINAPI *text_block_get_text_width_a_t) (lts::TextBlockHandle handle, const char* text);
  446. typedef int (WINAPI *text_block_get_text_width_w_t) (lts::TextBlockHandle handle, const wchar_t* text);
  447. typedef lts::ErrorCode (WINAPI *text_block_text_out_a_t) (lts::TextBlockHandle handle, const char* text);
  448. typedef lts::ErrorCode (WINAPI *text_block_text_out_w_t) (lts::TextBlockHandle handle, const wchar_t* text);
  449. typedef lts::ErrorCode (WINAPI *text_block_destroy_t) (lts::TextBlockHandle handle);
  450. typedef lts::ImageHandle (WINAPI *image_create_t) (lts::ContextHandle handle);
  451. typedef lts::ErrorCode (WINAPI *image_is_empty_t) (lts::ImageHandle handle, bool& value);
  452. typedef int (WINAPI *image_get_width_t) (lts::ImageHandle handle);
  453. typedef int (WINAPI *image_get_height_t) (lts::ImageHandle handle);
  454. typedef int (WINAPI *image_get_line_size_t) (lts::ImageHandle handle);
  455. typedef int (WINAPI *image_get_data_size_t) (lts::ImageHandle handle);
  456. typedef lts::ErrorCode (WINAPI *image_get_format_t) (lts::ImageHandle handle, lts::Format& value);
  457. typedef void* (WINAPI *image_get_data_t) (lts::ImageHandle handle);
  458. typedef void* (WINAPI *image_get_scanline_t) (lts::ImageHandle handle, int index);
  459. typedef lts::ErrorCode (WINAPI *image_get_pixel_at_t) (lts::ImageHandle handle, int x, int y, lts::Color4f pixel);
  460. typedef lts::ErrorCode (WINAPI *image_assign_t) (lts::ImageHandle handle, lts::ImageHandle source);
  461. typedef lts::ErrorCode (WINAPI *image_create_empty_t) (lts::ImageHandle handle, lts::Format format, int width, int height);
  462. typedef lts::ErrorCode (WINAPI *image_load_from_func_t) (lts::ImageHandle handle, image_load_callback_t callback, void* args);
  463. typedef lts::ErrorCode (WINAPI *image_resize_t) (lts::ImageHandle handle, int width, int height, int x, int y);
  464. typedef lts::ErrorCode (WINAPI *image_fill_color_t) (lts::ImageHandle handle, lts::Color4f color, lts::ColorChannels mask, lts::ImageModes modes);
  465. typedef lts::ErrorCode (WINAPI *image_fill_pattern_t) (lts::ImageHandle handle, lts::ImageHandle pattern, int x, int y, lts::ColorChannels mask, lts::ImageModes modes);
  466. typedef lts::ErrorCode (WINAPI *image_blend_t) (lts::ImageHandle handle, lts::ImageHandle source, int x, int y, image_blend_callback_t callback, void* args);
  467. typedef lts::ErrorCode (WINAPI *image_blur_t) (lts::ImageHandle handle, float horzRad, float horzStr, float vertRad, float vertStr, lts::ColorChannels mask);
  468. typedef lts::ErrorCode (WINAPI *image_destroy_t) (lts::ImageHandle handle);
  469. typedef lts::ErrorCode (WINAPI *post_processor_add_range_t) (lts::PostProcessorHandle handle, lts::CharRangeUsage usage, wchar_t start, wchar_t stop);
  470. typedef lts::ErrorCode (WINAPI *post_processor_add_chars_t) (lts::PostProcessorHandle handle, lts::CharRangeUsage usage, const wchar_t* chars);
  471. typedef lts::ErrorCode (WINAPI *post_processor_clear_ranges_t) (lts::PostProcessorHandle handle);
  472. typedef lts::ErrorCode (WINAPI *post_processor_execute_t) (lts::PostProcessorHandle handle, lts::CharHandle charHandle, lts::ImageHandle image);
  473. typedef lts::PostProcessorHandle(WINAPI *post_processor_fill_color_create_t) (lts::PostProcessorHandle handle, lts::Color4f color, lts::ImageModes modes, lts::ColorChannels channels);
  474. typedef lts::PostProcessorHandle(WINAPI *post_processor_fill_pattern_create_t) (lts::PostProcessorHandle handle, lts::ImageHandle pattern, bool ownsPattern, lts::Position position, lts::ImageModes modes, lts::ColorChannels channels);
  475. typedef lts::PostProcessorHandle(WINAPI *post_processor_border_create_t) (lts::PostProcessorHandle handle, float width, float strength, lts::Color4f color, bool keepSize);
  476. typedef lts::PostProcessorHandle(WINAPI *post_processor_shadow_create_t) (lts::PostProcessorHandle handle, float radius, float strength, lts::Position offset, lts::Color4f color);
  477. typedef lts::PostProcessorHandle(WINAPI *post_processor_custom_create_t) (lts::PostProcessorHandle handle, const PostProcessorData& data);
  478. typedef lts::ErrorCode (WINAPI *post_processor_destroy_t) (lts::PostProcessorHandle handle);
  479. typedef lts::ErrorCode (WINAPI *char_get_char_code_t) (lts::CharHandle handle, wchar_t* value);
  480. typedef lts::ErrorCode (WINAPI *char_get_glyph_metric_t) (lts::CharHandle handle, lts::GlyphMetric& value);
  481. typedef lts::ErrorCode (WINAPI *char_set_glyph_metric_t) (lts::CharHandle handle, lts::GlyphMetric value);
  482. typedef lts::ErrorCode (WINAPI *initialize_t) ();
  483. typedef lts::ErrorCode (WINAPI *get_last_error_code_t) ();
  484. typedef const char* (WINAPI *get_last_error_msg_t) ();
  485. typedef lts::ErrorCode (WINAPI *finalize_t) ();
  486. private:
  487. lts::Library::Handle _handle;
  488. template <typename T>
  489. inline void loadProc(T& proc, const char* name)
  490. {
  491. proc = getAddr<T>(_handle, name);
  492. if (!proc)
  493. throw lts::Exception(std::string("unable to load method from library: ") + name, lts::ErrorCode::InvalidMethodName);
  494. }
  495. public:
  496. context_create_t context_create;
  497. context_get_code_page_t context_get_code_page;
  498. context_get_default_char_t context_get_default_char;
  499. context_set_code_page_t context_set_code_page;
  500. context_set_default_char_t context_set_default_char;
  501. context_ansi_to_wide_t context_ansi_to_wide;
  502. context_destroy_t context_destroy;
  503. renderer_create_t renderer_create;
  504. renderer_create_custom_t renderer_create_custom;
  505. renderer_begin_block_t renderer_begin_block;
  506. renderer_end_block_t renderer_end_block;
  507. renderer_abort_block_t renderer_abort_block;
  508. renderer_get_text_width_a_t renderer_get_text_width_a;
  509. renderer_get_text_width_w_t renderer_get_text_width_w;
  510. renderer_destroy_t renderer_destroy;
  511. font_creator_create_t font_creator_create;
  512. font_creator_get_font_by_name_t font_creator_get_font_by_name;
  513. font_creator_get_font_by_file_t font_creator_get_font_by_file;
  514. font_creator_get_font_by_stream_t font_creator_get_font_by_stream;
  515. font_creator_destroy_t font_creator_destroy;
  516. font_get_post_processor_t font_get_post_processor;
  517. font_get_tab_width_t font_get_tab_width;
  518. font_get_char_spacing_t font_get_char_spacing;
  519. font_get_line_spacing_t font_get_line_spacing;
  520. font_get_metric_t font_get_metric;
  521. font_get_fontname_t font_get_fontname;
  522. font_get_facename_t font_get_facename;
  523. font_get_stylename_t font_get_stylename;
  524. font_get_fillname_t font_get_fillname;
  525. font_get_copyright_t font_get_copyright;
  526. font_set_post_processor_t font_set_post_processor;
  527. font_set_tab_width_t font_set_tab_width;
  528. font_set_char_spacing_t font_set_char_spacing;
  529. font_set_line_spacing_t font_set_line_spacing;
  530. font_destroy_t font_destroy;
  531. text_block_get_rect_t text_block_get_rect;
  532. text_block_get_width_t text_block_get_width;
  533. text_block_get_height_t text_block_get_height;
  534. text_block_get_flags_t text_block_get_flags;
  535. text_block_get_top_t text_block_get_top;
  536. text_block_get_left_t text_block_get_left;
  537. text_block_get_vert_align_t text_block_get_vert_align;
  538. text_block_get_horz_align_t text_block_get_horz_align;
  539. text_block_get_clipping_t text_block_get_clipping;
  540. text_block_get_color_t text_block_get_color;
  541. text_block_get_font_t text_block_get_font;
  542. text_block_set_top_t text_block_set_top;
  543. text_block_set_left_t text_block_set_left;
  544. text_block_set_vert_align_t text_block_set_vert_align;
  545. text_block_set_horz_align_t text_block_set_horz_align;
  546. text_block_set_clipping_t text_block_set_clipping;
  547. text_block_set_color_t text_block_set_color;
  548. text_block_set_font_t text_block_set_font;
  549. text_block_get_actual_height_t text_block_get_actual_height;
  550. text_block_get_text_width_a_t text_block_get_text_width_a;
  551. text_block_get_text_width_w_t text_block_get_text_width_w;
  552. text_block_text_out_a_t text_block_text_out_a;
  553. text_block_text_out_w_t text_block_text_out_w;
  554. text_block_destroy_t text_block_destroy;
  555. image_create_t image_create;
  556. image_is_empty_t image_is_empty;
  557. image_get_width_t image_get_width;
  558. image_get_height_t image_get_height;
  559. image_get_line_size_t image_get_line_size;
  560. image_get_data_size_t image_get_data_size;
  561. image_get_format_t image_get_format;
  562. image_get_data_t image_get_data;
  563. image_get_scanline_t image_get_scanline;
  564. image_get_pixel_at_t image_get_pixel_at;
  565. image_assign_t image_assign;
  566. image_create_empty_t image_create_empty;
  567. image_load_from_func_t image_load_from_func;
  568. image_resize_t image_resize;
  569. image_fill_color_t image_fill_color;
  570. image_fill_pattern_t image_fill_pattern;
  571. image_blend_t image_blend;
  572. image_blur_t image_blur;
  573. image_destroy_t image_destroy;
  574. post_processor_add_range_t post_processor_add_range;
  575. post_processor_add_chars_t post_processor_add_chars;
  576. post_processor_clear_ranges_t post_processor_clear_ranges;
  577. post_processor_execute_t post_processor_execute;
  578. post_processor_fill_color_create_t post_processor_fill_color_create;
  579. post_processor_fill_pattern_create_t post_processor_fill_pattern_create;
  580. post_processor_border_create_t post_processor_border_create;
  581. post_processor_shadow_create_t post_processor_shadow_create;
  582. post_processor_custom_create_t post_processor_custom_create;
  583. post_processor_destroy_t post_processor_destroy;
  584. char_get_char_code_t char_get_char_code;
  585. char_get_glyph_metric_t char_get_glyph_metric;
  586. char_set_glyph_metric_t char_set_glyph_metric;
  587. initialize_t initialize;
  588. get_last_error_code_t get_last_error_code;
  589. get_last_error_msg_t get_last_error_msg;
  590. finalize_t finalize;
  591. public:
  592. Impl(lts::Library::Handle handle) :
  593. _handle (handle),
  594. context_create (NULL),
  595. context_get_code_page (NULL),
  596. context_get_default_char (NULL),
  597. context_set_code_page (NULL),
  598. context_set_default_char (NULL),
  599. context_ansi_to_wide (NULL),
  600. context_destroy (NULL),
  601. renderer_create (NULL),
  602. renderer_create_custom (NULL),
  603. renderer_begin_block (NULL),
  604. renderer_end_block (NULL),
  605. renderer_abort_block (NULL),
  606. renderer_get_text_width_a (NULL),
  607. renderer_get_text_width_w (NULL),
  608. renderer_destroy (NULL),
  609. font_creator_create (NULL),
  610. font_creator_get_font_by_name (NULL),
  611. font_creator_get_font_by_file (NULL),
  612. font_creator_get_font_by_stream (NULL),
  613. font_creator_destroy (NULL),
  614. font_get_post_processor (NULL),
  615. font_get_tab_width (NULL),
  616. font_get_char_spacing (NULL),
  617. font_get_line_spacing (NULL),
  618. font_get_metric (NULL),
  619. font_get_fontname (NULL),
  620. font_get_facename (NULL),
  621. font_get_stylename (NULL),
  622. font_get_fillname (NULL),
  623. font_get_copyright (NULL),
  624. font_set_post_processor (NULL),
  625. font_set_tab_width (NULL),
  626. font_set_char_spacing (NULL),
  627. font_set_line_spacing (NULL),
  628. font_destroy (NULL),
  629. text_block_get_rect (NULL),
  630. text_block_get_width (NULL),
  631. text_block_get_height (NULL),
  632. text_block_get_flags (NULL),
  633. text_block_get_top (NULL),
  634. text_block_get_left (NULL),
  635. text_block_get_vert_align (NULL),
  636. text_block_get_horz_align (NULL),
  637. text_block_get_clipping (NULL),
  638. text_block_get_color (NULL),
  639. text_block_get_font (NULL),
  640. text_block_set_top (NULL),
  641. text_block_set_left (NULL),
  642. text_block_set_vert_align (NULL),
  643. text_block_set_horz_align (NULL),
  644. text_block_set_clipping (NULL),
  645. text_block_set_color (NULL),
  646. text_block_set_font (NULL),
  647. text_block_get_actual_height (NULL),
  648. text_block_get_text_width_a (NULL),
  649. text_block_get_text_width_w (NULL),
  650. text_block_text_out_a (NULL),
  651. text_block_text_out_w (NULL),
  652. text_block_destroy (NULL),
  653. image_create (NULL),
  654. image_is_empty (NULL),
  655. image_get_width (NULL),
  656. image_get_height (NULL),
  657. image_get_line_size (NULL),
  658. image_get_data_size (NULL),
  659. image_get_format (NULL),
  660. image_get_data (NULL),
  661. image_get_scanline (NULL),
  662. image_get_pixel_at (NULL),
  663. image_assign (NULL),
  664. image_create_empty (NULL),
  665. image_load_from_func (NULL),
  666. image_resize (NULL),
  667. image_fill_color (NULL),
  668. image_fill_pattern (NULL),
  669. image_blend (NULL),
  670. image_blur (NULL),
  671. image_destroy (NULL),
  672. post_processor_add_range (NULL),
  673. post_processor_add_chars (NULL),
  674. post_processor_clear_ranges (NULL),
  675. post_processor_execute (NULL),
  676. post_processor_fill_color_create (NULL),
  677. post_processor_fill_pattern_create (NULL),
  678. post_processor_border_create (NULL),
  679. post_processor_shadow_create (NULL),
  680. post_processor_custom_create (NULL),
  681. post_processor_destroy (NULL),
  682. char_get_char_code (NULL),
  683. char_get_glyph_metric (NULL),
  684. char_set_glyph_metric (NULL),
  685. get_last_error_code (NULL),
  686. get_last_error_msg (NULL)
  687. {
  688. loadProc(context_create, "ltsContextCreate");
  689. loadProc(context_get_code_page, "ltsContextGetCodePage");
  690. loadProc(context_get_default_char, "ltsContextGetDefaultChar");
  691. loadProc(context_set_code_page, "ltsContextSetCodePage");
  692. loadProc(context_set_default_char, "ltsContextSetDefaultChar");
  693. loadProc(context_ansi_to_wide, "ltsContextAnsiToWide");
  694. loadProc(context_destroy, "ltsContextDestroy");
  695. loadProc(renderer_create, "ltsRendererCreate");
  696. loadProc(renderer_create_custom, "ltsRendererCustomCreate");
  697. loadProc(renderer_begin_block, "ltsRendererBeginBlock");
  698. loadProc(renderer_end_block, "ltsRendererEndBlock");
  699. loadProc(renderer_abort_block, "ltsRendererAbortBlock");
  700. loadProc(renderer_get_text_width_a, "ltsRendererGetTextWidthA");
  701. loadProc(renderer_get_text_width_w, "ltsRendererGetTextWidthW");
  702. loadProc(renderer_destroy, "ltsRendererDestroy");
  703. loadProc(font_creator_create, "ltsFontCreatorCreate");
  704. loadProc(font_creator_get_font_by_name, "ltsFontCreatorGetFontByName");
  705. loadProc(font_creator_get_font_by_file, "ltsFontCreatorGetFontByFile");
  706. loadProc(font_creator_get_font_by_stream, "ltsFontCreatorGetFontByStream");
  707. loadProc(font_creator_destroy, "ltsFontCreatorDestroy");
  708. loadProc(font_get_post_processor, "ltsFontGetPostProcessor");
  709. loadProc(font_get_tab_width, "ltsFontGetTabWidth");
  710. loadProc(font_get_char_spacing, "ltsFontGetCharSpacing");
  711. loadProc(font_get_line_spacing, "ltsFontGetLineSpacing");
  712. loadProc(font_get_metric, "ltsFontGetMetric");
  713. loadProc(font_get_fontname, "ltsFontGetFontname");
  714. loadProc(font_get_facename, "ltsFontGetFacename");
  715. loadProc(font_get_stylename, "ltsFontGetStylename");
  716. loadProc(font_get_fillname, "ltsFontGetFullname");
  717. loadProc(font_get_copyright, "ltsFontGetCopyright");
  718. loadProc(font_set_post_processor, "ltsFontSetPostProcessor");
  719. loadProc(font_set_tab_width, "ltsFontSetTabWidth");
  720. loadProc(font_set_char_spacing, "ltsFontSetCharSpacing");
  721. loadProc(font_set_line_spacing, "ltsFontSetLineSpacing");
  722. loadProc(font_destroy, "ltsFontDestroy");
  723. loadProc(text_block_get_rect, "ltsTextBlockGetRect");
  724. loadProc(text_block_get_width, "ltsTextBlockGetWidth");
  725. loadProc(text_block_get_height, "ltsTextBlockGetHeight");
  726. loadProc(text_block_get_flags, "ltsTextBlockGetFlags");
  727. loadProc(text_block_get_top, "ltsTextBlockGetTop");
  728. loadProc(text_block_get_left, "ltsTextBlockGetLeft");
  729. loadProc(text_block_get_vert_align, "ltsTextBlockGetVertAlign");
  730. loadProc(text_block_get_horz_align, "ltsTextBlockGetHorzAlign");
  731. loadProc(text_block_get_clipping, "ltsTextBlockGetClipping");
  732. loadProc(text_block_get_color, "ltsTextBlockGetColor");
  733. loadProc(text_block_get_font, "ltsTextBlockGetFont");
  734. loadProc(text_block_set_top, "ltsTextBlockSetTop");
  735. loadProc(text_block_set_left, "ltsTextBlockSetLeft");
  736. loadProc(text_block_set_vert_align, "ltsTextBlockSetVertAlign");
  737. loadProc(text_block_set_horz_align, "ltsTextBlockSetHorzAlign");
  738. loadProc(text_block_set_clipping, "ltsTextBlockSetClipping");
  739. loadProc(text_block_set_color, "ltsTextBlockSetColor");
  740. loadProc(text_block_set_font, "ltsTextBlockSetFont");
  741. loadProc(text_block_get_actual_height, "ltsTextBlockGetActualHeight");
  742. loadProc(text_block_get_text_width_a, "ltsTextBlockGetTextWidthA");
  743. loadProc(text_block_get_text_width_w, "ltsTextBlockGetTextWidthW");
  744. loadProc(text_block_text_out_a, "ltsTextBlockTextOutA");
  745. loadProc(text_block_text_out_w, "ltsTextBlockTextOutW");
  746. loadProc(text_block_destroy, "ltsTextBlockDestroy");
  747. loadProc(image_create, "ltsImageCreate");
  748. loadProc(image_is_empty, "ltsImageIsEmpty");
  749. loadProc(image_get_width, "ltsImageGetWidth");
  750. loadProc(image_get_height, "ltsImageGetHeight");
  751. loadProc(image_get_line_size, "ltsImageGetLineSize");
  752. loadProc(image_get_data_size, "ltsImageGetDataSize");
  753. loadProc(image_get_format, "ltsImageGetFormat");
  754. loadProc(image_get_data, "ltsImageGetData");
  755. loadProc(image_get_scanline, "ltsImageGetScanline");
  756. loadProc(image_get_pixel_at, "ltsImageGetPixelAt");
  757. loadProc(image_assign, "ltsImageAssign");
  758. loadProc(image_create_empty, "ltsImageCreateEmpty");
  759. loadProc(image_load_from_func, "ltsImageLoadFromFunc");
  760. loadProc(image_resize, "ltsImageResize");
  761. loadProc(image_fill_color, "ltsImageFillColor");
  762. loadProc(image_fill_pattern, "ltsImageFillPattern");
  763. loadProc(image_blend, "ltsImageBlend");
  764. loadProc(image_blur, "ltsImageBlur");
  765. loadProc(image_destroy, "ltsImageDestroy");
  766. loadProc(post_processor_add_range, "ltsPostProcessorAddRange");
  767. loadProc(post_processor_add_chars, "ltsPostProcessorAddChars");
  768. loadProc(post_processor_clear_ranges, "ltsPostProcessorClearRanges");
  769. loadProc(post_processor_execute, "ltsPostProcessorExecute");
  770. loadProc(post_processor_fill_color_create, "ltsPostProcessorFillColorCreate");
  771. loadProc(post_processor_fill_pattern_create, "ltsPostProcessorFillPatterCreate");
  772. loadProc(post_processor_border_create, "ltsPostProcessorBorderCreate");
  773. loadProc(post_processor_shadow_create, "ltsPostProcessorShadowCreate");
  774. loadProc(post_processor_custom_create, "ltsPostProcessorCustomCreate");
  775. loadProc(post_processor_destroy, "ltsPostProcessorDestroy");
  776. loadProc(char_get_char_code, "ltsCharGetCharCode");
  777. loadProc(char_get_glyph_metric, "ltsCharGetGlyphMetric");
  778. loadProc(char_set_glyph_metric, "ltsCharSetGlyphMetric");
  779. loadProc(initialize, "ltsInitialize");
  780. loadProc(get_last_error_code, "ltsGetLastErrorCode");
  781. loadProc(get_last_error_msg, "ltsGetLastErrorMsg");
  782. loadProc(finalize, "ltsFinalize");
  783. auto err = initialize();
  784. if (err != ErrorCode::None)
  785. throw Exception(std::string("unable to initialize library: ") + get_last_error_msg(), get_last_error_code());
  786. };
  787. ~Impl()
  788. {
  789. finalize();
  790. }
  791. };
  792. /* Exception **********************************************************************************************************************/
  793. inline lts::ErrorCode lts::Exception::getErrorCode() const
  794. { return _errorCode; }
  795. inline std::string lts::Exception::getMessage() const
  796. { return _message; }
  797. const char* lts::Exception::what() const throw()
  798. { return _message.c_str(); };
  799. lts::Exception::Exception(std::string message, ErrorCode errorCode) :
  800. _message (message),
  801. _errorCode (errorCode)
  802. { }
  803. lts::Exception::~Exception() throw()
  804. { }
  805. /* Library ************************************************************************************************************************/
  806. inline lts::ErrorCode lts::Library::getLastErrorCode() const
  807. { return _impl->get_last_error_code(); }
  808. inline std::string lts::Library::getLastErrorMsg() const
  809. { return std::string(_impl->get_last_error_msg()); }
  810. lts::Library::Library(const std::string& libName) :
  811. _handle (0),
  812. _impl (NULL)
  813. {
  814. _handle = libOpen(libName.c_str());
  815. if (!_handle)
  816. throw Exception("unable to open shared library: " + libName, lts::ErrorCode::InvalidLibHandle);
  817. }
  818. lts::Library::~Library()
  819. {
  820. if (_impl)
  821. {
  822. delete _impl;
  823. _impl = NULL;
  824. }
  825. if (_handle)
  826. {
  827. libClose(_handle);
  828. _handle = NULL;
  829. }
  830. }
  831. /* Context ************************************************************************************************************************/
  832. #endif /* LIB_TEXT_SUITE_HPP */