Simplify Font ptrs management in ui-lib returning FontRefs

We can return "const text::FontRef&" to avoid adding a new reference
to an existing FontRef just to access a Style/Widget font.
This commit is contained in:
David Capello 2024-11-29 17:57:41 -03:00
parent 7e780bf3bf
commit ab599c5863
16 changed files with 50 additions and 44 deletions

View File

@ -53,10 +53,10 @@ void Canvas::callPaint()
GraphicsContext gc(m_surface, m_autoScaling ? ui::guiscale() : 1);
if (m_autoScaling) {
auto theme = skin::SkinTheme::get(this);
gc.font(AddRef(theme->getUnscaledFont(font())));
gc.font(theme->getUnscaledFont(font()));
}
else
gc.font(AddRef(font()));
gc.font(font());
Paint(gc);
}

View File

@ -156,7 +156,7 @@ private:
ui::Display* display = this->Base::display();
{
ui::Graphics g(display, surface, 0, 0);
g.setFont(AddRef(this->font()));
g.setFont(this->font());
drawFloatingOverlay(g);
}

View File

@ -1267,13 +1267,14 @@ void Editor::drawTileNumbers(ui::Graphics* g, const Cel* cel)
gfx::Color color = color_utils::color_for_ui(Preferences::instance().guides.autoGuidesColor());
gfx::Color fgColor = color_utils::blackandwhite_neg(color);
const text::FontRef& font = g->font();
const doc::Grid grid = getSite().grid();
const gfx::Size tileSize = editorToScreen(grid.tileToCanvas(gfx::Rect(0, 0, 1, 1))).size();
const int th = g->font()->height();
const int th = font->height();
if (tileSize.h > th) {
const gfx::Point offset =
gfx::Point(tileSize.w/2,
tileSize.h/2 - g->font()->height()/2)
tileSize.h/2 - font->height()/2)
+ mainTilePosition();
int ti_offset =
@ -1295,7 +1296,7 @@ void Editor::drawTileNumbers(ui::Graphics* g, const Cel* cel)
text = fmt::format("{}", ti + ti_offset);
gfx::Point pt2(pt);
pt2.x -= g->font()->textLength(text)/2;
pt2.x -= font->textLength(text)/2;
g->drawText(text, fgColor, color, pt2);
if (tf && tileSize.h > 2*th) {

View File

@ -940,9 +940,9 @@ void PaletteView::onPaint(ui::PaintEvent& ev)
m_adapter->drawEntry(g, theme, i, k, childSpacing(),
box2, negColor);
text::Font* minifont = theme->getMiniFont();
text::FontRef minifont = theme->getMiniFont();
const std::string text = base::convert_to<std::string>(k);
g->setFont(AddRef(minifont));
g->setFont(minifont);
g->drawText(text, negColor, gfx::ColorNone,
gfx::Point(box2.x + box2.w/2 - minifont->textLength(text)/2,
box2.y + box2.h/2 - minifont->height()/2));

View File

@ -922,7 +922,7 @@ os::SurfaceRef SkinTheme::sliceUnscaledSheet(os::SurfaceRef sur, const gfx::Rect
return app::skin::sliceSheet(m_unscaledSheet, sur, bounds);
}
text::Font* SkinTheme::getWidgetFont(const Widget* widget) const
text::FontRef SkinTheme::getWidgetFont(const Widget* widget) const
{
auto skinPropery = std::static_pointer_cast<SkinProperty>(widget->getProperty(SkinProperty::Name));
if (skinPropery && skinPropery->hasMiniFont())
@ -1610,7 +1610,7 @@ void SkinTheme::drawText(Graphics* g, const char* t,
if (t || widget->hasText()) {
Rect textrc;
g->setFont(AddRef(widget->font()));
g->setFont(widget->font());
if (!t)
t = widget->text().c_str();

View File

@ -63,13 +63,13 @@ namespace app {
int preferredScreenScaling() const { return m_preferredScreenScaling; }
int preferredUIScaling() const { return m_preferredUIScaling; }
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(); }
text::Font* getUnscaledFont(text::Font* font) const {
auto it = m_unscaledFonts.find(font);
text::FontRef getDefaultFont() const override { return m_defaultFont; }
text::FontRef getWidgetFont(const ui::Widget* widget) const override;
text::FontRef getMiniFont() const { return m_miniFont; }
text::FontRef getUnscaledFont(const text::FontRef& font) const {
auto it = m_unscaledFonts.find(font.get());
if (it != m_unscaledFonts.end())
return it->second.get();
return it->second;
else
return font;
}

View File

@ -983,7 +983,7 @@ void Tabs::createFloatingOverlay(Tab* tab)
}
{
Graphics g(display, surface, 0, 0);
g.setFont(AddRef(font()));
g.setFont(font());
drawTab(&g, g.getClipBounds(), tab, 0, true, true);
}

View File

@ -505,15 +505,17 @@ bool Entry::onProcessMessage(Message* msg)
gfx::Size Entry::sizeHintWithText(Entry* entry,
const std::string& text)
{
const auto& font = entry->font();
int w =
entry->font()->textLength(text) +
font->textLength(text) +
+ 2*entry->theme()->getEntryCaretSize(entry).w
+ entry->border().width();
w = std::min(w, entry->display()->workareaSizeUIScale().w/2);
int h =
+ entry->font()->height()
const int h =
+ font->height()
+ entry->border().height();
return gfx::Size(w, h);
@ -521,18 +523,20 @@ gfx::Size Entry::sizeHintWithText(Entry* entry,
void Entry::onSizeHint(SizeHintEvent& ev)
{
int trailing = font()->textLength(getSuffix());
const auto& font = this->font();
int trailing = font->textLength(getSuffix());
trailing = std::max(trailing, 2*theme()->getEntryCaretSize(this).w);
int w =
font()->textLength("w") * std::min(m_maxsize, 6) +
font->textLength("w") * std::min(m_maxsize, 6) +
+ trailing
+ border().width();
w = std::min(w, display()->workareaSizeUIScale().w/2);
int h =
+ font()->height()
+ font->height()
+ border().height();
ev.setSizeHint(w, h);
@ -958,7 +962,7 @@ void Entry::recalcCharBoxes(const std::string& text)
float lastX =
text::draw_text(nullptr,
theme()->fontMgr(),
base::AddRef(font()), text,
font(), text,
gfx::ColorNone, gfx::ColorNone, 0, 0,
&delegate,
onGetTextShaperFeatures()).w;

View File

@ -632,7 +632,7 @@ void Graphics::dirty(const gfx::Rect& bounds)
ScreenGraphics::ScreenGraphics(Display* display)
: Graphics(display, base::AddRef(display->surface()), 0, 0)
{
setFont(base::AddRef(get_theme()->getDefaultFont()));
setFont(get_theme()->getDefaultFont());
}
ScreenGraphics::~ScreenGraphics()

View File

@ -101,7 +101,7 @@ namespace ui {
// FONT & TEXT
// ======================================================================
text::Font* font() { return m_font.get(); }
const text::FontRef& font() { return m_font; }
void setFont(const text::FontRef& font);
[[deprecated]]

View File

@ -147,11 +147,12 @@ void IntEntry::onInitTheme(InitThemeEvent& ev)
void IntEntry::onSizeHint(SizeHintEvent& ev)
{
int trailing = font()->textLength(getSuffix());
const text::FontRef& font = this->font();
int trailing = font->textLength(getSuffix());
trailing = std::max(trailing, 2*theme()->getEntryCaretSize(this).w);
int min_w = font()->textLength(m_slider->convertValueToText(m_min));
int max_w = font()->textLength(m_slider->convertValueToText(m_max)) + trailing;
int min_w = font->textLength(m_slider->convertValueToText(m_min));
int max_w = font->textLength(m_slider->convertValueToText(m_max)) + trailing;
int w = std::max(min_w, max_w);
int h = textHeight();

View File

@ -115,7 +115,7 @@ namespace ui {
const gfx::Size& minSize() const { return m_minSize; }
const gfx::Size& maxSize() const { return m_maxSize; }
const gfx::Size& gap() const { return m_gap; }
text::Font* font() const { return m_font.get(); }
const text::FontRef& font() const { return m_font; }
const bool mnemonics() const { return m_mnemonics; }
const Layers& layers() const { return m_layers; }
Layers& layers() { return m_layers; }

View File

@ -445,9 +445,9 @@ void Theme::paintLayer(Graphics* g,
break;
if (layer.color() != gfx::ColorNone) {
text::FontRef oldFont = base::AddRef(g->font());
text::FontRef oldFont = g->font();
if (style->font())
g->setFont(AddRef(style->font()));
g->setFont(style->font());
if (layer.align() & WORDWRAP) {
gfx::Rect textBounds = rc;
@ -614,7 +614,7 @@ void Theme::measureLayer(const Widget* widget,
case Style::Layer::Type::kText:
if (layer.color() != gfx::ColorNone) {
text::Font* styleFont = style->font();
const text::FontRef& styleFont = style->font();
gfx::Size textSize;
if (styleFont &&
styleFont != widget->font()) {
@ -868,7 +868,7 @@ void Theme::drawTextBox(Graphics* g, const Widget* widget,
int x, y, chr, len;
gfx::Point scroll;
int textheight = widget->textHeight();
text::Font* font = widget->font();
const text::FontRef& font = widget->font();
char *beg_end, *old_end;
int width;
gfx::Rect vp;

View File

@ -65,9 +65,9 @@ namespace ui {
Theme();
virtual ~Theme();
virtual text::Font* getDefaultFont() const = 0;
virtual text::Font* getWidgetFont(const Widget* widget) const = 0;
virtual text::FontMgrRef fontMgr() const { return m_fontMgr; }
virtual text::FontRef getDefaultFont() const = 0;
virtual text::FontRef getWidgetFont(const Widget* widget) const = 0;
virtual ui::Cursor* getStandardCursor(CursorType type) = 0;
virtual void initWidget(Widget* widget) = 0;

View File

@ -169,11 +169,11 @@ void Widget::setTextQuiet(const std::string& text)
enableFlags(HAS_TEXT);
}
text::Font* Widget::font() const
const text::FontRef& Widget::font() const
{
if (!m_font && m_theme)
m_font = AddRef(m_theme->getWidgetFont(this));
return m_font.get();
m_font = m_theme->getWidgetFont(this);
return m_font;
}
void Widget::setFont(const text::FontRef& font)
@ -223,7 +223,7 @@ void Widget::setStyle(Style* style)
m_minSize = m_theme->calcMinSize(this, style);
m_maxSize = m_theme->calcMaxSize(this, style);
if (style->font())
m_font = AddRef(style->font());
m_font = style->font();
}
// ===============================================================
@ -1192,7 +1192,7 @@ void Widget::paint(Graphics* graphics,
base::AddRef(graphics->getInternalSurface()),
widget->bounds().x,
widget->bounds().y);
graphics2.setFont(AddRef(widget->font()));
graphics2.setFont(widget->font());
for (const gfx::Rect& rc : region) {
IntersectClip clip(&graphics2,
@ -1357,7 +1357,7 @@ GraphicsPtr Widget::getGraphics(const gfx::Rect& clip)
graphics.reset(new Graphics(display, dstSurface, bounds().x, bounds().y));
}
graphics->setFont(AddRef(font()));
graphics->setFont(font());
return graphics;
}
@ -1809,7 +1809,7 @@ text::TextBlobRef Widget::onMakeTextBlob() const
{
return text::TextBlob::MakeWithShaper(
theme()->fontMgr(),
AddRef(font()),
font(),
text(),
nullptr,
onGetTextShaperFeatures());

View File

@ -139,7 +139,7 @@ namespace ui {
// LOOK & FEEL
// ===============================================================
text::Font* font() const;
const text::FontRef& font() const;
void setFont(const text::FontRef& font);
// Gets the background color of the widget.