Files
asteria-qt/src/backend/reading/layout/HtmlBlockParser.h
T
2026-10-25 19:59:23 +01:00

108 lines
3.8 KiB
C++

#pragma once
#include <QColor>
#include <QString>
#include <QStringList>
#include <QVector>
// Intermediate representation between HTML chunks and the Knuth-Plass layout.
// All lengths are resolved to pixels for the LayoutParams used at parse time.
enum class TextAlign { Justify, Left, Center, Right };
enum class TextTransform { None, Uppercase, Lowercase, Capitalize };
struct TextStyle {
QStringList fontFamilies; // preferred families in order (may contain generic names); empty = default
qreal fontSize = 16; // px
int weight = 400; // CSS weight 100..900
bool italic = false;
bool smallCaps = false;
TextTransform transform = TextTransform::None;
qreal letterSpacing = 0; // px
bool underline = false;
bool strikeOut = false;
bool overline = false;
bool link = false;
QColor color; // invalid = theme default
QColor background; // invalid = none
qreal baselineShift = 0; // px, positive = raised (superscript)
bool operator==(const TextStyle &o) const {
return fontFamilies == o.fontFamilies
&& qFuzzyCompare(fontSize, o.fontSize)
&& weight == o.weight
&& italic == o.italic
&& smallCaps == o.smallCaps
&& transform == o.transform
&& qFuzzyCompare(letterSpacing, o.letterSpacing)
&& underline == o.underline
&& strikeOut == o.strikeOut
&& overline == o.overline
&& link == o.link
&& color == o.color
&& background == o.background
&& qFuzzyCompare(baselineShift, o.baselineShift);
}
};
struct InlineItem {
enum class Kind { Text, Image, LineBreak };
Kind kind = Kind::Text;
QString text; // whitespace already collapsed unless preformatted
TextStyle style;
QString imageUrl;
qreal cssWidth = -1; // px, -1 = auto
qreal cssHeight = -1;
qreal cssMaxWidth = -1;
};
struct BoxStyle {
qreal marginTop = 0, marginBottom = 0, marginLeft = 0, marginRight = 0;
qreal paddingTop = 0, paddingBottom = 0, paddingLeft = 0, paddingRight = 0;
qreal borderTop = 0, borderBottom = 0, borderLeft = 0, borderRight = 0;
QColor borderColor; // invalid = theme dim colour
QColor background; // invalid = none
// Boxes with a visible top edge stop margins collapsing through them.
bool blocksTopCollapse() const
{
return paddingTop > 0 || borderTop > 0 || borderLeft > 0 || borderRight > 0 || background.isValid();
}
bool blocksBottomCollapse() const
{
return paddingBottom > 0 || borderBottom > 0 || borderLeft > 0 || borderRight > 0 || background.isValid();
}
};
struct BlockItem {
enum class Kind { ContainerStart, ContainerEnd, Paragraph, Rule };
Kind kind = Kind::Paragraph;
BoxStyle box; // ContainerStart / ContainerEnd (same values on both)
// Paragraph properties
TextAlign align = TextAlign::Justify;
qreal textIndent = 0; // px, first line only
qreal lineHeight = 0; // multiplier of fontSize; 0 = LayoutParams default
qreal fontSize = 16; // block font size (minimum line height basis)
bool preformatted = false;
QString marker; // list marker drawn left of the first line
TextStyle markerStyle;
QVector<InlineItem> inlines;
QColor ruleColor; // Rule only; invalid = theme dim colour
};
struct ParseParams {
qreal baseFontSize = 16; // user preference, px
qreal contentWidth = 600; // px, resolves percentage lengths
};
namespace HtmlBlockParser {
// Parses one standalone HTML chunk (an XHTML fragment whose elements carry
// their cascaded CSS in `style` attributes) into a flat block list.
QVector<BlockItem> parse(const QString &html, const ParseParams &params);
} // namespace HtmlBlockParser