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 <svx/sdr/attribute/sdrformtextattribute.hxx>
21 #include <basegfx/vector/b2enums.hxx>
22 #include <svl/itemset.hxx>
23 #include <svx/xftdiit.hxx>
24 #include <svx/xftstit.hxx>
25 #include <svx/xftshxy.hxx>
26 #include <svx/xftshtit.hxx>
27 #include <svx/xtextit0.hxx>
28 #include <svx/xftadit.hxx>
29 #include <svx/xftshit.hxx>
30 #include <svx/xftshcit.hxx>
31 #include <svx/xftmrit.hxx>
32 #include <svx/xftouit.hxx>
33 #include <svx/sdshtitm.hxx>
34 #include <svx/xlntrit.hxx>
35 #include <svx/sdshcitm.hxx>
36 #include <svx/xlnclit.hxx>
37 #include <svx/xlnwtit.hxx>
38 #include <svx/xlinjoit.hxx>
39 #include <svx/xlncapit.hxx>
40 #include <svx/xlineit0.hxx>
41 #include <svx/xdash.hxx>
42 #include <svx/xlndsit.hxx>
43 #include <drawinglayer/attribute/lineattribute.hxx>
44 #include <drawinglayer/attribute/strokeattribute.hxx>
45 #include <sdr/attribute/sdrformtextoutlineattribute.hxx>
46 #include <com/sun/star/drawing/LineCap.hpp>
47 #include <com/sun/star/drawing/LineStyle.hpp>
48 #include <rtl/instance.hxx>
49 
50 
51 // helper to get line, stroke and transparence attributes from SfxItemSet
52 
53 namespace
54 {
impGetB2DLineJoin(css::drawing::LineJoint eLineJoint)55     basegfx::B2DLineJoin impGetB2DLineJoin(css::drawing::LineJoint eLineJoint)
56     {
57         switch(eLineJoint)
58         {
59             case css::drawing::LineJoint_BEVEL :
60             {
61                 return basegfx::B2DLineJoin::Bevel;
62             }
63             case css::drawing::LineJoint_MIDDLE :
64             case css::drawing::LineJoint_MITER :
65             {
66                 return basegfx::B2DLineJoin::Miter;
67             }
68             case css::drawing::LineJoint_ROUND :
69             {
70                 return basegfx::B2DLineJoin::Round;
71             }
72             default : // css::drawing::LineJoint_NONE
73             {
74                 return basegfx::B2DLineJoin::NONE; // XLINEJOINT_NONE
75             }
76         }
77     }
78 
impGetStrokeTransparence(bool bShadow,const SfxItemSet & rSet)79     sal_uInt8 impGetStrokeTransparence(bool bShadow, const SfxItemSet& rSet)
80     {
81         sal_uInt8 nRetval;
82 
83         if(bShadow)
84         {
85             nRetval = static_cast<sal_uInt8>((rSet.Get(SDRATTR_SHADOWTRANSPARENCE).GetValue() * 255) / 100);
86         }
87         else
88         {
89             nRetval = static_cast<sal_uInt8>((rSet.Get(XATTR_LINETRANSPARENCE).GetValue() * 255) / 100);
90         }
91 
92         return nRetval;
93     }
94 
impGetLineAttribute(bool bShadow,const SfxItemSet & rSet)95     drawinglayer::attribute::LineAttribute impGetLineAttribute(bool bShadow, const SfxItemSet& rSet)
96     {
97         basegfx::BColor aColorAttribute;
98 
99         if(bShadow)
100         {
101             const Color aShadowColor(rSet.Get(SDRATTR_SHADOWCOLOR).GetColorValue());
102             aColorAttribute = aShadowColor.getBColor();
103         }
104         else
105         {
106             const Color aLineColor(rSet.Get(XATTR_LINECOLOR).GetColorValue());
107             aColorAttribute = aLineColor.getBColor();
108         }
109 
110         const sal_uInt32 nLineWidth = rSet.Get(XATTR_LINEWIDTH).GetValue();
111         const css::drawing::LineJoint eLineJoint = rSet.Get(XATTR_LINEJOINT).GetValue();
112         const css::drawing::LineCap eLineCap = rSet.Get(XATTR_LINECAP).GetValue();
113 
114         return drawinglayer::attribute::LineAttribute(
115             aColorAttribute,
116             static_cast<double>(nLineWidth),
117             impGetB2DLineJoin(eLineJoint),
118             eLineCap);
119     }
120 
impGetStrokeAttribute(const SfxItemSet & rSet)121     drawinglayer::attribute::StrokeAttribute impGetStrokeAttribute(const SfxItemSet& rSet)
122     {
123         const css::drawing::LineStyle eLineStyle = rSet.Get(XATTR_LINESTYLE).GetValue();
124         double fFullDotDashLen(0.0);
125         ::std::vector< double > aDotDashArray;
126 
127         if(css::drawing::LineStyle_DASH == eLineStyle)
128         {
129             const XDash& rDash = rSet.Get(XATTR_LINEDASH).GetDashValue();
130 
131             if(rDash.GetDots() || rDash.GetDashes())
132             {
133                 const sal_uInt32 nLineWidth = rSet.Get(XATTR_LINEWIDTH).GetValue();
134                 fFullDotDashLen = rDash.CreateDotDashArray(aDotDashArray, static_cast<double>(nLineWidth));
135             }
136         }
137 
138         return drawinglayer::attribute::StrokeAttribute(aDotDashArray, fFullDotDashLen);
139     }
140 } // end of anonymous namespace
141 
142 
143 namespace drawinglayer
144 {
145     namespace attribute
146     {
147         class ImpSdrFormTextAttribute
148         {
149         public:
150             // FormText (FontWork) Attributes
151             sal_Int32 const                               mnFormTextDistance;     // distance from line in upright direction
152             sal_Int32 const                               mnFormTextStart;        // shift from polygon start
153             sal_Int32 const                               mnFormTextShdwXVal;     // shadow distance or 10th degrees
154             sal_Int32 const                               mnFormTextShdwYVal;     // shadow distance or scaling
155             sal_uInt16 const                              mnFormTextShdwTransp;   // shadow transparence
156             XFormTextStyle const                          meFormTextStyle;        // on/off and char orientation
157             XFormTextAdjust const                         meFormTextAdjust;       // adjustment (left/right/center) and scale
158             XFormTextShadow const                         meFormTextShadow;       // shadow mode
159             Color const                                   maFormTextShdwColor;    // shadow color
160 
161             // outline attributes; used when getFormTextOutline() is true and (for
162             // shadow) when getFormTextShadow() != XFormTextShadow::NONE
163             SdrFormTextOutlineAttribute             maOutline;
164             SdrFormTextOutlineAttribute             maShadowOutline;
165 
166             bool const                                    mbFormTextMirror : 1;   // change orientation
167             bool const                                    mbFormTextOutline : 1;  // show contour of objects
168 
ImpSdrFormTextAttribute(const SfxItemSet & rSet)169             explicit ImpSdrFormTextAttribute(const SfxItemSet& rSet)
170             :   mnFormTextDistance(rSet.Get(XATTR_FORMTXTDISTANCE).GetValue()),
171                 mnFormTextStart(rSet.Get(XATTR_FORMTXTSTART).GetValue()),
172                 mnFormTextShdwXVal(rSet.Get(XATTR_FORMTXTSHDWXVAL).GetValue()),
173                 mnFormTextShdwYVal(rSet.Get(XATTR_FORMTXTSHDWYVAL).GetValue()),
174                 mnFormTextShdwTransp(rSet.Get(XATTR_FORMTXTSHDWTRANSP).GetValue()),
175                 meFormTextStyle(rSet.Get(XATTR_FORMTXTSTYLE).GetValue()),
176                 meFormTextAdjust(rSet.Get(XATTR_FORMTXTADJUST).GetValue()),
177                 meFormTextShadow(rSet.Get(XATTR_FORMTXTSHADOW).GetValue()),
178                 maFormTextShdwColor(rSet.Get(XATTR_FORMTXTSHDWCOLOR).GetColorValue()),
179                 maOutline(),
180                 maShadowOutline(),
181                 mbFormTextMirror(rSet.Get(XATTR_FORMTXTMIRROR).GetValue()),
182                 mbFormTextOutline(rSet.Get(XATTR_FORMTXTOUTLINE).GetValue())
183             {
184                 if(getFormTextOutline())
185                 {
186                     const StrokeAttribute aStrokeAttribute(impGetStrokeAttribute(rSet));
187 
188                     // also need to prepare attributes for outlines
189                     {
190                         const LineAttribute aLineAttribute(impGetLineAttribute(false, rSet));
191                         const sal_uInt8 nTransparence(impGetStrokeTransparence(false, rSet));
192 
193                         maOutline = SdrFormTextOutlineAttribute(
194                             aLineAttribute, aStrokeAttribute, nTransparence);
195                     }
196 
197                     if(XFormTextShadow::NONE != getFormTextShadow())
198                     {
199                         // also need to prepare attributes for shadow outlines
200                         const LineAttribute aLineAttribute(impGetLineAttribute(true, rSet));
201                         const sal_uInt8 nTransparence(impGetStrokeTransparence(true, rSet));
202 
203                         maShadowOutline = SdrFormTextOutlineAttribute(
204                             aLineAttribute, aStrokeAttribute, nTransparence);
205                     }
206                 }
207             }
208 
ImpSdrFormTextAttribute()209             ImpSdrFormTextAttribute()
210             :   mnFormTextDistance(0),
211                 mnFormTextStart(0),
212                 mnFormTextShdwXVal(0),
213                 mnFormTextShdwYVal(0),
214                 mnFormTextShdwTransp(0),
215                 meFormTextStyle(XFormTextStyle::NONE),
216                 meFormTextAdjust(XFormTextAdjust::Center),
217                 meFormTextShadow(XFormTextShadow::NONE),
218                 maFormTextShdwColor(),
219                 maOutline(),
220                 maShadowOutline(),
221                 mbFormTextMirror(false),
222                 mbFormTextOutline(false)
223             {
224             }
225 
226             // data read access
getFormTextDistance() const227             sal_Int32 getFormTextDistance() const { return mnFormTextDistance; }
getFormTextStart() const228             sal_Int32 getFormTextStart() const { return mnFormTextStart; }
getFormTextShdwXVal() const229             sal_Int32 getFormTextShdwXVal() const { return mnFormTextShdwXVal; }
getFormTextShdwYVal() const230             sal_Int32 getFormTextShdwYVal() const { return mnFormTextShdwYVal; }
getFormTextStyle() const231             XFormTextStyle getFormTextStyle() const { return meFormTextStyle; }
getFormTextAdjust() const232             XFormTextAdjust getFormTextAdjust() const { return meFormTextAdjust; }
getFormTextShadow() const233             XFormTextShadow getFormTextShadow() const { return meFormTextShadow; }
getFormTextShdwColor() const234             const Color& getFormTextShdwColor() const { return maFormTextShdwColor; }
getOutline() const235             const SdrFormTextOutlineAttribute& getOutline() const { return maOutline; }
getShadowOutline() const236             const SdrFormTextOutlineAttribute& getShadowOutline() const { return maShadowOutline; }
getFormTextMirror() const237             bool getFormTextMirror() const { return mbFormTextMirror; }
getFormTextOutline() const238             bool getFormTextOutline() const { return mbFormTextOutline; }
239 
240             // compare operator
operator ==(const ImpSdrFormTextAttribute & rCandidate) const241             bool operator==(const ImpSdrFormTextAttribute& rCandidate) const
242             {
243                 return (getFormTextDistance() == rCandidate.getFormTextDistance()
244                     && getFormTextStart() == rCandidate.getFormTextStart()
245                     && getFormTextShdwXVal() == rCandidate.getFormTextShdwXVal()
246                     && getFormTextShdwYVal() == rCandidate.getFormTextShdwYVal()
247                     && mnFormTextShdwTransp == rCandidate.mnFormTextShdwTransp
248                     && getFormTextStyle() == rCandidate.getFormTextStyle()
249                     && getFormTextAdjust() == rCandidate.getFormTextAdjust()
250                     && getFormTextShadow() == rCandidate.getFormTextShadow()
251                     && getFormTextShdwColor() == rCandidate.getFormTextShdwColor()
252                     && getOutline() == rCandidate.getOutline()
253                     && getShadowOutline() == rCandidate.getShadowOutline()
254                     && getFormTextMirror() == rCandidate.getFormTextMirror()
255                     && getFormTextOutline() == rCandidate.getFormTextOutline());
256             }
257         };
258 
259         namespace
260         {
261             struct theGlobalDefault :
262                 public rtl::Static< SdrFormTextAttribute::ImplType, theGlobalDefault > {};
263         }
264 
SdrFormTextAttribute(const SfxItemSet & rSet)265         SdrFormTextAttribute::SdrFormTextAttribute(const SfxItemSet& rSet)
266         :   mpSdrFormTextAttribute(ImpSdrFormTextAttribute(rSet))
267         {
268         }
269 
SdrFormTextAttribute()270         SdrFormTextAttribute::SdrFormTextAttribute()
271         :   mpSdrFormTextAttribute(theGlobalDefault::get())
272         {
273         }
274 
SdrFormTextAttribute(const SdrFormTextAttribute & rCandidate)275         SdrFormTextAttribute::SdrFormTextAttribute(const SdrFormTextAttribute& rCandidate)
276         :   mpSdrFormTextAttribute(rCandidate.mpSdrFormTextAttribute)
277         {
278         }
279 
SdrFormTextAttribute(SdrFormTextAttribute && rCandidate)280         SdrFormTextAttribute::SdrFormTextAttribute(SdrFormTextAttribute&& rCandidate) noexcept
281         :   mpSdrFormTextAttribute(std::move(rCandidate.mpSdrFormTextAttribute))
282         {
283         }
284 
~SdrFormTextAttribute()285         SdrFormTextAttribute::~SdrFormTextAttribute()
286         {
287         }
288 
isDefault() const289         bool SdrFormTextAttribute::isDefault() const
290         {
291             return mpSdrFormTextAttribute.same_object(theGlobalDefault::get());
292         }
293 
operator =(const SdrFormTextAttribute & rCandidate)294         SdrFormTextAttribute& SdrFormTextAttribute::operator=(const SdrFormTextAttribute& rCandidate)
295         {
296             mpSdrFormTextAttribute = rCandidate.mpSdrFormTextAttribute;
297             return *this;
298         }
299 
operator =(SdrFormTextAttribute && rCandidate)300         SdrFormTextAttribute& SdrFormTextAttribute::operator=(SdrFormTextAttribute&& rCandidate) noexcept
301         {
302             mpSdrFormTextAttribute = std::move(rCandidate.mpSdrFormTextAttribute);
303             return *this;
304         }
305 
operator ==(const SdrFormTextAttribute & rCandidate) const306         bool SdrFormTextAttribute::operator==(const SdrFormTextAttribute& rCandidate) const
307         {
308             // tdf#87509 default attr is always != non-default attr, even with same values
309             if(rCandidate.isDefault() != isDefault())
310                 return false;
311 
312             return rCandidate.mpSdrFormTextAttribute == mpSdrFormTextAttribute;
313         }
314 
getFormTextDistance() const315         sal_Int32 SdrFormTextAttribute::getFormTextDistance() const
316         {
317             return mpSdrFormTextAttribute->getFormTextDistance();
318         }
319 
getFormTextStart() const320         sal_Int32 SdrFormTextAttribute::getFormTextStart() const
321         {
322             return mpSdrFormTextAttribute->getFormTextStart();
323         }
324 
getFormTextShdwXVal() const325         sal_Int32 SdrFormTextAttribute::getFormTextShdwXVal() const
326         {
327             return mpSdrFormTextAttribute->getFormTextShdwXVal();
328         }
329 
getFormTextShdwYVal() const330         sal_Int32 SdrFormTextAttribute::getFormTextShdwYVal() const
331         {
332             return mpSdrFormTextAttribute->getFormTextShdwYVal();
333         }
334 
getFormTextStyle() const335         XFormTextStyle SdrFormTextAttribute::getFormTextStyle() const
336         {
337             return mpSdrFormTextAttribute->getFormTextStyle();
338         }
339 
getFormTextAdjust() const340         XFormTextAdjust SdrFormTextAttribute::getFormTextAdjust() const
341         {
342             return mpSdrFormTextAttribute->getFormTextAdjust();
343         }
344 
getFormTextShadow() const345         XFormTextShadow SdrFormTextAttribute::getFormTextShadow() const
346         {
347             return mpSdrFormTextAttribute->getFormTextShadow();
348         }
349 
getFormTextShdwColor() const350         Color const & SdrFormTextAttribute::getFormTextShdwColor() const
351         {
352             return mpSdrFormTextAttribute->getFormTextShdwColor();
353         }
354 
getOutline() const355         const SdrFormTextOutlineAttribute& SdrFormTextAttribute::getOutline() const
356         {
357             return mpSdrFormTextAttribute->getOutline();
358         }
359 
getShadowOutline() const360         const SdrFormTextOutlineAttribute& SdrFormTextAttribute::getShadowOutline() const
361         {
362             return mpSdrFormTextAttribute->getShadowOutline();
363         }
364 
getFormTextMirror() const365         bool SdrFormTextAttribute::getFormTextMirror() const
366         {
367             return mpSdrFormTextAttribute->getFormTextMirror();
368         }
369 
getFormTextOutline() const370         bool SdrFormTextAttribute::getFormTextOutline() const
371         {
372             return mpSdrFormTextAttribute->getFormTextOutline();
373         }
374     } // end of namespace attribute
375 } // end of namespace drawinglayer
376 
377 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
378