Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions pytiled_parser/parsers/tmx/tileset.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ def _parse_tile(
else:
tile.image = Path(image_element.attrib["source"])

tile.image_width = int(image_element.attrib["width"])
tile.width = tile.image_width
tile.image_height = int(image_element.attrib["height"])
tile.height = tile.image_height
if image_element.attrib.get("width") is not None:
tile.image_width = int(image_element.attrib["width"])
tile.width = tile.image_width
if image_element.attrib.get("height") is not None:
tile.image_height = int(image_element.attrib["height"])
tile.height = tile.image_height

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

tileset.image_width = int(image_element.attrib["width"])
tileset.image_height = int(image_element.attrib["height"])
if image_element.attrib.get("width") is not None:
tileset.image_width = int(image_element.attrib["width"])
if image_element.attrib.get("height") is not None:
tileset.image_height = int(image_element.attrib["height"])

if image_element.attrib.get("trans") is not None:
my_string = image_element.attrib["trans"]
Expand Down
58 changes: 58 additions & 0 deletions tests/test_optional_image_dimensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""Image dimensions are optional in TSX, just as in JSON tilesets."""

import json
import xml.etree.ElementTree as etree
from pathlib import Path

import pytest

from pytiled_parser import parse_tileset


@pytest.mark.parametrize("format_", ["json", "tsx"])
@pytest.mark.parametrize("collection", [False, True])
@pytest.mark.parametrize(
"dimensions", [{}, {"width": 32}, {"height": 16}, {"width": 32, "height": 16}]
)
def test_optional_image_dimensions(tmp_path, format_, collection, dimensions):
attributes = {
"name": "test",
"spacing": 0,
"margin": 0,
"tilecount": 1,
"tilewidth": 32,
"tileheight": 16,
"columns": 0 if collection else 1,
}
filename = tmp_path / ("tileset." + format_)
if format_ == "json":
image = {"image": "tile.png"}
image.update({"image" + key: value for key, value in dimensions.items()})
if collection:
attributes["tiles"] = [dict(image, id=0)]
else:
attributes.update(image)
filename.write_text(json.dumps(attributes), encoding="utf-8")
else:
root = etree.Element(
"tileset", {key: str(value) for key, value in attributes.items()}
)
parent = etree.SubElement(root, "tile", id="0") if collection else root
etree.SubElement(
parent,
"image",
source="tile.png",
**{key: str(value) for key, value in dimensions.items()},
)
etree.ElementTree(root).write(filename, encoding="utf-8")

tileset = parse_tileset(filename)
image = tileset.tiles[0] if collection else tileset
assert image.image == Path("tile.png")
assert image.image_width == dimensions.get("width")
assert image.image_height == dimensions.get("height")
if collection:
assert image.width == dimensions.get("width", 0)
assert image.height == dimensions.get("height", 0)
assert tileset.tile_width == 32
assert tileset.tile_height == 16