Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
DocLabyrinth opened this issue May 22, 2015 · 0 comments

Comments

@DocLabyrinth
Copy link

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
        },
    }
...
}
sprites@2x.json (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@557e6dd

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant