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 Apple Inc. All rights reserved.
      6  * Copyright (C) 2006 Graham Dennis (graham.dennis (at) gmail.com)
      7  *
      8  * This library is free software; you can redistribute it and/or
      9  * modify it under the terms of the GNU Library General Public
     10  * License as published by the Free Software Foundation; either
     11  * version 2 of the License, or (at your option) any later version.
     12  *
     13  * This library is distributed in the hope that it will be useful,
     14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     16  * Library General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU Library General Public License
     19  * along with this library; see the file COPYING.LIB.  If not, write to
     20  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     21  * Boston, MA 02110-1301, USA.
     22  *
     23  */
     24 
     25 #ifndef FillLayer_h
     26 #define FillLayer_h
     27 
     28 #include "core/rendering/style/RenderStyleConstants.h"
     29 #include "core/rendering/style/StyleImage.h"
     30 #include "platform/Length.h"
     31 #include "platform/LengthSize.h"
     32 #include "platform/graphics/GraphicsTypes.h"
     33 #include "wtf/RefPtr.h"
     34 
     35 namespace WebCore {
     36 
     37 struct FillSize {
     38     FillSize()
     39         : type(SizeLength)
     40     {
     41     }
     42 
     43     FillSize(EFillSizeType t, LengthSize l)
     44         : type(t)
     45         , size(l)
     46     {
     47     }
     48 
     49     bool operator==(const FillSize& o) const
     50     {
     51         return type == o.type && size == o.size;
     52     }
     53     bool operator!=(const FillSize& o) const
     54     {
     55         return !(*this == o);
     56     }
     57 
     58     EFillSizeType type;
     59     LengthSize size;
     60 };
     61 
     62 class FillLayer {
     63     WTF_MAKE_FAST_ALLOCATED;
     64 public:
     65     FillLayer(EFillLayerType, bool useInitialValues = false);
     66     ~FillLayer();
     67 
     68     StyleImage* image() const { return m_image.get(); }
     69     Length xPosition() const { return m_xPosition; }
     70     Length yPosition() const { return m_yPosition; }
     71     BackgroundEdgeOrigin backgroundXOrigin() const { return static_cast<BackgroundEdgeOrigin>(m_backgroundXOrigin); }
     72     BackgroundEdgeOrigin backgroundYOrigin() const { return static_cast<BackgroundEdgeOrigin>(m_backgroundYOrigin); }
     73     EFillAttachment attachment() const { return static_cast<EFillAttachment>(m_attachment); }
     74     EFillBox clip() const { return static_cast<EFillBox>(m_clip); }
     75     EFillBox origin() const { return static_cast<EFillBox>(m_origin); }
     76     EFillRepeat repeatX() const { return static_cast<EFillRepeat>(m_repeatX); }
     77     EFillRepeat repeatY() const { return static_cast<EFillRepeat>(m_repeatY); }
     78     CompositeOperator composite() const { return static_cast<CompositeOperator>(m_composite); }
     79     blink::WebBlendMode blendMode() const { return static_cast<blink::WebBlendMode>(m_blendMode); }
     80     LengthSize sizeLength() const { return m_sizeLength; }
     81     EFillSizeType sizeType() const { return static_cast<EFillSizeType>(m_sizeType); }
     82     FillSize size() const { return FillSize(static_cast<EFillSizeType>(m_sizeType), m_sizeLength); }
     83     EMaskSourceType maskSourceType() const { return static_cast<EMaskSourceType>(m_maskSourceType); }
     84 
     85     const FillLayer* next() const { return m_next; }
     86     FillLayer* next() { return m_next; }
     87 
     88     bool isImageSet() const { return m_imageSet; }
     89     bool isXPositionSet() const { return m_xPosSet; }
     90     bool isYPositionSet() const { return m_yPosSet; }
     91     bool isBackgroundXOriginSet() const { return m_backgroundXOriginSet; }
     92     bool isBackgroundYOriginSet() const { return m_backgroundYOriginSet; }
     93     bool isAttachmentSet() const { return m_attachmentSet; }
     94     bool isClipSet() const { return m_clipSet; }
     95     bool isOriginSet() const { return m_originSet; }
     96     bool isRepeatXSet() const { return m_repeatXSet; }
     97     bool isRepeatYSet() const { return m_repeatYSet; }
     98     bool isCompositeSet() const { return m_compositeSet; }
     99     bool isBlendModeSet() const { return m_blendModeSet; }
    100     bool isSizeSet() const { return m_sizeType != SizeNone; }
    101     bool isMaskSourceTypeSet() const { return m_maskSourceTypeSet; }
    102 
    103     void setImage(PassRefPtr<StyleImage> i) { m_image = i; m_imageSet = true; }
    104     void setXPosition(Length position) { m_xPosition = position; m_xPosSet = true; m_backgroundXOriginSet = false; m_backgroundXOrigin = LeftEdge; }
    105     void setYPosition(Length position) { m_yPosition = position; m_yPosSet = true; m_backgroundYOriginSet = false; m_backgroundYOrigin = TopEdge; }
    106     void setBackgroundXOrigin(BackgroundEdgeOrigin origin) { m_backgroundXOrigin = origin; m_backgroundXOriginSet = true; }
    107     void setBackgroundYOrigin(BackgroundEdgeOrigin origin) { m_backgroundYOrigin = origin; m_backgroundYOriginSet = true; }
    108     void setAttachment(EFillAttachment attachment) { m_attachment = attachment; m_attachmentSet = true; }
    109     void setClip(EFillBox b) { m_clip = b; m_clipSet = true; }
    110     void setOrigin(EFillBox b) { m_origin = b; m_originSet = true; }
    111     void setRepeatX(EFillRepeat r) { m_repeatX = r; m_repeatXSet = true; }
    112     void setRepeatY(EFillRepeat r) { m_repeatY = r; m_repeatYSet = true; }
    113     void setComposite(CompositeOperator c) { m_composite = c; m_compositeSet = true; }
    114     void setBlendMode(blink::WebBlendMode b) { m_blendMode = b; m_blendModeSet = true; }
    115     void setSizeType(EFillSizeType b) { m_sizeType = b; }
    116     void setSizeLength(LengthSize l) { m_sizeLength = l; }
    117     void setSize(FillSize f) { m_sizeType = f.type; m_sizeLength = f.size; }
    118     void setMaskSourceType(EMaskSourceType m) { m_maskSourceType = m; m_maskSourceTypeSet = true; }
    119 
    120     void clearImage() { m_image.clear(); m_imageSet = false; }
    121     void clearXPosition()
    122     {
    123         m_xPosSet = false;
    124         m_backgroundXOriginSet = false;
    125     }
    126     void clearYPosition()
    127     {
    128         m_yPosSet = false;
    129         m_backgroundYOriginSet = false;
    130     }
    131 
    132     void clearAttachment() { m_attachmentSet = false; }
    133     void clearClip() { m_clipSet = false; }
    134     void clearOrigin() { m_originSet = false; }
    135     void clearRepeatX() { m_repeatXSet = false; }
    136     void clearRepeatY() { m_repeatYSet = false; }
    137     void clearComposite() { m_compositeSet = false; }
    138     void clearBlendMode() { m_blendModeSet = false; }
    139     void clearSize() { m_sizeType = SizeNone; }
    140     void clearMaskSourceType() { m_maskSourceTypeSet = false; }
    141 
    142     void setNext(FillLayer* n) { if (m_next != n) { delete m_next; m_next = n; } }
    143 
    144     FillLayer& operator=(const FillLayer& o);
    145     FillLayer(const FillLayer& o);
    146 
    147     bool operator==(const FillLayer& o) const;
    148     bool operator!=(const FillLayer& o) const
    149     {
    150         return !(*this == o);
    151     }
    152 
    153     bool containsImage(StyleImage*) const;
    154     bool imagesAreLoaded() const;
    155 
    156     bool hasImage() const
    157     {
    158         if (m_image)
    159             return true;
    160         return m_next ? m_next->hasImage() : false;
    161     }
    162 
    163     bool hasFixedImage() const
    164     {
    165         if (m_image && m_attachment == FixedBackgroundAttachment)
    166             return true;
    167         return m_next ? m_next->hasFixedImage() : false;
    168     }
    169 
    170     bool hasOpaqueImage(const RenderObject*) const;
    171     bool hasRepeatXY() const;
    172     bool clipOccludesNextLayers(bool firstLayer) const;
    173 
    174     EFillLayerType type() const { return static_cast<EFillLayerType>(m_type); }
    175 
    176     void fillUnsetProperties();
    177     void cullEmptyLayers();
    178 
    179     static EFillAttachment initialFillAttachment(EFillLayerType) { return ScrollBackgroundAttachment; }
    180     static EFillBox initialFillClip(EFillLayerType) { return BorderFillBox; }
    181     static EFillBox initialFillOrigin(EFillLayerType type) { return type == BackgroundFillLayer ? PaddingFillBox : BorderFillBox; }
    182     static EFillRepeat initialFillRepeatX(EFillLayerType) { return RepeatFill; }
    183     static EFillRepeat initialFillRepeatY(EFillLayerType) { return RepeatFill; }
    184     static CompositeOperator initialFillComposite(EFillLayerType) { return CompositeSourceOver; }
    185     static blink::WebBlendMode initialFillBlendMode(EFillLayerType) { return blink::WebBlendModeNormal; }
    186     static EFillSizeType initialFillSizeType(EFillLayerType) { return SizeLength; }
    187     static LengthSize initialFillSizeLength(EFillLayerType) { return LengthSize(); }
    188     static FillSize initialFillSize(EFillLayerType type) { return FillSize(initialFillSizeType(type), initialFillSizeLength(type)); }
    189     static Length initialFillXPosition(EFillLayerType) { return Length(0.0, Percent); }
    190     static Length initialFillYPosition(EFillLayerType) { return Length(0.0, Percent); }
    191     static StyleImage* initialFillImage(EFillLayerType) { return 0; }
    192     static EMaskSourceType initialFillMaskSourceType(EFillLayerType) { return MaskAlpha; }
    193 
    194 private:
    195     friend class RenderStyle;
    196 
    197     void computeClipMax() const;
    198 
    199     FillLayer() { }
    200 
    201     FillLayer* m_next;
    202 
    203     RefPtr<StyleImage> m_image;
    204 
    205     Length m_xPosition;
    206     Length m_yPosition;
    207 
    208     LengthSize m_sizeLength;
    209 
    210     unsigned m_attachment : 2; // EFillAttachment
    211     unsigned m_clip : 2; // EFillBox
    212     unsigned m_origin : 2; // EFillBox
    213     unsigned m_repeatX : 3; // EFillRepeat
    214     unsigned m_repeatY : 3; // EFillRepeat
    215     unsigned m_composite : 4; // CompositeOperator
    216     unsigned m_sizeType : 2; // EFillSizeType
    217     unsigned m_blendMode : 5; // blink::WebBlendMode
    218     unsigned m_maskSourceType : 1; // EMaskSourceType
    219     unsigned m_backgroundXOrigin : 2; // BackgroundEdgeOrigin
    220     unsigned m_backgroundYOrigin : 2; // BackgroundEdgeOrigin
    221 
    222     unsigned m_imageSet : 1;
    223     unsigned m_attachmentSet : 1;
    224     unsigned m_clipSet : 1;
    225     unsigned m_originSet : 1;
    226     unsigned m_repeatXSet : 1;
    227     unsigned m_repeatYSet : 1;
    228     unsigned m_xPosSet : 1;
    229     unsigned m_yPosSet : 1;
    230     unsigned m_backgroundXOriginSet : 1;
    231     unsigned m_backgroundYOriginSet : 1;
    232     unsigned m_compositeSet : 1;
    233     unsigned m_blendModeSet : 1;
    234     unsigned m_maskSourceTypeSet : 1;
    235 
    236     unsigned m_type : 1; // EFillLayerType
    237 
    238     mutable unsigned m_clipMax : 2; // EFillBox, maximum m_clip value from this to bottom layer
    239 };
    240 
    241 } // namespace WebCore
    242 
    243 #endif // FillLayer_h
    244