~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/spriteutils.py

  • Committer: Edwin Grubbs
  • Date: 2010-02-03 05:18:21 UTC
  • mto: This revision was merged to the branch mainline in revision 10304.
  • Revision ID: edwin.grubbs@canonical.com-20100203051821-p7lbeyzh8il3m5ol
Completely working.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import Image
22
22
import simplejson
23
23
 
24
 
MARGIN = 18
25
 
 
26
24
class SpriteUtil:
27
 
    def __init__(self):
 
25
    def __init__(self, margin=150):
28
26
        self.sprites = None
29
27
        self.combined_image = None
30
28
        self.positions = None
31
29
        self.css_object = None
 
30
        self.margin=margin
32
31
 
33
32
    def loadCSSTemplate(self, css_file, group_name,
34
33
                        url_prefix_substitutions=None):
86
85
            max_width = max(width, max_width)
87
86
            total_height += height
88
87
 
89
 
        master_width = max_width
90
88
        # Separate each image with lots of whitespace.
91
 
        master_height = total_height + (MARGIN * len(self.sprites))
 
89
        combined_image_height = (
 
90
            total_height + (self.margin * len(self.sprites)))
92
91
        transparent = (0,0,0,0)
93
 
        master = Image.new(
 
92
        combined_image = Image.new(
94
93
            mode='RGBA',
95
 
            size=(master_width, master_height),
 
94
            size=(max_width, combined_image_height),
96
95
            color=transparent)
97
96
 
98
97
        y = 0
99
98
        positions = {}
100
99
        for index, sprite in enumerate(self.sprites):
101
 
            master.paste(sprite['image'], (0, y))
 
100
            try:
 
101
                combined_image.paste(sprite['image'], (0, y))
 
102
            except:
 
103
                print >> sys.stderr, (
 
104
                    "Error with image file %s" % sprite['filename'])
 
105
                raise
102
106
            # This is the position of the combined image on an HTML
103
107
            # element. Therefore, it subtracts the position of the
104
108
            # sprite in the file to move it to the top of the element.
105
109
            positions[sprite['filename']] = (0, -y)
106
 
            y += sprite['image'].size[1] + MARGIN
 
110
            y += sprite['image'].size[1] + self.margin
107
111
 
108
112
        self.positions = positions
109
 
        self.combined_image = master
 
113
        self.combined_image = combined_image
110
114
 
111
115
    def savePNG(self, filename):
112
 
        self.combined_image.save(filename, format='png')
 
116
        self.combined_image.save(filename, format='png', optimize=True)
113
117
 
114
118
    def savePositioning(self, filename):
115
119
        simplejson.dump(self.positions, fp=open(filename, 'w'), indent=4)