~ubuntu-branches/ubuntu/jaunty/kaa-imlib2/jaunty

« back to all changes in this revision

Viewing changes to src/__init__.py

  • Committer: Bazaar Package Importer
  • Author(s): Jeremie Corbier
  • Date: 2007-05-29 12:48:02 UTC
  • Revision ID: james.westby@ubuntu.com-20070529124802-7rqvjajce01xpyly
Tags: upstream-0.2.1
ImportĀ upstreamĀ versionĀ 0.2.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: iso-8859-1 -*-
 
2
# -----------------------------------------------------------------------------
 
3
# Imlib2.py - Imlib2 wrapper for Python
 
4
# -----------------------------------------------------------------------------
 
5
# $Id: __init__.py 2604 2007-03-28 09:21:52Z dmeyer $
 
6
# -----------------------------------------------------------------------------
 
7
# kaa.imlib2 - An imlib2 wrapper for Python
 
8
# Copyright (C) 2004-2006 Dirk Meyer, Jason Tackaberry
 
9
#
 
10
# First Edition: Jason Tackaberry <tack@urandom.ca>
 
11
# Maintainer:    Jason Tackaberry <tack@urandom.ca>
 
12
#
 
13
# Please see the file AUTHORS for a complete list of authors.
 
14
#
 
15
# This library is free software; you can redistribute it and/or modify
 
16
# it under the terms of the GNU Lesser General Public License version
 
17
# 2.1 as published by the Free Software Foundation.
 
18
#
 
19
# This library is distributed in the hope that it will be useful, but
 
20
# WITHOUT ANY WARRANTY; without even the implied warranty of
 
21
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
22
# Lesser General Public License for more details.
 
23
#
 
24
# You should have received a copy of the GNU Lesser General Public
 
25
# License along with this library; if not, write to the Free Software
 
26
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 
27
# 02110-1301 USA
 
28
#
 
29
# -----------------------------------------------------------------------------
 
30
 
 
31
import types
 
32
import math
 
33
import os
 
34
import glob
 
35
import md5
 
36
 
 
37
from version import VERSION
 
38
 
 
39
# imlib2 wrapper
 
40
import _Imlib2
 
41
 
 
42
# image and font wrapper
 
43
from image import *
 
44
from font import *
 
45
 
 
46
# Implement a crude image cache.
 
47
#
 
48
# Imlib2 maintains its own cache, but I don't think it caches
 
49
# the raw image data, since this ends up being faster.
 
50
 
 
51
_image_cache = {
 
52
    "images": {},
 
53
    "order": [],
 
54
    "size": 0,
 
55
    "max-size": 16*1024   # 16 MB
 
56
}
 
57
 
 
58
def open_without_cache(file):
 
59
    """
 
60
    Create a new image object from the file 'file' without using the
 
61
    internal cache.
 
62
    """
 
63
    return Image(file, False)
 
64
 
 
65
 
 
66
def open(file):
 
67
    """
 
68
    Create a new image object from the file 'file'.
 
69
    """
 
70
    if file in _image_cache["images"]:
 
71
        return _image_cache["images"][file].copy()
 
72
 
 
73
    image = Image(file)
 
74
    _image_cache["images"][file] = image
 
75
    _image_cache["order"].insert(0, file)
 
76
    _image_cache["size"] += image.width * image.height * 4 / 1024
 
77
 
 
78
    while _image_cache["size"] > _image_cache["max-size"]:
 
79
        file = _image_cache["order"].pop()
 
80
        expired = _image_cache["images"][file]
 
81
        del _image_cache["images"][file]
 
82
        _image_cache["size"] -= expired.width * expired.height * 4 / 1024
 
83
 
 
84
    return image
 
85
 
 
86
 
 
87
def open_from_memory(buf):
 
88
    """
 
89
    Create a new image object from a memory buffer.
 
90
    """
 
91
    if type(buf) == str:
 
92
        buf = buffer(buf)
 
93
    img = _Imlib2.open_from_memory(buf)
 
94
    return Image(img)
 
95
 
 
96
 
 
97
def new(size, bytes = None, from_format = "BGRA", copy = True):
 
98
    """
 
99
    Generates a new Image of size 'size', which is a tuple holding the width
 
100
    and height.  If 'bytes' is specified, the image is initialized from the
 
101
    raw BGRA data.  If 'copy' is False, 'bytes' must be either a write buffer
 
102
    or an integer pointing to a location in memory that will be used to hold
 
103
    the image.  (In this caes, from_format must be BGRA.)
 
104
    """
 
105
    if 0 in size:
 
106
        raise ValueError, "Invalid image size %s" % repr(size)
 
107
    for val in size:
 
108
        if not isinstance(val, int):
 
109
            raise ValueError, "Invalid image size %s" % repr(size)
 
110
    if bytes:
 
111
        if False in map(lambda x: x in "RGBA", list(from_format)):
 
112
            raise ValueError, "Converting from unsupported format: " +  from_format
 
113
        if type(bytes) != int and len(bytes) < size[0]*size[1]*len(from_format):
 
114
            raise ValueError, "Not enough bytes for converted format: expected %d, got %d" % \
 
115
                              (size[0]*size[1]*len(from_format), len(bytes))
 
116
        return Image(_Imlib2.create(size, bytes, from_format, copy))
 
117
    else:
 
118
        return Image(_Imlib2.create(size))
 
119
 
 
120
 
 
121
def add_font_path(path):
 
122
    """
 
123
    Add the given path to the list of paths to scan when loading fonts.
 
124
    """
 
125
    _Imlib2.add_font_path(path)
 
126
 
 
127
 
 
128
def load_font(font, size):
 
129
    """
 
130
    Return a Font object from the given font specified in the form
 
131
    'FontName/Size', such as 'Arial/16'
 
132
    """
 
133
    return Font(font + "/" + str(size))
 
134
 
 
135
 
 
136
def get_font_style_geometry(style):
 
137
    """
 
138
    Return the additional pixel the font needs for the style. This function
 
139
    will return left, top, right, bottom as number of pixels the text will
 
140
    start to the left/top and the number of pixels it needs more at the
 
141
    right/bottom. To avoid extra calculations the function will also return
 
142
    the additional width and height needed for the style.
 
143
    """
 
144
    return TEXT_STYLE_GEOMETRY[style]