An SVG with no intrinsic size does not draw correct slices when used as a border...
[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
32 namespace WebCore {
33
34 static DataRef<NinePieceImageData>& defaultData()
35 {
36     static NeverDestroyed<DataRef<NinePieceImageData>> data(NinePieceImageData::create());
37     return data.get();
38 }
39
40 NinePieceImage::NinePieceImage()
41     : m_data(defaultData())
42 {
43 }
44
45 NinePieceImage::NinePieceImage(PassRefPtr<StyleImage> image, LengthBox imageSlices, bool fill, LengthBox borderSlices, LengthBox outset, ENinePieceImageRule horizontalRule, ENinePieceImageRule verticalRule)
46     : m_data(NinePieceImageData::create())
47 {
48     m_data.access()->image = image;
49     m_data.access()->imageSlices = WTF::move(imageSlices);
50     m_data.access()->borderSlices = WTF::move(borderSlices);
51     m_data.access()->outset = WTF::move(outset);
52     m_data.access()->fill = fill;
53     m_data.access()->horizontalRule = horizontalRule;
54     m_data.access()->verticalRule = verticalRule;
55 }
56
57 LayoutUnit NinePieceImage::computeSlice(Length length, LayoutUnit width, LayoutUnit slice, LayoutUnit extent)
58 {
59     if (length.isRelative())
60         return length.value() * width;
61     if (length.isAuto())
62         return slice;
63     return valueForLength(length, extent);
64 }
65
66 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, int scaleFactor)
67 {
68     LayoutUnit top    = std::min<LayoutUnit>(size.height(), valueForLength(lengths.top(),    size.height())) * scaleFactor;
69     LayoutUnit right  = std::min<LayoutUnit>(size.width(),  valueForLength(lengths.right(),  size.width()))  * scaleFactor;
70     LayoutUnit bottom = std::min<LayoutUnit>(size.height(), valueForLength(lengths.bottom(), size.height())) * scaleFactor;
71     LayoutUnit left   = std::min<LayoutUnit>(size.width(),  valueForLength(lengths.left(),   size.width()))  * scaleFactor;
72     return LayoutBoxExtent(top, right, bottom, left);
73 }
74
75 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, const FloatBoxExtent& widths, const LayoutBoxExtent& slices)
76 {
77     LayoutUnit top    = computeSlice(lengths.top(),    widths.top(),    slices.top(),    size.height());
78     LayoutUnit right  = computeSlice(lengths.right(),  widths.right(),  slices.right(),  size.width());
79     LayoutUnit bottom = computeSlice(lengths.bottom(), widths.bottom(), slices.bottom(), size.height());
80     LayoutUnit left   = computeSlice(lengths.left(),   widths.left(),   slices.left(),   size.width());
81     return LayoutBoxExtent(top, right, bottom, left);
82 }
83
84 void NinePieceImage::scaleSlicesIfNeeded(const LayoutSize& size, LayoutBoxExtent& slices, int scaleFactor)
85 {
86     LayoutUnit width  = std::max<LayoutUnit>(1 / scaleFactor, slices.left() + slices.right());
87     LayoutUnit height = std::max<LayoutUnit>(1 / scaleFactor, slices.top() + slices.bottom());
88
89     float sliceScaleFactor = std::min((float)size.width() / width, (float)size.height() / height);
90
91     if (sliceScaleFactor >= 1)
92         return;
93
94     // All slices are reduced by multiplying them by sliceScaleFactor.
95     slices.top()    *= sliceScaleFactor;
96     slices.right()  *= sliceScaleFactor;
97     slices.bottom() *= sliceScaleFactor;
98     slices.left()   *= sliceScaleFactor;
99 }
100
101 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const LayoutBoxExtent& slices)
102 {
103     if (piece == MiddlePiece)
104         return false;
105
106     PhysicalBoxSide horizontalSide = imagePieceHorizontalSide(piece);
107     PhysicalBoxSide verticalSide = imagePieceVerticalSide(piece);
108     return !((horizontalSide == NilSide || slices.at(horizontalSide)) && (verticalSide == NilSide || slices.at(verticalSide)));
109 }
110
111 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
112 {
113     return destinationRects[piece].isEmpty() || sourceRects[piece].isEmpty();
114 }
115
116 Vector<FloatRect> NinePieceImage::computeIntrinsicRects(const FloatRect& outer, const LayoutBoxExtent& slices, float deviceScaleFactor)
117 {
118     FloatRect inner = outer;
119     inner.move(slices.left(), slices.top());
120     inner.contract(slices.left() + slices.right(), slices.top() + slices.bottom());
121     ASSERT(outer.contains(inner));
122
123     Vector<FloatRect> rects(MaxPiece);
124
125     rects[TopLeftPiece]     = snapRectToDevicePixels(outer.x(),    outer.y(),    slices.left(),  slices.top(),    deviceScaleFactor);
126     rects[BottomLeftPiece]  = snapRectToDevicePixels(outer.x(),    inner.maxY(), slices.left(),  slices.bottom(), deviceScaleFactor);
127     rects[LeftPiece]        = snapRectToDevicePixels(outer.x(),    inner.y(),    slices.left(),  inner.height(),  deviceScaleFactor);
128
129     rects[TopRightPiece]    = snapRectToDevicePixels(inner.maxX(), outer.y(),    slices.right(), slices.top(),    deviceScaleFactor);
130     rects[BottomRightPiece] = snapRectToDevicePixels(inner.maxX(), inner.maxY(), slices.right(), slices.bottom(), deviceScaleFactor);
131     rects[RightPiece]       = snapRectToDevicePixels(inner.maxX(), inner.y(),    slices.right(), inner.height(),  deviceScaleFactor);
132
133     rects[TopPiece]         = snapRectToDevicePixels(inner.x(),    outer.y(),    inner.width(),  slices.top(),    deviceScaleFactor);
134     rects[BottomPiece]      = snapRectToDevicePixels(inner.x(),    inner.maxY(), inner.width(),  slices.bottom(), deviceScaleFactor);
135
136     rects[MiddlePiece]      = snapRectToDevicePixels(inner.x(),    inner.y(),    inner.width(),  inner.height(),  deviceScaleFactor);
137     return rects;
138 }
139
140 Vector<FloatRect> NinePieceImage::computeNonIntrinsicRects(const Vector<FloatRect>& intrinsicRects, const LayoutBoxExtent& slices)
141 {
142     Vector<FloatRect> rects(MaxPiece);
143
144     for (ImagePiece piece = MinPiece; piece < MaxPiece; ++piece) {
145         if (isEmptyPieceRect(piece, slices))
146             continue;
147         rects[piece] = FloatRect(FloatPoint(), intrinsicRects[piece].size());
148     }
149
150     return rects;
151 }
152
153 FloatSize NinePieceImage::computeIntrinsicSideTileScale(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
154 {
155     ASSERT(!isCornerPiece(piece) && !isMiddlePiece(piece));
156     if (isEmptyPieceRect(piece, destinationRects, sourceRects))
157         return FloatSize(1, 1);
158
159     float scale;
160     if (isVerticalPiece(piece))
161         scale = destinationRects[piece].height() / sourceRects[piece].height();
162     else
163         scale = destinationRects[piece].width() / sourceRects[piece].width();
164
165     return FloatSize(scale, scale);
166 }
167
168 FloatSize NinePieceImage::computeIntrinsicMiddleTileScale(const Vector<FloatSize>& scales, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
169 {
170     FloatSize scale(1, 1);
171     if (isEmptyPieceRect(MiddlePiece, destinationRects, sourceRects))
172         return scale;
173
174     // Unlike the side pieces, the middle piece can have "stretch" specified in one axis but not the other.
175     // In fact the side pieces don't even use the scale factor unless they have a rule other than "stretch".
176     if (hRule == StretchImageRule)
177         scale.setWidth(destinationRects[MiddlePiece].width() / sourceRects[MiddlePiece].width());
178     else if (!isEmptyPieceRect(TopPiece, destinationRects, sourceRects))
179         scale.setWidth(scales[TopPiece].width());
180     else if (!isEmptyPieceRect(BottomPiece, destinationRects, sourceRects))
181         scale.setWidth(scales[BottomPiece].width());
182
183     if (vRule == StretchImageRule)
184         scale.setHeight(destinationRects[MiddlePiece].height() / sourceRects[MiddlePiece].height());
185     else if (!isEmptyPieceRect(LeftPiece, destinationRects, sourceRects))
186         scale.setHeight(scales[LeftPiece].height());
187     else if (!isEmptyPieceRect(RightPiece, destinationRects, sourceRects))
188         scale.setHeight(scales[RightPiece].height());
189
190     return scale;
191 }
192
193 Vector<FloatSize> NinePieceImage::computeIntrinsicTileScales(const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
194 {
195     Vector<FloatSize> scales(MaxPiece, FloatSize(1, 1));
196
197     scales[TopPiece]    = computeIntrinsicSideTileScale(TopPiece,    destinationRects, sourceRects);
198     scales[RightPiece]  = computeIntrinsicSideTileScale(RightPiece,  destinationRects, sourceRects);
199     scales[BottomPiece] = computeIntrinsicSideTileScale(BottomPiece, destinationRects, sourceRects);
200     scales[LeftPiece]   = computeIntrinsicSideTileScale(LeftPiece,   destinationRects, sourceRects);
201
202     scales[MiddlePiece] = computeIntrinsicMiddleTileScale(scales, destinationRects, sourceRects, hRule, vRule);
203     return scales;
204 }
205
206 Vector<FloatSize> NinePieceImage::computeNonIntrinsicTileScales()
207 {
208     return Vector<FloatSize>(MaxPiece, FloatSize(1, 1));
209 }
210
211 void NinePieceImage::paint(GraphicsContext* graphicsContext, RenderElement* renderer, const RenderStyle& style, const LayoutRect& destination, const LayoutSize& source,  bool intrinsicSource, float deviceScaleFactor, CompositeOperator op) const
212 {
213     StyleImage* styleImage = image();
214     ASSERT(styleImage && styleImage->isLoaded());
215
216     LayoutBoxExtent sourceSlices = computeSlices(source, imageSlices(), styleImage->imageScaleFactor());
217     LayoutBoxExtent destinationSlices = computeSlices(destination.size(), borderSlices(), style.borderWidth(), sourceSlices);
218
219     scaleSlicesIfNeeded(destination.size(), destinationSlices, deviceScaleFactor);
220
221     Vector<FloatRect> destinationRects = computeIntrinsicRects(destination, destinationSlices, deviceScaleFactor);
222     Vector<FloatRect> sourceRects;
223     Vector<FloatSize> tileScales;
224
225     if (intrinsicSource) {
226         sourceRects = computeIntrinsicRects(FloatRect(FloatPoint(), source), sourceSlices, deviceScaleFactor);
227         tileScales = computeIntrinsicTileScales(destinationRects, sourceRects, horizontalRule(), verticalRule());
228     } else {
229         sourceRects = computeNonIntrinsicRects(destinationRects, sourceSlices);
230         tileScales = computeNonIntrinsicTileScales();
231     }
232
233     RefPtr<Image> image = styleImage->image(renderer, source);
234     ColorSpace colorSpace = style.colorSpace();
235
236     for (ImagePiece piece = MinPiece; piece < MaxPiece; ++piece) {
237         if ((piece == MiddlePiece && !fill()) || isEmptyPieceRect(piece, destinationRects, sourceRects))
238             continue;
239
240         if (isCornerPiece(piece)) {
241             graphicsContext->drawImage(image.get(), colorSpace, destinationRects[piece], sourceRects[piece], op);
242             continue;
243         }
244
245         Image::TileRule hRule = isHorizontalPiece(piece) ? static_cast<Image::TileRule>(horizontalRule()) : Image::StretchTile;
246         Image::TileRule vRule = isVerticalPiece(piece) ? static_cast<Image::TileRule>(verticalRule()) : Image::StretchTile;
247         graphicsContext->drawTiledImage(image.get(), colorSpace, destinationRects[piece], sourceRects[piece], tileScales[piece], hRule, vRule, op);
248     }
249 }
250
251 NinePieceImageData::NinePieceImageData()
252     : fill(false)
253     , horizontalRule(StretchImageRule)
254     , verticalRule(StretchImageRule)
255     , image(nullptr)
256     , imageSlices(Length(100, Percent), Length(100, Percent), Length(100, Percent), Length(100, Percent))
257     , borderSlices(Length(1, Relative), Length(1, Relative), Length(1, Relative), Length(1, Relative))
258     , outset(0)
259 {
260 }
261
262 inline NinePieceImageData::NinePieceImageData(const NinePieceImageData& other)
263     : RefCounted<NinePieceImageData>()
264     , fill(other.fill)
265     , horizontalRule(other.horizontalRule)
266     , verticalRule(other.verticalRule)
267     , image(other.image)
268     , imageSlices(other.imageSlices)
269     , borderSlices(other.borderSlices)
270     , outset(other.outset)
271 {
272 }
273
274 Ref<NinePieceImageData> NinePieceImageData::copy() const
275 {
276     return adoptRef(*new NinePieceImageData(*this));
277 }
278
279 bool NinePieceImageData::operator==(const NinePieceImageData& other) const
280 {
281     return StyleImage::imagesEquivalent(image.get(), other.image.get())
282         && imageSlices == other.imageSlices
283         && fill == other.fill
284         && borderSlices == other.borderSlices
285         && outset == other.outset
286         && horizontalRule == other.horizontalRule
287         && verticalRule == other.verticalRule;
288 }
289
290 }