[Techdraw] Import SVG files with UTF-8 encoding (#18816)

* [Techdraw] Fix importing SVG files with UTF-8 encoding

* [Techdraw] fix Lint feedback
This commit is contained in:
Syres916
2025-01-06 17:32:56 +00:00
committed by GitHub
parent 5236541045
commit b86e1adb22
4 changed files with 63 additions and 8 deletions

View File

@@ -193,6 +193,7 @@ SET(TDTestFile_SRCS
TDTest/TestHatch.svg
TDTest/TestImage.png
TDTest/TestSymbol.svg
TDTest/TestNonAsciiSymbol.svg
TDTest/TestTemplate.svg
)

View File

@@ -456,16 +456,25 @@ void CmdTechDrawView::activated(int iMsg)
filename = Base::Tools::escapeEncodeFilename(filename);
auto filespec = DU::cleanFilespecBackslash(filename.toStdString());
openCommand(QT_TRANSLATE_NOOP("Command", "Create Symbol"));
doCommand(Doc, "f = open(\"%s\", 'r')", filespec.c_str());
doCommand(Doc, "import codecs");
doCommand(Doc,
"f = codecs.open(\"%s\", 'r', encoding=\"utf-8\")",
filespec.c_str());
doCommand(Doc, "svg = f.read()");
doCommand(Doc, "f.close()");
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",
doCommand(Doc,
"App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",
FeatName.c_str());
doCommand(
Doc,
"App.activeDocument().%s.translateLabel('DrawViewSymbol', 'Symbol', '%s')",
FeatName.c_str(),
FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.translateLabel('DrawViewSymbol', 'Symbol', '%s')",
FeatName.c_str(), FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.Symbol = svg", FeatName.c_str());
doCommand(Doc, "App.activeDocument().%s.addView(App.activeDocument().%s)", PageName.c_str(),
FeatName.c_str());
doCommand(Doc,
"App.activeDocument().%s.addView(App.activeDocument().%s)",
PageName.c_str(),
FeatName.c_str());
}
else {
std::string FeatName = getUniqueObjectName("Image");
@@ -1551,7 +1560,8 @@ void CmdTechDrawSymbol::activated(int iMsg)
filename = Base::Tools::escapeEncodeFilename(filename);
auto filespec = DU::cleanFilespecBackslash(filename.toStdString());
openCommand(QT_TRANSLATE_NOOP("Command", "Create Symbol"));
doCommand(Doc, "f = open(\"%s\", 'r')", (const char*)filespec.c_str());
doCommand(Doc, "import codecs");
doCommand(Doc, "f = codecs.open(\"%s\", 'r', encoding=\"utf-8\")", filespec.c_str());
doCommand(Doc, "svg = f.read()");
doCommand(Doc, "f.close()");
doCommand(Doc, "App.activeDocument().addObject('TechDraw::DrawViewSymbol', '%s')",

View File

@@ -1,5 +1,6 @@
import FreeCAD
import codecs
import os
import unittest
from .TechDrawTestUtilities import createPageWithSVGTemplate
@@ -21,7 +22,26 @@ class DrawViewSymbolTest(unittest.TestCase):
sym = FreeCAD.ActiveDocument.addObject("TechDraw::DrawViewSymbol", "TestSymbol")
path = os.path.dirname(os.path.abspath(__file__))
symbolFileSpec = path + "/TestSymbol.svg"
f = open(symbolFileSpec, "r")
f = codecs.open(symbolFileSpec, "r", encoding="utf-8")
svg = f.read()
f.close()
sym.Symbol = svg
self.page.addView(sym)
sym.X = 220.0
sym.Y = 150.0
FreeCAD.ActiveDocument.recompute()
self.assertTrue("Up-to-date" in sym.State)
def testNonAsciiSymbol(self):
"""Tests if a Non-Ascii symbol can be added to page"""
sym = FreeCAD.ActiveDocument.addObject(
"TechDraw::DrawViewSymbol", "NonAsciiSymbol"
)
path = os.path.dirname(os.path.abspath(__file__))
symbolFileSpec = path + "/TestNonAsciiSymbol.svg"
f = codecs.open(symbolFileSpec, "r", encoding="utf-8")
svg = f.read()
f.close()
sym.Symbol = svg
@@ -34,5 +54,6 @@ class DrawViewSymbolTest(unittest.TestCase):
self.assertTrue("Up-to-date" in sym.State)
if __name__ == "__main__":
unittest.main()

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg
xmlns="http://www.w3.org/2000/svg" version="1.1"
xmlns:freecad="http://www.freecad.org/wiki/index.php?title=Svg_Namespace"
width ="57.0mm"
height="10mm"
viewBox="0 0 57.0 10">
<g id="first-frame"
style="fill:#fff;fill-opacity:1;stroke:#000;stroke-width:0.5;stroke-linecap:round;stroke-linejoin:round;font-size:5.0;text-anchor:middle;font-family:osifont">
<rect width="56.5" height="9.5" x="0.25" y="0.25" />
<circle cx="5.0" cy="5.0" r="1.75" />
<path d="m 2 5 h 6 m -3 -3 v 6 " />
<path d="m 9.75 0.25 v 9.5 " />
<text freecad:editable="Value1" x="20.0" y="7" fill="#000"> <tspan>⌀ 0,01</tspan> </text>
<path d="m 28.25 0.25 v 9.5 " />
<text freecad:editable="Value2" x="34.0" y="7" fill="#000"> <tspan>Ä</tspan> </text>
<path d="m 37.75 0.25 v 9.5 " />
<text freecad:editable="Value3" x="43.5" y="7" fill="#000"> <tspan>Ö</tspan> </text>
<path d="m 47.25 0.25 v 9.5 " />
<text freecad:editable="Value4" x="53.0" y="7" fill="#000"> <tspan>Ü</tspan> </text>
</g>
</svg>

After

Width:  |  Height:  |  Size: 1.1 KiB