From 1b53261012e5c642b078394cc75fe49499e0cb79 Mon Sep 17 00:00:00 2001 From: vocx-fc Date: Wed, 8 Jul 2020 19:38:17 -0500 Subject: [PATCH] Draft: move getText out of getSVG function --- src/Mod/Draft/getSVG.py | 246 ++++++++++++++++++++++++++-------------- 1 file changed, 159 insertions(+), 87 deletions(-) diff --git a/src/Mod/Draft/getSVG.py b/src/Mod/Draft/getSVG.py index 26c998de38..e6d37dafed 100644 --- a/src/Mod/Draft/getSVG.py +++ b/src/Mod/Draft/getSVG.py @@ -341,6 +341,143 @@ def getOvershoot(point, shootsize, color, linewidth, angle=0): return get_overshoot(point, shootsize, color, linewidth, angle) +def get_text(plane, techdraw, + tcolor, fontsize, fontname, + angle, base, text, + linespacing=0.5, align="center", flip=True): + """Get the SVG representation of a textual element.""" + if isinstance(angle, FreeCAD.Rotation): + if not plane: + angle = angle.Angle + else: + if plane.axis.getAngle(angle.Axis) < 0.001: + angle = angle.Angle + elif abs(plane.axis.getAngle(angle.Axis) - math.pi) < 0.001: + if abs(angle.Angle) > 0.1: + angle = -angle.Angle + else: + angle = angle.Angle + elif abs(plane.axis.getAngle(angle.Axis) - math.pi/2) < 0.001: + # text is perpendicular to view, so it shouldn't appear + return "" + else: + # TODO maybe there is something better to do here? + angle = 0 + + # text should be a list of strings separated by a newline + if not isinstance(text, list): + text = text.split("\n") + + if align.lower() == "center": + anchor = "middle" + elif align.lower() == "left": + anchor = "start" + else: + anchor = "end" + + if techdraw: + svg = "" + for i in range(len(text)): + _t = text[i].replace("&", "&") + _t = _t.replace("<", "<") + t = _t.replace(">", ">") + + if six.PY2 and not isinstance(t, six.text_type): + t = t.decode("utf8") + + # possible workaround if UTF8 is unsupported + # import unicodedata as U + # v = list() + # for c in U.normalize("NFKD", t): + # if not U.combining(c): + # v.append(c) + # + # t = u"".join(v) + # t = t.encode("utf8") + + svg += '", ">") + except: + _t = text[0].decode("utf8") + _t = _t.replace("&", "&").replace("<", "<") + svg += _t.replace(">", ">") + else: + for i in range(len(text)): + if i == 0: + svg += '' + else: + svg += ''.format(linespacing) + + try: + _t = text[i].replace("&", "&").replace("<", "<") + svg += _t.replace(">", ">") + except: + _t = text[i].decode("utf8") + _t = _t.replace("&", "&").replace("<", "<") + svg += _t.replace(">", ">") + + svg += '\n' + svg += '\n' + return svg + + +def getText(plane, techdraw, + tcolor, fontsize, fontname, + angle, base, text, + linespacing=0.5, align="center", flip=True): + """Get the SVG representation of a textual element. DEPRECATED.""" + utils.use_instead("get_text") + return get_text(plane, techdraw, + tcolor, fontsize, fontname, + angle, base, text, + linespacing, align, flip) + + def get_path(obj, plane, fill, pathdata, stroke, linewidth, lstyle, fill_opacity=None, @@ -679,86 +816,6 @@ def getSVG(obj, if hasattr(obj, "ViewObject") and hasattr(obj.ViewObject, "DrawStyle"): lstyle = get_line_style(obj.ViewObject.DrawStyle, scale) - def getText(tcolor,fontsize,fontname,angle,base,text,linespacing=0.5,align="center",flip=True): - if isinstance(angle,FreeCAD.Rotation): - if not plane: - angle = angle.Angle - else: - if plane.axis.getAngle(angle.Axis) < 0.001: - angle = angle.Angle - elif abs(plane.axis.getAngle(angle.Axis)-math.pi) < 0.001: - if abs(angle.Angle) > 0.1: - angle = -angle.Angle - else: - angle = angle.Angle - elif abs(plane.axis.getAngle(angle.Axis)-math.pi/2) < 0.001: - return "" # text is perpendicular to view, so it shouldn't appear - else: - angle = 0 #TODO maybe there is something better to do here? - if not isinstance(text,list): - text = text.split("\n") - if align.lower() == "center": - anchor = "middle" - elif align.lower() == "left": - anchor = "start" - else: - anchor = "end" - if techdraw: - svg = "" - for i in range(len(text)): - t = text[i].replace("&","&").replace("<","<").replace(">",">") - if six.PY2 and not isinstance(t, six.text_type): - t = t.decode("utf8") - # possible workaround if UTF8 is unsupported - # import unicodedata - # t = u"".join([c for c in unicodedata.normalize("NFKD",t) if not unicodedata.combining(c)]).encode("utf8") - svg += '\n' - else: - svg = '",">") - except: - svg += text[0].decode("utf8").replace("&","&").replace("<","<").replace(">",">") - else: - for i in range(len(text)): - if i == 0: - svg += '' - else: - svg += '' - try: - svg += text[i].replace("&","&").replace("<","<").replace(">",">") - except: - svg += text[i].decode("utf8").replace("&","&").replace("<","<").replace(">",">") - svg += '\n' - svg += '\n' - return svg - if not obj: pass @@ -876,7 +933,9 @@ def getSVG(obj, angle + math.pi) # drawing text - svg += getText(stroke,fontsize,obj.ViewObject.FontName,tangle,tbase,prx.string) + svg += get_text(plane, techdraw, + stroke, fontsize, obj.ViewObject.FontName, + tangle, tbase, prx.string) elif utils.get_type(obj) == "AngularDimension": if FreeCAD.GuiUp: @@ -945,7 +1004,9 @@ def getSVG(obj, else: tangle = 0 tbase = get_proj(prx.tbase, plane) - svg += getText(stroke,fontsize,obj.ViewObject.FontName,tangle,tbase,prx.string) + svg += get_text(plane, techdraw, + stroke, fontsize, obj.ViewObject.FontName, + tangle, tbase, prx.string) elif utils.get_type(obj) == "Label": if getattr(obj.ViewObject, "Line", True): # some Labels may have no Line property @@ -988,8 +1049,10 @@ def getSVG(obj, rotation = obj.Placement.Rotation justification = obj.ViewObject.TextAlignment text = obj.Text - svg += getText(stroke, fontsize, fontname, rotation, position, - text, linespacing, justification) + svg += get_text(plane, techdraw, + stroke, fontsize, fontname, + rotation, position, text, + linespacing, justification) elif utils.get_type(obj) in ["Annotation", "DraftText", "Text"]: # returns an svg representation of a document annotation @@ -1007,7 +1070,10 @@ def getSVG(obj, r = obj.Placement.Rotation t = obj.Text j = obj.ViewObject.Justification - svg += getText(stroke,fontsize,n,r,p,t,linespacing,j) + svg += get_text(plane, techdraw, + stroke, fontsize, n, + r, p, t, + linespacing, j) elif utils.get_type(obj) == "Axis": # returns the SVG representation of an Arch Axis system @@ -1132,13 +1198,19 @@ def getSVG(obj, lspc = FreeCAD.Vector(obj.ViewObject.Proxy.header.translation.getValue().getValue()) p1 = p2.add(lspc) j = obj.ViewObject.TextAlign - t3 = getText(c,f1,n,a,get_proj(p1, plane),t1,linespacing,j,flip=True) + t3 = get_text(plane, techdraw, + c, f1, n, + a, get_proj(p1, plane), t1, + linespacing, j, flip=True) svg += t3 if t2: ofs = FreeCAD.Vector(0,-lspc.Length,0) if a: ofs = FreeCAD.Rotation(FreeCAD.Vector(0,0,1),-rotation).multVec(ofs) - t4 = getText(c,fontsize,n,a,get_proj(p1, plane).add(ofs),t2,linespacing,j,flip=True) + t4 = get_text(plane, techdraw, + c, fontsize, n, + a, get_proj(p1, plane).add(ofs), t2, + linespacing, j, flip=True) svg += t4 elif obj.isDerivedFrom('Part::Feature'):