From kde-commits Wed Feb 28 22:26:47 2018 From: =?utf-8?q?Wolthera_van_H=C3=B6vell_tot_Westerflier?= Date: Wed, 28 Feb 2018 22:26:47 +0000 To: kde-commits Subject: [krita] plugins/python/comics_project_management_tools: CPMT: move the svg to acbf text to acbf file Message-Id: X-MARC-Message: https://marc.info/?l=kde-commits&m=151985682020713 Git commit 95adc320714e7aad82d862d7ef310976ff8bbd60 by Wolthera van H=C3=B6= vell tot Westerflier. Committed on 28/02/2018 at 22:25. Pushed by woltherav into branch 'master'. CPMT: move the svg to acbf text to acbf file and port to minidom. This allows us to fix some issues with the output as well. M +3 -70 plugins/python/comics_project_management_tools/comics_export= er.py M +54 -1 plugins/python/comics_project_management_tools/exporters/CPM= T_ACBF_XML_Exporter.py https://commits.kde.org/krita/95adc320714e7aad82d862d7ef310976ff8bbd60 diff --git a/plugins/python/comics_project_management_tools/comics_exporter= .py b/plugins/python/comics_project_management_tools/comics_exporter.py index 285c7128b86..e2d12ba83a3 100644 --- a/plugins/python/comics_project_management_tools/comics_exporter.py +++ b/plugins/python/comics_project_management_tools/comics_exporter.py @@ -23,8 +23,8 @@ An exporter that take the comicsConfig and uses it to gen= erate several files. import sys from pathlib import Path import zipfile -import xml.etree.ElementTree as ET from xml.dom import minidom +from xml.etree import ElementTree as ET import types import re from PyQt5.QtWidgets import QLabel, QProgressDialog, qApp # For the progr= ess dialog. @@ -479,76 +479,9 @@ class comicsExporter(): listOfPoints.append(point) shapeDesc["boundingBox"] =3D listOfPoints if (shape.type() =3D=3D "KoSvgTextShapeID" and textOnly is True): - textRoot =3D ET.fromstring(shape.toSvg()) - paragraph =3D ET.Element("p") - if (len(textRoot) > 0): - self.parseTextChildren(textRoot, paragraph) - shapeDesc["text"] =3D ET.tostring(paragraph, "unicode") + shapeDesc["text"] =3D shape.toSvg() list.append(shapeDesc) = - """ - Function to parse svg text to acbf ready text - TODO: Move to a new file. - """ - - def parseTextChildren(self, elRead, elWrite): - - if elRead.text is not None: - if len(elWrite) > 0: - if (elWrite[-1].tail is None): - elWrite[-1].tail =3D str() - elWrite[-1].tail =3D " ".join([elWrite[-1].tail, elRead.te= xt]) - else: - if elWrite.text is None: - elWrite.text =3D elRead.text - else: - elWrite.text =3D " ".join([elWrite.text, elRead.text]) - - for childNode in elRead: - fontWeight =3D childNode.get("font-weight") - fontItalic =3D childNode.get("font-style") - fontStrikeThrough =3D childNode.get("text-decoration") - fontBaseLine =3D childNode.get("baseline-shift") - newElementMade =3D False - if fontItalic is not None: - if (fontItalic =3D=3D "italic"): - newElement =3D ET.Element("Emphasis") - newElementMade =3D True - elif fontWeight is not None: - if (fontWeight =3D=3D "bold" or int(fontWeight) > 400): - newElement =3D ET.Element("Strong") - newElementMade =3D True - elif fontStrikeThrough is not None: - if (fontStrikeThrough =3D=3D "line-through"): - newElement =3D ET.Element("Strikethrough") - newElementMade =3D True - elif fontBaseLine is not None: - if (fontBaseLine =3D=3D "super"): - newElement =3D ET.Element("Sup") - newElementMade =3D True - elif (fontBaseLine =3D=3D "sub"): - newElement =3D ET.Element("Sub") - newElementMade =3D True - - if newElementMade is True: - if (len(childNode) > 0): - self.parseTextChildren(childNode, newElement) - else: - newElement.text =3D childNode.text - elWrite.append(newElement) - else: - if (len(childNode) > 0): - self.parseTextChildren(childNode, elWrite) - else: - if len(elWrite) > 0: - if (elWrite[-1].tail is None): - elWrite[-1].tail =3D str() - elWrite[-1].tail =3D " ".join([elWrite[-1].tail, c= hildNode.text]) - else: - if elWrite.text is None: - elWrite.text =3D str() - elWrite.text =3D " ".join([elWrite.text, childNode= .text]) - """ Function to remove layers when they have the given labels. = @@ -575,7 +508,7 @@ class comicsExporter(): title =3D self.configDictionary["title"] = # Get the appropriate path. - url =3D str(exportPath / "metadata"/ str(title + ".cbz")) + url =3D str(exportPath / str(title + ".cbz")) = # Create a zip file. cbzArchive =3D zipfile.ZipFile(url, mode=3D"w", compression=3Dzipf= ile.ZIP_STORED) diff --git a/plugins/python/comics_project_management_tools/exporters/CPMT_= ACBF_XML_Exporter.py b/plugins/python/comics_project_management_tools/expor= ters/CPMT_ACBF_XML_Exporter.py index 5c7cc7e4a8b..f01a57faf20 100644 --- a/plugins/python/comics_project_management_tools/exporters/CPMT_ACBF_XM= L_Exporter.py +++ b/plugins/python/comics_project_management_tools/exporters/CPMT_ACBF_XM= L_Exporter.py @@ -324,7 +324,10 @@ def write_xml(configDictionary =3D {}, pageData =3D []= , pagesLocationList =3D [], loc textArea =3D document.createElement("text-area") textArea.setAttribute("points", " ".join(boundingBoxText)) # TODO: Rotate will require proper global transform api as= transform info is not written intotext. #textArea.s= etAttribute("text-rotation", str(v["rotate"])) - paragraph =3D minidom.parseString(v["text"]) + svg =3D minidom.parseString(v["text"]) + paragraph =3D minidom.Document() + paragraph.appendChild(paragraph.createElement("p")) + parseTextChildren(paragraph, svg.documentElement, paragrap= h.documentElement) = textArea.appendChild(paragraph.documentElement) textLayer.appendChild(textArea) else: @@ -416,3 +419,53 @@ def createStandAloneACBF(configDictionary, document, l= ocation, pagesLocationList f.write(document.toprettyxml(indent=3D" ")) f.close() return True + +""" +Function to parse svg text to acbf ready text +""" + +def parseTextChildren(document, elRead, elWrite): + for childNode in elRead.childNodes: + if childNode.nodeType =3D=3D minidom.Node.TEXT_NODE: + if len(childNode.data) > 0: + if len(elWrite.childNodes)>0 and str(childNode.data).start= swith(" ") is False: + elWrite.appendChild(document.createTextNode(" ")) + elWrite.appendChild(document.createTextNode(str(childNode.= data))) + elif len(childNode.childNodes)>0: + fontWeight =3D str(childNode.getAttribute("font-weight")) + fontItalic =3D str(childNode.getAttribute("font-style")) + fontStrikeThrough =3D str(childNode.getAttribute("text-decorat= ion")) + fontBaseLine =3D str(childNode.getAttribute("baseline-shift")) + newElementMade =3D False + if fontItalic.isalnum(): + if (fontItalic =3D=3D "italic"): + newElement =3D document.createElement("Emphasis") + newElementMade =3D True + elif fontWeight.isalnum(): + if (fontWeight =3D=3D "bold" or int(fontWeight) > 400): + newElement =3D document.createElement("Strong") + newElementMade =3D True + elif fontStrikeThrough.isalnum(): + if (fontStrikeThrough =3D=3D "line-through"): + newElement =3D document.createElement("Strikethrough") + newElementMade =3D True + elif fontBaseLine.isalnum(): + if (fontBaseLine =3D=3D "super"): + newElement =3D document.createElement("Sup") + newElementMade =3D True + elif (fontBaseLine =3D=3D "sub"): + newElement =3D document.createElement("Sub") + newElementMade =3D True + + if newElementMade is True: + parseTextChildren(document, childNode, newElement) + elWrite.appendChild(newElement) + else: + parseTextChildren(document, childNode, elWrite) + + # If it is not a text node, nor does it have children(which could = be textnodes), + # we should assume it's empty and ignore it. + elWrite.normalize() + for e in elWrite.childNodes: + if e.nodeType =3D=3D minidom.Node.TEXT_NODE: + e.data =3D str(e.data).replace(" ", " ")