-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrender_stroke_font_text.py
251 lines (188 loc) · 9.85 KB
/
render_stroke_font_text.py
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Inkscape extension to render text with the stroke fonts.
The path data used to render the text is generated by the
'Generate Font Data' extension
The original concept is from Hershey Text extension (Copyright 2011, Windell H. Oskay),
that comes bundled with Inkscape
This tool extends it with a number of rendering options like flow in boxes,
text alignment and char & line spacing
Copyright 2019 Shrinivas Kulkarni
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""
from inkex import addNS, NSS, Effect, errormsg
import os, sys
from simplepath import parsePath, translatePath, formatPath
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from stroke_font_common import CommonDefs, InkscapeCharDataFactory, getEtree, getAddFnTypes
from stroke_font_common import computePtInNode, getDecodedChars
from stroke_font_common import getViewCenter, runEffect, getSelectedElements
from stroke_font_common import formatStyle, getCharStyle, getTranslatedPath, getCurrentLayer
from stroke_font_manager import DrawContext
class InkscapeFontRenderer:
def __init__(self, layer, vc, strokeWidth):
self.layer = layer
self.g = None
self.currG = None
self.vc = computePtInNode(vc, layer)
self.strokeWidth = strokeWidth
self.box = None
def renderChar(self, charData, x, y, naChar):
d = charData.pathStr
style = getCharStyle(self.strokeWidth, naChar)
d = getTranslatedPath(d, x, y)
attribs = {'style':formatStyle(style), 'd':d}
getEtree().SubElement(self.currG, addNS('path','svg'), attribs)
def beforeRender(self):
self.g = getEtree().SubElement(self.layer, 'g')
self.currG = self.g
def newBoxToBeRendered(self, box, addPlane):
boxG = getEtree().SubElement(self.layer, 'g')
if('transform' in box.attrib):
boxG.set('transform', box.get('transform'))
box.getparent().remove(box)
# order important!
self.g.append(box)
self.g.append(boxG)
self.currG = boxG
self.box = box
def moveBoxInYDir(self, moveBy):
t = 'translate(' + str(0) + ',' + str(moveBy) + ')'
self.currG.set('transform', t)
def centerInView(self, width, height):
t = 'translate(' + str(self.vc[0] - width) + ',' + str(self.vc[1] - height) + ')'
self.g.set('transform', t)
def renderPlainText(self, text, size, x = None, y = None, objName = None):
textStyle = {'fill':'#000000', 'fill-opacity':'1'}
textStyle['font-size'] = str(size)
attribs = {'style':formatStyle(textStyle)}
if(x != None and y != None):
attribs['transform'] = 'translate(' + str(x) + ', '+ str(y)+')'
textElem = getEtree().SubElement(self.g, addNS('text','svg'), attribs)
textElem.text = text
def getBoxLeftTopRightBottom(self, box):
x1 = float(box.get('x'))
y1 = float(box.get('y'))
w = float(box.get('width'))
h = float(box.get('height'))
x2 = w + x1
y2 = h + y1
return x1, y1, x2, y2
def getBoxFromCoords(self, x1, y1, x2, y2):
attribs = {'x':str(x1), 'y':str(y1), 'width':str(x2-x1), 'height':str(y2-y1)}
attribs['style'] = self.box.get('style')
self.box = getEtree().SubElement(self.layer, addNS('rect','svg'), attribs)
return self.box
def getDefaultStartLocation(self):
return 0, 0
class RenderStrokeFontText(Effect):
def __init__( self ):
Effect.__init__( self )
addFn, typeFloat, typeInt, typeString, typeBool = getAddFnTypes(self)
addFn( '--tab', action = 'store', type = typeString, dest = 'tab', \
default = 'splash', help = 'The active tab when Apply was pressed')
addFn( '--action', action = 'store', type = typeString, dest = 'action', \
default = 'render', help = 'The active option when Apply was pressed' )
addFn( '--text', action = 'store', type = typeString, dest = 'text', \
default = 'Hello World', help = 'The input text to render')
addFn( '--filePath', action = 'store', type = typeString, dest = 'filePath', \
default = '', help = 'Complete path of the text file')
addFn( '--fontName', action = 'store', type = typeString, dest = 'fontName', \
default = 'Script', help = 'The custom font to be used for rendering')
addFn('--fontSize', action = 'store', type = typeFloat, dest = 'fontSize', \
default = '100', help = 'Size of the font')
addFn('--charSpacing', action = 'store', type = typeFloat, dest = 'charSpacing', \
default = '1', help = 'Spacing between characters')
addFn('--wordSpacing', action = 'store', type = typeFloat, dest = 'wordSpacing', \
default = '1', help = 'Spacing between words')
addFn('--lineSpacing', action = 'store', type = typeFloat, dest = 'lineSpacing', \
default = '1.5', help = 'Spacing between the lines')
addFn('--strokeWidthMult', action = 'store', type = typeFloat, dest = 'strokeWidthMult', \
default = '1', help = 'Stroke Width Proportion')
addFn('--flowInBox', action = 'store', type = typeBool, dest = 'flowInBox', \
default = False, help = 'Fit the text in the selected rectangle objects')
addFn('--margin', action = 'store', type = typeFloat, dest = 'margin', default = '.1', \
help = 'Inside margin of text within the box')
addFn( '--hAlignment', action='store', type = typeString, dest = 'hAlignment', \
default = 'left', help='Horizontal text alignment within the box')
addFn( '--vAlignment', action='store', type = typeString, dest = 'vAlignment', \
default = 'none', help='Vertical text alignment within the box')
addFn( '--expandDir', action='store', type = typeString, dest = 'expandDir', \
default = 'x', help='Create new rectangles if text doesn\'t fit the selected ones')
addFn('--expandDist', action = 'store', type = typeFloat, dest = 'expandDist', \
default = True, help = 'Offset distance between the newly created rectangles')
def effect(self):
fontName = self.options.fontName
fontSize = self.options.fontSize
filePath = self.options.filePath
action = self.options.action
strokeWidthMult = self.options.strokeWidthMult
if(action == "renderTable"):
charSpacing = 1
wordSpacing = 1
lineSpacing = 2
else:
charSpacing = self.options.charSpacing
wordSpacing = self.options.wordSpacing
lineSpacing = self.options.lineSpacing
flowInBox = self.options.flowInBox
margin = self.options.margin
hAlignment = self.options.hAlignment
vAlignment = self.options.vAlignment
expandDir = self.options.expandDir
expandDist = self.options.expandDist
if(expandDir == 'none'):
expandDir = None
expandDist = None
extPath = os.path.dirname(os.path.abspath(__file__))
strokeWidth = 0.02 * fontSize * strokeWidthMult
layer = getCurrentLayer(self)
renderer = InkscapeFontRenderer(layer, getViewCenter(self), strokeWidth)
context = DrawContext(extPath, fontName, fontSize, \
charSpacing, wordSpacing, lineSpacing, InkscapeCharDataFactory(), renderer)
if(not context.fontHasGlyphs()):
errormsg('No font data; please select a font and ' + \
'ensure that the list is synchronized')
return
if(action == "renderTable"):
context.renderGlyphTable()
return
if(action == "renderText"):
text = self.options.text
text = text.replace('\\n','\n').replace('\\\n','\\n')
text = getDecodedChars(text)
elif(action == "renderFile"):
try:
readmode = 'rU' if CommonDefs.pyVer == 2 else 'r'
with open(filePath, readmode) as f:
text = f.read()
if(CommonDefs.pyVer == 2): text = unicode(text, 'utf-8')
except Exception as e:
errormsg("Error reading the file specified in Text File input box."+ str(e))
return
if(text[0] == u'\ufeff'):
text = text[1:]
if(flowInBox == True):
selElems = getSelectedElements(self)
selIds = [selElems[key].get('id') for key in selElems.keys()]
rectNodes = [r for r in self.document.xpath('//svg:rect', \
namespaces=NSS) if r.get('id') in selIds]
if(len(rectNodes) == 0):
errormsg(_("No rectangle objects selected."))
return
context.renderCharsInSelBoxes(text, rectNodes, margin, hAlignment, vAlignment, \
False, expandDir, expandDist)
else:
context.renderCharsWithoutBox(text)
runEffect(RenderStrokeFontText())