From ab18cd986c8e501e0178a689fff424e2c985f9da Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 7 Mar 2024 19:23:38 -0300 Subject: [PATCH] Update to new laf draw_text() API As the new text::draw_text() can draw text with "native" (Skia) fonts, we have to pass the font manager (text::FontMgr) to it, which is in SkiaTheme. So we've added a new abstract fontMgr() member function to ui::Theme to get the active font manager. --- laf | 2 +- src/app/script/graphics_context.cpp | 13 +++++++++---- src/app/ui/skin/skin_theme.cpp | 2 +- src/app/ui/skin/skin_theme.h | 6 +++--- src/ui/entry.cpp | 6 ++++-- src/ui/graphics.cpp | 18 ++++++++++-------- src/ui/theme.h | 4 +++- 7 files changed, 31 insertions(+), 20 deletions(-) diff --git a/laf b/laf index 3bea53125..1b5834cd5 160000 --- a/laf +++ b/laf @@ -1 +1 @@ -Subproject commit 3bea53125ccafce878d17fcf8d9d192ad69fafb4 +Subproject commit 1b5834cd52340f14408112b746c8c46a581c3488 diff --git a/src/app/script/graphics_context.cpp b/src/app/script/graphics_context.cpp index 0b4d734dc..b5bedf165 100644 --- a/src/app/script/graphics_context.cpp +++ b/src/app/script/graphics_context.cpp @@ -33,14 +33,19 @@ namespace script { void GraphicsContext::fillText(const std::string& text, int x, int y) { - text::draw_text(m_surface.get(), m_font, - text, m_paint.color(), 0, x, y, nullptr); + if (auto theme = skin::SkinTheme::instance()) { + text::draw_text(m_surface.get(), theme->fontMgr(), m_font, + text, m_paint.color(), 0, x, y, nullptr); + } } gfx::Size GraphicsContext::measureText(const std::string& text) const { - return text::draw_text(nullptr, m_font, text, - 0, 0, 0, 0, nullptr).size(); + if (auto theme = skin::SkinTheme::instance()) { + return text::draw_text(nullptr, theme->fontMgr(), m_font, text, + 0, 0, 0, 0, nullptr).size(); + } + return gfx::Size(); } void GraphicsContext::drawImage(const doc::Image* img, int x, int y) diff --git a/src/app/ui/skin/skin_theme.cpp b/src/app/ui/skin/skin_theme.cpp index 2ce86a204..1475688a3 100644 --- a/src/app/ui/skin/skin_theme.cpp +++ b/src/app/ui/skin/skin_theme.cpp @@ -1172,7 +1172,7 @@ public: const gfx::Rect& textBounds() const { return m_textBounds; } void preProcessChar(const int index, - const int codepoint, + const base::codepoint_t codepoint, gfx::Color& fg, gfx::Color& bg, const gfx::Rect& charBounds) override { diff --git a/src/app/ui/skin/skin_theme.h b/src/app/ui/skin/skin_theme.h index 8472a88d4..a673fb526 100644 --- a/src/app/ui/skin/skin_theme.h +++ b/src/app/ui/skin/skin_theme.h @@ -48,8 +48,8 @@ namespace app { // This is the GUI theme used by Aseprite (which use images from // data/skins directory). - class SkinTheme : public ui::Theme - , public app::gen::ThemeFile { + class SkinTheme final : public ui::Theme + , public app::gen::ThemeFile { public: static const char* kThemesFolderName; @@ -63,7 +63,7 @@ namespace app { int preferredScreenScaling() { return m_preferredScreenScaling; } int preferredUIScaling() { return m_preferredUIScaling; } - text::FontMgrRef fontMgr() const { return m_fontMgr; } + text::FontMgrRef fontMgr() const override { return m_fontMgr; } text::Font* getDefaultFont() const override { return m_defaultFont.get(); } text::Font* getWidgetFont(const ui::Widget* widget) const override; text::Font* getMiniFont() const { return m_miniFont.get(); } diff --git a/src/ui/entry.cpp b/src/ui/entry.cpp index 8b1ebd9ac..927368b20 100644 --- a/src/ui/entry.cpp +++ b/src/ui/entry.cpp @@ -894,7 +894,7 @@ public: const Entry::CharBoxes& boxes() const { return m_boxes; } void preProcessChar(const int index, - const int codepoint, + const base::codepoint_t codepoint, gfx::Color& fg, gfx::Color& bg, const gfx::Rect& charBounds) override { @@ -926,7 +926,9 @@ void Entry::recalcCharBoxes(const std::string& text) { int lastTextIndex = int(text.size()); CalcBoxesTextDelegate delegate(lastTextIndex); - text::draw_text(nullptr, base::AddRef(font()), text, + text::draw_text(nullptr, + theme()->fontMgr(), + base::AddRef(font()), text, gfx::ColorNone, gfx::ColorNone, 0, 0, &delegate); m_boxes = delegate.boxes(); diff --git a/src/ui/graphics.cpp b/src/ui/graphics.cpp index 0c1784118..e69959322 100644 --- a/src/ui/graphics.cpp +++ b/src/ui/graphics.cpp @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2019-2022 Igara Studio S.A. +// Copyright (C) 2019-2024 Igara Studio S.A. // Copyright (C) 2001-2018 David Capello // // This file is released under the terms of the MIT license. @@ -352,7 +352,8 @@ void Graphics::drawText(const std::string& str, os::SurfaceLock lock(m_surface.get()); gfx::Rect textBounds = - text::draw_text(m_surface.get(), m_font, str, fg, bg, pt.x, pt.y, delegate); + text::draw_text(m_surface.get(), get_theme()->fontMgr(), + m_font, str, fg, bg, pt.x, pt.y, delegate); dirty(gfx::Rect(pt.x, pt.y, textBounds.w, textBounds.h)); } @@ -373,7 +374,7 @@ public: gfx::Rect bounds() const { return m_bounds; } void preProcessChar(const int index, - const int codepoint, + const base::codepoint_t codepoint, gfx::Color& fg, gfx::Color& bg, const gfx::Rect& charBounds) override { @@ -431,8 +432,8 @@ void Graphics::drawUIText(const std::string& str, gfx::Color fg, gfx::Color bg, int y = m_dy+pt.y; DrawUITextDelegate delegate(m_surface.get(), m_font.get(), mnemonic); - text::draw_text(m_surface.get(), m_font, str, - fg, bg, x, y, &delegate); + text::draw_text(m_surface.get(), get_theme()->fontMgr(), + m_font, str, fg, bg, x, y, &delegate); dirty(delegate.bounds()); } @@ -455,9 +456,10 @@ int Graphics::measureUITextLength(const std::string& str, text::Font* font) { DrawUITextDelegate delegate(nullptr, font, 0); - text::draw_text(nullptr, base::AddRef(font), str, - gfx::ColorNone, gfx::ColorNone, 0, 0, - &delegate); + text::draw_text(nullptr, get_theme()->fontMgr(), + base::AddRef(font), str, + gfx::ColorNone, gfx::ColorNone, + 0, 0, &delegate); return delegate.bounds().w; } diff --git a/src/ui/theme.h b/src/ui/theme.h index d5d0045e7..68c2237bd 100644 --- a/src/ui/theme.h +++ b/src/ui/theme.h @@ -13,10 +13,11 @@ #include "gfx/color.h" #include "gfx/rect.h" #include "gfx/size.h" +#include "text/fwd.h" #include "ui/base.h" #include "ui/cursor_type.h" -#include "ui/style.h" #include "ui/scale.h" +#include "ui/style.h" namespace gfx { class Region; @@ -62,6 +63,7 @@ namespace ui { Theme(); virtual ~Theme(); + virtual text::FontMgrRef fontMgr() const = 0; virtual text::Font* getDefaultFont() const = 0; virtual text::Font* getWidgetFont(const Widget* widget) const = 0;