Add @page margin support
authorzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Apr 2019 23:33:41 +0000 (23:33 +0000)
committerzalan@apple.com <zalan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 11 Apr 2019 23:33:41 +0000 (23:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=196680
<rdar://problem/45217707>

Reviewed by Tim Horton.

Source/WebCore:

This patch adds support for @page margin. This feature is currently behind a runtime flag and is off by default.
We resolve the @page margin values in the WebProcess (currently no pseudo class is supported) and transfer them to the UIProcess through computedPagesCallback.
UIProcess is responsible for applying these new values on the printer. We also re-compute the available width/height for the current printing context
in the WebProcess if needed (see PrintInfo for current printer margins).

Tests: printing/page-with-10mm-left-margin.html
       printing/page-with-zero-margin.html

* page/PrintContext.cpp:
(WebCore::PrintContext::computedPageMargin):
(WebCore::PrintContext::computedPageSize):
* page/PrintContext.h:
* page/RuntimeEnabledFeatures.h:
(WebCore::RuntimeEnabledFeatures::pageAtRuleSupportEnabled const):
(WebCore::RuntimeEnabledFeatures::setPageAtRuleSupportEnabled):

Source/WebKit:

* Shared/PrintInfo.cpp:
(WebKit::PrintInfo::encode const):
(WebKit::PrintInfo::decode):
* Shared/PrintInfo.h:
* Shared/WebPreferences.yaml:
* Shared/gtk/PrintInfoGtk.cpp:
(WebKit::PrintInfo::PrintInfo):
* Shared/mac/PrintInfoMac.mm:
(WebKit::PrintInfo::PrintInfo):
* UIProcess/API/C/WKPage.cpp:
(WKPageComputePagesForPrinting):
* UIProcess/API/C/WKPreferences.cpp:
(WKPreferencesSetPageAtRuleSupportEnabled):
(WKPreferencesGetPageAtRuleSupportEnabled):
* UIProcess/API/C/WKPreferencesRefPrivate.h:
* UIProcess/GenericCallback.h:
* UIProcess/WebPageProxy.cpp:
(WebKit::WebPageProxy::computedPagesCallback):
* UIProcess/WebPageProxy.h:
* UIProcess/WebPageProxy.messages.in:
* UIProcess/mac/WKPrintingView.mm:
(pageDidComputePageRects):
(-[WKPrintingView _askPageToComputePageRects]):
* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::beginPrinting):
(WebKit::WebPage::computePagesForPrinting):
(WebKit::WebPage::computePagesForPrintingImpl):
* WebProcess/WebPage/WebPage.h:
* WebProcess/WebPage/ios/WebPageIOS.mm:
(WebKit::WebPage::computePagesForPrintingAndDrawToPDF):

LayoutTests:

* printing/page-with-10mm-left-margin-expected.pdf: Added.
* printing/page-with-10mm-left-margin.html: Added.
* printing/page-with-zero-margin-expected.pdf: Added.
* printing/page-with-zero-margin.html: Added.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@244202 268f45cc-cd09-0410-ab3c-d52691b4dbfc

26 files changed:
LayoutTests/ChangeLog
LayoutTests/printing/page-with-10mm-left-margin-expected.pdf [new file with mode: 0644]
LayoutTests/printing/page-with-10mm-left-margin-expected.txt [new file with mode: 0644]
LayoutTests/printing/page-with-10mm-left-margin.html [new file with mode: 0644]
LayoutTests/printing/page-with-zero-margin-expected.pdf [new file with mode: 0644]
LayoutTests/printing/page-with-zero-margin-expected.txt [new file with mode: 0644]
LayoutTests/printing/page-with-zero-margin.html [new file with mode: 0644]
Source/WebCore/ChangeLog
Source/WebCore/page/PrintContext.cpp
Source/WebCore/page/PrintContext.h
Source/WebCore/page/RuntimeEnabledFeatures.h
Source/WebKit/ChangeLog
Source/WebKit/Shared/PrintInfo.cpp
Source/WebKit/Shared/PrintInfo.h
Source/WebKit/Shared/WebPreferences.yaml
Source/WebKit/Shared/gtk/PrintInfoGtk.cpp
Source/WebKit/Shared/mac/PrintInfoMac.mm
Source/WebKit/UIProcess/API/C/WKPage.cpp
Source/WebKit/UIProcess/GenericCallback.h
Source/WebKit/UIProcess/WebPageProxy.cpp
Source/WebKit/UIProcess/WebPageProxy.h
Source/WebKit/UIProcess/WebPageProxy.messages.in
Source/WebKit/UIProcess/mac/WKPrintingView.mm
Source/WebKit/WebProcess/WebPage/WebPage.cpp
Source/WebKit/WebProcess/WebPage/WebPage.h
Source/WebKit/WebProcess/WebPage/ios/WebPageIOS.mm

index 962782f..6f06c59 100644 (file)
@@ -1,3 +1,16 @@
+2019-04-11  Zalan Bujtas  <zalan@apple.com>
+
+        Add @page margin support
+        https://bugs.webkit.org/show_bug.cgi?id=196680
+        <rdar://problem/45217707>
+
+        Reviewed by Tim Horton.
+
+        * printing/page-with-10mm-left-margin-expected.pdf: Added.
+        * printing/page-with-10mm-left-margin.html: Added.
+        * printing/page-with-zero-margin-expected.pdf: Added.
+        * printing/page-with-zero-margin.html: Added.
+
 2019-04-09  Ryosuke Niwa  <rniwa@webkit.org>
 
         [iOS] Moving backwards by word granularity does not work if the previous line was inside another block element
diff --git a/LayoutTests/printing/page-with-10mm-left-margin-expected.pdf b/LayoutTests/printing/page-with-10mm-left-margin-expected.pdf
new file mode 100644 (file)
index 0000000..beb10da
Binary files /dev/null and b/LayoutTests/printing/page-with-10mm-left-margin-expected.pdf differ
diff --git a/LayoutTests/printing/page-with-10mm-left-margin-expected.txt b/LayoutTests/printing/page-with-10mm-left-margin-expected.txt
new file mode 100644 (file)
index 0000000..c2b7903
--- /dev/null
@@ -0,0 +1,7 @@
+layer at (0,0) size 1000x18
+  RenderView at (0,0) size 1000x18
+layer at (0,0) size 1000x18
+  RenderBlock {HTML} at (0,0) size 1000x18
+    RenderBody {BODY} at (0,0) size 1000x18
+      RenderText {#text} at (0,0) size 374x18
+        text run at (0,0) width 374: "Text should start at 10mm from the right edge of the page."
diff --git a/LayoutTests/printing/page-with-10mm-left-margin.html b/LayoutTests/printing/page-with-10mm-left-margin.html
new file mode 100644 (file)
index 0000000..3ddb64b
--- /dev/null
@@ -0,0 +1,24 @@
+<!DOCTYPE html><!-- webkit-test-runner [ experimental:PageAtRuleSupportEnabled=true ] -->
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+  <title>This tests that the content has a 1cm left margin.</title>
+  <style type="text/css">
+    @page {
+        margin-left: 10mm;
+    }
+    
+    html, body {
+        padding: 0px;
+        margin: 0px;
+    }
+  </style>
+  <script>
+    if (window.testRunner)
+      testRunner.setPrinting();
+  </script>
+</head>
+<body>
+Text should start at 10mm from the right edge of the page.
+</body>
+</html>
diff --git a/LayoutTests/printing/page-with-zero-margin-expected.pdf b/LayoutTests/printing/page-with-zero-margin-expected.pdf
new file mode 100644 (file)
index 0000000..a35a966
Binary files /dev/null and b/LayoutTests/printing/page-with-zero-margin-expected.pdf differ
diff --git a/LayoutTests/printing/page-with-zero-margin-expected.txt b/LayoutTests/printing/page-with-zero-margin-expected.txt
new file mode 100644 (file)
index 0000000..40083c8
--- /dev/null
@@ -0,0 +1,7 @@
+layer at (0,0) size 1000x18
+  RenderView at (0,0) size 1000x18
+layer at (0,0) size 1000x18
+  RenderBlock {HTML} at (0,0) size 1000x18
+    RenderBody {BODY} at (0,0) size 1000x18
+      RenderText {#text} at (0,0) size 318x18
+        text run at (0,0) width 318: "Text should start at the top right edge of the page."
diff --git a/LayoutTests/printing/page-with-zero-margin.html b/LayoutTests/printing/page-with-zero-margin.html
new file mode 100644 (file)
index 0000000..5c1b253
--- /dev/null
@@ -0,0 +1,25 @@
+<!DOCTYPE html><!-- webkit-test-runner [ experimental:PageAtRuleSupportEnabled=true ] -->
+<html>
+<head>
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
+  <title>This tests zero @page margin.</title>
+  <style type="text/css">
+    @page {
+        margin-top: 0;
+        margin-left: 0;
+    }
+    
+    html, body {
+        padding: 0px;
+        margin: 0px;
+    }
+  </style>
+  <script>
+    if (window.testRunner)
+      testRunner.setPrinting();
+  </script>
+</head>
+<body>
+Text should start at the top right edge of the page.
+</body>
+</html>
index d6d0d90..5ef61b7 100644 (file)
@@ -1,3 +1,27 @@
+2019-04-11  Zalan Bujtas  <zalan@apple.com>
+
+        Add @page margin support
+        https://bugs.webkit.org/show_bug.cgi?id=196680
+        <rdar://problem/45217707>
+
+        Reviewed by Tim Horton.
+
+        This patch adds support for @page margin. This feature is currently behind a runtime flag and is off by default.
+        We resolve the @page margin values in the WebProcess (currently no pseudo class is supported) and transfer them to the UIProcess through computedPagesCallback.
+        UIProcess is responsible for applying these new values on the printer. We also re-compute the available width/height for the current printing context
+        in the WebProcess if needed (see PrintInfo for current printer margins).
+
+        Tests: printing/page-with-10mm-left-margin.html
+               printing/page-with-zero-margin.html
+
+        * page/PrintContext.cpp:
+        (WebCore::PrintContext::computedPageMargin):
+        (WebCore::PrintContext::computedPageSize):
+        * page/PrintContext.h:
+        * page/RuntimeEnabledFeatures.h:
+        (WebCore::RuntimeEnabledFeatures::pageAtRuleSupportEnabled const):
+        (WebCore::RuntimeEnabledFeatures::setPageAtRuleSupportEnabled):
+
 2019-04-11  Dean Jackson  <dino@apple.com>
 
         Extract UTI mapping and allow for additions
index a3f900f..5ad3b93 100644 (file)
@@ -25,7 +25,9 @@
 #include "GraphicsContext.h"
 #include "Frame.h"
 #include "FrameView.h"
+#include "LengthBox.h"
 #include "RenderView.h"
+#include "RuntimeEnabledFeatures.h"
 #include "StyleInheritedData.h"
 #include "StyleResolver.h"
 #include "StyleScope.h"
@@ -78,6 +80,33 @@ void PrintContext::computePageRects(const FloatRect& printRect, float headerHeig
     computePageRectsWithPageSizeInternal(FloatSize(pageWidth / userScaleFactor, pageHeight / userScaleFactor), allowHorizontalTiling);
 }
 
+FloatBoxExtent PrintContext::computedPageMargin(FloatBoxExtent printMargin)
+{
+    if (!frame() || !frame()->document())
+        return printMargin;
+    if (!RuntimeEnabledFeatures::sharedFeatures().pageAtRuleSupportEnabled())
+        return printMargin;
+    // FIXME Currently no pseudo class is supported.
+    auto style = frame()->document()->styleScope().resolver().styleForPage(0);
+
+    auto pixelToPointScaleFactor = 1 / CSSPrimitiveValue::conversionToCanonicalUnitsScaleFactor(CSSPrimitiveValue::CSS_PT);
+    return { style->marginTop().isAuto() ? printMargin.top() : style->marginTop().value() * pixelToPointScaleFactor,
+        style->marginRight().isAuto() ? printMargin.right() : style->marginRight().value() * pixelToPointScaleFactor,
+        style->marginBottom().isAuto() ? printMargin.bottom() : style->marginBottom().value() * pixelToPointScaleFactor,
+        style->marginLeft().isAuto() ? printMargin.left() : style->marginLeft().value() * pixelToPointScaleFactor };
+}
+
+FloatSize PrintContext::computedPageSize(FloatSize pageSize, FloatBoxExtent printMargin)
+{
+    auto computedMargin = computedPageMargin(printMargin);
+    if (computedMargin == printMargin)
+        return pageSize;
+
+    auto horizontalMarginDelta = (printMargin.left() - computedMargin.left()) + (printMargin.right() - computedMargin.right()); 
+    auto verticalMarginDelta = (printMargin.top() - computedMargin.top()) + (printMargin.bottom() - computedMargin.bottom());
+    return { pageSize.width() + horizontalMarginDelta, pageSize.height() + verticalMarginDelta };
+}
+
 void PrintContext::computePageRectsWithPageSize(const FloatSize& pageSizeInPixels, bool allowHorizontalTiling)
 {
     m_pageRects.clear();
index ab3b41b..1827a13 100644 (file)
@@ -21,6 +21,7 @@
 #pragma once
 
 #include "FrameDestructionObserver.h"
+#include "LengthBox.h"
 #include <wtf/Forward.h>
 #include <wtf/HashMap.h>
 #include <wtf/Vector.h>
@@ -56,6 +57,8 @@ public:
     size_t pageCount() const { return m_pageRects.size(); }
     const IntRect& pageRect(size_t pageNumber) const { return m_pageRects[pageNumber]; }
     const Vector<IntRect>& pageRects() const { return m_pageRects; }
+    WEBCORE_EXPORT FloatBoxExtent computedPageMargin(FloatBoxExtent printMargin);
+    WEBCORE_EXPORT FloatSize computedPageSize(FloatSize pageSize, FloatBoxExtent printMargin);
 
     WEBCORE_EXPORT float computeAutomaticScaleFactor(const FloatSize& availablePaperSize);
 
index 9879f4c..c61c8fd 100644 (file)
@@ -350,6 +350,9 @@ public:
     bool referrerPolicyAttributeEnabled() const { return m_referrerPolicyAttributeEnabled; }
     void setReferrerPolicyAttributeEnabled(bool isEnabled) { m_referrerPolicyAttributeEnabled = isEnabled; }
 
+    bool pageAtRuleSupportEnabled() const { return m_pageAtRuleSupportEnabled; }
+    void setPageAtRuleSupportEnabled(bool isEnabled) { m_pageAtRuleSupportEnabled = isEnabled; }
+
     WEBCORE_EXPORT static RuntimeEnabledFeatures& sharedFeatures();
 
 private:
@@ -399,6 +402,7 @@ private:
     bool m_CSSCustomPropertiesAndValuesEnabled { false };
     bool m_pointerEventsEnabled { true };
     bool m_webSQLEnabled { true };
+    bool m_pageAtRuleSupportEnabled { false };
 
 #if ENABLE(LAYOUT_FORMATTING_CONTEXT)
     bool m_layoutFormattingContextEnabled { false };
index 2474177..4b4aab1 100644 (file)
@@ -1,3 +1,42 @@
+2019-04-11  Zalan Bujtas  <zalan@apple.com>
+
+        Add @page margin support
+        https://bugs.webkit.org/show_bug.cgi?id=196680
+        <rdar://problem/45217707>
+
+        Reviewed by Tim Horton.
+
+        * Shared/PrintInfo.cpp:
+        (WebKit::PrintInfo::encode const):
+        (WebKit::PrintInfo::decode):
+        * Shared/PrintInfo.h:
+        * Shared/WebPreferences.yaml:
+        * Shared/gtk/PrintInfoGtk.cpp:
+        (WebKit::PrintInfo::PrintInfo):
+        * Shared/mac/PrintInfoMac.mm:
+        (WebKit::PrintInfo::PrintInfo):
+        * UIProcess/API/C/WKPage.cpp:
+        (WKPageComputePagesForPrinting):
+        * UIProcess/API/C/WKPreferences.cpp:
+        (WKPreferencesSetPageAtRuleSupportEnabled):
+        (WKPreferencesGetPageAtRuleSupportEnabled):
+        * UIProcess/API/C/WKPreferencesRefPrivate.h:
+        * UIProcess/GenericCallback.h:
+        * UIProcess/WebPageProxy.cpp:
+        (WebKit::WebPageProxy::computedPagesCallback):
+        * UIProcess/WebPageProxy.h:
+        * UIProcess/WebPageProxy.messages.in:
+        * UIProcess/mac/WKPrintingView.mm:
+        (pageDidComputePageRects):
+        (-[WKPrintingView _askPageToComputePageRects]):
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::beginPrinting):
+        (WebKit::WebPage::computePagesForPrinting):
+        (WebKit::WebPage::computePagesForPrintingImpl):
+        * WebProcess/WebPage/WebPage.h:
+        * WebProcess/WebPage/ios/WebPageIOS.mm:
+        (WebKit::WebPage::computePagesForPrintingAndDrawToPDF):
+
 2019-04-11  Dean Jackson  <dino@apple.com>
 
         Extract UTI mapping and allow for additions
index 69f64cb..e55cdca 100644 (file)
@@ -40,6 +40,7 @@ void PrintInfo::encode(IPC::Encoder& encoder) const
     encoder << pageSetupScaleFactor;
     encoder << availablePaperWidth;
     encoder << availablePaperHeight;
+    encoder << margin;
 
 #if PLATFORM(GTK)
     IPC::encode(encoder, printSettings.get());
@@ -60,6 +61,8 @@ bool PrintInfo::decode(IPC::Decoder& decoder, PrintInfo& info)
         return false;
     if (!decoder.decode(info.availablePaperHeight))
         return false;
+    if (!decoder.decode(info.margin))
+        return false;
 
 #if PLATFORM(GTK)
     if (!IPC::decode(decoder, info.printSettings))
index 8845881..15c6b28 100644 (file)
@@ -35,6 +35,7 @@ typedef struct _GtkPageSetup GtkPageSetup;
 // FIXME: This should use the windows equivalent.
 class NSPrintInfo;
 #endif
+#include <WebCore/LengthBox.h>
 
 namespace IPC {
 class Decoder;
@@ -56,9 +57,11 @@ struct PrintInfo {
     explicit PrintInfo(NSPrintInfo *);
 #endif
 
+    // These values are in 'point' unit (and not CSS pixel).
     float pageSetupScaleFactor { 0 };
     float availablePaperWidth { 0 };
     float availablePaperHeight { 0 };
+    WebCore::FloatBoxExtent margin;
 #if PLATFORM(IOS_FAMILY)
     bool snapshotFirstPage { false };
 #endif
index 2a56f89..8ff2ca0 100644 (file)
@@ -1631,6 +1631,14 @@ ReferrerPolicyAttributeEnabled:
   category: experimental
   webcoreBinding: RuntimeEnabledFeatures
 
+PageAtRuleSupportEnabled:
+  type: bool
+  defaultValue: false
+  humanReadableName: "@page CSS at-rule support"
+  humanReadableDescription: "Enable @page support"
+  category: experimental
+  webcoreBinding: RuntimeEnabledFeatures
+
 ApplePayRemoteUIEnabled:
   category: internal
   condition: ENABLE(APPLE_PAY_REMOTE_UI)
index 968723f..455ea15 100644 (file)
@@ -34,6 +34,7 @@ PrintInfo::PrintInfo(GtkPrintSettings* settings, GtkPageSetup* pageSetup, PrintM
     : pageSetupScaleFactor(gtk_print_settings_get_scale(settings) / 100.0)
     , availablePaperWidth(gtk_page_setup_get_paper_width(pageSetup, GTK_UNIT_POINTS) - gtk_page_setup_get_left_margin(pageSetup, GTK_UNIT_POINTS) - gtk_page_setup_get_right_margin(pageSetup, GTK_UNIT_POINTS))
     , availablePaperHeight(gtk_page_setup_get_paper_height(pageSetup, GTK_UNIT_POINTS) - gtk_page_setup_get_top_margin(pageSetup, GTK_UNIT_POINTS) - gtk_page_setup_get_bottom_margin(pageSetup, GTK_UNIT_POINTS))
+    , margin(gtk_page_setup_get_top_margin(pageSetup, GTK_UNIT_POINTS), gtk_page_setup_get_right_margin(pageSetup, GTK_UNIT_POINTS), gtk_page_setup_get_bottom_margin(pageSetup, GTK_UNIT_POINTS), gtk_page_setup_get_left_margin(pageSetup, GTK_UNIT_POINTS))
     , printSettings(settings)
     , pageSetup(pageSetup)
     , printMode(printMode)
index 7c08428..625ddad 100644 (file)
@@ -34,6 +34,7 @@ PrintInfo::PrintInfo(NSPrintInfo *printInfo)
     : pageSetupScaleFactor([[[printInfo dictionary] objectForKey:NSPrintScalingFactor] floatValue])
     , availablePaperWidth([printInfo paperSize].width - [printInfo leftMargin] - [printInfo rightMargin])
     , availablePaperHeight([printInfo paperSize].height - [printInfo topMargin] - [printInfo bottomMargin])
+    , margin([printInfo topMargin], [printInfo rightMargin], [printInfo bottomMargin], [printInfo leftMargin])
 {
     ASSERT(printInfo);
 }
index 7cd7215..5f665a5 100644 (file)
@@ -2417,7 +2417,7 @@ static PrintInfo printInfoFromWKPrintInfo(const WKPrintInfo& printInfo)
 
 void WKPageComputePagesForPrinting(WKPageRef page, WKFrameRef frame, WKPrintInfo printInfo, WKPageComputePagesForPrintingFunction callback, void* context)
 {
-    toImpl(page)->computePagesForPrinting(toImpl(frame), printInfoFromWKPrintInfo(printInfo), ComputedPagesCallback::create([context, callback](const Vector<WebCore::IntRect>& rects, double scaleFactor, WebKit::CallbackBase::Error error) {
+    toImpl(page)->computePagesForPrinting(toImpl(frame), printInfoFromWKPrintInfo(printInfo), ComputedPagesCallback::create([context, callback](const Vector<WebCore::IntRect>& rects, double scaleFactor, const WebCore::FloatBoxExtent& computedPageMargin, WebKit::CallbackBase::Error error) {
         Vector<WKRect> wkRects(rects.size());
         for (size_t i = 0; i < rects.size(); ++i)
             wkRects[i] = toAPI(rects[i]);
index 7f07306..b277920 100644 (file)
@@ -155,7 +155,7 @@ static typename GenericCallback<InternalReturnValueType>::CallbackFunction toGen
 }
 
 typedef GenericCallback<> VoidCallback;
-typedef GenericCallback<const Vector<WebCore::IntRect>&, double> ComputedPagesCallback;
+typedef GenericCallback<const Vector<WebCore::IntRect>&, double, WebCore::FloatBoxExtent> ComputedPagesCallback;
 typedef GenericCallback<const ShareableBitmap::Handle&> ImageCallback;
 
 template<typename T>
index 2214438..3121f9b 100644 (file)
@@ -6417,7 +6417,7 @@ void WebPageProxy::scriptValueCallback(const IPC::DataReference& dataReference,
     callback->performCallbackWithReturnValue(API::SerializedScriptValue::adopt(WTFMove(data)).ptr(), hadException, details);
 }
 
-void WebPageProxy::computedPagesCallback(const Vector<IntRect>& pageRects, double totalScaleFactorForPrinting, CallbackID callbackID)
+void WebPageProxy::computedPagesCallback(const Vector<IntRect>& pageRects, double totalScaleFactorForPrinting, const FloatBoxExtent& computedPageMargin, CallbackID callbackID)
 {
     auto callback = m_callbacks.take<ComputedPagesCallback>(callbackID);
     if (!callback) {
@@ -6425,7 +6425,7 @@ void WebPageProxy::computedPagesCallback(const Vector<IntRect>& pageRects, doubl
         return;
     }
 
-    callback->performCallbackWithReturnValue(pageRects, totalScaleFactorForPrinting);
+    callback->performCallbackWithReturnValue(pageRects, totalScaleFactorForPrinting, computedPageMargin);
 }
 
 void WebPageProxy::validateCommandCallback(const String& commandName, bool isEnabled, int state, CallbackID callbackID)
index 9faa7fe..3e86af9 100644 (file)
@@ -1821,7 +1821,7 @@ private:
     void stringCallback(const String&, CallbackID);
     void invalidateStringCallback(CallbackID);
     void scriptValueCallback(const IPC::DataReference&, bool hadException, const WebCore::ExceptionDetails&, CallbackID);
-    void computedPagesCallback(const Vector<WebCore::IntRect>&, double totalScaleFactorForPrinting, CallbackID);
+    void computedPagesCallback(const Vector<WebCore::IntRect>&, double totalScaleFactorForPrinting, const WebCore::FloatBoxExtent& computedPageMargin, CallbackID);
     void validateCommandCallback(const String&, bool, int, CallbackID);
     void unsignedCallback(uint64_t, CallbackID);
     void editingRangeCallback(const EditingRange&, CallbackID);
index ac0238c..1553f85 100644 (file)
@@ -168,7 +168,7 @@ messages -> WebPageProxy {
     StringCallback(String resultString, WebKit::CallbackID callbackID)
     InvalidateStringCallback(WebKit::CallbackID callbackID)
     ScriptValueCallback(IPC::DataReference resultData, bool hadException, struct WebCore::ExceptionDetails details, WebKit::CallbackID callbackID)
-    ComputedPagesCallback(Vector<WebCore::IntRect> pageRects, double totalScaleFactorForPrinting, WebKit::CallbackID callbackID)
+    ComputedPagesCallback(Vector<WebCore::IntRect> pageRects, double totalScaleFactorForPrinting, WebCore::RectEdges<float> computedPageMargin, WebKit::CallbackID callbackID)
     ValidateCommandCallback(String command, bool isEnabled, int32_t state, WebKit::CallbackID callbackID)
     EditingRangeCallback(struct WebKit::EditingRange range, WebKit::CallbackID callbackID)
     UnsignedCallback(uint64_t result, WebKit::CallbackID callbackID)
index f89d4ee..b3db624 100644 (file)
@@ -293,7 +293,7 @@ static void pageDidDrawToImage(const WebKit::ShareableBitmap::Handle& imageHandl
     _webFrame->page()->drawPagesToPDF(_webFrame.get(), printInfo, firstPage - 1, lastPage - firstPage + 1, WTFMove(callback));
 }
 
-static void pageDidComputePageRects(const Vector<WebCore::IntRect>& pageRects, double totalScaleFactorForPrinting, IPCCallbackContext* context)
+static void pageDidComputePageRects(const Vector<WebCore::IntRect>& pageRects, double totalScaleFactorForPrinting, const WebCore::FloatBoxExtent& computedPageMargin, IPCCallbackContext* context)
 {
     ASSERT(RunLoop::isMain());
 
@@ -323,6 +323,12 @@ static void pageDidComputePageRects(const Vector<WebCore::IntRect>& pageRects, d
             ceil(lastPrintingPageRect.maxY() * view->_totalScaleFactorForPrinting));
         LOG(Printing, "WKPrintingView setting frame size to x:%g y:%g width:%g height:%g", newFrameSize.origin.x, newFrameSize.origin.y, newFrameSize.size.width, newFrameSize.size.height);
         [view setFrame:newFrameSize];
+        // Set @page margin.
+        auto *printInfo = [view->_printOperation printInfo];
+        [printInfo setTopMargin:computedPageMargin.top()];
+        [printInfo setBottomMargin:computedPageMargin.bottom()];
+        [printInfo setLeftMargin:computedPageMargin.left()];
+        [printInfo setRightMargin:computedPageMargin.right()];
 
         if ([view _isPrintingPreview]) {
             // Show page count, and ask for an actual image to replace placeholder.
@@ -344,9 +350,9 @@ static void pageDidComputePageRects(const Vector<WebCore::IntRect>& pageRects, d
     ASSERT(!_expectedComputedPagesCallback);
 
     IPCCallbackContext* context = new IPCCallbackContext;
-    auto callback = WebKit::ComputedPagesCallback::create([context](const Vector<WebCore::IntRect>& pageRects, double totalScaleFactorForPrinting, WebKit::CallbackBase::Error) {
+    auto callback = WebKit::ComputedPagesCallback::create([context](const Vector<WebCore::IntRect>& pageRects, double totalScaleFactorForPrinting, const WebCore::FloatBoxExtent& computedPageMargin, WebKit::CallbackBase::Error) {
         std::unique_ptr<IPCCallbackContext> contextDeleter(context);
-        pageDidComputePageRects(pageRects, totalScaleFactorForPrinting, context);
+        pageDidComputePageRects(pageRects, totalScaleFactorForPrinting, computedPageMargin, context);
     });
     _expectedComputedPagesCallback = callback->callbackID().toInteger();
     context->view = self;
index e8ec7d4..c12305e 100644 (file)
@@ -4623,10 +4623,11 @@ void WebPage::beginPrinting(uint64_t frameID, const PrintInfo& printInfo)
 
     freezeLayerTree(LayerTreeFreezeReason::Printing);
 
-    m_printContext->begin(printInfo.availablePaperWidth, printInfo.availablePaperHeight);
+    auto computedPageSize = m_printContext->computedPageSize(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight), printInfo.margin);
+    m_printContext->begin(computedPageSize.width(), computedPageSize.height());
 
     float fullPageHeight;
-    m_printContext->computePageRects(FloatRect(0, 0, printInfo.availablePaperWidth, printInfo.availablePaperHeight), 0, 0, printInfo.pageSetupScaleFactor, fullPageHeight, true);
+    m_printContext->computePageRects(FloatRect(0, 0, computedPageSize.width(), computedPageSize.height()), 0, 0, printInfo.pageSetupScaleFactor, fullPageHeight, true);
 
 #if PLATFORM(GTK)
     if (!m_printOperation)
@@ -4648,11 +4649,12 @@ void WebPage::computePagesForPrinting(uint64_t frameID, const PrintInfo& printIn
 {
     Vector<IntRect> resultPageRects;
     double resultTotalScaleFactorForPrinting = 1;
-    computePagesForPrintingImpl(frameID, printInfo, resultPageRects, resultTotalScaleFactorForPrinting);
-    send(Messages::WebPageProxy::ComputedPagesCallback(resultPageRects, resultTotalScaleFactorForPrinting, callbackID));
+    auto computedPageMargin = printInfo.margin;
+    computePagesForPrintingImpl(frameID, printInfo, resultPageRects, resultTotalScaleFactorForPrinting, computedPageMargin);
+    send(Messages::WebPageProxy::ComputedPagesCallback(resultPageRects, resultTotalScaleFactorForPrinting, computedPageMargin, callbackID));
 }
 
-void WebPage::computePagesForPrintingImpl(uint64_t frameID, const PrintInfo& printInfo, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting)
+void WebPage::computePagesForPrintingImpl(uint64_t frameID, const PrintInfo& printInfo, Vector<WebCore::IntRect>& resultPageRects, double& resultTotalScaleFactorForPrinting, FloatBoxExtent& computedPageMargin)
 {
     ASSERT(resultPageRects.isEmpty());
 
@@ -4660,7 +4662,9 @@ void WebPage::computePagesForPrintingImpl(uint64_t frameID, const PrintInfo& pri
 
     if (m_printContext) {
         resultPageRects = m_printContext->pageRects();
-        resultTotalScaleFactorForPrinting = m_printContext->computeAutomaticScaleFactor(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight)) * printInfo.pageSetupScaleFactor;
+        computedPageMargin = m_printContext->computedPageMargin(printInfo.margin);
+        auto computedPageSize = m_printContext->computedPageSize(FloatSize(printInfo.availablePaperWidth, printInfo.availablePaperHeight), printInfo.margin);
+        resultTotalScaleFactorForPrinting = m_printContext->computeAutomaticScaleFactor(computedPageSize) * printInfo.pageSetupScaleFactor;
     }
 #if PLATFORM(COCOA)
     else
index aee7748..8bb07e0 100644 (file)
@@ -854,7 +854,7 @@ public:
     void beginPrinting(uint64_t frameID, const PrintInfo&);
     void endPrinting();
     void computePagesForPrinting(uint64_t frameID, const PrintInfo&, CallbackID);
-    void computePagesForPrintingImpl(uint64_t frameID, const PrintInfo&, Vector<WebCore::IntRect>& pageRects, double& totalScaleFactor);
+    void computePagesForPrintingImpl(uint64_t frameID, const PrintInfo&, Vector<WebCore::IntRect>& pageRects, double& totalScaleFactor, WebCore::FloatBoxExtent& computedMargin);
 
 #if PLATFORM(COCOA)
     void drawRectToImage(uint64_t frameID, const PrintInfo&, const WebCore::IntRect&, const WebCore::IntSize&, CallbackID);
index b37aff5..0684718 100644 (file)
@@ -3281,7 +3281,8 @@ void WebPage::computePagesForPrintingAndDrawToPDF(uint64_t frameID, const PrintI
 
     Vector<WebCore::IntRect> pageRects;
     double totalScaleFactor;
-    computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor);
+    auto margin = printInfo.margin;
+    computePagesForPrintingImpl(frameID, printInfo, pageRects, totalScaleFactor, margin);
 
     ASSERT(pageRects.size() >= 1);
     std::size_t pageCount = pageRects.size();