File indexing completed on 2024-12-22 04:08:56

0001 /*
0002  * SPDX-FileCopyrightText: 2017 Boudewijn Rempt <boud@valdyas.org>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 #include "HtmlWriter.h"
0007 
0008 #include <QDebug>
0009 #include <QIODevice>
0010 #include <QTextStream>
0011 
0012 #include <klocalizedstring.h>
0013 
0014 #include <KoShape.h>
0015 #include <KoShapeLayer.h>
0016 #include <KoShapeGroup.h>
0017 #include <KoSvgTextShape.h>
0018 
0019 #include <html/HtmlSavingContext.h>
0020 
0021 HtmlWriter::HtmlWriter(const QList<KoShape*> &toplevelShapes)
0022     : m_toplevelShapes(toplevelShapes)
0023 {
0024 }
0025 
0026 HtmlWriter::~HtmlWriter()
0027 {
0028 }
0029 
0030 bool HtmlWriter::save(QIODevice &outputDevice)
0031 {
0032     if (m_toplevelShapes.isEmpty()) {
0033         return false;
0034     }
0035 
0036     QTextStream htmlStream(&outputDevice);
0037     htmlStream.setCodec("UTF-8");
0038 
0039     // header
0040     htmlStream << QLatin1String("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" "
0041                                 "\"http://www.w3.org/TR/REC-html40/strict.dtd\">"
0042                                 "<html><head><meta name=\"Krita Svg Text\" />"
0043                                 "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/>"
0044                                 "</head>");
0045     htmlStream.flush();
0046     {
0047         HtmlSavingContext savingContext(outputDevice);
0048         saveShapes(m_toplevelShapes, savingContext);
0049     }
0050     htmlStream << "</html>";
0051     htmlStream.flush();
0052     return true;
0053 }
0054 
0055 QStringList HtmlWriter::errors() const
0056 {
0057     return m_errors;
0058 }
0059 
0060 QStringList HtmlWriter::warnings() const
0061 {
0062     return m_warnings;
0063 }
0064 
0065 void HtmlWriter::saveShapes(const QList<KoShape *> shapes, HtmlSavingContext &savingContext)
0066 {
0067     Q_FOREACH (KoShape *shape, shapes) {
0068         KoShapeLayer *layer = dynamic_cast<KoShapeLayer*>(shape);
0069         if (layer) {
0070             m_errors << i18n("Saving KoShapeLayer to html is not implemented yet!");
0071         } else {
0072             KoShapeGroup *group = dynamic_cast<KoShapeGroup*>(shape);
0073             if (group) {
0074                 m_errors << i18n("KoShapeGroup to html is not implemented yet!");
0075             }
0076             else {
0077                 KoSvgTextShape *svgTextShape = dynamic_cast<KoSvgTextShape*>(shape);
0078                 if (svgTextShape) {
0079                     if (!svgTextShape->saveHtml(savingContext)) {
0080                         m_errors << i18n("saving to html failed");
0081                     }
0082                 }
0083                 else {
0084                     m_errors << i18n("Cannot save %1 to html", shape->name());
0085                 }
0086             }
0087         }
0088     }
0089 }