1926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2012 Adobe Systems Incorporated. All rights reserved.
3926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
4926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Redistribution and use in source and binary forms, with or without
5926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * modification, are permitted provided that the following conditions
6926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * are met:
7926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
8926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 1. Redistributions of source code must retain the above
9926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    copyright notice, this list of conditions and the following
10926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    disclaimer.
11926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * 2. Redistributions in binary form must reproduce the above
12926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    copyright notice, this list of conditions and the following
13926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    disclaimer in the documentation and/or other materials
14926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *    provided with the distribution.
15926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) *
16926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
17926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
20926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
25926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
26926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * SUCH DAMAGE.
28926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) */
29926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
30591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#ifndef ShapeOutsideInfo_h
31591b958dee2cf159d33a0b931e6231072eaf38d5Ben Murdoch#define ShapeOutsideInfo_h
32926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
33a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "core/rendering/shapes/Shape.h"
34a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "core/rendering/style/RenderStyle.h"
35a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "core/rendering/style/ShapeValue.h"
36a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "platform/geometry/FloatRect.h"
371e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/geometry/LayoutSize.h"
38a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch#include "wtf/OwnPtr.h"
39926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
40c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
41926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
42f79f16f17ddc4f842d7b7a38603e280e94be826aTorne (Richard Coles)class RenderBlockFlow;
43926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class RenderBox;
4406f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)class FloatingObject;
45926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
469e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)class ShapeOutsideDeltas FINAL {
479e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)public:
489e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    ShapeOutsideDeltas()
499e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        : m_lineOverlapsShape(false)
509e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_isValid(false)
519e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    {
529e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    }
539e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
549e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    ShapeOutsideDeltas(LayoutUnit leftMarginBoxDelta, LayoutUnit rightMarginBoxDelta, bool lineOverlapsShape, LayoutUnit borderBoxLineTop, LayoutUnit lineHeight)
559e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        : m_leftMarginBoxDelta(leftMarginBoxDelta)
569e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_rightMarginBoxDelta(rightMarginBoxDelta)
579e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_borderBoxLineTop(borderBoxLineTop)
589e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_lineHeight(lineHeight)
599e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_lineOverlapsShape(lineOverlapsShape)
609e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        , m_isValid(true)
619e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    {
629e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    }
639e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
649e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    bool isForLine(LayoutUnit borderBoxLineTop, LayoutUnit lineHeight)
659e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    {
669e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)        return m_isValid && m_borderBoxLineTop == borderBoxLineTop && m_lineHeight == lineHeight;
679e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    }
689e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
699e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    bool isValid() { return m_isValid; }
709e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit leftMarginBoxDelta() { ASSERT(m_isValid); return m_leftMarginBoxDelta; }
719e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit rightMarginBoxDelta() { ASSERT(m_isValid); return m_rightMarginBoxDelta; }
729e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    bool lineOverlapsShape() { ASSERT(m_isValid); return m_lineOverlapsShape; }
739e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
749e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)private:
759e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit m_leftMarginBoxDelta;
769e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit m_rightMarginBoxDelta;
779e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit m_borderBoxLineTop;
789e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    LayoutUnit m_lineHeight;
799e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    bool m_lineOverlapsShape : 1;
809e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    bool m_isValid : 1;
819e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)};
829e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)
83a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdochclass ShapeOutsideInfo FINAL {
84a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    WTF_MAKE_FAST_ALLOCATED;
85926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
86a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    void setReferenceBoxLogicalSize(LayoutSize);
87a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
88a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalTop() const { return computedShape().shapeMarginLogicalBoundingBox().y() + logicalTopOffset(); }
89a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalBottom() const { return computedShape().shapeMarginLogicalBoundingBox().maxY() + logicalTopOffset(); }
90a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalLeft() const { return computedShape().shapeMarginLogicalBoundingBox().x() + logicalLeftOffset(); }
91a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalRight() const { return computedShape().shapeMarginLogicalBoundingBox().maxX() + logicalLeftOffset(); }
92a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalWidth() const { return computedShape().shapeMarginLogicalBoundingBox().width(); }
93a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit shapeLogicalHeight() const { return computedShape().shapeMarginLogicalBoundingBox().height(); }
94a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
95d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static PassOwnPtr<ShapeOutsideInfo> createInfo(const RenderBox& renderer) { return adoptPtr(new ShapeOutsideInfo(renderer)); }
96d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    static bool isEnabledFor(const RenderBox&);
9753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
989e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    ShapeOutsideDeltas computeDeltasForContainingBlockLine(const RenderBlockFlow&, const FloatingObject&, LayoutUnit lineTop, LayoutUnit lineHeight);
9906f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)
100a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    static ShapeOutsideInfo& ensureInfo(const RenderBox& key)
1019bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    {
102a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        InfoMap& infoMap = ShapeOutsideInfo::infoMap();
103a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        if (ShapeOutsideInfo* info = infoMap.get(&key))
104a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch            return *info;
105a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        InfoMap::AddResult result = infoMap.add(&key, ShapeOutsideInfo::createInfo(key));
106a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        return *result.storedValue->value;
1079bbd2f5e390b01907d97ecffde80aa1b06113aacTorne (Richard Coles)    }
108a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    static void removeInfo(const RenderBox& key) { infoMap().remove(&key); }
109a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    static ShapeOutsideInfo* info(const RenderBox& key) { return infoMap().get(&key); }
11053e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)
111a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    void markShapeAsDirty() { m_shape.clear(); }
112a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    bool isShapeDirty() { return !m_shape.get(); }
113a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutSize shapeSize() const { return m_referenceBoxLogicalSize; }
114f542a0e9557608d421c9d165487573efa35a5f91Ben Murdoch    bool isComputingShape() const { return m_isComputingShape; }
115d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)
116323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    LayoutRect computedShapePhysicalBoundingBox() const;
117323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    FloatPoint shapeToRendererPoint(FloatPoint) const;
118323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    FloatSize shapeToRendererSize(FloatSize) const;
119323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)    const Shape& computedShape() const;
120323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)
121323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)protected:
122d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    ShapeOutsideInfo(const RenderBox& renderer)
123a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        : m_renderer(renderer)
124f542a0e9557608d421c9d165487573efa35a5f91Ben Murdoch        , m_isComputingShape(false)
125d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    { }
126926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
127323480423219ecd77329f8326dc5e0e3b50926d4Torne (Richard Coles)private:
128f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)    PassOwnPtr<Shape> createShapeForImage(StyleImage*, float shapeImageThreshold, WritingMode, float margin) const;
129f6b7aed3f7ce69aca0d7a032d144cbd088b04393Torne (Richard Coles)
130a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit logicalTopOffset() const;
131a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutUnit logicalLeftOffset() const;
132a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
133a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    typedef HashMap<const RenderBox*, OwnPtr<ShapeOutsideInfo> > InfoMap;
134a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    static InfoMap& infoMap()
135a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    {
136a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        DEFINE_STATIC_LOCAL(InfoMap, staticInfoMap, ());
137a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch        return staticInfoMap;
138a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    }
139a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch
140a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    const RenderBox& m_renderer;
141a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    mutable OwnPtr<Shape> m_shape;
142a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    LayoutSize m_referenceBoxLogicalSize;
1439e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)    ShapeOutsideDeltas m_shapeOutsideDeltas;
144f542a0e9557608d421c9d165487573efa35a5f91Ben Murdoch    mutable bool m_isComputingShape;
145926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
146926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
147926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)}
148926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)#endif
149