Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Handle inline-size loading and saving.
Wolthera van Hövell tot Westerflier21ab9fe45581
diff --git a/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp b/libs/flake/text/KoSvgTextShapeMarkupConverter.cppindex 1ea24358db..034c73d810 100644--- a/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp+++ b/libs/flake/text/KoSvgTextShapeMarkupConverter.cpp@@ -1639,7 +1639,7 @@ QString stylesForPSDStyleSheet(QJsonObject PSDStyleSheet, QMap<int, font_info_psreturn styles.join("; ");}-bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba, QString *svgText, bool isHorizontal)+bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba, QString *svgText, QString *svgStyles, QPointF &offset, bool isHorizontal){debugFlake << "Convert from psd engine data";@@ -1685,7 +1685,9 @@ bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba,}}QString paragraphStyle = isHorizontal? "writing-mode: horizontal-tb;": "writing-mode: vertical-rl;";+ paragraphStyle += " white-space: pre-wrap;";QString inlineSize;+ QRectF bounds;QJsonObject rendered = engineDict["Rendered"].toObject();// rendering info...@@ -1694,11 +1696,11 @@ bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba,int shapeType = shapeChild["ShapeType"].toInt();if (shapeType == 1) {QJsonArray BoxBounds = shapeChild["Cookie"].toObject()["Photoshop"].toObject()["BoxBounds"].toArray();-+ bounds = QRectF(BoxBounds[0].toDouble(), BoxBounds[1].toDouble(), BoxBounds[2].toDouble(), BoxBounds[3].toDouble());if (isHorizontal) {- inlineSize = " inline-size:"+QString::number(BoxBounds[2].toDouble())+";";+ inlineSize = " inline-size:"+QString::number(bounds.width())+";";} else {- inlineSize = " inline-size:"+QString::number(BoxBounds[3].toDouble())+";";+ inlineSize = " inline-size:"+QString::number(bounds.height())+";";}}}@@ -1742,9 +1744,30 @@ bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba,QJsonObject documentResources = root["DocumentResources"].toObject();QBuffer svgBuffer;+ QBuffer styleBuffer;svgBuffer.open(QIODevice::WriteOnly);+ styleBuffer.open(QIODevice::WriteOnly);QXmlStreamWriter svgWriter(&svgBuffer);+ QXmlStreamWriter stylesWriter(&styleBuffer);+ stylesWriter.writeStartElement("defs");+ if (bounds.isValid()) {+ stylesWriter.writeStartElement("rect");+ stylesWriter.writeAttribute("id", "bounds");+ stylesWriter.writeAttribute("x", QString::number(bounds.x()));+ stylesWriter.writeAttribute("y", QString::number(bounds.y()));+ stylesWriter.writeAttribute("width", QString::number(bounds.width()));+ stylesWriter.writeAttribute("height", QString::number(bounds.height()));+ stylesWriter.writeEndElement();+ }+ if (textShape) {+ stylesWriter.writeStartElement("path");+ stylesWriter.writeAttribute("id", "textShape");+ stylesWriter.writeAttribute("d", textShape->toString());+ stylesWriter.writeAttribute("sodipodi:nodetypes", textShape->nodeTypes());+ stylesWriter.writeEndElement();+ }+// disable auto-formatting to avoid axtra spaces appearing here and theresvgWriter.setAutoFormatting(false);@@ -1777,13 +1800,26 @@ bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba,QJsonArray runArray = paragraphRun["RunArray"].toArray();QJsonObject styleSheet = runArray[0].toObject()["ParagraphSheet"].toObject()["Properties"].toObject();+ QString styleString = stylesForPSDParagraphSheet(styleSheet);if (textShape && textPathStartOffset < 0) {- paragraphStyle += " shape-inside:path('"+textShape->toString()+"'); ";- } else {+ paragraphStyle += " shape-inside:url(#textShape);";+ } else if (styleString.contains("text-align:justify") && bounds.isValid()) {+ paragraphStyle += " shape-inside:url(#bounds);";+ } else if (bounds.isValid()){+ offset = isHorizontal? bounds.topLeft(): bounds.topRight();+ if (styleString.contains("text-anchor:middle")) {+ offset = isHorizontal? QPointF(bounds.center().x(), offset.y()):+ QPointF(offset.x(), bounds.center().y());+ } else if (styleString.contains("text-anchor:end")) {+ offset = isHorizontal? QPointF(bounds.right(), offset.y()):+ QPointF(offset.x(), bounds.bottom());+ }paragraphStyle += inlineSize;+ svgWriter.writeAttribute("transform", QString("translate(%1, %2)").arg(offset.x()).arg(offset.y()));}- paragraphStyle += stylesForPSDParagraphSheet(styleSheet);+ paragraphStyle += styleString;svgWriter.writeAttribute("style", paragraphStyle);+}bool textPathCreated = false;@@ -1837,12 +1873,14 @@ bool KoSvgTextShapeMarkupConverter::convertPSDTextEngineDataToSVG(QByteArray ba,}svgWriter.writeEndElement();//text root element.+ stylesWriter.writeEndElement();- if (svgWriter.hasError()) {+ if (svgWriter.hasError() || stylesWriter.hasError()) {d->errors << i18n("Unknown error writing SVG text element");return false;}*svgText = QString::fromUtf8(svgBuffer.data()).trimmed();+ *svgStyles = QString::fromUtf8(styleBuffer.data()).trimmed();return true;}@@ -2058,7 +2096,7 @@ void gatherStyles(QDomElement el, QString &text, QJsonObject parentStyle, QMap<Q}}-QJsonObject gatherParagraphStyle(QDomElement el, QJsonObject defaultProperties, bool &isHorizontal) {+QJsonObject gatherParagraphStyle(QDomElement el, QJsonObject defaultProperties, bool &isHorizontal, QString *inlineSize) {QString cssStyle = el.attribute("style");QStringList dummy = cssStyle.split(";");QMap<QString, QString> cssStyles;@@ -2067,13 +2105,17 @@ QJsonObject gatherParagraphStyle(QDomElement el, QJsonObject defaultProperties,QString val = style.split(":").last().trimmed();cssStyles.insert(key, val);}+ if (el.hasAttribute("text-anchor")) {+ cssStyles.insert("text-anchor", el.attribute("text-anchor", "start"));+ }+ int alignVal = 0;+ int anchorVal = 0;QJsonObject paragraphStyleSheet = defaultProperties;Q_FOREACH(QString key, cssStyles.keys()) {QString val = cssStyles.value(key);if (key == "text-align") {- int alignVal = 0;if (val == "start") {alignVal = 0;}if (val == "center") {alignVal = 2;}if (val == "end") {alignVal = 1;}@@ -2081,7 +2123,11 @@ QJsonObject gatherParagraphStyle(QDomElement el, QJsonObject defaultProperties,if (val == "justify center") {alignVal = 4;}if (val == "justify end") {alignVal = 5;}if (val == "justify") {alignVal = 6;}- paragraphStyleSheet["Justification"] = alignVal;+ }+ if (key == "text-anchor") {+ if (val == "start") {anchorVal = 0;}+ if (val == "middle") {anchorVal = 2;}+ if (val == "end") {anchorVal = 1;}}if (key == "writing-mode") {if (val == "horizontal-tb") {@@ -2090,11 +2136,19 @@ QJsonObject gatherParagraphStyle(QDomElement el, QJsonObject defaultProperties,isHorizontal = false;}}+ if (key == "inline-size") {+ *inlineSize = val;+ }+ }+ if (cssStyles.keys().contains("shape-inside")) {+ paragraphStyleSheet["Justification"] = alignVal;+ } else {+ paragraphStyleSheet["Justification"] = anchorVal;}return QJsonObject{{"DefaultStyleSheet", 0},{"Properties", paragraphStyleSheet}};}-bool KoSvgTextShapeMarkupConverter::convertToPSDTextEngineData(const QString &svgText, QByteArray *ba, QString &textTotal, bool &isHorizontal)+bool KoSvgTextShapeMarkupConverter::convertToPSDTextEngineData(const QString &svgText, const QRectF boundingBox, QByteArray *ba, QString &textTotal, bool &isHorizontal){QJsonObject root;@@ -2185,7 +2239,9 @@ bool KoSvgTextShapeMarkupConverter::convertToPSDTextEngineData(const QString &svQDomDocument doc;doc.setContent(svgText);gatherStyles(doc.documentElement(), text, QJsonObject(), QMap<QString, QString>(), styles, styleRunArray, fontSet);- QJsonObject paragraphStyle = gatherParagraphStyle(doc.documentElement(), defaultParagraphProps, isHorizontal);++ QString inlineSize;+ QJsonObject paragraphStyle = gatherParagraphStyle(doc.documentElement(), defaultParagraphProps, isHorizontal, &inlineSize);QJsonObject editor;editor["Text"] = text;@@ -2240,8 +2296,22 @@ bool KoSvgTextShapeMarkupConverter::convertToPSDTextEngineData(const QString &svresourceDict["FontSet"] = fontSet;+ QRectF bounds;+ if (!(inlineSize.isEmpty() || inlineSize == "auto")) {+ bounds = boundingBox;+ bool ok;+ double inlineSizeVal = inlineSize.toDouble(&ok);+ if (ok) {+ if (isHorizontal) {+ bounds.setWidth(inlineSizeVal);+ } else {+ bounds.setHeight(inlineSizeVal);+ }+ }+ }+QJsonObject rendered;- int shapeType = 0; // 0 point, 1 paragraph, but what does that make text-on-path?+ int shapeType = bounds.isEmpty()? 0: 1; // 0 point, 1 paragraph, but what does that make text-on-path?int writingDirection = 0;QJsonObject photoshop = QJsonObject {{"ShapeType", shapeType},{"TransformPoint0", QJsonArray({1.0, 0.0})},@@ -2251,7 +2321,7 @@ bool KoSvgTextShapeMarkupConverter::convertToPSDTextEngineData(const QString &svphotoshop["PointBase"] = QJsonArray({0.0, 0.0});} else if (shapeType == 1) {// this is the bounding box of the paragraph shape.- photoshop["BoxBounds"] = QJsonArray({0.0, 0.0, 100, 50});+ photoshop["BoxBounds"] = QJsonArray({bounds.x(), bounds.y(), bounds.width(), bounds.height()});}QJsonObject renderChild = QJsonObject{{"ShapeType", shapeType},diff --git a/libs/flake/text/KoSvgTextShapeMarkupConverter.h b/libs/flake/text/KoSvgTextShapeMarkupConverter.hindex 78a6eeed1a..d6dddfb3a1 100644--- a/libs/flake/text/KoSvgTextShapeMarkupConverter.h+++ b/libs/flake/text/KoSvgTextShapeMarkupConverter.h@@ -84,8 +84,8 @@ public:*/bool convertSvgToDocument(const QString &svgText, QTextDocument *doc);- bool convertPSDTextEngineDataToSVG(QByteArray ba, QString *svgText, bool isHorizontal);- bool convertToPSDTextEngineData(const QString &svgText, QByteArray *ba, QString &textTotal, bool &isHorizontal);+ bool convertPSDTextEngineDataToSVG(QByteArray ba, QString *svgText, QString *svgStyles, QPointF &offset, bool isHorizontal);+ bool convertToPSDTextEngineData(const QString &svgText, const QRectF boundingBox, QByteArray *ba, QString &textTotal, bool &isHorizontal);/*** A list of errors happened during loading the user's textdiff --git a/libs/psd/psd_additional_layer_info_block.h b/libs/psd/psd_additional_layer_info_block.hindex 18ab12b625..22dc9536fa 100644--- a/libs/psd/psd_additional_layer_info_block.h+++ b/libs/psd/psd_additional_layer_info_block.h@@ -849,6 +849,15 @@ struct KRITAPSD_EXPORT psd_layer_type_shape {}}+ static void setupCatcher(const QString path, KisAslCallbackObjectCatcher &catcher, psd_layer_type_shape *data) {+ catcher.subscribeRawData(path + "/TxLr/EngineData", std::bind(&psd_layer_type_shape::setEngineData, data, std::placeholders::_1));+ catcher.subscribeEnum(path + "/TxLr/Ornt", "Ornt", std::bind(&psd_layer_type_shape::setWritingMode, data, std::placeholders::_1));+ catcher.subscribeUnitFloat(path + "/TxLr/bounds/Left", "#Pnt", std::bind(&psd_layer_type_shape::setLeft, data, std::placeholders::_1));+ catcher.subscribeUnitFloat(path + "/TxLr/bounds/Top ", "#Pnt", std::bind(&psd_layer_type_shape::setTop, data, std::placeholders::_1));+ catcher.subscribeUnitFloat(path + "/TxLr/bounds/Rght", "#Pnt", std::bind(&psd_layer_type_shape::setRight, data, std::placeholders::_1));+ catcher.subscribeUnitFloat(path + "/TxLr/bounds/Btom", "#Pnt", std::bind(&psd_layer_type_shape::setBottom, data, std::placeholders::_1));+ }+QDomDocument textDataASLXML() {KisAslXmlWriter w;w.enterDescriptor("", "", "TxLr");diff --git a/libs/psd/psd_layer_section.cpp b/libs/psd/psd_layer_section.cppindex 1be038b18e..658a680a3e 100644--- a/libs/psd/psd_layer_section.cpp+++ b/libs/psd/psd_layer_section.cpp@@ -666,10 +666,18 @@ void PSDLayerMaskSection::writePsdImpl(QIODevice &io, KisNodeSP rootLayer, psd_cQString svgtext;QString styles;convert.convertToSvg(&svgtext, &styles);- convert.convertToPSDTextEngineData(svgtext, &textData.engineData, textData.text, textData.isHorizontal);- textData.boundingBox = text->outlineRect();+ // unsure about the bounds, needs more research.+ textData.boundingBox = text->boundingRect().normalized();+ textData.bounds = text->outlineRect().normalized();+ textData.bounds.setTopLeft(QPointF());+ convert.convertToPSDTextEngineData(svgtext, textData.bounds, &textData.engineData, textData.text, textData.isHorizontal);textData.textIndex = 0;- textData.transform = text->absoluteTransformation()/* * FlaketoPixels*/;+ if (text->outlineRect().topLeft().isNull()) {+ textData.transform = text->absoluteTransformation()/* * FlaketoPixels*/;+ } else {+ QTransform offset = QTransform::fromTranslate(text->outlineRect().topLeft().x(), text->outlineRect().topLeft().y());+ textData.transform = offset * text->absoluteTransformation()/* * FlaketoPixels*/;+ }} else {KoPathShape *pathShape = dynamic_cast<KoPathShape*>(shapeLayer->shapes().first());if (pathShape){diff --git a/plugins/impex/psd/psd_loader.cpp b/plugins/impex/psd/psd_loader.cppindex 783635c0e1..3c9eefc485 100644--- a/plugins/impex/psd/psd_loader.cpp+++ b/plugins/impex/psd/psd_loader.cpp@@ -415,20 +415,31 @@ KisImportExportErrorCode PSDLoader::decode(QIODevice &io)KisShapeLayerSP textLayer = new KisShapeLayer(m_doc->shapeController(), m_image, layerRecord->layerName, layerRecord->opacity);KisAslCallbackObjectCatcher catcher;psd_layer_type_shape text;- catcher.subscribeRawData("/TxLr/EngineData", std::bind(&psd_layer_type_shape::setEngineData, &text, _1));- catcher.subscribeEnum("/TxLr/Ornt", "Ornt", std::bind(&psd_layer_type_shape::setWritingMode, &text, _1));+ psd_layer_type_shape::setupCatcher(QString(), catcher, &text);KisAslXmlParser parser;parser.parseXML(layerRecord->infoBlocks.textData, catcher);KoSvgTextShape *shape = new KoSvgTextShape();KoSvgTextShapeMarkupConverter converter(shape);QString svg;QString styles;- bool res = converter.convertPSDTextEngineDataToSVG(text.engineData, &svg, text.isHorizontal);+ QPointF offset1;+ bool res = converter.convertPSDTextEngineDataToSVG(text.engineData, &svg, &styles, offset1, text.isHorizontal);if (!res) {qDebug() << converter.errors();}converter.convertFromSvg(svg, styles, m_image->bounds(), m_image->xRes()*72.0);- shape->setTransformation(layerRecord->infoBlocks.textTransform);+ if (!offset1.isNull()) {+ QPointF offset2 = shape->position() - shape->boundingRect().topLeft();+ if (text.isHorizontal) {+ offset2.setX(offset1.x());+ } else {+ offset2.setY(offset1.x());+ }+ shape->setTransformation(QTransform::fromTranslate(offset2.x(), offset2.y())+ * layerRecord->infoBlocks.textTransform);+ } else {+ shape->setTransformation(layerRecord->infoBlocks.textTransform);+ }textLayer->addShape(shape);layer = textLayer;} else {