From 9051776ed1aff5a664d440bb74c45cabe869e6b4 Mon Sep 17 00:00:00 2001 From: Thomas Holder Date: Sat, 16 Dec 2023 20:27:39 +0100 Subject: [PATCH] Test: th2 round trip (create.th2) --- extensions/th2_output.py | 36 +++++++++++++++++++++++++++++++----- tests/data/create.th2 | 37 +++++++++++++++++++++++++++++++++++++ tests/data/create.xvi | 16 ++++++++++++++++ tests/data/label-align.th2 | 2 +- tests/test_th2.py | 8 +++++--- 5 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 tests/data/create.th2 create mode 100644 tests/data/create.xvi diff --git a/extensions/th2_output.py b/extensions/th2_output.py index 0ef1236..785927e 100755 --- a/extensions/th2_output.py +++ b/extensions/th2_output.py @@ -39,7 +39,33 @@ def orientation(mat): return 0.0 def fstr(x): + """ + Format float with 4 digits after the period + """ s = "%.4f" % x + return fstr_trim_zeros(s) + + +def fstr2(x: float, dbl_dig=15, max_dig=20) -> str: + """ + Format float with a maximum of max_dig digits after the period, and taking + the number of significant digits (dbl_dig) into account. + """ + try: + digits = dbl_dig - math.ceil(math.log10(abs(x))) + except ValueError: + digits = 0 + digits = max(0, min(digits, max_dig)) + s = f"{x:.{digits}f}" + if digits == 0: + s += ".0" + return fstr_trim_zeros(s) + + +def fstr_trim_zeros(s: str) -> str: + """ + Strip trailing zeros from a string that represents a floating point number. + """ i = len(s) - 1 while s[i] == '0': i -= 1 if s[i] == '.': i += 1 @@ -170,9 +196,9 @@ def output(self): print('encoding utf-8') if doc_width and doc_height: - print('##XTHERION## xth_me_area_adjust 0 0 %f %f' % ( - doc_width * th2pref.basescale, - doc_height * th2pref.basescale)) + print('##XTHERION## xth_me_area_adjust 0 0 %s %s' % ( + fstr2(doc_width * th2pref.basescale), + fstr2(doc_height * th2pref.basescale))) print('##XTHERION## xth_me_area_zoom_to 100') # text on path @@ -209,8 +235,8 @@ def output(self): continue if href.startswith('file://'): href = href[7:] - print('##XTHERION## xth_me_image_insert {%f 1 1.0} {%f %s} "%s" 0 {}' % \ - (paramsTrans[0], paramsTrans[1], XVIroot, href)) + print('##XTHERION## xth_me_image_insert {%s 1 1.0} {%s %s} "%s" 0 {}' % \ + (fstr2(paramsTrans[0]), fstr2(paramsTrans[1]), XVIroot, href)) self.print_scrap_begin('scrap1', not self.options.lay2scr) diff --git a/tests/data/create.th2 b/tests/data/create.th2 new file mode 100644 index 0000000..8f2de66 --- /dev/null +++ b/tests/data/create.th2 @@ -0,0 +1,37 @@ +encoding utf-8 +##XTHERION## xth_me_area_adjust 0 0 748.125 749.88 +##XTHERION## xth_me_area_zoom_to 100 +##XTHERION## xth_me_image_insert {456.253 1 1.0} {60.18 0@create.} "create.xvi" 0 {} + + + +scrap create.s -scale [-128.0 -128.0 620.125 -128.0 0.0 0.0 19.002375 0.0 m] + +line wall + 437.0 56.0 + 426.0 98.0 396.0 123.0 370.0 158.0 + 344.0 193.0 265.0 239.0 240.0 281.0 + 215.0 323.0 218.0 344.0 164.0 333.0 + 110.0 322.0 78.0 227.0 55.0 319.0 + 32.0 411.0 215.0 393.0 150.0 481.0 +endline + +point 60.842 321.52 station -name 1@sub.create + +point 199.612 418.56 station -name 0@sub.create + +point 290.952 288.05 station -name 2@create + +point 433.082 135.69 station -name 1@create + +point 456.502 59.12 station -name 0@create + +line wall + 470.0 62.0 + 460.71 184.57 359.0 236.0 309.0 289.0 + 259.0 342.0 175.0 473.0 165.0 490.0 +endline + +endscrap + + diff --git a/tests/data/create.xvi b/tests/data/create.xvi new file mode 100644 index 0000000..baa404c --- /dev/null +++ b/tests/data/create.xvi @@ -0,0 +1,16 @@ +set XVIgrids {1.0 m} +set XVIstations { + { 197.83 -179.72 0@create.} + { 174.41 -103.15 1@create.} + { 32.28 49.21 2@create.} + { -59.06 179.72 3@create.} + { -197.83 82.68 1@sub.create.} + { -59.06 179.72 0@sub.create.} +} +set XVIshots { + { 197.83 -179.72 174.41 -103.15} + { 174.41 -103.15 32.28 49.21} + { 32.28 49.21 -59.06 179.72} + { -59.06 179.72 -197.83 82.68} +} +set XVIgrid {-254.921 -238.78 19.685 0.0 0.0 19.685 26 24} diff --git a/tests/data/label-align.th2 b/tests/data/label-align.th2 index dc10aee..66cad6e 100644 --- a/tests/data/label-align.th2 +++ b/tests/data/label-align.th2 @@ -1,5 +1,5 @@ encoding utf-8 -##XTHERION## xth_me_area_adjust 0 0 290.000000 260.000000 +##XTHERION## xth_me_area_adjust 0 0 290.0 260.0 ##XTHERION## xth_me_area_zoom_to 100 diff --git a/tests/test_th2.py b/tests/test_th2.py index a60b6bc..6051a18 100644 --- a/tests/test_th2.py +++ b/tests/test_th2.py @@ -16,7 +16,7 @@ def _find_script(name: str) -> str: def _non_empty_lines(buf: AnyStr) -> list[AnyStr]: - return [line for line in buf.splitlines() if line] + return [line.rstrip() for line in buf.splitlines() if line] def _assert_non_empty_lines_equal(left: AnyStr, right: AnyStr): @@ -29,9 +29,11 @@ def _assert_non_empty_lines_equal(left: AnyStr, right: AnyStr): @pytest.mark.parametrize("stem", [ ("label-align"), + ("create"), ]) -def test_th2_round_trip(stem): - path_input = TESTS_DATA / f"{stem}.th2" +def test_th2_round_trip(stem, monkeypatch): + monkeypatch.chdir(TESTS_DATA) + path_input = Path(f"{stem}.th2") svgcontent = subprocess.check_output([ sys.executable, script_th2_input,