junglejourney

changeset 172:0488e95f9431

Changed the tool to produce SVG files instead of PNG images. Added a license header.
author David Boddie <david@boddie.org.uk>
date Wed Sep 21 23:33:03 2011 +0200
parents 6d63447e97fd
children 95a2a00b5a92
files TODO.txt materials/make_packaging.py
diffstat 2 files changed, 109 insertions(+), 30 deletions(-) [+]
line diff
     1.1 --- a/TODO.txt	Wed Sep 21 00:31:10 2011 +0200
     1.2 +++ b/TODO.txt	Wed Sep 21 23:33:03 2011 +0200
     1.3 @@ -1,6 +1,8 @@
     1.4  To Do
     1.5  
     1.6  Add last two enemy sprite sets.
     1.7 +Include the source in the disk image, avoiding potential license issues with
     1.8 +redistribution.
     1.9  Nice to have: make one of the enemies have different movement.
    1.10  Nice to have: make the boomerang turn before hitting a wall.
    1.11  Nice to have: make projectiles start next to the player, not on top of him.
     2.1 --- a/materials/make_packaging.py	Wed Sep 21 00:31:10 2011 +0200
     2.2 +++ b/materials/make_packaging.py	Wed Sep 21 23:33:03 2011 +0200
     2.3 @@ -1,9 +1,96 @@
     2.4  #!/usr/bin/env python
     2.5  
     2.6 -import os, sys
     2.7 +"""
     2.8 +Copyright (C) 2011 David Boddie <david@boddie.org.uk>
     2.9 +
    2.10 +This program is free software: you can redistribute it and/or modify
    2.11 +it under the terms of the GNU General Public License as published by
    2.12 +the Free Software Foundation, either version 3 of the License, or
    2.13 +(at your option) any later version.
    2.14 +
    2.15 +This program is distributed in the hope that it will be useful,
    2.16 +but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.17 +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    2.18 +GNU General Public License for more details.
    2.19 +
    2.20 +You should have received a copy of the GNU General Public License
    2.21 +along with this program.  If not, see <http://www.gnu.org/licenses/>.
    2.22 +"""
    2.23 +
    2.24 +import codecs, os, sys
    2.25  from PyQt4.QtCore import QSize
    2.26  from PyQt4.QtGui import *
    2.27  
    2.28 +def relpath(source, destination):
    2.29 +
    2.30 +    source = os.path.abspath(source)
    2.31 +    destination = os.path.abspath(destination)
    2.32 +    
    2.33 +    src_pieces = source.split(os.sep)
    2.34 +    dest_pieces = destination.split(os.sep)
    2.35 +    
    2.36 +    if os.path.isfile(source):
    2.37 +        src_pieces.pop()
    2.38 +    
    2.39 +    common = []
    2.40 +    for i in range(min(len(src_pieces), len(dest_pieces))):
    2.41 +    
    2.42 +        if src_pieces[i] == dest_pieces[i]:
    2.43 +            common.append(src_pieces[i])
    2.44 +            i -= 1
    2.45 +        else:
    2.46 +            break
    2.47 +    
    2.48 +    to_common = os.sep.join([os.pardir]*(len(src_pieces)-len(common)))
    2.49 +    return to_common + os.sep + os.sep.join(dest_pieces[len(common):])
    2.50 +
    2.51 +
    2.52 +class SVG:
    2.53 +
    2.54 +    def __init__(self, path):
    2.55 +    
    2.56 +        self.path = path
    2.57 +    
    2.58 +    def _escape(self, text):
    2.59 +    
    2.60 +        for s, r in (("&", "&amp;"), ("<", "&lt;"), (">", "&gt;")):
    2.61 +            text = text.replace(s, r)
    2.62 +        
    2.63 +        return text
    2.64 +    
    2.65 +    def open(self):
    2.66 +    
    2.67 +        self.text = ('<?xml version="1.0" encoding="UTF-8" standalone="yes"?>\n'
    2.68 +                     '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n'
    2.69 +                     '  "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'
    2.70 +                     '<svg version="1.1"\n'
    2.71 +                     '     xmlns="http://www.w3.org/2000/svg"\n'
    2.72 +                     '     xmlns:xlink="http://www.w3.org/1999/xlink">\n')
    2.73 +    
    2.74 +    def add_image(self, x, y, width, height, path):
    2.75 +    
    2.76 +        path = os.path.join(relpath(self.path, os.curdir), path)
    2.77 +        self.text += '<image x="%f" y="%f" width="%f" height="%f"\n' % (x, y, width, height)
    2.78 +        self.text += '       xlink:href="%s" />\n' % path
    2.79 +        
    2.80 +    def add_text(self, x, y, font, text):
    2.81 +    
    2.82 +        self.text += '<text x="%f" y="%f"\n' % (x, y)
    2.83 +        self.text += ('      font-family="%s"\n'
    2.84 +                      '      font-size="%i"\n') % (font["family"], font["size"])
    2.85 +        if font.has_key("weight"):
    2.86 +            self.text += '      font-weight="%s"\n' % font["weight"]
    2.87 +        if font.has_key("style"):
    2.88 +            self.text += '      font-style="%s"\n' % font["style"]
    2.89 +        self.text += '>\n'
    2.90 +        self.text += self._escape(text)
    2.91 +        self.text += '</text>\n'
    2.92 +    
    2.93 +    def close(self):
    2.94 +    
    2.95 +        self.text += "</svg>\n"
    2.96 +        codecs.open(self.path, "w", "utf-8").write(self.text)
    2.97 +
    2.98  
    2.99  class Page:
   2.100  
   2.101 @@ -12,19 +99,15 @@
   2.102          self.size = size
   2.103          self.objects = objects
   2.104      
   2.105 -    def render(self, image = None):
   2.106 +    def render(self, svg):
   2.107      
   2.108 -        if not image:
   2.109 -            image = QImage(QSize(*self.size), QImage.Format_RGB32)
   2.110 -            image.fill(qRgb(255,255,255))
   2.111 -        
   2.112          positions = [(0, 0)]
   2.113          for obj in self.objects:
   2.114          
   2.115 -            x, y = obj.render(image, positions)
   2.116 +            x, y = obj.render(svg, positions)
   2.117              positions.append((x, y))
   2.118          
   2.119 -        return image
   2.120 +        return svg
   2.121  
   2.122  class TextBox:
   2.123  
   2.124 @@ -35,17 +118,13 @@
   2.125          self.follow = follow
   2.126          self.index = index
   2.127      
   2.128 -    def render(self, image, positions):
   2.129 +    def render(self, svg, positions):
   2.130      
   2.131          x, y, width, height = self.bbox
   2.132          
   2.133          if self.follow:
   2.134              y = y + positions[self.index][1]
   2.135          
   2.136 -        p = QPainter()
   2.137 -        p.begin(image)
   2.138 -        p.setRenderHint(QPainter.TextAntialiasing)
   2.139 -        
   2.140          for text_item in self.text_items:
   2.141          
   2.142              left_indent = text_item.font.get("left indent", 0)
   2.143 @@ -57,13 +136,10 @@
   2.144              
   2.145                  for font, word_x, text in pieces:
   2.146                  
   2.147 -                    p.setFont(font)
   2.148 -                    p.drawText(item_x + word_x, y, text)
   2.149 +                    svg.add_text(item_x + word_x, y, font, text)
   2.150                  
   2.151                  y += line_height
   2.152          
   2.153 -        p.end()
   2.154 -        
   2.155          return x, y
   2.156  
   2.157  class Text:
   2.158 @@ -150,7 +226,7 @@
   2.159          
   2.160          for word in words:
   2.161          
   2.162 -            output.append((word.font(), x, word.text))
   2.163 +            output.append((word._font, x, word.text))
   2.164              x += word.width()
   2.165              if spacing is not None:
   2.166                  x += spacing
   2.167 @@ -218,25 +294,24 @@
   2.168          self.index = index
   2.169          self.scale = scale
   2.170      
   2.171 -    def render(self, image, positions):
   2.172 +    def render(self, svg, positions):
   2.173      
   2.174          x, y, width, height = self.bbox
   2.175          
   2.176          if self.follow:
   2.177              y = y + positions[self.index][1]
   2.178          
   2.179 -        p = QPainter()
   2.180 -        p.begin(image)
   2.181 -        p.setRenderHint(QPainter.TextAntialiasing)
   2.182 +        im = QImage(self.path)
   2.183 +        width = im.size().width()
   2.184 +        height = im.size().height()
   2.185          
   2.186 -        im = QImage(self.path)
   2.187          if self.scale:
   2.188 -            im = im.scaled(self.scale * im.width(), self.scale * im.height())
   2.189 -        p.drawImage(x, y, im)
   2.190 +            width = width * self.scale
   2.191 +            height = height * self.scale
   2.192          
   2.193 -        p.end()
   2.194 +        svg.add_image(x, y, width, height, self.path)
   2.195          
   2.196 -        return x + im.size().width(), y + im.size().height()
   2.197 +        return x + width, y + height
   2.198  
   2.199  
   2.200  if __name__ == "__main__":
   2.201 @@ -442,9 +517,11 @@
   2.202      i = 0
   2.203      for page in pages:
   2.204      
   2.205 -        path = os.path.join(output_dir, "page-%i.png" % i)
   2.206 -        image = page.render()
   2.207 -        image.save(path)
   2.208 +        path = os.path.join(output_dir, "page-%i.svg" % i)
   2.209 +        svg = SVG(path)
   2.210 +        svg.open()
   2.211 +        page.render(svg)
   2.212 +        svg.close()
   2.213          i += 1
   2.214      
   2.215      sys.exit()