fb2 support
This commit is contained in:
18 files changed
+2352
-54
No files matched your search
@@ -249,6 +249,9 @@ private:
|
||||
if (p.words.isEmpty() && p.kpItems.isEmpty())
|
||||
return;
|
||||
|
||||
m_curY += m_prevMarginBottom;
|
||||
m_prevMarginBottom = 0;
|
||||
|
||||
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) {
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
#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
|
||||
@@ -0,0 +1,29 @@
|
||||
#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
|
||||
@@ -17,6 +17,7 @@ struct StyleContext {
|
||||
qreal textIndent = 0;
|
||||
qreal lineHeight = 0;
|
||||
bool preformatted = false;
|
||||
bool isHeading = false;
|
||||
};
|
||||
|
||||
QString localName(const QDomElement &e)
|
||||
@@ -152,6 +153,28 @@ void applyCssDeclarations(const QVector<Css::Declaration> &decls,
|
||||
auto m = Css::parseLength(val, lenCtx); if (m) box.marginLeft = *m;
|
||||
} 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];
|
||||
}
|
||||
} else if (prop == QLatin1String("padding-top")) {
|
||||
auto p = Css::parseLength(val, lenCtx); if (p) box.paddingTop = *p;
|
||||
} else if (prop == QLatin1String("padding-bottom")) {
|
||||
@@ -160,6 +183,28 @@ 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("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];
|
||||
}
|
||||
} else if (prop == QLatin1String("border")) {
|
||||
auto b = Css::parseBorderSide(val, lenCtx);
|
||||
if (b.width) {
|
||||
@@ -374,30 +419,42 @@ private:
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.8;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 1.0;
|
||||
box.marginBottom = m_params.baseFontSize * 0.5;
|
||||
} else if (tag == QLatin1String("h2")) {
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.5;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 0.8;
|
||||
box.marginBottom = m_params.baseFontSize * 0.4;
|
||||
} else if (tag == QLatin1String("h3")) {
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.3;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 0.6;
|
||||
box.marginBottom = m_params.baseFontSize * 0.3;
|
||||
} else if (tag == QLatin1String("h4") || tag == QLatin1String("h5") || tag == QLatin1String("h6")) {
|
||||
ctx.text.fontSize = m_params.baseFontSize * 1.1;
|
||||
ctx.text.weight = 700;
|
||||
ctx.textIndent = 0;
|
||||
ctx.align = TextAlign::Left;
|
||||
ctx.isHeading = true;
|
||||
box.marginTop = m_params.baseFontSize * 0.5;
|
||||
box.marginBottom = m_params.baseFontSize * 0.25;
|
||||
} else if (tag == QLatin1String("p")) {
|
||||
box.marginTop = m_params.baseFontSize * 0.25;
|
||||
box.marginBottom = m_params.baseFontSize * 0.5;
|
||||
ctx.textIndent = m_params.baseFontSize * 1.25;
|
||||
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")) {
|
||||
box.marginLeft = m_params.baseFontSize * 1.5;
|
||||
box.marginRight = m_params.baseFontSize * 1.0;
|
||||
|
||||
Reference in new issue
Block a user