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