texthtmlbuilder.cpp 12.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
   Copyright (C) 2020 Laurent Montel <montel@kde.org>
   based on code from Stephen Kelly <steveire@gmail.com>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "texthtmlbuilder.h"

#include <QList>
#include <QTextDocument>
Laurent Montel's avatar
Laurent Montel committed
25
#include <QDebug>
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

namespace KPIMTextEdit {

class TextHTMLBuilderPrivate
{
public:
    TextHTMLBuilderPrivate(TextHTMLBuilder *b) : q_ptr(b) {}

    QList<QTextListFormat::Style> currentListItemStyles;
    QString m_text;

    TextHTMLBuilder *q_ptr;

    Q_DECLARE_PUBLIC(TextHTMLBuilder)
};
}

using namespace KPIMTextEdit;
TextHTMLBuilder::TextHTMLBuilder()
    : AbstractMarkupBuilder(), d_ptr(new TextHTMLBuilderPrivate(this))
{
}

Laurent Montel's avatar
Laurent Montel committed
49
50
51
52
TextHTMLBuilder::~TextHTMLBuilder()
{
    delete d_ptr;
}
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

void TextHTMLBuilder::beginStrong()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<strong>"));
}

void TextHTMLBuilder::endStrong()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</strong>"));
}

void TextHTMLBuilder::beginEmph()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<em>"));
}

void TextHTMLBuilder::endEmph()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</em>"));
}

void TextHTMLBuilder::beginUnderline()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<u>"));
}

void TextHTMLBuilder::endUnderline()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</u>"));
}

void TextHTMLBuilder::beginStrikeout()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<s>"));
}

void TextHTMLBuilder::endStrikeout()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</s>"));
}

void TextHTMLBuilder::beginForeground(const QBrush &brush)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(
                QStringLiteral("<span style=\"color:%1;\">").arg(brush.color().name()));
}

void TextHTMLBuilder::endForeground()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</span>"));
}

void TextHTMLBuilder::beginBackground(const QBrush &brush)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<span style=\"background-color:%1;\">")
                     .arg(brush.color().name()));
}

void TextHTMLBuilder::endBackground()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</span>"));
}

void TextHTMLBuilder::beginAnchor(const QString &href, const QString &name)
{
    Q_D(TextHTMLBuilder);
    if (!href.isEmpty()) {
        if (!name.isEmpty()) {
            d->m_text.append(
                        QStringLiteral("<a href=\"%1\" name=\"%2\">").arg(href, name));
        } else {
            d->m_text.append(QStringLiteral("<a href=\"%1\">").arg(href));
        }
    } else {
        if (!name.isEmpty()) {
            d->m_text.append(QStringLiteral("<a name=\"%1\">").arg(name));
        }
    }
}

void TextHTMLBuilder::endAnchor()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</a>"));
}

void TextHTMLBuilder::beginFontFamily(const QString &family)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(
                QStringLiteral("<span style=\"font-family:%1;\">").arg(family));
}

void TextHTMLBuilder::endFontFamily()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</span>"));
}

void TextHTMLBuilder::beginFontPointSize(int size)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<span style=\"font-size:%1pt;\">")
                     .arg(QString::number(size)));
}

void TextHTMLBuilder::endFontPointSize()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</span>"));
}

void TextHTMLBuilder::beginParagraph(Qt::Alignment al, qreal topMargin,
                                     qreal bottomMargin, qreal leftMargin,
                                     qreal rightMargin)
{
    Q_D(TextHTMLBuilder);
    // Don't put paragraph tags inside li tags. Qt bug reported.
    //     if (currentListItemStyles.size() != 0)
    //     {
    QString styleString;
Laurent Montel's avatar
Laurent Montel committed
186
187
188
189
    styleString.append(QStringLiteral("margin-top:%1;").arg(topMargin));
    styleString.append(QStringLiteral("margin-bottom:%1;").arg(bottomMargin));
    styleString.append(QStringLiteral("margin-left:%1;").arg(leftMargin));
    styleString.append(QStringLiteral("margin-right:%1;").arg(rightMargin));
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207

    // Using == doesn't work here.
    // Using bitwise comparison because an alignment can contain a vertical and
    // a
    // horizontal part.
    if (al & Qt::AlignRight) {
        d->m_text.append(QStringLiteral("<p align=\"right\" "));
    } else if (al & Qt::AlignHCenter) {
        d->m_text.append(QStringLiteral("<p align=\"center\" "));
    } else if (al & Qt::AlignJustify) {
        d->m_text.append(QStringLiteral("<p align=\"justify\" "));
    } else if (al & Qt::AlignLeft) {
        d->m_text.append(QStringLiteral("<p"));
    } else {
        d->m_text.append(QStringLiteral("<p"));
    }

    if (!styleString.isEmpty()) {
Laurent Montel's avatar
Laurent Montel committed
208
        d->m_text.append(QStringLiteral(" style=\"") + styleString + QLatin1Char('"'));
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
    }
    d->m_text.append(QLatin1Char('>'));
    //     }
}

void TextHTMLBuilder::beginHeader(int level)
{
    Q_D(TextHTMLBuilder);
    switch (level) {
    case 1:
        d->m_text.append(QStringLiteral("<h1>"));
        break;
    case 2:
        d->m_text.append(QStringLiteral("<h2>"));
        break;
    case 3:
        d->m_text.append(QStringLiteral("<h3>"));
        break;
    case 4:
        d->m_text.append(QStringLiteral("<h4>"));
        break;
    case 5:
        d->m_text.append(QStringLiteral("<h5>"));
        break;
    case 6:
        d->m_text.append(QStringLiteral("<h6>"));
        break;
    default:
        break;
    }
}

void TextHTMLBuilder::endHeader(int level)
{
    Q_D(TextHTMLBuilder);
    switch (level) {
    case 1:
        d->m_text.append(QStringLiteral("</h1>"));
        break;
    case 2:
        d->m_text.append(QStringLiteral("</h2>"));
        break;
    case 3:
        d->m_text.append(QStringLiteral("</h3>"));
        break;
    case 4:
        d->m_text.append(QStringLiteral("</h4>"));
        break;
    case 5:
        d->m_text.append(QStringLiteral("</h5>"));
        break;
    case 6:
        d->m_text.append(QStringLiteral("</h6>"));
        break;
    default:
        break;
    }
}

void TextHTMLBuilder::endParagraph()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</p>\n"));
}

void TextHTMLBuilder::addNewline()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<p>&nbsp;"));
}

void TextHTMLBuilder::insertHorizontalRule(int width)
{
    Q_D(TextHTMLBuilder);
    if (width != -1) {
        d->m_text.append(QStringLiteral("<hr width=\"%1\" />\n").arg(width));
    }
    d->m_text.append(QStringLiteral("<hr />\n"));
}

void TextHTMLBuilder::insertImage(const QString &src, qreal width, qreal height)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<img src=\"%1\" ").arg(src));
    if (width != 0)
        d->m_text.append(QStringLiteral("width=\"%2\" ").arg(width));
    if (height != 0)
        d->m_text.append(QStringLiteral("height=\"%2\" ").arg(height));
    d->m_text.append(QStringLiteral("/>"));
}

void TextHTMLBuilder::beginList(QTextListFormat::Style type)
{
    Q_D(TextHTMLBuilder);
    d->currentListItemStyles.append(type);
    switch (type) {
    case QTextListFormat::ListDisc:
        d->m_text.append(QStringLiteral("<ul type=\"disc\">\n"));
        break;
    case QTextListFormat::ListCircle:
        d->m_text.append(QStringLiteral("\n<ul type=\"circle\">\n"));
        break;
    case QTextListFormat::ListSquare:
        d->m_text.append(QStringLiteral("\n<ul type=\"square\">\n"));
        break;
    case QTextListFormat::ListDecimal:
        d->m_text.append(QStringLiteral("\n<ol type=\"1\">\n"));
        break;
    case QTextListFormat::ListLowerAlpha:
        d->m_text.append(QStringLiteral("\n<ol type=\"a\">\n"));
        break;
    case QTextListFormat::ListUpperAlpha:
        d->m_text.append(QStringLiteral("\n<ol type=\"A\">\n"));
        break;
    case QTextListFormat::ListLowerRoman:
        d->m_text.append(QStringLiteral("\n<ol type=\"i\">\n"));
        break;
    case QTextListFormat::ListUpperRoman:
        d->m_text.append(QStringLiteral("\n<ol type=\"I\">\n"));
        break;
    default:
        break;
    }
}
void TextHTMLBuilder::endList()
{
    Q_D(TextHTMLBuilder);
    switch (d->currentListItemStyles.last()) {
    case QTextListFormat::ListDisc:
    case QTextListFormat::ListCircle:
    case QTextListFormat::ListSquare:
        d->m_text.append(QStringLiteral("</ul>\n"));
        break;
    case QTextListFormat::ListDecimal:
    case QTextListFormat::ListLowerAlpha:
    case QTextListFormat::ListUpperAlpha:
    case QTextListFormat::ListLowerRoman:
    case QTextListFormat::ListUpperRoman:
        d->m_text.append(QStringLiteral("</ol>\n"));
        break;
    default:
        break;
    }
    d->currentListItemStyles.removeLast();
}
void TextHTMLBuilder::beginListItem()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<li>"));
}

void TextHTMLBuilder::endListItem()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</li>\n"));
}

void TextHTMLBuilder::beginSuperscript()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<sup>"));
}

void TextHTMLBuilder::endSuperscript()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</sup>"));
}

void TextHTMLBuilder::beginSubscript()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<sub>"));
}

void TextHTMLBuilder::endSubscript()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</sub>"));
}

void TextHTMLBuilder::beginTable(qreal cellpadding, qreal cellspacing,
                                 const QString &width)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(
                QStringLiteral("<table cellpadding=\"%1\" cellspacing=\"%2\" "
                               "width=\"%3\" border=\"1\">")
                .arg(cellpadding)
                .arg(cellspacing)
                .arg(width));
}

void TextHTMLBuilder::beginTableRow()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("<tr>"));
}

void TextHTMLBuilder::beginTableHeaderCell(const QString &width, int colspan,
                                           int rowspan)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(
                QStringLiteral("<th width=\"%1\" colspan=\"%2\" rowspan=\"%3\">")
                .arg(width)
                .arg(colspan)
                .arg(rowspan));
}

void TextHTMLBuilder::beginTableCell(const QString &width, int colspan,
                                     int rowspan)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(
                QStringLiteral("<td width=\"%1\" colspan=\"%2\" rowspan=\"%3\">")
                .arg(width)
                .arg(colspan)
                .arg(rowspan));
}

void TextHTMLBuilder::endTable()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</table>"));
}

void TextHTMLBuilder::endTableRow()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</tr>"));
}

void TextHTMLBuilder::endTableHeaderCell()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</th>"));
}

void TextHTMLBuilder::endTableCell()
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(QStringLiteral("</td>"));
}

void TextHTMLBuilder::appendLiteralText(const QString &text)
{
    Q_D(TextHTMLBuilder);
Laurent Montel's avatar
Laurent Montel committed
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    const QString textEscaped = text.toHtmlEscaped();
    QString textEscapedResult;
    for (int i = 0, total = textEscaped.count(); i < total; ++i) {
        const QChar c = textEscaped.at(i);

        if (c == QLatin1Char(' ')) {
            if (i == 0) {
                textEscapedResult += QStringLiteral("&nbsp;");
            } else {
                if (i + 1 < textEscaped.count() && (textEscaped.at(i + 1) == QLatin1Char(' '))) {
                    textEscapedResult += QStringLiteral("&nbsp;");
                } else {
                    textEscapedResult += c;
                }
            }
Laurent Montel's avatar
Laurent Montel committed
472
473
474
        } else if (c == QLatin1Char('\t')) {
            //TODO fixme ?
            textEscapedResult += QStringLiteral("&nbsp;&nbsp;&nbsp; ");
Laurent Montel's avatar
Laurent Montel committed
475
476
477
478
479
        } else {
            textEscapedResult += c;
        }
    }
    d->m_text.append(textEscapedResult);
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
}

void TextHTMLBuilder::appendRawText(const QString &text)
{
    Q_D(TextHTMLBuilder);
    d->m_text.append(text);
}

QString TextHTMLBuilder::getResult()
{
    Q_D(TextHTMLBuilder);
    auto ret = d->m_text;
    d->m_text.clear();
    return ret;
}