我正在使用Python中的svgwrite根据我的Tensorflow模型生成输出来输出模拟手写文本。 我目前的设置需要一个字符串数组来表示换行,但是生成的文本大小不一致,有时会在行末呈现出尴尬的间距,例如
。
是否可能将文本换行到单个长行中,当当前行达到给定的最大宽度时自动添加换行符? Google搜索将我带到了svgwrite页面,并建议使用TextArea,但给出的示例是HTML。
这是我的Python当前解决方案,可以输出上面的示例。
![this](https://i.imgur.com/IB21F6gl.png)
是否可能将文本换行到单个长行中,当当前行达到给定的最大宽度时自动添加换行符? Google搜索将我带到了svgwrite页面,并建议使用TextArea,但给出的示例是HTML。
def _draw(self, strokes, lines, filename, stroke_colors=None, \
stroke_widths=None, background_color='white'):
lines = [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit,",
"sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris",
"nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in",
"reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur."
]
stroke_colors = stroke_colors or ['black']*len(lines)
stroke_widths = stroke_widths or [2]*len(lines)
line_height = 35
view_width = 152.4
view_height = 101.6
dwg = svgwrite.Drawing(filename=filename)
dwg.viewbox(width=view_width, height=view_height)
dwg.add(dwg.rect(insert=(0, 0), size=('153mm', '102mm'), fill=background_color))
for i in range(3):
initial_coord = np.array([30,-((i*450)+25)])
strokesc = self._sample(lines, [1 for i in lines], [7 for i in lines]);
for offsets, line, color, width in zip(strokesc, lines, stroke_colors, stroke_widths):
if not line:
initial_coord[1] -= line_height
continue
offsets[:, :2] *= random.randint(150, 190)/100
strokesc = drawing.offsets_to_coords(offsets)
strokesc = drawing.denoise(strokesc)
strokesc[:, :2] = drawing.align(strokesc[:, :2])
strokesc[:, 1] *= -1
strokesc[:, :2] -= strokesc[:, :2].min() + initial_coord
prev_eos = 1.0
p = "M{},{} ".format(0, 0)
for x, y, eos in zip(*strokesc.T):
p += '{}{},{} '.format('M' if prev_eos == 1.0 else 'L', x, y)
prev_eos = eos
path = svgwrite.path.Path(p)
path = path.stroke(color=color, width=width, linecap='round').fill("none")
dwg.add(path)
initial_coord[1] -= line_height
dwg.save()
这是我的Python当前解决方案,可以输出上面的示例。
svgwrite
,但也许它有一些选项可以对给定的多行文本进行“对齐”?(例如,拉伸空格以实现相同的行长度) - raphaelsvgwrite
并更新了答案,提供了一个小的工作示例,展示了如何实际获取文本对齐。 - raphael