1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_svx.hxx"
25 
26 #include <svx/sdr/attribute/sdrformtextattribute.hxx>
27 #include <basegfx/vector/b2enums.hxx>
28 #include <svl/itemset.hxx>
29 #include <svx/xftdiit.hxx>
30 #include <svx/xftstit.hxx>
31 #include <svx/xftshxy.hxx>
32 #include <svx/xftshtit.hxx>
33 #include <svx/xtextit0.hxx>
34 #include <svx/xftadit.hxx>
35 #include <svx/xftshit.hxx>
36 #include <svx/xftshcit.hxx>
37 #include <svx/xftmrit.hxx>
38 #include <svx/xftouit.hxx>
39 #include <svx/sdshtitm.hxx>
40 #include <svx/xlntrit.hxx>
41 #include <svx/sdshcitm.hxx>
42 #include <svx/xlnclit.hxx>
43 #include <svx/xlnwtit.hxx>
44 #include <svx/xlinjoit.hxx>
45 #include <svx/xlineit0.hxx>
46 #include <svx/xdash.hxx>
47 #include <svx/xlndsit.hxx>
48 #include <drawinglayer/attribute/lineattribute.hxx>
49 #include <drawinglayer/attribute/strokeattribute.hxx>
50 #include <svx/sdr/attribute/sdrformtextoutlineattribute.hxx>
51 
52 //////////////////////////////////////////////////////////////////////////////
53 // helper to get line, stroke and transparence attributes from SfxItemSet
54 
55 namespace
56 {
57 	basegfx::B2DLineJoin impGetB2DLineJoin(XLineJoint eLineJoint)
58 	{
59 		switch(eLineJoint)
60 		{
61 			case XLINEJOINT_MIDDLE	:
62 			{
63 				return basegfx::B2DLINEJOIN_MIDDLE;
64 			}
65 			case XLINEJOINT_BEVEL	:
66 			{
67 				return basegfx::B2DLINEJOIN_BEVEL;
68 			}
69 			case XLINEJOINT_MITER	:
70 			{
71 				return basegfx::B2DLINEJOIN_MITER;
72 			}
73 			case XLINEJOINT_ROUND	:
74 			{
75 				return basegfx::B2DLINEJOIN_ROUND;
76 			}
77 			default :
78 			{
79 				return basegfx::B2DLINEJOIN_NONE; // XLINEJOINT_NONE
80 			}
81 		}
82 	}
83 
84     sal_uInt8 impGetStrokeTransparence(bool bShadow, const SfxItemSet& rSet)
85 	{
86 		sal_uInt8 nRetval;
87 
88 		if(bShadow)
89 		{
90 			nRetval = (sal_uInt8)((((SdrShadowTransparenceItem&)(rSet.Get(SDRATTR_SHADOWTRANSPARENCE))).GetValue() * 255) / 100);
91 		}
92 		else
93 		{
94 			nRetval = (sal_uInt8)((((XLineTransparenceItem&)(rSet.Get(XATTR_LINETRANSPARENCE))).GetValue() * 255) / 100);
95 		}
96 
97 		return nRetval;
98 	}
99 
100 	drawinglayer::attribute::LineAttribute impGetLineAttribute(bool bShadow, const SfxItemSet& rSet)
101 	{
102 		basegfx::BColor aColorAttribute;
103 
104 		if(bShadow)
105 		{
106 			const Color aShadowColor(((SdrShadowColorItem&)(rSet.Get(SDRATTR_SHADOWCOLOR))).GetColorValue());
107 			aColorAttribute = aShadowColor.getBColor();
108 		}
109 		else
110 		{
111 			const Color aLineColor(((XLineColorItem&)(rSet.Get(XATTR_LINECOLOR))).GetColorValue());
112 			aColorAttribute = aLineColor.getBColor();
113 		}
114 
115 		const sal_uInt32 nLineWidth = ((const XLineWidthItem&)(rSet.Get(XATTR_LINEWIDTH))).GetValue();
116 		const XLineJoint eLineJoint = ((const XLineJointItem&)(rSet.Get(XATTR_LINEJOINT))).GetValue();
117 
118 		return drawinglayer::attribute::LineAttribute(aColorAttribute, (double)nLineWidth, impGetB2DLineJoin(eLineJoint));
119 	}
120 
121     drawinglayer::attribute::StrokeAttribute impGetStrokeAttribute(const SfxItemSet& rSet)
122 	{
123 		const XLineStyle eLineStyle = ((XLineStyleItem&)(rSet.Get(XATTR_LINESTYLE))).GetValue();
124 		double fFullDotDashLen(0.0);
125 		::std::vector< double > aDotDashArray;
126 
127 		if(XLINE_DASH == eLineStyle)
128 		{
129 			const XDash& rDash = ((const XLineDashItem&)(rSet.Get(XATTR_LINEDASH))).GetDashValue();
130 
131 			if(rDash.GetDots() || rDash.GetDashes())
132 			{
133         		const sal_uInt32 nLineWidth = ((const XLineWidthItem&)(rSet.Get(XATTR_LINEWIDTH))).GetValue();
134 				fFullDotDashLen = rDash.CreateDotDashArray(aDotDashArray, (double)nLineWidth);
135 			}
136 		}
137 
138 		return drawinglayer::attribute::StrokeAttribute(aDotDashArray, fFullDotDashLen);
139 	}
140 } // end of anonymous namespace
141 
142 //////////////////////////////////////////////////////////////////////////////
143 
144 namespace drawinglayer
145 {
146 	namespace attribute
147 	{
148 	    class ImpSdrFormTextAttribute
149 	    {
150 		public:
151 			// refcounter
152 			sal_uInt32								mnRefCount;
153 
154             // FormText (FontWork) Attributes
155 		    sal_Int32								mnFormTextDistance;		// distance from line in upright direction
156 		    sal_Int32								mnFormTextStart;		// shift from polygon start
157 		    sal_Int32								mnFormTextShdwXVal;		// shadow distance or 10th degrees
158 		    sal_Int32								mnFormTextShdwYVal;		// shadow distance or scaling
159 		    sal_uInt16								mnFormTextShdwTransp;	// shadow transparence
160 		    XFormTextStyle							meFormTextStyle;		// on/off and char orientation
161 		    XFormTextAdjust							meFormTextAdjust;		// adjustment (left/right/center) and scale
162 		    XFormTextShadow							meFormTextShadow;		// shadow mode
163 		    Color									maFormTextShdwColor;	// shadow color
164 
165             // outline attributes; used when getFormTextOutline() is true and (for
166             // shadow) when getFormTextShadow() != XFTSHADOW_NONE
167             SdrFormTextOutlineAttribute				maOutline;
168             SdrFormTextOutlineAttribute				maShadowOutline;
169 
170 		    // bitfield
171 		    unsigned								mbFormTextMirror : 1;	// change orientation
172 		    unsigned								mbFormTextOutline : 1;	// show contour of objects
173 
174 		    ImpSdrFormTextAttribute(const SfxItemSet& rSet)
175 			:	mnRefCount(0),
176 				mnFormTextDistance(((const XFormTextDistanceItem&)rSet.Get(XATTR_FORMTXTDISTANCE)).GetValue()),
177 				mnFormTextStart(((const XFormTextStartItem&)rSet.Get(XATTR_FORMTXTSTART)).GetValue()),
178 				mnFormTextShdwXVal(((const XFormTextShadowXValItem&)rSet.Get(XATTR_FORMTXTSHDWXVAL)).GetValue()),
179 				mnFormTextShdwYVal(((const XFormTextShadowYValItem&)rSet.Get(XATTR_FORMTXTSHDWYVAL)).GetValue()),
180 				mnFormTextShdwTransp(((const XFormTextShadowTranspItem&)rSet.Get(XATTR_FORMTXTSHDWTRANSP)).GetValue()),
181 				meFormTextStyle(((const XFormTextStyleItem&)rSet.Get(XATTR_FORMTXTSTYLE)).GetValue()),
182 				meFormTextAdjust(((const XFormTextAdjustItem&)rSet.Get(XATTR_FORMTXTADJUST)).GetValue()),
183 				meFormTextShadow(((const XFormTextShadowItem&)rSet.Get(XATTR_FORMTXTSHADOW)).GetValue()),
184 				maFormTextShdwColor(((const XFormTextShadowColorItem&)rSet.Get(XATTR_FORMTXTSHDWCOLOR)).GetColorValue()),
185 				maOutline(),
186 				maShadowOutline(),
187 				mbFormTextMirror(((const XFormTextMirrorItem&)rSet.Get(XATTR_FORMTXTMIRROR)).GetValue()),
188 				mbFormTextOutline(((const XFormTextOutlineItem&)rSet.Get(XATTR_FORMTXTOUTLINE)).GetValue())
189 			{
190 				if(getFormTextOutline())
191 				{
192 					const StrokeAttribute aStrokeAttribute(impGetStrokeAttribute(rSet));
193 
194 					// also need to prepare attributes for outlines
195 					{
196 						const LineAttribute aLineAttribute(impGetLineAttribute(false, rSet));
197 						const sal_uInt8 nTransparence(impGetStrokeTransparence(false, rSet));
198 
199 						maOutline = SdrFormTextOutlineAttribute(
200 							aLineAttribute, aStrokeAttribute, nTransparence);
201 					}
202 
203 					if(XFTSHADOW_NONE != getFormTextShadow())
204 					{
205 						// also need to prepare attributes for shadow outlines
206 						const LineAttribute aLineAttribute(impGetLineAttribute(true, rSet));
207 						const sal_uInt8 nTransparence(impGetStrokeTransparence(true, rSet));
208 
209 						maShadowOutline = SdrFormTextOutlineAttribute(
210 							aLineAttribute, aStrokeAttribute, nTransparence);
211 					}
212 				}
213 			}
214 
215 			ImpSdrFormTextAttribute()
216 			:	mnRefCount(0),
217 				mnFormTextDistance(0),
218 				mnFormTextStart(0),
219 				mnFormTextShdwXVal(0),
220 				mnFormTextShdwYVal(0),
221 				mnFormTextShdwTransp(0),
222 				meFormTextStyle(XFT_NONE),
223 				meFormTextAdjust(XFT_CENTER),
224 				meFormTextShadow(XFTSHADOW_NONE),
225 				maFormTextShdwColor(),
226 				maOutline(),
227 				maShadowOutline(),
228 				mbFormTextMirror(false),
229 				mbFormTextOutline(false)
230 			{
231 			}
232 
233 		    // data read access
234 		    sal_Int32 getFormTextDistance() const { return mnFormTextDistance; }
235 		    sal_Int32 getFormTextStart() const { return mnFormTextStart; }
236 		    sal_Int32 getFormTextShdwXVal() const { return mnFormTextShdwXVal; }
237 		    sal_Int32 getFormTextShdwYVal() const { return mnFormTextShdwYVal; }
238 		    sal_uInt16 getFormTextShdwTransp() const { return mnFormTextShdwTransp; }
239 		    XFormTextStyle getFormTextStyle() const { return meFormTextStyle; }
240 		    XFormTextAdjust getFormTextAdjust() const { return meFormTextAdjust; }
241 		    XFormTextShadow getFormTextShadow() const { return meFormTextShadow; }
242 		    Color getFormTextShdwColor() const { return maFormTextShdwColor; }
243             const SdrFormTextOutlineAttribute& getOutline() const { return maOutline; }
244             const SdrFormTextOutlineAttribute& getShadowOutline() const { return maShadowOutline; }
245             bool getFormTextMirror() const { return mbFormTextMirror; }
246 		    bool getFormTextOutline() const { return mbFormTextOutline; }
247 
248             // compare operator
249 			bool operator==(const ImpSdrFormTextAttribute& rCandidate) const
250 			{
251 				return (getFormTextDistance() == rCandidate.getFormTextDistance()
252 					&& getFormTextStart() == rCandidate.getFormTextStart()
253 					&& getFormTextShdwXVal() == rCandidate.getFormTextShdwXVal()
254 					&& getFormTextShdwYVal() == rCandidate.getFormTextShdwYVal()
255 					&& getFormTextShdwTransp() == rCandidate.getFormTextShdwTransp()
256 					&& getFormTextStyle() == rCandidate.getFormTextStyle()
257 					&& getFormTextAdjust() == rCandidate.getFormTextAdjust()
258 					&& getFormTextShadow() == rCandidate.getFormTextShadow()
259 					&& getFormTextShdwColor() == rCandidate.getFormTextShdwColor()
260 					&& getOutline() == rCandidate.getOutline()
261 					&& getShadowOutline() == rCandidate.getShadowOutline()
262 					&& getFormTextMirror() == rCandidate.getFormTextMirror()
263 					&& getFormTextOutline() == rCandidate.getFormTextOutline());
264 			}
265 
266             static ImpSdrFormTextAttribute* get_global_default()
267             {
268                 static ImpSdrFormTextAttribute* pDefault = 0;
269 
270                 if(!pDefault)
271                 {
272                     pDefault = new ImpSdrFormTextAttribute();
273 
274                     // never delete; start with RefCount 1, not 0
275     			    pDefault->mnRefCount++;
276                 }
277 
278                 return pDefault;
279             }
280 	    };
281 
282         SdrFormTextAttribute::SdrFormTextAttribute(const SfxItemSet& rSet)
283 		:	mpSdrFormTextAttribute(new ImpSdrFormTextAttribute(rSet))
284 		{
285 		}
286 
287 		SdrFormTextAttribute::SdrFormTextAttribute()
288         :	mpSdrFormTextAttribute(ImpSdrFormTextAttribute::get_global_default())
289 		{
290 			mpSdrFormTextAttribute->mnRefCount++;
291 		}
292 
293         SdrFormTextAttribute::SdrFormTextAttribute(const SdrFormTextAttribute& rCandidate)
294 		:	mpSdrFormTextAttribute(rCandidate.mpSdrFormTextAttribute)
295 		{
296 			mpSdrFormTextAttribute->mnRefCount++;
297 		}
298 
299 		SdrFormTextAttribute::~SdrFormTextAttribute()
300 		{
301 			if(mpSdrFormTextAttribute->mnRefCount)
302 			{
303 				mpSdrFormTextAttribute->mnRefCount--;
304 			}
305 			else
306 			{
307 				delete mpSdrFormTextAttribute;
308 			}
309 		}
310 
311         bool SdrFormTextAttribute::isDefault() const
312         {
313             return mpSdrFormTextAttribute == ImpSdrFormTextAttribute::get_global_default();
314         }
315 
316         SdrFormTextAttribute& SdrFormTextAttribute::operator=(const SdrFormTextAttribute& rCandidate)
317 		{
318 			if(rCandidate.mpSdrFormTextAttribute != mpSdrFormTextAttribute)
319 			{
320 				if(mpSdrFormTextAttribute->mnRefCount)
321 				{
322 					mpSdrFormTextAttribute->mnRefCount--;
323 				}
324 				else
325 				{
326 					delete mpSdrFormTextAttribute;
327 				}
328 
329 				mpSdrFormTextAttribute = rCandidate.mpSdrFormTextAttribute;
330 				mpSdrFormTextAttribute->mnRefCount++;
331 			}
332 
333 			return *this;
334 		}
335 
336 		bool SdrFormTextAttribute::operator==(const SdrFormTextAttribute& rCandidate) const
337 		{
338 			if(rCandidate.mpSdrFormTextAttribute == mpSdrFormTextAttribute)
339 			{
340 				return true;
341 			}
342 
343 			if(rCandidate.isDefault() != isDefault())
344 			{
345 				return false;
346 			}
347 
348 			return (*rCandidate.mpSdrFormTextAttribute == *mpSdrFormTextAttribute);
349 		}
350 
351 	    sal_Int32 SdrFormTextAttribute::getFormTextDistance() const
352 		{
353 			return mpSdrFormTextAttribute->getFormTextDistance();
354 		}
355 
356 		sal_Int32 SdrFormTextAttribute::getFormTextStart() const
357 		{
358 			return mpSdrFormTextAttribute->getFormTextStart();
359 		}
360 
361 		sal_Int32 SdrFormTextAttribute::getFormTextShdwXVal() const
362 		{
363 			return mpSdrFormTextAttribute->getFormTextShdwXVal();
364 		}
365 
366 		sal_Int32 SdrFormTextAttribute::getFormTextShdwYVal() const
367 		{
368 			return mpSdrFormTextAttribute->getFormTextShdwYVal();
369 		}
370 
371 		sal_uInt16 SdrFormTextAttribute::getFormTextShdwTransp() const
372 		{
373 			return mpSdrFormTextAttribute->getFormTextShdwTransp();
374 		}
375 
376 		XFormTextStyle SdrFormTextAttribute::getFormTextStyle() const
377 		{
378 			return mpSdrFormTextAttribute->getFormTextStyle();
379 		}
380 
381 		XFormTextAdjust SdrFormTextAttribute::getFormTextAdjust() const
382 		{
383 			return mpSdrFormTextAttribute->getFormTextAdjust();
384 		}
385 
386 		XFormTextShadow SdrFormTextAttribute::getFormTextShadow() const
387 		{
388 			return mpSdrFormTextAttribute->getFormTextShadow();
389 		}
390 
391 		Color SdrFormTextAttribute::getFormTextShdwColor() const
392 		{
393 			return mpSdrFormTextAttribute->getFormTextShdwColor();
394 		}
395 
396 		const SdrFormTextOutlineAttribute& SdrFormTextAttribute::getOutline() const
397 		{
398 			return mpSdrFormTextAttribute->getOutline();
399 		}
400 
401 		const SdrFormTextOutlineAttribute& SdrFormTextAttribute::getShadowOutline() const
402 		{
403 			return mpSdrFormTextAttribute->getShadowOutline();
404 		}
405 
406 		bool SdrFormTextAttribute::getFormTextMirror() const
407 		{
408 			return mpSdrFormTextAttribute->getFormTextMirror();
409 		}
410 
411 		bool SdrFormTextAttribute::getFormTextOutline() const
412 		{
413 			return mpSdrFormTextAttribute->getFormTextOutline();
414 		}
415 	} // end of namespace attribute
416 } // end of namespace drawinglayer
417 
418 //////////////////////////////////////////////////////////////////////////////
419 // eof
420