1 /* 2 * Copyright (C) 2000 Lars Knoll (knoll (at) kde.org) 3 * (C) 2000 Antti Koivisto (koivisto (at) kde.org) 4 * (C) 2000 Dirk Mueller (mueller (at) kde.org) 5 * Copyright (C) 2003, 2005, 2006, 2007, 2008, 2013 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 #ifndef NinePieceImage_h 25 #define NinePieceImage_h 26 27 #include "core/platform/LayoutUnit.h" 28 #include "core/platform/LengthBox.h" 29 #include "core/rendering/style/DataRef.h" 30 #include "core/rendering/style/StyleImage.h" 31 32 namespace WebCore { 33 34 enum ENinePieceImageRule { 35 StretchImageRule, RoundImageRule, SpaceImageRule, RepeatImageRule 36 }; 37 38 class NinePieceImageData : public RefCounted<NinePieceImageData> { 39 public: 40 static PassRefPtr<NinePieceImageData> create() { return adoptRef(new NinePieceImageData); } 41 PassRefPtr<NinePieceImageData> copy() const { return adoptRef(new NinePieceImageData(*this)); } 42 43 bool operator==(const NinePieceImageData&) const; 44 bool operator!=(const NinePieceImageData& o) const { return !(*this == o); } 45 46 bool fill : 1; 47 unsigned horizontalRule : 2; // ENinePieceImageRule 48 unsigned verticalRule : 2; // ENinePieceImageRule 49 RefPtr<StyleImage> image; 50 LengthBox imageSlices; 51 LengthBox borderSlices; 52 LengthBox outset; 53 54 private: 55 NinePieceImageData(); 56 NinePieceImageData(const NinePieceImageData&); 57 }; 58 59 class NinePieceImage { 60 public: 61 NinePieceImage(); 62 NinePieceImage(PassRefPtr<StyleImage>, LengthBox imageSlices, bool fill, LengthBox borderSlices, LengthBox outset, ENinePieceImageRule horizontalRule, ENinePieceImageRule verticalRule); 63 64 bool operator==(const NinePieceImage& other) const { return m_data == other.m_data; } 65 bool operator!=(const NinePieceImage& other) const { return m_data != other.m_data; } 66 67 bool hasImage() const { return m_data->image; } 68 StyleImage* image() const { return m_data->image.get(); } 69 void setImage(PassRefPtr<StyleImage> image) { m_data.access()->image = image; } 70 71 const LengthBox& imageSlices() const { return m_data->imageSlices; } 72 void setImageSlices(const LengthBox& slices) { m_data.access()->imageSlices = slices; } 73 74 bool fill() const { return m_data->fill; } 75 void setFill(bool fill) { m_data.access()->fill = fill; } 76 77 const LengthBox& borderSlices() const { return m_data->borderSlices; } 78 void setBorderSlices(const LengthBox& slices) { m_data.access()->borderSlices = slices; } 79 80 const LengthBox& outset() const { return m_data->outset; } 81 void setOutset(const LengthBox& outset) { m_data.access()->outset = outset; } 82 83 ENinePieceImageRule horizontalRule() const { return static_cast<ENinePieceImageRule>(m_data->horizontalRule); } 84 void setHorizontalRule(ENinePieceImageRule rule) { m_data.access()->horizontalRule = rule; } 85 86 ENinePieceImageRule verticalRule() const { return static_cast<ENinePieceImageRule>(m_data->verticalRule); } 87 void setVerticalRule(ENinePieceImageRule rule) { m_data.access()->verticalRule = rule; } 88 89 void copyImageSlicesFrom(const NinePieceImage& other) 90 { 91 m_data.access()->imageSlices = other.m_data->imageSlices; 92 m_data.access()->fill = other.m_data->fill; 93 } 94 95 void copyBorderSlicesFrom(const NinePieceImage& other) 96 { 97 m_data.access()->borderSlices = other.m_data->borderSlices; 98 } 99 100 void copyOutsetFrom(const NinePieceImage& other) 101 { 102 m_data.access()->outset = other.m_data->outset; 103 } 104 105 void copyRepeatFrom(const NinePieceImage& other) 106 { 107 m_data.access()->horizontalRule = other.m_data->horizontalRule; 108 m_data.access()->verticalRule = other.m_data->verticalRule; 109 } 110 111 void setMaskDefaults() 112 { 113 m_data.access()->imageSlices = LengthBox(0); 114 m_data.access()->fill = true; 115 m_data.access()->borderSlices = LengthBox(); 116 } 117 118 static LayoutUnit computeOutset(Length outsetSide, LayoutUnit borderSide) 119 { 120 if (outsetSide.isRelative()) 121 return outsetSide.value() * borderSide; 122 return outsetSide.value(); 123 } 124 125 private: 126 DataRef<NinePieceImageData> m_data; 127 }; 128 129 } // namespace WebCore 130 131 #endif // NinePieceImage_h 132