Skip to content

Commit

Permalink
Replace the more_text function with an add_text method
Browse files Browse the repository at this point in the history
  • Loading branch information
spakin committed Jan 30, 2022
1 parent 794d8be commit cb09e34
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 48 deletions.
12 changes: 6 additions & 6 deletions examples/text_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
232, 0, 300, 68, 300, 150],
transform='translate(%.5g, %.5g)' % (width/2 - 150, height/2 - 75),
fill='#380000', stroke='none')
text('The sun sets early, ', (0, 0),
font_family='"Zapf Chancery", "TeX Gyre Chorus", cursive',
text_align='justify', font_size='24px', fill='#fdaa30',
shape_inside=sun)
more_text('crimson rays obscured by clouds; ', fill='#fb6854')
more_text('a cold darkness spreads.', fill='#fd4539')
t = text('The sun sets early, ', (0, 0),
font_family='"Zapf Chancery", "TeX Gyre Chorus", cursive',
text_align='justify', font_size='24px', fill='#fdaa30',
shape_inside=sun)
t.add_text('crimson rays obscured by clouds; ', fill='#fb6854')
t.add_text('a cold darkness spreads.', fill='#fd4539')
42 changes: 19 additions & 23 deletions simple_inkscape_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,23 @@ def append(self, other):
return self


class SimpleTextObject(SimpleObject):
'''A SimpleTextObject is a SimpleObject to which additional text can
be added.'''

def add_text(self, msg, base=None, **style):
'''Append text, possibly at a non-adjacent position and possibly
with a different style.'''
tspan = inkex.Tspan()
tspan.text = msg
tspan.style = self._construct_style({}, style)
if base is not None:
tspan.set('x', _python_to_svg_str(base[0]))
tspan.set('y', _python_to_svg_str(base[1]))
self._inkscape_obj.append(tspan)
return self


class SimpleMarker(SimpleObject):
'Represent a path marker, which wraps an arbitrary object.'

Expand Down Expand Up @@ -1552,29 +1569,8 @@ def text(msg, base, path=None, transform=None, conn_avoid=False,
tp = obj.add(inkex.TextPath())
tp.href = path._inkscape_obj.get_id()

# Wrap the text object within a SimpleObject.
return SimpleObject(obj, transform, conn_avoid, clip_path, {}, style)


def more_text(msg, base=None, conn_avoid=False, **style):
'Append text to the preceding object, which must be text.'
global _simple_top
try:
obj = _simple_top.last_obj()
except IndexError:
_abend(_('more_text must immediately follow'
' text or another more_text'))
if not isinstance(obj._inkscape_obj, inkex.TextElement):
_abend(_('more_text must immediately follow'
' text or another more_text'))
tspan = inkex.Tspan()
tspan.text = msg
tspan.style = obj._construct_style({}, style)
if base is not None:
tspan.set('x', _python_to_svg_str(base[0]))
tspan.set('y', _python_to_svg_str(base[1]))
obj._inkscape_obj.append(tspan)
return obj
# Wrap the text object within a SimpleTextObject.
return SimpleTextObject(obj, transform, conn_avoid, clip_path, {}, style)


def image(fname, ul, embed=True, transform=None, conn_avoid=False,
Expand Down
19 changes: 13 additions & 6 deletions svg_to_simp_ink_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -426,26 +426,33 @@ def convert_text(self, node):
# Convert all sub-text objects. We assume that <tspan> tags are
# not nested. (SVG allows this, but it's not currently supported
# by Simple Inkscape Scripting.)
var_name = self.Statement.id2var(node.get_id())
need_var_name = False
for tspan in [c for c in node.iter() if c.tag[-5:] == 'tspan']:
if tspan.text is not None:
# The text within a <tspan> can have a specified position
# and style.
need_var_name = True
x, y = tspan.get('x'), tspan.get('y')
extra, extra_deps = self.extra_args(tspan, {})
all_deps = all_deps.union(extra_deps)
if x is not None and y is not None:
# Specified position
code.append('more_text(%s, (%s, %s)%s)' %
(repr(tspan.text), x, y, extra))
code.append('%s.add_text(%s, (%s, %s)%s)' %
(var_name, repr(tspan.text), x, y, extra))
else:
# Unspecified position
code.append('more_text(%s%s)' %
(repr(tspan.text), extra))
code.append('%s.add_text(%s%s)' %
(var_name, repr(tspan.text), extra))
if tspan.tail is not None:
# The text following a <tspan> has neither a specified
# position nor style.
code.append('more_text(%s)' % repr(tspan.tail))
return self.Statement(code, node.get_id(), sorted(all_deps))
need_var_name = True
code.append('%s.add_text(%s)' % (var_name, repr(tspan.tail)))
stmt = self.Statement(code, node.get_id(), sorted(all_deps))
if need_var_name:
stmt.need_var_name = True
return stmt

def convert_image(self, node):
'Return Python code for including an image.'
Expand Down
20 changes: 10 additions & 10 deletions tests/data/refs/svg_to_simp_ink_script.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ path(['M', 100, 600, 200, 700, 300, 600, 400, 700], display='inline', stroke_wid
path(['m', 500, 600, 'c', 0, 0, 0, 100, 100, 100, 100, 0, 0, -100, 100, -100, 100, 0, 100, 100, 100, 100], display='inline', stroke_width=10)
s1 = star(5, (189.03001, 847.93945), (69.364868, 34.682434), angles=(0.63598373, 1.2643023), stroke='#008000', fill='#ffff00', display='inline', stroke_width=10)
clone(s1, transform='translate(200, 2.99622)', display='inline')
text('', (100, 100), font_size='14.66666698px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
more_text('Hello World', (100, 100))
t1 = text('', (100, 100), font_size='14.66666698px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
t1.add_text('Hello World', (100, 100))
rect((105.71, 219.66), (370.28, 481.82), stroke='none', fill='black')
text('', (200, 100), font_size='14.66666698px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
more_text('UPPER', (200, 100), font_size='14.66666698px')
text('', (300, 100), font_size='10.58333302px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
more_text('Multi line', (300, 100), font_size='14.66666698px')
more_text('text', (300, 118.33334), font_size='14.66666698px')
more_text('FOO', (300, 136.66667), font_size='14.66666698px')
t2 = text('', (200, 100), font_size='14.66666698px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
t2.add_text('UPPER', (200, 100), font_size='14.66666698px')
t3 = text('', (300, 100), font_size='10.58333302px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
t3.add_text('Multi line', (300, 100), font_size='14.66666698px')
t3.add_text('text', (300, 118.33334), font_size='14.66666698px')
t3.add_text('FOO', (300, 136.66667), font_size='14.66666698px')
text3762 = text('', (54.289616, 229.64807), font_size='10.58333302px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
more_text('Grouped', (54.289616, 229.64807), font_size='14.66666698px')
text3762.add_text('Grouped', (54.289616, 229.64807), font_size='14.66666698px')
text3766 = text('', (54.289619, 259.64807), font_size='14.66666698px', line_height=1.25, font_family='sans-serif', fill='#000000', stroke='none')
more_text('text', (54.289619, 259.64807), font_size='14.66666698px')
text3766.add_text('text', (54.289619, 259.64807), font_size='14.66666698px')
group([text3762, text3766], transform='translate(445.71, -129.648)')
rect((100, 200), (200, 300), stroke='none', fill='#000080')
rect((300, 200), (500, 300), stroke='#ff0000', stroke_width=16)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_simple_inkscape_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ def make_rect(center, fill, edge=100):
''',),
("--program=text('Simple Inkscape Scripting', (0, height), font_size='36pt')",),
('''--program=
text('Hello, ', (width/2, height/2), font_size='24pt', text_anchor='middle')
more_text('Inkscape', font_weight='bold', fill='#800000')
more_text('!!!')
t = text('Hello, ', (width/2, height/2), font_size='24pt', text_anchor='middle')
t.add_text('Inkscape', font_weight='bold', fill='#800000')
t.add_text('!!!')
''',),
("--program=image('https://media.inkscape.org/static/images/inkscape-logo.png', (0, 0), embed=False)",),

Expand Down

0 comments on commit cb09e34

Please sign in to comment.