glue icon indicating copy to clipboard operation
glue copied to clipboard

JSON output for ratio images is incorrect in CAAT JSON (and possibly others)

Open DocLabyrinth opened this issue 9 years ago • 0 comments

When the CAAT JSON format generator is run with an option which generates multiple ratios, the images are correctly generated but the resulting output JSON always reflects the 1.0 ratio.

Command
glue sprites sprites-out --caat --retina
sprites.json
{
    "meta": {
        "width": 471, 
        "sprite_filename": "sprites.png", 
        "version": "0.11.1", 
        "hash": "1bcee0c2d7", 
        "height": 469
    }, 
    "sprites": {
        "308.png": {
            "y": 164, 
            "x": 133, 
            "height": 11, 
            "width": 17
        },
    }
...
}
[email protected] (should be twice the size)
{
    "meta": {
        "width": 471, 
        "sprite_filename": "sprites.png", 
        "version": "0.11.1", 
        "hash": "1bcee0c2d7", 
        "height": 469
    }, 
    "sprites": {
        "308.png": {
            "y": 164, 
            "x": 133, 
            "height": 11, 
            "width": 17
        },
    }
...
}

The reason is that the code which generates the dictionary to be serialised as JSON is passed a dictionary generated by BaseTextFormat#get_context(), which contains the information about the ratio adjustments for the image and each sprite, but it does not use this information.

    # https://github.com/jorgebastida/glue/blob/master/glue/formats/caat.py#L24-35
    def get_context(self, *args, **kwargs):
        context = super(CAATFormat, self).get_context(*args, **kwargs)

        # context['ratios'][ kwargs['ratio'] ] has the metadata
        data = dict(sprites={}, meta={'version': context['version'],
                                      'hash': context['hash'],
                                      'sprite_filename': context['sprite_filename'],
                                      'width': context['width'],
                                      'height': context['height']})
        for i in context['images']:
            # context['ratios'][ kwargs['ratio'] ] has the image info
            data['sprites'][i['filename']] = {"x" : i['abs_x'],
                                              "y" : i['abs_y'],
                                              "width" : i['width'],
                                              "height" : i['height']}

Because the CAATFormat#get_context function is invoked once per ratio, with the ratio passed as a keyword argument, the issue can be fixed by inspecting the keyword argument and choosing the right ratio-adjusted values, or just proceeding as usual if the ratio argument isn't provided. An example of this can be seen in a pull request I submitted recently: DocLabyrinth/glue@557e6dd46b8c52cd80c7fa4219b30a544280cd77

The tests for the CAAT format just check that one expected key is a dictionary without checking the format or values of the generated json. It's worth making the testing more thorough when addressing this issue.

DocLabyrinth avatar May 22 '15 08:05 DocLabyrinth