Title: [96626] trunk
Revision
96626
Author
commit-qu...@webkit.org
Date
2011-10-04 11:58:47 -0700 (Tue, 04 Oct 2011)

Log Message

HTML canvas strokes with dash and dashOffset
https://bugs.webkit.org/show_bug.cgi?id=63933

Patch by Young Han Lee <joybro...@gmail.com> on 2011-10-04
Reviewed by Simon Fraser.

Add webkitLineDash and webkitLineDashOffset attributes to CanvasRenderingContext2D for JSC.
These attributes can be used to determine the dash-style of stroke in HTML Canvas.

As this kind of attributes are not specified in the HTML Canvas specification yet,
the 'webkit' prefix is added to its names.

Mozilla already implemented these attributes, mozDash and mozDashOffset [1], and this patch
is created by referring to the implementation. This patch is basically using the specified
behavior of stroke-dasharray and stroke-dashoffset in the SVG specification [2], except
that doesn't support units or percentages, just floating point numbers.

[1] https://bugzilla.mozilla.org/show_bug.cgi?id=662038
[2] http://www.w3.org/TR/SVG/painting.html#StrokeProperties

Source/WebCore:

Tests: fast/canvas/canvas-webkitLineDash-invalid.html
       fast/canvas/canvas-webkitLineDash.html

* bindings/js/JSCanvasRenderingContext2DCustom.cpp:
(WebCore::JSCanvasRenderingContext2D::webkitLineDash):
(WebCore::JSCanvasRenderingContext2D::setWebkitLineDash):
* html/canvas/CanvasRenderingContext2D.cpp:
(WebCore::CanvasRenderingContext2D::State::State):
(WebCore::CanvasRenderingContext2D::webkitLineDash):
(WebCore::CanvasRenderingContext2D::setWebkitLineDash):
(WebCore::CanvasRenderingContext2D::webkitLineDashOffset):
(WebCore::CanvasRenderingContext2D::setWebkitLineDashOffset):
* html/canvas/CanvasRenderingContext2D.h:
* html/canvas/CanvasRenderingContext2D.idl:

LayoutTests:

* fast/canvas/canvas-webkitLineDash-expected.txt: Added.
* fast/canvas/canvas-webkitLineDash-invalid-expected.txt: Added.
* fast/canvas/canvas-webkitLineDash-invalid.html: Added.
* fast/canvas/canvas-webkitLineDash.html: Added.
* fast/canvas/script-tests/canvas-webkitLineDash-invalid.js: Added.
(trySettingLineDash):
(trySettingLineDashOffset):
* fast/canvas/script-tests/canvas-webkitLineDash.js: Added.
* platform/chromium/test_expectations.txt:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (96625 => 96626)


--- trunk/LayoutTests/ChangeLog	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/LayoutTests/ChangeLog	2011-10-04 18:58:47 UTC (rev 96626)
@@ -1,3 +1,34 @@
+2011-10-04  Young Han Lee  <joybro...@gmail.com>
+
+        HTML canvas strokes with dash and dashOffset
+        https://bugs.webkit.org/show_bug.cgi?id=63933
+
+        Reviewed by Simon Fraser.
+
+        Add webkitLineDash and webkitLineDashOffset attributes to CanvasRenderingContext2D for JSC.
+        These attributes can be used to determine the dash-style of stroke in HTML Canvas.
+
+        As this kind of attributes are not specified in the HTML Canvas specification yet,
+        the 'webkit' prefix is added to its names.
+
+        Mozilla already implemented these attributes, mozDash and mozDashOffset [1], and this patch
+        is created by referring to the implementation. This patch is basically using the specified
+        behavior of stroke-dasharray and stroke-dashoffset in the SVG specification [2], except
+        that doesn't support units or percentages, just floating point numbers.
+
+        [1] https://bugzilla.mozilla.org/show_bug.cgi?id=662038
+        [2] http://www.w3.org/TR/SVG/painting.html#StrokeProperties
+
+        * fast/canvas/canvas-webkitLineDash-expected.txt: Added.
+        * fast/canvas/canvas-webkitLineDash-invalid-expected.txt: Added.
+        * fast/canvas/canvas-webkitLineDash-invalid.html: Added.
+        * fast/canvas/canvas-webkitLineDash.html: Added.
+        * fast/canvas/script-tests/canvas-webkitLineDash-invalid.js: Added.
+        (trySettingLineDash):
+        (trySettingLineDashOffset):
+        * fast/canvas/script-tests/canvas-webkitLineDash.js: Added.
+        * platform/chromium/test_expectations.txt:
+
 2011-10-04  Tim Horton  <timothy_hor...@apple.com>
 
         Additional unreviewed Lion SVG pixel test rebaselining.

Added: trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-expected.txt (0 => 96626)


--- trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-expected.txt	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,13 @@
+Basic test for webkitLineDash and webkitLineDashOffset
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS ctx.webkitLineDashOffset is 0
+PASS lineDash[0] is 15
+PASS lineDash[1] is 10
+PASS ctx.webkitLineDashOffset is 5
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid-expected.txt (0 => 96626)


--- trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid-expected.txt	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,21 @@
+Test for invalid input of webkitLineDash and webkitLineDashOffset
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS trySettingLineDash([1, 2, 3]) is [1, 2, 3]
+PASS trySettingLineDash([1, -1]) is [1.5, 2.5]
+PASS trySettingLineDash([1, Infinity]) is [1.5, 2.5]
+PASS trySettingLineDash([1, -Infinity]) is [1.5, 2.5]
+PASS trySettingLineDash([1, NaN]) is [1.5, 2.5]
+PASS trySettingLineDash([1, 'string']) is [1.5, 2.5]
+PASS trySettingLineDashOffset(0.5) is 0.5
+PASS trySettingLineDashOffset(-0.5) is -0.5
+PASS trySettingLineDashOffset(Infinity) is 1.5
+PASS trySettingLineDashOffset(-Infinity) is 1.5
+PASS trySettingLineDashOffset(NaN) is 1.5
+PASS trySettingLineDashOffset('string') is 1.5
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid.html (0 => 96626)


--- trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid.html	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/canvas-webkitLineDash-invalid.html	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<link rel="stylesheet" href=""
+<script src=""
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src=""
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/fast/canvas/canvas-webkitLineDash.html (0 => 96626)


--- trunk/LayoutTests/fast/canvas/canvas-webkitLineDash.html	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/canvas-webkitLineDash.html	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,13 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<link rel="stylesheet" href=""
+<script src=""
+</head>
+<body>
+<p id="description"></p>
+<div id="console"></div>
+<script src=""
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash-invalid.js (0 => 96626)


--- trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash-invalid.js	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash-invalid.js	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,35 @@
+description("Test for invalid input of webkitLineDash and webkitLineDashOffset");
+
+var canvas = document.createElement('canvas');
+document.body.appendChild(canvas);
+canvas.setAttribute('width', '700');
+canvas.setAttribute('height', '700');
+var ctx = canvas.getContext('2d');
+
+function trySettingLineDash(value) {
+    ctx.webkitLineDash = [1.5, 2.5];
+    ctx.webkitLineDash = value;
+    return ctx.webkitLineDash;
+}
+
+function trySettingLineDashOffset(value) {
+    ctx.webkitLineDashOffset = 1.5;
+    ctx.webkitLineDashOffset = value;
+    return ctx.webkitLineDashOffset;
+}
+
+shouldBe("trySettingLineDash([1, 2, 3])", "[1, 2, 3]");
+shouldBe("trySettingLineDash([1, -1])", "[1.5, 2.5]");
+shouldBe("trySettingLineDash([1, Infinity])", "[1.5, 2.5]");
+shouldBe("trySettingLineDash([1, -Infinity])", "[1.5, 2.5]");
+shouldBe("trySettingLineDash([1, NaN])", "[1.5, 2.5]");
+shouldBe("trySettingLineDash([1, 'string'])", "[1.5, 2.5]");
+
+shouldBe("trySettingLineDashOffset(0.5)", "0.5");
+shouldBe("trySettingLineDashOffset(-0.5)", "-0.5");
+shouldBe("trySettingLineDashOffset(Infinity)", "1.5");
+shouldBe("trySettingLineDashOffset(-Infinity)", "1.5");
+shouldBe("trySettingLineDashOffset(NaN)", "1.5");
+shouldBe("trySettingLineDashOffset('string')", "1.5");
+
+var successfullyParsed = true;

Added: trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash.js (0 => 96626)


--- trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash.js	                        (rev 0)
+++ trunk/LayoutTests/fast/canvas/script-tests/canvas-webkitLineDash.js	2011-10-04 18:58:47 UTC (rev 96626)
@@ -0,0 +1,24 @@
+description("Basic test for webkitLineDash and webkitLineDashOffset");
+
+var canvas = document.createElement('canvas');
+document.body.appendChild(canvas);
+canvas.setAttribute('width', '700');
+canvas.setAttribute('height', '700');
+var ctx = canvas.getContext('2d');
+
+// Verify default values.
+shouldBe('ctx.webkitLineDashOffset', '0');
+
+// Set dash-style.
+ctx.webkitLineDash = [15, 10];
+ctx.webkitLineDashOffset = 5;
+ctx.strokeRect (10,10,100,100);
+
+// Verify dash and offset.
+var lineDash;
+lineDash = ctx.webkitLineDash;
+shouldBe('lineDash[0]', '15');
+shouldBe('lineDash[1]', '10');
+shouldBe('ctx.webkitLineDashOffset', '5');
+
+var successfullyParsed = true;

Modified: trunk/LayoutTests/platform/chromium/test_expectations.txt (96625 => 96626)


--- trunk/LayoutTests/platform/chromium/test_expectations.txt	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/LayoutTests/platform/chromium/test_expectations.txt	2011-10-04 18:58:47 UTC (rev 96626)
@@ -97,6 +97,10 @@
 // fails for other platforms...
 BUGCR20404 : editing/execCommand/copy-without-selection.html = TEXT
 
+// This will soon be fixed after implementing webkitLineDash for V8.
+BUGWK63933 : fast/canvas/canvas-webkitLineDash.html = FAIL
+BUGWK63933 : fast/canvas/canvas-webkitLineDash-invalid.html = FAIL
+
 // -----------------------------------------------------------------
 // WONTFIX TESTS
 // -----------------------------------------------------------------

Modified: trunk/Source/WebCore/ChangeLog (96625 => 96626)


--- trunk/Source/WebCore/ChangeLog	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/Source/WebCore/ChangeLog	2011-10-04 18:58:47 UTC (rev 96626)
@@ -1,3 +1,39 @@
+2011-10-04  Young Han Lee  <joybro...@gmail.com>
+
+        HTML canvas strokes with dash and dashOffset
+        https://bugs.webkit.org/show_bug.cgi?id=63933
+
+        Reviewed by Simon Fraser.
+
+        Add webkitLineDash and webkitLineDashOffset attributes to CanvasRenderingContext2D for JSC.
+        These attributes can be used to determine the dash-style of stroke in HTML Canvas.
+
+        As this kind of attributes are not specified in the HTML Canvas specification yet,
+        the 'webkit' prefix is added to its names.
+
+        Mozilla already implemented these attributes, mozDash and mozDashOffset [1], and this patch
+        is created by referring to the implementation. This patch is basically using the specified
+        behavior of stroke-dasharray and stroke-dashoffset in the SVG specification [2], except
+        that doesn't support units or percentages, just floating point numbers.
+
+        [1] https://bugzilla.mozilla.org/show_bug.cgi?id=662038
+        [2] http://www.w3.org/TR/SVG/painting.html#StrokeProperties
+
+        Tests: fast/canvas/canvas-webkitLineDash-invalid.html
+               fast/canvas/canvas-webkitLineDash.html
+
+        * bindings/js/JSCanvasRenderingContext2DCustom.cpp:
+        (WebCore::JSCanvasRenderingContext2D::webkitLineDash):
+        (WebCore::JSCanvasRenderingContext2D::setWebkitLineDash):
+        * html/canvas/CanvasRenderingContext2D.cpp:
+        (WebCore::CanvasRenderingContext2D::State::State):
+        (WebCore::CanvasRenderingContext2D::webkitLineDash):
+        (WebCore::CanvasRenderingContext2D::setWebkitLineDash):
+        (WebCore::CanvasRenderingContext2D::webkitLineDashOffset):
+        (WebCore::CanvasRenderingContext2D::setWebkitLineDashOffset):
+        * html/canvas/CanvasRenderingContext2D.h:
+        * html/canvas/CanvasRenderingContext2D.idl:
+
 2011-10-04  Matthew Delaney  <mdela...@apple.com>
 
         Allow canvas backing store to be lazily allocated

Modified: trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp (96625 => 96626)


--- trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/Source/WebCore/bindings/js/JSCanvasRenderingContext2DCustom.cpp	2011-10-04 18:58:47 UTC (rev 96626)
@@ -158,4 +158,35 @@
     return jsUndefined();
 }
 
+JSValue JSCanvasRenderingContext2D::webkitLineDash(ExecState* exec) const
+{
+    CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl());
+    const DashArray* dash = context->webkitLineDash();
+
+    MarkedArgumentBuffer list;
+    DashArray::const_iterator end = dash->end();
+    for (DashArray::const_iterator it = dash->begin(); it != end; ++it)
+        list.append(JSValue(*it));
+    return constructArray(exec, globalObject(), list);
+}
+
+void JSCanvasRenderingContext2D::setWebkitLineDash(ExecState* exec, JSValue value)
+{
+    if (!isJSArray(&exec->globalData(), value))
+        return;
+
+    DashArray dash;
+    JSArray* valueArray = asArray(value);
+    for (unsigned i = 0; i < valueArray->length(); ++i) {
+        float elem = valueArray->getIndex(i).toFloat(exec);
+        if (elem <= 0 || !isfinite(elem))
+            return;
+
+        dash.append(elem);
+    }
+
+    CanvasRenderingContext2D* context = static_cast<CanvasRenderingContext2D*>(impl());
+    context->setWebkitLineDash(dash);
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp (96625 => 96626)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.cpp	2011-10-04 18:58:47 UTC (rev 96626)
@@ -192,6 +192,7 @@
     , m_globalAlpha(1)
     , m_globalComposite(CompositeSourceOver)
     , m_invertibleCTM(true)
+    , m_lineDashOffset(0)
     , m_textAlign(StartTextAlign)
     , m_textBaseline(AlphabeticTextBaseline)
     , m_unparsedFont(defaultFont)
@@ -496,6 +497,39 @@
     applyShadow();
 }
 
+const DashArray* CanvasRenderingContext2D::webkitLineDash() const
+{
+    return &state().m_lineDash;
+}
+
+void CanvasRenderingContext2D::setWebkitLineDash(const DashArray& dash)
+{
+    state().m_lineDash = dash;
+
+    GraphicsContext* c = drawingContext();
+    if (!c)
+        return;
+    c->setLineDash(state().m_lineDash, state().m_lineDashOffset);
+}
+
+float CanvasRenderingContext2D::webkitLineDashOffset() const
+{
+    return state().m_lineDashOffset;
+}
+
+void CanvasRenderingContext2D::setWebkitLineDashOffset(float offset)
+{
+    if (!isfinite(offset))
+        return;
+
+    state().m_lineDashOffset = offset;
+
+    GraphicsContext* c = drawingContext();
+    if (!c)
+        return;
+    c->setLineDash(state().m_lineDash, state().m_lineDashOffset);
+}
+
 float CanvasRenderingContext2D::globalAlpha() const
 {
     return state().m_globalAlpha;

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h (96625 => 96626)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.h	2011-10-04 18:58:47 UTC (rev 96626)
@@ -29,6 +29,7 @@
 #include "AffineTransform.h"
 #include "CanvasRenderingContext.h"
 #include "Color.h"
+#include "DashArray.h"
 #include "FloatSize.h"
 #include "Font.h"
 #include "GraphicsTypes.h"
@@ -85,6 +86,12 @@
     float miterLimit() const;
     void setMiterLimit(float);
 
+    const DashArray* webkitLineDash() const;
+    void setWebkitLineDash(const DashArray&);
+
+    float webkitLineDashOffset() const;
+    void setWebkitLineDashOffset(float);
+
     float shadowOffsetX() const;
     void setShadowOffsetX(float);
 
@@ -242,6 +249,8 @@
         CompositeOperator m_globalComposite;
         AffineTransform m_transform;
         bool m_invertibleCTM;
+        DashArray m_lineDash;
+        float m_lineDashOffset;
 
         // Text state.
         TextAlign m_textAlign;

Modified: trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.idl (96625 => 96626)


--- trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.idl	2011-10-04 18:56:26 UTC (rev 96625)
+++ trunk/Source/WebCore/html/canvas/CanvasRenderingContext2D.idl	2011-10-04 18:58:47 UTC (rev 96626)
@@ -60,6 +60,12 @@
         attribute float shadowBlur;
         attribute [ConvertNullToNullString] DOMString shadowColor;
 
+        // FIXME: These attributes should also be implemented for V8.
+#if !(defined(V8_BINDING) && V8_BINDING)
+        attribute [Custom] Array webkitLineDash;
+        attribute float webkitLineDashOffset;
+#endif
+
         void clearRect(in float x, in float y, in float width, in float height);
         void fillRect(in float x, in float y, in float width, in float height);
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to