Skip to content

Commit

Permalink
Test: th2 round trip (create.th2)
Browse files Browse the repository at this point in the history
  • Loading branch information
speleo3 committed Dec 16, 2023
1 parent ec6ee6c commit 9051776
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 9 deletions.
36 changes: 31 additions & 5 deletions extensions/th2_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 37 additions & 0 deletions tests/data/create.th2
Original file line number Diff line number Diff line change
@@ -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 [email protected]

point 199.612 418.56 station -name [email protected]

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


16 changes: 16 additions & 0 deletions tests/data/create.xvi
Original file line number Diff line number Diff line change
@@ -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 [email protected].}
{ -59.06 179.72 [email protected].}
}
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}
2 changes: 1 addition & 1 deletion tests/data/label-align.th2
Original file line number Diff line number Diff line change
@@ -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


Expand Down
8 changes: 5 additions & 3 deletions tests/test_th2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
Expand Down

0 comments on commit 9051776

Please sign in to comment.