[education/rkward] rkward/rbackend/rkwarddevice: Warn, if exact font could not be loaded.

Thomas Friedrichsmeier null at kde.org
Sat Sep 7 10:22:47 BST 2024


Git commit 71c47debc85ab07bbb587435a6761f8855b1bea2 by Thomas Friedrichsmeier.
Committed on 07/09/2024 at 08:30.
Pushed by tfry into branch 'master'.

Warn, if exact font could not be loaded.

M  +12   -4    rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
M  +1    -1    rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
M  +3    -1    rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
M  +1    -1    rkward/rbackend/rkwarddevice/rkgraphicsdevice_protocol_shared.h
M  +23   -14   rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp

https://invent.kde.org/education/rkward/-/commit/71c47debc85ab07bbb587435a6761f8855b1bea2

diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
index b131bd005..e2b589e5d 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.cpp
@@ -501,17 +501,24 @@ void RKGraphicsDevice::text(double x, double y, const QString& text, double rot,
 	triggerUpdate();
 }
 
-void RKGraphicsDevice::glyph(const QString &font, quint8 index, const QString &family, quint32 weight, QFont::Style style, double size, const QColor &col, double rot, QVector<QPointF> points, QVector<quint32> glyphs) {
+QString RKGraphicsDevice::glyph(const QString &font, quint8 index, const QString &family, quint32 weight, QFont::Style style, double size, const QColor &col, double rot, QVector<QPointF> points, QVector<quint32> glyphs) {
 	RK_TRACE(GRAPHICS_DEVICE);
 	RK_ASSERT(points.size() == glyphs.size());
 
 	QRawFont rfnt;
+	QString ret;
 	if (index == 0) {
 		rfnt = QRawFont(font, size);
 	}
 	if (!rfnt.isValid()) {
-qDebug("invalid font %s, %d", qPrintable(font), index);
-// TODO create warning, somewhere
+		if (index != 0) {
+			// NOTE: This is currently the only graphics operation that may return a warning message.
+			//       If warnings were handled some other way, it might not have to be synchronous, but the real-life performance impact
+			//       is thought to be neglegible. (Measured around 7% on the grid-library glyphs test case.)
+			ret = QStringLiteral("Using font index != 0 is not supported in RK() device.");
+		} else {
+			ret = QStringLiteral("Invalid font '%1'.").arg(font);
+		}
 		QFont fnt(family, size, weight);
 		fnt.setStyle(style);
 		rfnt = QRawFont::fromFont(fnt);
@@ -533,7 +540,7 @@ qDebug("invalid font %s, %d", qPrintable(font), index);
 
 	if (recording_path) {
 		recorded_path.addPath(path);
-		return;
+		return ret;
 	}
 
 	if (current_mask) initMaskedDraw();
@@ -541,6 +548,7 @@ qDebug("invalid font %s, %d", qPrintable(font), index);
 	if (current_mask) commitMaskedDraw();
 
 	triggerUpdate();
+	return ret;
 }
 
 void RKGraphicsDevice::metricInfo (const QChar& c, const QFont& font, double* ascent, double* descent, double* width) {
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
index a27fc1688..62aee1013 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice.h
@@ -51,7 +51,7 @@ public:
 	void rect (const QRectF& rec, const QPen& pen, const QBrush& brush);
 	QSizeF strSize (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);
-	void glyph(const QString &font, quint8 index, const QString &family, quint32 weight, QFont::Style style, double size, const QColor &col, double rot, QVector<QPointF> points, QVector<quint32> glyphs);
+	QString glyph(const QString &font, quint8 index, const QString &family, quint32 weight, QFont::Style style, double size, const QColor &col, double rot, QVector<QPointF> points, QVector<quint32> glyphs);
 	void metricInfo (const QChar& c, const QFont& font, double *ascent, double *descent, double *width);
 	void setClip (const QRectF& new_clip);
 	void polygon (const QPolygonF& pol, const QPen& pen, const QBrush &brush);
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
index 2aac1b19a..dedf0a77e 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_frontendtransmitter.cpp
@@ -479,7 +479,9 @@ void RKGraphicsDeviceFrontendTransmitter::newData () {
 			}
 			// NOTE: contrary to other font sizes, size here is given in device independent "bigpts" (1 inch / 72), which need to be scaled to resolution
 			size = size * (dpix / 72.0);
-			device->glyph(font, index, family, weight, static_cast<QFont::Style>(style), size, col, rot, points, glyphs);
+			QString ret = device->glyph(font, index, family, weight, static_cast<QFont::Style>(style), size, col, rot, points, glyphs);
+			streamer.outstream << ret;
+			streamer.writeOutBuffer();
 		} else if (opcode == RKDCapture) {
 			QImage image = device->capture ();
 			quint32 w = image.width ();
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_protocol_shared.h b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_protocol_shared.h
index 54b754ece..b001ed674 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_protocol_shared.h
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_protocol_shared.h
@@ -89,7 +89,6 @@ enum RKDOpcodes {
 	RKDDefineGroupBegin,
 	RKDDefineGroupStep2,
 	RKDUseGroup,           // 25
-	RKDGlyph,
 
 	// Synchronous operations
 	RKDFetchNextEvent      = 100,
@@ -108,6 +107,7 @@ enum RKDOpcodes {
 	RKDEndRecordMask,
 	RKDDefineGroupEnd,
 	RKDClose,             // 115
+	RKDGlyph,
 
 	// Protocol operations
 	RKDCancel              = 200
diff --git a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
index 19890d3aa..bd11df00b 100644
--- a/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
+++ b/rkward/rbackend/rkwarddevice/rkgraphicsdevice_stubs.cpp
@@ -938,20 +938,29 @@ void RKD_FillStroke(SEXP path, int rule, const pGEcontext gc, pDevDesc dev) {
 #if R_VERSION >= R_Version(4,3,0)
 void RKD_Glyph(int n, int *glyphs, double *x, double *y, SEXP font, double size, int colour, double rot, pDevDesc dev) {
 	RK_TRACE(GRAPHICS_DEVICE);
-	RKGraphicsDataStreamWriteGuard guard;
-	WRITE_HEADER(RKDGlyph, dev);
-
-	QString qfont = QString(RFn::R_GE_glyphFontFile(font));
-	quint8 index = RFn::R_GE_glyphFontIndex(font);
-	QString family = QString(RFn::R_GE_glyphFontFamily(font));
-	quint32 weight = RFn::R_GE_glyphFontWeight(font);
-	quint8 style = mapTextStyle(RFn::R_GE_glyphFontStyle(font));
-	// NOTE: family, weight, and style are used as fallback, if font(-file), and index don't work
-
-	WRITE_COLOR_BYTES(colour);
-	RKD_OUT_STREAM << qfont << index << family << weight << style << size << rot << (quint32) n;
-	for (int i = 0; i < n; ++i) {
-		RKD_OUT_STREAM << x[i] << y[i] << (quint32) glyphs[i];
+	{
+		RKGraphicsDataStreamWriteGuard guard;
+		WRITE_HEADER(RKDGlyph, dev);
+
+		QString qfont = QString(RFn::R_GE_glyphFontFile(font));
+		quint8 index = RFn::R_GE_glyphFontIndex(font);
+		QString family = QString(RFn::R_GE_glyphFontFamily(font));
+		quint32 weight = RFn::R_GE_glyphFontWeight(font);
+		quint8 style = mapTextStyle(RFn::R_GE_glyphFontStyle(font));
+		// NOTE: family, weight, and style are used as fallback, if font(-file), and index don't work
+
+		WRITE_COLOR_BYTES(colour);
+		RKD_OUT_STREAM << qfont << index << family << weight << style << size << rot << (quint32) n;
+		for (int i = 0; i < n; ++i) {
+			RKD_OUT_STREAM << x[i] << y[i] << (quint32) glyphs[i];
+		}
+	}
+
+	{
+		RKGraphicsDataStreamReadGuard rguard;
+		QString warning;
+		RKD_IN_STREAM >> warning;
+		if (!warning.isEmpty()) RFn::Rf_warning("%s", qPrintable(warning));
 	}
 }
 #endif



More information about the rkward-tracker mailing list