[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    [krita] libs/flake/text: Fix text-align to text-anchor conversion.
From:       Wolthera_van_Hövell_tot_Westerflier <null () kde ! org>
Date:       2017-12-31 21:46:00
Message-ID: E1eVlQe-0005FJ-FB () code ! kde ! org
[Download RAW message or body]

Git commit 308a7e7997a02b353ce5b06e72a59c09ba40d29c by Wolthera van Hövell tot \
Westerflier. Committed on 31/12/2017 at 21:26.
Pushed by woltherav into branch 'master'.

Fix text-align to text-anchor conversion.

The SVGToHTML conversion now assumes that the toplevel nodes
under the root text are always paragraphs. This makes sense from a
html standards perspective, but will do odd things to certain
configurations.

This has been considered acceptable to handle situations where
the user creates text with rich text, saves it, and reopens the editor.

M  +50   -53   libs/flake/text/KoSvgTextChunkShape.cpp
M  +21   -13   libs/flake/text/KoSvgTextShapeMarkupConverter.cpp

https://commits.kde.org/krita/308a7e7997a02b353ce5b06e72a59c09ba40d29c

diff --git a/libs/flake/text/KoSvgTextChunkShape.cpp \
b/libs/flake/text/KoSvgTextChunkShape.cpp index 821002bcdaf..42694385194 100644
--- a/libs/flake/text/KoSvgTextChunkShape.cpp
+++ b/libs/flake/text/KoSvgTextChunkShape.cpp
@@ -399,72 +399,69 @@ bool KoSvgTextChunkShape::saveHtml(HtmlSavingContext &context)
         appendLazy(&dyPos, t.dyPos, i);
     }
 
+    KoSvgTextChunkShape *parent = !isRootTextNode() ? \
dynamic_cast<KoSvgTextChunkShape*>(this->parent()) : 0; +    KoSvgTextProperties \
parentProperties = +        parent ? parent->textProperties() : \
KoSvgTextProperties::defaultProperties(); +
+    // XXX: we don't save fill, stroke, text length, length adjust or spacing and \
glyphs. +    KoSvgTextProperties ownProperties = \
textProperties().ownProperties(parentProperties); +
     if (isRootTextNode()) {
         context.shapeWriter().startElement("body", false);
-        context.shapeWriter().startElement("p", false);
+        if (layoutInterface()->isTextNode()) {
+            context.shapeWriter().startElement("p", false);
+        }
         // XXX: Save the style?
 
-    } else if (!dyPos.isEmpty()) {
+    } else if (parent->isRootTextNode()) {
         context.shapeWriter().startElement("p", false);
     } else {
         context.shapeWriter().startElement("span", false);
         // XXX: Save the style?
     }
 
-    if (layoutInterface()->isTextNode()) {
-
-        KoSvgTextChunkShape *parent = !isRootTextNode() ? \
                dynamic_cast<KoSvgTextChunkShape*>(this->parent()) : 0;
-
-
-        qDebug() << "saveHTML" << this << d->text << xPos << yPos << dxPos << dyPos;
-        KoSvgTextProperties parentProperties =
-            parent ? parent->textProperties() : \
                KoSvgTextProperties::defaultProperties();
-
-        // XXX: we don't save fill, stroke, text length, length adjust or spacing \
                and glyphs.
-        KoSvgTextProperties ownProperties = \
                textProperties().ownProperties(parentProperties);
-        QMap<QString, QString> attributes = \
                ownProperties.convertToSvgTextAttributes();
-
-        if (attributes.count() > 0) {
-            QString styleString;
-            for (auto it = attributes.constBegin(); it != attributes.constEnd(); \
                ++it) {
-                if (QString(it.key().toLatin1().data()).contains("text-anchor")) {
-                    QString val = it.value();
-                    if (it.value()=="middle") {
-                        val = "center";
-                    } else if (it.value()=="end") {
-                        val = "right";
-                    } else {
-                        val = "left";
-                    }
-                    styleString.append("text-align")
-                            .append(": ")
-                            .append(val)
-                            .append(";" );
-                } else if (QString(it.key().toLatin1().data()).contains("fill")){
-                    styleString.append("color")
-                            .append(": ")
-                            .append(it.value())
-                            .append(";" );
-                } else if \
                (QString(it.key().toLatin1().data()).contains("font-size")){
-                    QString val = it.value();
-                    if (QRegExp ("\\d*").exactMatch(val)) {
-                        val.append("pt");
-                    }
-                    styleString.append(it.key().toLatin1().data())
-                            .append(": ")
-                            .append(val)
-                            .append(";" );
+    QMap<QString, QString> attributes = ownProperties.convertToSvgTextAttributes();
+    if (attributes.size() > 0) {
+        QString styleString;
+        for (auto it = attributes.constBegin(); it != attributes.constEnd(); ++it) {
+            if (QString(it.key().toLatin1().data()).contains("text-anchor")) {
+                QString val = it.value();
+                if (it.value()=="middle") {
+                    val = "center";
+                } else if (it.value()=="end") {
+                    val = "right";
                 } else {
-                    styleString.append(it.key().toLatin1().data())
-                            .append(": ")
-                            .append(it.value())
-                            .append(";" );
+                    val = "left";
+                }
+                styleString.append("text-align")
+                        .append(": ")
+                        .append(val)
+                        .append(";" );
+            } else if (QString(it.key().toLatin1().data()).contains("fill")){
+                styleString.append("color")
+                        .append(": ")
+                        .append(it.value())
+                        .append(";" );
+            } else if (QString(it.key().toLatin1().data()).contains("font-size")){
+                QString val = it.value();
+                if (QRegExp ("\\d*").exactMatch(val)) {
+                    val.append("pt");
                 }
+                styleString.append(it.key().toLatin1().data())
+                        .append(": ")
+                        .append(val)
+                        .append(";" );
+            } else {
+                styleString.append(it.key().toLatin1().data())
+                        .append(": ")
+                        .append(it.value())
+                        .append(";" );
             }
-            context.shapeWriter().addAttribute("style", styleString);
         }
-
-
+        context.shapeWriter().addAttribute("style", styleString);
+    }
+    if (layoutInterface()->isTextNode()) {
+        qDebug() << "saveHTML" << this << d->text << xPos << yPos << dxPos << dyPos;
         // After adding all the styling to the <p> element, add the text
         context.shapeWriter().addTextNode(d->text);
     }
@@ -476,7 +473,7 @@ bool KoSvgTextChunkShape::saveHtml(HtmlSavingContext &context)
         }
     }
 
-    if (isRootTextNode()) {
+    if (isRootTextNode() && layoutInterface()->isTextNode()) {
         context.shapeWriter().endElement(); // body
     }
     context.shapeWriter().endElement(); // p or span
diff --git a/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp \
b/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp index c532714ef59..059079a1233 \
                100644
--- a/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp
+++ b/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp
@@ -257,21 +257,32 @@ bool KoSvgTextShapeMarkupConverter::convertFromHtml(const \
QString &htmlText, QSt  }
 
             QXmlStreamAttributes attributes = htmlReader.attributes();
+
+            QString textAlign;
+            if (attributes.hasAttribute("align")) {
+                textAlign = attributes.value("align").toString();
+            }
+
             if (attributes.hasAttribute("style")) {
                 QString filteredStyles;
                 QStringList svgStyles = QString("font-family font-size font-weight \
font-variant word-spacing text-decoration font-style font-size-adjust font-stretch \
                direction").split(" ");
                 QStringList styles = \
attributes.value("style").toString().split(";");  for(int i=0; i<styles.size(); i++) \
{  QStringList style = QString(styles.at(i)).split(":");
-
-                    if (svgStyles.contains(QString(style.at(0)).remove(" "))) {
+                    qDebug()<<style.at(0);
+                    if (svgStyles.contains(QString(style.at(0)).trimmed())) {
                         filteredStyles.append(styles.at(i)+";");
                     }
-                    if (QString(style.at(0)).contains("color") && \
!QString(style.at(0)).contains("-")) { +
+                    if (QString(style.at(0)).trimmed() == "color") {
                         filteredStyles.append(" fill:"+style.at(1)+";");
                     }
 
-                    if (QString(style.at(0)).contains("line-height")){
+                    if (QString(style.at(0)).trimmed() == "text-align") {
+                        textAlign = QString(style.at(1)).trimmed();
+                    }
+
+                    if (QString(style.at(0)).trimmed() == "line-height"){
                         if (style.at(1).contains("%")) {
                             double percentage = \
QString(style.at(1)).remove("%").toDouble();  em = \
QString::number(percentage/100.0)+"em"; @@ -286,15 +297,12 @@ bool \
KoSvgTextShapeMarkupConverter::convertFromHtml(const QString &htmlText, QSt  }
                 }
 
-                if (attributes.hasAttribute("align")) {
-                    QString a = attributes.value("align").toString();
-                    if (a.contains("center")) {
-                        filteredStyles.append(" text-anchor:middle;");
-                    } else if (a.contains("right")) {
-                        filteredStyles.append(" text-anchor:end;");
-                    } else {
-                        filteredStyles.append(" text-anchor:start;");
-                    }
+                if (textAlign == "center") {
+                    filteredStyles.append(" text-anchor:middle;");
+                } else if (textAlign == "right") {
+                    filteredStyles.append(" text-anchor:end;");
+                } else if (textAlign == "left"){
+                    filteredStyles.append(" text-anchor:start;");
                 }
 
                 filteredStyles.append(appendStyle);


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic