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

@@ -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();