changeset 11176:51364187b4e1 12+9

8215702: SVG gradients are not rendered Reviewed-by: mbilla, kcr
author arajkumar
date Thu, 17 Jan 2019 11:56:03 +0530
parents ba2286ffd9cb
children 6c5cd73318dd
files modules/javafx.web/src/main/native/Source/WebCore/platform/graphics/java/GraphicsContextJava.cpp modules/javafx.web/src/shims/java/com/sun/javafx/webkit/prism/WCBufferedContextShim.java modules/javafx.web/src/shims/java/com/sun/webkit/WebPageShim.java modules/javafx.web/src/shims/java/com/sun/webkit/graphics/WCGraphicsManagerShim.java modules/javafx.web/src/test/addExports modules/javafx.web/src/test/java/test/javafx/scene/web/CanvasTest.java modules/javafx.web/src/test/java/test/javafx/scene/web/MiscellaneousTest.java modules/javafx.web/src/test/java/test/javafx/scene/web/TestBase.java
diffstat 8 files changed, 162 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/modules/javafx.web/src/main/native/Source/WebCore/platform/graphics/java/GraphicsContextJava.cpp	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/main/native/Source/WebCore/platform/graphics/java/GraphicsContextJava.cpp	Thu Jan 17 11:56:03 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -84,6 +84,9 @@
             }
     );
 
+    p0 = gt.mapPoint(p0);
+    p1 = gt.mapPoint(p1);
+
     context->rq().freeSpace(4 * 11 + 8 * nStops)
     << id
     << (jfloat)p0.x()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/javafx.web/src/shims/java/com/sun/javafx/webkit/prism/WCBufferedContextShim.java	Thu Jan 17 11:56:03 2019 +0530
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.javafx.webkit.prism;
+
+import com.sun.javafx.webkit.prism.PrismImage;
+import com.sun.webkit.graphics.WCGraphicsContext;
+import com.sun.webkit.graphics.WCGraphicsManagerShim;
+import com.sun.webkit.graphics.WCImage;
+
+public class WCBufferedContextShim {
+    public static WCGraphicsContext createBufferedContext(int w, int h) {
+        final WCImage img = WCGraphicsManagerShim.createRTImage(w, h);
+        return new WCBufferedContext((PrismImage) img);
+    }
+}
+
--- a/modules/javafx.web/src/shims/java/com/sun/webkit/WebPageShim.java	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/shims/java/com/sun/webkit/WebPageShim.java	Thu Jan 17 11:56:03 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2017, 2019 Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,12 +25,15 @@
 
 package com.sun.webkit;
 
+import com.sun.javafx.webkit.prism.WCBufferedContextShim;
 import com.sun.webkit.WebPage;
 import com.sun.webkit.event.WCMouseEvent;
 import com.sun.webkit.graphics.WCGraphicsContext;
 import com.sun.webkit.graphics.WCGraphicsManager;
+import com.sun.webkit.graphics.WCGraphicsManagerShim;
 import com.sun.webkit.graphics.WCPageBackBuffer;
 import com.sun.webkit.graphics.WCRectangle;
+import java.awt.image.BufferedImage;
 
 public class WebPageShim {
 
@@ -42,10 +45,13 @@
         page.setBounds(x, y, w, h);
         // forces layout and renders the page into RenderQueue.
         page.updateContent(new WCRectangle(x, y, w, h));
+        return WCBufferedContextShim.createBufferedContext(w, h);
+    }
 
-        final WCPageBackBuffer buffer = WCGraphicsManager.getGraphicsManager().createPageBackBuffer();
-        buffer.validate(w, h);
-        return buffer.createGraphics();
+    public static BufferedImage paint(WebPage page, int x, int y, int w, int h) {
+        final WCGraphicsContext gc = setupPageWithGraphics(page, x, y, w, h);
+        page.paint(gc, x, y, w, h);
+        return gc.getImage().toBufferedImage();
     }
 
     public static void mockPrint(WebPage page, int x, int y, int w, int h) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/modules/javafx.web/src/shims/java/com/sun/webkit/graphics/WCGraphicsManagerShim.java	Thu Jan 17 11:56:03 2019 +0530
@@ -0,0 +1,36 @@
+/*
+ * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+package com.sun.webkit.graphics;
+
+import com.sun.webkit.graphics.WCGraphicsManager;
+import com.sun.webkit.graphics.WCImage;
+
+public class WCGraphicsManagerShim {
+
+    public static WCImage createRTImage(int w, int h) {
+        return WCGraphicsManager.getGraphicsManager().createRTImage(w, h);
+    }
+}
--- a/modules/javafx.web/src/test/addExports	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/test/addExports	Thu Jan 17 11:56:03 2019 +0530
@@ -48,9 +48,11 @@
 --add-exports javafx.graphics/com.sun.scenario=ALL-UNNAMED
 #
 --add-exports javafx.web/com.sun.javafx.scene.web=ALL-UNNAMED
+--add-exports javafx.web/com.sun.javafx.webkit.prism=ALL-UNNAMED
 --add-exports javafx.web/com.sun.javafx.webkit=ALL-UNNAMED
---add-exports javafx.web/com.sun.webkit=ALL-UNNAMED
 --add-exports javafx.web/com.sun.webkit.dom=ALL-UNNAMED
 --add-exports javafx.web/com.sun.webkit.event=ALL-UNNAMED
+--add-exports javafx.web/com.sun.webkit.graphics=ALL-UNNAMED
 --add-exports javafx.web/com.sun.webkit.network=ALL-UNNAMED
 --add-exports javafx.web/com.sun.webkit.text=ALL-UNNAMED
+--add-exports javafx.web/com.sun.webkit=ALL-UNNAMED
--- a/modules/javafx.web/src/test/java/test/javafx/scene/web/CanvasTest.java	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/test/java/test/javafx/scene/web/CanvasTest.java	Thu Jan 17 11:56:03 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -26,7 +26,6 @@
 package test.javafx.scene.web;
 
 import java.awt.Color;
-import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -126,21 +125,6 @@
         });
     }
 
-    // Color comparison algorithm is based on WebKit's Tools/ImageDiff/PlaformImage.cpp#PlatformImage::difference implemenation.
-    // https://trac.webkit.org/browser/webkit/trunk/Tools/ImageDiff/PlatformImage.cpp
-    private static float getColorDifference(final Color base, final Color c) {
-        final float red = (c.getRed() - base.getRed()) / Math.max(255.0f - base.getRed(), base.getRed());
-        final float green = (c.getGreen() - base.getGreen()) / Math.max(255.0f - base.getGreen(), base.getGreen());
-        final float blue = (c.getBlue() - base.getBlue()) / Math.max(255.0f - base.getBlue(), base.getBlue());
-        final float alpha = (c.getAlpha() - base.getAlpha()) / Math.max(255.0f - base.getAlpha(), base.getAlpha());
-        final float distance = ((float) Math.sqrt(red * red + green * green + blue * blue + alpha * alpha)) / 2.0f;
-        return distance >= (1 / 255.0f) ? distance * 100.0f : 0;
-    }
-
-    private static boolean isColorsSimilar(final Color base, final Color c, float toleranceInPercentage) {
-        return toleranceInPercentage >= getColorDifference(base, c);
-    }
-
     private BufferedImage htmlCanvasToBufferedImage(final String mime) throws Exception {
         ByteArrayOutputStream errStream = new ByteArrayOutputStream();
         System.setErr(new PrintStream(errStream));
--- a/modules/javafx.web/src/test/java/test/javafx/scene/web/MiscellaneousTest.java	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/test/java/test/javafx/scene/web/MiscellaneousTest.java	Thu Jan 17 11:56:03 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,12 +25,13 @@
 
 package test.javafx.scene.web;
 
+import java.awt.Color;
+import java.awt.image.BufferedImage;
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileReader;
 import java.io.IOException;
-import static java.lang.String.format;
 import java.net.HttpURLConnection;
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -45,15 +46,22 @@
 import javafx.scene.web.WebEngine;
 import javafx.scene.web.WebView;
 import netscape.javascript.JSObject;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNull;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
 import org.junit.Test;
 import org.w3c.dom.Document;
 
+import static java.lang.String.format;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import javafx.scene.web.WebEngineShim;
+import com.sun.webkit.WebPage;
+import com.sun.webkit.WebPageShim;
+import com.sun.webkit.graphics.WCGraphicsContext;
+
 public class MiscellaneousTest extends TestBase {
 
     @Test public void testNoEffectOnFollowRedirects() {
@@ -453,4 +461,40 @@
             }
         });
     }
+
+    @Test public void testSVGRenderingWithGradient() {
+        loadContent("<html>\n" +
+                    "<body style='margin: 0px 0px;'>\n" +
+                    "<svg width='400' height='150'>\n" +
+                    "<defs>\n" +
+                    "<linearGradient id='grad1' x1='0%' y1='0%' x2='100%' y2='100%'>\n" +
+                    "<stop offset='0%' style='stop-color:red' />\n" +
+                    "<stop offset='100%' style='stop-color:yellow' />\n" +
+                    "</linearGradient>\n" +
+                    "</defs>\n" +
+                    "<rect width='400' height='150' fill='url(#grad1)' />\n" +
+                    "</svg>\n" +
+                    "</body>\n" +
+                    "</html>");
+        submit(() -> {
+            final WebPage webPage = WebEngineShim.getPage(getEngine());
+            assertNotNull(webPage);
+            final BufferedImage img = WebPageShim.paint(webPage, 0, 0, 800, 600);
+            assertNotNull(img);
+
+            final Color pixelAt0x0 = new Color(img.getRGB(0, 0), true);
+            assertTrue("Color should be opaque red:" + pixelAt0x0, isColorsSimilar(Color.RED, pixelAt0x0, 1));
+
+            final Color pixelAt100x36 = new Color(img.getRGB(100, 36), true);
+            assertTrue("Color should be almost red:" + pixelAt100x36, isColorsSimilar(Color.RED, pixelAt100x36, 40));
+            assertFalse("Color shouldn't be yellow:" + pixelAt100x36, isColorsSimilar(Color.YELLOW, pixelAt100x36, 10));
+
+            final Color pixelAt200x75 = new Color(img.getRGB(200, 75), true);
+            assertFalse("Color shouldn't be red:" + pixelAt200x75, isColorsSimilar(Color.RED, pixelAt200x75, 10));
+            assertTrue("Color should look like yellow:" + pixelAt200x75, isColorsSimilar(Color.YELLOW, pixelAt200x75, 40));
+
+            final Color pixelAt399x145 = new Color(img.getRGB(399, 149), true);
+            assertTrue("Color should be opaque yellow:" + pixelAt399x145, isColorsSimilar(Color.YELLOW, pixelAt399x145, 1));
+        });
+    }
 }
--- a/modules/javafx.web/src/test/java/test/javafx/scene/web/TestBase.java	Wed Jan 16 17:12:32 2019 -0800
+++ b/modules/javafx.web/src/test/java/test/javafx/scene/web/TestBase.java	Thu Jan 17 11:56:03 2019 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -25,6 +25,7 @@
 
 package test.javafx.scene.web;
 
+import java.awt.Color;
 import java.io.File;
 import java.util.concurrent.Callable;
 import java.util.concurrent.CountDownLatch;
@@ -300,4 +301,19 @@
 
         return clazz != null;
     }
+
+    // Color comparison algorithm is based on WebKit's Tools/ImageDiff/PlaformImage.cpp#PlatformImage::difference implemenation.
+    // https://trac.webkit.org/browser/webkit/trunk/Tools/ImageDiff/PlatformImage.cpp
+    protected static float getColorDifference(final Color base, final Color c) {
+        final float red = (c.getRed() - base.getRed()) / Math.max(255.0f - base.getRed(), base.getRed());
+        final float green = (c.getGreen() - base.getGreen()) / Math.max(255.0f - base.getGreen(), base.getGreen());
+        final float blue = (c.getBlue() - base.getBlue()) / Math.max(255.0f - base.getBlue(), base.getBlue());
+        final float alpha = (c.getAlpha() - base.getAlpha()) / Math.max(255.0f - base.getAlpha(), base.getAlpha());
+        final float distance = ((float) Math.sqrt(red * red + green * green + blue * blue + alpha * alpha)) / 2.0f;
+        return distance >= (1 / 255.0f) ? distance * 100.0f : 0;
+    }
+
+    protected static boolean isColorsSimilar(final Color base, final Color c, float toleranceInPercentage) {
+        return toleranceInPercentage >= getColorDifference(base, c);
+    }
 }