Skip to content

Commit

Permalink
gui: Avoid that part of the cursor doesn't flash with fractional scal…
Browse files Browse the repository at this point in the history
…ing.

* libgui/qterminal/libqterminal/win32/QWinTerminalImpl.cpp
(QConsolePrivate::drawTextBackground, QConsolePrivate::drawSelection,
QConsolePrivate::drawText, QConsolePrivate::cursorRect,
QConsolePrivate::boundingRect): "horizontalAdvance" returns an integer. Don't
use floor or ceil on it. Instead extend ranges by one pixel where it makes
sense.
  • Loading branch information
mmuetzel committed Sep 26, 2023
1 parent bbc7016 commit 3ef137f
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions libgui/qterminal/libqterminal/win32/QWinTerminalImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ see <https://www.gnu.org/licenses/>.
*/

#include <algorithm>
#include <cmath>
#include <csignal>
#include <cstdio>
#include <cstdarg>
Expand Down Expand Up @@ -653,7 +652,7 @@ void QConsolePrivate::drawTextBackground (QPainter& p, int cx1, int cy1,
{
int len = 0;
bool hasChar = false;
double x = fm.horizontalAdvance (sample, cx1);
int x = fm.horizontalAdvance (sample, cx1);
WORD attr = 0;
int curr_cx1 = cx1;

Expand All @@ -672,7 +671,7 @@ void QConsolePrivate::drawTextBackground (QPainter& p, int cx1, int cy1,
if (attr & 0x00f0)
// Try to not paint over parts of the preceeding
// character.
p.fillRect (std::ceil (x), y-ascent,
p.fillRect (x, y-ascent,
fm.horizontalAdvance (sample, len), ch,
p.brush ());
}
Expand Down Expand Up @@ -701,7 +700,7 @@ void QConsolePrivate::drawTextBackground (QPainter& p, int cx1, int cy1,

if (attr & 0x00f0)
// Try to not paint over parts of the preceeding character.
p.fillRect (std::ceil (x), y-ascent,
p.fillRect (x, y-ascent,
fm.horizontalAdvance (sample, len), ch, p.brush ());
}
}
Expand Down Expand Up @@ -857,8 +856,8 @@ void QConsolePrivate::drawSelection (QPainter& p, int cx1, int cy1,
? end.x () - selectionBegin + 1
: stride - selectionBegin);

p.fillRect (std::floor (fm.horizontalAdvance (sample, selectionBegin)),
y-ascent, std::ceil (fm.horizontalAdvance (sample, len)),
p.fillRect (fm.horizontalAdvance (sample, selectionBegin),
y-ascent, fm.horizontalAdvance (sample, len),
ch, selectionColor ());
}
}
Expand Down Expand Up @@ -932,7 +931,7 @@ void QConsolePrivate::drawText (QPainter& p, int cx1, int cy1,
// Reset string buffer and starting X coordinate
s.clear ();
bool hasChar = false;
double x = fm.horizontalAdvance (sample, cx1);
int x = fm.horizontalAdvance (sample, cx1);
WORD attr = 0;
int curr_cx1 = cx1;

Expand Down Expand Up @@ -971,7 +970,7 @@ void QConsolePrivate::drawText (QPainter& p, int cx1, int cy1,
// No need to update s or x, they will be reset on the next
// for-loop iteration

p.drawText (std::ceil (x), y, s);
p.drawText (x, y, s);
}
}

Expand Down Expand Up @@ -1472,16 +1471,16 @@ QConsolePrivate::cursorRect (void)
int ch = m_charSize.height ();

// Make sure the cursor starts *right* of the previous character.
return QRect (std::ceil (fm.horizontalAdvance (sample)),
return QRect (fm.horizontalAdvance (sample),
(m_cursorPos.y () - m_consoleRect.y ()) * ch,
cw, ch);
}

QRect
QConsolePrivate::boundingRect (void)
{
// This might be slightly larger than cursorRect to make sure the entirety
// of a character is redrawn.
// This is slightly larger than cursorRect to make sure the entirety of a
// character is redrawn.

QFontMetrics fm = m_consoleView->fontMetrics ();
QString sample ('m');
Expand All @@ -1492,9 +1491,9 @@ QConsolePrivate::boundingRect (void)
int cw = m_charSize.width ();
int ch = m_charSize.height ();

return QRect (std::floor (fm.horizontalAdvance (sample)),
return QRect (fm.horizontalAdvance (sample)-1,
(m_cursorPos.y () - m_consoleRect.y ()) * ch,
cw+1, ch);
cw+2, ch);
}

//////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 3ef137f

Please sign in to comment.