Changeset 214406 in webkit


Ignore:
Timestamp:
Mar 25, 2017, 5:53:24 PM (8 years ago)
Author:
aestes@apple.com
Message:

[iOS] Use snapshotting instead of printing to draw single-page PDFs
https://bugs.webkit.org/show_bug.cgi?id=170103
<rdar://problem/30542960>

Reviewed by Tim Horton.

  • WebProcess/WebPage/WebPage.cpp:

(WebKit::paintSnapshotAtSize): Moved the painting logic from WebPage::snapshotAtSize() to here.
(WebKit::WebPage::snapshotAtSize): Changed to call paintSnapshotAtSize() with the
WebImage's graphics context.
(WebKit::WebPage::pdfSnapshotAtSize): Created a CGPDFGraphicsContext, passed it to
paintSnapshotAtSize(), and returned the context's data.

  • WebProcess/WebPage/WebPage.h: Made snapshotAtSize() and snapshotNode() private, changed

their return values from PassRefPtr to RefPtr, and declared pdfSnapshotAtSize().

  • WebProcess/WebPage/ios/WebPageIOS.mm:

(WebKit::WebPage::computePagesForPrintingAndDrawToPDF): If snapshotting the first page,
returned a page count of 1 and created a PDF using pdfSnapshotAtSize().

Location:
trunk/Source/WebKit2
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit2/ChangeLog

    r214404 r214406  
     12017-03-25  Andy Estes  <aestes@apple.com>
     2
     3        [iOS] Use snapshotting instead of printing to draw single-page PDFs
     4        https://bugs.webkit.org/show_bug.cgi?id=170103
     5        <rdar://problem/30542960>
     6
     7        Reviewed by Tim Horton.
     8
     9        * WebProcess/WebPage/WebPage.cpp:
     10        (WebKit::paintSnapshotAtSize): Moved the painting logic from WebPage::snapshotAtSize() to here.
     11        (WebKit::WebPage::snapshotAtSize): Changed to call paintSnapshotAtSize() with the
     12        WebImage's graphics context.
     13        (WebKit::WebPage::pdfSnapshotAtSize): Created a CGPDFGraphicsContext, passed it to
     14        paintSnapshotAtSize(), and returned the context's data.
     15        * WebProcess/WebPage/WebPage.h: Made snapshotAtSize() and snapshotNode() private, changed
     16        their return values from PassRefPtr to RefPtr, and declared pdfSnapshotAtSize().
     17        * WebProcess/WebPage/ios/WebPageIOS.mm:
     18        (WebKit::WebPage::computePagesForPrintingAndDrawToPDF): If snapshotting the first page,
     19        returned a page count of 1 and created a PDF using pdfSnapshotAtSize().
     20
    1212017-03-25  John Wilander  <wilander@apple.com>
    222
  • trunk/Source/WebKit2/WebProcess/WebPage/WebPage.cpp

    r214284 r214406  
    19461946}
    19471947
    1948 PassRefPtr<WebImage> WebPage::snapshotAtSize(const IntRect& rect, const IntSize& bitmapSize, SnapshotOptions options)
    1949 {
    1950     Frame* coreFrame = m_mainFrame->coreFrame();
    1951     if (!coreFrame)
    1952         return nullptr;
    1953 
    1954     FrameView* frameView = coreFrame->view();
    1955     if (!frameView)
    1956         return nullptr;
    1957 
     1948static void paintSnapshotAtSize(const IntRect& rect, const IntSize& bitmapSize, SnapshotOptions options, Frame& frame, FrameView& frameView, GraphicsContext& graphicsContext)
     1949{
    19581950    IntRect snapshotRect = rect;
    19591951    float horizontalScaleFactor = static_cast<float>(bitmapSize.width()) / rect.width();
     
    19611953    float scaleFactor = std::max(horizontalScaleFactor, verticalScaleFactor);
    19621954
    1963     auto snapshot = WebImage::create(bitmapSize, snapshotOptionsToImageOptions(options));
    1964 
    1965     auto graphicsContext = snapshot->bitmap().createGraphicsContext();
    1966 
    19671955    if (options & SnapshotOptionsPrinting) {
    1968         PrintContext::spoolAllPagesWithBoundaries(*coreFrame, *graphicsContext, snapshotRect.size());
    1969         return snapshot;
    1970     }
    1971 
    1972     Color documentBackgroundColor = frameView->documentBackgroundColor();
    1973     Color backgroundColor = (coreFrame->settings().backgroundShouldExtendBeyondPage() && documentBackgroundColor.isValid()) ? documentBackgroundColor : frameView->baseBackgroundColor();
    1974     graphicsContext->fillRect(IntRect(IntPoint(), bitmapSize), backgroundColor);
     1956        PrintContext::spoolAllPagesWithBoundaries(frame, graphicsContext, snapshotRect.size());
     1957        return;
     1958    }
     1959
     1960    Color documentBackgroundColor = frameView.documentBackgroundColor();
     1961    Color backgroundColor = (frame.settings().backgroundShouldExtendBeyondPage() && documentBackgroundColor.isValid()) ? documentBackgroundColor : frameView.baseBackgroundColor();
     1962    graphicsContext.fillRect(IntRect(IntPoint(), bitmapSize), backgroundColor);
    19751963
    19761964    if (!(options & SnapshotOptionsExcludeDeviceScaleFactor)) {
    1977         double deviceScaleFactor = corePage()->deviceScaleFactor();
    1978         graphicsContext->applyDeviceScaleFactor(deviceScaleFactor);
     1965        double deviceScaleFactor = frame.page()->deviceScaleFactor();
     1966        graphicsContext.applyDeviceScaleFactor(deviceScaleFactor);
    19791967        scaleFactor /= deviceScaleFactor;
    19801968    }
    19811969
    1982     graphicsContext->scale(scaleFactor);
    1983     graphicsContext->translate(-snapshotRect.x(), -snapshotRect.y());
     1970    graphicsContext.scale(scaleFactor);
     1971    graphicsContext.translate(-snapshotRect.x(), -snapshotRect.y());
    19841972
    19851973    FrameView::SelectionInSnapshot shouldPaintSelection = FrameView::IncludeSelection;
     
    19911979        coordinateSpace = FrameView::ViewCoordinates;
    19921980
    1993     frameView->paintContentsForSnapshot(*graphicsContext, snapshotRect, shouldPaintSelection, coordinateSpace);
     1981    frameView.paintContentsForSnapshot(graphicsContext, snapshotRect, shouldPaintSelection, coordinateSpace);
    19941982
    19951983    if (options & SnapshotOptionsPaintSelectionRectangle) {
    1996         FloatRect selectionRectangle = m_mainFrame->coreFrame()->selection().selectionBounds();
    1997         graphicsContext->setStrokeColor(Color(0xFF, 0, 0));
    1998         graphicsContext->strokeRect(selectionRectangle, 1);
    1999     }
    2000    
     1984        FloatRect selectionRectangle = frame.selection().selectionBounds();
     1985        graphicsContext.setStrokeColor(Color(0xFF, 0, 0));
     1986        graphicsContext.strokeRect(selectionRectangle, 1);
     1987    }
     1988}
     1989
     1990RefPtr<WebImage> WebPage::snapshotAtSize(const IntRect& rect, const IntSize& bitmapSize, SnapshotOptions options)
     1991{
     1992    Frame* coreFrame = m_mainFrame->coreFrame();
     1993    if (!coreFrame)
     1994        return nullptr;
     1995
     1996    FrameView* frameView = coreFrame->view();
     1997    if (!frameView)
     1998        return nullptr;
     1999
     2000    auto snapshot = WebImage::create(bitmapSize, snapshotOptionsToImageOptions(options));
     2001    auto graphicsContext = snapshot->bitmap().createGraphicsContext();
     2002
     2003    paintSnapshotAtSize(rect, bitmapSize, options, *coreFrame, *frameView, *graphicsContext);
     2004
    20012005    return snapshot;
    20022006}
    20032007
    2004 PassRefPtr<WebImage> WebPage::snapshotNode(WebCore::Node& node, SnapshotOptions options, unsigned maximumPixelCount)
     2008#if USE(CF)
     2009RetainPtr<CFDataRef> WebPage::pdfSnapshotAtSize(const IntRect& rect, const IntSize& bitmapSize, SnapshotOptions options)
     2010{
     2011    Frame* coreFrame = m_mainFrame->coreFrame();
     2012    if (!coreFrame)
     2013        return nullptr;
     2014
     2015    FrameView* frameView = coreFrame->view();
     2016    if (!frameView)
     2017        return nullptr;
     2018
     2019    auto data = adoptCF(CFDataCreateMutable(kCFAllocatorDefault, 0));
     2020
     2021#if USE(CG)
     2022    auto dataConsumer = adoptCF(CGDataConsumerCreateWithCFData(data.get()));
     2023    auto mediaBox = CGRectMake(0, 0, bitmapSize.width(), bitmapSize.height());
     2024    auto pdfContext = adoptCF(CGPDFContextCreate(dataConsumer.get(), &mediaBox, nullptr));
     2025
     2026    CGPDFContextBeginPage(pdfContext.get(), nullptr);
     2027
     2028    GraphicsContext graphicsContext { pdfContext.get() };
     2029    graphicsContext.scale({ 1, -1 });
     2030    graphicsContext.translate(0, -bitmapSize.height());
     2031    paintSnapshotAtSize(rect, bitmapSize, options, *coreFrame, *frameView, graphicsContext);
     2032
     2033    CGPDFContextEndPage(pdfContext.get());
     2034    CGPDFContextClose(pdfContext.get());
     2035#endif
     2036
     2037    return WTFMove(data);
     2038}
     2039#endif
     2040
     2041RefPtr<WebImage> WebPage::snapshotNode(WebCore::Node& node, SnapshotOptions options, unsigned maximumPixelCount)
    20052042{
    20062043    Frame* coreFrame = m_mainFrame->coreFrame();
  • trunk/Source/WebKit2/WebProcess/WebPage/WebPage.h

    r214113 r214406  
    491491   
    492492    PassRefPtr<WebImage> scaledSnapshotWithOptions(const WebCore::IntRect&, double additionalScaleFactor, SnapshotOptions);
    493     PassRefPtr<WebImage> snapshotAtSize(const WebCore::IntRect&, const WebCore::IntSize& bitmapSize, SnapshotOptions);
    494     PassRefPtr<WebImage> snapshotNode(WebCore::Node&, SnapshotOptions, unsigned maximumPixelCount = std::numeric_limits<unsigned>::max());
    495493
    496494    static const WebEvent* currentEvent();
     
    12751273    void urlSchemeHandlerTaskDidComplete(uint64_t handlerIdentifier, uint64_t taskIdentifier, const WebCore::ResourceError&);
    12761274
     1275    RefPtr<WebImage> snapshotAtSize(const WebCore::IntRect&, const WebCore::IntSize& bitmapSize, SnapshotOptions);
     1276    RefPtr<WebImage> snapshotNode(WebCore::Node&, SnapshotOptions, unsigned maximumPixelCount = std::numeric_limits<unsigned>::max());
     1277#if USE(CF)
     1278    RetainPtr<CFDataRef> pdfSnapshotAtSize(const WebCore::IntRect&, const WebCore::IntSize& bitmapSize, SnapshotOptions);
     1279#endif
     1280
    12771281    uint64_t m_pageID;
    12781282
  • trunk/Source/WebKit2/WebProcess/WebPage/ios/WebPageIOS.mm

    r214266 r214406  
    32533253void WebPage::computePagesForPrintingAndDrawToPDF(uint64_t frameID, const PrintInfo& printInfo, uint64_t callbackID, PassRefPtr<Messages::WebPage::ComputePagesForPrintingAndDrawToPDF::DelayedReply> reply)
    32543254{
     3255    if (printInfo.snapshotFirstPage) {
     3256        reply->send(1);
     3257        IntSize snapshotSize { FloatSize { printInfo.availablePaperWidth, printInfo.availablePaperHeight } };
     3258        IntRect snapshotRect { {0, 0}, snapshotSize };
     3259        auto pdfData = pdfSnapshotAtSize(snapshotRect, snapshotSize, 0);
     3260        send(Messages::WebPageProxy::DrawToPDFCallback(IPC::DataReference(CFDataGetBytePtr(pdfData.get()), CFDataGetLength(pdfData.get())), callbackID));
     3261        return;
     3262    }
     3263
    32553264    Vector<WebCore::IntRect> pageRects;
    32563265    double totalScaleFactor;
     
    32583267
    32593268    ASSERT(pageRects.size() >= 1);
    3260     std::size_t pageCount = printInfo.snapshotFirstPage ? 1 : pageRects.size();
     3269    std::size_t pageCount = pageRects.size();
    32613270    ASSERT(pageCount <= std::numeric_limits<uint32_t>::max());
    32623271    reply->send(pageCount);
Note: See TracChangeset for help on using the changeset viewer.