Skip to content

Commit 08332c8

Browse files
committed
Allow drawing the selected text bg earlier
Add new methods to DrawTextDelegate to allow drawing the background of the selected text before drawing each character. This fixes an issue when the glyphs used by a TTF were overlapped.
1 parent a8246e0 commit 08332c8

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

text/draw_text.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Text Library
2-
// Copyright (c) 2022-2024 Igara Studio S.A.
2+
// Copyright (c) 2022-2025 Igara Studio S.A.
33
// Copyright (C) 2017 David Capello
44
//
55
// This file is released under the terms of the MIT license.
@@ -32,6 +32,20 @@ class DrawTextDelegate {
3232
public:
3333
virtual ~DrawTextDelegate() {}
3434

35+
// This is called before processing and drawing character by character.
36+
// Returns true if the character index is in the range of selected characters.
37+
virtual bool isSelectedChar(const int index)
38+
{
39+
// Do nothing
40+
return false;
41+
}
42+
43+
// This is called before processing and drawing character by character.
44+
virtual void drawSelectionBg(const gfx::RectF& bounds)
45+
{
46+
// Do nothing
47+
}
48+
3549
// This is called before drawing the character.
3650
virtual void preProcessChar(const int index,
3751
const codepoint_t codepoint,

text/draw_text_shaper.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// LAF Text Library
2-
// Copyright (c) 2024 Igara Studio S.A.
2+
// Copyright (c) 2024-2025 Igara Studio S.A.
33
//
44
// This file is released under the terms of the MIT license.
55
// Read LICENSE.txt for more information.
@@ -49,11 +49,31 @@ class AdapterBuilder : public TextBlob::RunHandler {
4949
void commitRunBuffer(TextBlob::RunInfo& info) override
5050
{
5151
if (info.clusters && info.glyphCount > 0) {
52-
float advanceX = 0.0f;
52+
gfx::RectF selectionBounds;
53+
gfx::RectF textBounds;
54+
std::vector<gfx::RectF> glyphsBounds;
55+
for (int i = 0; i < info.glyphCount; ++i) {
56+
auto bounds = info.getGlyphBounds(i);
57+
glyphsBounds.push_back(bounds);
58+
textBounds |= bounds;
59+
60+
if (m_delegate && m_delegate->isSelectedChar(i))
61+
selectionBounds |= bounds;
62+
}
5363

5464
os::Paint paint;
5565
paint.style(os::Paint::Fill);
5666

67+
if (m_surface && m_bg != gfx::ColorNone) {
68+
paint.color(m_bg);
69+
m_surface->drawRect(textBounds.offset(m_origin), paint);
70+
}
71+
72+
if (m_delegate && !selectionBounds.isEmpty())
73+
m_delegate->drawSelectionBg(selectionBounds.offset(m_origin));
74+
75+
float advanceX = 0.0f;
76+
5777
for (int i = 0; i < info.glyphCount; ++i) {
5878
int utf8Begin, utf8End;
5979

@@ -71,7 +91,7 @@ class AdapterBuilder : public TextBlob::RunHandler {
7191

7292
const std::string utf8text = m_text.substr(utf8Begin, utf8End - utf8Begin);
7393

74-
gfx::RectF bounds = info.getGlyphBounds(i);
94+
gfx::RectF bounds = glyphsBounds[i];
7595
bounds.offset(m_origin);
7696

7797
advanceX += bounds.w;
@@ -100,11 +120,6 @@ class AdapterBuilder : public TextBlob::RunHandler {
100120
if (m_delegate)
101121
m_delegate->preDrawChar(bounds);
102122

103-
if (m_bg != gfx::ColorNone) {
104-
paint.color(m_bg);
105-
m_surface->drawRect(bounds, paint);
106-
}
107-
108123
if (m_surface && info.font) {
109124
if (info.font->type() == FontType::SpriteSheet) {
110125
const auto* spriteFont = static_cast<const SpriteSheetFont*>(info.font.get());

0 commit comments

Comments
 (0)