From 56273b6cafd6b5ce6291336b1bbfdf2cfe5ea209 Mon Sep 17 00:00:00 2001 From: Bergmann89 Date: Thu, 5 Nov 2015 20:50:45 +0100 Subject: [PATCH] * implemented post processors and font --- header/libTextSuite.hpp | 448 ++++++++++++++++++++++++++-------------- 1 file changed, 293 insertions(+), 155 deletions(-) diff --git a/header/libTextSuite.hpp b/header/libTextSuite.hpp index dedaf90..343eac9 100644 --- a/header/libTextSuite.hpp +++ b/header/libTextSuite.hpp @@ -217,6 +217,9 @@ namespace lts Flags(const TEnum& e) : _value(1 << e) { }; + Flags(const Flags& f) : + _value(f._value) + { }; Flags(const std::initializer_list& vec) : _value(0) { @@ -275,6 +278,12 @@ namespace lts float arr[4]; }; + union ImageModes + { + struct { ImageMode r, g, b, a; }; + ImageMode arr[4]; + }; + struct GlyphMetric { Position glyphOrigin; @@ -314,7 +323,6 @@ namespace lts typedef float Vector4f[4]; typedef Vector4f Matrix4f[4]; - typedef ImageMode ImageModes[4]; /* exeption ***********************************************************************************************************************/ class Exception : public std::exception @@ -361,6 +369,7 @@ namespace lts class Context { friend class Image; + friend class PostProcessor; private: const Library::Impl& _impl; ContextHandle _handle; @@ -459,12 +468,13 @@ namespace lts class PostProcessor { private: - const Library::Impl& _impl; - PostProcessorHandle _handle; + PostProcessorHandle _handle; PostProcessor(const PostProcessor&) = delete; protected: - virtual void execute(Char c, Image i); - PostProcessor(PostProcessorHandle handle); + const Library::Impl& _impl; + void setHandle(PostProcessorHandle handle); + virtual void execute(Char& c, Image& i); + PostProcessor(const Context& context); public: PostProcessorHandle getHandle() const; @@ -479,7 +489,6 @@ namespace lts class PostProcessorFillColor final : public PostProcessor { private: - const Library::Impl& _impl; Color4f _color; ImageModes _modes; ColorChannels _channels; @@ -488,7 +497,11 @@ namespace lts const ImageModes& getModes() const; const ColorChannels& getChannels() const; - PostProcessorFillColor(const Context& context, const Color4f& color, const ImageModes& modes, const ColorChannels& channels); + PostProcessorFillColor( + const Context& context, + const Color4f& color, + const ImageModes& modes, + const ColorChannels& channels); }; /* post processor fill pattern ****************************************************************************************************/ @@ -496,22 +509,21 @@ namespace lts { private: const Image& _pattern; - bool _ownsPattern; Position _position; ImageModes _modes; ColorChannels _channels; - protected: - const Library::Impl& _impl; public: const Image& getPattern() const; - bool getOwnsPattern() const; const Position& getPosition() const; const ImageModes& getModes() const; const ColorChannels& getChannels() const; - PostProcessorFillPattern(const Context& context, const Image& pattern, bool ownsPattern, - const Position& pos, ImageModes modes, ColorChannels channels); - virtual ~PostProcessorFillPattern(); + PostProcessorFillPattern( + const Context& context, + const Image& pattern, + const Position& pos, + ImageModes modes, + ColorChannels channels); }; /* post processor border **********************************************************************************************************/ @@ -537,22 +549,26 @@ namespace lts private: float _radius; float _strength; - Position _position; + Position _offset; Color4f _color; public: float getRadius() const; float getStrength() const; - Position getPosition() const; + Position getOffset() const; Color4f getColor() const; PostProcessorShadow(const Context& context, float radius, float strength, const Position& offset, const Color4f& color); }; /* post processor custom **********************************************************************************************************/ - class PostProcessorCustom + class PostProcessorCustom : public PostProcessor { protected: - virtual void execute(Char c, Image i); + virtual void execute(Char& c, Image& i); + public: + PostProcessorCustom(const Context& context); + public: + static void WINAPI executeCallback(CharHandle charHandle, ImageHandle imageHandle, void* args); }; /* post processor list ************************************************************************************************************/ @@ -561,7 +577,7 @@ namespace lts public std::list { protected: - virtual void execute(Char c, Image i); + virtual void execute(Char& c, Image& i); public: PostProcessorList(const Context& context); }; @@ -599,7 +615,7 @@ namespace lts void setCharSpacing(int value); void setLineSpacing(int value); - Font(FontHandle handle); + Font(Library::Impl& impl, FontHandle handle); virtual ~Font(); }; typedef std::unique_ptr FontPtrU; @@ -795,7 +811,7 @@ public: /* stream */ public: /* custom post processor */ struct PostProcessorData { - typedef void (WINAPI *post_processor_execute_t)(lts::CharHandle charHandle, lts::ImageHandle imageHandle); + typedef void (WINAPI *post_processor_execute_t)(lts::CharHandle charHandle, lts::ImageHandle imageHandle, void* args); void* args; post_processor_execute_t execute; @@ -863,7 +879,7 @@ public: /* callbacks */ typedef const char* (WINAPI *font_get_fontname_t) (lts::FontHandle handle); typedef const char* (WINAPI *font_get_facename_t) (lts::FontHandle handle); typedef const char* (WINAPI *font_get_stylename_t) (lts::FontHandle handle); - typedef const char* (WINAPI *font_get_fillname_t) (lts::FontHandle handle); + typedef const char* (WINAPI *font_get_fullname_t) (lts::FontHandle handle); typedef const char* (WINAPI *font_get_copyright_t) (lts::FontHandle handle); typedef lts::ErrorCode (WINAPI *font_set_post_processor_t) (lts::FontHandle handle, lts::PostProcessorHandle pp); typedef lts::ErrorCode (WINAPI *font_set_tab_width_t) (lts::FontHandle handle, int value); @@ -920,10 +936,10 @@ public: /* callbacks */ typedef lts::ErrorCode (WINAPI *post_processor_add_chars_t) (lts::PostProcessorHandle handle, lts::CharRangeUsage usage, const WideChar* chars); typedef lts::ErrorCode (WINAPI *post_processor_clear_ranges_t) (lts::PostProcessorHandle handle); typedef lts::ErrorCode (WINAPI *post_processor_execute_t) (lts::PostProcessorHandle handle, lts::CharHandle charHandle, lts::ImageHandle image); - typedef lts::PostProcessorHandle(WINAPI *post_processor_fill_color_create_t) (lts::PostProcessorHandle handle, lts::Color4f color, lts::ImageModes modes, lts::ColorChannels channels); - 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); - typedef lts::PostProcessorHandle(WINAPI *post_processor_border_create_t) (lts::PostProcessorHandle handle, float width, float strength, lts::Color4f color, bool keepSize); - typedef lts::PostProcessorHandle(WINAPI *post_processor_shadow_create_t) (lts::PostProcessorHandle handle, float radius, float strength, lts::Position offset, lts::Color4f color); + 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); + 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); + typedef lts::PostProcessorHandle(WINAPI *post_processor_border_create_t) (lts::PostProcessorHandle handle, float width, float strength, const lts::Color4f color, bool keepSize); + typedef lts::PostProcessorHandle(WINAPI *post_processor_shadow_create_t) (lts::PostProcessorHandle handle, float radius, float strength, const lts::Position offset, const lts::Color4f color); typedef lts::PostProcessorHandle(WINAPI *post_processor_custom_create_t) (lts::PostProcessorHandle handle, const PostProcessorData& data); typedef lts::ErrorCode (WINAPI *post_processor_destroy_t) (lts::PostProcessorHandle handle); @@ -980,7 +996,7 @@ public: font_get_fontname_t font_get_fontname; font_get_facename_t font_get_facename; font_get_stylename_t font_get_stylename; - font_get_fillname_t font_get_fillname; + font_get_fullname_t font_get_fullname; font_get_copyright_t font_get_copyright; font_set_post_processor_t font_set_post_processor; font_set_tab_width_t font_set_tab_width; @@ -1088,7 +1104,7 @@ public: font_get_fontname (NULL), font_get_facename (NULL), font_get_stylename (NULL), - font_get_fillname (NULL), + font_get_fullname (NULL), font_get_copyright (NULL), font_set_post_processor (NULL), font_set_tab_width (NULL), @@ -1190,7 +1206,7 @@ public: loadProc(font_get_fontname, "ltsFontGetFontname"); loadProc(font_get_facename, "ltsFontGetFacename"); loadProc(font_get_stylename, "ltsFontGetStylename"); - loadProc(font_get_fillname, "ltsFontGetFullname"); + loadProc(font_get_fullname, "ltsFontGetFullname"); loadProc(font_get_copyright, "ltsFontGetCopyright"); loadProc(font_set_post_processor, "ltsFontSetPostProcessor"); loadProc(font_set_tab_width, "ltsFontSetTabWidth"); @@ -1294,6 +1310,15 @@ namespace lts return value; } + template + T getFunc(const Library::Impl& impl, Handle handle, T (WINAPI * const callback)(Handle), T invalidValue) + { + T value = callback(handle); + if (value == invalidValue) + throw Exception(impl.get_last_error_msg(), impl.get_last_error_code()); + return value; + } + template void setFunc(const Library::Impl& impl, Handle handle, ErrorCode (WINAPI * const callback)(Handle, T), const T& value) { @@ -1554,154 +1579,267 @@ void lts::Image::blendFunc(ImageHandle handle, lts::Color4f src, lts::Color4f ds result = (*ba->func)(*ba->image, src, dst); } -/* post processor ***************************************************************************************************************** -class PostProcessor +/* post processor *****************************************************************************************************************/ +inline void lts::PostProcessor::setHandle(PostProcessorHandle handle) + { _handle = handle; }; + +void lts::PostProcessor::execute(Char& c, Image& i) { -private: - const Library::Impl& _impl; - PostProcessorHandle _handle; - PostProcessor(const PostProcessor&) = delete; -protected: - virtual void execute(Char c, Image i); - PostProcessor(PostProcessorHandle handle); -public: - PostProcessorHandle getHandle() const; - - void addRange(CharRangeUsage usage, WideChar start, WideChar stop); - void addChars(CharRangeUsage usage, WideChar* chars); - void clearRanges(); - - virtual ~PostProcessor(); -}; + auto err = _impl.post_processor_execute(_handle, c.getHandle(), i.getHandle()); + if (err != ErrorCode::None) + throw Exception(_impl.get_last_error_msg(), err); +} -/* post processor fill color ****************************************************************************************************** -class PostProcessorFillColor final : public PostProcessor +lts::PostProcessor::PostProcessor(const Context& context) : + _impl (context._impl), + _handle (NULL) + { }; + +inline lts::PostProcessorHandle lts::PostProcessor::getHandle() const + { return _handle; }; + +void lts::PostProcessor::addRange(CharRangeUsage usage, WideChar start, WideChar stop) { -private: - const Library::Impl& _impl; - Color4f _color; - ImageModes _modes; - ColorChannels _channels; -public: - const Color4f& getColor() const; - const ImageModes& getModes() const; - const ColorChannels& getChannels() const; - - PostProcessorFillColor(const Context& context, const Color4f& color, const ImageModes& modes, const ColorChannels& channels); + auto err = _impl.post_processor_add_range(_handle, usage, start, stop); + if (err != ErrorCode::None) + throw Exception(_impl.get_last_error_msg(), err); +} + +void lts::PostProcessor::addChars(CharRangeUsage usage, WideChar* chars) +{ + auto err = _impl.post_processor_add_chars(_handle, usage, chars); + if (err != ErrorCode::None) + throw Exception(_impl.get_last_error_msg(), err); +} + +void lts::PostProcessor::clearRanges() +{ + auto err = _impl.post_processor_clear_ranges(_handle); + if (err != ErrorCode::None) + throw Exception(_impl.get_last_error_msg(), err); +} + +lts::PostProcessor::~PostProcessor() +{ + if (_handle) + { + _impl.post_processor_destroy(_handle); + _handle = NULL; + } +} + +/* post processor fill color ******************************************************************************************************/ +inline const lts::Color4f& lts::PostProcessorFillColor::getColor() const + { return _color; }; + +inline const lts::ImageModes& lts::PostProcessorFillColor::getModes() const + { return _modes; }; + +const lts::ColorChannels& lts::PostProcessorFillColor::getChannels() const + { return _channels; }; + +lts::PostProcessorFillColor::PostProcessorFillColor( + const Context& context, + const Color4f& color, + const ImageModes& modes, + const ColorChannels& channels) : + PostProcessor (context), + _color (color), + _modes (modes), + _channels (channels) +{ + auto handle = _impl.post_processor_fill_color_create(context.getHandle(), color, modes, channels); + if (!handle) + throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code()); + setHandle(handle); }; -/* post processor fill pattern **************************************************************************************************** -class PostProcessorFillPattern final : public PostProcessor +/* post processor fill pattern ****************************************************************************************************/ +inline const lts::Image& lts::PostProcessorFillPattern::getPattern() const + { return _pattern; }; + +inline const lts::Position& lts::PostProcessorFillPattern::getPosition() const + { return _position; }; + +inline const lts::ImageModes& lts::PostProcessorFillPattern::getModes() const + { return _modes; }; + +inline const lts::ColorChannels& lts::PostProcessorFillPattern::getChannels() const + { return _channels; }; + +lts::PostProcessorFillPattern::PostProcessorFillPattern( + const Context& context, + const Image& pattern, + const Position& pos, + ImageModes modes, + ColorChannels channels) : + PostProcessor (context), + _pattern (pattern), + _position (pos), + _modes (modes), + _channels (channels) { -private: - const Image& _pattern; - bool _ownsPattern; - Position _position; - ImageModes _modes; - ColorChannels _channels; -protected: - const Library::Impl& _impl; -public: - const Image& getPattern() const; - bool getOwnsPattern() const; - const Position& getPosition() const; - const ImageModes& getModes() const; - const ColorChannels& getChannels() const; - - PostProcessorFillPattern(const Context& context, const Image& pattern, bool ownsPattern, - const Position& pos, ImageModes modes, ColorChannels channels); - virtual ~PostProcessorFillPattern(); + auto handle = _impl.post_processor_fill_pattern_create(context.getHandle(), pattern.getHandle(), false, pos, modes, channels); + if (!handle) + throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code()); + setHandle(handle); }; -/* post processor border ********************************************************************************************************** -class PostProcessorBorder final : public PostProcessor +/* post processor border **********************************************************************************************************/ +inline float lts::PostProcessorBorder::getWidth() const + { return _width; }; + +inline float lts::PostProcessorBorder::getStrength() const + { return _strength; }; + +inline lts::Color4f lts::PostProcessorBorder::getColor() const + { return _color; }; + +inline bool lts::PostProcessorBorder::getKeepSize() const + { return _keepSize; }; + +lts::PostProcessorBorder::PostProcessorBorder( + const Context& context, + float width, + float strength, + Color4f color, + bool keepSize) : + PostProcessor (context), + _width (width), + _strength (strength), + _color (color), + _keepSize (keepSize) { -private: - float _width; - float _strength; - Color4f _color; - bool _keepSize; -public: - float getWidth() const; - float getStrength() const; - Color4f getColor() const; - bool getKeepSize() const; - - PostProcessorBorder(const Context& context, float width, float strength, Color4f color, bool keepSize); + auto handle = _impl.post_processor_border_create(context.getHandle(), width, strength, color, keepSize); + if (!handle) + throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code()); + setHandle(handle); }; -/* post processor shadow ********************************************************************************************************** -class PostProcessorShadow final : public PostProcessor +/* post processor shadow **********************************************************************************************************/ +inline float lts::PostProcessorShadow::getRadius() const + { return _radius; }; + +inline float lts::PostProcessorShadow::getStrength() const + { return _strength; }; + +inline lts::Position lts::PostProcessorShadow::getOffset() const + { return _offset; }; + +inline lts::Color4f lts::PostProcessorShadow::getColor() const + { return _color; }; + +lts::PostProcessorShadow::PostProcessorShadow( + const Context& context, + float radius, + float strength, + const Position& offset, + const Color4f& color) : + PostProcessor (context), + _radius (radius), + _strength (strength), + _offset (offset), + _color (color) { -private: - float _radius; - float _strength; - Position _position; - Color4f _color; -public: - float getRadius() const; - float getStrength() const; - Position getPosition() const; - Color4f getColor() const; + auto handle = _impl.post_processor_shadow_create(context.getHandle(), radius, strength, offset, color); + if (!handle) + throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code()); + setHandle(handle); +} + +/* post processor custom **********************************************************************************************************/ +void lts::PostProcessorCustom::execute(Char& c, Image& i) + { /* do not call base class to avoid infinite loop */ } + +lts::PostProcessorCustom::PostProcessorCustom(const Context& context) : + PostProcessor(context) +{ + Library::Impl::PostProcessorData ppd; + ppd.args = this; + ppd.execute = &PostProcessorCustom::executeCallback; + auto handle = _impl.post_processor_custom_create(context.getHandle(), ppd); + if (!handle) + throw Exception(_impl.get_last_error_msg(), _impl.get_last_error_code()); + setHandle(handle); +} + +void lts::PostProcessorCustom::executeCallback(CharHandle charHandle, ImageHandle imageHandle, void* args) +{ + auto ppc = static_cast(args); + Char chr(ppc->_impl, charHandle); + Image img(ppc->_impl, imageHandle); + ppc->execute(chr, img); +} + +/* post processor list ************************************************************************************************************/ +template +void lts::PostProcessorList::execute(Char& c, Image& i) +{ + for (auto& pp : *this) + pp.execute(c, i); +} + +template +lts::PostProcessorList::PostProcessorList(const Context& context) : + PostProcessor(context) + { }; + +/* font ***************************************************************************************************************************/ +inline lts::FontHandle lts::Font::getHandle() const + { return _handle; }; - PostProcessorShadow(const Context& context, float radius, float strength, const Position& offset, const Color4f& color); -}; +inline const lts::Font::Names& lts::Font::getNames() const + { return _names; }; + +inline const lts::FontMetric& lts::Font::getMetric() const + { return _metric; }; + +inline lts::PostProcessor* lts::Font::getPostProcessor() const + { return _postProcessor; }; + +inline int lts::Font::getTabWidth() const + { return getFunc(_impl, _handle, _impl.font_get_tab_width); }; -/* post processor custom ********************************************************************************************************** -class PostProcessorCustom +inline int lts::Font::getCharSpacing() const + { return getFunc(_impl, _handle, _impl.font_get_char_spacing); }; + +inline int lts::Font::getLineSpacing() const + { return getFunc(_impl, _handle, _impl.font_get_line_spacing); }; + +inline void lts::Font::setPostProcessor(PostProcessor* value) { -protected: - virtual void execute(Char c, Image i); + _postProcessor = value; + setFunc(_impl, _handle, _impl.font_set_post_processor, (value ? value->getHandle() : NULL)); }; -/* post processor list ************************************************************************************************************ -template -class PostProcessorList : public PostProcessorCustom, - public std::list +inline void lts::Font::setTabWidth(int value) + { setFunc(_impl, _handle, _impl.font_set_tab_width, value); }; + +inline void lts::Font::setCharSpacing(int value) + { setFunc(_impl, _handle, _impl.font_set_char_spacing, value); }; + +inline void lts::Font::setLineSpacing(int value) + { setFunc(_impl, _handle, _impl.font_set_line_spacing, value); }; + +lts::Font::Font(Library::Impl& impl, FontHandle handle) : + _impl (impl), + _handle (handle) { -protected: - virtual void execute(Char c, Image i); -public: - PostProcessorList(const Context& context); + _metric = getFunc(_impl, _handle, _impl.font_get_metric); + _names.fontname = std::string(getFunc(_impl, _handle, _impl.font_get_fontname, NULL)); + _names.facename = std::string(getFunc(_impl, _handle, _impl.font_get_facename, NULL)); + _names.stylename = std::string(getFunc(_impl, _handle, _impl.font_get_stylename, NULL)); + _names.fullname = std::string(getFunc(_impl, _handle, _impl.font_get_fullname, NULL)); }; -/* font *************************************************************************************************************************** -class Font +lts::Font::~Font() { -public: - struct Names + if (_handle) { - std::string fontname; - std::string copyright; - std::string facename; - std::string stylename; - std::string fullname; - }; -private: - const Library::Impl& _impl; - FontHandle _handle; - FontMetric _metric; - Names _names; - PostProcessor* _postProcessor; - Font(const Font&) = delete; -public: - FontHandle getHandle() const; - const Names& getNames() const; - const FontMetric& getMetric() const; - PostProcessor* getPostProcessor() const; - int getTabWidth() const; - int getCharSpacing() const; - int getLineSpacing() const; - - void setPostProcessor(PostProcessor* value); - void setTabWidth(int value); - void setCharSpacing(int value); - void setLineSpacing(int value); - - Font(FontHandle handle); - virtual ~Font(); -}; -typedef std::unique_ptr FontPtrU; + _impl.font_destroy(_handle); + _handle = NULL; + } +} /* font creator ******************************************************************************************************************* class FontCreator