Example #1
0
    def decorate(self, dp, texrunner):
        if self.texrunner:
            texrunner = self.texrunner
        import text as textmodule
        self.defaulttextattrs = [textmodule.halign.center]

        dp.ensurenormpath()
        if self.arclenfrombegin is not None:
            textpos = dp.path.begin() + self.arclenfrombegin
        elif self.arclenfromend is not None:
            textpos = dp.path.end() - self.arclenfromend
        else:
            # relarcpos is used if neither arcfrombegin nor arcfromend is given
            textpos = self.relarclenpos * dp.path.arclen()

        textattrs = self.defaulttextattrs + self.textattrs
        t = texrunner.text(0, 0, self.text, textattrs, singlecharmode=1)
        t.ensuredvicanvas()

        c = canvas.canvas()
        for item in t.dvicanvas.items:
            bbox = item.bbox()
            if bbox:
                x = item.bbox().center()[0]
                atrafo = dp.path.trafo(textpos+x)
                c.insert(item, [trafo.translate(-x, 0), atrafo])
                if self.exclude is not None:
                    dp.excluderange(textpos+bbox.left()-self.exclude, textpos+bbox.right()+self.exclude)
            else:
                c.insert(item)
        dp.ornaments.insert(c)
Example #2
0
    def _process(self, processMethod, contentfile, writer, context, registry, bbox):
        # usually, it is the bbox of the canvas enlarged by self.bboxenlarge, but
        # it might be a different bbox as specified in the page constructor
        assert not bbox
        if self.pagebbox:
            bbox.set(self.pagebbox)
        else:
            bbox.set(self.canvas.bbox()) # this bbox is not accurate
            bbox.enlarge(self.bboxenlarge)

        # check whether we expect a page trafo and use a temporary canvas to insert the
        # page canvas
        if self.paperformat and (self.rotated or self.centered or self.fittosize) and bbox:
            # calculate the pagetrafo
            paperwidth, paperheight = self.paperformat.width, self.paperformat.height

            # center (optionally rotated) output on page
            if self.rotated:
                pagetrafo = trafo.rotate(90).translated(paperwidth, 0)
                if self.centered or self.fittosize:
                    if not self.fittosize and (bbox.height() > paperwidth or bbox.width() > paperheight):
                        warnings.warn("content exceeds the papersize")
                    pagetrafo = pagetrafo.translated(-0.5*(paperwidth - bbox.height()) + bbox.bottom(),
                                                      0.5*(paperheight - bbox.width()) - bbox.left())
            else:
                if not self.fittosize and (bbox.width() > paperwidth or bbox.height() > paperheight):
                    warnings.warn("content exceeds the papersize")
                pagetrafo = trafo.translate(0.5*(paperwidth - bbox.width())  - bbox.left(),
                                            0.5*(paperheight - bbox.height()) - bbox.bottom())

            if self.fittosize:

                if 2*self.margin > paperwidth or 2*self.margin > paperheight:
                    raise ValueError("Margins too broad for selected paperformat. Aborting.")

                paperwidth -= 2 * self.margin
                paperheight -= 2 * self.margin

                # scale output to pagesize - margins
                if self.rotated:
                    sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
                else:
                    sfactor = min(unit.topt(paperwidth)/bbox.width_pt(), unit.topt(paperheight)/bbox.height_pt())

                pagetrafo = pagetrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)

            bbox.transform(pagetrafo)
            cc = canvasmodule.canvas()
            cc.insert(self.canvas, [pagetrafo])
        else:
            cc = self.canvas

        getattr(style.linewidth.normal, processMethod)(contentfile, writer, context, registry, bbox)
        if self.pagebbox:
            bbox = bbox.copy() # don't alter the bbox provided to the constructor -> use a copy
        getattr(cc, processMethod)(contentfile, writer, context, registry, bbox)
Example #3
0
    def _process(self, processMethod, contentfile, writer, context, registry, bbox):
        # usually, it is the bbox of the canvas enlarged by self.bboxenlarge, but
        # it might be a different bbox as specified in the page constructor
        assert not bbox
        if self.pagebbox:
            bbox.set(self.pagebbox)
        else:
            bbox.set(self.canvas.bbox()) # this bbox is not accurate
            bbox.enlarge(self.bboxenlarge)

        # check whether we expect a page trafo and use a temporary canvas to insert the
        # page canvas
        if self.paperformat and (self.rotated or self.centered or self.fittosize) and bbox:
            # calculate the pagetrafo
            paperwidth, paperheight = self.paperformat.width, self.paperformat.height

            # center (optionally rotated) output on page
            if self.rotated:
                pagetrafo = trafo.rotate(90).translated(paperwidth, 0)
                if self.centered or self.fittosize:
                    if not self.fittosize and (bbox.height() > paperwidth or bbox.width() > paperheight):
                        warnings.warn("content exceeds the papersize")
                    pagetrafo = pagetrafo.translated(-0.5*(paperwidth - bbox.height()) + bbox.bottom(),
                                                      0.5*(paperheight - bbox.width()) - bbox.left())
            else:
                if not self.fittosize and (bbox.width() > paperwidth or bbox.height() > paperheight):
                    warnings.warn("content exceeds the papersize")
                pagetrafo = trafo.translate(0.5*(paperwidth - bbox.width())  - bbox.left(),
                                            0.5*(paperheight - bbox.height()) - bbox.bottom())

            if self.fittosize:

                if 2*self.margin > paperwidth or 2*self.margin > paperheight:
                    raise ValueError("Margins too broad for selected paperformat. Aborting.")

                paperwidth -= 2 * self.margin
                paperheight -= 2 * self.margin

                # scale output to pagesize - margins
                if self.rotated:
                    sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
                else:
                    sfactor = min(unit.topt(paperwidth)/bbox.width_pt(), unit.topt(paperheight)/bbox.height_pt())

                pagetrafo = pagetrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)

            bbox.transform(pagetrafo)
            cc = canvasmodule.canvas()
            cc.insert(self.canvas, [pagetrafo])
        else:
            cc = self.canvas

        getattr(style.linewidth.normal, processMethod)(contentfile, writer, context, registry, bbox)
        if self.pagebbox:
            bbox = bbox.copy() # don't alter the bbox provided to the constructor -> use a copy
        getattr(cc, processMethod)(contentfile, writer, context, registry, bbox)
Example #4
0
    def _process(self, processMethod, contentfile, writer, context, registry,
                 bbox):
        assert not bbox

        # check whether we expect a page trafo and use a temporary canvasfile to insert the
        # pagetrafo in front after the bbox was calculated
        expectpagetrafo = self.paperformat and (self.rotated or self.centered
                                                or self.fittosize)
        if expectpagetrafo:
            canvasfile = cStringIO.StringIO()
        else:
            canvasfile = contentfile

        getattr(style.linewidth.normal, processMethod)(canvasfile, writer,
                                                       context, registry, bbox)
        getattr(self.canvas, processMethod)(canvasfile, writer, context,
                                            registry, bbox)

        # usually its the bbox of the canvas enlarged by self.bboxenlarge, but
        # it might be a different bbox as specified in the page constructor
        if self.pagebbox:
            bbox.set(self.pagebbox)
        elif bbox:
            bbox.enlarge(self.bboxenlarge)

        if expectpagetrafo:

            if bbox:
                # calculate the pagetrafo
                paperwidth, paperheight = self.paperformat.width, self.paperformat.height

                # center (optionally rotated) output on page
                if self.rotated:
                    pagetrafo = trafo.rotate(90).translated(paperwidth, 0)
                    if self.centered or self.fittosize:
                        if not self.fittosize and (bbox.height() > paperwidth
                                                   or
                                                   bbox.width() > paperheight):
                            warnings.warn("content exceeds the papersize")
                        pagetrafo = pagetrafo.translated(
                            -0.5 * (paperwidth - bbox.height()) +
                            bbox.bottom(),
                            0.5 * (paperheight - bbox.width()) - bbox.left())
                else:
                    if not self.fittosize and (bbox.width() > paperwidth
                                               or bbox.height() > paperheight):
                        warnings.warn("content exceeds the papersize")
                    pagetrafo = trafo.translate(
                        0.5 * (paperwidth - bbox.width()) - bbox.left(),
                        0.5 * (paperheight - bbox.height()) - bbox.bottom())

                if self.fittosize:

                    if 2 * self.margin > paperwidth or 2 * self.margin > paperheight:
                        raise ValueError(
                            "Margins too broad for selected paperformat. Aborting."
                        )

                    paperwidth -= 2 * self.margin
                    paperheight -= 2 * self.margin

                    # scale output to pagesize - margins
                    if self.rotated:
                        sfactor = min(
                            unit.topt(paperheight) / bbox.width_pt(),
                            unit.topt(paperwidth) / bbox.height_pt())
                    else:
                        sfactor = min(
                            unit.topt(paperwidth) / bbox.width_pt(),
                            unit.topt(paperheight) / bbox.height_pt())

                    pagetrafo = pagetrafo.scaled(
                        sfactor, sfactor, self.margin + 0.5 * paperwidth,
                        self.margin + 0.5 * paperheight)

                # apply the pagetrafo and write it to the contentfile
                bbox.transform(pagetrafo)
                pagetrafofile = cStringIO.StringIO()
                # context, bbox, registry are just passed as stubs (the trafo should not touch them)
                getattr(pagetrafo, processMethod)(pagetrafofile, writer,
                                                  context, registry, bbox)
                contentfile.write(pagetrafofile.getvalue())
                pagetrafofile.close()

            contentfile.write(canvasfile.getvalue())
            canvasfile.close()
Example #5
0
        url, name = line.rstrip().split('\t')
        pdffilename = url.split('/')[-1]
        metafilename = year + '/' + pdffilename[:-3] + 'meta'
        metadata = {
            'url': url,
            'name': name,
            'name_en': name,
            'pages': readnumfromfile(year + '/' + pdffilename + '.pages'),
            'words': readnumfromfile(year + '/' + pdffilename + '.words'),
        }
        # translate name to other langs
        if 'é' in name:
            srclang = 'fr'
            metadata['name_fr'] = name
            tgtlang = 'en'
            name_en = translate(name, srclang, tgtlang)
            if name_en:
                metadata['name_' + tgtlang] = name_en
                name = name_en
                srclang = 'en'
                for tgtlang in ['cs', 'de']:
                    name_tr = translate(name, srclang, tgtlang)
                    if name_tr:
                        metadata['name_' + tgtlang] = name_tr
        else:
            srclang = 'en'
            for tgtlang in ['cs', 'de', 'fr']:
                name_tr = translate(name, srclang, tgtlang)
                if name_tr:
                    metadata['name_' + tgtlang] = name_tr
Example #6
0
 def linealign(self, *args):
     self.transform(trafo.translate(*self.linealignvector(*args)))
     return self
Example #7
0
with open('namelist.csv') as linklist:
    for line in linklist:
        filename, name = line.rstrip().split('\t')
        if filename[1:3] == year[-2:]:
            txtfilename = year + '/' + filename + '.txt'
            metafilename = year + '/' + filename + '.meta'
            print(year + '/' + filename)
            if path.exists(txtfilename) and not path.exists(metafilename):
                print('is go')
                metadata = {
                        'name': name,
                        'pages': readnumfromfile(year + '/' + filename + '.pages'),
                        'words': readnumfromfile(year + '/' + filename + '.words'),
                        }
                # translate name to other langs
                metadata['name_cs'] = name
                name_en = translate(name, 'cs', 'en')
                if name_en:
                    metadata['name_en'] = name_en
                    for tgtlang in ['fr', 'de']:
                        name_tr = translate(name_en, 'en', tgtlang)
                        if name_tr:
                            metadata['name_'+tgtlang] = name_tr

                # output
                with open(metafilename, 'w') as metafile:
                    json.dump(metadata, metafile, indent=4)


Example #8
0
    def _process(self, processMethod, contentfile, writer, context, registry, bbox):
        assert not bbox

        # check whether we expect a page trafo and use a temporary canvasfile to insert the
        # pagetrafo in front after the bbox was calculated
        expectpagetrafo = self.paperformat and (self.rotated or self.centered or self.fittosize)
        if expectpagetrafo:
            canvasfile = cStringIO.StringIO()
        else:
            canvasfile = contentfile

        getattr(style.linewidth.normal, processMethod)(canvasfile, writer, context, registry, bbox)
        getattr(self.canvas, processMethod)(canvasfile, writer, context, registry, bbox)

        # usually its the bbox of the canvas enlarged by self.bboxenlarge, but
        # it might be a different bbox as specified in the page constructor
        if self.pagebbox:
            bbox.set(self.pagebbox)
        elif bbox:
            bbox.enlarge(self.bboxenlarge)

        if expectpagetrafo:

            if bbox:
                # calculate the pagetrafo
                paperwidth, paperheight = self.paperformat.width, self.paperformat.height

                # center (optionally rotated) output on page
                if self.rotated:
                    pagetrafo = trafo.rotate(90).translated(paperwidth, 0)
                    if self.centered or self.fittosize:
                        if not self.fittosize and (bbox.height() > paperwidth or bbox.width() > paperheight):
                            warnings.warn("content exceeds the papersize")
                        pagetrafo = pagetrafo.translated(-0.5*(paperwidth - bbox.height()) + bbox.bottom(),
                                                          0.5*(paperheight - bbox.width()) - bbox.left())
                else:
                    if not self.fittosize and (bbox.width() > paperwidth or bbox.height() > paperheight):
                        warnings.warn("content exceeds the papersize")
                    pagetrafo = trafo.translate(0.5*(paperwidth - bbox.width())  - bbox.left(),
                                                0.5*(paperheight - bbox.height()) - bbox.bottom())

                if self.fittosize:

                    if 2*self.margin > paperwidth or 2*self.margin > paperheight:
                        raise ValueError("Margins too broad for selected paperformat. Aborting.")

                    paperwidth -= 2 * self.margin
                    paperheight -= 2 * self.margin

                    # scale output to pagesize - margins
                    if self.rotated:
                        sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
                    else:
                        sfactor = min(unit.topt(paperwidth)/bbox.width_pt(), unit.topt(paperheight)/bbox.height_pt())

                    pagetrafo = pagetrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)

                # apply the pagetrafo and write it to the contentfile
                bbox.transform(pagetrafo)
                pagetrafofile = cStringIO.StringIO()
                # context, bbox, registry are just passed as stubs (the trafo should not touch them)
                getattr(pagetrafo, processMethod)(pagetrafofile, writer, context, registry, bbox)
                contentfile.write(pagetrafofile.getvalue())
                pagetrafofile.close()

            contentfile.write(canvasfile.getvalue())
            canvasfile.close()
Example #9
0
 def linealign(self, *args):
     self.transform(trafo.translate(*self.linealignvector(*args)))
     return self