~edwin-grubbs/python-imaging/trunk

« back to all changes in this revision

Viewing changes to PIL/ImageFont.py

  • Committer: effbot
  • Date: 2006-03-01 19:11:48 UTC
  • Revision ID: svn-v4:be285980-f00d-0410-a9fe-d4747b46ecd0:pil:18
Load Imaging-1.1.3 into pil.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
# The Python Imaging Library.
3
 
# $Id$
 
3
# $Id: //modules/pil/PIL/ImageFont.py#4 $
4
4
#
5
5
# PIL raster font management
6
6
#
10
10
# 1999-02-06 fl   rewrote most font management stuff in C
11
11
# 1999-03-17 fl   take pth files into account in load_path (from Richard Jones)
12
12
# 2001-02-17 fl   added freetype support
 
13
# 2001-05-09 fl   added TransposedFont wrapper class
 
14
# 2002-03-04 fl   make sure we have a "L" or "1" font
13
15
#
14
16
# Todo:
15
17
# Adapt to PILFONT2 format (16-bit fonts, compressed, single file)
16
18
#
17
 
# Copyright (c) Secret Labs AB 1997-2001
18
 
# Copyright (c) Fredrik Lundh 1996-2001
 
19
# Copyright (c) 1997-2002 by Secret Labs AB
 
20
# Copyright (c) 1996-2001 by Fredrik Lundh
19
21
#
20
22
# See the README file for information on usage and redistribution.
21
23
#
58
60
        data = fp.read(256*20)
59
61
 
60
62
        # read PILfont bitmap
61
 
        image = None
62
63
        for ext in (".png", ".gif", ".pbm"):
63
64
            try:
64
 
                image = Image.open(os.path.splitext(filename)[0] + ext)
 
65
                fullname = os.path.splitext(filename)[0] + ext
 
66
                image = Image.open(fullname)
65
67
            except:
66
68
                pass
67
 
            if image:
68
 
                break
69
 
        if image is None:
 
69
            else:
 
70
                if image and image.mode in ("1", "L"):
 
71
                    break
 
72
        else:
70
73
            raise IOError, "cannot find glyph data file"
71
74
 
72
75
        image.load()
 
76
 
 
77
        self.file = fullname
73
78
        self.font = Image.core.font(image.im, data)
74
79
 
75
80
        # delegate critical operations to internal type
94
99
        self.font.render(text, im.id)
95
100
        return im
96
101
 
 
102
 
 
103
class TransposedFont:
 
104
    "Wrapper for writing rotated or mirrored text"
 
105
 
 
106
    def __init__(self, font, orientation=None):
 
107
        self.font = font
 
108
        self.orientation = orientation # any 'transpose' argument, or None
 
109
 
 
110
    def getsize(self, text):
 
111
        w, h = self.font.getsize(text)
 
112
        if self.orientation in (Image.ROTATE_90, Image.ROTATE_270):
 
113
            return h, w
 
114
        return w, h
 
115
 
 
116
    def getmask(self, text):
 
117
        im = self.font.getmask(text)
 
118
        if self.orientation is not None:
 
119
            return im.transpose(self.orientation)
 
120
        return im
 
121
 
97
122
#
98
123
# --------------------------------------------------------------------
99
124