1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <sal/config.h>
21 
22 #include <algorithm>
23 
24 #include <drawinglayer/attribute/fillgraphicattribute.hxx>
25 #include <vcl/graph.hxx>
26 
27 namespace drawinglayer
28 {
29     namespace attribute
30     {
31         class ImpFillGraphicAttribute
32         {
33         public:
34             // data definitions
35             Graphic                                 maGraphic;
36             basegfx::B2DRange                       maGraphicRange;
37 
38             bool                                    mbTiling : 1;
39 
40             // tiling definitions, offsets in X/Y in percent for each 2nd row.
41             // If both are set, Y is ignored (X has precedence)
42             double                                  mfOffsetX;
43             double                                  mfOffsetY;
44 
ImpFillGraphicAttribute(const Graphic & rGraphic,const basegfx::B2DRange & rGraphicRange,bool bTiling,double fOffsetX,double fOffsetY)45             ImpFillGraphicAttribute(
46                 const Graphic& rGraphic,
47                 const basegfx::B2DRange& rGraphicRange,
48                 bool bTiling,
49                 double fOffsetX,
50                 double fOffsetY)
51             :   maGraphic(rGraphic),
52                 maGraphicRange(rGraphicRange),
53                 mbTiling(bTiling),
54                 mfOffsetX(fOffsetX),
55                 mfOffsetY(fOffsetY)
56             {
57                 // access once to ensure that the buffered bitmap exists, else
58                 // the SolarMutex may be needed to create it. This may not be
59                 // available when a renderer works with multi-threading.
60                 // When changing this, please check if it is still possible to
61                 // use a metafile as texture for a 3D object
62                 maGraphic.GetBitmapEx();
63             }
64 
ImpFillGraphicAttribute()65             ImpFillGraphicAttribute()
66             :   maGraphic(Graphic()),
67                 maGraphicRange(basegfx::B2DRange()),
68                 mbTiling(false),
69                 mfOffsetX(0.0),
70                 mfOffsetY(0.0)
71             {
72             }
73 
74             // data read access
getGraphic() const75             const Graphic& getGraphic() const { return maGraphic; }
getGraphicRange() const76             const basegfx::B2DRange& getGraphicRange() const { return maGraphicRange; }
getTiling() const77             bool getTiling() const { return mbTiling; }
getOffsetX() const78             double getOffsetX() const { return mfOffsetX; }
getOffsetY() const79             double getOffsetY() const { return mfOffsetY; }
80 
operator ==(const ImpFillGraphicAttribute & rCandidate) const81             bool operator==(const ImpFillGraphicAttribute& rCandidate) const
82             {
83                 return (getGraphic() == rCandidate.getGraphic()
84                     && getGraphicRange() == rCandidate.getGraphicRange()
85                     && getTiling() == rCandidate.getTiling()
86                     && getOffsetX() == rCandidate.getOffsetX()
87                     && getOffsetY() == rCandidate.getOffsetY());
88             }
89         };
90 
91         namespace
92         {
93             struct theGlobalDefault :
94                 public rtl::Static< FillGraphicAttribute::ImplType, theGlobalDefault > {};
95         }
96 
FillGraphicAttribute(const Graphic & rGraphic,const basegfx::B2DRange & rGraphicRange,bool bTiling,double fOffsetX,double fOffsetY)97         FillGraphicAttribute::FillGraphicAttribute(
98             const Graphic& rGraphic,
99             const basegfx::B2DRange& rGraphicRange,
100             bool bTiling,
101             double fOffsetX,
102             double fOffsetY)
103         :   mpFillGraphicAttribute(ImpFillGraphicAttribute(
104                 rGraphic, rGraphicRange, bTiling,
105                     std::clamp(fOffsetX, 0.0, 1.0),
106                     std::clamp(fOffsetY, 0.0, 1.0)))
107         {
108         }
109 
110         FillGraphicAttribute::FillGraphicAttribute(const FillGraphicAttribute&) = default;
111 
112         FillGraphicAttribute::~FillGraphicAttribute() = default;
113 
isDefault() const114         bool FillGraphicAttribute::isDefault() const
115         {
116             return mpFillGraphicAttribute.same_object(theGlobalDefault::get());
117         }
118 
119         FillGraphicAttribute& FillGraphicAttribute::operator=(const FillGraphicAttribute&) = default;
120 
operator ==(const FillGraphicAttribute & rCandidate) const121         bool FillGraphicAttribute::operator==(const FillGraphicAttribute& rCandidate) const
122         {
123             // tdf#87509 default attr is always != non-default attr, even with same values
124             if(rCandidate.isDefault() != isDefault())
125                 return false;
126 
127             return rCandidate.mpFillGraphicAttribute == mpFillGraphicAttribute;
128         }
129 
getGraphic() const130         const Graphic& FillGraphicAttribute::getGraphic() const
131         {
132             return mpFillGraphicAttribute->getGraphic();
133         }
134 
getGraphicRange() const135         const basegfx::B2DRange& FillGraphicAttribute::getGraphicRange() const
136         {
137             return mpFillGraphicAttribute->getGraphicRange();
138         }
139 
getTiling() const140         bool FillGraphicAttribute::getTiling() const
141         {
142             return mpFillGraphicAttribute->getTiling();
143         }
144 
getOffsetX() const145         double FillGraphicAttribute::getOffsetX() const
146         {
147             return mpFillGraphicAttribute->getOffsetX();
148         }
149 
getOffsetY() const150         double FillGraphicAttribute::getOffsetY() const
151         {
152             return mpFillGraphicAttribute->getOffsetY();
153         }
154 
155     } // end of namespace attribute
156 } // end of namespace drawinglayer
157 
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
159