1/*
2 * Copyright (C) 2004, 2005, 2006, 2013 Apple 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 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 *    notice, this list of conditions and the following disclaimer in the
11 *    documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef PDFDocumentImage_h
27#define PDFDocumentImage_h
28
29#include "AffineTransform.h"
30#include "FloatRect.h"
31#include "GraphicsTypes.h"
32#include "Image.h"
33
34#if USE(CG)
35
36#if PLATFORM(MAC)
37#define WTF_USE_PDFKIT_FOR_PDFDOCUMENTIMAGE 1
38#endif
39
40typedef struct CGPDFDocument *CGPDFDocumentRef;
41OBJC_CLASS PDFDocument;
42
43namespace WebCore {
44
45class GraphicsContext;
46class ImageBuffer;
47
48class PDFDocumentImage final : public Image {
49public:
50    static PassRefPtr<PDFDocumentImage> create(ImageObserver* observer)
51    {
52        return adoptRef(new PDFDocumentImage(observer));
53    }
54
55private:
56    PDFDocumentImage(ImageObserver*);
57    virtual ~PDFDocumentImage();
58
59    virtual bool isPDFDocumentImage() const override { return true; }
60
61    virtual String filenameExtension() const override;
62
63    virtual bool hasSingleSecurityOrigin() const override { return true; }
64
65    virtual bool dataChanged(bool allDataReceived) override;
66
67    virtual void destroyDecodedData(bool /*destroyAll*/ = true) override;
68
69    virtual void computeIntrinsicDimensions(Length& intrinsicWidth, Length& intrinsicHeight, FloatSize& intrinsicRatio) override;
70    virtual FloatSize size() const override;
71
72    virtual void draw(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect, ColorSpace styleColorSpace, CompositeOperator, BlendMode, ImageOrientationDescription) override;
73
74    // FIXME: Implement this to be less conservative.
75    virtual bool currentFrameKnownToBeOpaque() override { return false; }
76
77    void createPDFDocument();
78    void computeBoundsForCurrentPage();
79    unsigned pageCount() const;
80    void drawPDFPage(GraphicsContext*);
81
82    void updateCachedImageIfNeeded(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect);
83    bool cacheParametersMatch(GraphicsContext*, const FloatRect& dstRect, const FloatRect& srcRect) const;
84
85#if USE(PDFKIT_FOR_PDFDOCUMENTIMAGE)
86    RetainPtr<PDFDocument> m_document;
87#else
88    RetainPtr<CGPDFDocumentRef> m_document;
89#endif
90
91    std::unique_ptr<ImageBuffer> m_cachedImageBuffer;
92    AffineTransform m_cachedTransform;
93    FloatSize m_cachedDestinationSize;
94    FloatRect m_cachedSourceRect;
95    size_t m_cachedBytes;
96
97    FloatRect m_cropBox;
98    int m_rotationDegrees; // Can only be 0, 90, 180, or 270 degrees.
99    bool m_hasPage;
100};
101
102}
103
104#endif // USE(CG)
105
106#endif // PDFDocumentImage_h
107