some refactor
This commit is contained in:
20 files changed
+1204
-948
No files matched your search
@@ -322,17 +322,6 @@ QVector<Declaration> cascade(const StyleSheet &sheet, const QDomElement &element
|
||||
return out;
|
||||
}
|
||||
|
||||
QString serialize(const QVector<Declaration> &declarations)
|
||||
{
|
||||
QString out;
|
||||
for (const Declaration &d : declarations) {
|
||||
if (!out.isEmpty())
|
||||
out += QLatin1Char(';');
|
||||
out += d.property + QLatin1Char(':') + d.value;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::optional<qreal> parseLength(QStringView valueIn, const LengthContext &ctx)
|
||||
{
|
||||
const QString value = valueIn.trimmed().toString().toLower();
|
||||
|
||||
@@ -61,7 +61,6 @@ bool matches(const Selector &selector, const QDomElement &element);
|
||||
// Cascade result for one element, in application order (later wins):
|
||||
// stylesheet normal, inline normal, stylesheet !important, inline !important.
|
||||
QVector<Declaration> cascade(const StyleSheet &sheet, const QDomElement &element);
|
||||
QString serialize(const QVector<Declaration> &declarations);
|
||||
|
||||
// --- Values -----------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -0,0 +1,305 @@
|
||||
#include "DocumentChunkBuilder.h"
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
namespace {
|
||||
|
||||
void countInlines(const QVector<InlineItem> &inlines, int &pChars, int &pWords)
|
||||
{
|
||||
pChars = 0;
|
||||
pWords = 0;
|
||||
for (const auto &inl : inlines) {
|
||||
if (inl.kind == InlineItem::Kind::Text) {
|
||||
pChars += inl.text.size();
|
||||
pWords += inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
|
||||
} else if (inl.kind == InlineItem::Kind::Image) {
|
||||
pChars += 1;
|
||||
pWords += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
DocumentChunkBuilder::DocumentChunkBuilder(QString sectionHref, int targetChars)
|
||||
: m_sectionHref(std::move(sectionHref))
|
||||
, m_targetChars(targetChars)
|
||||
{
|
||||
m_currentChunk.sectionHref = m_sectionHref;
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::checkPendingFlush()
|
||||
{
|
||||
if (m_pendingFlush) {
|
||||
m_pendingFlush = false;
|
||||
flushCurrentChunk();
|
||||
}
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::flushCurrentChunk()
|
||||
{
|
||||
if (m_currentChunk.isEmpty())
|
||||
return;
|
||||
|
||||
// Close outer open containers at chunk boundary
|
||||
for (int j = m_openContainers.size() - 1; j >= 0; --j) {
|
||||
BlockItem endItem;
|
||||
endItem.kind = BlockItem::Kind::ContainerEnd;
|
||||
endItem.box = m_openContainers[j];
|
||||
endItem.box.borderBottom = 0;
|
||||
endItem.box.paddingBottom = 0;
|
||||
endItem.box.marginBottom = 0;
|
||||
m_currentChunk.blocks.append(endItem);
|
||||
}
|
||||
|
||||
m_chunks.append(std::move(m_currentChunk));
|
||||
m_currentChunk = ContentChunk();
|
||||
m_currentChunk.sectionHref = m_sectionHref;
|
||||
m_currentChunk.unnestedContainers = m_openContainers.size();
|
||||
|
||||
// Reopen outer containers at start of next chunk
|
||||
for (int j = 0; j < m_openContainers.size(); ++j) {
|
||||
BlockItem startItem;
|
||||
startItem.kind = BlockItem::Kind::ContainerStart;
|
||||
startItem.box = m_openContainers[j];
|
||||
startItem.box.borderTop = 0;
|
||||
startItem.box.paddingTop = 0;
|
||||
startItem.box.marginTop = 0;
|
||||
m_currentChunk.blocks.append(startItem);
|
||||
}
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::startContainer(const BoxStyle &box)
|
||||
{
|
||||
finishParagraph();
|
||||
checkPendingFlush();
|
||||
|
||||
m_openContainers.append(box);
|
||||
BlockItem start;
|
||||
start.kind = BlockItem::Kind::ContainerStart;
|
||||
start.box = box;
|
||||
m_currentChunk.blocks.append(start);
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::endContainer(const BoxStyle &box)
|
||||
{
|
||||
finishParagraph();
|
||||
|
||||
BoxStyle effectiveBox = box;
|
||||
if (!m_openContainers.isEmpty()) {
|
||||
BoxStyle popped = m_openContainers.takeLast();
|
||||
if (effectiveBox.marginTop == 0 && effectiveBox.marginBottom == 0
|
||||
&& effectiveBox.marginLeft == 0 && effectiveBox.marginRight == 0
|
||||
&& effectiveBox.paddingTop == 0 && effectiveBox.paddingBottom == 0
|
||||
&& effectiveBox.borderLeft == 0 && !effectiveBox.background.isValid()) {
|
||||
effectiveBox = popped;
|
||||
}
|
||||
}
|
||||
|
||||
BlockItem end;
|
||||
end.kind = BlockItem::Kind::ContainerEnd;
|
||||
end.box = effectiveBox;
|
||||
m_currentChunk.blocks.append(end);
|
||||
|
||||
checkPendingFlush();
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::startParagraph(TextAlign align,
|
||||
qreal textIndent,
|
||||
qreal lineHeight,
|
||||
qreal fontSize,
|
||||
bool preformatted,
|
||||
const QString &marker,
|
||||
const TextStyle &markerStyle)
|
||||
{
|
||||
finishParagraph();
|
||||
checkPendingFlush();
|
||||
|
||||
m_hasActiveParagraph = true;
|
||||
m_currentParagraph = BlockItem();
|
||||
m_currentParagraph.kind = BlockItem::Kind::Paragraph;
|
||||
m_currentParagraph.align = align;
|
||||
m_currentParagraph.textIndent = textIndent;
|
||||
m_currentParagraph.lineHeight = lineHeight;
|
||||
m_currentParagraph.fontSize = fontSize;
|
||||
m_currentParagraph.preformatted = preformatted;
|
||||
m_currentParagraph.marker = marker;
|
||||
m_currentParagraph.markerStyle = markerStyle;
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::appendText(const QString &text, const TextStyle &style, bool preformatted)
|
||||
{
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
if (!m_hasActiveParagraph) {
|
||||
startParagraph(TextAlign::Justify, 0, 0, style.fontSize, preformatted);
|
||||
}
|
||||
|
||||
QString processed = text;
|
||||
if (!preformatted && !m_currentParagraph.preformatted) {
|
||||
static const QRegularExpression kSpaces(QStringLiteral("\\s+"));
|
||||
processed.replace(kSpaces, QStringLiteral(" "));
|
||||
if (m_currentParagraph.inlines.isEmpty() && processed == QLatin1String(" "))
|
||||
return;
|
||||
}
|
||||
|
||||
if (processed.isEmpty())
|
||||
return;
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Text;
|
||||
item.text = processed;
|
||||
item.style = style;
|
||||
m_currentParagraph.inlines.append(item);
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::appendImage(const QString &imageUrl, qreal cssWidth, qreal cssHeight, qreal cssMaxWidth, const TextStyle &style)
|
||||
{
|
||||
if (imageUrl.isEmpty())
|
||||
return;
|
||||
|
||||
if (!m_hasActiveParagraph) {
|
||||
startParagraph(TextAlign::Justify, 0, 0, style.fontSize > 0 ? style.fontSize : 16);
|
||||
}
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Image;
|
||||
item.imageUrl = imageUrl;
|
||||
item.cssWidth = cssWidth;
|
||||
item.cssHeight = cssHeight;
|
||||
item.cssMaxWidth = cssMaxWidth;
|
||||
item.style = style;
|
||||
m_currentParagraph.inlines.append(item);
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::appendLineBreak()
|
||||
{
|
||||
if (!m_hasActiveParagraph) {
|
||||
startParagraph();
|
||||
}
|
||||
|
||||
InlineItem br;
|
||||
br.kind = InlineItem::Kind::LineBreak;
|
||||
m_currentParagraph.inlines.append(br);
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::finishParagraph()
|
||||
{
|
||||
if (!m_hasActiveParagraph)
|
||||
return;
|
||||
|
||||
auto &inlines = m_currentParagraph.inlines;
|
||||
|
||||
if (!m_currentParagraph.preformatted) {
|
||||
// Drop trailing line breaks and whitespace
|
||||
while (!inlines.isEmpty()) {
|
||||
const auto &last = inlines.last();
|
||||
if (last.kind == InlineItem::Kind::LineBreak) {
|
||||
inlines.removeLast();
|
||||
} else if (last.kind == InlineItem::Kind::Text && last.text.trimmed().isEmpty()) {
|
||||
inlines.removeLast();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!inlines.isEmpty() && inlines.last().kind == InlineItem::Kind::Text) {
|
||||
while (inlines.last().text.endsWith(QLatin1Char(' '))) {
|
||||
inlines.last().text.chop(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Drop leading whitespace
|
||||
while (!inlines.isEmpty()) {
|
||||
const auto &first = inlines.first();
|
||||
if (first.kind == InlineItem::Kind::Text && first.text.trimmed().isEmpty()) {
|
||||
inlines.removeFirst();
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!inlines.isEmpty() && inlines.first().kind == InlineItem::Kind::Text) {
|
||||
while (inlines.first().text.startsWith(QLatin1Char(' '))) {
|
||||
inlines.first().text.remove(0, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (inlines.isEmpty()) {
|
||||
m_hasActiveParagraph = false;
|
||||
m_currentParagraph = BlockItem();
|
||||
return;
|
||||
}
|
||||
|
||||
int pChars = 0;
|
||||
int pWords = 0;
|
||||
countInlines(inlines, pChars, pWords);
|
||||
|
||||
m_currentChunk.blocks.append(m_currentParagraph);
|
||||
m_currentChunk.charCount += pChars;
|
||||
m_currentChunk.wordCount += pWords;
|
||||
|
||||
m_hasActiveParagraph = false;
|
||||
m_currentParagraph = BlockItem();
|
||||
|
||||
if (m_currentChunk.charCount >= m_targetChars) {
|
||||
m_pendingFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::appendRule(const QColor &ruleColor)
|
||||
{
|
||||
finishParagraph();
|
||||
checkPendingFlush();
|
||||
|
||||
BlockItem r;
|
||||
r.kind = BlockItem::Kind::Rule;
|
||||
r.ruleColor = ruleColor;
|
||||
m_currentChunk.blocks.append(r);
|
||||
m_currentChunk.charCount += 1;
|
||||
|
||||
if (m_currentChunk.charCount >= m_targetChars) {
|
||||
m_pendingFlush = true;
|
||||
}
|
||||
}
|
||||
|
||||
void DocumentChunkBuilder::appendBlock(const BlockItem &block)
|
||||
{
|
||||
finishParagraph();
|
||||
checkPendingFlush();
|
||||
|
||||
if (block.kind == BlockItem::Kind::ContainerStart) {
|
||||
startContainer(block.box);
|
||||
} else if (block.kind == BlockItem::Kind::ContainerEnd) {
|
||||
endContainer(block.box);
|
||||
} else if (block.kind == BlockItem::Kind::Rule) {
|
||||
appendRule(block.ruleColor);
|
||||
} else if (block.kind == BlockItem::Kind::Paragraph) {
|
||||
int pChars = 0;
|
||||
int pWords = 0;
|
||||
countInlines(block.inlines, pChars, pWords);
|
||||
m_currentChunk.blocks.append(block);
|
||||
m_currentChunk.charCount += pChars;
|
||||
m_currentChunk.wordCount += pWords;
|
||||
|
||||
if (m_currentChunk.charCount >= m_targetChars) {
|
||||
m_pendingFlush = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QVector<ContentChunk> DocumentChunkBuilder::build()
|
||||
{
|
||||
finishParagraph();
|
||||
m_pendingFlush = false;
|
||||
|
||||
if (!m_currentChunk.isEmpty()) {
|
||||
m_chunks.append(std::move(m_currentChunk));
|
||||
m_currentChunk = ContentChunk();
|
||||
m_currentChunk.sectionHref = m_sectionHref;
|
||||
}
|
||||
|
||||
return std::move(m_chunks);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include "DocumentIr.h"
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
|
||||
class DocumentChunkBuilder {
|
||||
public:
|
||||
explicit DocumentChunkBuilder(QString sectionHref = {}, int targetChars = 512);
|
||||
|
||||
void startContainer(const BoxStyle &box);
|
||||
void endContainer(const BoxStyle &box = {});
|
||||
|
||||
void startParagraph(TextAlign align = TextAlign::Justify,
|
||||
qreal textIndent = 0,
|
||||
qreal lineHeight = 0,
|
||||
qreal fontSize = 16,
|
||||
bool preformatted = false,
|
||||
const QString &marker = {},
|
||||
const TextStyle &markerStyle = {});
|
||||
|
||||
void appendText(const QString &text, const TextStyle &style, bool preformatted = false);
|
||||
void appendImage(const QString &imageUrl, qreal cssWidth = -1, qreal cssHeight = -1, qreal cssMaxWidth = -1, const TextStyle &style = {});
|
||||
void appendLineBreak();
|
||||
void finishParagraph();
|
||||
|
||||
void appendRule(const QColor &ruleColor = {});
|
||||
void appendBlock(const BlockItem &block);
|
||||
|
||||
QVector<ContentChunk> build();
|
||||
|
||||
private:
|
||||
void checkPendingFlush();
|
||||
void flushCurrentChunk();
|
||||
|
||||
QString m_sectionHref;
|
||||
int m_targetChars = 512;
|
||||
|
||||
QVector<ContentChunk> m_chunks;
|
||||
ContentChunk m_currentChunk;
|
||||
QVector<BoxStyle> m_openContainers;
|
||||
|
||||
bool m_hasActiveParagraph = false;
|
||||
BlockItem m_currentParagraph;
|
||||
bool m_pendingFlush = false;
|
||||
};
|
||||
@@ -0,0 +1,161 @@
|
||||
#pragma once
|
||||
|
||||
#include <QColor>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
|
||||
// Core format-neutral Document Intermediate Representation (IR).
|
||||
// Produced by format readers (EPUB, FB2, TXT, MOBI, PDB) and consumed by ChunkLayoutEngine.
|
||||
|
||||
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;
|
||||
|
||||
bool operator==(const InlineItem &o) const
|
||||
{
|
||||
return kind == o.kind
|
||||
&& text == o.text
|
||||
&& style == o.style
|
||||
&& imageUrl == o.imageUrl
|
||||
&& qFuzzyCompare(cssWidth, o.cssWidth)
|
||||
&& qFuzzyCompare(cssHeight, o.cssHeight)
|
||||
&& qFuzzyCompare(cssMaxWidth, o.cssMaxWidth);
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
bool operator==(const BoxStyle &o) const
|
||||
{
|
||||
return qFuzzyCompare(marginTop, o.marginTop)
|
||||
&& qFuzzyCompare(marginBottom, o.marginBottom)
|
||||
&& qFuzzyCompare(marginLeft, o.marginLeft)
|
||||
&& qFuzzyCompare(marginRight, o.marginRight)
|
||||
&& qFuzzyCompare(paddingTop, o.paddingTop)
|
||||
&& qFuzzyCompare(paddingBottom, o.paddingBottom)
|
||||
&& qFuzzyCompare(paddingLeft, o.paddingLeft)
|
||||
&& qFuzzyCompare(paddingRight, o.paddingRight)
|
||||
&& qFuzzyCompare(borderTop, o.borderTop)
|
||||
&& qFuzzyCompare(borderBottom, o.borderBottom)
|
||||
&& qFuzzyCompare(borderLeft, o.borderLeft)
|
||||
&& qFuzzyCompare(borderRight, o.borderRight)
|
||||
&& borderColor == o.borderColor
|
||||
&& background == o.background;
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
bool operator==(const BlockItem &o) const
|
||||
{
|
||||
return kind == o.kind
|
||||
&& box == o.box
|
||||
&& align == o.align
|
||||
&& qFuzzyCompare(textIndent, o.textIndent)
|
||||
&& qFuzzyCompare(lineHeight, o.lineHeight)
|
||||
&& qFuzzyCompare(fontSize, o.fontSize)
|
||||
&& preformatted == o.preformatted
|
||||
&& marker == o.marker
|
||||
&& markerStyle == o.markerStyle
|
||||
&& inlines == o.inlines
|
||||
&& ruleColor == o.ruleColor;
|
||||
}
|
||||
};
|
||||
|
||||
struct ParseParams {
|
||||
qreal baseFontSize = 16; // user preference, px
|
||||
qreal contentWidth = 600; // px, resolves percentage lengths
|
||||
};
|
||||
|
||||
struct ContentChunk {
|
||||
QString sectionHref;
|
||||
QVector<BlockItem> blocks;
|
||||
int wordCount = 0;
|
||||
int charCount = 0;
|
||||
int unnestedContainers = 0;
|
||||
|
||||
bool isEmpty() const { return blocks.isEmpty(); }
|
||||
|
||||
bool operator==(const ContentChunk &o) const
|
||||
{
|
||||
return sectionHref == o.sectionHref
|
||||
&& blocks == o.blocks
|
||||
&& wordCount == o.wordCount
|
||||
&& charCount == o.charCount
|
||||
&& unnestedContainers == o.unnestedContainers;
|
||||
}
|
||||
};
|
||||
@@ -1,68 +0,0 @@
|
||||
#include "DomChunkPipeline.h"
|
||||
|
||||
#include <functional>
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
namespace DomChunkPipeline {
|
||||
|
||||
QVector<ContentChunk> cascadeAndChunk(QDomElement &root,
|
||||
const Css::StyleSheet &sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString §ionHref,
|
||||
const QString &baseDir,
|
||||
int targetChars)
|
||||
{
|
||||
std::function<void(QDomElement &)> processElement = [&](QDomElement &el) {
|
||||
const auto decls = Css::cascade(sheet, el);
|
||||
if (!decls.isEmpty()) {
|
||||
el.setAttribute(QStringLiteral("style"), Css::serialize(decls));
|
||||
}
|
||||
|
||||
const QString tag = el.tagName().toLower();
|
||||
if (tag == QLatin1String("img") || tag == QLatin1String("image")) {
|
||||
QString srcAttr;
|
||||
if (el.hasAttribute(QStringLiteral("src"))) {
|
||||
srcAttr = QStringLiteral("src");
|
||||
} else if (el.hasAttribute(QStringLiteral("xlink:href"))) {
|
||||
srcAttr = QStringLiteral("xlink:href");
|
||||
} else if (el.hasAttribute(QStringLiteral("l:href"))) {
|
||||
srcAttr = QStringLiteral("l:href");
|
||||
} else if (el.hasAttribute(QStringLiteral("href"))) {
|
||||
srcAttr = QStringLiteral("href");
|
||||
}
|
||||
if (!srcAttr.isEmpty()) {
|
||||
const QString src = el.attribute(srcAttr);
|
||||
const QString resolved = ImageUrl::resolve(scheme, src, sourcePath, baseDir);
|
||||
el.setAttribute(srcAttr, resolved);
|
||||
}
|
||||
}
|
||||
|
||||
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
if (child.isElement()) {
|
||||
QDomElement childEl = child.toElement();
|
||||
processElement(childEl);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
processElement(root);
|
||||
return HtmlBlockParser::parseChunks(root, sectionHref, targetChars);
|
||||
}
|
||||
|
||||
QVector<ContentChunk> cascadeAndChunk(QDomElement &root,
|
||||
const QStringList &stylesheetTexts,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString §ionHref,
|
||||
const QString &baseDir,
|
||||
int targetChars)
|
||||
{
|
||||
Css::StyleSheet combinedSheet;
|
||||
for (const QString &text : stylesheetTexts) {
|
||||
combinedSheet.append(Css::StyleSheet::parse(text));
|
||||
}
|
||||
return cascadeAndChunk(root, combinedSheet, scheme, sourcePath, sectionHref, baseDir, targetChars);
|
||||
}
|
||||
|
||||
} // namespace DomChunkPipeline
|
||||
@@ -1,29 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <QDomElement>
|
||||
#include <QString>
|
||||
#include <QStringList>
|
||||
#include <QVector>
|
||||
|
||||
#include "backend/reading/layout/HtmlBlockParser.h"
|
||||
#include "backend/reading/layout/CssParser.h"
|
||||
|
||||
namespace DomChunkPipeline {
|
||||
|
||||
QVector<ContentChunk> cascadeAndChunk(QDomElement &root,
|
||||
const QStringList &stylesheetTexts,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString §ionHref,
|
||||
const QString &baseDir = {},
|
||||
int targetChars = 512);
|
||||
|
||||
QVector<ContentChunk> cascadeAndChunk(QDomElement &root,
|
||||
const Css::StyleSheet &sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString §ionHref,
|
||||
const QString &baseDir = {},
|
||||
int targetChars = 512);
|
||||
|
||||
} // namespace DomChunkPipeline
|
||||
@@ -1,11 +1,12 @@
|
||||
#include "HtmlBlockParser.h"
|
||||
|
||||
#include <QDomDocument>
|
||||
#include <QRegularExpression>
|
||||
#include <QSet>
|
||||
#include <algorithm>
|
||||
|
||||
#include "CssParser.h"
|
||||
#include "DocumentChunkBuilder.h"
|
||||
#include "backend/formats/HtmlEntities.h"
|
||||
#include "backend/formats/ImageUrl.h"
|
||||
|
||||
namespace HtmlBlockParser {
|
||||
|
||||
@@ -31,7 +32,7 @@ QString localName(const QDomElement &e)
|
||||
|
||||
bool isBlockTag(const QString &tag)
|
||||
{
|
||||
static const QStringList kBlocks = {
|
||||
static const QSet<QString> kBlocks = {
|
||||
QStringLiteral("p"), QStringLiteral("div"),
|
||||
QStringLiteral("h1"), QStringLiteral("h2"),
|
||||
QStringLiteral("h3"), QStringLiteral("h4"),
|
||||
@@ -44,16 +45,75 @@ bool isBlockTag(const QString &tag)
|
||||
QStringLiteral("svg"), QStringLiteral("figcaption"),
|
||||
QStringLiteral("section"), QStringLiteral("article"),
|
||||
QStringLiteral("header"), QStringLiteral("footer"), QStringLiteral("aside"),
|
||||
QStringLiteral("body")
|
||||
QStringLiteral("body"),
|
||||
// FB2 semantic block tags
|
||||
QStringLiteral("title"), QStringLiteral("subtitle"),
|
||||
QStringLiteral("epigraph"), QStringLiteral("poem"),
|
||||
QStringLiteral("stanza"), QStringLiteral("v"),
|
||||
QStringLiteral("text-author"),QStringLiteral("date"),
|
||||
QStringLiteral("empty-line")
|
||||
};
|
||||
return kBlocks.contains(tag);
|
||||
}
|
||||
|
||||
void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
StyleContext &ctx,
|
||||
BoxStyle &box,
|
||||
const ParseParams ¶ms,
|
||||
bool isBlock)
|
||||
void applyFourSides(qreal &top, qreal &right, qreal &bottom, qreal &left,
|
||||
const QString &val, const Css::LengthContext &lenCtx)
|
||||
{
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> v;
|
||||
v.reserve(tokens.size());
|
||||
for (const QString &tok : tokens) {
|
||||
if (auto len = Css::parseLength(tok, lenCtx))
|
||||
v.append(*len);
|
||||
}
|
||||
if (v.size() == 1) {
|
||||
top = right = bottom = left = v[0];
|
||||
} else if (v.size() == 2) {
|
||||
top = bottom = v[0];
|
||||
right = left = v[1];
|
||||
} else if (v.size() == 3) {
|
||||
top = v[0];
|
||||
right = left = v[1];
|
||||
bottom = v[2];
|
||||
} else if (v.size() >= 4) {
|
||||
top = v[0];
|
||||
right = v[1];
|
||||
bottom = v[2];
|
||||
left = v[3];
|
||||
}
|
||||
}
|
||||
|
||||
void applyBorderWidths(BoxStyle &box, const QString &val, const Css::LengthContext &lenCtx)
|
||||
{
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> widths;
|
||||
widths.reserve(tokens.size());
|
||||
for (const QString &tok : tokens) {
|
||||
if (auto w = Css::parseBorderSide(tok, lenCtx).width)
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
void applyDeclarations(const QVector<Css::Declaration> &decls,
|
||||
StyleContext &ctx,
|
||||
BoxStyle &box,
|
||||
const ParseParams ¶ms,
|
||||
bool isBlock)
|
||||
{
|
||||
Css::LengthContext lenCtx;
|
||||
lenCtx.em = ctx.text.fontSize;
|
||||
@@ -61,6 +121,12 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
lenCtx.percentBase = params.contentWidth;
|
||||
lenCtx.absoluteScale = params.baseFontSize / 16.0;
|
||||
|
||||
auto applySide = [&](qreal &w, const QString &v) {
|
||||
auto b = Css::parseBorderSide(v, lenCtx);
|
||||
if (b.width) w = *b.width;
|
||||
if (b.color.isValid()) box.borderColor = b.color;
|
||||
};
|
||||
|
||||
for (const auto &d : decls) {
|
||||
const QString &prop = d.property;
|
||||
const QString &val = d.value;
|
||||
@@ -154,27 +220,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} else if (prop == QLatin1String("margin-right")) {
|
||||
auto m = Css::parseLength(val, lenCtx); if (m) box.marginRight = *m;
|
||||
} else if (prop == QLatin1String("margin")) {
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> vals;
|
||||
for (const QString &tok : tokens) {
|
||||
auto m = Css::parseLength(tok, lenCtx);
|
||||
if (m) vals.append(*m);
|
||||
}
|
||||
if (vals.size() == 1) {
|
||||
box.marginTop = box.marginRight = box.marginBottom = box.marginLeft = vals[0];
|
||||
} else if (vals.size() == 2) {
|
||||
box.marginTop = box.marginBottom = vals[0];
|
||||
box.marginRight = box.marginLeft = vals[1];
|
||||
} else if (vals.size() == 3) {
|
||||
box.marginTop = vals[0];
|
||||
box.marginRight = box.marginLeft = vals[1];
|
||||
box.marginBottom = vals[2];
|
||||
} else if (vals.size() >= 4) {
|
||||
box.marginTop = vals[0];
|
||||
box.marginRight = vals[1];
|
||||
box.marginBottom = vals[2];
|
||||
box.marginLeft = vals[3];
|
||||
}
|
||||
applyFourSides(box.marginTop, box.marginRight, box.marginBottom, box.marginLeft, val, lenCtx);
|
||||
} else if (prop == QLatin1String("padding-top")) {
|
||||
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingTop = *p;
|
||||
} else if (prop == QLatin1String("padding-bottom")) {
|
||||
@@ -184,52 +230,19 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} else if (prop == QLatin1String("padding-right")) {
|
||||
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingRight = *p;
|
||||
} else if (prop == QLatin1String("padding")) {
|
||||
const QStringList tokens = Css::splitValue(val);
|
||||
QVector<qreal> vals;
|
||||
for (const QString &tok : tokens) {
|
||||
auto p = Css::parseLength(tok, lenCtx);
|
||||
if (p) vals.append(*p);
|
||||
}
|
||||
if (vals.size() == 1) {
|
||||
box.paddingTop = box.paddingRight = box.paddingBottom = box.paddingLeft = vals[0];
|
||||
} else if (vals.size() == 2) {
|
||||
box.paddingTop = box.paddingBottom = vals[0];
|
||||
box.paddingRight = box.paddingLeft = vals[1];
|
||||
} else if (vals.size() == 3) {
|
||||
box.paddingTop = vals[0];
|
||||
box.paddingRight = box.paddingLeft = vals[1];
|
||||
box.paddingBottom = vals[2];
|
||||
} else if (vals.size() >= 4) {
|
||||
box.paddingTop = vals[0];
|
||||
box.paddingRight = vals[1];
|
||||
box.paddingBottom = vals[2];
|
||||
box.paddingLeft = vals[3];
|
||||
}
|
||||
applyFourSides(box.paddingTop, box.paddingRight, box.paddingBottom, box.paddingLeft, val, lenCtx);
|
||||
} 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.width) box.borderTop = box.borderRight = box.borderBottom = 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;
|
||||
applySide(box.borderTop, val);
|
||||
} 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;
|
||||
applySide(box.borderRight, val);
|
||||
} 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;
|
||||
applySide(box.borderBottom, val);
|
||||
} 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;
|
||||
applySide(box.borderLeft, val);
|
||||
} 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")) {
|
||||
@@ -239,27 +252,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
} 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];
|
||||
}
|
||||
applyBorderWidths(box, val, lenCtx);
|
||||
} else if (prop == QLatin1String("border-color") || prop.endsWith(QLatin1String("-color"))) {
|
||||
QColor c = Css::parseColor(val);
|
||||
if (c.isValid()) box.borderColor = c;
|
||||
@@ -269,7 +262,7 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
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; }
|
||||
else box.borderTop = box.borderRight = box.borderBottom = box.borderLeft = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -277,128 +270,40 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
|
||||
class Builder {
|
||||
public:
|
||||
Builder(const ParseParams ¶ms) : m_params(params)
|
||||
Builder(DocumentChunkBuilder &chunkBuilder,
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet = nullptr,
|
||||
const QString &scheme = {},
|
||||
const QString &sourcePath = {},
|
||||
const QString &baseDir = {},
|
||||
int headingDepth = 1,
|
||||
const QDomNode &root = QDomNode())
|
||||
: m_chunkBuilder(chunkBuilder)
|
||||
, m_params(params)
|
||||
, m_sheet(sheet)
|
||||
, m_scheme(scheme)
|
||||
, m_sourcePath(sourcePath)
|
||||
, m_baseDir(baseDir)
|
||||
, m_headingDepth(headingDepth)
|
||||
, m_root(root)
|
||||
{
|
||||
StyleContext root;
|
||||
root.text.fontSize = params.baseFontSize;
|
||||
m_stack.append(root);
|
||||
StyleContext rootCtx;
|
||||
rootCtx.text.fontSize = params.baseFontSize;
|
||||
m_stack.append(rootCtx);
|
||||
}
|
||||
|
||||
QVector<BlockItem> build(const QDomElement &root)
|
||||
void build(const QDomElement &root)
|
||||
{
|
||||
walk(root);
|
||||
finishCurrentParagraph();
|
||||
return m_blocks;
|
||||
m_chunkBuilder.finishParagraph();
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
BlockItem p;
|
||||
p.kind = BlockItem::Kind::Paragraph;
|
||||
p.align = m_currentAlign;
|
||||
p.textIndent = m_currentIndent;
|
||||
p.lineHeight = m_currentLineHeight;
|
||||
p.fontSize = m_currentBlockFontSize;
|
||||
p.preformatted = m_currentPreformatted;
|
||||
p.marker = m_currentMarker;
|
||||
p.markerStyle = m_currentMarkerStyle;
|
||||
p.inlines = std::move(m_currentInlines);
|
||||
|
||||
m_blocks.append(p);
|
||||
|
||||
m_currentInlines.clear();
|
||||
m_currentMarker.clear();
|
||||
m_currentIndent = 0;
|
||||
}
|
||||
|
||||
void startParagraph(const StyleContext &ctx, const QString &marker = {}, const TextStyle &markerStyle = {})
|
||||
{
|
||||
finishCurrentParagraph();
|
||||
m_currentAlign = ctx.align;
|
||||
m_currentIndent = ctx.textIndent;
|
||||
m_currentLineHeight = ctx.lineHeight;
|
||||
m_currentBlockFontSize = ctx.text.fontSize;
|
||||
m_currentPreformatted = ctx.preformatted;
|
||||
m_currentMarker = marker;
|
||||
m_currentMarkerStyle = markerStyle;
|
||||
}
|
||||
|
||||
void appendText(const QString &text, const TextStyle &style, bool pre)
|
||||
{
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
if (m_currentInlines.isEmpty()) {
|
||||
startParagraph(m_stack.last());
|
||||
}
|
||||
|
||||
QString processed = text;
|
||||
if (!pre) {
|
||||
// Collapse whitespaces
|
||||
static const QRegularExpression kSpaces(QStringLiteral("\\s+"));
|
||||
processed.replace(kSpaces, QStringLiteral(" "));
|
||||
if (m_currentInlines.isEmpty() && processed == QLatin1String(" "))
|
||||
return;
|
||||
}
|
||||
|
||||
if (processed.isEmpty())
|
||||
return;
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Text;
|
||||
item.text = processed;
|
||||
item.style = style;
|
||||
m_currentInlines.append(item);
|
||||
}
|
||||
|
||||
void walk(const QDomNode &node)
|
||||
{
|
||||
if (node.isText()) {
|
||||
const StyleContext &ctx = m_stack.last();
|
||||
appendText(node.nodeValue(), ctx.text, ctx.preformatted);
|
||||
m_chunkBuilder.appendText(node.nodeValue(), ctx.text, ctx.preformatted);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -411,10 +316,28 @@ private:
|
||||
if (tag == QLatin1String("head") || tag == QLatin1String("style") || tag == QLatin1String("script"))
|
||||
return;
|
||||
|
||||
// Skip nested sections in FB2 so they can be processed independently
|
||||
if (node != m_root && tag == QLatin1String("section") && m_scheme == QLatin1String("fb2"))
|
||||
return;
|
||||
|
||||
// Child handling within heading/title
|
||||
if (m_stack.last().isHeading) {
|
||||
if (tag == QLatin1String("p") || tag == QLatin1String("empty-line")) {
|
||||
if (!m_firstTitleLine) {
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
}
|
||||
m_firstTitleLine = false;
|
||||
for (QDomNode child = el.firstChild(); !child.isNull(); child = child.nextSibling()) {
|
||||
walk(child);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
StyleContext ctx = m_stack.last();
|
||||
BoxStyle box;
|
||||
|
||||
// Default HTML semantics
|
||||
// Default HTML & FB2 semantics
|
||||
if (tag == QLatin1String("h1")) {
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.8;
|
||||
ctx.text.weight = 700;
|
||||
@@ -447,15 +370,30 @@ private:
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 0.5;
|
||||
box.marginBottom = m_params.baseFontSize * 0.25;
|
||||
} else if (tag == QLatin1String("title")) {
|
||||
const int hLevel = qBound(1, m_headingDepth, 6);
|
||||
const qreal hScale = (hLevel == 1) ? 1.8 : (hLevel == 2 ? 1.5 : (hLevel == 3 ? 1.3 : 1.1));
|
||||
const qreal hMarginTop = (hLevel == 1) ? 1.0 : (hLevel == 2 ? 0.8 : (hLevel == 3 ? 0.6 : 0.5));
|
||||
ctx.text.fontSize = m_params.baseFontSize * hScale;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * hMarginTop;
|
||||
box.marginBottom = m_params.baseFontSize * (hMarginTop * 0.5);
|
||||
m_firstTitleLine = true;
|
||||
} else if (tag == QLatin1String("subtitle")) {
|
||||
ctx.align = TextAlign::Center;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("p")) {
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
if (!ctx.isHeading) {
|
||||
ctx.textIndent = m_params.baseFontSize * 2.0;
|
||||
} else {
|
||||
ctx.textIndent = 0;
|
||||
}
|
||||
} else if (tag == QLatin1String("blockquote")) {
|
||||
ctx.textIndent = ctx.isHeading ? 0 : (m_params.baseFontSize * 2.0);
|
||||
} else if (tag == QLatin1String("blockquote") || tag == QLatin1String("epigraph")
|
||||
|| (tag == QLatin1String("cite") && m_scheme == QLatin1String("fb2"))) {
|
||||
box.marginLeft = m_params.baseFontSize * 1.5;
|
||||
box.marginRight = m_params.baseFontSize * 1.0;
|
||||
box.marginTop = m_params.baseFontSize * 0.5;
|
||||
@@ -463,6 +401,33 @@ private:
|
||||
box.borderLeft = 3;
|
||||
box.paddingLeft = m_params.baseFontSize * 0.75;
|
||||
ctx.text.italic = true;
|
||||
} else if (tag == QLatin1String("poem")) {
|
||||
box.marginLeft = m_params.baseFontSize * 2.0;
|
||||
box.marginRight = m_params.baseFontSize * 2.0;
|
||||
} else if (tag == QLatin1String("stanza")) {
|
||||
box.marginTop = m_params.baseFontSize * 1.0;
|
||||
box.marginBottom = m_params.baseFontSize * 1.0;
|
||||
} else if (tag == QLatin1String("v")) {
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("text-author") || tag == QLatin1String("date")) {
|
||||
ctx.align = TextAlign::Right;
|
||||
ctx.text.italic = true;
|
||||
ctx.textIndent = 0;
|
||||
box.marginTop = 0;
|
||||
box.marginBottom = 0;
|
||||
} else if (tag == QLatin1String("empty-line")) {
|
||||
m_chunkBuilder.finishParagraph();
|
||||
BoxStyle emptyBox;
|
||||
emptyBox.marginTop = m_params.baseFontSize * 0.5;
|
||||
emptyBox.marginBottom = m_params.baseFontSize * 0.5;
|
||||
m_chunkBuilder.startContainer(emptyBox);
|
||||
m_chunkBuilder.startParagraph(TextAlign::Left, 0, 0, m_params.baseFontSize);
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
m_chunkBuilder.endContainer(emptyBox);
|
||||
return;
|
||||
} else if (tag == QLatin1String("pre")) {
|
||||
ctx.text.fontFamilies = {QStringLiteral("monospace")};
|
||||
ctx.preformatted = true;
|
||||
@@ -470,11 +435,12 @@ private:
|
||||
box.marginBottom = m_params.baseFontSize * 0.5;
|
||||
} else if (tag == QLatin1String("b") || tag == QLatin1String("strong")) {
|
||||
ctx.text.weight = 700;
|
||||
} else if (tag == QLatin1String("i") || tag == QLatin1String("em") || tag == QLatin1String("cite")) {
|
||||
} else if (tag == QLatin1String("i") || tag == QLatin1String("em") || tag == QLatin1String("emphasis")
|
||||
|| (tag == QLatin1String("cite") && m_scheme != QLatin1String("fb2"))) {
|
||||
ctx.text.italic = true;
|
||||
} else if (tag == QLatin1String("u") || tag == QLatin1String("ins")) {
|
||||
ctx.text.underline = true;
|
||||
} else if (tag == QLatin1String("s") || tag == QLatin1String("strike") || tag == QLatin1String("del")) {
|
||||
} else if (tag == QLatin1String("s") || tag == QLatin1String("strike") || tag == QLatin1String("del") || tag == QLatin1String("strikethrough")) {
|
||||
ctx.text.strikeOut = true;
|
||||
} else if (tag == QLatin1String("sub")) {
|
||||
ctx.text.fontSize *= 0.75;
|
||||
@@ -491,15 +457,10 @@ private:
|
||||
ctx.text.underline = true;
|
||||
ctx.text.link = true;
|
||||
} else if (tag == QLatin1String("hr")) {
|
||||
finishCurrentParagraph();
|
||||
BlockItem r;
|
||||
r.kind = BlockItem::Kind::Rule;
|
||||
m_blocks.append(r);
|
||||
m_chunkBuilder.appendRule();
|
||||
return;
|
||||
} else if (tag == QLatin1String("br")) {
|
||||
InlineItem br;
|
||||
br.kind = InlineItem::Kind::LineBreak;
|
||||
m_currentInlines.append(br);
|
||||
m_chunkBuilder.appendLineBreak();
|
||||
return;
|
||||
} else if (tag == QLatin1String("img") || tag == QLatin1String("image")) {
|
||||
handleImage(el, ctx);
|
||||
@@ -517,10 +478,14 @@ private:
|
||||
|
||||
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, isBlock);
|
||||
QVector<Css::Declaration> decls;
|
||||
if (m_sheet && !m_sheet->rules.isEmpty()) {
|
||||
decls = Css::cascade(*m_sheet, el);
|
||||
} else if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
}
|
||||
if (!decls.isEmpty()) {
|
||||
applyDeclarations(decls, ctx, box, m_params, isBlock);
|
||||
}
|
||||
|
||||
const bool hasBox = (box.marginTop > 0 || box.marginBottom > 0 || box.marginLeft > 0 || box.marginRight > 0
|
||||
@@ -529,12 +494,9 @@ private:
|
||||
|| box.background.isValid());
|
||||
|
||||
if (isBlock) {
|
||||
finishCurrentParagraph();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
if (hasBox) {
|
||||
BlockItem start;
|
||||
start.kind = BlockItem::Kind::ContainerStart;
|
||||
start.box = box;
|
||||
m_blocks.append(start);
|
||||
m_chunkBuilder.startContainer(box);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -552,7 +514,7 @@ private:
|
||||
}
|
||||
|
||||
if (isBlock) {
|
||||
startParagraph(ctx, marker, markerStyle);
|
||||
m_chunkBuilder.startParagraph(ctx.align, ctx.textIndent, ctx.lineHeight, ctx.text.fontSize, ctx.preformatted, marker, markerStyle);
|
||||
}
|
||||
|
||||
m_stack.append(ctx);
|
||||
@@ -562,12 +524,9 @@ private:
|
||||
m_stack.removeLast();
|
||||
|
||||
if (isBlock) {
|
||||
finishCurrentParagraph();
|
||||
m_chunkBuilder.finishParagraph();
|
||||
if (hasBox) {
|
||||
BlockItem end;
|
||||
end.kind = BlockItem::Kind::ContainerEnd;
|
||||
end.box = box;
|
||||
m_blocks.append(end);
|
||||
m_chunkBuilder.endContainer(box);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -579,12 +538,15 @@ private:
|
||||
src = el.attribute(QStringLiteral("xlink:href"));
|
||||
if (src.isEmpty())
|
||||
src = el.attribute(QStringLiteral("href"));
|
||||
if (src.isEmpty())
|
||||
src = el.attribute(QStringLiteral("l:href"));
|
||||
if (src.isEmpty())
|
||||
return;
|
||||
|
||||
InlineItem item;
|
||||
item.kind = InlineItem::Kind::Image;
|
||||
item.imageUrl = src;
|
||||
QString resolved = src;
|
||||
if (!m_scheme.isEmpty()) {
|
||||
resolved = ImageUrl::resolve(m_scheme, src, m_sourcePath, m_baseDir);
|
||||
}
|
||||
|
||||
Css::LengthContext lenCtx;
|
||||
lenCtx.em = ctx.text.fontSize;
|
||||
@@ -592,64 +554,53 @@ private:
|
||||
lenCtx.percentBase = m_params.contentWidth;
|
||||
lenCtx.absoluteScale = m_params.baseFontSize / 16.0;
|
||||
|
||||
qreal cssW = -1;
|
||||
qreal cssH = -1;
|
||||
qreal cssMaxW = -1;
|
||||
|
||||
if (el.hasAttribute(QStringLiteral("width"))) {
|
||||
auto w = Css::parseLength(el.attribute(QStringLiteral("width")), lenCtx);
|
||||
if (w) item.cssWidth = *w;
|
||||
if (w) cssW = *w;
|
||||
}
|
||||
if (el.hasAttribute(QStringLiteral("height"))) {
|
||||
auto h = Css::parseLength(el.attribute(QStringLiteral("height")), lenCtx);
|
||||
if (h) item.cssHeight = *h;
|
||||
if (h) cssH = *h;
|
||||
}
|
||||
|
||||
if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
const auto decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
for (const auto &d : decls) {
|
||||
if (d.property == QLatin1String("width")) {
|
||||
auto w = Css::parseLength(d.value, lenCtx); if (w) item.cssWidth = *w;
|
||||
} else if (d.property == QLatin1String("height")) {
|
||||
auto h = Css::parseLength(d.value, lenCtx); if (h) item.cssHeight = *h;
|
||||
} else if (d.property == QLatin1String("max-width")) {
|
||||
auto mw = Css::parseLength(d.value, lenCtx); if (mw) item.cssMaxWidth = *mw;
|
||||
}
|
||||
QVector<Css::Declaration> decls;
|
||||
if (m_sheet && !m_sheet->rules.isEmpty()) {
|
||||
decls = Css::cascade(*m_sheet, el);
|
||||
} else if (el.hasAttribute(QStringLiteral("style"))) {
|
||||
decls = Css::parseDeclarations(el.attribute(QStringLiteral("style")));
|
||||
}
|
||||
|
||||
for (const auto &d : decls) {
|
||||
if (d.property == QLatin1String("width")) {
|
||||
auto w = Css::parseLength(d.value, lenCtx); if (w) cssW = *w;
|
||||
} else if (d.property == QLatin1String("height")) {
|
||||
auto h = Css::parseLength(d.value, lenCtx); if (h) cssH = *h;
|
||||
} else if (d.property == QLatin1String("max-width")) {
|
||||
auto mw = Css::parseLength(d.value, lenCtx); if (mw) cssMaxW = *mw;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_currentInlines.isEmpty()) {
|
||||
startParagraph(ctx);
|
||||
}
|
||||
m_currentInlines.append(item);
|
||||
m_chunkBuilder.appendImage(resolved, cssW, cssH, cssMaxW, ctx.text);
|
||||
}
|
||||
|
||||
DocumentChunkBuilder &m_chunkBuilder;
|
||||
const ParseParams &m_params;
|
||||
const Css::StyleSheet *m_sheet = nullptr;
|
||||
QString m_scheme;
|
||||
QString m_sourcePath;
|
||||
QString m_baseDir;
|
||||
int m_headingDepth = 1;
|
||||
QDomNode m_root;
|
||||
QVector<StyleContext> m_stack;
|
||||
QVector<BlockItem> m_blocks;
|
||||
|
||||
QVector<InlineItem> m_currentInlines;
|
||||
TextAlign m_currentAlign = TextAlign::Justify;
|
||||
qreal m_currentIndent = 0;
|
||||
qreal m_currentLineHeight = 0;
|
||||
qreal m_currentBlockFontSize = 16;
|
||||
bool m_currentPreformatted = false;
|
||||
QString m_currentMarker;
|
||||
TextStyle m_currentMarkerStyle;
|
||||
bool m_firstTitleLine = false;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<BlockItem> parse(const QDomElement &root, const ParseParams ¶ms)
|
||||
QDomDocument parseHtmlToDoc(const QString &html)
|
||||
{
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
Builder builder(params);
|
||||
return builder.build(root);
|
||||
}
|
||||
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
{
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QString wrapped = html.trimmed();
|
||||
if (!wrapped.startsWith(QLatin1String("<")))
|
||||
wrapped = QStringLiteral("<p>") + wrapped + QStringLiteral("</p>");
|
||||
@@ -657,12 +608,40 @@ QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
wrapped = QStringLiteral("<body>") + wrapped + QStringLiteral("</body>");
|
||||
|
||||
const QString safeXml = HtmlEntities::toXmlSafe(wrapped);
|
||||
|
||||
QDomDocument doc;
|
||||
QString errorMsg;
|
||||
int errorLine = 0, errorColumn = 0;
|
||||
if (!doc.setContent(safeXml, false, &errorMsg, &errorLine, &errorColumn)) {
|
||||
// Fallback: return as a simple paragraph
|
||||
doc.setContent(safeXml);
|
||||
return doc;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
QVector<BlockItem> parse(const QDomElement &root,
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString &baseDir,
|
||||
int headingDepth)
|
||||
{
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
DocumentChunkBuilder chunkBuilder({}, 1000000000);
|
||||
Builder builder(chunkBuilder, params, sheet, scheme, sourcePath, baseDir, headingDepth, root);
|
||||
builder.build(root);
|
||||
auto chunks = chunkBuilder.build();
|
||||
if (chunks.isEmpty())
|
||||
return {};
|
||||
return chunks.first().blocks;
|
||||
}
|
||||
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms)
|
||||
{
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QDomDocument doc = parseHtmlToDoc(html);
|
||||
if (doc.documentElement().isNull()) {
|
||||
BlockItem b;
|
||||
b.kind = BlockItem::Kind::Paragraph;
|
||||
InlineItem textItem;
|
||||
@@ -682,105 +661,30 @@ QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
||||
if (blocks.isEmpty())
|
||||
return {};
|
||||
|
||||
QVector<ContentChunk> chunks;
|
||||
ContentChunk current;
|
||||
current.sectionHref = sectionHref;
|
||||
|
||||
QVector<BoxStyle> openContainers;
|
||||
|
||||
auto flushCurrent = [&]() {
|
||||
if (current.isEmpty())
|
||||
return;
|
||||
|
||||
// Close outer containers at chunk boundary
|
||||
for (int j = openContainers.size() - 1; j >= 0; --j) {
|
||||
BlockItem endItem;
|
||||
endItem.kind = BlockItem::Kind::ContainerEnd;
|
||||
endItem.box = openContainers[j];
|
||||
endItem.box.borderBottom = 0;
|
||||
endItem.box.paddingBottom = 0;
|
||||
endItem.box.marginBottom = 0;
|
||||
current.blocks.append(endItem);
|
||||
}
|
||||
|
||||
chunks.append(std::move(current));
|
||||
current = ContentChunk();
|
||||
current.sectionHref = sectionHref;
|
||||
current.unnestedContainers = openContainers.size();
|
||||
|
||||
// Reopen outer containers at start of next chunk
|
||||
for (int j = 0; j < openContainers.size(); ++j) {
|
||||
BlockItem startItem;
|
||||
startItem.kind = BlockItem::Kind::ContainerStart;
|
||||
startItem.box = openContainers[j];
|
||||
startItem.box.borderTop = 0;
|
||||
startItem.box.paddingTop = 0;
|
||||
startItem.box.marginTop = 0;
|
||||
current.blocks.append(startItem);
|
||||
}
|
||||
};
|
||||
|
||||
for (int i = 0; i < blocks.size(); ++i) {
|
||||
const auto &b = blocks[i];
|
||||
if (b.kind == BlockItem::Kind::ContainerStart) {
|
||||
openContainers.append(b.box);
|
||||
current.blocks.append(b);
|
||||
} else if (b.kind == BlockItem::Kind::ContainerEnd) {
|
||||
if (!openContainers.isEmpty())
|
||||
openContainers.removeLast();
|
||||
current.blocks.append(b);
|
||||
} else if (b.kind == BlockItem::Kind::Rule) {
|
||||
current.blocks.append(b);
|
||||
current.charCount += 1;
|
||||
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
|
||||
flushCurrent();
|
||||
}
|
||||
} else if (b.kind == BlockItem::Kind::Paragraph) {
|
||||
int pChars = 0;
|
||||
int pWords = 0;
|
||||
for (const auto &inl : b.inlines) {
|
||||
if (inl.kind == InlineItem::Kind::Text) {
|
||||
pChars += inl.text.size();
|
||||
const QStringList words = inl.text.split(QLatin1Char(' '), Qt::SkipEmptyParts);
|
||||
pWords += words.size();
|
||||
} else if (inl.kind == InlineItem::Kind::Image) {
|
||||
pChars += 1;
|
||||
pWords += 1;
|
||||
}
|
||||
}
|
||||
|
||||
current.blocks.append(b);
|
||||
current.charCount += pChars;
|
||||
current.wordCount += pWords;
|
||||
|
||||
// Consume immediately following ContainerEnd that closes this paragraph's leaf box
|
||||
if (i + 1 < blocks.size() && blocks[i + 1].kind == BlockItem::Kind::ContainerEnd) {
|
||||
if (!openContainers.isEmpty())
|
||||
openContainers.removeLast();
|
||||
current.blocks.append(blocks[i + 1]);
|
||||
++i;
|
||||
}
|
||||
|
||||
if (current.charCount >= targetChars && i + 1 < blocks.size()) {
|
||||
flushCurrent();
|
||||
}
|
||||
}
|
||||
DocumentChunkBuilder builder(sectionHref, targetChars);
|
||||
for (const auto &b : blocks) {
|
||||
builder.appendBlock(b);
|
||||
}
|
||||
|
||||
if (!current.isEmpty()) {
|
||||
chunks.append(std::move(current));
|
||||
}
|
||||
|
||||
return chunks;
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QDomElement &root,
|
||||
const QString §ionHref,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
const ParseParams ¶ms,
|
||||
const Css::StyleSheet *sheet,
|
||||
const QString &scheme,
|
||||
const QString &sourcePath,
|
||||
const QString &baseDir,
|
||||
int headingDepth)
|
||||
{
|
||||
const auto blocks = parse(root, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
if (root.isNull())
|
||||
return {};
|
||||
|
||||
DocumentChunkBuilder chunkBuilder(sectionHref, targetChars);
|
||||
Builder builder(chunkBuilder, params, sheet, scheme, sourcePath, baseDir, headingDepth, root);
|
||||
builder.build(root);
|
||||
return chunkBuilder.build();
|
||||
}
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QString &html,
|
||||
@@ -788,9 +692,26 @@ QVector<ContentChunk> parseChunks(const QString &html,
|
||||
int targetChars,
|
||||
const ParseParams ¶ms)
|
||||
{
|
||||
const auto blocks = parse(html, params);
|
||||
return partitionBlocks(blocks, sectionHref, targetChars);
|
||||
if (html.trimmed().isEmpty())
|
||||
return {};
|
||||
|
||||
QDomDocument doc = parseHtmlToDoc(html);
|
||||
if (doc.documentElement().isNull()) {
|
||||
ContentChunk chunk;
|
||||
chunk.sectionHref = sectionHref;
|
||||
BlockItem b;
|
||||
b.kind = BlockItem::Kind::Paragraph;
|
||||
InlineItem textItem;
|
||||
textItem.kind = InlineItem::Kind::Text;
|
||||
textItem.text = html;
|
||||
b.inlines.append(textItem);
|
||||
chunk.blocks.append(b);
|
||||
chunk.charCount = html.size();
|
||||
chunk.wordCount = html.split(QLatin1Char(' '), Qt::SkipEmptyParts).size();
|
||||
return {chunk};
|
||||
}
|
||||
|
||||
return parseChunks(doc.documentElement(), sectionHref, targetChars, params);
|
||||
}
|
||||
|
||||
} // namespace HtmlBlockParser
|
||||
|
||||
@@ -1,173 +1,21 @@
|
||||
#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;
|
||||
|
||||
bool operator==(const InlineItem &o) const
|
||||
{
|
||||
return kind == o.kind
|
||||
&& text == o.text
|
||||
&& style == o.style
|
||||
&& imageUrl == o.imageUrl
|
||||
&& qFuzzyCompare(cssWidth, o.cssWidth)
|
||||
&& qFuzzyCompare(cssHeight, o.cssHeight)
|
||||
&& qFuzzyCompare(cssMaxWidth, o.cssMaxWidth);
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
bool operator==(const BoxStyle &o) const
|
||||
{
|
||||
return qFuzzyCompare(marginTop, o.marginTop)
|
||||
&& qFuzzyCompare(marginBottom, o.marginBottom)
|
||||
&& qFuzzyCompare(marginLeft, o.marginLeft)
|
||||
&& qFuzzyCompare(marginRight, o.marginRight)
|
||||
&& qFuzzyCompare(paddingTop, o.paddingTop)
|
||||
&& qFuzzyCompare(paddingBottom, o.paddingBottom)
|
||||
&& qFuzzyCompare(paddingLeft, o.paddingLeft)
|
||||
&& qFuzzyCompare(paddingRight, o.paddingRight)
|
||||
&& qFuzzyCompare(borderTop, o.borderTop)
|
||||
&& qFuzzyCompare(borderBottom, o.borderBottom)
|
||||
&& qFuzzyCompare(borderLeft, o.borderLeft)
|
||||
&& qFuzzyCompare(borderRight, o.borderRight)
|
||||
&& borderColor == o.borderColor
|
||||
&& background == o.background;
|
||||
}
|
||||
};
|
||||
|
||||
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
|
||||
|
||||
bool operator==(const BlockItem &o) const
|
||||
{
|
||||
return kind == o.kind
|
||||
&& box == o.box
|
||||
&& align == o.align
|
||||
&& qFuzzyCompare(textIndent, o.textIndent)
|
||||
&& qFuzzyCompare(lineHeight, o.lineHeight)
|
||||
&& qFuzzyCompare(fontSize, o.fontSize)
|
||||
&& preformatted == o.preformatted
|
||||
&& marker == o.marker
|
||||
&& markerStyle == o.markerStyle
|
||||
&& inlines == o.inlines
|
||||
&& ruleColor == o.ruleColor;
|
||||
}
|
||||
};
|
||||
|
||||
struct ParseParams {
|
||||
qreal baseFontSize = 16; // user preference, px
|
||||
qreal contentWidth = 600; // px, resolves percentage lengths
|
||||
};
|
||||
|
||||
struct ContentChunk {
|
||||
QString sectionHref;
|
||||
QVector<BlockItem> blocks;
|
||||
int wordCount = 0;
|
||||
int charCount = 0;
|
||||
int unnestedContainers = 0;
|
||||
|
||||
bool isEmpty() const { return blocks.isEmpty(); }
|
||||
|
||||
bool operator==(const ContentChunk &o) const
|
||||
{
|
||||
return sectionHref == o.sectionHref
|
||||
&& blocks == o.blocks
|
||||
&& wordCount == o.wordCount
|
||||
&& charCount == o.charCount
|
||||
&& unnestedContainers == o.unnestedContainers;
|
||||
}
|
||||
};
|
||||
#include "DocumentIr.h"
|
||||
#include "CssParser.h"
|
||||
|
||||
class QDomElement;
|
||||
|
||||
namespace HtmlBlockParser {
|
||||
|
||||
// Parses HTML or DOM element (whose elements carry cascaded CSS in `style` attributes)
|
||||
// into a flat block list.
|
||||
// Parses HTML or DOM element into a flat block list.
|
||||
QVector<BlockItem> parse(const QString &html, const ParseParams ¶ms = {});
|
||||
QVector<BlockItem> parse(const QDomElement &root, const ParseParams ¶ms = {});
|
||||
QVector<BlockItem> parse(const QDomElement &root,
|
||||
const ParseParams ¶ms = {},
|
||||
const Css::StyleSheet *sheet = nullptr,
|
||||
const QString &scheme = {},
|
||||
const QString &sourcePath = {},
|
||||
const QString &baseDir = {},
|
||||
int headingDepth = 1);
|
||||
|
||||
// Partitions a block list into granular ContentChunks (~targetChars per chunk).
|
||||
QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
||||
@@ -178,7 +26,12 @@ QVector<ContentChunk> partitionBlocks(const QVector<BlockItem> &blocks,
|
||||
QVector<ContentChunk> parseChunks(const QDomElement &root,
|
||||
const QString §ionHref = {},
|
||||
int targetChars = 512,
|
||||
const ParseParams ¶ms = {});
|
||||
const ParseParams ¶ms = {},
|
||||
const Css::StyleSheet *sheet = nullptr,
|
||||
const QString &scheme = {},
|
||||
const QString &sourcePath = {},
|
||||
const QString &baseDir = {},
|
||||
int headingDepth = 1);
|
||||
|
||||
QVector<ContentChunk> parseChunks(const QString &html,
|
||||
const QString §ionHref = {},
|
||||
@@ -186,4 +39,3 @@ QVector<ContentChunk> parseChunks(const QString &html,
|
||||
const ParseParams ¶ms = {});
|
||||
|
||||
} // namespace HtmlBlockParser
|
||||
|
||||
Reference in new issue
Block a user