-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathqbasichtmlexporter.cpp
More file actions
368 lines (338 loc) · 11.1 KB
/
Copy pathqbasichtmlexporter.cpp
File metadata and controls
368 lines (338 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#include "qbasichtmlexporter.h"
#include <QDebug>
#include <QTextDocument>
#include <QTextList>
#include <QtMath>
#include <QTextTable>
#include <QTextTableFormat>
#include <QDebug>
static QTextFormat formatDifference(const QTextFormat &from, const QTextFormat &to)
{
QTextFormat diff = to;
const QMap<int, QVariant> props = to.properties();
for (QMap<int, QVariant>::ConstIterator it = props.begin(), end = props.end();
it != end; ++it)
if (it.value() == from.property(it.key()))
diff.clearProperty(it.key());
return diff;
}
QBasicHtmlExporter::QBasicHtmlExporter(QTextDocument *_doc)
{
doc = _doc;
const QFont defaultFont = doc->defaultFont();
defaultCharFormat.setFont(defaultFont);
// don't export those for the default font since we cannot turn them off with CSS
defaultCharFormat.clearProperty(QTextFormat::FontUnderline);
defaultCharFormat.clearProperty(QTextFormat::FontOverline);
defaultCharFormat.clearProperty(QTextFormat::FontStrikeOut);
defaultCharFormat.clearProperty(QTextFormat::TextUnderlineStyle);
}
QString QBasicHtmlExporter::toHtml()
{
html = QLatin1String("");
emitFrame(doc->rootFrame()->begin());
// Remove newlines at beginning
html.remove(QRegExp("^[\r\n]+"));
return html;
}
QBasicHtmlExporter::Heading QBasicHtmlExporter::headingType(QString name)
{
if ( name == "xx-large" ) return h1;
if ( name == "x-large" ) return h2;
if ( name == "large" ) return h3;
if ( name == "medium" ) return h4;
if ( name == "small" ) return h5;
return paragraph;
}
QString QBasicHtmlExporter::headingStr(QBasicHtmlExporter::Heading heading)
{
switch (heading) {
case h1:
return "h1";
case h2:
return "h2";
case h3:
return "h3";
case h4:
return "h4";
case h5:
return "h5";
default:
return "p";
}
}
QString QBasicHtmlExporter::getTagName(const QTextCharFormat &format)
{
Heading cur_heading = paragraph;
if (format.hasProperty(QTextFormat::FontSizeAdjustment)) {
static const char sizeNameData[] =
"small" "\0"
"medium" "\0"
"xx-large" ;
static const quint8 sizeNameOffsets[] = {
0, // "small"
sizeof("small"), // "medium"
sizeof("small") + sizeof("medium") + 3, // "large" )
sizeof("small") + sizeof("medium") + 1, // "x-large" )> compressed into "xx-large"
sizeof("small") + sizeof("medium"), // "xx-large" )
};
const char *name = nullptr;
const int idx = format.intProperty(QTextFormat::FontSizeAdjustment) + 1;
if (idx >= 0 && idx <= 4) {
name = sizeNameData + sizeNameOffsets[idx];
}
if (name) {
cur_heading = headingType(name);
}
}
return headingStr( cur_heading );
}
void QBasicHtmlExporter::emitFrame(const QTextFrame::Iterator &frameIt)
{
if (!frameIt.atEnd()) {
QTextFrame::Iterator next = frameIt;
++next;
if (next.atEnd()
&& frameIt.currentFrame() == nullptr
&& frameIt.parentFrame() != doc->rootFrame()
&& frameIt.currentBlock().begin().atEnd())
return;
}
for (QTextFrame::Iterator it = frameIt;
!it.atEnd(); ++it) {
if (QTextFrame *f = it.currentFrame()) {
if (QTextTable *table = qobject_cast<QTextTable *>(f)) {
emitTable(table);
} else {
emitTextFrame(f);
}
} else if (it.currentBlock().isValid()) {
emitBlock(it.currentBlock());
}
}
}
void QBasicHtmlExporter::emitTable(const QTextTable *table)
{
QTextTableFormat format = table->format();
html += QLatin1String("\n<table>");
const int rows = table->rows();
const int columns = table->columns();
QVector<QTextLength> columnWidths = format.columnWidthConstraints();
if (columnWidths.isEmpty()) {
columnWidths.resize(columns);
columnWidths.fill(QTextLength());
}
Q_ASSERT(columnWidths.count() == columns);
QVarLengthArray<bool> widthEmittedForColumn(columns);
for (int i = 0; i < columns; ++i)
widthEmittedForColumn[i] = false;
const int headerRowCount = qMin(format.headerRowCount(), rows);
if (headerRowCount > 0)
html += QLatin1String("<thead>");
for (int row = 0; row < rows; ++row) {
html += QLatin1String("\n<tr>");
for (int col = 0; col < columns; ++col) {
const QTextTableCell cell = table->cellAt(row, col);
// for col/rowspans
if (cell.row() != row)
continue;
if (cell.column() != col)
continue;
html += QLatin1String("\n<td>");
emitFrame(cell.begin());
html += QLatin1String("</td>");
}
html += QLatin1String("</tr>");
if (headerRowCount > 0 && row == headerRowCount - 1)
html += QLatin1String("</thead>");
}
html += QLatin1String("</table>");
}
void QBasicHtmlExporter::emitTextFrame(const QTextFrame *f)
{
html += QLatin1String("\n<table>");
QTextFrameFormat format = f->frameFormat();
html += QLatin1String("\n<tr>\n<td\">");
emitFrame(f->begin());
html += QLatin1String("</td></tr></table>");
}
void QBasicHtmlExporter::emitBlock(const QTextBlock &block)
{
html += QLatin1Char('\n');
// save and later restore, in case we 'change' the default format by
// emitting block char format information
QTextCharFormat oldDefaultCharFormat = defaultCharFormat;
QTextList *list = block.textList();
bool numbered_list = false;
if (list) {
if (list->itemNumber(block) == 0) { // first item? emit <ul> or appropriate
const QTextListFormat format = list->format();
const int style = format.style();
switch (style) {
case QTextListFormat::ListDecimal: numbered_list = true; break;
case QTextListFormat::ListLowerAlpha: numbered_list = true; break;
case QTextListFormat::ListUpperAlpha: numbered_list = true; break;
case QTextListFormat::ListLowerRoman: numbered_list = true; break;
case QTextListFormat::ListUpperRoman: numbered_list = true; break;
}
html += QString("<%1>").arg(numbered_list ? "ol" : "ul");
}
html += QLatin1String("<li>");
const QTextCharFormat blockFmt = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat();
if (!blockFmt.properties().isEmpty()) {
emitCharFormatStyle(blockFmt);
defaultCharFormat.merge(block.charFormat());
}
}
const QTextBlockFormat blockFormat = block.blockFormat();
if (blockFormat.hasProperty(QTextFormat::BlockTrailingHorizontalRulerWidth)) {
html += QLatin1String("<hr />");
return;
}
const bool pre = blockFormat.nonBreakableLines();
static bool inPre = false;
if (pre && !inPre) {
html += QLatin1String("<pre>");
inPre = true;
} else if (!list) {
const QTextCharFormat charFmt = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat();
QString tagname = getTagName(charFmt);
if ( !inPre || tagname != "p" ) {
html += QString("<%1>").arg( tagname );
}
}
// Text
QTextBlock::Iterator it = block.begin();
for (; !it.atEnd(); ++it)
emitFragment(it.fragment());
block.next();
if ((pre && !block.next().isValid()) ||
(pre && block.next().isValid() && !block.next().blockFormat().nonBreakableLines())) {
html += QLatin1String("</pre>");
inPre = false;
if ( !block.next().isValid() ) {
html+="<br />";
}
} else if (list)
html += QLatin1String("</li>");
else {
const QTextCharFormat charFmt = formatDifference(defaultCharFormat, block.charFormat()).toCharFormat();
QString tagname = getTagName(charFmt);
if ( !inPre ) {
html += QString("</%1>").arg( tagname );
}
}
if (list) {
if (list->itemNumber(block) == list->count() - 1) { // last item? close list
html += QString("<%1>").arg(numbered_list ? "ol" : "ul");
}
}
defaultCharFormat = oldDefaultCharFormat;
}
void QBasicHtmlExporter::emitFragment(const QTextFragment &fragment)
{
const QTextCharFormat format = fragment.charFormat();
bool closeAnchor = false;
if (format.isAnchor()) {
const QString name = format.anchorName();
if (!name.isEmpty()) {
html += QLatin1String("<a name=\"");
html += name.toHtmlEscaped();
html += QLatin1String("\"></a>");
}
const QString href = format.anchorHref();
if (!href.isEmpty()) {
html += QLatin1String("<a href=\"");
html += href.toHtmlEscaped();
html += QLatin1String("\">");
closeAnchor = true;
}
}
QString txt = fragment.text();
const bool isObject = txt.contains(QChar::ObjectReplacementCharacter);
const bool isImage = isObject && format.isImageFormat();
// QLatin1String styleTag("<span style=\"");
// html += styleTag;
QStringList closing_tags = emitCharFormatStyle(format);
bool attributesEmitted = false;
if (!isImage)
attributesEmitted = closing_tags.length();
// if (attributesEmitted)
// html += QLatin1String("\">");
// else
// html.chop(styleTag.size());
if (isObject) {
for (int i = 0; isImage && i < txt.length(); ++i) {
QTextImageFormat imgFmt = format.toImageFormat();
html += QLatin1String("<img");
if (imgFmt.hasProperty(QTextFormat::ImageName))
emitAttribute("src", imgFmt.name());
html += QLatin1String(" />");
}
} else {
Q_ASSERT(!txt.contains(QChar::ObjectReplacementCharacter));
txt = txt.toHtmlEscaped();
// split for [\n{LineSeparator}]
QString forcedLineBreakRegExp = QString::fromLatin1("[\\na]");
forcedLineBreakRegExp[3] = QChar::LineSeparator;
// space in BR on purpose for compatibility with old-fashioned browsers
html += txt.replace(QRegExp(forcedLineBreakRegExp), QLatin1String("<br />"));
}
for (int i = 0; i < closing_tags.length(); i++)
html += closing_tags[i];
if (closeAnchor)
html += QLatin1String("</a>");
}
void QBasicHtmlExporter::emitAttribute(const char *attribute, const QString &value)
{
html += QLatin1Char(' ');
html += QLatin1String(attribute);
html += QLatin1String("=\"");
html += value.toHtmlEscaped();
html += QLatin1Char('"');
}
QStringList QBasicHtmlExporter::emitCharFormatStyle(const QTextCharFormat &format)
{
QStringList closing_tags;
QRegExp heading_tag_regex("h\\d");
bool isHeading = heading_tag_regex.exactMatch( getTagName(format) );
int fontWeight = format.fontWeight();
bool isBold = fontWeight > defaultCharFormat.fontWeight();
if (!isBold) { // Not bold? Let's quickly check to make sure there is not a fontweight property.
if (format.hasProperty(QTextFormat::FontWeight && !isHeading)
&& format.fontWeight() != defaultCharFormat.fontWeight()) {
isBold = true;
}
}
if (isBold && !isHeading) {
html += QLatin1String("<strong>");
closing_tags << "</strong>";
}
if (format.hasProperty(QTextFormat::FontItalic)
&& format.fontItalic() != defaultCharFormat.fontItalic()) {
html += QLatin1String("<em>");
closing_tags << "</em>";
isHeading = false;
}
// UNDERLINE CODE
// QLatin1String decorationTag(" text-decoration:");
// html += decorationTag;
// bool hasDecoration = false;
// bool atLeastOneDecorationSet = false;
// if ((format.hasProperty(QTextFormat::FontUnderline) || format.hasProperty(QTextFormat::TextUnderlineStyle))
// && format.fontUnderline() != defaultCharFormat.fontUnderline()) {
// hasDecoration = true;
// if (format.fontUnderline()) {
// html += QLatin1String(" underline");
// atLeastOneDecorationSet = true;
// }
// }
if (format.hasProperty(QTextFormat::FontStrikeOut) ||
format.fontStrikeOut()) {
if (format.fontStrikeOut()) {
html += QLatin1String("<del>");
closing_tags << "</del>";
}
}
return closing_tags;
}