Home | History | Annotate | Download | only in graphics
      1 // Copyright (C) 2013 Google Inc. All rights reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions are
      5 // met:
      6 //
      7 //    * Redistributions of source code must retain the above copyright
      8 // notice, this list of conditions and the following disclaimer.
      9 //    * Redistributions in binary form must reproduce the above
     10 // copyright notice, this list of conditions and the following disclaimer
     11 // in the documentation and/or other materials provided with the
     12 // distribution.
     13 //    * Neither the name of Google Inc. nor the names of its
     14 // contributors may be used to endorse or promote products derived from
     15 // this software without specific prior written permission.
     16 //
     17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 
     29 #ifndef StrokeData_h
     30 #define StrokeData_h
     31 
     32 #include "core/platform/graphics/DashArray.h"
     33 #include "core/platform/graphics/Gradient.h"
     34 #include "core/platform/graphics/GraphicsTypes.h"
     35 #include "core/platform/graphics/Pattern.h"
     36 
     37 #include "third_party/skia/include/core/SkColorPriv.h"
     38 #include "third_party/skia/include/effects/SkDashPathEffect.h"
     39 
     40 #include "wtf/PassRefPtr.h"
     41 #include "wtf/RefPtr.h"
     42 
     43 namespace WebCore {
     44 
     45 // Encapsulates stroke painting information.
     46 // It is pulled out of GraphicsContextState to enable other methods to use it.
     47 class StrokeData {
     48 public:
     49     StrokeData()
     50         : m_style(SolidStroke)
     51         , m_thickness(0)
     52         , m_color(Color::black)
     53         , m_lineCap(SkPaint::kDefault_Cap)
     54         , m_lineJoin(SkPaint::kDefault_Join)
     55         , m_miterLimit(4)
     56     {
     57     }
     58 
     59     StrokeStyle style() const { return m_style; }
     60     void setStyle(const StrokeStyle style) { m_style = style; }
     61 
     62     float thickness() const { return m_thickness; }
     63     void setThickness(const float thickness) { m_thickness = thickness; }
     64 
     65     Color color() const { return m_color; }
     66     void setColor(const Color& color) { m_color = color; }
     67 
     68     Gradient* gradient() const { return m_gradient.get(); }
     69     void setGradient(const PassRefPtr<Gradient> gradient) { m_gradient = gradient; }
     70     void clearGradient() { m_gradient.clear(); }
     71 
     72     Pattern* pattern() const { return m_pattern.get(); }
     73     void setPattern(const PassRefPtr<Pattern> pattern) { m_pattern = pattern; }
     74     void clearPattern() { m_pattern.clear(); }
     75 
     76     LineCap lineCap() const { return (LineCap)m_lineCap; }
     77     void setLineCap(const LineCap cap) { m_lineCap = (SkPaint::Cap)cap; }
     78 
     79     LineJoin lineJoin() const { return (LineJoin)m_lineJoin; }
     80     void setLineJoin(const LineJoin join) { m_lineJoin = (SkPaint::Join)join; }
     81 
     82     float miterLimit() const { return m_miterLimit; }
     83     void setMiterLimit(const float miterLimit) { m_miterLimit = miterLimit; }
     84 
     85     void setLineDash(const DashArray&, const float);
     86 
     87     // Sets everything on the paint except the pattern, gradient and color.
     88     // GraphicsContext::setupShader does that. Returns a float representing the
     89     // effective width of the pen. If a non-zero length is provided, the
     90     // number of dashes/dots on a dashed/dotted line will be adjusted to
     91     // start and end that length with a dash/dot.
     92     float setupPaint(SkPaint*, int length = 0) const;
     93 
     94 private:
     95     StrokeStyle m_style;
     96     float m_thickness;
     97     Color m_color;
     98     RefPtr<Gradient> m_gradient;
     99     RefPtr<Pattern> m_pattern;
    100     SkPaint::Cap m_lineCap;
    101     SkPaint::Join m_lineJoin;
    102     float m_miterLimit;
    103     RefPtr<SkDashPathEffect> m_dash;
    104 };
    105 
    106 } // namespace WebCore
    107 
    108 #endif // StrokeData_h
    109