Skip to content

Commit

Permalink
Improve th2_output <-> xtherion likeness
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed Feb 7, 2024
1 parent cb1e312 commit 971afe2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
23 changes: 19 additions & 4 deletions extensions/th2_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ def fstr_trim_zeros(s: str) -> str:
return "0.0" if s == "-0.0" else s


def format_options_leading_space(options):
"""
Like format_options() but with a leading space if non-empty.
"""
formatted = format_options(options)
if formatted:
return " " + formatted
return ""


class Th2Line:
def __init__(self, type = 'wall'):
self.type = type
Expand All @@ -92,7 +102,8 @@ def close(self):
self._last.split()[-2:] != self.points[0].split()):
self.points.append(self.points[0])
def output(self):
print_utf8("line %s %s" % (self.type, format_options(self.options)))
formatted_options = format_options_leading_space(self.options)
print_utf8(f"line {self.type}{formatted_options}")
print(" " + "\n ".join(self.points))
print("endline\n")

Expand Down Expand Up @@ -126,7 +137,8 @@ def output(self, prefix):
line.output()

# output area
print_utf8("area %s %s" % (self.type, format_options(self.options)))
formatted_options = format_options_leading_space(self.options)
print_utf8(f"area {self.type}{formatted_options}")
for lineid in ids:
print_utf8(" " + lineid)
print("endarea\n")
Expand Down Expand Up @@ -172,7 +184,7 @@ def print_scrap_begin(self, id, test, options = {}):

def print_scrap_end(self, test):
if test:
print("endscrap\n")
print("endscrap\n\n")

def output(self):
root = self.document.getroot()
Expand Down Expand Up @@ -241,6 +253,8 @@ def output(self):
print('##XTHERION## xth_me_image_insert {%s 1 1.0} {%s %s} "%s" 0 {}' % \
(fstr2(paramsTrans[0]), fstr2(paramsTrans[1]), XVIroot, href))

print('\n')

self.print_scrap_begin('scrap1', not self.options.lay2scr)

if self.options.layers == 'current':
Expand Down Expand Up @@ -514,7 +528,8 @@ def output_point(self, node):
options['orientation'] = orient

# output in therion format
print_utf8("point %s %s %s %s" % (fstr(params[0]), fstr(params[1]), type, format_options(options)))
formatted_options = format_options_leading_space(options)
print_utf8("point %s %s %s%s\n" % (fstr(params[0]), fstr(params[1]), type, formatted_options))

def output_area(self, node):
mat = self.i2d_affine(node)
Expand Down
12 changes: 11 additions & 1 deletion extensions/th2ex.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,16 @@ def parse_options_new(a):

parse_options = parse_options_new


def key_options_item(item: tuple) -> tuple:
"""
Sort key for options. Alphabetic with some exceptions.
"""
if item[0] == 'id':
return '0' + item[0], item[1]
return item


# TODO this fails for -text "[foo bar]" (will be -text [foo bar], no quotes)
def format_options(options):
'''
Expand Down Expand Up @@ -334,7 +344,7 @@ def format_option(key, value):
return ret

ret = []
for key,value in sorted(options.items()):
for key,value in sorted(options.items(), key=key_options_item):
if key in repeatable_options and isinstance(value, list):
for v in value:
ret.append(format_option(key, v))
Expand Down

0 comments on commit 971afe2

Please sign in to comment.