home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_1475 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  10.5 KB  |  239 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. import os
  8. from calibre.constants import plugins, filesystem_encoding
  9. (_magick, _merr) = plugins['magick']
  10. if _magick is None:
  11.     raise RuntimeError('Failed to load ImageMagick: ' + _merr)
  12. _magick is None
  13. _gravity_map = [](_[1])
  14. _type_map = [](_[2])
  15.  
  16. class Rect(object):
  17.     
  18.     def __init__(self, left, top, right, bottom):
  19.         (self.left, self.top, self.right, self.bottom) = (left, top, right, bottom)
  20.  
  21.     
  22.     def __str__(self):
  23.         return '(%s, %s) -- (%s, %s)' % (self.left, self.top, self.right, self.bottom)
  24.  
  25.  
  26.  
  27. class FontMetrics(object):
  28.     
  29.     def __init__(self, ret):
  30.         self._attrs = []
  31.         for i, x in enumerate(('char_width', 'char_height', 'ascender', 'descender', 'text_width', 'text_height', 'max_horizontal_advance')):
  32.             setattr(self, x, ret[i])
  33.             self._attrs.append(x)
  34.         
  35.         self.bounding_box = Rect(ret[7], ret[8], ret[9], ret[10])
  36.         self.x = ret[11]
  37.         self.y = ret[12]
  38.         self._attrs.extend([
  39.             'bounding_box',
  40.             'x',
  41.             'y'])
  42.         self._attrs = tuple(self._attrs)
  43.  
  44.     
  45.     def __str__(self):
  46.         return [] % []([ getattr(self, x) for x in self._attrs ])
  47.  
  48.  
  49.  
  50. class PixelWand(_magick.PixelWand):
  51.     pass
  52.  
  53.  
  54. class DrawingWand(_magick.DrawingWand):
  55.     
  56.     def font(self):
  57.         
  58.         def fget(self):
  59.             return self.font_.decode(filesystem_encoding, 'replace').lower()
  60.  
  61.         
  62.         def fset(self, val):
  63.             if isinstance(val, unicode):
  64.                 val = val.encode(filesystem_encoding)
  65.             
  66.             self.font_ = str(val)
  67.  
  68.         return property(fget = fget, fset = fset, doc = _magick.DrawingWand.font_.__doc__)
  69.  
  70.     font = dynamic_property(font)
  71.     
  72.     def gravity(self):
  73.         
  74.         def fget(self):
  75.             val = self.gravity_
  76.             return _gravity_map[val]
  77.  
  78.         
  79.         def fset(self, val):
  80.             val = getattr(_magick, str(val))
  81.             self.gravity_ = val
  82.  
  83.         return property(fget = fget, fset = fset, doc = _magick.DrawingWand.gravity_.__doc__)
  84.  
  85.     gravity = dynamic_property(gravity)
  86.     
  87.     def font_size(self):
  88.         
  89.         def fget(self):
  90.             return self.font_size_
  91.  
  92.         
  93.         def fset(self, val):
  94.             self.font_size_ = float(val)
  95.  
  96.         return property(fget = fget, fset = fset, doc = _magick.DrawingWand.font_size_.__doc__)
  97.  
  98.     font_size = dynamic_property(font_size)
  99.  
  100.  
  101. class Image(_magick.Image):
  102.     
  103.     def clone(self):
  104.         ans = Image()
  105.         ans.copy(self)
  106.         return ans
  107.  
  108.     clone = property(clone)
  109.     
  110.     def load(self, data):
  111.         return _magick.Image.load(self, bytes(data))
  112.  
  113.     
  114.     def open(self, path_or_file):
  115.         if not hasattr(path_or_file, 'read') and path_or_file.lower().endswith('.wmf'):
  116.             if isinstance(path_or_file, unicode):
  117.                 path_or_file = path_or_file.encode(filesystem_encoding)
  118.             
  119.             return _magick.Image.read(self, path_or_file)
  120.         data = path_or_file
  121.         if hasattr(data, 'read'):
  122.             data = data.read()
  123.         else:
  124.             data = open(data, 'rb').read()
  125.         self.load(data)
  126.  
  127.     
  128.     def format(self):
  129.         
  130.         def fget(self):
  131.             return self.format_.decode('utf-8', 'ignore').lower()
  132.  
  133.         
  134.         def fset(self, val):
  135.             self.format_ = str(val)
  136.  
  137.         return property(fget = fget, fset = fset, doc = _magick.Image.format_.__doc__)
  138.  
  139.     format = dynamic_property(format)
  140.     
  141.     def type(self):
  142.         
  143.         def fget(self):
  144.             return _type_map[self.type_]
  145.  
  146.         
  147.         def fset(self, val):
  148.             val = getattr(_magick, str(val))
  149.             self.type_ = val
  150.  
  151.         return property(fget = fget, fset = fset, doc = _magick.Image.type_.__doc__)
  152.  
  153.     type = dynamic_property(type)
  154.     
  155.     def size(self):
  156.         
  157.         def fget(self):
  158.             return self.size_
  159.  
  160.         
  161.         def fset(self, val):
  162.             filter = 'CatromFilter'
  163.             if len(val) > 2:
  164.                 filter = val[2]
  165.             
  166.             filter = int(getattr(_magick, filter))
  167.             blur = 1
  168.             if len(val) > 3:
  169.                 blur = float(val[3])
  170.             
  171.             self.size_ = (int(val[0]), int(val[1]), filter, blur)
  172.  
  173.         return property(fget = fget, fset = fset, doc = _magick.Image.size_.__doc__)
  174.  
  175.     size = dynamic_property(size)
  176.     
  177.     def save(self, path, format = None):
  178.         if format is None:
  179.             ext = os.path.splitext(path)[1]
  180.             if len(ext) < 2:
  181.                 raise ValueError('No format specified')
  182.             len(ext) < 2
  183.             format = ext[1:]
  184.         
  185.         format = format.upper()
  186.         
  187.         try:
  188.             f = _[1]
  189.             f.write(self.export(format))
  190.         finally:
  191.             pass
  192.  
  193.  
  194.     
  195.     def compose(self, img, left = 0, top = 0, operation = 'OverCompositeOp'):
  196.         op = getattr(_magick, operation)
  197.         bounds = self.size
  198.         if left < 0 and top < 0 and left >= bounds[0] or top >= bounds[1]:
  199.             raise ValueError('left and/or top out of bounds')
  200.         top >= bounds[1]
  201.         _magick.Image.compose(self, img, int(left), int(top), op)
  202.  
  203.     
  204.     def font_metrics(self, drawing_wand, text):
  205.         if isinstance(text, unicode):
  206.             text = text.encode('UTF-8')
  207.         
  208.         return FontMetrics(_magick.Image.font_metrics(self, drawing_wand, text))
  209.  
  210.     
  211.     def annotate(self, drawing_wand, x, y, angle, text):
  212.         if isinstance(text, unicode):
  213.             text = text.encode('UTF-8')
  214.         
  215.         return _magick.Image.annotate(self, drawing_wand, x, y, angle, text)
  216.  
  217.     
  218.     def distort(self, method, arguments, bestfit):
  219.         method = getattr(_magick, method)
  220.         arguments = [ float(x) for x in arguments ]
  221.         _magick.Image.distort(self, method, arguments, bestfit)
  222.  
  223.     
  224.     def rotate(self, background_pixel_wand, degrees):
  225.         _magick.Image.rotate(self, background_pixel_wand, float(degrees))
  226.  
  227.     
  228.     def quantize(self, number_colors, colorspace = 'RGBColorspace', treedepth = 0, dither = True, measure_error = False):
  229.         colorspace = getattr(_magick, colorspace)
  230.         _magick.Image.quantize(self, number_colors, colorspace, treedepth, dither, measure_error)
  231.  
  232.  
  233.  
  234. def create_canvas(width, height, bgcolor = '#ffffff'):
  235.     canvas = Image()
  236.     canvas.create_canvas(int(width), int(height), str(bgcolor))
  237.     return canvas
  238.  
  239.