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.
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.
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.
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.
25 #include "NinePieceImage.h"
27 #include "GraphicsContext.h"
28 #include "LengthFunctions.h"
29 #include "RenderStyle.h"
30 #include <wtf/NeverDestroyed.h>
34 static DataRef<NinePieceImageData>& defaultData()
36 static NeverDestroyed<DataRef<NinePieceImageData>> data(NinePieceImageData::create());
40 NinePieceImage::NinePieceImage()
41 : m_data(defaultData())
45 NinePieceImage::NinePieceImage(PassRefPtr<StyleImage> image, LengthBox imageSlices, bool fill, LengthBox borderSlices, LengthBox outset, ENinePieceImageRule horizontalRule, ENinePieceImageRule verticalRule)
46 : m_data(NinePieceImageData::create())
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;
57 LayoutUnit NinePieceImage::computeSlice(Length length, LayoutUnit width, LayoutUnit slice, LayoutUnit extent)
59 if (length.isRelative())
60 return length.value() * width;
63 return valueForLength(length, extent);
66 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, int scaleFactor)
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);
75 LayoutBoxExtent NinePieceImage::computeSlices(const LayoutSize& size, const LengthBox& lengths, const FloatBoxExtent& widths, const LayoutBoxExtent& slices)
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);
84 void NinePieceImage::scaleSlicesIfNeeded(const LayoutSize& size, LayoutBoxExtent& slices, int scaleFactor)
86 LayoutUnit width = std::max<LayoutUnit>(1 / scaleFactor, slices.left() + slices.right());
87 LayoutUnit height = std::max<LayoutUnit>(1 / scaleFactor, slices.top() + slices.bottom());
89 float sliceScaleFactor = std::min((float)size.width() / width, (float)size.height() / height);
91 if (sliceScaleFactor >= 1)
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;
101 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const LayoutBoxExtent& slices)
103 if (piece == MiddlePiece)
106 PhysicalBoxSide horizontalSide = imagePieceHorizontalSide(piece);
107 PhysicalBoxSide verticalSide = imagePieceVerticalSide(piece);
108 return !((horizontalSide == NilSide || slices.at(horizontalSide)) && (verticalSide == NilSide || slices.at(verticalSide)));
111 bool NinePieceImage::isEmptyPieceRect(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
113 return destinationRects[piece].isEmpty() || sourceRects[piece].isEmpty();
116 Vector<FloatRect> NinePieceImage::computeIntrinsicRects(const FloatRect& outer, const LayoutBoxExtent& slices, float deviceScaleFactor)
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));
123 Vector<FloatRect> rects(MaxPiece);
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);
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);
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);
136 rects[MiddlePiece] = snapRectToDevicePixels(inner.x(), inner.y(), inner.width(), inner.height(), deviceScaleFactor);
140 Vector<FloatRect> NinePieceImage::computeNonIntrinsicRects(const Vector<FloatRect>& intrinsicRects, const LayoutBoxExtent& slices)
142 Vector<FloatRect> rects(MaxPiece);
144 for (ImagePiece piece = MinPiece; piece < MaxPiece; ++piece) {
145 if (isEmptyPieceRect(piece, slices))
147 rects[piece] = FloatRect(FloatPoint(), intrinsicRects[piece].size());
153 FloatSize NinePieceImage::computeIntrinsicSideTileScale(ImagePiece piece, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects)
155 ASSERT(!isCornerPiece(piece) && !isMiddlePiece(piece));
156 if (isEmptyPieceRect(piece, destinationRects, sourceRects))
157 return FloatSize(1, 1);
160 if (isVerticalPiece(piece))
161 scale = destinationRects[piece].height() / sourceRects[piece].height();
163 scale = destinationRects[piece].width() / sourceRects[piece].width();
165 return FloatSize(scale, scale);
168 FloatSize NinePieceImage::computeIntrinsicMiddleTileScale(const Vector<FloatSize>& scales, const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
170 FloatSize scale(1, 1);
171 if (isEmptyPieceRect(MiddlePiece, destinationRects, sourceRects))
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());
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());
193 Vector<FloatSize> NinePieceImage::computeIntrinsicTileScales(const Vector<FloatRect>& destinationRects, const Vector<FloatRect>& sourceRects, ENinePieceImageRule hRule, ENinePieceImageRule vRule)
195 Vector<FloatSize> scales(MaxPiece, FloatSize(1, 1));
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);
202 scales[MiddlePiece] = computeIntrinsicMiddleTileScale(scales, destinationRects, sourceRects, hRule, vRule);
206 Vector<FloatSize> NinePieceImage::computeNonIntrinsicTileScales()
208 return Vector<FloatSize>(MaxPiece, FloatSize(1, 1));
211 void NinePieceImage::paint(GraphicsContext* graphicsContext, RenderElement* renderer, const RenderStyle& style, const LayoutRect& destination, const LayoutSize& source, bool intrinsicSource, float deviceScaleFactor, CompositeOperator op) const
213 StyleImage* styleImage = image();
214 ASSERT(styleImage && styleImage->isLoaded());
216 LayoutBoxExtent sourceSlices = computeSlices(source, imageSlices(), styleImage->imageScaleFactor());
217 LayoutBoxExtent destinationSlices = computeSlices(destination.size(), borderSlices(), style.borderWidth(), sourceSlices);
219 scaleSlicesIfNeeded(destination.size(), destinationSlices, deviceScaleFactor);
221 Vector<FloatRect> destinationRects = computeIntrinsicRects(destination, destinationSlices, deviceScaleFactor);
222 Vector<FloatRect> sourceRects;
223 Vector<FloatSize> tileScales;
225 if (intrinsicSource) {
226 sourceRects = computeIntrinsicRects(FloatRect(FloatPoint(), source), sourceSlices, deviceScaleFactor);
227 tileScales = computeIntrinsicTileScales(destinationRects, sourceRects, horizontalRule(), verticalRule());
229 sourceRects = computeNonIntrinsicRects(destinationRects, sourceSlices);
230 tileScales = computeNonIntrinsicTileScales();
233 RefPtr<Image> image = styleImage->image(renderer, source);
234 ColorSpace colorSpace = style.colorSpace();
236 for (ImagePiece piece = MinPiece; piece < MaxPiece; ++piece) {
237 if ((piece == MiddlePiece && !fill()) || isEmptyPieceRect(piece, destinationRects, sourceRects))
240 if (isCornerPiece(piece)) {
241 graphicsContext->drawImage(image.get(), colorSpace, destinationRects[piece], sourceRects[piece], op);
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);
251 NinePieceImageData::NinePieceImageData()
253 , horizontalRule(StretchImageRule)
254 , verticalRule(StretchImageRule)
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))
262 inline NinePieceImageData::NinePieceImageData(const NinePieceImageData& other)
263 : RefCounted<NinePieceImageData>()
265 , horizontalRule(other.horizontalRule)
266 , verticalRule(other.verticalRule)
268 , imageSlices(other.imageSlices)
269 , borderSlices(other.borderSlices)
270 , outset(other.outset)
274 Ref<NinePieceImageData> NinePieceImageData::copy() const
276 return adoptRef(*new NinePieceImageData(*this));
279 bool NinePieceImageData::operator==(const NinePieceImageData& other) const
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;