# java wrapper for reportlab libraries

from reportlab.pdfgen.canvas import Canvas
from rlextra.pageCatcher import pageCatcher

import SJCanvas
class JCanvas(Canvas, SJCanvas):
    def __init__(self, *args, **kw):
        Canvas.__init__(self, *args, **kw)
        self._pageCaughtFiles = {}

    def copyPages(self, pdfFileName):
        pageCatcher.copyPages(pdfFileName, self)

    def annotate(self, out, pdfin, x, y, font, fontSize, text):
        pageCatcher.annotate(out, pdfin, x, y, font, fontSize, text)

    def rml_to_pdf(self, xmlInputFile):
        from rlextra.rml2pdf.rml2pdf import go
        go(open(xmlInputFile).read())

    # below are original Canvas methods
    def setAuthor(self, author):
        Canvas.setAuthor(self, author)

    def addOutlineEntry(self, title, key, level=0, closed=None):
        Canvas.addOutlineEntry(self, title, key, level, closed)

    def setTitle(self, title):
        Canvas.setTitle(self, title)

    def setSubject(self, subject):
        Canvas.setSubject(self, title)

    def pageHasData(self):
        return Canvas.pageHasData(self)

    def showOutline(self):
        Canvas.showOutline(self)

    def showFullScreen0(self):
        Canvas.showFullScreen0(self)

    def showPage(self):
        Canvas.showPage(self)

    def setPageCallBack(self, cb):
        def cb_func(pageNumber,cb=cb):
            cb.run(pageNumber)
        Canvas.setPageCallBack(self, cb_func)

    def bookmarkPage(self, key, fitType="Fit",
                     left=None, top=None, bottom=None,
                     right=None, zoom=None):
        Canvas.bookmarkPage(self, key, fitType,
                            left, top, bottom,
                            right, zoom)

    def bookmarkHorizontalAbsolute(self, key, yhorizontal):
        Canvas.bookmarkHorizontalAbsolute(self, key, yhorizontal)

    def bookmarkHorizontal(self, key, relativeX, relativeY):
        Canvas.bookmarkHorizontal(self, key, relativeX, relativeY)

    def doForm(self, name):
        Canvas.doForm(self, name)

    def hasForm(self, name):
        Canvas.hasForm(self, name)

        ######################################################
        #
        #   Image routines
        #
        ######################################################

    def drawInlineImage(self, image, x, y, width=None, height=None):
        return Canvas.drawInlineImage(self, image, x, y, width, height)

    def drawImage(self, image, x, y, width=None, height=None, mask=None):
        return Canvas.drawImage(self, image, x, y, width, height, mask)


    def beginForm(self, name, lowerx=0, lowery=0, upperx=None, uppery=None):
        Canvas.beginForm(self, name, lowerx, lowery, upperx, uppery)

    def endForm(self):
        Canvas.endForm(self)

    def textAnnotation0(self, contents, Rect=None, addtopage=1, name=None, **kw):
        "Experimental."
        Canvas.textAnnotation0(self, contents, Rect, addtopage, name, kw)

    def inkAnnotation0(self, contents, InkList=None, Rect=None, addtopage=1, name=None, **kw):
        "Experimental"
        Canvas.inkAnnotation0(self, contents, InkList, Rect, addtopage, name, kw)

    def linkAbsolute(self, contents, destinationname, Rect=None, addtopage=1, name=None, **kw):
        Canvas.linkAbsolute(self, contents, destinationname, Rect, addtopage, name, kw)

    def linkRect(self, contents, destinationname, Rect=None, addtopage=1, name=None, **kw):
        return Canvas.linkRect(self, contents, destinationname, Rect, addtopage, name, kw)

    def linkURL(self, url, rect, relative=0, thickness=0, color=None, dashArray=None):
        Canvas.linkURL(self, url, rect, relative, thickness, color, dashArray)

    def getPageNumber(self):
        return Canvas.getPageNumber(self)

    def save(self):
        Canvas.save(self)

    def getpdfdata(self):
        return Canvas.getpdfdata(self)

    def setPageSize(self, width, height):
        Canvas.setPageSize(self, (width, height))

    def setPageRotation(self, rot):
        Canvas.setPageRotation(self, rot)

    def addLiteral(self, s, escaped=1):
        Canvas.addLiteral(self, s, escaped)

        ######################################################################
        #
        #      coordinate transformations
        #
        ######################################################################
    def resetTransforms(self):
        Canvas.resetTransforms(self)

    def transform(self, a,b,c,d,e,f):
        Canvas.transform(self, a,b,c,d,e,f)

    def absolutePosition(self, x, y):
        return Canvas.absolutePosition(self, x, y)

    def translate(self, dx, dy):
        Canvas.translate(self, dx, dy)

    def scale(self, x, y):
        Canvas.scale(self, x, y)

    def rotate(self, theta):
        Canvas.rotate(self, theta)

    def skew(self, alpha, beta):
        Canvas.skew(self, alpha, beta)

        ######################################################################
        #
        #      graphics state management
        #
        ######################################################################

    def saveState(self):
        Canvas.saveState(self)

    def restoreState(self):
        Canvas.restoreState(self)

        ###############################################################
        #
        #   Drawing methods.  These draw things directly without
        #   fiddling around with Path objects.  We can add any geometry
        #   methods we wish as long as their meaning is precise and
        #   they are of general use.
        #
        #   In general there are two patterns.  Closed shapes
        #   have the pattern shape(self, args, stroke=1, fill=0);
        #   by default they draw an outline only. Line segments come
        #   in three flavours: line, bezier, arc (which is a segment
        #   of an elliptical arc, approximated by up to four bezier
        #   curves, one for each quadrant.
        #
        #   In the case of lines, we provide a 'plural' to unroll
        #   the inner loop; it is useful for drawing big grids
        ################################################################

        #--------first the line drawing methods-----------------------

    def line(self, x1,y1, x2,y2):
        Canvas.line(self, x1,y1, x2,y2)

    def lines(self, linelist):
        Canvas.lines(self, linelist)

    def grid(self, xlist, ylist):
        Canvas.grid(self, xlist, ylist)

    def bezier(self, x1, y1, x2, y2, x3, y3, x4, y4):
        Canvas.bezier(self, x1, y1, x2, y2, x3, y3, x4, y4)

    def arc(self, x1,y1, x2,y2, startAng=0, extent=90):
        Canvas.arc(self, x1,y1, x2,y2, startAng, extent)

        #--------now the shape drawing methods-----------------------

    def rect(self, x, y, width, height, stroke=1, fill=0):
        Canvas.rect(self, x, y, width, height, stroke, fill)

    def ellipse(self, x1, y1, x2, y2, stroke=1, fill=0):
        Canvas.ellipse(self, x1, y1, x2, y2, stroke, fill)

    def wedge(self, x1,y1, x2,y2, startAng, extent, stroke=1, fill=0):
        Canvas.wedge(self, x1,y1, x2,y2, startAng, extent, stroke, fill)

    def circle(self, x_cen, y_cen, r, stroke=1, fill=0):
        Canvas.circle(self, x_cen, y_cen, r, stroke, fill)

    def roundRect(self, x, y, width, height, radius, stroke=1, fill=0):
        Canvas.roundRect(self, x, y, width, height, radius, stroke, fill)

        ##################################################
        #
        #  Text methods
        #
        # As with graphics, a separate object ensures that
        # everything is bracketed between  text operators.
        # The methods below are a high-level convenience.
        # use PDFTextObject for multi-line text.
        ##################################################

    def setFillColorCMYK(self, c, m, y, k):
         Canvas.setFillColorCMYK(self, c, m, y, k)

    def setStrokeColorCMYK(self, c, m, y, k):
         Canvas.setStrokeColorCMYK(self, c, m, y, k)

    def drawString(self, x, y, text):
        Canvas.drawString(self, x, y, text)

    def drawRightString(self, x, y, text):
        Canvas.drawRightString(self, x, y, text)

    def drawCentredString(self, x, y, text):
        Canvas.drawCentredString(self, x, y, text)

    def getAvailableFonts(self):
        return Canvas.getAvailableFonts(self)

    def addFont(self, fontObj):
        Canvas.addFont(self, fontObj)

    def listLoadedFonts0(self):
        return Canvas.listLoadedFonts0(self)

    def setFont(self, fontName, size):
        Canvas.setFont(self, fontName, size)

    def stringWidth(self, text, fontName, fontSize, encoding=None):
        return Canvas.stringWidth(self, text, fontName, fontSize, encoding)

    # basic graphics modes

    def setLineWidth(self, width):
        Canvas.setLineWidth(self, width)

    def setLineCap(self, mode):
        Canvas.setLineCap(self, mode)

    def setLineJoin(self, mode):
        Canvas.setLineJoin(self, mode)

    def setMiterLimit(self, limit):
        Canvas.setMiterLimit(self, limit)

    def setDash(self, array=[], phase=0):
        Canvas.setDash(self, array, phase)

    def setFillColorRGB(self, r, g, b):
        Canvas.setFillColorRGB(self, r, g, b)

    def setStrokeColorRGB(self, r, g, b):
        Canvas.setStrokeColorRGB(self, r, g, b)

    def setFillColor(self, aColor):
        Canvas.setFillColor(self, aColor)

    def setStrokeColor(self, aColor):
        Canvas.setStrokeColor(self, aColor)

    def setFillGray(self, gray):
        Canvas.setFillGray(self, gray)

    def setStrokeGray(self, gray):
        Canvas.setStrokeGray(self, gray)

    # path stuff - the separate path object builds it

    def beginPath(self):
        return Canvas.beginPath(self)

    def drawPath(self, aPath, stroke=1, fill=0):
        Canvas.drawPath(self, aPath, stroke, fill)

    def clipPath(self, aPath, stroke=1, fill=0):
        Canvas.clipPath(self, aPath, stroke, fill)

    def beginText(self, x=0, y=0):
        return Canvas.beginText(self, x, y)

    def drawText(self, aTextObject):
        Canvas.drawText(self, aTextObject)

    def setPageCompression(self, pageCompression=None):
        Canvas.setPageCompression(self, pageCompression)

    def setPageDuration(self, duration=None):
        Canvas.setPageDuration(self, duration)

    def setPageTransition(self, effectname=None, duration=1,
                        direction=0,dimension='H',motion='I'):
        Canvas.setPageTransition(self, effectname, duration,
                        direction,dimension,motion)


if __name__=='__main__':
    c = JCanvas('jcanvas_test.pdf')
    c.setFont('Helvetica-Bold',24)
    c.drawString(10, 700, 'Jcanvas at work...')
    c.showPage()
    c.copyPages('ir684.pdf')
    c.showPage()
    c.setFont('Helvetica-Bold',24)
    c.drawString(10, 700, 'did it work?...')
    c.save()
    print 'saved jcanvas_test.pdf'

