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

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