Skip to content

Commit

Permalink
Fix pt and px units (#387)
Browse files Browse the repository at this point in the history
Fixed as reported as solved in #386.
  • Loading branch information
deeplook authored Oct 21, 2023
1 parent 473627a commit f207876
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions svglib/svglib.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,11 +320,11 @@ def convertLength(self, svgAttr, em_base=DEFAULT_FONT_SIZE, attr_name=None, defa
elif text.endswith("pc"):
return float(text[:-2]) * pica
elif text.endswith("pt"):
return float(text[:-2]) * 1.25
return float(text[:-2])
elif text.endswith("em"):
return float(text[:-2]) * em_base
elif text.endswith("px"):
return float(text[:-2])
return float(text[:-2]) * 0.75
elif text.endswith("ex"):
# The x-height of the text must be assumed to be 0.5em tall when the
# text cannot be measured.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,12 +313,12 @@ def test_length_conversion(self):
("1e1cm", 10*cm),
("1e1in", 10*inch),
("-8e-2cm", (-8e-2)*cm),
("20px", 20),
("20pt", 20 * 1.25),
("20px", 20 * 0.75),
("20pt", 20),
("1.5em", 12 * 1.5),
("10.5mm", 10.5*(cm*0.1)),
("3, 5 -7", [3, 5, -7]),
("2pt 12pt", [2 * 1.25, 12 * 1.25]),
("2pt 12pt", [2, 12]),
)
ac = svglib.Svg2RlgAttributeConverter()
failed = _testit(ac.convertLength, mapping)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def test_units_svg(self):
assert unit_widths == sorted(unit_widths)
unit_names = ["px", "pt", "mm", "ex", "ch", "em", "pc", "cm"]
lengths_by_name = dict(zip(unit_names, unit_widths))
assert lengths_by_name["px"] == 1 # 1px == 1px
assert lengths_by_name["px"] == lengths_by_name["pt"] * 0.75
assert lengths_by_name["em"] == svglib.DEFAULT_FONT_SIZE # 1 em == font size
assert lengths_by_name["ex"] == lengths_by_name["em"] / 2
assert lengths_by_name["ch"] == lengths_by_name["ex"]
Expand Down

0 comments on commit f207876

Please sign in to comment.