~ubuntu-branches/debian/sid/pyx/sid

« back to all changes in this revision

Viewing changes to pyx/epsfile.py

  • Committer: Package Import Robot
  • Author(s): Stuart Prescott
  • Date: 2012-12-17 13:45:12 UTC
  • mfrom: (1.1.4)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: package-import@ubuntu.com-20121217134512-u0w6lrgdowsc1sfu
Tags: 0.12.1-1
* New upstream release
* Update maintainer address.
* Update copyright format URL.
* Bump standards version to 3.9.4 (no changes required).
* Drop postinst that was needed for lenny->squeeze upgrades.

Show diffs side-by-side

added added

removed removed

Lines of Context:
65
65
    #       in the DSC spec #5001, while '\n\r' *is* a *single* linebreak
66
66
    #       according to the EPSF spec #5002
67
67
 
68
 
    def __init__(self, filename, typicallinelen=257):
 
68
    def __init__(self, file, typicallinelen=257):
69
69
        """Opens the file filename for reading.
70
70
 
71
71
        typicallinelen defines the default buffer increase
74
74
        # note: The maximal line size in an EPS is 255 plus the
75
75
        #       linebreak characters. However, we also handle
76
76
        #       lines longer than that.
77
 
 
78
 
        self.file = open(filename, "rb")
 
77
        self.file = file
79
78
        self.buffer = ""
80
79
        self.typicallinelen = typicallinelen
81
80
 
138
137
        self.file.close()
139
138
 
140
139
 
141
 
def _readbbox(filename):
 
140
def _readbbox(file):
142
141
    """returns bounding box of EPS file filename"""
143
142
 
144
 
    file = linefilereader(filename)
 
143
    file = linefilereader(file)
145
144
 
146
145
    # check the %! header comment
147
146
    if not file.readline().startswith("%!"):
248
247
        self.y_pt = unit.topt(y)
249
248
        self.filename = filename
250
249
        self.kpsearch = kpsearch
251
 
        self.mybbox = bbox or _readbbox(self.filename)
 
250
        if bbox:
 
251
            self.mybbox = bbox
 
252
        else:
 
253
            epsfile = self.open()
 
254
            self.mybbox = _readbbox(epsfile)
 
255
            epsfile.close()
252
256
 
253
257
        # determine scaling in x and y direction
254
258
        self.scalex = self.scaley = scale
307
311
        if translatebbox:
308
312
            self.trafo = self.trafo * trafo.translate_pt(-self.mybbox.llx_pt, -self.mybbox.lly_pt)
309
313
 
 
314
    def open(self):
 
315
        if self.kpsearch:
 
316
            return filelocator.open(self.filename, [filelocator.format.pict], "rb")
 
317
        else:
 
318
            return open(self.filename, "rb")
 
319
 
310
320
    def bbox(self):
311
321
        return self.mybbox.transformed(self.trafo)
312
322
 
315
325
        registry.add(_EndEPSF)
316
326
        bbox += self.bbox()
317
327
 
318
 
        if self.kpsearch:
319
 
            epsfile = filelocator.open(self.filename, [filelocator.format.pict], "rb")
320
 
        else:
321
 
            epsfile = open(self.filename, "rb")
322
 
 
323
328
        file.write("BeginEPSF\n")
324
329
 
325
330
        if self.clip:
329
334
        self.trafo.processPS(file, writer, context, registry, bbox)
330
335
 
331
336
        file.write("%%%%BeginDocument: %s\n" % self.filename)
 
337
 
 
338
        epsfile = self.open()
332
339
        file.write(epsfile.read())
 
340
        epsfile.close()
 
341
 
333
342
        file.write("%%EndDocument\n")
334
343
        file.write("EndEPSF\n")
335
 
        epsfile.close()
336
344
 
337
345
    def processPDF(self, file, writer, context, registry, bbox):
338
346
        warnings.warn("EPS file is included as a bitmap created using pipeGS")
340
348
        import Image
341
349
        c = canvas.canvas()
342
350
        c.insert(self)
343
 
        fd, fname = tempfile.mkstemp()
344
 
        f = os.fdopen(fd, "wb")
345
 
        f.close()
346
 
        c.pipeGS(fname, device="pngalpha", resolution=600)
347
 
        i = Image.open(fname)
 
351
        i = Image.open(c.pipeGS(device="pngalpha", resolution=600, seekable=True))
348
352
        i.load()
349
 
        os.unlink(fname)
350
353
        b = bitmap.bitmap_pt(self.bbox().llx_pt, self.bbox().lly_pt, i)
351
354
        # we slightly shift the bitmap to re-center it, as the bitmap might contain some additional border
352
355
        # unfortunately we need to construct another bitmap instance for that ...