-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UnifiedPDF] Get basic PDF page layout working
https://bugs.webkit.org/show_bug.cgi?id=262389 rdar://116249928 Reviewed by Tim Horton and Richard Robinson. Introduce PDFDocumentLayout for UnifiedPDF, which contains the logic to lay out the PDF pages, taking page size, rotation and the layout mode into account. Implement basic single column layout. Fix PDF page drawing to flip the context when drawing pages. Hook up an invalidate code path, so that we can repaint the plugin when the PDF loads. * Source/WebKit/SourcesCocoa.txt: * Source/WebKit/WebKit.xcodeproj/project.pbxproj: * Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.h: * Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.mm: (WebKit::PDFPluginBase::invalidateRect): * Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.h: Added. (WebKit::PDFDocumentLayout::pdfDocument const): (WebKit::PDFDocumentLayout::layoutSize const): * Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.mm: Added. (WebKit::PDFDocumentLayout::documentMargin): (WebKit::PDFDocumentLayout::pageMargin): (WebKit::PDFDocumentLayout::setPDFDocument): (WebKit::PDFDocumentLayout::hasPDFDocument const): (WebKit::PDFDocumentLayout::pageAtIndex const): (WebKit::PDFDocumentLayout::updateGeometry): (WebKit::PDFDocumentLayout::layoutPages): (WebKit::PDFDocumentLayout::layoutSingleColumn): (WebKit::PDFDocumentLayout::layoutTwoUpColumn): (WebKit::PDFDocumentLayout::pageCount const): (WebKit::PDFDocumentLayout::boundsForPageAtIndex const): * Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.h: * Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm: (WebKit::UnifiedPDFPlugin::createPDFDocument): (WebKit::UnifiedPDFPlugin::installPDFDocument): (WebKit::UnifiedPDFPlugin::paint): * Source/WebKit/WebProcess/Plugins/PluginView.h: * Source/WebKit/WebProcess/WebCoreSupport/WebEditorClient.h: Canonical link: https://commits.webkit.org/268670@main
- Loading branch information
Showing
10 changed files
with
288 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2023 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | ||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
* THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#if ENABLE(UNIFIED_PDF) | ||
|
||
#include <CoreGraphics/CoreGraphics.h> | ||
#include <WebCore/FloatRect.h> | ||
#include <wtf/RetainPtr.h> | ||
|
||
namespace WebCore { | ||
class GraphicsContext; | ||
class IntRect; | ||
} | ||
|
||
namespace WebKit { | ||
|
||
class PDFDocumentLayout { | ||
public: | ||
using PageIndex = size_t; // This is a zero-based index. | ||
|
||
enum class DisplayMode : uint8_t { | ||
SinglePage, | ||
Continuous, | ||
TwoUp, | ||
TwoUpContinuous, | ||
}; | ||
|
||
PDFDocumentLayout(); | ||
~PDFDocumentLayout(); | ||
|
||
void setPDFDocument(RetainPtr<CGPDFDocumentRef>&&); | ||
CGPDFDocumentRef pdfDocument() const { return m_pdfDocument.get(); } | ||
|
||
bool hasPDFDocument() const; | ||
size_t pageCount() const; | ||
|
||
RetainPtr<CGPDFPageRef> pageAtIndex(PageIndex) const; | ||
WebCore::FloatRect boundsForPageAtIndex(PageIndex) const; | ||
|
||
WebCore::FloatSize layoutSize() const { return m_documentBounds.size(); } | ||
|
||
private: | ||
|
||
void updateGeometry(); | ||
|
||
void layoutPages(float availableWidth); | ||
|
||
void layoutSingleColumn(float availableWidth); | ||
void layoutTwoUpColumn(float availableWidth); | ||
|
||
static FloatSize documentMargin(); | ||
static FloatSize pageMargin(); | ||
|
||
RetainPtr<CGPDFDocumentRef> m_pdfDocument; | ||
Vector<WebCore::FloatRect> m_pageBounds; | ||
WebCore::FloatRect m_documentBounds; | ||
DisplayMode m_displayMode { DisplayMode::Continuous }; | ||
}; | ||
|
||
} // namespace WebKit | ||
|
||
#endif // ENABLE(UNIFIED_PDF) |
158 changes: 158 additions & 0 deletions
158
Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright (C) 2023 Apple Inc. All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions | ||
* are met: | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | ||
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | ||
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
* THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
|
||
#include "config.h" | ||
#include "PDFDocumentLayout.h" | ||
|
||
#if ENABLE(UNIFIED_PDF) | ||
|
||
#include <CoreGraphics/CoreGraphics.h> | ||
|
||
namespace WebKit { | ||
using namespace WebCore; | ||
|
||
FloatSize PDFDocumentLayout::documentMargin() | ||
{ | ||
return { 20, 20 }; | ||
} | ||
|
||
FloatSize PDFDocumentLayout::pageMargin() | ||
{ | ||
return { 10, 10 }; | ||
} | ||
|
||
PDFDocumentLayout::PDFDocumentLayout() = default; | ||
PDFDocumentLayout::~PDFDocumentLayout() = default; | ||
|
||
void PDFDocumentLayout::setPDFDocument(RetainPtr<CGPDFDocumentRef>&& pdfDocument) | ||
{ | ||
m_pdfDocument = WTFMove(pdfDocument); | ||
updateGeometry(); | ||
} | ||
|
||
bool PDFDocumentLayout::hasPDFDocument() const | ||
{ | ||
return !!m_pdfDocument; | ||
} | ||
|
||
RetainPtr<CGPDFPageRef> PDFDocumentLayout::pageAtIndex(PageIndex index) const | ||
{ | ||
RetainPtr page = CGPDFDocumentGetPage(m_pdfDocument.get(), index + 1); // CG Page index is 1-based | ||
return page; | ||
} | ||
|
||
void PDFDocumentLayout::updateGeometry() | ||
{ | ||
auto pageCount = this->pageCount(); | ||
m_pageBounds.clear(); | ||
m_documentBounds = { }; | ||
|
||
for (PageIndex i = 0; i < pageCount; ++i) { | ||
auto page = pageAtIndex(i); | ||
if (!page) { | ||
m_pageBounds.append({ }); | ||
continue; | ||
} | ||
|
||
auto pageCropBox = FloatRect { CGPDFPageGetBoxRect(page.get(), kCGPDFCropBox) }; | ||
|
||
// FIXME: Handle page rotation | ||
m_pageBounds.append(pageCropBox); | ||
} | ||
|
||
float availableWidth = 1000; // FIXME: Fixed for now, but should use plugin width, maybe with a second layout pass when horizontal scrolling is required. | ||
layoutPages(availableWidth); | ||
|
||
// FIXME: Update plugin size here. | ||
} | ||
|
||
void PDFDocumentLayout::layoutPages(float availableWidth) | ||
{ | ||
// We always lay out in a continuous mode. We handle non-continuous mode via scroll snap. | ||
switch (m_displayMode) { | ||
case DisplayMode::SinglePage: | ||
case DisplayMode::Continuous: | ||
layoutSingleColumn(availableWidth); | ||
break; | ||
|
||
case DisplayMode::TwoUp: | ||
case DisplayMode::TwoUpContinuous: | ||
layoutTwoUpColumn(availableWidth); | ||
break; | ||
} | ||
} | ||
|
||
void PDFDocumentLayout::layoutSingleColumn(float availableWidth) | ||
{ | ||
auto documentMargin = PDFDocumentLayout::documentMargin(); | ||
auto pageMargin = PDFDocumentLayout::documentMargin(); | ||
|
||
float currentYOffset = documentMargin.height(); | ||
float maxPageWidth = 0; | ||
auto pageCount = this->pageCount(); | ||
|
||
for (PageIndex i = 0; i < pageCount; ++i) { | ||
auto pageBounds = m_pageBounds[i]; | ||
|
||
auto pageLeft = std::max<float>(std::floor((availableWidth - pageBounds.width()) / 2), 0); | ||
pageBounds.setLocation({ pageLeft, currentYOffset }); | ||
|
||
currentYOffset += pageBounds.height() + pageMargin.height(); | ||
maxPageWidth = std::max(maxPageWidth, pageBounds.width()); | ||
|
||
m_pageBounds[i] = pageBounds; | ||
} | ||
|
||
// Subtract the last page's bottom margin. | ||
currentYOffset -= pageMargin.height(); | ||
currentYOffset += documentMargin.height(); | ||
|
||
m_documentBounds = FloatRect { 0, 0, std::max(maxPageWidth, availableWidth), currentYOffset }; | ||
} | ||
|
||
void PDFDocumentLayout::layoutTwoUpColumn(float availableWidth) | ||
{ | ||
// FIMXE: Not implemented yet. | ||
} | ||
|
||
size_t PDFDocumentLayout::pageCount() const | ||
{ | ||
if (!hasPDFDocument()) | ||
return 0; | ||
|
||
return CGPDFDocumentGetNumberOfPages(m_pdfDocument.get()); | ||
} | ||
|
||
WebCore::FloatRect PDFDocumentLayout::boundsForPageAtIndex(PageIndex index) const | ||
{ | ||
if (index >= m_pageBounds.size()) | ||
return { }; | ||
|
||
return m_pageBounds[index]; | ||
} | ||
|
||
} // namespace WebKit | ||
|
||
#endif // ENABLE(UNIFIED_PDF) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.