visual fix

This commit is contained in:
Agel committed 2026-10-25 22:12:32 +01:00
1 parent 00c88a940c
commit 905c61da52
20 files changed
+669 -32

No files matched your search

+85 -15
View File
@@ -58,6 +58,24 @@ void KnuthPlassItem::setDimColor(const QColor &c)
}
}
void KnuthPlassItem::setSurfaceColor(const QColor &c)
{
if (m_surfaceColor != c) {
m_surfaceColor = c;
emit colorsChanged();
update();
}
}
void KnuthPlassItem::setUseBookColours(bool v)
{
if (m_useBookColours != v) {
m_useBookColours = v;
emit useBookColoursChanged();
update();
}
}
void KnuthPlassItem::setHorizontalMargin(qreal m)
{
if (!qFuzzyCompare(m_horizontalMargin, m)) {
@@ -67,6 +85,26 @@ void KnuthPlassItem::setHorizontalMargin(qreal m)
}
}
qreal KnuthPlassItem::relativeLuminance(const QColor &c)
{
auto channel = [](qreal val) {
return (val <= 0.03928) ? (val / 12.92) : std::pow((val + 0.055) / 1.055, 2.4);
};
const qreal r = channel(c.redF());
const qreal g = channel(c.greenF());
const qreal b = channel(c.blueF());
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
qreal KnuthPlassItem::contrastRatio(const QColor &c1, const QColor &c2)
{
const qreal l1 = relativeLuminance(c1);
const qreal l2 = relativeLuminance(c2);
const qreal lighter = std::max(l1, l2);
const qreal darker = std::min(l1, l2);
return (lighter + 0.05) / (darker + 0.05);
}
void KnuthPlassItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
QQuickPaintedItem::geometryChange(newGeometry, oldGeometry);
@@ -91,19 +129,30 @@ void KnuthPlassItem::paint(QPainter *painter)
QRectF r = dec.rect;
r.translate(offX, 0);
if (dec.background.isValid()) {
painter->fillRect(r, dec.background);
}
if (m_useBookColours) {
if (dec.background.isValid() && dec.background.alpha() > 0) {
painter->fillRect(r, dec.background);
}
QColor borderColor = dec.borderColor.isValid() ? dec.borderColor : m_dimColor;
if (dec.borderLeft > 0) {
painter->fillRect(QRectF(r.left(), r.top(), dec.borderLeft, r.height()), borderColor);
}
if (dec.borderTop > 0) {
painter->fillRect(QRectF(r.left(), r.top(), r.width(), dec.borderTop), borderColor);
}
if (dec.borderBottom > 0) {
painter->fillRect(QRectF(r.left(), r.bottom() - dec.borderBottom, r.width(), dec.borderBottom), borderColor);
QColor borderColor = dec.borderColor.isValid() ? dec.borderColor : m_dimColor;
if (dec.borderLeft > 0) {
painter->fillRect(QRectF(r.left(), r.top(), dec.borderLeft, r.height()), borderColor);
}
if (dec.borderTop > 0) {
painter->fillRect(QRectF(r.left(), r.top(), r.width(), dec.borderTop), borderColor);
}
if (dec.borderBottom > 0) {
painter->fillRect(QRectF(r.left(), r.bottom() - dec.borderBottom, r.width(), dec.borderBottom), borderColor);
}
if (dec.borderRight > 0) {
painter->fillRect(QRectF(r.right() - dec.borderRight, r.top(), dec.borderRight, r.height()), borderColor);
}
} else {
// When useBookColours is false, omit publisher boxes/borders.
// Only draw semantic quotation bars (e.g. <blockquote> with only borderLeft).
if (dec.borderLeft > 0 && dec.borderTop == 0 && dec.borderBottom == 0 && dec.borderRight == 0 && !dec.borderColor.isValid()) {
painter->fillRect(QRectF(r.left(), r.top(), dec.borderLeft, r.height()), m_dimColor);
}
}
}
@@ -111,7 +160,8 @@ void KnuthPlassItem::paint(QPainter *painter)
for (const auto &rule : m_layout.rules) {
QRectF r = rule.rect;
r.translate(offX, 0);
painter->fillRect(r, rule.color.isValid() ? rule.color : m_dimColor);
QColor rc = (m_useBookColours && rule.color.isValid()) ? rule.color : m_dimColor;
painter->fillRect(r, rc);
}
// 3. Draw text runs
@@ -120,18 +170,38 @@ void KnuthPlassItem::paint(QPainter *painter)
bool hasFont = false;
bool hasPen = false;
const QColor baseSurface = m_surfaceColor.isValid() ? m_surfaceColor : QColor(25, 29, 53);
for (const auto &line : m_layout.lines) {
for (const auto &run : line.runs) {
QRectF r = run.rect;
r.translate(offX, 0);
// Highlight background
if (run.background.isValid()) {
if (m_useBookColours && run.background.isValid() && run.background.alpha() > 0) {
painter->fillRect(r, run.background);
}
// Word text
QColor fg = run.color.isValid() ? run.color : (run.link ? m_linkColor : m_textColor);
QColor fg;
if (!m_useBookColours) {
fg = run.link ? m_linkColor : m_textColor;
} else {
QColor effectiveBg = (run.background.isValid() && run.background.alpha() > 0)
? run.background
: ((run.surfaceBackground.isValid() && run.surfaceBackground.alpha() > 0)
? run.surfaceBackground
: baseSurface);
QColor desired = run.color.isValid() ? run.color : (run.link ? m_linkColor : m_textColor);
if (contrastRatio(desired, effectiveBg) < 3.0) {
qreal crBlack = contrastRatio(Qt::black, effectiveBg);
qreal crWhite = contrastRatio(Qt::white, effectiveBg);
fg = (crBlack >= crWhite) ? QColor(Qt::black) : QColor(Qt::white);
} else {
fg = desired;
}
}
if (!hasPen || currentPenColor != fg) {
painter->setPen(fg);
currentPenColor = fg;
+14
View File
@@ -15,6 +15,8 @@ class KnuthPlassItem : public QQuickPaintedItem {
Q_PROPERTY(QColor textColor READ textColor WRITE setTextColor NOTIFY colorsChanged)
Q_PROPERTY(QColor linkColor READ linkColor WRITE setLinkColor NOTIFY colorsChanged)
Q_PROPERTY(QColor dimColor READ dimColor WRITE setDimColor NOTIFY colorsChanged)
Q_PROPERTY(QColor surfaceColor READ surfaceColor WRITE setSurfaceColor NOTIFY colorsChanged)
Q_PROPERTY(bool useBookColours READ useBookColours WRITE setUseBookColours NOTIFY useBookColoursChanged)
Q_PROPERTY(qreal horizontalMargin READ horizontalMargin WRITE setHorizontalMargin NOTIFY horizontalMarginChanged)
public:
@@ -32,9 +34,18 @@ public:
QColor dimColor() const { return m_dimColor; }
void setDimColor(const QColor &c);
QColor surfaceColor() const { return m_surfaceColor; }
void setSurfaceColor(const QColor &c);
bool useBookColours() const { return m_useBookColours; }
void setUseBookColours(bool v);
qreal horizontalMargin() const { return m_horizontalMargin; }
void setHorizontalMargin(qreal m);
static qreal relativeLuminance(const QColor &c);
static qreal contrastRatio(const QColor &c1, const QColor &c2);
void paint(QPainter *painter) override;
static void setImageResolver(BookImageResolver *resolver);
@@ -45,6 +56,7 @@ protected:
signals:
void layoutChanged();
void colorsChanged();
void useBookColoursChanged();
void horizontalMarginChanged();
private:
@@ -54,6 +66,8 @@ private:
QColor m_textColor = QColor(196, 200, 216); // Theme.readingText default
QColor m_linkColor = QColor(87, 133, 193); // Theme.buttonAccent default
QColor m_dimColor = QColor(113, 115, 140); // Theme.dimText default
QColor m_surfaceColor = QColor(25, 29, 53); // Theme.surface default
bool m_useBookColours = false;
qreal m_horizontalMargin = 0;
static BookImageResolver *s_imageResolver;
@@ -159,8 +159,10 @@ private:
dec.borderLeft = box.borderLeft;
dec.borderTop = box.borderTop;
dec.borderBottom = box.borderBottom;
dec.borderRight = box.borderRight;
if (dec.background.isValid() || dec.borderLeft > 0 || dec.borderTop > 0 || dec.borderBottom > 0) {
if (dec.background.isValid() || dec.borderLeft > 0 || dec.borderTop > 0
|| dec.borderBottom > 0 || dec.borderRight > 0) {
m_layout.decorations.append(dec);
}
@@ -247,6 +249,14 @@ private:
if (p.words.isEmpty() && p.kpItems.isEmpty())
return;
QColor enclosingBg;
for (int i = m_boxStack.size() - 1; i >= 0; --i) {
if (m_boxStack[i].box.background.isValid() && m_boxStack[i].box.background.alpha() > 0) {
enclosingBg = m_boxStack[i].box.background;
break;
}
}
qreal left = currentLeft();
qreal right = currentRight();
qreal availWidth = std::max(50.0, m_params.contentWidth - left - right);
@@ -379,6 +389,7 @@ private:
run.font = tok.font;
run.color = tok.style.color;
run.background = tok.style.background;
run.surfaceBackground = enclosingBg;
run.link = tok.style.link;
run.underline = tok.style.underline;
run.strikeOut = tok.style.strikeOut;
@@ -19,6 +19,7 @@ struct PositionedWordRun {
QFont font;
QColor color;
QColor background;
QColor surfaceBackground;
bool link = false;
bool underline = false;
bool strikeOut = false;
@@ -48,6 +49,7 @@ struct PositionedDecoration {
qreal borderLeft = 0;
qreal borderTop = 0;
qreal borderBottom = 0;
qreal borderRight = 0;
};
struct ChunkLayout {
+42
View File
@@ -398,6 +398,48 @@ QColor parseColor(QStringView valueIn)
return c.isValid() ? c : QColor();
}
BorderSide parseBorderSide(QStringView valueIn, const LengthContext &ctx)
{
BorderSide res;
const QStringList tokens = splitValue(valueIn);
bool hasStyle = false;
for (const QString &token : tokens) {
const QString lower = token.trimmed().toLower();
if (lower.isEmpty())
continue;
if (lower == QLatin1String("none") || lower == QLatin1String("hidden")) {
res.width = 0.0;
res.isNone = true;
} else if (lower == QLatin1String("thin")) {
res.width = 1.0 * ctx.absoluteScale;
} else if (lower == QLatin1String("medium")) {
res.width = 3.0 * ctx.absoluteScale;
} else if (lower == QLatin1String("thick")) {
res.width = 5.0 * ctx.absoluteScale;
} else if (lower == QLatin1String("solid") || lower == QLatin1String("dotted")
|| lower == QLatin1String("dashed") || lower == QLatin1String("double")
|| lower == QLatin1String("groove") || lower == QLatin1String("ridge")
|| lower == QLatin1String("inset") || lower == QLatin1String("outset")) {
hasStyle = true;
} else if (auto len = parseLength(token, ctx)) {
res.width = *len;
} else {
QColor c = parseColor(token);
if (c.isValid()) {
res.color = c;
}
}
}
if (hasStyle && !res.width.has_value() && !res.isNone) {
res.width = 3.0 * ctx.absoluteScale;
}
return res;
}
QStringList splitValue(QStringView value)
{
QStringList out;
+7
View File
@@ -72,8 +72,15 @@ struct LengthContext {
qreal absoluteScale = 1; // scale applied to absolute units (px, pt...) to follow the user's font size
};
struct BorderSide {
std::optional<qreal> width;
QColor color;
bool isNone = false;
};
std::optional<qreal> parseLength(QStringView value, const LengthContext &ctx);
QColor parseColor(QStringView value);
BorderSide parseBorderSide(QStringView value, const LengthContext &ctx);
// Splits a value on top-level whitespace (keeps "rgb(1, 2, 3)" together).
QStringList splitValue(QStringView value);
+124 -12
View File
@@ -51,7 +51,8 @@ bool isBlockTag(const QString &tag)
void applyCssDeclarations(const QVector<Css::Declaration> &decls,
StyleContext &ctx,
BoxStyle &box,
const ParseParams &params)
const ParseParams &params,
bool isBlock)
{
Css::LengthContext lenCtx;
lenCtx.em = ctx.text.fontSize;
@@ -111,7 +112,20 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
if (c.isValid()) ctx.text.color = c;
} else if (prop == QLatin1String("background-color") || prop == QLatin1String("background")) {
QColor c = Css::parseColor(val);
if (c.isValid()) ctx.text.background = c;
if (c.isValid() && c.alpha() > 0) {
if (isBlock) {
box.background = c;
ctx.text.background = QColor();
} else {
ctx.text.background = c;
}
} else if (val.trimmed() == QLatin1String("none") || val.trimmed() == QLatin1String("transparent")) {
if (isBlock) {
box.background = QColor();
} else {
ctx.text.background = QColor();
}
}
} else if (prop == QLatin1String("text-align")) {
if (val == QLatin1String("left") || val == QLatin1String("start")) ctx.align = TextAlign::Left;
else if (val == QLatin1String("center")) ctx.align = TextAlign::Center;
@@ -146,15 +160,72 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingLeft = *p;
} else if (prop == QLatin1String("padding-right")) {
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingRight = *p;
} else if (prop == QLatin1String("border-left") || prop == QLatin1String("border-left-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderLeft = *w;
} else if (prop == QLatin1String("border-top") || prop == QLatin1String("border-top-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderTop = *w;
} else if (prop == QLatin1String("border-bottom") || prop == QLatin1String("border-bottom-width")) {
auto w = Css::parseLength(val, lenCtx); if (w) box.borderBottom = *w;
} else if (prop == QLatin1String("border-color") || prop == QLatin1String("border-left-color")) {
} else if (prop == QLatin1String("border")) {
auto b = Css::parseBorderSide(val, lenCtx);
if (b.width) {
box.borderTop = *b.width;
box.borderRight = *b.width;
box.borderBottom = *b.width;
box.borderLeft = *b.width;
}
if (b.color.isValid()) box.borderColor = b.color;
} else if (prop == QLatin1String("border-top")) {
auto b = Css::parseBorderSide(val, lenCtx);
if (b.width) box.borderTop = *b.width;
if (b.color.isValid()) box.borderColor = b.color;
} else if (prop == QLatin1String("border-right")) {
auto b = Css::parseBorderSide(val, lenCtx);
if (b.width) box.borderRight = *b.width;
if (b.color.isValid()) box.borderColor = b.color;
} else if (prop == QLatin1String("border-bottom")) {
auto b = Css::parseBorderSide(val, lenCtx);
if (b.width) box.borderBottom = *b.width;
if (b.color.isValid()) box.borderColor = b.color;
} else if (prop == QLatin1String("border-left")) {
auto b = Css::parseBorderSide(val, lenCtx);
if (b.width) box.borderLeft = *b.width;
if (b.color.isValid()) box.borderColor = b.color;
} else if (prop == QLatin1String("border-top-width")) {
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderTop = *w;
} else if (prop == QLatin1String("border-right-width")) {
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderRight = *w;
} else if (prop == QLatin1String("border-bottom-width")) {
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderBottom = *w;
} else if (prop == QLatin1String("border-left-width")) {
auto w = Css::parseBorderSide(val, lenCtx).width; if (w) box.borderLeft = *w;
} else if (prop == QLatin1String("border-width")) {
const QStringList tokens = Css::splitValue(val);
QVector<qreal> widths;
for (const QString &tok : tokens) {
auto w = Css::parseBorderSide(tok, lenCtx).width;
if (w) widths.append(*w);
}
if (widths.size() == 1) {
box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = widths[0];
} else if (widths.size() == 2) {
box.borderTop = box.borderBottom = widths[0];
box.borderRight = box.borderLeft = widths[1];
} else if (widths.size() == 3) {
box.borderTop = widths[0];
box.borderRight = box.borderLeft = widths[1];
box.borderBottom = widths[2];
} else if (widths.size() >= 4) {
box.borderTop = widths[0];
box.borderRight = widths[1];
box.borderBottom = widths[2];
box.borderLeft = widths[3];
}
} else if (prop == QLatin1String("border-color") || prop.endsWith(QLatin1String("-color"))) {
QColor c = Css::parseColor(val);
if (c.isValid()) box.borderColor = c;
} else if (prop == QLatin1String("border-style") || prop.endsWith(QLatin1String("-style"))) {
if (val.contains(QLatin1String("none")) || val.contains(QLatin1String("hidden"))) {
if (prop == QLatin1String("border-top-style")) box.borderTop = 0;
else if (prop == QLatin1String("border-right-style")) box.borderRight = 0;
else if (prop == QLatin1String("border-bottom-style")) box.borderBottom = 0;
else if (prop == QLatin1String("border-left-style")) box.borderLeft = 0;
else { box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = 0; }
}
}
}
}
@@ -178,6 +249,45 @@ public:
private:
void finishCurrentParagraph()
{
if (m_currentInlines.isEmpty())
return;
if (!m_currentPreformatted) {
// Drop trailing line breaks and whitespace
while (!m_currentInlines.isEmpty()) {
const auto &last = m_currentInlines.last();
if (last.kind == InlineItem::Kind::LineBreak) {
m_currentInlines.removeLast();
} else if (last.kind == InlineItem::Kind::Text && last.text.trimmed().isEmpty()) {
m_currentInlines.removeLast();
} else {
break;
}
}
if (!m_currentInlines.isEmpty() && m_currentInlines.last().kind == InlineItem::Kind::Text) {
while (m_currentInlines.last().text.endsWith(QLatin1Char(' '))) {
m_currentInlines.last().text.chop(1);
}
}
// Drop leading whitespace
while (!m_currentInlines.isEmpty()) {
const auto &first = m_currentInlines.first();
if (first.kind == InlineItem::Kind::Text && first.text.trimmed().isEmpty()) {
m_currentInlines.removeFirst();
} else {
break;
}
}
if (!m_currentInlines.isEmpty() && m_currentInlines.first().kind == InlineItem::Kind::Text) {
while (m_currentInlines.first().text.startsWith(QLatin1Char(' '))) {
m_currentInlines.first().text.remove(0, 1);
}
}
}
if (m_currentInlines.isEmpty())
return;
@@ -348,16 +458,18 @@ private:
else if (a == QLatin1String("left")) ctx.align = TextAlign::Left;
}
const bool isBlock = isBlockTag(tag);
// Apply CSS declarations from cascaded style attribute
if (el.hasAttribute(QStringLiteral("style"))) {
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
applyCssDeclarations(decls, ctx, box, m_params);
applyCssDeclarations(decls, ctx, box, m_params, isBlock);
}
const bool isBlock = isBlockTag(tag);
const bool hasBox = (box.marginTop > 0 || box.marginBottom > 0 || box.marginLeft > 0 || box.marginRight > 0
|| box.paddingTop > 0 || box.paddingBottom > 0 || box.paddingLeft > 0 || box.paddingRight > 0
|| box.borderLeft > 0 || box.borderTop > 0 || box.borderBottom > 0);
|| box.borderLeft > 0 || box.borderTop > 0 || box.borderBottom > 0 || box.borderRight > 0
|| box.background.isValid());
if (isBlock) {
finishCurrentParagraph();