chore: format llm c files
C++ / fmt (pull_request) Failing after 4s
C++ / build (pull_request) Failing after 9s
JS/TS / lint (pull_request) Successful in 9s
JS/TS / fmt (pull_request) Successful in 16s
C++ / clang-tidy (pull_request) Failing after 43s
Python / static (pull_request) Failing after 57s
Rust / fmt (pull_request) Successful in 1m30s
Rust / build (pull_request) Successful in 2m5s
Rust / clippy (pull_request) Successful in 1m55s
Python / verify (pull_request) Successful in 2m23s

This commit is contained in:
2026-08-31 19:02:26 +02:00
parent dbb51ceadd
commit 52feb6006a
27 changed files with 741 additions and 1190 deletions
+99 -123
View File
@@ -16,15 +16,10 @@
namespace ZShell::llm {
namespace {
// A few px of breathing room around the equation.
constexpr int kRenderMargin = 2;
constexpr unsigned int kResolutionDpi = 96;
// The render cache is unbounded between clears; cap it so a very long
// session with many distinct equations cannot grow it forever.
constexpr int kCacheLimit = 512;
// Registers the embedded Latin Modern faces with the font database
// (once per process) and reports which families became available.
struct LatinModern {
bool roman = false;
bool math = false;
@@ -33,37 +28,44 @@ struct LatinModern {
const LatinModern& loadLatinModern() {
static const LatinModern fonts = [] {
LatinModern result;
// addApplicationFont(QByteArray) fails in this environment, so
// the embedded bytes are staged to a per-process temp file and
// registered through the (stable) file-based API.
const auto add = [](const unsigned char* data, size_t size,
const QString& fileName, const QString& family) {
const auto add = [](const unsigned char* data,
size_t size,
const QString& fileName,
const QString& family) {
const QString path = QDir::tempPath() + QLatin1Char('/') + fileName;
{
QFile f(path);
if (!f.open(QIODevice::WriteOnly) ||
f.write(reinterpret_cast<const char*>(data),
static_cast<qint64>(size)) != static_cast<qint64>(size))
f.write(
reinterpret_cast<const char*>(data),
static_cast<qint64>(size)) != static_cast<qint64>(size))
return false;
}
const int key = QFontDatabase::addApplicationFont(path);
if (key < 0)
return false;
if (key < 0) return false;
return QFontDatabase::applicationFontFamilies(key).contains(family);
};
result.roman =
add(lmfont::lmroman10_regular, sizeof(lmfont::lmroman10_regular),
QStringLiteral("lmroman10-regular.otf"), QStringLiteral("LMRoman10"))
&& add(lmfont::lmroman10_italic, sizeof(lmfont::lmroman10_italic),
QStringLiteral("lmroman10-italic.otf"), QStringLiteral("LMRoman10"))
&& add(lmfont::lmroman10_bold, sizeof(lmfont::lmroman10_bold),
QStringLiteral("lmroman10-bold.otf"), QStringLiteral("LMRoman10"))
&& add(lmfont::lmroman10_bolditalic, sizeof(lmfont::lmroman10_bolditalic),
QStringLiteral("lmroman10-bolditalic.otf"),
QStringLiteral("LMRoman10"));
result.math = add(
lmfont::latinmodern_math, sizeof(lmfont::latinmodern_math),
QStringLiteral("latinmodern-math.otf"), QStringLiteral("Latin Modern Math"));
result.roman = add(lmfont::lmroman10_regular,
sizeof(lmfont::lmroman10_regular),
QStringLiteral("lmroman10-regular.otf"),
QStringLiteral("LMRoman10")) &&
add(lmfont::lmroman10_italic,
sizeof(lmfont::lmroman10_italic),
QStringLiteral("lmroman10-italic.otf"),
QStringLiteral("LMRoman10")) &&
add(lmfont::lmroman10_bold,
sizeof(lmfont::lmroman10_bold),
QStringLiteral("lmroman10-bold.otf"),
QStringLiteral("LMRoman10")) &&
add(lmfont::lmroman10_bolditalic,
sizeof(lmfont::lmroman10_bolditalic),
QStringLiteral("lmroman10-bolditalic.otf"),
QStringLiteral("LMRoman10"));
result.math =
add(lmfont::latinmodern_math,
sizeof(lmfont::latinmodern_math),
QStringLiteral("latinmodern-math.otf"),
QStringLiteral("Latin Modern Math"));
return result;
}();
return fonts;
@@ -72,22 +74,17 @@ const LatinModern& loadLatinModern() {
} // namespace
LlmMathText::LlmMathText(QObject* parent)
// No parent: the renderer is used from pool threads, and a parented
// QObject would taint children it creates there.
: QObject(parent), m_renderer(std::make_shared<JKQTMathText>(
nullptr, /* useFontsForGUI */ true)) {
// Latin Modern is the default font of modern LaTeX; use the embedded
// faces instead of whatever the system happens to have installed.
: QObject(parent)
, m_renderer(
std::make_shared<JKQTMathText>(nullptr, /* useFontsForGUI */ true)) {
const LatinModern& fonts = loadLatinModern();
if (fonts.roman)
m_renderer->setFontRomanAndMath(QStringLiteral("LMRoman10"),
JKQTMathTextFontEncoding::MTFEUnicode);
m_renderer->setFontRomanAndMath(
QStringLiteral("LMRoman10"), JKQTMathTextFontEncoding::MTFEUnicode);
if (fonts.math) {
// Same pattern as JKQTMathText's useXITS(): the OpenType math
// font supplies the math alphabet and operators from its MATH
// table.
m_renderer->setFontMathRoman(QStringLiteral("Latin Modern Math"),
JKQTMathTextFontEncoding::MTFEUnicode);
m_renderer->setFontMathRoman(
QStringLiteral("Latin Modern Math"),
JKQTMathTextFontEncoding::MTFEUnicode);
m_renderer->setFallbackFontSymbols(
QStringLiteral("Latin Modern Math"),
JKQTMathTextFontEncoding::MTFEUnicode);
@@ -95,38 +92,31 @@ LlmMathText::LlmMathText(QObject* parent)
}
void LlmMathText::setLatex(const QString& value) {
if (m_latex == value)
return;
if (m_latex == value) return;
m_latex = value;
reRender();
}
void LlmMathText::setColor(const QColor& value) {
if (m_color == value)
return;
if (m_color == value) return;
m_color = value;
reRender();
}
void LlmMathText::setFontPointSize(double value) {
if (qFuzzyCompare(m_fontPointSize, value))
return;
if (qFuzzyCompare(m_fontPointSize, value)) return;
m_fontPointSize = value;
reRender();
}
void LlmMathText::setDevicePixelRatio(qreal value) {
if (qFuzzyCompare(m_devicePixelRatio, value))
return;
if (qFuzzyCompare(m_devicePixelRatio, value)) return;
m_devicePixelRatio = value;
reRender();
}
namespace {
// Process-wide render cache; the key is the full render state, so
// re-opening a chat reuses already-rendered equations. Accessed on the
// GUI thread only.
struct MathRender {
bool ok = false;
QImage image;
@@ -154,10 +144,9 @@ void LlmMathText::reRender() {
return;
}
const QString key = m_latex
+ QLatin1Char(0x1f) + m_color.name()
+ QLatin1Char(0x1f) + QString::number(m_fontPointSize)
+ QLatin1Char(0x1f) + QString::number(m_devicePixelRatio);
const QString key = m_latex + QLatin1Char(0x1f) + m_color.name() +
QLatin1Char(0x1f) + QString::number(m_fontPointSize) +
QLatin1Char(0x1f) + QString::number(m_devicePixelRatio);
if (auto it = mathCache().find(key); it != mathCache().end()) {
m_image = it->image;
m_imageUrl = it->url;
@@ -168,83 +157,70 @@ void LlmMathText::reRender() {
return;
}
// A render is already running; it re-renders the latest state when
// it completes (id mismatch), so there is nothing to do here.
if (m_inFlight)
return;
if (m_inFlight) return;
m_inFlight = true;
const QString latex = m_latex;
const QColor color = m_color;
const double pointSize = m_fontPointSize;
const qreal dpr = m_devicePixelRatio;
// The worker captures the renderer by value (shared_ptr) so it
// stays alive even if this object is destroyed mid-render; it is
// only ever used by the single in-flight worker (m_inFlight),
// never concurrently.
auto renderer = m_renderer;
QThreadPool::globalInstance()->start([this, renderer, id = m_requestId, key, latex, color, pointSize, dpr]() {
MathRender render;
renderer->setFontPointSize(pointSize);
renderer->setFontColor(color);
if (renderer->parse(
latex, JKQTMathText::LatexParser, JKQTMathText::DefaultParseOptions)) {
const QImage image = renderer->drawIntoImage(
/* drawBoxes */ false,
QColor(Qt::transparent),
kRenderMargin,
dpr,
kResolutionDpi);
if (!image.isNull()) {
QByteArray png;
{
QBuffer buffer(&png);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
QThreadPool::globalInstance()->start(
[this, renderer, id = m_requestId, key, latex, color, pointSize, dpr]() {
MathRender render;
renderer->setFontPointSize(pointSize);
renderer->setFontColor(color);
if (renderer->parse(
latex,
JKQTMathText::LatexParser,
JKQTMathText::DefaultParseOptions)) {
const QImage image = renderer->drawIntoImage(
/* drawBoxes */ false,
QColor(Qt::transparent),
kRenderMargin,
dpr,
kResolutionDpi);
if (!image.isNull()) {
QByteArray png;
{
QBuffer buffer(&png);
buffer.open(QIODevice::WriteOnly);
image.save(&buffer, "PNG");
}
render.image = image;
render.url = QUrl(
QStringLiteral("data:image/png;base64,") +
QString::fromLatin1(png.toBase64()));
render.width = image.width() / dpr;
render.height = image.height() / dpr;
render.ok = true;
}
render.image = image;
render.url = QUrl(
QStringLiteral("data:image/png;base64,")
+ QString::fromLatin1(png.toBase64()));
// drawIntoImage renders at devicePixelRatio; convert
// back to logical pixels.
render.width = image.width() / dpr;
render.height = image.height() / dpr;
render.ok = true;
}
}
// Deliver through the app instance (never destroyed) and
// re-check the pointer on the GUI thread: posting to `this`
// from the pool thread would race with its destruction.
QPointer<LlmMathText> guard(this);
QMetaObject::invokeMethod(
QCoreApplication::instance(),
[guard, id, key, render = std::move(render)]() mutable {
LlmMathText* self = guard;
if (!self)
return;
self->m_inFlight = false;
if (id != self->m_requestId) {
// Superseded while the worker ran; render the
// latest state.
self->reRender();
return;
}
if (render.ok) {
auto& cache = mathCache();
if (cache.size() >= kCacheLimit)
cache.clear();
cache.insert(key, render);
}
self->m_image = render.image;
self->m_imageUrl = render.url;
self->m_width = render.width;
self->m_height = render.height;
self->m_ok = render.ok;
Q_EMIT self->changed();
},
Qt::QueuedConnection);
});
QPointer<LlmMathText> guard(this);
QMetaObject::invokeMethod(
QCoreApplication::instance(),
[guard, id, key, render = std::move(render)]() mutable {
LlmMathText* self = guard;
if (!self) return;
self->m_inFlight = false;
if (id != self->m_requestId) {
self->reRender();
return;
}
if (render.ok) {
auto& cache = mathCache();
if (cache.size() >= kCacheLimit) cache.clear();
cache.insert(key, render);
}
self->m_image = render.image;
self->m_imageUrl = render.url;
self->m_width = render.width;
self->m_height = render.height;
self->m_ok = render.ok;
Q_EMIT self->changed();
},
Qt::QueuedConnection);
});
}
} // namespace ZShell::llm