~ubuntu-branches/ubuntu/wily/python-imaging/wily

« back to all changes in this revision

Viewing changes to .pc/git-updates.diff/PIL/FontFile.py

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-01-31 20:49:20 UTC
  • mfrom: (27.1.1 raring-proposed)
  • Revision ID: package-import@ubuntu.com-20130131204920-b5zshy6vgfvdionl
Tags: 1.1.7+1.7.8-1ubuntu1
Rewrite build dependencies to allow cross builds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#
 
2
# The Python Imaging Library
 
3
# $Id$
 
4
#
 
5
# base class for raster font file parsers
 
6
#
 
7
# history:
 
8
# 1997-06-05 fl   created
 
9
# 1997-08-19 fl   restrict image width
 
10
#
 
11
# Copyright (c) 1997-1998 by Secret Labs AB
 
12
# Copyright (c) 1997-1998 by Fredrik Lundh
 
13
#
 
14
# See the README file for information on usage and redistribution.
 
15
#
 
16
 
 
17
import os
 
18
import Image
 
19
 
 
20
import marshal
 
21
 
 
22
try:
 
23
    import zlib
 
24
except ImportError:
 
25
    zlib = None
 
26
 
 
27
WIDTH = 800
 
28
 
 
29
def puti16(fp, values):
 
30
    # write network order (big-endian) 16-bit sequence
 
31
    for v in values:
 
32
        if v < 0:
 
33
            v = v + 65536
 
34
        fp.write(chr(v>>8&255) + chr(v&255))
 
35
 
 
36
##
 
37
# Base class for raster font file handlers.
 
38
 
 
39
class FontFile:
 
40
 
 
41
    bitmap = None
 
42
 
 
43
    def __init__(self):
 
44
 
 
45
        self.info = {}
 
46
        self.glyph = [None] * 256
 
47
 
 
48
    def __getitem__(self, ix):
 
49
        return self.glyph[ix]
 
50
 
 
51
    def compile(self):
 
52
        "Create metrics and bitmap"
 
53
 
 
54
        if self.bitmap:
 
55
            return
 
56
 
 
57
        # create bitmap large enough to hold all data
 
58
        h = w = maxwidth = 0
 
59
        lines = 1
 
60
        for glyph in self:
 
61
            if glyph:
 
62
                d, dst, src, im = glyph
 
63
                h = max(h, src[3] - src[1])
 
64
                w = w + (src[2] - src[0])
 
65
                if w > WIDTH:
 
66
                    lines = lines + 1
 
67
                    w = (src[2] - src[0])
 
68
                maxwidth = max(maxwidth, w)
 
69
 
 
70
        xsize = maxwidth
 
71
        ysize = lines * h
 
72
 
 
73
        if xsize == 0 and ysize == 0:
 
74
            return ""
 
75
 
 
76
        self.ysize = h
 
77
 
 
78
        # paste glyphs into bitmap
 
79
        self.bitmap = Image.new("1", (xsize, ysize))
 
80
        self.metrics = [None] * 256
 
81
        x = y = 0
 
82
        for i in range(256):
 
83
            glyph = self[i]
 
84
            if glyph:
 
85
                d, dst, src, im = glyph
 
86
                xx, yy = src[2] - src[0], src[3] - src[1]
 
87
                x0, y0 = x, y
 
88
                x = x + xx
 
89
                if x > WIDTH:
 
90
                    x, y = 0, y + h
 
91
                    x0, y0 = x, y
 
92
                    x = xx
 
93
                s = src[0] + x0, src[1] + y0, src[2] + x0, src[3] + y0
 
94
                self.bitmap.paste(im.crop(src), s)
 
95
                # print chr(i), dst, s
 
96
                self.metrics[i] = d, dst, s
 
97
 
 
98
 
 
99
    def save1(self, filename):
 
100
        "Save font in version 1 format"
 
101
 
 
102
        self.compile()
 
103
 
 
104
        # font data
 
105
        self.bitmap.save(os.path.splitext(filename)[0] + ".pbm", "PNG")
 
106
 
 
107
        # font metrics
 
108
        fp = open(os.path.splitext(filename)[0] + ".pil", "wb")
 
109
        fp.write("PILfont\n")
 
110
        fp.write(";;;;;;%d;\n" % self.ysize) # HACK!!!
 
111
        fp.write("DATA\n")
 
112
        for id in range(256):
 
113
            m = self.metrics[id]
 
114
            if not m:
 
115
                puti16(fp, [0] * 10)
 
116
            else:
 
117
                puti16(fp, m[0] + m[1] + m[2])
 
118
        fp.close()
 
119
 
 
120
 
 
121
    def save2(self, filename):
 
122
        "Save font in version 2 format"
 
123
 
 
124
        # THIS IS WORK IN PROGRESS
 
125
 
 
126
        self.compile()
 
127
 
 
128
        data = marshal.dumps((self.metrics, self.info))
 
129
 
 
130
        if zlib:
 
131
            data = "z" + zlib.compress(data, 9)
 
132
        else:
 
133
            data = "u" + data
 
134
 
 
135
        fp = open(os.path.splitext(filename)[0] + ".pil", "wb")
 
136
 
 
137
        fp.write("PILfont2\n" + self.name + "\n" + "DATA\n")
 
138
 
 
139
        fp.write(data)
 
140
 
 
141
        self.bitmap.save(fp, "PNG")
 
142
 
 
143
        fp.close()
 
144
 
 
145
 
 
146
    save = save1 # for now