Skip to content
  • timothy_horton@apple.com's avatar
    [mac] PDFDocumentImage should use PDFKit to draw · 4fca9bce
    timothy_horton@apple.com authored
    https://bugs.webkit.org/show_bug.cgi?id=120651
    <rdar://problem/12810731>
    
    Reviewed by Alexey Proskuryakov.
    
    Use PDFKit to draw PDF-in-<img> on Mac, so that annotations in PDF are painted.
    
    Test: fast/images/pdf-as-image-with-annotations.html
    
    * WebCore.xcodeproj/project.pbxproj:
    Add PDFDocumentImageMac.mm.
    
    * platform/graphics/cg/PDFDocumentImage.cpp:
    (WebCore::PDFDocumentImage::PDFDocumentImage):
    We don't need to initialize m_document anymore because it's now a RetainPtr.
    Initialize m_hasPage to false.
    
    (WebCore::PDFDocumentImage::~PDFDocumentImage):
    We don't need to manually release our CGPDFDocumentRef anymore because it's now a RetainPtr.
    
    (WebCore::PDFDocumentImage::size):
    Use expandedIntSize for explicitness.
    
    (WebCore::PDFDocumentImage::applyRotationForPainting):
    Rename adjustCTM to applyRotationForPainting, and move the one non-rotation related transformation out.
    We need to do this because PDFKit applies the rotation transformation itself, so we don't need to.
    Also, explicitly use std::min instead of importing the whole std namespace.
    
    (WebCore::PDFDocumentImage::dataChanged):
    Assert that we don't re-enter dataChanged after we've created a document,
    because it seems like that shouldn't happen - we only create a document when
    we've supposedly received all data, and it would be nice if we didn't have
    all this varying code to deal with SharedBuffers changing out from under each other.
    Factor the code to create a CGPDFDocumentRef out into createPDFDocument, so we can
    replace it with a different implementation that creates a PDFDocument.
    
    (WebCore::PDFDocumentImage::setCurrentPage):
    Make all page-number-related things unsigned, since they are in CG and PDFKit,
    and add m_hasPage to denote whether we've acquired a valid page yet.
    Factor code to retrieve the PDF box bounds into computeBoundsForCurrentPage.
    Remove setCurrentPage itself, as we never use a page other than the first page;
    we will call computeBoundsForCurrentPage and set hasPage in dataChanged, now.
    
    (WebCore::PDFDocumentImage::draw):
    Fix a weird comment.
    Factor code that actually draws the PDF out into drawPDFPage.
    
    The rest of the functions in this file are only built for USE(CG) && !PLATFORM(MAC):
    
    (WebCore::PDFDocumentImage::createPDFDocument):
    Factored out of dataChanged(). Use the PLATFORM(MAC) way of making a
    CGDataProvider from a SharedBuffer everywhere; we're already using it on
    iOS, and it should also work for Windows, the only other USE(CG) platform.
    Since we only createPDFDocument() after all data has been received, remove
    the comment about the SharedBuffer being secretly written to behind its back.
    
    (WebCore::PDFDocumentImage::computeBoundsForCurrentPage):
    Factored out of setCurrentPage().
    Use deg2rad since we have it.
    
    (WebCore::PDFDocumentImage::pageCount):
    (WebCore::PDFDocumentImage::drawPDFPage):
    Factored out of draw().
    Reduced an unnecessary transform (we were effectively doing mediaBox.origin - cropBox.origin - mediaBox.origin).
    
    * platform/graphics/cg/PDFDocumentImage.h:
    Unindent all the things, as we don't indent the first level inside a namespace scope.
    Forward-declare PDFDocument.
    Make m_currentPage unsigned as it should be.
    Add m_hasPage.
    Make m_document a PDFDocument on PLATFORM(MAC) and a CGPDFDocumentRef on other platforms.
    
    * platform/graphics/mac/PDFDocumentImageMac.mm: Added.
    Soft-link in PDFKit and specifically the PDFDocument class.
    
    (WebCore::PDFDocumentImage::createPDFDocument):
    Create our PDFDocument from the SharedBuffer's NSData directly. This shouldn't be a problem
    (with respect to the SharedBuffer's data being appended to in secret), because we've waited
    for the document to finish loading before getting here.
    
    (WebCore::PDFDocumentImage::computeBoundsForCurrentPage):
    Ask PDFKit for the page's media and crop boxes; it automatically performs the correct fallback.
    Ask PDFKit for the page's rotation.
    
    (WebCore::PDFDocumentImage::pageCount):
    Ask PDFKit for the page count.
    
    (WebCore::PDFDocumentImage::drawPDFPage):
    Paint the current page of the PDF. Note that we do not apply the rotation here
    via applyRotationForPainting because PDFKit will do it itself.
    Save and restore two bits of CG state which aren't saved and restored with the
    graphics state, as PDFKit can mutate them above us (<rdar://problem/14951759>).
    
    * platform/mac/SoftLinking.h:
    I couldn't find any clients of SOFT_LINK_FRAMEWORK_IN_CORESERVICES_UMBRELLA,
    so I made it the more generic SOFT_LINK_FRAMEWORK_IN_UMBRELLA, taking the name
    of the umbrella framework and the subframework, so I could use it to soft link
    Quartz.framework's PDFKit.framework.
    
    * fast/images/pdf-as-image-with-annotations-expected.html: Added.
    * fast/images/pdf-as-image-with-annotations.html: Added.
    * fast/images/resources/annotation.pdf: Added.
    Add a test that ensures that PDF-in-<img> draws simple annotations.
    
    * platform/mac/fast/images/pdf-as-image-landscape-expected.png:
    * platform/mac/fast/images/pdf-as-image-landscape-expected.txt:
    Extremely minor rebaselines probably due to PDFKit handling rotation in the new implementation.
    
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@155461 268f45cc-cd09-0410-ab3c-d52691b4dbfc
    4fca9bce