[education/rkward] /: Fix horizontal text offset in RK() device
Thomas Friedrichsmeier
null at kde.org
Sun Jan 18 10:20:43 GMT 2026
Git commit a43bb289ee58d9560581bcbaffd43a45ec0f128a by Thomas Friedrichsmeier.
Committed on 18/01/2026 at 10:20.
Pushed by tfry into branch 'master'.
Fix horizontal text offset in RK() device
BUG: 514784
M +1 -0 ChangeLog
M +7 -8 rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
M +1 -1 rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
M +1 -1 rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
https://invent.kde.org/education/rkward/-/commit/a43bb289ee58d9560581bcbaffd43a45ec0f128a
diff --git a/ChangeLog b/ChangeLog
index 4e20eedbb..94d189258 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,5 @@
--- Version 0.8.3 - UNRELEASED
+- Fixed: Horizontal text alignment would be inaccurate for some symbols in RK() device
- Fixed: Parts of a command entered interactively could run twice under specific circumstances
- Fixed: Output window would fail to open, automatically, for certain plugins
- Fixed: Width of "progress" dialogs (showing R output) would exceed screen size depending on configuration
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
index c4ba593ad..8af4fc244 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
@@ -473,12 +473,11 @@ void RKGraphicsDevice::rect(const QRectF &rec, const QPen &pen, const QBrush &br
triggerUpdate();
}
-QSizeF RKGraphicsDevice::strSize(const QString &text, const QFont &font) {
+double RKGraphicsDevice::strWidth(const QString &text, const QFont &font) {
RK_TRACE(GRAPHICS_DEVICE);
painter.setFont(font);
- QSizeF size = painter.fontMetrics().boundingRect(text).size();
- return size;
+ return painter.fontMetrics().horizontalAdvance(text);
}
void RKGraphicsDevice::text(double x, double y, const QString &text, double rot, double hadj, const QColor &col, const QFont &font) {
@@ -486,8 +485,8 @@ void RKGraphicsDevice::text(double x, double y, const QString &text, double rot,
if (recording_path) {
QPainterPath sub;
- QSizeF size = strSize(text, font);
- sub.addText(-(hadj * size.width()), 0, font, text);
+ const auto hoffset = hadj * strWidth(text, font);
+ sub.addText(-hoffset, 0, font, text);
QTransform trans;
trans.translate(x, y);
trans.rotate(-rot);
@@ -497,12 +496,12 @@ void RKGraphicsDevice::text(double x, double y, const QString &text, double rot,
if (current_mask) initMaskedDraw();
painter.save();
- QSizeF size = strSize(text, font); // NOTE: side-effect of setting font!
- // painter.setFont(font);
+ // NOTE: side effect of setting font
+ const auto hoffset = hadj * strWidth(text, font);
painter.setPen(QPen(col));
painter.translate(x, y);
painter.rotate(-rot);
- painter.drawText(-(hadj * size.width()), 0, text);
+ painter.drawText(-hoffset, 0, text);
// painter.drawRect(painter.fontMetrics().boundingRect(text)); // for debugging
painter.restore(); // undo rotation / translation
if (current_mask) commitMaskedDraw();
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
index 243ba6965..a8ce33591 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
@@ -50,7 +50,7 @@ class RKGraphicsDevice : public QObject {
void circle(double x, double y, double r, const QPen &pen, const QBrush &brush);
void line(double x1, double y1, double x2, double y2, const QPen &pen);
void rect(const QRectF &rec, const QPen &pen, const QBrush &brush);
- QSizeF strSize(const QString &text, const QFont &font);
+ double strWidth(const QString &text, const QFont &font);
void text(double x, double y, const QString &text, double rot, double hadj, const QColor &col, const QFont &font);
QString glyph(const QString &font, quint8 index, const QString &family, quint32 weight, QFont::Style style, double size, const QColor &col, double rot, const QVector<QPointF> &points, const QVector<quint32> &glyphs);
void metricInfo(const QChar &c, const QFont &font, double *ascent, double *descent, double *width);
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
index 60f654d54..0f568c8c5 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
@@ -309,7 +309,7 @@ void RKGraphicsDeviceFrontendTransmitter::newData() {
} else if (opcode == RKDStrWidthUTF8) {
QString out;
streamer.instream >> out;
- double w = device->strSize(out, readFont(streamer.instream)).width();
+ double w = device->strWidth(out, readFont(streamer.instream));
streamer.outstream << w;
streamer.writeOutBuffer();
} else if (opcode == RKDMetricInfo) {
More information about the rkward-tracker
mailing list