Skip to content

Commit b946961

Browse files
Allow omitted image dimensions in TSX tilesets
AI-assisted by Codex (GPT-6).
1 parent 00691fa commit b946961

2 files changed

Lines changed: 68 additions & 6 deletions

File tree

‎pytiled_parser/parsers/tmx/tileset.py‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,12 @@ def _parse_tile(
108108
else:
109109
tile.image = Path(image_element.attrib["source"])
110110

111-
tile.image_width = int(image_element.attrib["width"])
112-
tile.width = tile.image_width
113-
tile.image_height = int(image_element.attrib["height"])
114-
tile.height = tile.image_height
111+
if image_element.attrib.get("width") is not None:
112+
tile.image_width = int(image_element.attrib["width"])
113+
tile.width = tile.image_width
114+
if image_element.attrib.get("height") is not None:
115+
tile.image_height = int(image_element.attrib["height"])
116+
tile.height = tile.image_height
115117

116118
if raw_tile.attrib.get("x") is not None:
117119
tile.x = int(raw_tile.attrib["x"])
@@ -181,8 +183,10 @@ def parse(
181183
else:
182184
tileset.image = Path(image_element.attrib["source"])
183185

184-
tileset.image_width = int(image_element.attrib["width"])
185-
tileset.image_height = int(image_element.attrib["height"])
186+
if image_element.attrib.get("width") is not None:
187+
tileset.image_width = int(image_element.attrib["width"])
188+
if image_element.attrib.get("height") is not None:
189+
tileset.image_height = int(image_element.attrib["height"])
186190

187191
if image_element.attrib.get("trans") is not None:
188192
my_string = image_element.attrib["trans"]
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
"""Image dimensions are optional in TSX, just as in JSON tilesets."""
2+
3+
import json
4+
import xml.etree.ElementTree as etree
5+
from pathlib import Path
6+
7+
import pytest
8+
9+
from pytiled_parser import parse_tileset
10+
11+
12+
@pytest.mark.parametrize("format_", ["json", "tsx"])
13+
@pytest.mark.parametrize("collection", [False, True])
14+
@pytest.mark.parametrize(
15+
"dimensions", [{}, {"width": 32}, {"height": 16}, {"width": 32, "height": 16}]
16+
)
17+
def test_optional_image_dimensions(tmp_path, format_, collection, dimensions):
18+
attributes = {
19+
"name": "test",
20+
"spacing": 0,
21+
"margin": 0,
22+
"tilecount": 1,
23+
"tilewidth": 32,
24+
"tileheight": 16,
25+
"columns": 0 if collection else 1,
26+
}
27+
filename = tmp_path / ("tileset." + format_)
28+
if format_ == "json":
29+
image = {"image": "tile.png"}
30+
image.update({"image" + key: value for key, value in dimensions.items()})
31+
if collection:
32+
attributes["tiles"] = [dict(image, id=0)]
33+
else:
34+
attributes.update(image)
35+
filename.write_text(json.dumps(attributes), encoding="utf-8")
36+
else:
37+
root = etree.Element(
38+
"tileset", {key: str(value) for key, value in attributes.items()}
39+
)
40+
parent = etree.SubElement(root, "tile", id="0") if collection else root
41+
etree.SubElement(
42+
parent,
43+
"image",
44+
source="tile.png",
45+
**{key: str(value) for key, value in dimensions.items()},
46+
)
47+
etree.ElementTree(root).write(filename, encoding="utf-8")
48+
49+
tileset = parse_tileset(filename)
50+
image = tileset.tiles[0] if collection else tileset
51+
assert image.image == Path("tile.png")
52+
assert image.image_width == dimensions.get("width")
53+
assert image.image_height == dimensions.get("height")
54+
if collection:
55+
assert image.width == dimensions.get("width", 0)
56+
assert image.height == dimensions.get("height", 0)
57+
assert tileset.tile_width == 32
58+
assert tileset.tile_height == 16

0 commit comments

Comments
 (0)