Skip to content

Commit 707828a

Browse files
committed
Updated multiline font drawing algorithm
1 parent 877ac83 commit 707828a

2 files changed

Lines changed: 12 additions & 36 deletions

File tree

src/main/helpers/draw.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2026 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2026 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-tk-lib
66
* Created on: 22 авг. 2020 г.
@@ -299,7 +299,7 @@ namespace lsp
299299
// Calculate text location
300300
font->get_text_parameters(s, &xtp, fscaling, text, last, tail);
301301
float dx = (r->nWidth - xtp.Width) * 0.5f;
302-
ssize_t x = r->nLeft + dx * halign - xtp.XBearing;
302+
float x = roundf(r->nLeft + dx * halign - xtp.XBearing);
303303
y += fp->Height;
304304

305305
font->draw(s, color, x, y, fscaling, text, last, tail);

src/main/widgets/simple/Button.cpp

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
*/
2121

2222
#include <lsp-plug.in/tk/tk.h>
23+
#include <lsp-plug.in/common/debug.h>
2324
#include <lsp-plug.in/stdlib/math.h>
25+
#include <lsp-plug.in/tk/helpers/draw.h>
2426
#include <private/tk/style/BuiltinStyle.h>
2527

2628
namespace lsp
@@ -667,40 +669,14 @@ namespace lsp
667669
sFont.get_parameters(s, fscaling, &fp);
668670
sFont.get_multitext_parameters(s, &tp, fscaling, &text);
669671

670-
// Prepare to draw
671-
float halign = lsp_limit(sTextLayout.halign() + 1.0f, 0.0f, 2.0f);
672-
float valign = lsp_limit(sTextLayout.valign() + 1.0f, 0.0f, 2.0f);
673-
float dy = (r.nHeight - tp.Height) * 0.5f;
674-
ssize_t y = r.nTop + dy * valign - fp.Descent;
672+
// Draw multi-line text
673+
const float halign = lsp_limit(sTextLayout.halign(), -1.0f, 1.0f);
674+
const float valign = lsp_limit(sTextLayout.valign(), -1.0f, 1.0f);
675675

676-
// Estimate text size
677-
ssize_t last = 0, curr = 0, tail = 0, len = text.length();
678-
679-
while (curr < len)
680-
{
681-
// Get next line indexes
682-
curr = text.index_of(last, '\n');
683-
if (curr < 0)
684-
{
685-
curr = len;
686-
tail = len;
687-
}
688-
else
689-
{
690-
tail = curr;
691-
if ((tail > last) && (text.at(tail-1) == '\r'))
692-
--tail;
693-
}
694-
695-
// Calculate text location
696-
sFont.get_text_parameters(s, &tp, fscaling, &text, last, tail);
697-
float dx = (r.nWidth - tp.Width) * 0.5f;
698-
ssize_t x = r.nLeft + dx * halign - tp.XBearing;
699-
y += fp.Height;
700-
701-
sFont.draw(s, tcolor, x, y, fscaling, &text, last, tail);
702-
last = curr + 1;
703-
}
676+
draw_multiline_text(
677+
s, &sFont, &r, tcolor,
678+
&fp, &tp, halign, valign, fscaling,
679+
&text);
704680

705681
s->clip_end();
706682
}

0 commit comments

Comments
 (0)