コード例 #1
0
def MakeImageDocument(url, imageType, exportInfo):
    # First make a bitmap context for a US Letter size
    # raster at the requested resolution.
    dpi = exportInfo.dpi
    width = int(8.5 * dpi)
    height = int(11 * dpi)

    # For JPEG output type the bitmap should not be transparent. If other types are added that
    # do not support transparency, this code should be updated to check for those types as well.
    needTransparentBitmap = (imageType.lower() != kUTTypeJPEG.lower())

    # Create an RGB Bitmap context using the generic calibrated RGB color space
    # instead of the display color space.
    useDisplayColorSpace = False
    c = createRGBBitmapContext(width, height, useDisplayColorSpace,
                               needTransparentBitmap)

    if c is None:
        print >> sys.stderr, "Couldn't make destination bitmap context"
        return memFullErr

    # Scale the coordinate system based on the resolution in dots per inch.
    CGContextScaleCTM(c, dpi / 72, dpi / 72)

    # Set the font smoothing parameter to false since it's better to
    # draw any text without special LCD text rendering when creating
    # rendered data for export.
    if hasattr(Quartz, 'CGContextSetShouldSmoothFonts'):
        CGContextSetShouldSmoothFonts(c, False)

    # Set the scaling factor for shadows. This is a hack so that
    # drawing code that needs to know the scaling factor can
    # obtain it. Better would be that DispatchDrawing and the code
    # it calls would take this scaling factor as a parameter.
    Utilities.setScalingFactor(dpi / 72)

    # Draw into that raster...
    AppDrawing.DispatchDrawing(c, exportInfo.command)

    # Set the scaling factor back to 1.0.
    Utilities.setScalingFactor(1.0)

    # Create an image from the raster data. Calling
    # createImageFromBitmapContext gives up ownership
    # of the raster data used by the context.
    image = createImageFromBitmapContext(c)

    # Release the context now that the image is created.
    del c

    if image is None:
        # Users of this code should update this to be an error code they find useful.
        return memFullErr

    # Now export the image.
    if exportInfo.useQTForExport:
        exportCGImageToFileWithQT(image, url, imageType, exportInfo.dpi)
    else:
        exportCGImageToFileWithDestination(image, url, imageType,
                                           exportInfo.dpi)
コード例 #2
0
ファイル: MyView.py プロジェクト: rongilson/pyobjc
    def drawRect_(self, rect):
        context = Cocoa.NSGraphicsContext.currentContext().graphicsPort()

        if _pdfDocument is None:
            if _drawingCommand in (
                    UIHandling.kHICommandDrawNSString,
                    UIHandling.kHICommandDrawNSLayoutMgr,
                    UIHandling.kHICommandDrawCustomNSLayoutMgr,
            ):

                if _drawingCommand == UIHandling.kHICommandDrawNSString:
                    FrameworkTextDrawing.drawNSStringWithAttributes()

                elif _drawingCommand == UIHandling.kHICommandDrawNSLayoutMgr:
                    FrameworkTextDrawing.drawWithNSLayout()

                else:
                    FrameworkTextDrawing.drawWithCustomNSLayout()
            else:
                AppDrawing.DispatchDrawing(context, _drawingCommand)

        else:
            mediaRect = Quartz.CGPDFDocumentGetMediaBox(_pdfDocument, 1)
            mediaRect.origin.x = mediaRect.origin.y = 0
            Quartz.CGContextDrawPDFDocument(context, mediaRect, _pdfDocument,
                                            1)
コード例 #3
0
ファイル: PDFHandling.py プロジェクト: danchr/pyobjc-git
def MakePDFDocument(url, exportInfo):
    # Use this as the media box for the document.
    # In a real application this should be the bounding
    # rectangle of the graphics that will be the PDF content.
    mediaRect = Quartz.CGRectMake(0, 0, 612, 792)

    info = {
        # Add the title information for this document.
        Quartz.kCGPDFContextTitle:
        "BasicDrawing Sample Graphics",
        # Add the author information for this document. This is typically
        # the user creating the document.
        Quartz.kCGPDFContextAuthor:
        "David Gelphman and Bunny Laden",
        # The creator is the application creating the document.
        Quartz.kCGPDFContextCreator:
        "BasicDrawing Application",
    }

    if 0:
        # Before using the kCGPDFContextCropBox key, check to ensure that it
        # is available.
        if hasattr(Quartz, "kCFPDFContextCropBox"):
            # Prepare the crop box entry. Use this rectangle as the crop box for
            # this example.

            # XXX:fixme: need to encode as CFData!!!
            info[Quartz.kCGPDFContextCropBox] = Quartz.CGRectMake(
                100, 100, 200, 200)

    if url is not None:
        pdfContext = Quartz.CGPDFContextCreateWithURL(url, mediaRect, info)
        if pdfContext is not None:
            Quartz.CGContextBeginPage(pdfContext, mediaRect)
            if 1:
                Quartz.CGContextSaveGState(pdfContext)
                if 1:
                    Quartz.CGContextClipToRect(pdfContext, mediaRect)
                    AppDrawing.DispatchDrawing(pdfContext, exportInfo.command)
                Quartz.CGContextRestoreGState(pdfContext)
            Quartz.CGContextEndPage(pdfContext)
            del pdfContext
        else:
            print("Can't create PDF document!")
コード例 #4
0
def cfDataCreatePDFDocumentFromCommand(command):
    # Media rect for the drawing. In a real application this
    # should be the bounding rectangle of the graphics
    # that will be the PDF content.
    mediaRect = CGRectMake(0, 0, 612, 792)

    # Create a dictionary to hold the optional information describing the PDF data.
    dict = {}

    # Add the creator and title information to the PDF content.
    dict[kCGPDFContextTitle] = "Pasted From Sample Quartz Application"
    dict[kCGPDFContextCreator] = "Sample Quartz Application"

    # Create a mutable CFData object with unlimited capacity.
    data = CFDataCreateMutable(None, 0)
    if data is None:
        print >> sys.stderr, "Couldn't make CFData!"
        return None

    # Create the data consumer to capture the PDF data.
    consumer = DataProvidersAndConsumers.myCGDataConsumerCreateWithCFData(data)
    if consumer is None:
        print >> sys.stderr, "Couldn't create data consumer!"
        return None

    pdfContext, mediaRect = CGPDFContextCreate(consumer, None, dict)
    del consumer
    del dict

    if pdfContext is None:
        print >> sys.stderr, "Couldn't create pdf context!"
        return None

    mediaRect = CGContextBeginPage(pdfContext)
    if 1:
        CGContextSaveGState(pdfContext)
        if 1:
            CGContextClipToRect(pdfContext, mediaRect)
            AppDrawing.DispatchDrawing(pdfContext, command)
        CGContextRestoreGState(pdfContext)
    CGContextEndPage(pdfContext)

    return data