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
10
# First Edition: Jason Tackaberry <tack@urandom.ca>
11
# Maintainer: Jason Tackaberry <tack@urandom.ca>
13
# Please see the file AUTHORS for a complete list of authors.
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.
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.
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
29
# -----------------------------------------------------------------------------
37
from version import VERSION
42
# image and font wrapper
46
# Implement a crude image cache.
48
# Imlib2 maintains its own cache, but I don't think it caches
49
# the raw image data, since this ends up being faster.
55
"max-size": 16*1024 # 16 MB
58
def open_without_cache(file):
60
Create a new image object from the file 'file' without using the
63
return Image(file, False)
68
Create a new image object from the file 'file'.
70
if file in _image_cache["images"]:
71
return _image_cache["images"][file].copy()
74
_image_cache["images"][file] = image
75
_image_cache["order"].insert(0, file)
76
_image_cache["size"] += image.width * image.height * 4 / 1024
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
87
def open_from_memory(buf):
89
Create a new image object from a memory buffer.
93
img = _Imlib2.open_from_memory(buf)
97
def new(size, bytes = None, from_format = "BGRA", copy = True):
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.)
106
raise ValueError, "Invalid image size %s" % repr(size)
108
if not isinstance(val, int):
109
raise ValueError, "Invalid image size %s" % repr(size)
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))
118
return Image(_Imlib2.create(size))
121
def add_font_path(path):
123
Add the given path to the list of paths to scan when loading fonts.
125
_Imlib2.add_font_path(path)
128
def load_font(font, size):
130
Return a Font object from the given font specified in the form
131
'FontName/Size', such as 'Arial/16'
133
return Font(font + "/" + str(size))
136
def get_font_style_geometry(style):
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.
144
return TEXT_STYLE_GEOMETRY[style]