pdf: text that names no colour comes out black, not the palette

A QTextDocument takes its default text colour from the application
palette, and markdown names no colour at all: in a window running a dark
palette -- which is what a phone in dark mode hands Qt -- every plain
paragraph was laid out #eeeeee on a white page. Printed, that is a blank
document; on screen it reads as "the font is oddly faint". The first
PDFs off the phone were exactly that.

- PdfTools: paperDocument() sets the default stylesheet (body colour
  #000000) before setHtml, and writePdf() lays that out. A colour the
  document does name still wins, so an .html handed to the tool keeps its
  own -- checked with a #cc0000 document under both palettes.
- --uitest: draws that document on a white image under a dark palette and
  looks for ink (238 before the fix, 0 after).

Verified on the NX789J: md_to_pdf on the device now writes CID TrueType
NotoSansCJKHK-Regular/-Bold text whose page renders at min=0.
This commit is contained in:
WHC
2026-10-11 20:35:41 +08:00
parent da04913071
commit 4c69ef39ab
3 changed files with 67 additions and 8 deletions
+34
View File
@@ -13,6 +13,7 @@
#include <QBuffer>
#include <QFile>
#include <QImage>
#include <QPalette>
#include <QJsonDocument>
#include <QJsonObject>
#include <QPainter>
@@ -22,14 +23,17 @@
#include <QQmlContext>
#include <QQuickWindow>
#include <QTest>
#include <QTextDocument>
#include <QVariantMap>
#include <cstdio>
#include <cstring>
#include <memory>
#include "cst_agent.h"
#include "cst_attach.h"
#include "core/ToolProvider.h"
#include "ui/SessionImages.h"
#include "ui/ThemeIcons.h"
@@ -482,6 +486,36 @@ int runUiTest(AgentCore &core, const QString &size) {
}
}
/* Paper is white whatever the window's palette says: markdown carries no
* colour, so a dark window turned every plain paragraph #eeeeee -- a PDF
* that looked blank. Draw it on a white image and look for ink. */
{
const QPalette was = QGuiApplication::palette();
QPalette darkPalette = was;
darkPalette.setColor(QPalette::Text, QColor(0xee, 0xee, 0xee));
darkPalette.setColor(QPalette::WindowText, QColor(0xee, 0xee, 0xee));
QGuiApplication::setPalette(darkPalette);
std::unique_ptr<QTextDocument> page(app::paperDocument(
QStringLiteral("<html><body>text that names no colour of its own"
"</body></html>")));
QImage paper(320, 60, QImage::Format_RGB32);
paper.fill(0xffffffffu);
QPainter ink(&paper);
page->drawContents(&ink);
ink.end();
int darkest = 255;
for (int y = 0; y < paper.height(); y++) {
const QRgb *row = reinterpret_cast<const QRgb *>(paper.constScanLine(y));
for (int x = 0; x < paper.width(); x++)
darkest = qMin(darkest, qGray(row[x]));
}
check(darkest < 64,
QStringLiteral("a dark window still lays out black ink on paper "
"(darkest pixel %1)")
.arg(darkest));
QGuiApplication::setPalette(was);
}
/* new sessions: the engine moves, the view empties, the store grows */
const int before = core.sessions().size();
clickMouse(win, QStringLiteral("newSessionButton"));