• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /macosx-10.10.1/pyobjc-45/2.6/pyobjc/pyobjc-framework-Quartz/Examples/Programming with Quartz/BasicDrawing/
1import AppDrawing
2import FrameworkUtilities
3import Utilities
4import DataProvidersAndConsumers
5
6from Quartz import *
7import Quartz
8
9import sys
10
11def createNewPDFRefFromPasteBoard():
12    # Create a reference to the PDF data on the pasteboard.
13    # The implementation of myCreatePDFDataFromPasteBoard depends
14    # on the application framework you are using for your application.
15    #
16    # myCreatePDFDataFromPasteBoard creates a reference that is owned
17    # by the calling application.
18    pasteBoardData = FrameworkUtilities.myCreatePDFDataFromPasteBoard()
19
20    if pasteBoardData is None:
21        print >>sys.stderr, "There is no PDF data on pasteboard!"
22        return None
23
24    # Create a data provider from the pasteboard data.
25    dataProvider = myCGDataProviderCreateWithCFData(pasteBoardData)
26    # Release the pasteboard data since the data provider retains
27    # it and this code owns a reference but no longer requires it.
28    del pasteBoardData
29
30    if dataProvider is None:
31        print >>sys.stderr, "Couldn't create data provider."
32        return None
33
34    pasteBoardPDFDocument = CGPDFDocumentCreateWithProvider(dataProvider)
35    # Release the data provider now that the code is done with it.
36    del dataProvider
37
38    if pasteBoardPDFDocument is None:
39        print >>sys.stderr, "Couldn't create PDF document from pasteboard data provider."
40        return None
41    return pasteBoardPDFDocument
42
43_pdfDoc = None
44def getPasteBoardPDFDoc(reset):
45    global _pdfDoc
46    if reset:
47        # Release any existing document.
48        _pdfDoc = createNewPDFRefFromPasteBoard()
49    else:
50        # If there isn't already one, create it fresh.
51        if _pdfDoc is None:
52            _pdfDoc = createNewPDFRefFromPasteBoard()
53    return _pdfDoc
54
55
56def drawPasteBoardPDF(context):
57    pdfDoc = getPasteBoardPDFDoc(False)   # Obtain the existing one.
58    if pdfDoc is None:
59        print >>sys.stderr, "Quartz couldn't create CGPDFDocumentRef from pasteboard."
60        return
61
62    # The media box is the bounding box of the PDF document.
63    pdfRect = CGPDFDocumentGetMediaBox(pdfDoc , 1);   # page 1
64    # Make the destination rect origin at the Quartz origin.
65    pdfRect.origin.x = pdfRect.origin.y = 0.;
66    CGContextDrawPDFDocument(context, pdfRect, pdfDoc, 1);  # page 1
67
68def cfDataCreatePDFDocumentFromCommand( command):
69    # Media rect for the drawing. In a real application this
70    # should be the bounding rectangle of the graphics
71    # that will be the PDF content.
72    mediaRect = CGRectMake(0, 0, 612, 792)
73
74    # Create a dictionary to hold the optional information describing the PDF data.
75    dict = {}
76
77    # Add the creator and title information to the PDF content.
78    dict[kCGPDFContextTitle] = "Pasted From Sample Quartz Application"
79    dict[kCGPDFContextCreator] = "Sample Quartz Application"
80
81    # Create a mutable CFData object with unlimited capacity.
82    data = CFDataCreateMutable(None, 0)
83    if data is None:
84        print >>sys.stderr, "Couldn't make CFData!"
85        return None
86
87    # Create the data consumer to capture the PDF data.
88    consumer = DataProvidersAndConsumers.myCGDataConsumerCreateWithCFData(data)
89    if consumer is None:
90        print >>sys.stderr, "Couldn't create data consumer!"
91        return None
92
93    pdfContext, mediaRect = CGPDFContextCreate(consumer, None, dict)
94    del consumer
95    del dict
96
97    if pdfContext is None:
98        print >>sys.stderr, "Couldn't create pdf context!"
99        return None
100
101    mediaRect = CGContextBeginPage(pdfContext)
102    if 1:
103        CGContextSaveGState(pdfContext)
104        if 1:
105            CGContextClipToRect(pdfContext, mediaRect)
106            AppDrawing.DispatchDrawing(pdfContext, command)
107        CGContextRestoreGState(pdfContext)
108    CGContextEndPage(pdfContext)
109
110    return data
111
112def MakePDFDocument(url, exportInfo):
113    # Use this as the media box for the document.
114    # In a real application this should be the bounding
115    # rectangle of the graphics that will be the PDF content.
116    mediaRect = CGRectMake(0, 0, 612, 792)
117
118    info = {
119        # Add the title information for this document.
120        kCGPDFContextTitle: "BasicDrawing Sample Graphics",
121
122        # Add the author information for this document. This is typically
123        # the user creating the document.
124        kCGPDFContextAuthor: "David Gelphman and Bunny Laden",
125
126        # The creator is the application creating the document.
127        kCGPDFContextCreator: "BasicDrawing Application",
128    }
129
130    if 0:
131        # Before using the kCGPDFContextCropBox key, check to ensure that it
132        # is available.
133        if hasattr(Quartz, 'kCFPDFContextCropBox'):
134            # Prepare the crop box entry. Use this rectangle as the crop box for
135            # this example.
136
137            # XXX:fixme: need to encode as CFData!!!
138            info[kCGPDFContextCropBox] = CGRectMake(100, 100, 200, 200)
139
140    if url is not None:
141        pdfContext = CGPDFContextCreateWithURL(url, mediaRect, info)
142        if pdfContext is not None:
143            CGContextBeginPage(pdfContext, mediaRect)
144            if 1:
145                CGContextSaveGState(pdfContext)
146                if 1:
147                    CGContextClipToRect(pdfContext, mediaRect)
148                    AppDrawing.DispatchDrawing(pdfContext, exportInfo.command)
149                CGContextRestoreGState(pdfContext)
150            CGContextEndPage(pdfContext)
151            del pdfContext
152        else:
153            print >>sys.stderr, "Can't create PDF document!"
154