d858b23b3d3e61af8d10de58bcd20fa0687dd414
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / rendering / RenderMultiColumnSet.h
1 /*
2  * Copyright (C) 2012 Apple Inc.  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  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26
27 #ifndef RenderMultiColumnSet_h
28 #define RenderMultiColumnSet_h
29
30 #include "core/rendering/RenderRegionSet.h"
31 #include "wtf/Vector.h"
32
33 namespace WebCore {
34
35 // RenderMultiColumnSet represents a set of columns that all have the same width and height. By combining runs of same-size columns into a single
36 // object, we significantly reduce the number of unique RenderObjects required to represent columns.
37 //
38 // A simple multi-column block will have exactly one RenderMultiColumnSet child. A simple paginated multi-column block will have three
39 // RenderMultiColumnSet children: one for the content at the bottom of the first page (whose columns will have a shorter height), one
40 // for the 2nd to n-1 pages, and then one last column set that will hold the shorter columns on the final page (that may have to be balanced
41 // as well).
42 //
43 // Column spans result in the creation of new column sets as well, since a spanning region has to be placed in between the column sets that
44 // come before and after the span.
45 class RenderMultiColumnSet FINAL : public RenderRegionSet {
46 public:
47     static RenderMultiColumnSet* createAnonymous(RenderFlowThread*);
48
49     virtual bool isRenderMultiColumnSet() const OVERRIDE { return true; }
50
51     RenderBlockFlow* multiColumnBlockFlow() const { return toRenderBlockFlow(parent()); }
52     RenderMultiColumnFlowThread* multiColumnFlowThread() const { return multiColumnBlockFlow()->multiColumnFlowThread(); }
53     unsigned computedColumnCount() const { return m_computedColumnCount; }
54     LayoutUnit computedColumnWidth() const { return m_computedColumnWidth; }
55     LayoutUnit computedColumnHeight() const { return m_computedColumnHeight; }
56
57     void setComputedColumnWidthAndCount(LayoutUnit width, unsigned count)
58     {
59         m_computedColumnWidth = width;
60         m_computedColumnCount = count;
61     }
62
63     LayoutUnit heightAdjustedForSetOffset(LayoutUnit height) const;
64
65     void updateMinimumColumnHeight(LayoutUnit height) { m_minimumColumnHeight = std::max(height, m_minimumColumnHeight); }
66     LayoutUnit minimumColumnHeight() const { return m_minimumColumnHeight; }
67
68     unsigned forcedBreaksCount() const { return m_contentRuns.size(); }
69     void clearForcedBreaks();
70     void addForcedBreak(LayoutUnit offsetFromFirstPage);
71
72     // (Re-)calculate the column height when contents are supposed to be balanced. If 'initial' is
73     // set, guess an initial column height; otherwise, stretch the column height a tad. Return true
74     // if column height changed and another layout pass is required.
75     bool recalculateBalancedHeight(bool initial);
76
77     // Record space shortage (the amount of space that would have been enough to prevent some
78     // element from being moved to the next column) at a column break. The smallest amount of space
79     // shortage we find is the amount with which we will stretch the column height, if it turns out
80     // after layout that the columns weren't tall enough.
81     void recordSpaceShortage(LayoutUnit spaceShortage);
82
83     virtual void updateLogicalWidth() OVERRIDE;
84
85     void prepareForLayout();
86
87 private:
88     RenderMultiColumnSet(RenderFlowThread*);
89
90     virtual void computeLogicalHeight(LayoutUnit logicalHeight, LayoutUnit logicalTop, LogicalExtentComputedValues&) const OVERRIDE;
91
92     virtual void paintObject(PaintInfo&, const LayoutPoint& paintOffset) OVERRIDE;
93
94     virtual LayoutUnit pageLogicalWidth() const OVERRIDE { return m_computedColumnWidth; }
95     virtual LayoutUnit pageLogicalHeight() const OVERRIDE { return m_computedColumnHeight; }
96
97     virtual LayoutUnit pageLogicalTopForOffset(LayoutUnit offset) const OVERRIDE;
98
99     // FIXME: This will change once we have column sets constrained by enclosing pages, etc.
100     virtual LayoutUnit logicalHeightOfAllFlowThreadContent() const OVERRIDE { return m_computedColumnHeight; }
101
102     virtual void repaintFlowThreadContent(const LayoutRect& repaintRect) const OVERRIDE;
103
104     virtual void collectLayerFragments(LayerFragments&, const LayoutRect& layerBoundingBox, const LayoutRect& dirtyRect) OVERRIDE;
105
106     virtual const char* renderName() const OVERRIDE;
107
108     void paintColumnRules(PaintInfo&, const LayoutPoint& paintOffset);
109
110     LayoutUnit columnGap() const;
111     LayoutRect columnRectAt(unsigned index) const;
112     unsigned columnCount() const;
113
114     LayoutRect flowThreadPortionRectAt(unsigned index) const;
115     LayoutRect flowThreadPortionOverflowRect(const LayoutRect& flowThreadPortion, unsigned index, unsigned colCount, LayoutUnit colGap) const;
116
117     enum ColumnIndexCalculationMode {
118         ClampToExistingColumns, // Stay within the range of already existing columns.
119         AssumeNewColumns // Allow column indices outside the range of already existing columns.
120     };
121     unsigned columnIndexAtOffset(LayoutUnit, ColumnIndexCalculationMode = ClampToExistingColumns) const;
122
123     void setAndConstrainColumnHeight(LayoutUnit);
124
125     // Return the index of the content run with the currently tallest columns, taking all implicit
126     // breaks assumed so far into account.
127     unsigned findRunWithTallestColumns() const;
128
129     // Given the current list of content runs, make assumptions about where we need to insert
130     // implicit breaks (if there's room for any at all; depending on the number of explicit breaks),
131     // and store the results. This is needed in order to balance the columns.
132     void distributeImplicitBreaks();
133
134     LayoutUnit calculateBalancedHeight(bool initial) const;
135
136     unsigned m_computedColumnCount; // Used column count (the resulting 'N' from the pseudo-algorithm in the multicol spec)
137     LayoutUnit m_computedColumnWidth; // Used column width (the resulting 'W' from the pseudo-algorithm in the multicol spec)
138     LayoutUnit m_computedColumnHeight;
139
140     // The following variables are used when balancing the column set.
141     LayoutUnit m_maxColumnHeight; // Maximum column height allowed.
142     LayoutUnit m_minSpaceShortage; // The smallest amout of space shortage that caused a column break.
143     LayoutUnit m_minimumColumnHeight;
144
145     // A run of content without explicit (forced) breaks; i.e. a flow thread portion between two
146     // explicit breaks, between flow thread start and an explicit break, between an explicit break
147     // and flow thread end, or, in cases when there are no explicit breaks at all: between flow flow
148     // thread start and flow thread end. We need to know where the explicit breaks are, in order to
149     // figure out where the implicit breaks will end up, so that we get the columns properly
150     // balanced. A content run starts out as representing one single column, and will represent one
151     // additional column for each implicit break "inserted" there.
152     class ContentRun {
153     public:
154         ContentRun(LayoutUnit breakOffset)
155             : m_breakOffset(breakOffset)
156             , m_assumedImplicitBreaks(0) { }
157
158         unsigned assumedImplicitBreaks() const { return m_assumedImplicitBreaks; }
159         void assumeAnotherImplicitBreak() { m_assumedImplicitBreaks++; }
160         LayoutUnit breakOffset() const { return m_breakOffset; }
161
162         // Return the column height that this content run would require, considering the implicit
163         // breaks assumed so far.
164         LayoutUnit columnLogicalHeight(LayoutUnit startOffset) const { return ceilf((m_breakOffset - startOffset).toFloat() / float(m_assumedImplicitBreaks + 1)); }
165
166     private:
167         LayoutUnit m_breakOffset; // Flow thread offset where this run ends.
168         unsigned m_assumedImplicitBreaks; // Number of implicit breaks in this run assumed so far.
169     };
170     Vector<ContentRun, 1> m_contentRuns;
171 };
172
173 DEFINE_RENDER_OBJECT_TYPE_CASTS(RenderMultiColumnSet, isRenderMultiColumnSet());
174
175 } // namespace WebCore
176
177 #endif // RenderMultiColumnSet_h
178