File size: 7,399 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | # SPDX-License-Identifier: LGPL-2.1-or-later
# -*- coding: utf8 -*-
# ***************************************************************************
# * Copyright (c) 2009 Yorik van Havre <yorik@uncreated.net> *
# * Copyright (c) 2018 George Shuklin (amarao) *
# * Copyright (c) 2020 Eliud Cabrera Castillo <e.cabrera-castillo@tum.de> *
# * *
# * 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 `<text>` tag.
::
<text ...> text[0] </text>
<text ...> text[1] </text>
"""
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 += "<text "
svg += 'stroke-width="0" stroke="{}" '.format(tcolor)
svg += 'fill="{}" font-size="{}" '.format(tcolor, fontsize)
svg += 'style="text-anchor:{};text-align:{};'.format(anchor, align.lower())
svg += 'font-family:{}" '.format(fontname)
svg += 'transform="'
svg += "rotate({},{},{}) ".format(math.degrees(angle), point.x, point.y)
svg += "translate({},{}) ".format(point.x, point.y)
svg += 'scale(1,-1)"'
# svg += 'freecad:skip="1"'
svg += ">\n"
svg += t
svg += "</text>\n"
return svg
def _get_text_header(tcolor, fontsize, anchor, align, fontname, angle, base, flip):
"""Return the initial <text> tag with style options.
The text must be added after this tag, and then must be closed.
::
<text ...>
...
</text>
"""
svg = "<text "
svg += 'stroke-width="0" stroke="{}" '.format(tcolor)
svg += 'fill="{}" font-size="{}" '.format(tcolor, fontsize)
svg += 'style="text-anchor:{};text-align:{};'.format(anchor, align.lower())
svg += 'font-family:{}" '.format(fontname)
svg += 'transform="'
svg += "rotate({},{},{}) ".format(math.degrees(angle), base.x, base.y)
if flip:
svg += "translate({},{}) ".format(base.x, base.y)
else:
svg += "translate({},{}) ".format(base.x, -base.y)
# svg += 'scale({},-{}) '.format(tmod/2000, tmod/2000)
if flip:
svg += "scale(1,-1) "
else:
svg += "scale(1,1) "
svg += '" '
svg += 'freecad:skip="1"'
svg += ">\n"
return svg
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, App.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:
# 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 ...> text[0] </text>
# <text ...> text[1] </text>
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 ...> text </text>
#
# For multiple elements, place each element inside a <tspan> tag.
# <text ...>
# <tspan>text[0]</tspan>
# <tspan>text[1]</tspan>
# </text>
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 += "<tspan>"
else:
svg += '<tspan x="0" dy="{}">'.format(linespacing)
_t = text[i].replace("&", "&").replace("<", "<")
svg += _t.replace(">", ">")
svg += "</tspan>\n"
svg += "</text>\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
)
## @}
|