• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2005, 2006 Apple Computer, Inc.  All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1.  Redistributions of source code must retain the above copyright
9 *     notice, this list of conditions and the following disclaimer.
10 * 2.  Redistributions in binary form must reproduce the above copyright
11 *     notice, this list of conditions and the following disclaimer in the
12 *     documentation and/or other materials provided with the distribution.
13 * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 *     its contributors may be used to endorse or promote products derived
15 *     from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#import <WebKit/WebDataSourcePrivate.h>
30#import <WebKit/WebFrame.h>
31#import <WebKit/WebFrameView.h>
32#import <WebKit/WebNSObjectExtras.h>
33#import <WebKit/WebPDFRepresentation.h>
34#import <WebKit/WebPDFView.h>
35#import <wtf/Assertions.h>
36
37#import <PDFKit/PDFDocument.h>
38
39@implementation WebPDFRepresentation
40
41+ (NSArray *)postScriptMIMETypes
42{
43    return [NSArray arrayWithObjects:
44        @"application/postscript",
45        nil];
46}
47
48+ (NSArray *)supportedMIMETypes
49{
50    return [[[self class] postScriptMIMETypes] arrayByAddingObjectsFromArray:
51        [NSArray arrayWithObjects:
52            @"text/pdf",
53            @"application/pdf",
54            nil]];
55}
56
57+ (Class)PDFDocumentClass
58{
59    static Class PDFDocumentClass = nil;
60    if (PDFDocumentClass == nil) {
61        PDFDocumentClass = [[WebPDFView PDFKitBundle] classNamed:@"PDFDocument"];
62        if (PDFDocumentClass == nil) {
63            LOG_ERROR("Couldn't find PDFDocument class in PDFKit.framework");
64        }
65    }
66    return PDFDocumentClass;
67}
68
69- (void)setDataSource:(WebDataSource *)dataSource;
70{
71}
72
73- (void)receivedData:(NSData *)data withDataSource:(WebDataSource *)dataSource;
74{
75}
76
77- (void)receivedError:(NSError *)error withDataSource:(WebDataSource *)dataSource;
78{
79}
80
81- (NSData *)convertPostScriptDataSourceToPDF:(NSData *)data
82{
83    // Convert PostScript to PDF using Quartz 2D API
84    // http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_ps_convert/chapter_16_section_1.html
85
86    CGPSConverterCallbacks callbacks = { 0, 0, 0, 0, 0, 0, 0, 0 };
87    CGPSConverterRef converter = CGPSConverterCreate(0, &callbacks, 0);
88    ASSERT(converter);
89
90    CGDataProviderRef provider = CGDataProviderCreateWithCFData((CFDataRef)data);
91    ASSERT(provider);
92
93    CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0);
94    ASSERT(result);
95
96    CGDataConsumerRef consumer = CGDataConsumerCreateWithCFData(result);
97    ASSERT(consumer);
98
99    // Error handled by detecting zero-length 'result' in caller
100    CGPSConverterConvert(converter, provider, consumer, 0);
101
102    CFRelease(converter);
103    CFRelease(provider);
104    CFRelease(consumer);
105
106    return WebCFAutorelease(result);
107}
108
109- (void)finishedLoadingWithDataSource:(WebDataSource *)dataSource
110{
111    NSData *data = [dataSource data];
112
113    NSArray *postScriptMIMETypes = [[self class] postScriptMIMETypes];
114    NSString *mimeType = [dataSource _responseMIMEType];
115    if ([postScriptMIMETypes containsObject:mimeType]) {
116        data = [self convertPostScriptDataSourceToPDF:data];
117        if ([data length] == 0)
118            return;
119    }
120
121    WebPDFView *view = (WebPDFView *)[[[dataSource webFrame] frameView] documentView];
122    PDFDocument *doc = [[[[self class] PDFDocumentClass] alloc] initWithData:data];
123    [view setPDFDocument:doc];
124    [doc release];
125}
126
127
128- (BOOL)canProvideDocumentSource
129{
130    return NO;
131}
132
133
134- (NSString *)documentSource
135{
136    return nil;
137}
138
139
140- (NSString *)title
141{
142    return nil;
143}
144
145@end
146