Home | History | Annotate | Download | only in shapes
      1 /*
      2 * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
      3 *
      4 * Redistribution and use in source and binary forms, with or without
      5 * modification, are permitted provided that the following conditions
      6 * are met:
      7 *
      8 * 1. Redistributions of source code must retain the above
      9 *    copyright notice, this list of conditions and the following
     10 *    disclaimer.
     11 * 2. Redistributions in binary form must reproduce the above
     12 *    copyright notice, this list of conditions and the following
     13 *    disclaimer in the documentation and/or other materials
     14 *    provided with the distribution.
     15 *
     16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
     17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
     20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
     21 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
     25 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
     26 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27 * SUCH DAMAGE.
     28 */
     29 
     30 #ifndef ShapeInfo_h
     31 #define ShapeInfo_h
     32 
     33 #include "core/platform/LayoutUnit.h"
     34 #include "core/platform/graphics/FloatRect.h"
     35 #include "core/rendering/shapes/Shape.h"
     36 #include "core/rendering/style/RenderStyle.h"
     37 #include "core/rendering/style/ShapeValue.h"
     38 #include "wtf/OwnPtr.h"
     39 #include "wtf/Vector.h"
     40 
     41 namespace WebCore {
     42 
     43 template<class KeyType, class InfoType>
     44 class MappedInfo {
     45 public:
     46     static InfoType* ensureInfo(const KeyType* key)
     47     {
     48         InfoMap& infoMap = MappedInfo<KeyType, InfoType>::infoMap();
     49         if (InfoType* info = infoMap.get(key))
     50             return info;
     51         typename InfoMap::AddResult result = infoMap.add(key, InfoType::createInfo(key));
     52         return result.iterator->value.get();
     53     }
     54     static void removeInfo(const KeyType* key) { infoMap().remove(key); }
     55     static InfoType* info(const KeyType* key) { return infoMap().get(key); }
     56 private:
     57     typedef HashMap<const KeyType*, OwnPtr<InfoType> > InfoMap;
     58     static InfoMap& infoMap()
     59     {
     60         DEFINE_STATIC_LOCAL(InfoMap, staticInfoMap, ());
     61         return staticInfoMap;
     62     }
     63 };
     64 
     65 template<class RenderType, ShapeValue* (RenderStyle::*shapeGetter)() const, void (Shape::*intervalGetter)(LayoutUnit, LayoutUnit, SegmentList&) const>
     66 class ShapeInfo {
     67     WTF_MAKE_FAST_ALLOCATED;
     68 public:
     69     virtual ~ShapeInfo() { }
     70 
     71     void setShapeSize(LayoutUnit logicalWidth, LayoutUnit logicalHeight)
     72     {
     73         if (m_renderer->style()->boxSizing() == CONTENT_BOX) {
     74             logicalWidth -= m_renderer->borderAndPaddingLogicalWidth();
     75             logicalHeight -= m_renderer->borderAndPaddingLogicalHeight();
     76         }
     77 
     78         if (m_shapeLogicalWidth == logicalWidth && m_shapeLogicalHeight == logicalHeight)
     79             return;
     80         dirtyShapeSize();
     81         m_shapeLogicalWidth = logicalWidth;
     82         m_shapeLogicalHeight = logicalHeight;
     83     }
     84 
     85     virtual bool computeSegmentsForLine(LayoutUnit lineTop, LayoutUnit lineHeight);
     86     void clearSegments() { m_segments.clear(); }
     87 
     88     LayoutUnit shapeLogicalTop() const { return computedShapeLogicalBoundingBox().y() + logicalTopOffset(); }
     89     LayoutUnit shapeLogicalBottom() const { return computedShapeLogicalBoundingBox().maxY() + logicalTopOffset(); }
     90     LayoutUnit shapeLogicalLeft() const { return computedShapeLogicalBoundingBox().x() + logicalLeftOffset(); }
     91     LayoutUnit shapeLogicalRight() const { return computedShapeLogicalBoundingBox().maxX() + logicalLeftOffset(); }
     92     LayoutUnit shapeLogicalWidth() const { return computedShapeLogicalBoundingBox().width(); }
     93     LayoutUnit shapeLogicalHeight() const { return computedShapeLogicalBoundingBox().height(); }
     94 
     95     LayoutUnit logicalLineTop() const { return m_shapeLineTop + logicalTopOffset(); }
     96     LayoutUnit logicalLineBottom() const { return m_shapeLineTop + m_lineHeight + logicalTopOffset(); }
     97 
     98     LayoutUnit shapeContainingBlockHeight() const { return (m_renderer->style()->boxSizing() == CONTENT_BOX) ? (m_shapeLogicalHeight + m_renderer->borderAndPaddingLogicalHeight()) : m_shapeLogicalHeight; }
     99 
    100     bool lineOverlapsShapeBounds() const { return logicalLineTop() < shapeLogicalBottom() && shapeLogicalTop() <= logicalLineBottom(); }
    101 
    102     void dirtyShapeSize() { m_shape.clear(); }
    103     bool shapeSizeDirty() { return !m_shape.get(); }
    104     const RenderType* owner() const { return m_renderer; }
    105 
    106 protected:
    107     ShapeInfo(const RenderType* renderer): m_renderer(renderer) { }
    108 
    109     const Shape* computedShape() const;
    110     virtual LayoutRect computedShapeLogicalBoundingBox() const = 0;
    111 
    112     LayoutUnit logicalTopOffset() const { return m_renderer->style()->boxSizing() == CONTENT_BOX ? m_renderer->borderAndPaddingBefore() : LayoutUnit(); }
    113     LayoutUnit logicalLeftOffset() const { return (m_renderer->style()->boxSizing() == CONTENT_BOX && !m_renderer->isRenderRegion()) ? m_renderer->borderAndPaddingStart() : LayoutUnit(); }
    114 
    115     LayoutUnit m_shapeLineTop;
    116     LayoutUnit m_lineHeight;
    117     SegmentList m_segments;
    118 
    119     const RenderType* m_renderer;
    120 
    121 private:
    122     mutable OwnPtr<Shape> m_shape;
    123 
    124     LayoutUnit m_shapeLogicalWidth;
    125     LayoutUnit m_shapeLogicalHeight;
    126 };
    127 }
    128 #endif
    129