Replace WTF::move with WTFMove
[WebKit-https.git] / Source / WebCore / rendering / style / NinePieceImage.cpp
1 /*
2  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
3  *           (C) 2000 Antti Koivisto (koivisto@kde.org)
4  *           (C) 2000 Dirk Mueller (mueller@kde.org)
5  * Copyright (C) 2003, 2005, 2006, 2007, 2008, 2013, 2015 Apple Inc. All rights reserved.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public License
18  * along with this library; see the file COPYING.LIB.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  *
22  */
23
24 #include "config.h"
25 #include "NinePieceImage.h"
26
27 #include "GraphicsContext.h"
28 #include "LengthFunctions.h"
29 #include "RenderStyle.h"
30 #include <wtf/NeverDestroyed.h>
31 #include <wtf/PointerComparison.h>
32
33 namespace WebCore {
34
35 static DataRef<NinePieceImageData>& defaultData()
36 {
37     static NeverDestroyed<DataRef<NinePieceImageData>> data(NinePieceImageData::create());
38     return data.get();
39 }
40
41 NinePieceImage::NinePieceImage()
42     : m_data(defaultData())
43 {
44 }
45
46 NinePieceImage::NinePieceImage(PassRefPtr<StyleImage> image, LengthBox imageSlices, bool fill, LengthBox borderSlices, LengthBox outset, ENinePieceImageRule horizontalRule, ENinePieceImageRule verticalRule)
47     : m_data(NinePieceImageData::create())
48 {
49     m_data.access()->image = image;
50     m_data.access()->imageSlices = WTFMove(imageSlices);
51     m_data.access()->borderSlices = WTFMove(borderSlices);
52     m_data.access()->outset = WTFMove(outset);
53     m_data.access()->fill = fill;
54     m_data.access()->horizontalRule = horizontalRule;
55     m_data.access()->verticalRule = verticalRule;
56 }
57
58 LayoutUnit NinePieceImage::computeSlice(Length length, LayoutUnit width, LayoutUnit slice, LayoutUnit extent)
59 {
60     if (length.isRelative())
61         return length.value() * width;
62     if (length.isAuto())
63         return slice;
64     return valueForLength(length, extent);
65 }
66
67 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, int scaleFactor)
68 {
69     LayoutUnit top    = std::min<LayoutUnit>(size.height(), valueForLength(lengths.top(),    size.height())) * scaleFactor;
70     LayoutUnit right  = std::min<LayoutUnit>(size.width(),  valueForLength(lengths.right(),  size.width()))  * scaleFactor;
71     LayoutUnit bottom = std::min<LayoutUnit>(size.height(), valueForLength(lengths.bottom(), size.height())) * scaleFactor;
72     LayoutUnit left   = std::min<LayoutUnit>(size.width(),  valueForLength(lengths.left(),   size.width()))  * scaleFactor;
73     return LayoutBoxExtent(top, right, bottom, left);
74 }
75
76 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, const FloatBoxExtent& widths, const LayoutBoxExtent& slices)
77 {
78     LayoutUnit top    = computeSlice(lengths.top(),    widths.top(),    slices.top(),    size.height());
79     LayoutUnit right  = computeSlice(lengths.right(),  widths.right(),  slices.right(),  size.width());
80     LayoutUnit bottom = computeSlice(lengths.bottom(), widths.bottom(), slices.bottom(), size.height());
81     LayoutUnit left   = computeSlice(lengths.left(),   widths.left(),   slices.left(),   size.width());
82     return LayoutBoxExtent(top, right, bottom, left);
83 }
84
85 void NinePieceImage::scaleSlicesIfNeeded(const LayoutSize& size, LayoutBoxExtent& slices, float deviceScaleFactor)
86 {
87     LayoutUnit width  = std::max<LayoutUnit>(1 / deviceScaleFactor, slices.left() + slices.right());
88     LayoutUnit height = std::max<LayoutUnit>(1 / deviceScaleFactor, slices.top() + slices.bottom());
89
90     float sliceScaleFactor = std::min((float)size.width() / width, (float)size.height() / height);
91
92     if (sliceScaleFactor >= 1)
93         return;
94
95     // All slices are reduced by multiplying them by sliceScaleFactor.
96     slices.top()    *= sliceScaleFactor;
97     slices.right()  *= sliceScaleFactor;
98     slices.bottom() *= sliceScaleFactor;
99     slices.left()   *= sliceScaleFactor;
100 }
101
102 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const LayoutBoxExtent& slices)
103 {
104     if (piece == MiddlePiece)
105         return false;
106
107     PhysicalBoxSide horizontalSide = imagePieceHorizontalSide(piece);
108     PhysicalBoxSide verticalSide = imagePieceVerticalSide(piece);
109     return !((horizontalSide == NilSide || slices.at(horizontalSide)) && (verticalSide == NilSide || slices.at(verticalSide)));
110 }
111
112 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
113 {
114     return destinationRects[piece].isEmpty() || sourceRects[piece].isEmpty();
115 }
116
117 Vector<FloatRect> NinePieceImage::computeNineRects(const FloatRect& outer, const LayoutBoxExtent& slices, float deviceScaleFactor)
118 {
119     FloatRect inner = outer;
120     inner.move(slices.left(), slices.top());
121     inner.contract(slices.left() + slices.right(), slices.top() + slices.bottom());
122     ASSERT(outer.contains(inner));
123
124     Vector<FloatRect> rects(MaxPiece);
125
126     rects[TopLeftPiece]     = snapRectToDevicePixels(outer.x(),    outer.y(),    slices.left(),  slices.top(),    deviceScaleFactor);
127     rects[BottomLeftPiece]  = snapRectToDevicePixels(outer.x(),    inner.maxY(), slices.left(),  slices.bottom(), deviceScaleFactor);
128     rects[LeftPiece]        = snapRectToDevicePixels(outer.x(),    inner.y(),    slices.left(),  inner.height(),  deviceScaleFactor);
129
130     rects[TopRightPiece]    = snapRectToDevicePixels(inner.maxX(), outer.y(),    slices.right(), slices.top(),    deviceScaleFactor);
131     rects[BottomRightPiece] = snapRectToDevicePixels(inner.maxX(), inner.maxY(), slices.right(), slices.bottom(), deviceScaleFactor);
132     rects[RightPiece]       = snapRectToDevicePixels(inner.maxX(), inner.y(),    slices.right(), inner.height(),  deviceScaleFactor);
133
134     rects[TopPiece]         = snapRectToDevicePixels(inner.x(),    outer.y(),    inner.width(),  slices.top(),    deviceScaleFactor);
135     rects[BottomPiece]      = snapRectToDevicePixels(inner.x(),    inner.maxY(), inner.width(),  slices.bottom(), deviceScaleFactor);
136
137     rects[MiddlePiece]      = snapRectToDevicePixels(inner.x(),    inner.y(),    inner.width(),  inner.height(),  deviceScaleFactor);
138     return rects;
139 }
140
141 FloatSize NinePieceImage::computeSideTileScale(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
142 {
143     ASSERT(!isCornerPiece(piece) && !isMiddlePiece(piece));
144     if (isEmptyPieceRect(piece, destinationRects, sourceRects))
145         return FloatSize(1, 1);
146
147     float scale;
148     if (isHorizontalPiece(piece))
149         scale = destinationRects[piece].height() / sourceRects[piece].height();
150     else
151         scale = destinationRects[piece].width() / sourceRects[piece].width();
152
153     return FloatSize(scale, scale);
154 }
155
156 FloatSize NinePieceImage::computeMiddleTileScale(const Vector<FloatSize>& scales, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
157 {
158     FloatSize scale(1, 1);
159     if (isEmptyPieceRect(MiddlePiece, destinationRects, sourceRects))
160         return scale;
161
162     // Unlike the side pieces, the middle piece can have "stretch" specified in one axis but not the other.
163     // In fact the side pieces don't even use the scale factor unless they have a rule other than "stretch".
164     if (hRule == StretchImageRule)
165         scale.setWidth(destinationRects[MiddlePiece].width() / sourceRects[MiddlePiece].width());
166     else if (!isEmptyPieceRect(TopPiece, destinationRects, sourceRects))
167         scale.setWidth(scales[TopPiece].width());
168     else if (!isEmptyPieceRect(BottomPiece, destinationRects, sourceRects))
169         scale.setWidth(scales[BottomPiece].width());
170
171     if (vRule == StretchImageRule)
172         scale.setHeight(destinationRects[MiddlePiece].height() / sourceRects[MiddlePiece].height());
173     else if (!isEmptyPieceRect(LeftPiece, destinationRects, sourceRects))
174         scale.setHeight(scales[LeftPiece].height());
175     else if (!isEmptyPieceRect(RightPiece, destinationRects, sourceRects))
176         scale.setHeight(scales[RightPiece].height());
177
178     return scale;
179 }
180
181 Vector<FloatSize> NinePieceImage::computeTileScales(const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
182 {
183     Vector<FloatSize> scales(MaxPiece, FloatSize(1, 1));
184
185     scales[TopPiece]    = computeSideTileScale(TopPiece,    destinationRects, sourceRects);
186     scales[RightPiece]  = computeSideTileScale(RightPiece,  destinationRects, sourceRects);
187     scales[BottomPiece] = computeSideTileScale(BottomPiece, destinationRects, sourceRects);
188     scales[LeftPiece]   = computeSideTileScale(LeftPiece,   destinationRects, sourceRects);
189
190     scales[MiddlePiece] = computeMiddleTileScale(scales, destinationRects, sourceRects, hRule, vRule);
191     return scales;
192 }
193
194 void NinePieceImage::paint(GraphicsContext& graphicsContext, RenderElement* renderer, const RenderStyle& style, const LayoutRect& destination, const LayoutSize& source, float deviceScaleFactor, CompositeOperator op) const
195 {
196     StyleImage* styleImage = image();
197     ASSERT(styleImage && styleImage->isLoaded());
198
199     LayoutBoxExtent sourceSlices = computeSlices(source, imageSlices(), styleImage->imageScaleFactor());
200     LayoutBoxExtent destinationSlices = computeSlices(destination.size(), borderSlices(), style.borderWidth(), sourceSlices);
201
202     scaleSlicesIfNeeded(destination.size(), destinationSlices, deviceScaleFactor);
203
204     Vector<FloatRect> destinationRects = computeNineRects(destination, destinationSlices, deviceScaleFactor);
205     Vector<FloatRect> sourceRects = computeNineRects(FloatRect(FloatPoint(), source), sourceSlices, deviceScaleFactor);
206     Vector<FloatSize> tileScales = computeTileScales(destinationRects, sourceRects, horizontalRule(), verticalRule());
207
208     RefPtr<Image> image = styleImage->image(renderer, source);
209     if (!image)
210         return;
211
212     for (ImagePiece piece = MinPiece; piece < MaxPiece; ++piece) {
213         if ((piece == MiddlePiece && !fill()) || isEmptyPieceRect(piece, destinationRects, sourceRects))
214             continue;
215
216         if (isCornerPiece(piece)) {
217             graphicsContext.drawImage(*image, destinationRects[piece], sourceRects[piece], op);
218             continue;
219         }
220
221         Image::TileRule hRule = isHorizontalPiece(piece) ? static_cast<Image::TileRule>(horizontalRule()) : Image::StretchTile;
222         Image::TileRule vRule = isVerticalPiece(piece) ? static_cast<Image::TileRule>(verticalRule()) : Image::StretchTile;
223         graphicsContext.drawTiledImage(*image, destinationRects[piece], sourceRects[piece], tileScales[piece], hRule, vRule, op);
224     }
225 }
226
227 NinePieceImageData::NinePieceImageData()
228     : fill(false)
229     , horizontalRule(StretchImageRule)
230     , verticalRule(StretchImageRule)
231     , image(nullptr)
232     , imageSlices(Length(100, Percent), Length(100, Percent), Length(100, Percent), Length(100, Percent))
233     , borderSlices(Length(1, Relative), Length(1, Relative), Length(1, Relative), Length(1, Relative))
234     , outset(0)
235 {
236 }
237
238 inline NinePieceImageData::NinePieceImageData(const NinePieceImageData& other)
239     : RefCounted<NinePieceImageData>()
240     , fill(other.fill)
241     , horizontalRule(other.horizontalRule)
242     , verticalRule(other.verticalRule)
243     , image(other.image)
244     , imageSlices(other.imageSlices)
245     , borderSlices(other.borderSlices)
246     , outset(other.outset)
247 {
248 }
249
250 Ref<NinePieceImageData> NinePieceImageData::copy() const
251 {
252     return adoptRef(*new NinePieceImageData(*this));
253 }
254
255 bool NinePieceImageData::operator==(const NinePieceImageData& other) const
256 {
257     return arePointingToEqualData(image, other.image)
258         && imageSlices == other.imageSlices
259         && fill == other.fill
260         && borderSlices == other.borderSlices
261         && outset == other.outset
262         && horizontalRule == other.horizontalRule
263         && verticalRule == other.verticalRule;
264 }
265
266 }