diff --git a/inc/basic_widgets/core/renderer.hpp b/inc/basic_widgets/core/renderer.hpp index afab015..d59e70f 100644 --- a/inc/basic_widgets/core/renderer.hpp +++ b/inc/basic_widgets/core/renderer.hpp @@ -44,8 +44,7 @@ namespace bwidgets [[nodiscard]] auto blend_mode() -> SDL_BlendMode; auto blend_mode(SDL_BlendMode) -> Renderer*; auto clear() -> Renderer*; - auto copy(const Texture*, const SDL_Rect*, const SDL_Rect*) - -> Renderer*; + auto copy(const Texture*, const SDL_Rect*, const SDL_Rect*) -> Renderer*; [[nodiscard]] auto draw_color() -> Color; auto draw_color(const Color&) -> Renderer*; auto draw_line(const SDL_Point&, const SDL_Point&) -> Renderer*; @@ -61,20 +60,17 @@ namespace bwidgets [[nodiscard]] auto viewport() noexcept -> SDL_Rect; auto viewport(const SDL_Rect*) -> Renderer*; - inline auto copy(const Texture* t, const SDL_Rect* src, - const SDL_Rect& dst) + inline auto copy(const Texture* t, const SDL_Rect* src, const SDL_Rect& dst) { const auto d = dst; return copy(t, src, &d); } - inline auto copy(const Texture* t, const SDL_Rect& src, - const SDL_Rect* dst) + inline auto copy(const Texture* t, const SDL_Rect& src, const SDL_Rect* dst) { const auto s = src; return copy(t, &s, dst); } - inline auto copy(const Texture* t, const SDL_Rect& src, - const SDL_Rect& dst) + inline auto copy(const Texture* t, const SDL_Rect& src, const SDL_Rect& dst) { const auto s = src; const auto d = dst; diff --git a/inc/basic_widgets/core/texture.hpp b/inc/basic_widgets/core/texture.hpp index 158a99a..bfb3d08 100644 --- a/inc/basic_widgets/core/texture.hpp +++ b/inc/basic_widgets/core/texture.hpp @@ -29,8 +29,7 @@ namespace bwidgets public: Texture(SDL_Texture*); - Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess, - int, int); + Texture(Renderer*, SDL_PixelFormatEnum, SDL_TextureAccess, int, int); Texture(Renderer*, SDL_Surface*); Texture(const Texture&) = delete; Texture(Texture&&) = delete; @@ -65,10 +64,11 @@ namespace bwidgets [[nodiscard]] static inline auto attributes(SDL_Texture* t) { Attr attr {}; - SDLError::success_or_throw(SDL_QueryTexture(t, &attr.format_raw, - (int*)&attr.access, &attr.w, - &attr.h), - __FILE__, __FUNCTION__, __LINE__); + SDLError::success_or_throw( + SDL_QueryTexture(t, &attr.format_raw, + // NOLINTNEXTLINE(cppcoreguidelines-pro-type-cstyle-cast) + (int*)&attr.access, &attr.w, &attr.h), + __FILE__, __FUNCTION__, __LINE__); attr.format = SDLError::ptr_or_throw(SDL_AllocFormat(attr.format_raw), __FILE__, __FUNCTION__, __LINE__); diff --git a/inc/basic_widgets/core/type/color.hpp b/inc/basic_widgets/core/type/color.hpp index 03ba1a2..74741e5 100644 --- a/inc/basic_widgets/core/type/color.hpp +++ b/inc/basic_widgets/core/type/color.hpp @@ -30,9 +30,8 @@ namespace bwidgets bool _operate_on_colors; template - inline auto - _operate(N operand, - const std::function& operator_) const noexcept + inline auto _operate(N operand, + const std::function& operator_) const noexcept { Color c(sdl_type, _operate_on_alpha, _operate_on_colors); if (_operate_on_alpha) { diff --git a/inc/basic_widgets/w/aligned_layout.hpp b/inc/basic_widgets/w/aligned_layout.hpp index 47b461e..ac47d3f 100644 --- a/inc/basic_widgets/w/aligned_layout.hpp +++ b/inc/basic_widgets/w/aligned_layout.hpp @@ -17,7 +17,9 @@ namespace bwidgets VERTICAL } alignment; - AlignedLayout(Alignment align, Widget* p = nullptr) noexcept : Layout(p), alignment(align) {} + AlignedLayout(Alignment align, Widget* p = nullptr) noexcept + : Layout(p), alignment(align) + {} [[nodiscard]] auto size() const noexcept -> Size override; }; diff --git a/inc/basic_widgets/w/base/layout.hpp b/inc/basic_widgets/w/base/layout.hpp index bcfc878..0066630 100644 --- a/inc/basic_widgets/w/base/layout.hpp +++ b/inc/basic_widgets/w/base/layout.hpp @@ -36,8 +36,7 @@ namespace bwidgets [[nodiscard]] auto size() const noexcept -> Size override = 0; virtual auto add_widget(const std::shared_ptr&) -> Layout*; - virtual void - for_widgets(const std::function&); + virtual void for_widgets(const std::function&); }; } diff --git a/inc/basic_widgets/w/base/widget.hpp b/inc/basic_widgets/w/base/widget.hpp index 024a1df..0384bf5 100644 --- a/inc/basic_widgets/w/base/widget.hpp +++ b/inc/basic_widgets/w/base/widget.hpp @@ -30,10 +30,10 @@ namespace bwidgets virtual ~Widget() = default; - virtual auto handle_event(const SDL_Event&) -> Widget*; + virtual auto handle_event(const SDL_Event&) -> Widget*; [[nodiscard]] virtual auto size() const noexcept -> Size = 0; - virtual auto render() -> Widget* final; + virtual auto render() -> Widget* final; virtual auto renderer(const std::shared_ptr&) -> Widget* final; virtual auto viewport(const SDL_Rect&) -> Widget* final; [[nodiscard]] virtual auto viewport() const -> const SDL_Rect& final; diff --git a/inc/basic_widgets/w/feat/focus_handler.hpp b/inc/basic_widgets/w/feat/focus_handler.hpp index ee84ce4..d3dae60 100644 --- a/inc/basic_widgets/w/feat/focus_handler.hpp +++ b/inc/basic_widgets/w/feat/focus_handler.hpp @@ -14,10 +14,10 @@ namespace bwidgets virtual void _handle_focus_change(bool) = 0; public: - FocusHandler() = default; + FocusHandler() = default; FocusHandler(const FocusHandler&) = delete; - FocusHandler(FocusHandler&&) = delete; - virtual ~FocusHandler() = default; + FocusHandler(FocusHandler&&) = delete; + virtual ~FocusHandler() = default; auto operator=(FocusHandler&&) -> FocusHandler& = delete; auto operator=(const FocusHandler&) -> FocusHandler& = delete; diff --git a/src/core/font.cpp b/src/core/font.cpp index ce71272..83c3222 100644 --- a/src/core/font.cpp +++ b/src/core/font.cpp @@ -12,147 +12,145 @@ using namespace bwidgets; const Color Font::default_color_bg {255, 255, 255, SDL_ALPHA_OPAQUE}; const Color Font::default_color_fg {0, 0, 0, SDL_ALPHA_OPAQUE}; - Font::Font(TTF_Font* f) - : Wrapper(ptr_or_throw(f, __FILE__, __FUNCTION__, __LINE__), - [f](TTF_Font*) noexcept { TTF_CloseFont(f); }), - ascent(TTF_FontAscent(f)), - descent(TTF_FontDescent(f)), - faces(TTF_FontFaces(f)), - family_name(TTF_FontFaceFamilyName(f)), - fixed_width(TTF_FontFaceIsFixedWidth(f) != 0), - height(TTF_FontHeight(f)), - line_skip(TTF_FontLineSkip(f)), - style_name(TTF_FontFaceStyleName(f)) - {} +Font::Font(TTF_Font* f) + : Wrapper(ptr_or_throw(f, __FILE__, __FUNCTION__, __LINE__), + [f](TTF_Font*) noexcept { TTF_CloseFont(f); }), + ascent(TTF_FontAscent(f)), + descent(TTF_FontDescent(f)), + faces(TTF_FontFaces(f)), + family_name(TTF_FontFaceFamilyName(f)), + fixed_width(TTF_FontFaceIsFixedWidth(f) != 0), + height(TTF_FontHeight(f)), + line_skip(TTF_FontLineSkip(f)), + style_name(TTF_FontFaceStyleName(f)) +{} - Font::Font(const std::string& file, int size) - : Font(TTF_OpenFont(file.c_str(), size)) - {} +Font::Font(const std::string& file, int size) : Font(TTF_OpenFont(file.c_str(), size)) {} - auto Font::hinting() const noexcept -> Font::Hinting - { - return (Hinting)TTF_GetFontHinting(_data()); +auto Font::hinting() const noexcept -> Font::Hinting +{ + return (Hinting)TTF_GetFontHinting(_data()); +} + +auto Font::hinting(const Font::Hinting h) noexcept -> Font* +{ + TTF_SetFontHinting(_data(), (int)h); + return this; +} + +auto Font::kerning() const noexcept -> bool +{ + return TTF_GetFontKerning(_data()) != 0; +} + +auto Font::kerning(const bool allowed) noexcept -> Font* +{ + TTF_SetFontKerning(_data(), static_cast(allowed)); + return this; +} + +auto Font::outline() const noexcept -> int +{ + return TTF_GetFontOutline(_data()); +} + +auto Font::outline(const int size) noexcept -> Font* +{ + TTF_SetFontOutline(_data(), size); + return this; +} + +auto Font::render(const RenderMode m, const std::string& str, const Color& fg, + const Color& bg) -> SDL_Surface* +{ + std::function renderer; + const char* c_str = str.empty() ? " " : str.c_str(); + + switch (m) { + case RenderMode::BLENDED: + renderer = [&fg, c_str, this]() { + return TTF_RenderUTF8_Blended(_data(), c_str, fg()); + }; + break; + case RenderMode::SHADED: + renderer = [&bg, &fg, c_str, this]() { + return TTF_RenderUTF8_Shaded(_data(), c_str, fg(), bg()); + }; + break; + case RenderMode::SOLID: + renderer = [&fg, c_str, this]() { + return TTF_RenderUTF8_Solid(_data(), c_str, fg()); + }; + break; } - auto Font::hinting(const Font::Hinting h) noexcept -> Font* - { - TTF_SetFontHinting(_data(), (int)h); - return this; - } + return ptr_or_throw(renderer(), __FILE__, __FUNCTION__, __LINE__); +} - auto Font::kerning() const noexcept -> bool - { - return TTF_GetFontKerning(_data()) != 0; - } +auto Font::style() const noexcept -> uint8_t +{ + return TTF_GetFontStyle(_data()); +} - auto Font::kerning(const bool allowed) noexcept -> Font* - { - TTF_SetFontKerning(_data(), static_cast(allowed)); - return this; - } +auto Font::style(const uint8_t s) noexcept -> Font* +{ + TTF_SetFontStyle(_data(), s); + return this; +} - auto Font::outline() const noexcept -> int - { - return TTF_GetFontOutline(_data()); - } +auto Font::text_size(const std::string& str) const noexcept -> Size +{ + Size s {}; + TTF_SizeUTF8(_data(), str.c_str(), &s.w, &s.h); + return s; +} - auto Font::outline(const int size) noexcept -> Font* - { - TTF_SetFontOutline(_data(), size); - return this; - } +auto Font::find(const std::string& pat) -> std::string +{ + std::stack> cleaners; - auto Font::render(const RenderMode m, const std::string& str, const Color& fg, - const Color& bg) -> SDL_Surface* - { - std::function renderer; - const char* c_str = str.empty() ? " " : str.c_str(); + FcConfig* conf = ptr_or_throw(FcInitLoadConfigAndFonts(), __FILE__, + __FUNCTION__, __LINE__, "init failed"); + cleaners.emplace([conf]() { FcConfigDestroy(conf); }); - switch (m) { - case RenderMode::BLENDED: - renderer = [&fg, c_str, this]() { - return TTF_RenderUTF8_Blended(_data(), c_str, fg()); - }; - break; - case RenderMode::SHADED: - renderer = [&bg, &fg, c_str, this]() { - return TTF_RenderUTF8_Shaded(_data(), c_str, fg(), bg()); - }; - break; - case RenderMode::SOLID: - renderer = [&fg, c_str, this]() { - return TTF_RenderUTF8_Solid(_data(), c_str, fg()); - }; - break; - } - - return ptr_or_throw(renderer(), __FILE__, __FUNCTION__, __LINE__); - } - - auto Font::style() const noexcept -> uint8_t - { - return TTF_GetFontStyle(_data()); - } - - auto Font::style(const uint8_t s) noexcept -> Font* - { - TTF_SetFontStyle(_data(), s); - return this; - } - - auto Font::text_size(const std::string& str) const noexcept -> Size - { - Size s {}; - TTF_SizeUTF8(_data(), str.c_str(), &s.w, &s.h); - return s; - } - - auto Font::find(const std::string& pat) -> std::string - { - std::stack> cleaners; - - FcConfig* conf = ptr_or_throw(FcInitLoadConfigAndFonts(), __FILE__, - __FUNCTION__, __LINE__, "init failed"); - cleaners.emplace([conf]() { FcConfigDestroy(conf); }); - - FcPattern* pattern = nullptr; - try { - pattern = - ptr_or_throw(FcNameParse((FcChar8*)pat.c_str()), __FILE__, - __FUNCTION__, __LINE__, "pattern parsing failed"); - cleaners.emplace([pattern]() { FcPatternDestroy(pattern); }); - - if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse) - throw FCError {__FILE__, __FUNCTION__, __LINE__, - "FcConfigSubstitute failed"}; - } catch (const std::exception& e) { - while (!cleaners.empty()) { - cleaners.top()(); - cleaners.pop(); - } - throw e; - } - - FcDefaultSubstitute(pattern); - - std::string file_path; - FcResult res {}; - FcPattern* font = FcFontMatch(conf, pattern, &res); - if (font != nullptr) { - FcChar8* file = nullptr; - if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { - file_path = (char*)file; - } - FcPatternDestroy(font); - } + FcPattern* pattern = nullptr; + try { + pattern = + ptr_or_throw(FcNameParse((FcChar8*)pat.c_str()), __FILE__, + __FUNCTION__, __LINE__, "pattern parsing failed"); + cleaners.emplace([pattern]() { FcPatternDestroy(pattern); }); + if (FcConfigSubstitute(conf, pattern, FcMatchPattern) == FcFalse) + throw FCError {__FILE__, __FUNCTION__, __LINE__, + "FcConfigSubstitute failed"}; + } catch (const std::exception& e) { while (!cleaners.empty()) { cleaners.top()(); cleaners.pop(); } - - if (file_path.empty()) - throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"}; - - return file_path; + throw e; } + + FcDefaultSubstitute(pattern); + + std::string file_path; + FcResult res {}; + FcPattern* font = FcFontMatch(conf, pattern, &res); + if (font != nullptr) { + FcChar8* file = nullptr; + if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) { + file_path = (char*)file; + } + FcPatternDestroy(font); + } + + while (!cleaners.empty()) { + cleaners.top()(); + cleaners.pop(); + } + + if (file_path.empty()) + throw FCError {__FILE__, __FUNCTION__, __LINE__, "no font found"}; + + return file_path; +} diff --git a/src/core/renderer.cpp b/src/core/renderer.cpp index dc40cdd..69d09eb 100644 --- a/src/core/renderer.cpp +++ b/src/core/renderer.cpp @@ -39,8 +39,8 @@ auto Renderer::clear() -> Renderer* return this; } -auto Renderer::copy(const Texture* t, const SDL_Rect* src, - const SDL_Rect* dst) -> Renderer* +auto Renderer::copy(const Texture* t, const SDL_Rect* src, const SDL_Rect* dst) + -> Renderer* { SDLError::success_or_throw(SDL_RenderCopy(_data(), t->_data(), src, dst), __FILE__, __FUNCTION__, __LINE__); diff --git a/src/core/texture.cpp b/src/core/texture.cpp index 6498000..1a1637a 100644 --- a/src/core/texture.cpp +++ b/src/core/texture.cpp @@ -10,8 +10,8 @@ Texture::Texture(SDL_Texture* t) _attributes = attributes(t); } -Texture::Texture(Renderer* r, const SDL_PixelFormatEnum f, - const SDL_TextureAccess a, int w, int h) +Texture::Texture(Renderer* r, const SDL_PixelFormatEnum f, const SDL_TextureAccess a, + int w, int h) : Texture(SDL_CreateTexture(r->_data(), f, a, w, h)) {}