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.
 
 
 
 
 

1982 lines
84 KiB

  1. #ifndef LIB_TEXT_SUITE_HPP
  2. #define LIB_TEXT_SUITE_HPP
  3. #include <list>
  4. #include <memory>
  5. #include <string>
  6. #include <stdint.h>
  7. #include <initializer_list>
  8. #if defined(WIN32) || defined(WIN64) || defined(_WIN32) || defined(_WIN64)
  9. # define LTS_WINDOWS
  10. # include <windows.h>
  11. #elif LINUX || __linux__
  12. # define LTS_LINUX
  13. # include <dlfcn.h>
  14. #else
  15. # error "unknown operation system"
  16. #endif
  17. #if __MINGW32__
  18. # define WINAPI __stdcall
  19. #else
  20. # define WINAPI
  21. #endif
  22. /**********************************************************************************************************************************/
  23. /* public interface */
  24. /**********************************************************************************************************************************/
  25. namespace lts
  26. {
  27. /* simple types *******************************************************************************************************************/
  28. typedef wchar_t WideChar;
  29. typedef void* RenderRef;
  30. typedef void* Handle;
  31. typedef Handle ContextHandle;
  32. typedef Handle RendererHandle;
  33. typedef Handle TextBlockHandle;
  34. typedef Handle FontCreatorHandle;
  35. typedef Handle FontHandle;
  36. typedef Handle PostProcessorHandle;
  37. typedef Handle ImageHandle;
  38. typedef Handle CharHandle;
  39. /* enumerations *******************************************************************************************************************/
  40. enum class ErrorCode : int32_t
  41. {
  42. Unknown = -1,
  43. None = 0,
  44. // misc
  45. NotInitialized = 1,
  46. InvalidEnum = 2,
  47. InvalidValue = 3,
  48. InvalidOperation = 4,
  49. InvalidType = 5,
  50. // invalid handles
  51. InvalidContextHandle = 100,
  52. InvalidRendererHandle = 101,
  53. InvalidTextBlockHandle = 102,
  54. InvalidFontHandle = 103,
  55. InvalidFontCreatorHandle = 104,
  56. InvalidImageHandle = 105,
  57. InvalidPostProcHandle = 106,
  58. // library
  59. InvalidLibName = 200,
  60. InvalidLibHandle = 201,
  61. InvalidMethodName = 202
  62. };
  63. static_assert(sizeof(ErrorCode) == 4, "size of lts::ErrorCode should be 4");
  64. enum class CodePage : uint32_t
  65. {
  66. cpUTF8,
  67. cpISO_8859_1,
  68. cpISO_8859_2,
  69. cpISO_8859_3,
  70. cpISO_8859_4,
  71. cpISO_8859_5,
  72. cpISO_8859_6,
  73. cpISO_8859_7,
  74. cpISO_8859_8,
  75. cpISO_8859_9,
  76. cpISO_8859_10,
  77. cpISO_8859_11,
  78. cpISO_8859_13,
  79. cpISO_8859_14,
  80. cpISO_8859_15,
  81. cpISO_8859_16,
  82. cpISO_037,
  83. cpISO_437,
  84. cpISO_500,
  85. cpISO_737,
  86. cpISO_775,
  87. cpISO_850,
  88. cpISO_852,
  89. cpISO_855,
  90. cpISO_857,
  91. cpISO_860,
  92. cpISO_861,
  93. cpISO_862,
  94. cpISO_863,
  95. cpISO_864,
  96. cpISO_865,
  97. cpISO_866,
  98. cpISO_869,
  99. cpISO_874,
  100. cpISO_875,
  101. cpISO_1026,
  102. cpISO_1250,
  103. cpISO_1251,
  104. cpISO_1252,
  105. cpISO_1253,
  106. cpISO_1254,
  107. cpISO_1255,
  108. cpISO_1256,
  109. cpISO_1257,
  110. cpISO_1258
  111. };
  112. static_assert(sizeof(CodePage) == 4, "size of lts::CodePage should be 4");
  113. enum class Format : uint32_t
  114. {
  115. Empty,
  116. RGBA8,
  117. LumAlpha8,
  118. Alpha8,
  119. Lum8
  120. };
  121. static_assert(sizeof(Format) == 4, "size of lts::Format should be 4");
  122. enum class RendererType : uint32_t
  123. {
  124. Unknown,
  125. OpenGL,
  126. OpenGLES
  127. };
  128. static_assert(sizeof(RendererType) == 4, "size of lts::RendererType should be 4");
  129. enum class FontCreatorType : uint32_t
  130. {
  131. Unknown,
  132. FreeType,
  133. GDI,
  134. Custom
  135. };
  136. static_assert(sizeof(FontCreatorType) == 4, "size of lts::FontCreatorType should be 4");
  137. enum class VertAlign : uint32_t
  138. {
  139. Top,
  140. Center,
  141. Bottom
  142. };
  143. static_assert(sizeof(VertAlign) == 4, "size of lts::VertAlign should be 4");
  144. enum class HorzAlign : uint32_t
  145. {
  146. Left,
  147. Center,
  148. Right,
  149. Justify
  150. };
  151. static_assert(sizeof(HorzAlign) == 4, "size of lts::HorzAlign should be 4");
  152. enum class Clipping : uint32_t
  153. {
  154. None,
  155. WordBorder,
  156. CharBorder,
  157. WordComplete,
  158. CharComplete
  159. };
  160. static_assert(sizeof(Clipping) == 4, "size of lts::Clipping should be 4");
  161. enum class AntiAliasing : uint32_t
  162. {
  163. None,
  164. Normal
  165. };
  166. static_assert(sizeof(AntiAliasing) == 4, "size of lts::AntiAliasing should be 4");
  167. enum class CharRangeUsage : uint32_t
  168. {
  169. Include,
  170. Exclude
  171. };
  172. static_assert(sizeof(CharRangeUsage) == 4, "size of lts::CharRangeUsage should be 4");
  173. enum class ImageMode : uint32_t
  174. {
  175. Ignore,
  176. Replace,
  177. Modulate
  178. };
  179. static_assert(sizeof(ImageMode) == 4, "size of lts::ImageMode should be 4");
  180. /* flags **************************************************************************************************************************/
  181. template <typename TEnum, typename TBase>
  182. class Flags
  183. {
  184. private:
  185. TBase _value;
  186. public:
  187. inline bool isSet(const TEnum& e) const
  188. { return (_value & (1 << e)); };
  189. inline void set(const TEnum& e)
  190. { _value |= (1 << e); };
  191. inline void unset(const TEnum& e)
  192. { _value &= ~(1 << e); };
  193. inline void clear()
  194. { _value = 0; };
  195. Flags() :
  196. _value(0)
  197. { };
  198. Flags(const TEnum& e) :
  199. _value(1 << e)
  200. { };
  201. Flags(const Flags& f) :
  202. _value(f._value)
  203. { };
  204. Flags(const std::initializer_list<TEnum>& vec) :
  205. _value(0)
  206. {
  207. for (const auto& e : vec)
  208. set(e);
  209. };
  210. template <typename T>
  211. Flags(T begIt, T endIt) :
  212. _value(0)
  213. {
  214. for (auto i = begIt; i != endIt; ++i)
  215. set(*i);
  216. };
  217. };
  218. enum class BlockFlag
  219. {
  220. WordWrap
  221. };
  222. typedef Flags<BlockFlag, uint32_t> BlockFlags;
  223. enum class FontStyle
  224. {
  225. Bold,
  226. Italic,
  227. Underline,
  228. Strikeout,
  229. };
  230. typedef Flags<FontStyle, uint32_t> FontStyles;
  231. enum class ColorChannel
  232. {
  233. Red,
  234. Green,
  235. Blue,
  236. Alpha
  237. };
  238. typedef Flags<ColorChannel, uint32_t> ColorChannels;
  239. /* structures *********************************************************************************************************************/
  240. struct Position
  241. {
  242. int x;
  243. int y;
  244. };
  245. union Rect
  246. {
  247. struct { Position top_left, bottom_right; };
  248. struct { int32_t left, top, right, bottom; };
  249. };
  250. union Color4f
  251. {
  252. struct { float r, g, b, a; };
  253. float arr[4];
  254. };
  255. union ImageModes
  256. {
  257. struct { ImageMode r, g, b, a; };
  258. ImageMode arr[4];
  259. };
  260. struct GlyphMetric
  261. {
  262. Position glyphOrigin;
  263. Rect glyphRect;
  264. int32_t advance;
  265. };
  266. struct TextMetric
  267. {
  268. int32_t ascent;
  269. int32_t descent;
  270. int32_t externalLeading;
  271. int32_t baseLineOffset;
  272. int32_t charSpacing;
  273. int32_t lineHeight;
  274. int32_t lineSpacing;
  275. };
  276. struct FontMetric
  277. {
  278. int32_t size;
  279. FontStyles styles;
  280. AntiAliasing antiAliasing;
  281. WideChar defaultChar;
  282. uint8_t __reserved[2];
  283. int32_t ascent;
  284. int32_t descent;
  285. int32_t externalLeading;
  286. int32_t baseLineOffset;
  287. int32_t underlinePos;
  288. int32_t underlineSize;
  289. int32_t strikeoutPos;
  290. int32_t strikeoutSize;
  291. };
  292. typedef float Vector4f[4];
  293. typedef Vector4f Matrix4f[4];
  294. /* exeption ***********************************************************************************************************************/
  295. class Exception : public std::exception
  296. {
  297. private:
  298. std::string _message;
  299. ErrorCode _errorCode;
  300. public:
  301. ErrorCode getErrorCode() const;
  302. std::string getMessage() const;
  303. virtual const char* what() const throw();
  304. Exception(std::string message, ErrorCode errorCode = ErrorCode::Unknown);
  305. ~Exception() throw();
  306. };
  307. /* library ************************************************************************************************************************/
  308. class Library
  309. {
  310. public:
  311. struct Impl;
  312. #if defined(LTS_WINDOWS)
  313. typedef HMODULE Handle; //!< shader file handle
  314. #elif defined(LTS_LINUX)
  315. typedef void* Handle; //!< shader file handle
  316. #else
  317. #error "unknown operation system"
  318. #endif
  319. private:
  320. Impl* _impl;
  321. Handle _handle;
  322. Library(const Library& that) = delete;
  323. public:
  324. const Impl& getImpl() const;
  325. ErrorCode getLastErrorCode() const;
  326. std::string getLastErrorMsg() const;
  327. Library(const std::string& libName);
  328. ~Library();
  329. };
  330. /* context ************************************************************************************************************************/
  331. class Context
  332. {
  333. friend class Image;
  334. friend class PostProcessor;
  335. private:
  336. const Library::Impl& _impl;
  337. ContextHandle _handle;
  338. Context(const Context&) = delete;
  339. public:
  340. ContextHandle getHandle() const;
  341. CodePage getCodePage() const;
  342. WideChar getDefaultChar() const;
  343. void setCodePage(const CodePage value);
  344. void setDefaultChar(const WideChar value);
  345. Context(const Library& library);
  346. };
  347. /* char ***************************************************************************************************************************/
  348. class Char
  349. {
  350. private:
  351. const Library::Impl& _impl;
  352. CharHandle _handle;
  353. Char(const Char&) = delete;
  354. public:
  355. CharHandle getHandle() const;
  356. WideChar getCharCode() const;
  357. GlyphMetric getGlyphMetric() const;
  358. void setGlyphMetric(const GlyphMetric& value);
  359. Char(const Library::Impl& impl, CharHandle handle);
  360. };
  361. /* image **************************************************************************************************************************/
  362. class Image
  363. {
  364. public:
  365. typedef void (*LoadCallback) (Image& image, int x, int y, Color4f& pixel, void* args);
  366. typedef Color4f (*BlendCallback)(Image& image, Color4f src, Color4f dst, void* args);
  367. typedef std::function<void(Image& image, int x, int y, Color4f& pixel)> LoadFunction;
  368. typedef std::function<Color4f(Image& image, Color4f src, Color4f dst)> BlendFunction;
  369. private:
  370. const Library::Impl& _impl;
  371. ImageHandle _handle;
  372. bool _ownsHandle;
  373. Image(const Image&) = delete;
  374. public:
  375. ImageHandle getHandle() const;
  376. bool getIsEmpty() const;
  377. int getWidth() const;
  378. int getHeight() const;
  379. int getLineSize() const;
  380. int getDataSize() const;
  381. Format getFormat() const;
  382. void* getData() const;
  383. void* getScanline(int index) const;
  384. bool getPixelAt(int x, int y, Color4f& color) const;
  385. void assign(const Image& image);
  386. void createEmpty(Format format, int width, int height);
  387. void loadFromFunc(LoadCallback callback, void* args);
  388. void loadFromFunc(LoadFunction func);
  389. void resize(int width, int height, int x, int y);
  390. void fillColor(Color4f color, ColorChannels mask, ImageModes modes);
  391. void fillPattern(const Image& pattern, int x, int y, ColorChannels mask, ImageModes modes);
  392. void blend(const Image& image, int x, int y, BlendCallback callback, void* args);
  393. void blend(const Image& image, int x, int y, BlendFunction func);
  394. void blur(float horzRad, float horzStr, float vertRad, float vertStr, ColorChannels mask);
  395. Image(const Library::Impl& impl, ImageHandle handle);
  396. Image(const Context& context);
  397. virtual ~Image();
  398. private:
  399. static void WINAPI loadCallback(ImageHandle handle, int x, int y, lts::Color4f& pixel, void* args);
  400. static void WINAPI loadFunc(ImageHandle handle, int x, int y, lts::Color4f& pixel, void* args);
  401. struct LoadArgs
  402. {
  403. void* args;
  404. Image* image;
  405. LoadCallback callback;
  406. LoadFunction* func;
  407. };
  408. static void WINAPI blendCallback(ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args);
  409. static void WINAPI blendFunc(ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args);
  410. struct BlendArgs
  411. {
  412. void* args;
  413. Image* image;
  414. BlendCallback callback;
  415. BlendFunction* func;
  416. };
  417. };
  418. /* post processor *****************************************************************************************************************/
  419. class PostProcessor
  420. {
  421. private:
  422. PostProcessorHandle _handle;
  423. PostProcessor(const PostProcessor&) = delete;
  424. protected:
  425. const Library::Impl& _impl;
  426. void setHandle(PostProcessorHandle handle);
  427. virtual void execute(Char& c, Image& i);
  428. PostProcessor(const Context& context);
  429. public:
  430. PostProcessorHandle getHandle() const;
  431. void addRange(CharRangeUsage usage, WideChar start, WideChar stop);
  432. void addChars(CharRangeUsage usage, WideChar* chars);
  433. void clearRanges();
  434. virtual ~PostProcessor();
  435. };
  436. /* post processor fill color ******************************************************************************************************/
  437. class PostProcessorFillColor final : public PostProcessor
  438. {
  439. private:
  440. Color4f _color;
  441. ImageModes _modes;
  442. ColorChannels _channels;
  443. public:
  444. const Color4f& getColor() const;
  445. const ImageModes& getModes() const;
  446. const ColorChannels& getChannels() const;
  447. PostProcessorFillColor(
  448. const Context& context,
  449. const Color4f& color,
  450. const ImageModes& modes,
  451. const ColorChannels& channels);
  452. };
  453. /* post processor fill pattern ****************************************************************************************************/
  454. class PostProcessorFillPattern final : public PostProcessor
  455. {
  456. private:
  457. const Image& _pattern;
  458. Position _position;
  459. ImageModes _modes;
  460. ColorChannels _channels;
  461. public:
  462. const Image& getPattern() const;
  463. const Position& getPosition() const;
  464. const ImageModes& getModes() const;
  465. const ColorChannels& getChannels() const;
  466. PostProcessorFillPattern(
  467. const Context& context,
  468. const Image& pattern,
  469. const Position& pos,
  470. ImageModes modes,
  471. ColorChannels channels);
  472. };
  473. /* post processor border **********************************************************************************************************/
  474. class PostProcessorBorder final : public PostProcessor
  475. {
  476. private:
  477. float _width;
  478. float _strength;
  479. Color4f _color;
  480. bool _keepSize;
  481. public:
  482. float getWidth() const;
  483. float getStrength() const;
  484. Color4f getColor() const;
  485. bool getKeepSize() const;
  486. PostProcessorBorder(const Context& context, float width, float strength, Color4f color, bool keepSize);
  487. };
  488. /* post processor shadow **********************************************************************************************************/
  489. class PostProcessorShadow final : public PostProcessor
  490. {
  491. private:
  492. float _radius;
  493. float _strength;
  494. Position _offset;
  495. Color4f _color;
  496. public:
  497. float getRadius() const;
  498. float getStrength() const;
  499. Position getOffset() const;
  500. Color4f getColor() const;
  501. PostProcessorShadow(const Context& context, float radius, float strength, const Position& offset, const Color4f& color);
  502. };
  503. /* post processor custom **********************************************************************************************************/
  504. class PostProcessorCustom : public PostProcessor
  505. {
  506. protected:
  507. virtual void execute(Char& c, Image& i);
  508. public:
  509. PostProcessorCustom(const Context& context);
  510. public:
  511. static void WINAPI executeCallback(CharHandle charHandle, ImageHandle imageHandle, void* args);
  512. };
  513. /* post processor list ************************************************************************************************************/
  514. template<typename T>
  515. class PostProcessorList : public PostProcessorCustom,
  516. public std::list<T>
  517. {
  518. protected:
  519. virtual void execute(Char& c, Image& i);
  520. public:
  521. PostProcessorList(const Context& context);
  522. };
  523. /* font ***************************************************************************************************************************/
  524. class Font
  525. {
  526. public:
  527. struct Names
  528. {
  529. std::string fontname;
  530. std::string copyright;
  531. std::string facename;
  532. std::string stylename;
  533. std::string fullname;
  534. };
  535. private:
  536. const Library::Impl& _impl;
  537. FontHandle _handle;
  538. FontMetric _metric;
  539. Names _names;
  540. PostProcessor* _postProcessor;
  541. Font(const Font&) = delete;
  542. public:
  543. FontHandle getHandle() const;
  544. const Names& getNames() const;
  545. const FontMetric& getMetric() const;
  546. PostProcessor* getPostProcessor() const;
  547. int getTabWidth() const;
  548. int getCharSpacing() const;
  549. int getLineSpacing() const;
  550. void setPostProcessor(PostProcessor* value);
  551. void setTabWidth(int value);
  552. void setCharSpacing(int value);
  553. void setLineSpacing(int value);
  554. Font(Library::Impl& impl, FontHandle handle);
  555. virtual ~Font();
  556. };
  557. typedef std::unique_ptr<Font> FontPtrU;
  558. /* font creator *******************************************************************************************************************/
  559. class FontCreator
  560. {
  561. private:
  562. const Library::Impl& _impl;
  563. FontCreatorHandle _handle;
  564. FontCreator(const FontCreator&);
  565. protected:
  566. FontCreator(FontCreatorHandle handle);
  567. public:
  568. FontCreatorHandle getHandle() const;
  569. FontPtrU getFontByName (const std::string& fontname, int size, FontStyles styles, AntiAliasing antiAliasing);
  570. FontPtrU getFontByFile (const std::string& filename, int size, FontStyles styles, AntiAliasing antiAliasing);
  571. FontPtrU getFontByStream(const std::istream& stream, int size, FontStyles styles, AntiAliasing antiAliasing);
  572. };
  573. /* font creator GDI ***************************************************************************************************************/
  574. class FontCreatorGDI
  575. {
  576. public:
  577. FontCreatorGDI(const Context& context);
  578. };
  579. /* font creator free type *********************************************************************************************************/
  580. class FontCreatorFreeType
  581. {
  582. public:
  583. FontCreatorFreeType(const Context& context);
  584. };
  585. /* text block *********************************************************************************************************************/
  586. class TextBlock
  587. {
  588. private:
  589. const Library::Impl& _impl;
  590. TextBlockHandle _handle;
  591. Font* _font;
  592. TextBlock(const TextBlock&);
  593. public:
  594. TextBlockHandle getHandle() const;
  595. Rect getRect() const;
  596. int getWidth() const;
  597. int getHeight() const;
  598. BlockFlags getFlags() const;
  599. int getTop() const;
  600. int getLeft() const;
  601. VertAlign getVertAlign() const;
  602. HorzAlign getHorzAlign() const;
  603. Clipping getClipping() const;
  604. Color4f getColor() const;
  605. Font* getFont() const;
  606. void setTop(int value);
  607. void setLeft(int value);
  608. void setVertAlign(VertAlign value);
  609. void setHorzAlign(HorzAlign value);
  610. void setClipping(Clipping value);
  611. void setColor(const Color4f& value);
  612. void setFont(Font* font);
  613. int getActualBlockHeight() const;
  614. void textOutA(const char* text);
  615. void textOutA(const std::string& text);
  616. void textOutW(const WideChar* text);
  617. void textOutW(const std::wstring& text);
  618. int getTextWidthA(const char* text);
  619. int getTextWidthA(const std::string& text);
  620. int getTextWidthW(const WideChar* text);
  621. int getTextWidthW(const std::wstring& text);
  622. TextBlock(TextBlockHandle handle);
  623. virtual ~TextBlock();
  624. };
  625. typedef std::unique_ptr<TextBlock> TextBlockPtrU;
  626. /* renderer ***********************************************************************************************************************/
  627. class Renderer
  628. {
  629. private:
  630. RendererHandle _handle;
  631. Renderer(const Renderer&) = delete;
  632. protected:
  633. const Library::Impl& _impl;
  634. Renderer(RendererHandle handle);
  635. public:
  636. RendererHandle getHandle() const;
  637. TextBlockPtrU beginBlock(int top, int left, int width, int height, BlockFlags flags);
  638. void endBlock(TextBlockPtrU block);
  639. void abortBlock(TextBlockPtrU block);
  640. int getTextWidthA(Font* font, const char* text);
  641. int getTextWidthA(Font* font, const std::string& text);
  642. int getTextWidthW(Font* font, const WideChar* text);
  643. int getTextWidthW(Font* font, const std::wstring& text);
  644. virtual ~Renderer();
  645. };
  646. /* renderer OpenGL ****************************************************************************************************************/
  647. class RendererOpenGL : public Renderer
  648. {
  649. public:
  650. RendererOpenGL(const Context& context, Format format);
  651. };
  652. /* renderer OpenGLES **************************************************************************************************************/
  653. class RendererOpenGLES : public Renderer
  654. {
  655. public:
  656. RendererOpenGLES(const Context& context, Format format);
  657. };
  658. /* renderer custom ****************************************************************************************************************/
  659. class RendererCustom : public Renderer
  660. {
  661. protected:
  662. Position _drawPos;
  663. Color4f _color;
  664. virtual void beginRender();
  665. virtual void endRender();
  666. virtual Position getDrawPos();
  667. virtual void setDrawPos(const Position& value);
  668. virtual void moveDrawPos(const Position& offset);
  669. virtual void setColor(const Color4f& color);
  670. virtual void render(RenderRef ref, int forcedWidth = 0);
  671. virtual RenderRef createRenderRef(Char& c, const Image& img);
  672. virtual void freeRenderRef(RenderRef ref);
  673. public:
  674. RendererCustom(const Context& context, Format format);
  675. };
  676. }
  677. /**********************************************************************************************************************************/
  678. /* private implementation */
  679. /**********************************************************************************************************************************/
  680. #if defined(LTS_WINDOWS)
  681. lts::Library::Handle libOpen(const char* name)
  682. { return LoadLibrary(name); };
  683. template <typename T>
  684. T getAddr(lts::Library::Handle handle, const char* name)
  685. { return reinterpret_cast<T>(GetProcAddress(handle, name)); };
  686. int libClose(lts::Library::Handle handle)
  687. { return FreeLibrary(handle); };
  688. #elif defined(LTS_LINUX)
  689. lts::Library::Handle libOpen(const char* name)
  690. { return dlopen(name, RTLD_LAZY); };
  691. template <typename T>
  692. T getAddr(lts::Library::Handle handle, const char* name)
  693. { return reinterpret_cast<T>(dlsym(handle, name)); };
  694. int libClose(lts::Library::Handle handle)
  695. { return !dlclose(handle); };
  696. #else
  697. # error "unknown operation system"
  698. #endif
  699. /* Library::Impl ******************************************************************************************************************/
  700. struct lts::Library::Impl
  701. {
  702. public: /* stream */
  703. struct StreamData
  704. {
  705. enum class Origin : uint32_t
  706. {
  707. Begin = 0,
  708. Current = 1,
  709. End = 2
  710. };
  711. static_assert(sizeof(Origin) == 4, "size of lts::StreamOrigin should be 4");
  712. typedef int (WINAPI *stream_read_t)(void* args, void* buffer, int count);
  713. typedef int (WINAPI *stream_seek_t)(void* args, Origin origin, int offset);
  714. void* args;
  715. stream_read_t read;
  716. stream_seek_t seek;
  717. };
  718. public: /* custom post processor */
  719. struct PostProcessorData
  720. {
  721. typedef void (WINAPI *post_processor_execute_t)(lts::CharHandle charHandle, lts::ImageHandle imageHandle, void* args);
  722. void* args;
  723. post_processor_execute_t execute;
  724. };
  725. public: /* custom renderer */
  726. struct RendererCustomData
  727. {
  728. typedef void (WINAPI *renderer_begin_render_t) (void* args);
  729. typedef void (WINAPI *renderer_end_render_t) (void* args);
  730. typedef lts::Position (WINAPI *renderer_get_draw_pos_t) (void* args);
  731. typedef void (WINAPI *renderer_set_draw_pos_t) (lts::Position value, void* args);
  732. typedef void (WINAPI *renderer_move_draw_pos_t) (lts::Position value, void* args);
  733. typedef void (WINAPI *renderer_set_color_t) (lts::Color4f value, void* args);
  734. typedef void (WINAPI *renderer_render) (void* ref, int forcedWidth, void* args);
  735. typedef void* (WINAPI *renderer_create_ref) (lts::CharHandle charHandle, lts::ImageHandle imageHandle, void* args);
  736. typedef void (WINAPI *renderer_free_ref) (void* ref, void* args);
  737. void* args;
  738. renderer_begin_render_t beginRender;
  739. renderer_end_render_t endRender;
  740. renderer_get_draw_pos_t getDrawPos;
  741. renderer_set_draw_pos_t setDrawPos;
  742. renderer_move_draw_pos_t moveDrawPos;
  743. renderer_set_color_t setColor;
  744. renderer_render render;
  745. renderer_create_ref createRef;
  746. renderer_free_ref freeRef;
  747. };
  748. public: /* callbacks */
  749. typedef void (WINAPI *image_load_callback_t) (lts::ImageHandle handle, int x, int y, lts::Color4f& pixel, void* args);
  750. typedef void (WINAPI *image_blend_callback_t)(lts::ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args);
  751. typedef lts::ContextHandle (WINAPI *context_create_t) ();
  752. typedef lts::ErrorCode (WINAPI *context_get_code_page_t) (lts::ContextHandle handle, lts::CodePage& value);
  753. typedef lts::ErrorCode (WINAPI *context_get_default_char_t) (lts::ContextHandle handle, WideChar& value);
  754. typedef lts::ErrorCode (WINAPI *context_set_code_page_t) (lts::ContextHandle handle, lts::CodePage value);
  755. typedef lts::ErrorCode (WINAPI *context_set_default_char_t) (lts::ContextHandle handle, WideChar value);
  756. typedef WideChar* (WINAPI *context_ansi_to_wide_t) (lts::ContextHandle handle, const char* text);
  757. typedef lts::ErrorCode (WINAPI *context_destroy_t) (lts::ContextHandle handle);
  758. typedef lts::RendererHandle (WINAPI *renderer_create_t) (lts::ContextHandle handle, lts::RendererType type, lts::Format format);
  759. typedef lts::RendererHandle (WINAPI *renderer_create_custom_t) (lts::ContextHandle handle, lts::Format format, const RendererCustomData& data);
  760. typedef lts::TextBlockHandle (WINAPI *renderer_begin_block_t) (lts::RendererHandle handle, int top, int left, int width, int height, lts::BlockFlags flags);
  761. typedef lts::ErrorCode (WINAPI *renderer_end_block_t) (lts::RendererHandle handle, lts::TextBlockHandle block);
  762. typedef lts::ErrorCode (WINAPI *renderer_abort_block_t) (lts::RendererHandle handle, lts::TextBlockHandle block);
  763. typedef int (WINAPI *renderer_get_text_width_a_t) (lts::RendererHandle handle, lts::FontHandle font, const char* text);
  764. typedef int (WINAPI *renderer_get_text_width_w_t) (lts::RendererHandle handle, lts::FontHandle font, const WideChar* text);
  765. typedef lts::ErrorCode (WINAPI *renderer_destroy_t) (lts::RendererHandle handle);
  766. typedef lts::FontCreatorHandle (WINAPI *font_creator_create_t) (lts::ContextHandle handle, lts::FontCreatorType type);
  767. 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);
  768. 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);
  769. typedef lts::FontHandle (WINAPI *font_creator_get_font_by_stream_t) (lts::FontCreatorHandle handle, StreamData* stream, int size, lts::FontStyles style, lts::AntiAliasing antialiasing);
  770. typedef lts::ErrorCode (WINAPI *font_creator_destroy_t) (lts::FontCreatorHandle handle);
  771. typedef lts::PostProcessorHandle(WINAPI *font_get_post_processor_t) (lts::FontHandle handle);
  772. typedef lts::ErrorCode (WINAPI *font_get_tab_width_t) (lts::FontHandle handle, int& value);
  773. typedef lts::ErrorCode (WINAPI *font_get_char_spacing_t) (lts::FontHandle handle, int& value);
  774. typedef lts::ErrorCode (WINAPI *font_get_line_spacing_t) (lts::FontHandle handle, float& value);
  775. typedef lts::ErrorCode (WINAPI *font_get_metric_t) (lts::FontHandle handle, lts::FontMetric& value);
  776. typedef const char* (WINAPI *font_get_fontname_t) (lts::FontHandle handle);
  777. typedef const char* (WINAPI *font_get_facename_t) (lts::FontHandle handle);
  778. typedef const char* (WINAPI *font_get_stylename_t) (lts::FontHandle handle);
  779. typedef const char* (WINAPI *font_get_fullname_t) (lts::FontHandle handle);
  780. typedef const char* (WINAPI *font_get_copyright_t) (lts::FontHandle handle);
  781. typedef lts::ErrorCode (WINAPI *font_set_post_processor_t) (lts::FontHandle handle, lts::PostProcessorHandle pp);
  782. typedef lts::ErrorCode (WINAPI *font_set_tab_width_t) (lts::FontHandle handle, int value);
  783. typedef lts::ErrorCode (WINAPI *font_set_char_spacing_t) (lts::FontHandle handle, int value);
  784. typedef lts::ErrorCode (WINAPI *font_set_line_spacing_t) (lts::FontHandle handle, float value);
  785. typedef lts::ErrorCode (WINAPI *font_destroy_t) (lts::FontHandle handle);
  786. typedef lts::ErrorCode (WINAPI *text_block_get_rect_t) (lts::TextBlockHandle handle, lts::Rect& value);
  787. typedef lts::ErrorCode (WINAPI *text_block_get_width_t) (lts::TextBlockHandle handle, int& value);
  788. typedef lts::ErrorCode (WINAPI *text_block_get_height_t) (lts::TextBlockHandle handle, int& value);
  789. typedef lts::ErrorCode (WINAPI *text_block_get_flags_t) (lts::TextBlockHandle handle, lts::BlockFlags& value);
  790. typedef lts::ErrorCode (WINAPI *text_block_get_top_t) (lts::TextBlockHandle handle, int& value);
  791. typedef lts::ErrorCode (WINAPI *text_block_get_left_t) (lts::TextBlockHandle handle, int& value);
  792. typedef lts::ErrorCode (WINAPI *text_block_get_vert_align_t) (lts::TextBlockHandle handle, lts::VertAlign& value);
  793. typedef lts::ErrorCode (WINAPI *text_block_get_horz_align_t) (lts::TextBlockHandle handle, lts::HorzAlign& value);
  794. typedef lts::ErrorCode (WINAPI *text_block_get_clipping_t) (lts::TextBlockHandle handle, lts::Clipping& value);
  795. typedef lts::ErrorCode (WINAPI *text_block_get_color_t) (lts::TextBlockHandle handle, lts::Color4f& value);
  796. typedef lts::ErrorCode (WINAPI *text_block_get_font_t) (lts::TextBlockHandle handle, lts::FontHandle& value);
  797. typedef lts::ErrorCode (WINAPI *text_block_set_top_t) (lts::TextBlockHandle handle, int value);
  798. typedef lts::ErrorCode (WINAPI *text_block_set_left_t) (lts::TextBlockHandle handle, int value);
  799. typedef lts::ErrorCode (WINAPI *text_block_set_vert_align_t) (lts::TextBlockHandle handle, lts::VertAlign value);
  800. typedef lts::ErrorCode (WINAPI *text_block_set_horz_align_t) (lts::TextBlockHandle handle, lts::HorzAlign value);
  801. typedef lts::ErrorCode (WINAPI *text_block_set_clipping_t) (lts::TextBlockHandle handle, lts::Clipping value);
  802. typedef lts::ErrorCode (WINAPI *text_block_set_color_t) (lts::TextBlockHandle handle, lts::Color4f value);
  803. typedef lts::ErrorCode (WINAPI *text_block_set_font_t) (lts::TextBlockHandle handle, lts::FontHandle value);
  804. typedef int (WINAPI *text_block_get_actual_height_t) (lts::TextBlockHandle handle);
  805. typedef int (WINAPI *text_block_get_text_width_a_t) (lts::TextBlockHandle handle, const char* text);
  806. typedef int (WINAPI *text_block_get_text_width_w_t) (lts::TextBlockHandle handle, const WideChar* text);
  807. typedef lts::ErrorCode (WINAPI *text_block_text_out_a_t) (lts::TextBlockHandle handle, const char* text);
  808. typedef lts::ErrorCode (WINAPI *text_block_text_out_w_t) (lts::TextBlockHandle handle, const WideChar* text);
  809. typedef lts::ErrorCode (WINAPI *text_block_destroy_t) (lts::TextBlockHandle handle);
  810. typedef lts::ImageHandle (WINAPI *image_create_t) (lts::ContextHandle handle);
  811. typedef lts::ErrorCode (WINAPI *image_is_empty_t) (lts::ImageHandle handle, bool& value);
  812. typedef int (WINAPI *image_get_width_t) (lts::ImageHandle handle);
  813. typedef int (WINAPI *image_get_height_t) (lts::ImageHandle handle);
  814. typedef int (WINAPI *image_get_line_size_t) (lts::ImageHandle handle);
  815. typedef int (WINAPI *image_get_data_size_t) (lts::ImageHandle handle);
  816. typedef lts::ErrorCode (WINAPI *image_get_format_t) (lts::ImageHandle handle, lts::Format& value);
  817. typedef void* (WINAPI *image_get_data_t) (lts::ImageHandle handle);
  818. typedef void* (WINAPI *image_get_scanline_t) (lts::ImageHandle handle, int index);
  819. typedef lts::ErrorCode (WINAPI *image_get_pixel_at_t) (lts::ImageHandle handle, int x, int y, lts::Color4f pixel);
  820. typedef lts::ErrorCode (WINAPI *image_assign_t) (lts::ImageHandle handle, lts::ImageHandle source);
  821. typedef lts::ErrorCode (WINAPI *image_create_empty_t) (lts::ImageHandle handle, lts::Format format, int width, int height);
  822. typedef lts::ErrorCode (WINAPI *image_load_from_func_t) (lts::ImageHandle handle, image_load_callback_t callback, void* args);
  823. typedef lts::ErrorCode (WINAPI *image_resize_t) (lts::ImageHandle handle, int width, int height, int x, int y);
  824. typedef lts::ErrorCode (WINAPI *image_fill_color_t) (lts::ImageHandle handle, lts::Color4f color, lts::ColorChannels mask, lts::ImageModes modes);
  825. typedef lts::ErrorCode (WINAPI *image_fill_pattern_t) (lts::ImageHandle handle, lts::ImageHandle pattern, int x, int y, lts::ColorChannels mask, lts::ImageModes modes);
  826. typedef lts::ErrorCode (WINAPI *image_blend_t) (lts::ImageHandle handle, lts::ImageHandle source, int x, int y, image_blend_callback_t callback, void* args);
  827. typedef lts::ErrorCode (WINAPI *image_blur_t) (lts::ImageHandle handle, float horzRad, float horzStr, float vertRad, float vertStr, lts::ColorChannels mask);
  828. typedef lts::ErrorCode (WINAPI *image_destroy_t) (lts::ImageHandle handle);
  829. typedef lts::ErrorCode (WINAPI *post_processor_add_range_t) (lts::PostProcessorHandle handle, lts::CharRangeUsage usage, WideChar start, wchar_t stop);
  830. typedef lts::ErrorCode (WINAPI *post_processor_add_chars_t) (lts::PostProcessorHandle handle, lts::CharRangeUsage usage, const WideChar* chars);
  831. typedef lts::ErrorCode (WINAPI *post_processor_clear_ranges_t) (lts::PostProcessorHandle handle);
  832. typedef lts::ErrorCode (WINAPI *post_processor_execute_t) (lts::PostProcessorHandle handle, lts::CharHandle charHandle, lts::ImageHandle image);
  833. typedef lts::PostProcessorHandle(WINAPI *post_processor_fill_color_create_t) (lts::PostProcessorHandle handle, const lts::Color4f color, const lts::ImageModes modes, const lts::ColorChannels channels);
  834. typedef lts::PostProcessorHandle(WINAPI *post_processor_fill_pattern_create_t) (lts::PostProcessorHandle handle, lts::ImageHandle pattern, bool ownsPattern, const lts::Position position, const lts::ImageModes modes, const lts::ColorChannels channels);
  835. typedef lts::PostProcessorHandle(WINAPI *post_processor_border_create_t) (lts::PostProcessorHandle handle, float width, float strength, const lts::Color4f color, bool keepSize);
  836. typedef lts::PostProcessorHandle(WINAPI *post_processor_shadow_create_t) (lts::PostProcessorHandle handle, float radius, float strength, const lts::Position offset, const lts::Color4f color);
  837. typedef lts::PostProcessorHandle(WINAPI *post_processor_custom_create_t) (lts::PostProcessorHandle handle, const PostProcessorData& data);
  838. typedef lts::ErrorCode (WINAPI *post_processor_destroy_t) (lts::PostProcessorHandle handle);
  839. typedef lts::ErrorCode (WINAPI *char_get_char_code_t) (lts::CharHandle handle, WideChar& value);
  840. typedef lts::ErrorCode (WINAPI *char_get_glyph_metric_t) (lts::CharHandle handle, lts::GlyphMetric& value);
  841. typedef lts::ErrorCode (WINAPI *char_set_glyph_metric_t) (lts::CharHandle handle, lts::GlyphMetric value);
  842. typedef lts::ErrorCode (WINAPI *initialize_t) ();
  843. typedef lts::ErrorCode (WINAPI *get_last_error_code_t) ();
  844. typedef const char* (WINAPI *get_last_error_msg_t) ();
  845. typedef lts::ErrorCode (WINAPI *finalize_t) ();
  846. private:
  847. lts::Library::Handle _handle;
  848. template <typename T>
  849. inline void loadProc(T& proc, const char* name)
  850. {
  851. proc = getAddr<T>(_handle, name);
  852. if (!proc)
  853. throw lts::Exception(std::string("unable to load method from library: ") + name, lts::ErrorCode::InvalidMethodName);
  854. }
  855. public:
  856. context_create_t context_create;
  857. context_get_code_page_t context_get_code_page;
  858. context_get_default_char_t context_get_default_char;
  859. context_set_code_page_t context_set_code_page;
  860. context_set_default_char_t context_set_default_char;
  861. context_ansi_to_wide_t context_ansi_to_wide;
  862. context_destroy_t context_destroy;
  863. renderer_create_t renderer_create;
  864. renderer_create_custom_t renderer_create_custom;
  865. renderer_begin_block_t renderer_begin_block;
  866. renderer_end_block_t renderer_end_block;
  867. renderer_abort_block_t renderer_abort_block;
  868. renderer_get_text_width_a_t renderer_get_text_width_a;
  869. renderer_get_text_width_w_t renderer_get_text_width_w;
  870. renderer_destroy_t renderer_destroy;
  871. font_creator_create_t font_creator_create;
  872. font_creator_get_font_by_name_t font_creator_get_font_by_name;
  873. font_creator_get_font_by_file_t font_creator_get_font_by_file;
  874. font_creator_get_font_by_stream_t font_creator_get_font_by_stream;
  875. font_creator_destroy_t font_creator_destroy;
  876. font_get_post_processor_t font_get_post_processor;
  877. font_get_tab_width_t font_get_tab_width;
  878. font_get_char_spacing_t font_get_char_spacing;
  879. font_get_line_spacing_t font_get_line_spacing;
  880. font_get_metric_t font_get_metric;
  881. font_get_fontname_t font_get_fontname;
  882. font_get_facename_t font_get_facename;
  883. font_get_stylename_t font_get_stylename;
  884. font_get_fullname_t font_get_fullname;
  885. font_get_copyright_t font_get_copyright;
  886. font_set_post_processor_t font_set_post_processor;
  887. font_set_tab_width_t font_set_tab_width;
  888. font_set_char_spacing_t font_set_char_spacing;
  889. font_set_line_spacing_t font_set_line_spacing;
  890. font_destroy_t font_destroy;
  891. text_block_get_rect_t text_block_get_rect;
  892. text_block_get_width_t text_block_get_width;
  893. text_block_get_height_t text_block_get_height;
  894. text_block_get_flags_t text_block_get_flags;
  895. text_block_get_top_t text_block_get_top;
  896. text_block_get_left_t text_block_get_left;
  897. text_block_get_vert_align_t text_block_get_vert_align;
  898. text_block_get_horz_align_t text_block_get_horz_align;
  899. text_block_get_clipping_t text_block_get_clipping;
  900. text_block_get_color_t text_block_get_color;
  901. text_block_get_font_t text_block_get_font;
  902. text_block_set_top_t text_block_set_top;
  903. text_block_set_left_t text_block_set_left;
  904. text_block_set_vert_align_t text_block_set_vert_align;
  905. text_block_set_horz_align_t text_block_set_horz_align;
  906. text_block_set_clipping_t text_block_set_clipping;
  907. text_block_set_color_t text_block_set_color;
  908. text_block_set_font_t text_block_set_font;
  909. text_block_get_actual_height_t text_block_get_actual_height;
  910. text_block_get_text_width_a_t text_block_get_text_width_a;
  911. text_block_get_text_width_w_t text_block_get_text_width_w;
  912. text_block_text_out_a_t text_block_text_out_a;
  913. text_block_text_out_w_t text_block_text_out_w;
  914. text_block_destroy_t text_block_destroy;
  915. image_create_t image_create;
  916. image_is_empty_t image_is_empty;
  917. image_get_width_t image_get_width;
  918. image_get_height_t image_get_height;
  919. image_get_line_size_t image_get_line_size;
  920. image_get_data_size_t image_get_data_size;
  921. image_get_format_t image_get_format;
  922. image_get_data_t image_get_data;
  923. image_get_scanline_t image_get_scanline;
  924. image_get_pixel_at_t image_get_pixel_at;
  925. image_assign_t image_assign;
  926. image_create_empty_t image_create_empty;
  927. image_load_from_func_t image_load_from_func;
  928. image_resize_t image_resize;
  929. image_fill_color_t image_fill_color;
  930. image_fill_pattern_t image_fill_pattern;
  931. image_blend_t image_blend;
  932. image_blur_t image_blur;
  933. image_destroy_t image_destroy;
  934. post_processor_add_range_t post_processor_add_range;
  935. post_processor_add_chars_t post_processor_add_chars;
  936. post_processor_clear_ranges_t post_processor_clear_ranges;
  937. post_processor_execute_t post_processor_execute;
  938. post_processor_fill_color_create_t post_processor_fill_color_create;
  939. post_processor_fill_pattern_create_t post_processor_fill_pattern_create;
  940. post_processor_border_create_t post_processor_border_create;
  941. post_processor_shadow_create_t post_processor_shadow_create;
  942. post_processor_custom_create_t post_processor_custom_create;
  943. post_processor_destroy_t post_processor_destroy;
  944. char_get_char_code_t char_get_char_code;
  945. char_get_glyph_metric_t char_get_glyph_metric;
  946. char_set_glyph_metric_t char_set_glyph_metric;
  947. initialize_t initialize;
  948. get_last_error_code_t get_last_error_code;
  949. get_last_error_msg_t get_last_error_msg;
  950. finalize_t finalize;
  951. public:
  952. Impl(lts::Library::Handle handle) :
  953. _handle (handle),
  954. context_create (NULL),
  955. context_get_code_page (NULL),
  956. context_get_default_char (NULL),
  957. context_set_code_page (NULL),
  958. context_set_default_char (NULL),
  959. context_ansi_to_wide (NULL),
  960. context_destroy (NULL),
  961. renderer_create (NULL),
  962. renderer_create_custom (NULL),
  963. renderer_begin_block (NULL),
  964. renderer_end_block (NULL),
  965. renderer_abort_block (NULL),
  966. renderer_get_text_width_a (NULL),
  967. renderer_get_text_width_w (NULL),
  968. renderer_destroy (NULL),
  969. font_creator_create (NULL),
  970. font_creator_get_font_by_name (NULL),
  971. font_creator_get_font_by_file (NULL),
  972. font_creator_get_font_by_stream (NULL),
  973. font_creator_destroy (NULL),
  974. font_get_post_processor (NULL),
  975. font_get_tab_width (NULL),
  976. font_get_char_spacing (NULL),
  977. font_get_line_spacing (NULL),
  978. font_get_metric (NULL),
  979. font_get_fontname (NULL),
  980. font_get_facename (NULL),
  981. font_get_stylename (NULL),
  982. font_get_fullname (NULL),
  983. font_get_copyright (NULL),
  984. font_set_post_processor (NULL),
  985. font_set_tab_width (NULL),
  986. font_set_char_spacing (NULL),
  987. font_set_line_spacing (NULL),
  988. font_destroy (NULL),
  989. text_block_get_rect (NULL),
  990. text_block_get_width (NULL),
  991. text_block_get_height (NULL),
  992. text_block_get_flags (NULL),
  993. text_block_get_top (NULL),
  994. text_block_get_left (NULL),
  995. text_block_get_vert_align (NULL),
  996. text_block_get_horz_align (NULL),
  997. text_block_get_clipping (NULL),
  998. text_block_get_color (NULL),
  999. text_block_get_font (NULL),
  1000. text_block_set_top (NULL),
  1001. text_block_set_left (NULL),
  1002. text_block_set_vert_align (NULL),
  1003. text_block_set_horz_align (NULL),
  1004. text_block_set_clipping (NULL),
  1005. text_block_set_color (NULL),
  1006. text_block_set_font (NULL),
  1007. text_block_get_actual_height (NULL),
  1008. text_block_get_text_width_a (NULL),
  1009. text_block_get_text_width_w (NULL),
  1010. text_block_text_out_a (NULL),
  1011. text_block_text_out_w (NULL),
  1012. text_block_destroy (NULL),
  1013. image_create (NULL),
  1014. image_is_empty (NULL),
  1015. image_get_width (NULL),
  1016. image_get_height (NULL),
  1017. image_get_line_size (NULL),
  1018. image_get_data_size (NULL),
  1019. image_get_format (NULL),
  1020. image_get_data (NULL),
  1021. image_get_scanline (NULL),
  1022. image_get_pixel_at (NULL),
  1023. image_assign (NULL),
  1024. image_create_empty (NULL),
  1025. image_load_from_func (NULL),
  1026. image_resize (NULL),
  1027. image_fill_color (NULL),
  1028. image_fill_pattern (NULL),
  1029. image_blend (NULL),
  1030. image_blur (NULL),
  1031. image_destroy (NULL),
  1032. post_processor_add_range (NULL),
  1033. post_processor_add_chars (NULL),
  1034. post_processor_clear_ranges (NULL),
  1035. post_processor_execute (NULL),
  1036. post_processor_fill_color_create (NULL),
  1037. post_processor_fill_pattern_create (NULL),
  1038. post_processor_border_create (NULL),
  1039. post_processor_shadow_create (NULL),
  1040. post_processor_custom_create (NULL),
  1041. post_processor_destroy (NULL),
  1042. char_get_char_code (NULL),
  1043. char_get_glyph_metric (NULL),
  1044. char_set_glyph_metric (NULL),
  1045. get_last_error_code (NULL),
  1046. get_last_error_msg (NULL)
  1047. {
  1048. loadProc(context_create, "ltsContextCreate");
  1049. loadProc(context_get_code_page, "ltsContextGetCodePage");
  1050. loadProc(context_get_default_char, "ltsContextGetDefaultChar");
  1051. loadProc(context_set_code_page, "ltsContextSetCodePage");
  1052. loadProc(context_set_default_char, "ltsContextSetDefaultChar");
  1053. loadProc(context_ansi_to_wide, "ltsContextAnsiToWide");
  1054. loadProc(context_destroy, "ltsContextDestroy");
  1055. loadProc(renderer_create, "ltsRendererCreate");
  1056. loadProc(renderer_create_custom, "ltsRendererCustomCreate");
  1057. loadProc(renderer_begin_block, "ltsRendererBeginBlock");
  1058. loadProc(renderer_end_block, "ltsRendererEndBlock");
  1059. loadProc(renderer_abort_block, "ltsRendererAbortBlock");
  1060. loadProc(renderer_get_text_width_a, "ltsRendererGetTextWidthA");
  1061. loadProc(renderer_get_text_width_w, "ltsRendererGetTextWidthW");
  1062. loadProc(renderer_destroy, "ltsRendererDestroy");
  1063. loadProc(font_creator_create, "ltsFontCreatorCreate");
  1064. loadProc(font_creator_get_font_by_name, "ltsFontCreatorGetFontByName");
  1065. loadProc(font_creator_get_font_by_file, "ltsFontCreatorGetFontByFile");
  1066. loadProc(font_creator_get_font_by_stream, "ltsFontCreatorGetFontByStream");
  1067. loadProc(font_creator_destroy, "ltsFontCreatorDestroy");
  1068. loadProc(font_get_post_processor, "ltsFontGetPostProcessor");
  1069. loadProc(font_get_tab_width, "ltsFontGetTabWidth");
  1070. loadProc(font_get_char_spacing, "ltsFontGetCharSpacing");
  1071. loadProc(font_get_line_spacing, "ltsFontGetLineSpacing");
  1072. loadProc(font_get_metric, "ltsFontGetMetric");
  1073. loadProc(font_get_fontname, "ltsFontGetFontname");
  1074. loadProc(font_get_facename, "ltsFontGetFacename");
  1075. loadProc(font_get_stylename, "ltsFontGetStylename");
  1076. loadProc(font_get_fullname, "ltsFontGetFullname");
  1077. loadProc(font_get_copyright, "ltsFontGetCopyright");
  1078. loadProc(font_set_post_processor, "ltsFontSetPostProcessor");
  1079. loadProc(font_set_tab_width, "ltsFontSetTabWidth");
  1080. loadProc(font_set_char_spacing, "ltsFontSetCharSpacing");
  1081. loadProc(font_set_line_spacing, "ltsFontSetLineSpacing");
  1082. loadProc(font_destroy, "ltsFontDestroy");
  1083. loadProc(text_block_get_rect, "ltsTextBlockGetRect");
  1084. loadProc(text_block_get_width, "ltsTextBlockGetWidth");
  1085. loadProc(text_block_get_height, "ltsTextBlockGetHeight");
  1086. loadProc(text_block_get_flags, "ltsTextBlockGetFlags");
  1087. loadProc(text_block_get_top, "ltsTextBlockGetTop");
  1088. loadProc(text_block_get_left, "ltsTextBlockGetLeft");
  1089. loadProc(text_block_get_vert_align, "ltsTextBlockGetVertAlign");
  1090. loadProc(text_block_get_horz_align, "ltsTextBlockGetHorzAlign");
  1091. loadProc(text_block_get_clipping, "ltsTextBlockGetClipping");
  1092. loadProc(text_block_get_color, "ltsTextBlockGetColor");
  1093. loadProc(text_block_get_font, "ltsTextBlockGetFont");
  1094. loadProc(text_block_set_top, "ltsTextBlockSetTop");
  1095. loadProc(text_block_set_left, "ltsTextBlockSetLeft");
  1096. loadProc(text_block_set_vert_align, "ltsTextBlockSetVertAlign");
  1097. loadProc(text_block_set_horz_align, "ltsTextBlockSetHorzAlign");
  1098. loadProc(text_block_set_clipping, "ltsTextBlockSetClipping");
  1099. loadProc(text_block_set_color, "ltsTextBlockSetColor");
  1100. loadProc(text_block_set_font, "ltsTextBlockSetFont");
  1101. loadProc(text_block_get_actual_height, "ltsTextBlockGetActualHeight");
  1102. loadProc(text_block_get_text_width_a, "ltsTextBlockGetTextWidthA");
  1103. loadProc(text_block_get_text_width_w, "ltsTextBlockGetTextWidthW");
  1104. loadProc(text_block_text_out_a, "ltsTextBlockTextOutA");
  1105. loadProc(text_block_text_out_w, "ltsTextBlockTextOutW");
  1106. loadProc(text_block_destroy, "ltsTextBlockDestroy");
  1107. loadProc(image_create, "ltsImageCreate");
  1108. loadProc(image_is_empty, "ltsImageIsEmpty");
  1109. loadProc(image_get_width, "ltsImageGetWidth");
  1110. loadProc(image_get_height, "ltsImageGetHeight");
  1111. loadProc(image_get_line_size, "ltsImageGetLineSize");
  1112. loadProc(image_get_data_size, "ltsImageGetDataSize");
  1113. loadProc(image_get_format, "ltsImageGetFormat");
  1114. loadProc(image_get_data, "ltsImageGetData");
  1115. loadProc(image_get_scanline, "ltsImageGetScanline");
  1116. loadProc(image_get_pixel_at, "ltsImageGetPixelAt");
  1117. loadProc(image_assign, "ltsImageAssign");
  1118. loadProc(image_create_empty, "ltsImageCreateEmpty");
  1119. loadProc(image_load_from_func, "ltsImageLoadFromFunc");
  1120. loadProc(image_resize, "ltsImageResize");
  1121. loadProc(image_fill_color, "ltsImageFillColor");
  1122. loadProc(image_fill_pattern, "ltsImageFillPattern");
  1123. loadProc(image_blend, "ltsImageBlend");
  1124. loadProc(image_blur, "ltsImageBlur");
  1125. loadProc(image_destroy, "ltsImageDestroy");
  1126. loadProc(post_processor_add_range, "ltsPostProcessorAddRange");
  1127. loadProc(post_processor_add_chars, "ltsPostProcessorAddChars");
  1128. loadProc(post_processor_clear_ranges, "ltsPostProcessorClearRanges");
  1129. loadProc(post_processor_execute, "ltsPostProcessorExecute");
  1130. loadProc(post_processor_fill_color_create, "ltsPostProcessorFillColorCreate");
  1131. loadProc(post_processor_fill_pattern_create, "ltsPostProcessorFillPatterCreate");
  1132. loadProc(post_processor_border_create, "ltsPostProcessorBorderCreate");
  1133. loadProc(post_processor_shadow_create, "ltsPostProcessorShadowCreate");
  1134. loadProc(post_processor_custom_create, "ltsPostProcessorCustomCreate");
  1135. loadProc(post_processor_destroy, "ltsPostProcessorDestroy");
  1136. loadProc(char_get_char_code, "ltsCharGetCharCode");
  1137. loadProc(char_get_glyph_metric, "ltsCharGetGlyphMetric");
  1138. loadProc(char_set_glyph_metric, "ltsCharSetGlyphMetric");
  1139. loadProc(initialize, "ltsInitialize");
  1140. loadProc(get_last_error_code, "ltsGetLastErrorCode");
  1141. loadProc(get_last_error_msg, "ltsGetLastErrorMsg");
  1142. loadProc(finalize, "ltsFinalize");
  1143. auto err = initialize();
  1144. if (err != ErrorCode::None)
  1145. throw Exception(std::string("unable to initialize library: ") + get_last_error_msg(), get_last_error_code());
  1146. };
  1147. ~Impl()
  1148. {
  1149. finalize();
  1150. }
  1151. };
  1152. namespace lts
  1153. {
  1154. template <typename T>
  1155. T getFunc(const Library::Impl& impl, Handle handle, ErrorCode (WINAPI * const callback)(Handle, T&))
  1156. {
  1157. T value;
  1158. auto err = callback(handle, value);
  1159. if (err != ErrorCode::None)
  1160. throw Exception(impl.get_last_error_msg(), err);
  1161. return value;
  1162. }
  1163. int getFunc(const Library::Impl& impl, Handle handle, int (WINAPI * const callback)(Handle))
  1164. {
  1165. int value = callback(handle);
  1166. if (value < 0)
  1167. throw Exception(impl.get_last_error_msg(), impl.get_last_error_code());
  1168. return value;
  1169. }
  1170. template<typename T>
  1171. T getFunc(const Library::Impl& impl, Handle handle, T (WINAPI * const callback)(Handle), T invalidValue)
  1172. {
  1173. T value = callback(handle);
  1174. if (value == invalidValue)
  1175. throw Exception(impl.get_last_error_msg(), impl.get_last_error_code());
  1176. return value;
  1177. }
  1178. template <typename T>
  1179. void setFunc(const Library::Impl& impl, Handle handle, ErrorCode (WINAPI * const callback)(Handle, T), const T& value)
  1180. {
  1181. auto err = callback(handle, value);
  1182. if (err != ErrorCode::None)
  1183. throw Exception(impl.get_last_error_msg(), err);
  1184. }
  1185. };
  1186. /* Exception **********************************************************************************************************************/
  1187. inline lts::ErrorCode lts::Exception::getErrorCode() const
  1188. { return _errorCode; }
  1189. inline std::string lts::Exception::getMessage() const
  1190. { return _message; }
  1191. const char* lts::Exception::what() const throw()
  1192. { return _message.c_str(); };
  1193. lts::Exception::Exception(std::string message, ErrorCode errorCode) :
  1194. _message (message),
  1195. _errorCode (errorCode)
  1196. { }
  1197. lts::Exception::~Exception() throw()
  1198. { }
  1199. /* Library ************************************************************************************************************************/
  1200. inline const lts::Library::Impl& lts::Library::getImpl() const
  1201. { return *_impl; };
  1202. inline lts::ErrorCode lts::Library::getLastErrorCode() const
  1203. { return _impl->get_last_error_code(); }
  1204. inline std::string lts::Library::getLastErrorMsg() const
  1205. { return std::string(_impl->get_last_error_msg()); }
  1206. lts::Library::Library(const std::string& libName) :
  1207. _handle (0),
  1208. _impl (NULL)
  1209. {
  1210. _handle = libOpen(libName.c_str());
  1211. if (!_handle)
  1212. throw Exception("unable to open shared library: " + libName, lts::ErrorCode::InvalidLibHandle);
  1213. _impl = new Impl(_handle);
  1214. }
  1215. lts::Library::~Library()
  1216. {
  1217. if (_impl)
  1218. {
  1219. delete _impl;
  1220. _impl = NULL;
  1221. }
  1222. if (_handle)
  1223. {
  1224. libClose(_handle);
  1225. _handle = NULL;
  1226. }
  1227. }
  1228. /* context ************************************************************************************************************************/
  1229. inline lts::ContextHandle lts::Context::getHandle() const
  1230. { return _handle; };
  1231. inline lts::CodePage lts::Context::getCodePage() const
  1232. { return getFunc(_impl, _handle, _impl.context_get_code_page); };
  1233. inline lts::WideChar lts::Context::getDefaultChar() const
  1234. { return getFunc(_impl, _handle, _impl.context_get_default_char); };
  1235. inline void lts::Context::setCodePage(const CodePage value)
  1236. { setFunc(_impl, _handle, _impl.context_set_code_page, value); };
  1237. inline void lts::Context::setDefaultChar(const WideChar value)
  1238. { setFunc(_impl, _handle, _impl.context_set_default_char, value); };
  1239. lts::Context::Context(const Library& library) :
  1240. _handle (NULL),
  1241. _impl (library.getImpl())
  1242. {
  1243. _handle = _impl.context_create();
  1244. if (!_handle)
  1245. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1246. };
  1247. /* char ***************************************************************************************************************************/
  1248. inline lts::CharHandle lts::Char::getHandle() const
  1249. { return _handle; }
  1250. inline lts::WideChar lts::Char::getCharCode() const
  1251. { return getFunc(_impl, _handle, _impl.char_get_char_code); };
  1252. inline lts::GlyphMetric lts::Char::getGlyphMetric() const
  1253. { return getFunc(_impl, _handle, _impl.char_get_glyph_metric); };
  1254. inline void lts::Char::setGlyphMetric(const GlyphMetric& value)
  1255. { setFunc(_impl, _handle, _impl.char_set_glyph_metric, value); };
  1256. lts::Char::Char(const Library::Impl& impl, CharHandle handle) :
  1257. _impl (impl),
  1258. _handle (handle)
  1259. { };
  1260. /* image **************************************************************************************************************************/
  1261. inline lts::ImageHandle lts::Image::getHandle() const
  1262. { return _handle; };
  1263. inline bool lts::Image::getIsEmpty() const
  1264. { return getFunc(_impl, _handle, _impl.image_is_empty); };
  1265. inline int lts::Image::getWidth() const
  1266. { return getFunc(_impl, _handle, _impl.image_get_width); };
  1267. inline int lts::Image::getHeight() const
  1268. { return getFunc(_impl, _handle, _impl.image_get_height); };
  1269. inline int lts::Image::getLineSize() const
  1270. { return getFunc(_impl, _handle, _impl.image_get_line_size); };
  1271. inline int lts::Image::getDataSize() const
  1272. { return getFunc(_impl, _handle, _impl.image_get_data_size); };
  1273. inline lts::Format lts::Image::getFormat() const
  1274. { return getFunc(_impl, _handle, _impl.image_get_format); };
  1275. inline void* lts::Image::getData() const
  1276. { return _impl.image_get_data(_handle); }
  1277. inline void* lts::Image::getScanline(int index) const
  1278. { return _impl.image_get_scanline(_handle, index); }
  1279. bool lts::Image::getPixelAt(int x, int y, Color4f& color) const
  1280. {
  1281. auto err = _impl.image_get_pixel_at(_handle, x, y, color);
  1282. if (err == ErrorCode::InvalidValue)
  1283. return false;
  1284. else if (err == ErrorCode::None)
  1285. return true;
  1286. throw Exception(_impl.get_last_error_msg(), err);
  1287. };
  1288. inline void lts::Image::assign(const Image& image)
  1289. { setFunc(_impl, _handle, _impl.image_assign, image.getHandle()); };
  1290. void lts::Image::createEmpty(Format format, int width, int height)
  1291. {
  1292. auto err = _impl.image_create_empty(_handle, format, width, height);
  1293. if (err != ErrorCode::None)
  1294. throw Exception(_impl.get_last_error_msg(), err);
  1295. };
  1296. void lts::Image::loadFromFunc(LoadCallback callback, void* args)
  1297. {
  1298. LoadArgs la;
  1299. la.callback = callback;
  1300. la.args = args;
  1301. la.image = this;
  1302. _impl.image_load_from_func(_handle, &lts::Image::loadCallback, &la);
  1303. };
  1304. void lts::Image::loadFromFunc(LoadFunction func)
  1305. {
  1306. LoadArgs la;
  1307. la.func = &func;
  1308. la.image = this;
  1309. _impl.image_load_from_func(_handle, &lts::Image::loadCallback, &la);
  1310. };
  1311. void lts::Image::resize(int width, int height, int x, int y)
  1312. {
  1313. auto err = _impl.image_resize(_handle, width, height, x, y);
  1314. if (err != ErrorCode::None)
  1315. throw Exception(_impl.get_last_error_msg(), err);
  1316. };
  1317. void lts::Image::fillColor(Color4f color, ColorChannels mask, ImageModes modes)
  1318. {
  1319. auto err = _impl.image_fill_color(_handle, color, mask, modes);
  1320. if (err != ErrorCode::None)
  1321. throw Exception(_impl.get_last_error_msg(), err);
  1322. };
  1323. void lts::Image::fillPattern(const Image& pattern, int x, int y, ColorChannels mask, ImageModes modes)
  1324. {
  1325. auto err = _impl.image_fill_pattern(_handle, pattern.getHandle(), x, y, mask, modes);
  1326. if (err != ErrorCode::None)
  1327. throw Exception(_impl.get_last_error_msg(), err);
  1328. };
  1329. void lts::Image::blend(const Image& image, int x, int y, BlendCallback callback, void* args)
  1330. {
  1331. BlendArgs ba;
  1332. ba.callback = callback;
  1333. ba.args = args;
  1334. ba.image = this;
  1335. _impl.image_load_from_func(_handle, &lts::Image::loadCallback, &ba);
  1336. };
  1337. void lts::Image::blend(const Image& image, int x, int y, BlendFunction func)
  1338. {
  1339. BlendArgs ba;
  1340. ba.func = &func;
  1341. ba.image = this;
  1342. _impl.image_load_from_func(_handle, &lts::Image::loadFunc, &ba);
  1343. };
  1344. void lts::Image::blur(float horzRad, float horzStr, float vertRad, float vertStr, ColorChannels mask)
  1345. {
  1346. auto err = _impl.image_blur(_handle, horzRad, horzStr, vertRad, vertStr, mask);
  1347. if (err != ErrorCode::None)
  1348. throw Exception(_impl.get_last_error_msg(), err);
  1349. };
  1350. lts::Image::Image(const Library::Impl& impl, ImageHandle handle) :
  1351. _impl (impl),
  1352. _handle (handle),
  1353. _ownsHandle (false)
  1354. { };
  1355. lts::Image::Image(const Context& context) :
  1356. _impl (context._impl),
  1357. _handle (NULL),
  1358. _ownsHandle (true)
  1359. {
  1360. _handle = _impl.image_create(context.getHandle());
  1361. if (!_handle)
  1362. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1363. }
  1364. lts::Image::~Image()
  1365. {
  1366. if (_ownsHandle && _handle)
  1367. _impl.image_destroy(_handle);
  1368. }
  1369. void lts::Image::loadCallback(ImageHandle handle, int x, int y, lts::Color4f& pixel, void* args)
  1370. {
  1371. auto la = static_cast<LoadArgs*>(args);
  1372. la->callback(*la->image, x, y, pixel, la->args);
  1373. }
  1374. void lts::Image::loadFunc(ImageHandle handle, int x, int y, lts::Color4f& pixel, void* args)
  1375. {
  1376. auto la = static_cast<LoadArgs*>(args);
  1377. (*la->func)(*la->image, x, y, pixel);
  1378. }
  1379. void lts::Image::blendCallback(ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args)
  1380. {
  1381. auto ba = static_cast<BlendArgs*>(args);
  1382. result = ba->callback(*ba->image, src, dst, ba->args);
  1383. }
  1384. void lts::Image::blendFunc(ImageHandle handle, lts::Color4f src, lts::Color4f dst, lts::Color4f& result, void* args)
  1385. {
  1386. auto ba = static_cast<BlendArgs*>(args);
  1387. result = (*ba->func)(*ba->image, src, dst);
  1388. }
  1389. /* post processor *****************************************************************************************************************/
  1390. inline void lts::PostProcessor::setHandle(PostProcessorHandle handle)
  1391. { _handle = handle; };
  1392. void lts::PostProcessor::execute(Char& c, Image& i)
  1393. {
  1394. auto err = _impl.post_processor_execute(_handle, c.getHandle(), i.getHandle());
  1395. if (err != ErrorCode::None)
  1396. throw Exception(_impl.get_last_error_msg(), err);
  1397. }
  1398. lts::PostProcessor::PostProcessor(const Context& context) :
  1399. _impl (context._impl),
  1400. _handle (NULL)
  1401. { };
  1402. inline lts::PostProcessorHandle lts::PostProcessor::getHandle() const
  1403. { return _handle; };
  1404. void lts::PostProcessor::addRange(CharRangeUsage usage, WideChar start, WideChar stop)
  1405. {
  1406. auto err = _impl.post_processor_add_range(_handle, usage, start, stop);
  1407. if (err != ErrorCode::None)
  1408. throw Exception(_impl.get_last_error_msg(), err);
  1409. }
  1410. void lts::PostProcessor::addChars(CharRangeUsage usage, WideChar* chars)
  1411. {
  1412. auto err = _impl.post_processor_add_chars(_handle, usage, chars);
  1413. if (err != ErrorCode::None)
  1414. throw Exception(_impl.get_last_error_msg(), err);
  1415. }
  1416. void lts::PostProcessor::clearRanges()
  1417. {
  1418. auto err = _impl.post_processor_clear_ranges(_handle);
  1419. if (err != ErrorCode::None)
  1420. throw Exception(_impl.get_last_error_msg(), err);
  1421. }
  1422. lts::PostProcessor::~PostProcessor()
  1423. {
  1424. if (_handle)
  1425. {
  1426. _impl.post_processor_destroy(_handle);
  1427. _handle = NULL;
  1428. }
  1429. }
  1430. /* post processor fill color ******************************************************************************************************/
  1431. inline const lts::Color4f& lts::PostProcessorFillColor::getColor() const
  1432. { return _color; };
  1433. inline const lts::ImageModes& lts::PostProcessorFillColor::getModes() const
  1434. { return _modes; };
  1435. const lts::ColorChannels& lts::PostProcessorFillColor::getChannels() const
  1436. { return _channels; };
  1437. lts::PostProcessorFillColor::PostProcessorFillColor(
  1438. const Context& context,
  1439. const Color4f& color,
  1440. const ImageModes& modes,
  1441. const ColorChannels& channels) :
  1442. PostProcessor (context),
  1443. _color (color),
  1444. _modes (modes),
  1445. _channels (channels)
  1446. {
  1447. auto handle = _impl.post_processor_fill_color_create(context.getHandle(), color, modes, channels);
  1448. if (!handle)
  1449. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1450. setHandle(handle);
  1451. };
  1452. /* post processor fill pattern ****************************************************************************************************/
  1453. inline const lts::Image& lts::PostProcessorFillPattern::getPattern() const
  1454. { return _pattern; };
  1455. inline const lts::Position& lts::PostProcessorFillPattern::getPosition() const
  1456. { return _position; };
  1457. inline const lts::ImageModes& lts::PostProcessorFillPattern::getModes() const
  1458. { return _modes; };
  1459. inline const lts::ColorChannels& lts::PostProcessorFillPattern::getChannels() const
  1460. { return _channels; };
  1461. lts::PostProcessorFillPattern::PostProcessorFillPattern(
  1462. const Context& context,
  1463. const Image& pattern,
  1464. const Position& pos,
  1465. ImageModes modes,
  1466. ColorChannels channels) :
  1467. PostProcessor (context),
  1468. _pattern (pattern),
  1469. _position (pos),
  1470. _modes (modes),
  1471. _channels (channels)
  1472. {
  1473. auto handle = _impl.post_processor_fill_pattern_create(context.getHandle(), pattern.getHandle(), false, pos, modes, channels);
  1474. if (!handle)
  1475. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1476. setHandle(handle);
  1477. };
  1478. /* post processor border **********************************************************************************************************/
  1479. inline float lts::PostProcessorBorder::getWidth() const
  1480. { return _width; };
  1481. inline float lts::PostProcessorBorder::getStrength() const
  1482. { return _strength; };
  1483. inline lts::Color4f lts::PostProcessorBorder::getColor() const
  1484. { return _color; };
  1485. inline bool lts::PostProcessorBorder::getKeepSize() const
  1486. { return _keepSize; };
  1487. lts::PostProcessorBorder::PostProcessorBorder(
  1488. const Context& context,
  1489. float width,
  1490. float strength,
  1491. Color4f color,
  1492. bool keepSize) :
  1493. PostProcessor (context),
  1494. _width (width),
  1495. _strength (strength),
  1496. _color (color),
  1497. _keepSize (keepSize)
  1498. {
  1499. auto handle = _impl.post_processor_border_create(context.getHandle(), width, strength, color, keepSize);
  1500. if (!handle)
  1501. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1502. setHandle(handle);
  1503. };
  1504. /* post processor shadow **********************************************************************************************************/
  1505. inline float lts::PostProcessorShadow::getRadius() const
  1506. { return _radius; };
  1507. inline float lts::PostProcessorShadow::getStrength() const
  1508. { return _strength; };
  1509. inline lts::Position lts::PostProcessorShadow::getOffset() const
  1510. { return _offset; };
  1511. inline lts::Color4f lts::PostProcessorShadow::getColor() const
  1512. { return _color; };
  1513. lts::PostProcessorShadow::PostProcessorShadow(
  1514. const Context& context,
  1515. float radius,
  1516. float strength,
  1517. const Position& offset,
  1518. const Color4f& color) :
  1519. PostProcessor (context),
  1520. _radius (radius),
  1521. _strength (strength),
  1522. _offset (offset),
  1523. _color (color)
  1524. {
  1525. auto handle = _impl.post_processor_shadow_create(context.getHandle(), radius, strength, offset, color);
  1526. if (!handle)
  1527. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1528. setHandle(handle);
  1529. }
  1530. /* post processor custom **********************************************************************************************************/
  1531. void lts::PostProcessorCustom::execute(Char& c, Image& i)
  1532. { /* do not call base class to avoid infinite loop */ }
  1533. lts::PostProcessorCustom::PostProcessorCustom(const Context& context) :
  1534. PostProcessor(context)
  1535. {
  1536. Library::Impl::PostProcessorData ppd;
  1537. ppd.args = this;
  1538. ppd.execute = &PostProcessorCustom::executeCallback;
  1539. auto handle = _impl.post_processor_custom_create(context.getHandle(), ppd);
  1540. if (!handle)
  1541. throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code());
  1542. setHandle(handle);
  1543. }
  1544. void lts::PostProcessorCustom::executeCallback(CharHandle charHandle, ImageHandle imageHandle, void* args)
  1545. {
  1546. auto ppc = static_cast<PostProcessorCustom*>(args);
  1547. Char chr(ppc->_impl, charHandle);
  1548. Image img(ppc->_impl, imageHandle);
  1549. ppc->execute(chr, img);
  1550. }
  1551. /* post processor list ************************************************************************************************************/
  1552. template<class T>
  1553. void lts::PostProcessorList<T>::execute(Char& c, Image& i)
  1554. {
  1555. for (auto& pp : *this)
  1556. pp.execute(c, i);
  1557. }
  1558. template<class T>
  1559. lts::PostProcessorList<T>::PostProcessorList(const Context& context) :
  1560. PostProcessor(context)
  1561. { };
  1562. /* font ***************************************************************************************************************************/
  1563. inline lts::FontHandle lts::Font::getHandle() const
  1564. { return _handle; };
  1565. inline const lts::Font::Names& lts::Font::getNames() const
  1566. { return _names; };
  1567. inline const lts::FontMetric& lts::Font::getMetric() const
  1568. { return _metric; };
  1569. inline lts::PostProcessor* lts::Font::getPostProcessor() const
  1570. { return _postProcessor; };
  1571. inline int lts::Font::getTabWidth() const
  1572. { return getFunc<int>(_impl, _handle, _impl.font_get_tab_width); };
  1573. inline int lts::Font::getCharSpacing() const
  1574. { return getFunc<int>(_impl, _handle, _impl.font_get_char_spacing); };
  1575. inline int lts::Font::getLineSpacing() const
  1576. { return getFunc<float>(_impl, _handle, _impl.font_get_line_spacing); };
  1577. inline void lts::Font::setPostProcessor(PostProcessor* value)
  1578. {
  1579. _postProcessor = value;
  1580. setFunc(_impl, _handle, _impl.font_set_post_processor, (value ? value->getHandle() : NULL));
  1581. };
  1582. inline void lts::Font::setTabWidth(int value)
  1583. { setFunc<int>(_impl, _handle, _impl.font_set_tab_width, value); };
  1584. inline void lts::Font::setCharSpacing(int value)
  1585. { setFunc<int>(_impl, _handle, _impl.font_set_char_spacing, value); };
  1586. inline void lts::Font::setLineSpacing(int value)
  1587. { setFunc<float>(_impl, _handle, _impl.font_set_line_spacing, value); };
  1588. lts::Font::Font(Library::Impl& impl, FontHandle handle) :
  1589. _impl (impl),
  1590. _handle (handle)
  1591. {
  1592. _metric = getFunc(_impl, _handle, _impl.font_get_metric);
  1593. _names.fontname = std::string(getFunc<const char*>(_impl, _handle, _impl.font_get_fontname, NULL));
  1594. _names.facename = std::string(getFunc<const char*>(_impl, _handle, _impl.font_get_facename, NULL));
  1595. _names.stylename = std::string(getFunc<const char*>(_impl, _handle, _impl.font_get_stylename, NULL));
  1596. _names.fullname = std::string(getFunc<const char*>(_impl, _handle, _impl.font_get_fullname, NULL));
  1597. };
  1598. lts::Font::~Font()
  1599. {
  1600. if (_handle)
  1601. {
  1602. _impl.font_destroy(_handle);
  1603. _handle = NULL;
  1604. }
  1605. }
  1606. /* font creator *******************************************************************************************************************
  1607. class FontCreator
  1608. {
  1609. private:
  1610. const Library::Impl& _impl;
  1611. FontCreatorHandle _handle;
  1612. FontCreator(const FontCreator&);
  1613. protected:
  1614. FontCreator(FontCreatorHandle handle);
  1615. public:
  1616. FontCreatorHandle getHandle() const;
  1617. FontPtrU getFontByName (const std::string& fontname, int size, FontStyles styles, AntiAliasing antiAliasing);
  1618. FontPtrU getFontByFile (const std::string& filename, int size, FontStyles styles, AntiAliasing antiAliasing);
  1619. FontPtrU getFontByStream(const std::istream& stream, int size, FontStyles styles, AntiAliasing antiAliasing);
  1620. };
  1621. /* font creator GDI ***************************************************************************************************************
  1622. class FontCreatorGDI
  1623. {
  1624. public:
  1625. FontCreatorGDI(const Context& context);
  1626. };
  1627. /* font creator free type *********************************************************************************************************
  1628. class FontCreatorFreeType
  1629. {
  1630. public:
  1631. FontCreatorFreeType(const Context& context);
  1632. };
  1633. /* text block *********************************************************************************************************************
  1634. class TextBlock
  1635. {
  1636. private:
  1637. const Library::Impl& _impl;
  1638. TextBlockHandle _handle;
  1639. Font* _font;
  1640. TextBlock(const TextBlock&);
  1641. public:
  1642. TextBlockHandle getHandle() const;
  1643. Rect getRect() const;
  1644. int getWidth() const;
  1645. int getHeight() const;
  1646. BlockFlags getFlags() const;
  1647. int getTop() const;
  1648. int getLeft() const;
  1649. VertAlign getVertAlign() const;
  1650. HorzAlign getHorzAlign() const;
  1651. Clipping getClipping() const;
  1652. Color4f getColor() const;
  1653. Font* getFont() const;
  1654. void setTop(int value);
  1655. void setLeft(int value);
  1656. void setVertAlign(VertAlign value);
  1657. void setHorzAlign(HorzAlign value);
  1658. void setClipping(Clipping value);
  1659. void setColor(const Color4f& value);
  1660. void setFont(Font* font);
  1661. int getActualBlockHeight() const;
  1662. void textOutA(const char* text);
  1663. void textOutA(const std::string& text);
  1664. void textOutW(const WideChar* text);
  1665. void textOutW(const std::wstring& text);
  1666. int getTextWidthA(const char* text);
  1667. int getTextWidthA(const std::string& text);
  1668. int getTextWidthW(const WideChar* text);
  1669. int getTextWidthW(const std::wstring& text);
  1670. TextBlock(TextBlockHandle handle);
  1671. virtual ~TextBlock();
  1672. };
  1673. typedef std::unique_ptr<TextBlock> TextBlockPtrU;
  1674. /* renderer ***********************************************************************************************************************
  1675. class Renderer
  1676. {
  1677. private:
  1678. RendererHandle _handle;
  1679. Renderer(const Renderer&) = delete;
  1680. protected:
  1681. const Library::Impl& _impl;
  1682. Renderer(RendererHandle handle);
  1683. public:
  1684. RendererHandle getHandle() const;
  1685. TextBlockPtrU beginBlock(int top, int left, int width, int height, BlockFlags flags);
  1686. void endBlock(TextBlockPtrU block);
  1687. void abortBlock(TextBlockPtrU block);
  1688. int getTextWidthA(Font* font, const char* text);
  1689. int getTextWidthA(Font* font, const std::string& text);
  1690. int getTextWidthW(Font* font, const WideChar* text);
  1691. int getTextWidthW(Font* font, const std::wstring& text);
  1692. virtual ~Renderer();
  1693. };
  1694. /* renderer OpenGL ****************************************************************************************************************
  1695. class RendererOpenGL : public Renderer
  1696. {
  1697. public:
  1698. RendererOpenGL(const Context& context, Format format);
  1699. };
  1700. /* renderer OpenGLES **************************************************************************************************************
  1701. class RendererOpenGLES : public Renderer
  1702. {
  1703. public:
  1704. RendererOpenGLES(const Context& context, Format format);
  1705. };
  1706. /* renderer custom ****************************************************************************************************************
  1707. class RendererCustom : public Renderer
  1708. {
  1709. protected:
  1710. Position _drawPos;
  1711. Color4f _color;
  1712. virtual void beginRender();
  1713. virtual void endRender();
  1714. virtual Position getDrawPos();
  1715. virtual void setDrawPos(const Position& value);
  1716. virtual void moveDrawPos(const Position& offset);
  1717. virtual void setColor(const Color4f& color);
  1718. virtual void render(RenderRef ref, int forcedWidth = 0);
  1719. virtual RenderRef createRenderRef(Char& c, const Image& img);
  1720. virtual void freeRenderRef(RenderRef ref);
  1721. public:
  1722. RendererCustom(const Context& context, Format format);
  1723. };
  1724. */
  1725. #endif /* LIB_TEXT_SUITE_HPP */