Reviewed by Darin.
authordarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 9 Feb 2006 04:05:15 +0000 (04:05 +0000)
committerdarin <darin@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 9 Feb 2006 04:05:15 +0000 (04:05 +0000)
        - Fix http://bugzilla.opendarwin.org/show_bug.cgi?id=3527
        Allow Safari to open postscript files in browser windows as well

        * WebView/WebPDFRepresentation.m:
        (+[WebPDFRepresentation postScriptMIMETypes]): Added.
        (+[WebPDFRepresentation supportedMIMETypes]): Include PostScript MIME types.
        (-[WebPDFRepresentation convertPostScriptDataSourceToPDF:]): Added.
        (-[WebPDFRepresentation finishedLoadingWithDataSource:]):
        Handle PostScript conversion using new convertPostScriptDataSourceToPDF method.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@12679 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebKit/ChangeLog
WebKit/WebView/WebPDFRepresentation.m

index f64d05c128f7ea428ba8f2c055095a46eaeaa842..a23fcc96e5c77ec1f3d928fc387bc9f455f28da8 100644 (file)
@@ -1,3 +1,17 @@
+2006-02-08  David Kilzer  <ddkilzer@kilzer.net>
+
+        Reviewed by Darin.
+
+        - Fix http://bugzilla.opendarwin.org/show_bug.cgi?id=3527
+        Allow Safari to open postscript files in browser windows as well
+
+        * WebView/WebPDFRepresentation.m:
+        (+[WebPDFRepresentation postScriptMIMETypes]): Added.
+        (+[WebPDFRepresentation supportedMIMETypes]): Include PostScript MIME types.
+        (-[WebPDFRepresentation convertPostScriptDataSourceToPDF:]): Added.
+        (-[WebPDFRepresentation finishedLoadingWithDataSource:]):
+        Handle PostScript conversion using new convertPostScriptDataSourceToPDF method.
+
 2006-02-07  Alexey Proskuryakov  <ap@nypop.com>
 
         Reviewed by Timothy.
index 94261bba11f9ae805b8083672e3ffe66c883972e..391308c5091b331c1e4abd63a33a776390f1b322 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005 Apple Computer, Inc.  All rights reserved.
+ * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,6 +30,7 @@
 #import <WebKit/WebDataSource.h>
 #import <WebKit/WebFrame.h>
 #import <WebKit/WebFrameView.h>
+#import <WebKit/WebNSObjectExtras.h>
 #import <WebKit/WebPDFRepresentation.h>
 #import <WebKit/WebPDFView.h>
 
 
 @implementation WebPDFRepresentation
 
-+ (NSArray *)supportedMIMETypes
++ (NSArray *)postScriptMIMETypes
 {
     return [NSArray arrayWithObjects:
+        @"application/postscript",
+        nil];
+}
+
++ (NSArray *)supportedMIMETypes
+{
+    return [[[self class] postScriptMIMETypes] arrayByAddingObjectsFromArray:
+        [NSArray arrayWithObjects:
             @"text/pdf",
             @"application/pdf",
-            nil];
+            nil]];
 }
 
 + (Class)PDFDocumentClass
 {
 }
 
+- (NSData *)convertPostScriptDataSourceToPDF:(NSData *)data
+{
+    // Convert PostScript to PDF using Quartz 2D API
+    // http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_ps_convert/chapter_16_section_1.html
+
+    CGPSConverterCallbacks callbacks = { 0, 0, 0, 0, 0, 0, 0, 0 };    
+    CGPSConverterRef converter = CGPSConverterCreate(0, &callbacks, 0);
+    ASSERT(converter);
+
+    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
+    ASSERT(provider);
+
+    CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
+    ASSERT(result);
+
+    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(result);
+    ASSERT(consumer);
+
+    // Error handled by detecting zero-length 'result' in caller
+    CGPSConverterConvert(converter, provider, consumer, 0);
+
+    CFRelease(converter);
+    CFRelease(provider);
+    CFRelease(consumer);
+
+    return WebCFAutorelease(result);
+}
+
 - (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
 {
+    NSData *data = [dataSource data];
+
+    NSArray *postScriptMIMETypes = [[self class] postScriptMIMETypes];
+    NSString *mimeType = [[dataSource response] MIMEType];
+    if ([postScriptMIMETypes containsObject:mimeType]) {
+        data = [self convertPostScriptDataSourceToPDF:data];
+        if ([data length] == 0)
+            return;
+    }
+
     WebPDFView *view = (WebPDFView *)[[[dataSource webFrame] frameView] documentView];
-    PDFDocument *doc = [[[[self class] PDFDocumentClass] alloc] initWithData:[dataSource data]];
+    PDFDocument *doc = [[[[self class] PDFDocumentClass] alloc] initWithData:data];
     [[view PDFSubview] setDocument:doc];
     [doc release];
 }