Skip to content

Commit

Permalink
[UnifiedPDF] Get basic PDF page layout working
Browse files Browse the repository at this point in the history
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
smfr committed Sep 29, 2023
1 parent 8d56732 commit 53ed985
Show file tree
Hide file tree
Showing 10 changed files with 288 additions and 15 deletions.
1 change: 1 addition & 0 deletions Source/WebKit/SourcesCocoa.txt
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ WebProcess/Plugins/PDF/PDFPluginChoiceAnnotation.mm
WebProcess/Plugins/PDF/PDFPluginPasswordField.mm
WebProcess/Plugins/PDF/PDFPluginTextAnnotation.mm
WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm
WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.mm

WebProcess/WebCoreSupport/WebCaptionPreferencesDelegate.cpp
WebProcess/WebCoreSupport/WebValidationMessageClient.cpp
Expand Down
4 changes: 4 additions & 0 deletions Source/WebKit/WebKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3007,6 +3007,8 @@
0F3157A928F4EDD800D79D05 /* RemoteLayerTreeDrawingAreaProxyIOS.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = RemoteLayerTreeDrawingAreaProxyIOS.mm; sourceTree = "<group>"; };
0F3C7257196F5F5000AEDD0C /* WKInspectorHighlightView.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WKInspectorHighlightView.mm; sourceTree = "<group>"; };
0F3C7259196F5F6800AEDD0C /* WKInspectorHighlightView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WKInspectorHighlightView.h; sourceTree = "<group>"; };
0F3D56722AC651390086D64E /* PDFDocumentLayout.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = PDFDocumentLayout.mm; sourceTree = "<group>"; };
0F3D56732AC651390086D64E /* PDFDocumentLayout.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PDFDocumentLayout.h; sourceTree = "<group>"; };
0F4000FD2527D69D00E91DA7 /* WebWheelEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebWheelEvent.h; sourceTree = "<group>"; };
0F4000FE2527D6C300E91DA7 /* WebMouseEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebMouseEvent.h; sourceTree = "<group>"; };
0F4000FF2527D6F700E91DA7 /* WebKeyboardEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = WebKeyboardEvent.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -7985,6 +7987,8 @@
0FFACF022AC2453300ED8DB6 /* UnifiedPDF */ = {
isa = PBXGroup;
children = (
0F3D56732AC651390086D64E /* PDFDocumentLayout.h */,
0F3D56722AC651390086D64E /* PDFDocumentLayout.mm */,
0FFACF042AC2453300ED8DB6 /* UnifiedPDFPlugin.h */,
0FFACF032AC2453300ED8DB6 /* UnifiedPDFPlugin.mm */,
);
Expand Down
2 changes: 2 additions & 0 deletions Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ class PDFPluginBase : public ThreadSafeRefCounted<PDFPluginBase> {
void ensureDataBufferLength(uint64_t);
void addArchiveResource();

void invalidateRect(const WebCore::IntRect&);

WeakPtr<PluginView> m_view;
WeakPtr<WebFrame> m_frame;

Expand Down
8 changes: 8 additions & 0 deletions Source/WebKit/WebProcess/Plugins/PDF/PDFPluginBase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@
m_size = pluginSize;
}

void PDFPluginBase::invalidateRect(const IntRect& rect)
{
if (!m_view)
return;

m_view->invalidateRect(rect);
}

} // namespace WebKit

#endif // ENABLE(PDFKIT_PLUGIN) || ENABLE(UNIFIED_PDF)
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 Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/PDFDocumentLayout.mm
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)
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#if ENABLE(UNIFIED_PDF)

#include "PDFDocumentLayout.h"
#include "PDFPluginBase.h"

namespace WebKit {
Expand Down Expand Up @@ -80,7 +81,7 @@ class UnifiedPDFPlugin final : public PDFPluginBase {
id accessibilityObject() const override;
id accessibilityAssociatedPluginParentForElement(WebCore::Element*) const override;

RetainPtr<CGPDFDocumentRef> m_pdfDocument;
PDFDocumentLayout m_documentLayout;
};

} // namespace WebKit
Expand Down
36 changes: 23 additions & 13 deletions Source/WebKit/WebProcess/Plugins/PDF/UnifiedPDF/UnifiedPDFPlugin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@
void UnifiedPDFPlugin::createPDFDocument()
{
auto dataProvider = adoptCF(CGDataProviderCreateWithCFData(m_data.get()));
m_pdfDocument = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));
auto pdfDocument = adoptCF(CGPDFDocumentCreateWithProvider(dataProvider.get()));

m_documentLayout.setPDFDocument(WTFMove(pdfDocument));
}

void UnifiedPDFPlugin::installPDFDocument()
Expand All @@ -63,38 +65,46 @@
if (m_hasBeenDestroyed)
return;

if (!m_pdfDocument)
if (!m_documentLayout.hasPDFDocument())
return;

if (!m_view)
return;

invalidateRect({ IntPoint(), size() });
}

void UnifiedPDFPlugin::paint(GraphicsContext& context, const WebCore::IntRect& rect)
{
ALWAYS_LOG_WITH_STREAM(stream << "UnifiedPDFPlugin::paint " << rect);
ASSERT(!context.paintingDisabled());

if (m_size.isEmpty())
return;

auto imageBuffer = ImageBuffer::create(m_size, RenderingPurpose::Unspecified, 1.0, DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
auto imageBuffer = ImageBuffer::create(m_size, RenderingPurpose::Unspecified, context.scaleFactor().width(), DestinationColorSpace::SRGB(), PixelFormat::BGRA8);
if (!imageBuffer)
return;

RetainPtr firstPage = CGPDFDocumentGetPage(m_pdfDocument.get(), 1);
if (!firstPage)
return;
auto& bufferContext = imageBuffer->context();

for (PDFDocumentLayout::PageIndex i = 0; i < m_documentLayout.pageCount(); ++i) {
auto page = m_documentLayout.pageAtIndex(i);
if (!page)
continue;

auto pageBounds = m_documentLayout.boundsForPageAtIndex(i);

auto stateSaver = GraphicsContextStateSaver(bufferContext);
bufferContext.clip(pageBounds);

CGContextRef cgContext = imageBuffer->context().platformContext();
CGRect pageCropBox = CGPDFPageGetBoxRect(firstPage.get(), kCGPDFCropBox);
bufferContext.fillRect(pageBounds, Color::white);

CGRect clipRect = CGRectMake(0, 0, pageCropBox.size.width, pageCropBox.size.height);
CGContextAddRect(cgContext, clipRect);
CGContextClip(cgContext);
bufferContext.translate(pageBounds.x(), pageBounds.y());
bufferContext.translate(0, pageBounds.height());
bufferContext.scale({ 1, -1 });

CGContextDrawPDFPage(cgContext, firstPage.get());
CGContextDrawPDFPage(imageBuffer->context().platformContext(), page.get());
}

context.drawImageBuffer(*imageBuffer, FloatPoint { });
}
Expand Down
3 changes: 2 additions & 1 deletion Source/WebKit/WebProcess/Plugins/PluginView.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ class PluginView final : public WebCore::PluginViewBase {

bool isUsingUISideCompositing() const;

void invalidateRect(const WebCore::IntRect&) final;

private:
PluginView(WebCore::HTMLPlugInElement&, const URL&, const String& contentType, bool shouldUseManualLoader, WebPage&);
virtual ~PluginView();
Expand Down Expand Up @@ -132,7 +134,6 @@ class PluginView final : public WebCore::PluginViewBase {
// WebCore::Widget
void setFrameRect(const WebCore::IntRect&) final;
void paint(WebCore::GraphicsContext&, const WebCore::IntRect&, WebCore::Widget::SecurityOriginPaintPolicy, WebCore::RegionContext*) final;
void invalidateRect(const WebCore::IntRect&) final;
void frameRectsChanged() final;
void setParent(WebCore::ScrollView*) final;
void handleEvent(WebCore::Event&) final;
Expand Down
Loading

0 comments on commit 53ed985

Please sign in to comment.