Exemple #1
0
    def processPS(self, file, writer, context, registry, bbox):
        # process pattern, letting it register its resources and calculate the bbox of the pattern
        patternfile = cStringIO.StringIO()
        realpatternbbox = bboxmodule.empty()
        canvas.canvas.processPS(self, patternfile, writer, pswriter.context(),
                                registry, realpatternbbox)
        patternproc = patternfile.getvalue()
        patternfile.close()

        if self.xstep is None:
            xstep = unit.topt(realpatternbbox.width())
        else:
            xstep = unit.topt(self.xstep)
        if self.ystep is None:
            ystep = unit.topt(realpatternbbox.height())
        else:
            ystep = unit.topt(self.ystep)
        if not xstep:
            raise ValueError("xstep in pattern cannot be zero")
        if not ystep:
            raise ValueError("ystep in pattern cannot be zero")
        patternbbox = self.patternbbox or realpatternbbox.enlarged(
            self.bboxenlarge)

        patternprefix = "\n".join(
            ("<<", "/PatternType %d" % self.patterntype, "/PaintType %d" %
             self.painttype, "/TilingType %d" % self.tilingtype,
             "/BBox [%g %g %g %g]" % patternbbox.highrestuple_pt(),
             "/XStep %g" % xstep, "/YStep %g" % ystep,
             "/PaintProc {\nbegin\n"))
        patterntrafostring = self.patterntrafo is None and "matrix" or str(
            self.patterntrafo)
        patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring

        registry.add(
            pswriter.PSdefinition(
                self.id, "".join((patternprefix, patternproc, patternsuffix))))

        # activate pattern
        file.write("%s setpattern\n" % self.id)
Exemple #2
0
import os, string, tempfile, warnings
import canvasitem, bbox, filelocator, unit, trafo, pswriter

# PostScript-procedure definitions (cf. 5002.EPSF_Spec_v3.0.pdf)
# with important correction in EndEPSF:
#   end operator is missing in the spec!

_BeginEPSF = pswriter.PSdefinition("BeginEPSF", """{
  /b4_Inc_state save def
  /dict_count countdictstack def
  /op_count count 1 sub def
  userdict begin
  /showpage { } def
  0 setgray 0 setlinecap
  1 setlinewidth 0 setlinejoin
  10 setmiterlimit [ ] 0 setdash newpath
  /languagelevel where
  {pop languagelevel
  1 ne
    {false setstrokeadjust false setoverprint
    } if
  } if
} bind""")

_EndEPSF = pswriter.PSdefinition("EndEPSF", """{
  end
  count op_count sub {pop} repeat
  countdictstack dict_count sub {end} repeat
  b4_Inc_state restore
} bind""")
Exemple #3
0
    def processPS(self, file, writer, context, registry, bbox):
        if self.PSstoreimage and not self.PSsinglestring:
            registry.add(
                pswriter.PSdefinition(
                    "imagedataaccess",
                    "{ /imagedataindex load "  # get list index
                    "dup 1 add /imagedataindex exch store "  # store increased index
                    "/imagedataid load exch get }")
            )  # select string from array
        if self.PSstoreimage:
            registry.add(
                PSimagedata(self.PSimagename, self.data, self.PSsinglestring,
                            self.PSmaxstrlen))
        bbox += self.bbox()

        file.write("gsave\n")
        if self.palettedata is not None:
            file.write("[ /Indexed %s %i\n" %
                       (self.palettecolorspace, len(self.palettedata) / 3 - 1))
            file.write("%%%%BeginData: %i ASCII Lines\n" %
                       ascii85lines(len(self.palettedata)))
            file.write("<~")
            ascii85stream(file, self.palettedata)
            file.write("~>\n" "%%EndData\n")
            file.write("] setcolorspace\n")
        else:
            file.write("%s setcolorspace\n" % self.colorspace)

        if self.PSstoreimage and not self.PSsinglestring:
            file.write(
                "/imagedataindex 0 store\n"  # not use the stack since interpreters differ in their stack usage
                "/imagedataid %s store\n" % self.PSimagename)

        file.write("<<\n" "/ImageType 1\n" "/Width %i\n" % self.imagewidth)
        file.write("/Height %i\n" % self.imageheight)
        file.write("/BitsPerComponent 8\n"
                   "/ImageMatrix %s\n" % self.imagematrixPS)
        file.write("/Decode %s\n" % self.decode)

        file.write("/DataSource ")
        if self.PSstoreimage:
            if self.PSsinglestring:
                file.write("/%s load" % self.PSimagename)
            else:
                file.write(
                    "/imagedataaccess load"
                )  # some printers do not allow for inline code here -> we store it in a resource
        else:
            if self.PSbinexpand == 2:
                file.write("currentfile /ASCIIHexDecode filter")
            else:
                file.write("currentfile /ASCII85Decode filter")
        if self.compressmode:
            file.write(" /%sDecode filter" % self.compressmode)
        file.write("\n")

        file.write(">>\n")

        if self.PSstoreimage:
            file.write("image\n")
        else:
            if self.PSbinexpand == 2:
                file.write("%%%%BeginData: %i ASCII Lines\n"
                           "image\n" % (asciihexlines(len(self.data)) + 1))
                asciihexstream(file, self.data)
            else:
                # the datasource is currentstream (plus some filters)
                file.write("%%%%BeginData: %i ASCII Lines\n"
                           "image\n" % (ascii85lines(len(self.data)) + 1))
                ascii85stream(file, self.data)
                file.write("~>\n")
            file.write("%%EndData\n")

        file.write("grestore\n")
Exemple #4
0
    def processPS(self, file, writer, context, registry, bbox):
        mode, data, alpha, palettemode, palettedata = self.imagedata(True)
        pstrafo = trafo.translate_pt(0, -1.0).scaled(
            self.imagewidth, -self.imageheight) * self.pdftrafo.inverse()

        PSsinglestring = self.PSstoreimage and len(data) < self.PSmaxstrlen
        if PSsinglestring:
            PSimagename = "image-%d-%s-singlestring" % (id(
                self.image), self.compressmode)
        else:
            PSimagename = "image-%d-%s-stringarray" % (id(
                self.image), self.compressmode)

        if self.PSstoreimage and not PSsinglestring:
            registry.add(
                pswriter.PSdefinition(
                    "imagedataaccess",
                    "{ /imagedataindex load "  # get list index
                    "dup 1 add /imagedataindex exch store "  # store increased index
                    "/imagedataid load exch get }")
            )  # select string from array
        if self.PSstoreimage:
            registry.add(
                PSimagedata(PSimagename, data, PSsinglestring,
                            self.PSmaxstrlen))
        bbox += self.bbox()

        file.write("gsave\n")
        if palettedata is not None:
            file.write("[ /Indexed %s %i\n" %
                       (devicenames[palettemode], len(palettedata) / 3 - 1))
            file.write("%%%%BeginData: %i ASCII Lines\n" %
                       ascii85lines(len(palettedata)))
            file.write("<~")
            ascii85stream(file, palettedata)
            file.write("~>\n" "%%EndData\n")
            file.write("] setcolorspace\n")
        else:
            file.write("%s setcolorspace\n" % devicenames[mode])

        if self.PSstoreimage and not PSsinglestring:
            file.write(
                "/imagedataindex 0 store\n"  # not use the stack since interpreters differ in their stack usage
                "/imagedataid %s store\n" % PSimagename)

        file.write("<<\n")
        if alpha:
            file.write("/ImageType 3\n" "/DataDict\n" "<<\n")
        file.write("/ImageType 1\n" "/Width %i\n" % self.imagewidth)
        file.write("/Height %i\n" % self.imageheight)
        file.write("/BitsPerComponent 8\n" "/ImageMatrix %s\n" % pstrafo)
        file.write("/Decode %s\n" % decodestrings[mode])

        file.write("/DataSource ")
        if self.PSstoreimage:
            if PSsinglestring:
                file.write("/%s load" % PSimagename)
            else:
                file.write(
                    "/imagedataaccess load"
                )  # some printers do not allow for inline code here -> we store it in a resource
        else:
            if self.PSbinexpand == 2:
                file.write("currentfile /ASCIIHexDecode filter")
            else:
                file.write("currentfile /ASCII85Decode filter")
        if self.compressmode or self.imagecompressed:
            file.write(" /%sDecode filter" %
                       (self.compressmode or self.imagecompressed))
        file.write("\n")

        file.write(">>\n")

        if alpha:
            file.write("/MaskDict\n"
                       "<<\n"
                       "/ImageType 1\n"
                       "/Width %i\n" % self.imagewidth)
            file.write("/Height %i\n" % self.imageheight)
            file.write("/BitsPerComponent 8\n" "/ImageMatrix %s\n" % pstrafo)
            file.write("/Decode [1 0]\n" ">>\n" "/InterleaveType 1\n" ">>\n")

        if self.PSstoreimage:
            file.write("image\n")
        else:
            if self.PSbinexpand == 2:
                file.write("%%%%BeginData: %i ASCII Lines\n"
                           "image\n" % (asciihexlines(len(data)) + 1))
                asciihexstream(file, data)
                file.write(">\n")
            else:
                # the datasource is currentstream (plus some filters)
                file.write("%%%%BeginData: %i ASCII Lines\n"
                           "image\n" % (ascii85lines(len(data)) + 1))
                ascii85stream(file, data)
                file.write("~>\n")
            file.write("%%EndData\n")

        file.write("grestore\n")