# SPDX-License-Identifier: LGPL-2.1-or-later # -*- coding: utf8 -*- # *************************************************************************** # * Copyright (c) 2009 Yorik van Havre * # * Copyright (c) 2018 George Shuklin (amarao) * # * Copyright (c) 2020 Eliud Cabrera Castillo * # * * # * This program is free software; you can redistribute it and/or modify * # * it under the terms of the GNU Lesser General Public License (LGPL) * # * as published by the Free Software Foundation; either version 2 of * # * the License, or (at your option) any later version. * # * for detail see the LICENCE text file. * # * * # * This program is distributed in the hope that it will be useful, * # * but WITHOUT ANY WARRANTY; without even the implied warranty of * # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * # * GNU Library General Public License for more details. * # * * # * You should have received a copy of the GNU Library General Public * # * License along with this program; if not, write to the Free Software * # * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * # * USA * # * * # *************************************************************************** """Provides functions to return the SVG representation of text elements.""" ## @package svgtext # \ingroup draftfunctions # \brief Provides functions to return the SVG representation of text elements. import math import FreeCAD as App import DraftVecUtils import draftutils.utils as utils ## \addtogroup draftfunctions # @{ def _get_text_techdraw(text, tcolor, fontsize, anchor, align, fontname, angle, base, linespacing): """Return the SVG representation of text for TechDraw display. `text` is a list of textual elements; they are iterated, styled, and added around a `` tag. :: text[0] text[1] """ svg = "" for i in range(len(text)): delta = App.Vector(0, -i * linespacing, 0) point = base + DraftVecUtils.rotate(delta, angle) _t = text[i].replace("&", "&") _t = _t.replace("<", "<") t = _t.replace(">", ">") svg += " tag with style options. The text must be added after this tag, and then must be closed. :: ... """ svg = " 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: # Compute an X vector vec = App.Vector(1, 0, 0) vec = angle.multVec(App.Vector(1, 0, 0)) angle = DraftVecUtils.angle(vec, plane.u) # 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: # For TechDraw display each item in the text list is placed # in an individual tag. # text[0] # text[1] svg = _get_text_techdraw( text, tcolor, fontsize, anchor, align, fontname, angle, base, linespacing ) else: # If the SVG is not for TechDraw, and there is a single item # in the text list, place it in a single tag. # text # # For multiple elements, place each element inside a tag. # # text[0] # text[1] # svg = _get_text_header(tcolor, fontsize, anchor, align, fontname, angle, base, flip) if len(text) == 1: _t = text[0].replace("&", "&").replace("<", "<") svg += _t.replace(">", ">") else: for i in range(len(text)): if i == 0: svg += "" else: svg += ''.format(linespacing) _t = text[i].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 ) ## @}