1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
/*
* Copyright (C) 2013 Adobe Systems Incorporated. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "RasterShape.h"
#include <wtf/MathExtras.h>
namespace WebCore {
class MarginIntervalGenerator {
public:
MarginIntervalGenerator(unsigned radius);
void set(int y, const IntShapeInterval&);
IntShapeInterval intervalAt(int y) const;
private:
Vector<int> m_xIntercepts;
int m_y;
int m_x1;
int m_x2;
};
MarginIntervalGenerator::MarginIntervalGenerator(unsigned radius)
: m_y(0)
, m_x1(0)
, m_x2(0)
{
m_xIntercepts.resize(radius + 1);
unsigned radiusSquared = radius * radius;
for (unsigned y = 0; y <= radius; y++)
m_xIntercepts[y] = sqrt(static_cast<double>(radiusSquared - y * y));
}
void MarginIntervalGenerator::set(int y, const IntShapeInterval& interval)
{
ASSERT(y >= 0 && interval.x1() >= 0);
m_y = y;
m_x1 = interval.x1();
m_x2 = interval.x2();
}
IntShapeInterval MarginIntervalGenerator::intervalAt(int y) const
{
unsigned xInterceptsIndex = abs(y - m_y);
int dx = (xInterceptsIndex >= m_xIntercepts.size()) ? 0 : m_xIntercepts[xInterceptsIndex];
return IntShapeInterval(m_x1 - dx, m_x2 + dx);
}
void RasterShapeIntervals::appendInterval(int y, int x1, int x2)
{
ASSERT(x2 > x1 && (intervalsAt(y).isEmpty() || x1 > intervalsAt(y).last().x2()));
m_bounds.unite(IntRect(x1, y, x2 - x1, 1));
intervalsAt(y).append(IntShapeInterval(x1, x2));
}
void RasterShapeIntervals::uniteMarginInterval(int y, const IntShapeInterval& interval)
{
ASSERT(intervalsAt(y).size() <= 1); // Each m_intervalLists entry has 0 or one interval.
if (intervalsAt(y).isEmpty())
intervalsAt(y).append(interval);
else {
IntShapeInterval& resultInterval = intervalsAt(y)[0];
resultInterval.set(std::min(resultInterval.x1(), interval.x1()), std::max(resultInterval.x2(), interval.x2()));
}
m_bounds.unite(IntRect(interval.x1(), y, interval.width(), 1));
}
static inline bool shapeIntervalsContain(const IntShapeIntervals& intervals, const IntShapeInterval& interval)
{
for (unsigned i = 0; i < intervals.size(); i++) {
if (intervals[i].x1() > interval.x2())
return false;
if (intervals[i].contains(interval))
return true;
}
return false;
}
bool RasterShapeIntervals::contains(const IntRect& rect) const
{
if (!bounds().contains(rect))
return false;
const IntShapeInterval& rectInterval = IntShapeInterval(rect.x(), rect.maxX());
for (int y = rect.y(); y < rect.maxY(); y++) {
if (!shapeIntervalsContain(intervalsAt(y), rectInterval))
return false;
}
return true;
}
static inline void appendX1Values(const IntShapeIntervals& intervals, int minIntervalWidth, Vector<int>& result)
{
for (unsigned i = 0; i < intervals.size(); i++)
if (intervals[i].width() >= minIntervalWidth)
result.append(intervals[i].x1());
}
bool RasterShapeIntervals::getIntervalX1Values(int y1, int y2, int minIntervalWidth, Vector<int>& result) const
{
ASSERT(y1 >= 0 && y2 > y1);
for (int y = y1; y < y2; y++) {
if (intervalsAt(y).isEmpty())
return false;
}
appendX1Values(intervalsAt(y1), minIntervalWidth, result);
for (int y = y1 + 1; y < y2; y++) {
if (intervalsAt(y) != intervalsAt(y - 1))
appendX1Values(intervalsAt(y), minIntervalWidth, result);
}
return true;
}
bool RasterShapeIntervals::firstIncludedIntervalY(int minY, const IntSize& minSize, LayoutUnit& result) const
{
minY = std::max<int>(bounds().y(), minY);
ASSERT(minY >= 0 && minY < size());
if (minSize.isEmpty() || minSize.width() > bounds().width())
return false;
for (int lineY = minY; lineY <= bounds().maxY() - minSize.height(); lineY++) {
Vector<int> intervalX1Values;
if (!getIntervalX1Values(lineY, lineY + minSize.height(), minSize.width(), intervalX1Values))
continue;
std::sort(intervalX1Values.begin(), intervalX1Values.end());
IntRect firstFitRect(IntPoint(0, 0), minSize);
for (unsigned i = 0; i < intervalX1Values.size(); i++) {
int lineX = intervalX1Values[i];
if (i > 0 && lineX == intervalX1Values[i - 1])
continue;
firstFitRect.setLocation(IntPoint(lineX, lineY));
if (contains(firstFitRect)) {
result = lineY;
return true;
}
}
}
return false;
}
void RasterShapeIntervals::getIncludedIntervals(int y1, int y2, IntShapeIntervals& result) const
{
ASSERT(y2 >= y1);
if (y1 < bounds().y() || y2 > bounds().maxY())
return;
for (int y = y1; y < y2; y++) {
if (intervalsAt(y).isEmpty())
return;
}
result = intervalsAt(y1);
for (int y = y1 + 1; y < y2 && !result.isEmpty(); y++) {
IntShapeIntervals intervals;
IntShapeInterval::intersectShapeIntervals(result, intervalsAt(y), intervals);
result.swap(intervals);
}
}
void RasterShapeIntervals::getExcludedIntervals(int y1, int y2, IntShapeIntervals& result) const
{
ASSERT(y2 >= y1);
if (y2 < bounds().y() || y1 >= bounds().maxY())
return;
y1 = std::max(y1, bounds().y());
y2 = std::min(y2, bounds().maxY());
result = intervalsAt(y1);
for (int y = y1 + 1; y < y2; y++) {
IntShapeIntervals intervals;
IntShapeInterval::uniteShapeIntervals(result, intervalsAt(y), intervals);
result.swap(intervals);
}
}
// Currently limited to computing the margin boundary for shape-outside for floats, see https://bugs.webkit.org/show_bug.cgi?id=116348.
PassOwnPtr<RasterShapeIntervals> RasterShapeIntervals::computeShapeMarginIntervals(unsigned shapeMargin) const
{
OwnPtr<RasterShapeIntervals> result = adoptPtr(new RasterShapeIntervals(size(), shapeMargin));
MarginIntervalGenerator marginIntervalGenerator(shapeMargin);
int minY = bounds().y();
int maxY = bounds().maxY();
for (int y = minY; y < maxY; ++y) {
const IntShapeInterval& intervalAtY = limitIntervalAt(y);
if (intervalAtY.isEmpty())
continue;
marginIntervalGenerator.set(y, intervalAtY);
int marginY0 = y - clampToInteger(shapeMargin);
int marginY1 = y + clampToInteger(shapeMargin);
for (int marginY = y - 1; marginY >= marginY0; --marginY) {
if (marginY > minY && limitIntervalAt(marginY).contains(intervalAtY))
break;
result->uniteMarginInterval(marginY, marginIntervalGenerator.intervalAt(marginY));
}
result->uniteMarginInterval(y, marginIntervalGenerator.intervalAt(y));
for (int marginY = y + 1; marginY <= marginY1; ++marginY) {
if (marginY < maxY && limitIntervalAt(marginY).contains(intervalAtY))
break;
result->uniteMarginInterval(marginY, marginIntervalGenerator.intervalAt(marginY));
}
}
return result.release();
}
void RasterShapeIntervals::buildBoundsPath(Path& path) const
{
for (int y = bounds().y(); y < bounds().maxY(); y++) {
if (intervalsAt(y).isEmpty())
continue;
IntShapeInterval extent = limitIntervalAt(y);
int endY = y + 1;
for (; endY < bounds().maxY(); endY++) {
if (intervalsAt(endY).isEmpty() || limitIntervalAt(endY) != extent)
break;
}
path.addRect(FloatRect(extent.x1(), y, extent.width(), endY - y));
y = endY - 1;
}
}
const RasterShapeIntervals& RasterShape::marginIntervals() const
{
ASSERT(shapeMargin() >= 0);
if (!shapeMargin())
return *m_intervals;
unsigned marginBoundaryRadius = std::min(clampToUnsigned(ceil(shapeMargin())), std::max<unsigned>(m_imageSize.width(), m_imageSize.height()));
if (!m_marginIntervals)
m_marginIntervals = m_intervals->computeShapeMarginIntervals(marginBoundaryRadius);
return *m_marginIntervals;
}
const RasterShapeIntervals& RasterShape::paddingIntervals() const
{
ASSERT(shapePadding() >= 0);
if (!shapePadding())
return *m_intervals;
// FIXME: Add support for non-zero padding, see https://bugs.webkit.org/show_bug.cgi?id=116348.
return *m_intervals;
}
static inline void appendLineSegments(const IntShapeIntervals& intervals, SegmentList& result)
{
for (unsigned i = 0; i < intervals.size(); i++)
result.append(LineSegment(intervals[i].x1(), intervals[i].x2() + 1));
}
void RasterShape::getExcludedIntervals(LayoutUnit logicalTop, LayoutUnit logicalHeight, SegmentList& result) const
{
const RasterShapeIntervals& intervals = marginIntervals();
if (intervals.isEmpty())
return;
IntShapeIntervals excludedIntervals;
intervals.getExcludedIntervals(logicalTop, logicalTop + logicalHeight, excludedIntervals);
appendLineSegments(excludedIntervals, result);
}
void RasterShape::getIncludedIntervals(LayoutUnit logicalTop, LayoutUnit logicalHeight, SegmentList& result) const
{
const RasterShapeIntervals& intervals = paddingIntervals();
if (intervals.isEmpty())
return;
IntShapeIntervals includedIntervals;
intervals.getIncludedIntervals(logicalTop, logicalTop + logicalHeight, includedIntervals);
appendLineSegments(includedIntervals, result);
}
bool RasterShape::firstIncludedIntervalLogicalTop(LayoutUnit minLogicalIntervalTop, const FloatSize& minLogicalIntervalSize, LayoutUnit& result) const
{
const RasterShapeIntervals& intervals = paddingIntervals();
if (intervals.isEmpty())
return false;
return intervals.firstIncludedIntervalY(minLogicalIntervalTop.floor(), flooredIntSize(minLogicalIntervalSize), result);
}
} // namespace WebCore
|