1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_drawinglayer.hxx"
30 
31 #include <drawinglayer/primitive2d/texteffectprimitive2d.hxx>
32 #include <drawinglayer/geometry/viewinformation2d.hxx>
33 #include <drawinglayer/primitive2d/modifiedcolorprimitive2d.hxx>
34 #include <drawinglayer/primitive2d/transformprimitive2d.hxx>
35 #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
36 #include <basegfx/matrix/b2dhommatrixtools.hxx>
37 
38 //////////////////////////////////////////////////////////////////////////////
39 
40 namespace drawinglayer
41 {
42 	namespace primitive2d
43 	{
44 		static double fDiscreteSize(1.1);
45 
46 		Primitive2DSequence TextEffectPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
47 		{
48 			Primitive2DSequence aRetval;
49 
50 			// get the distance of one discrete units from target display. Use between 1.0 and sqrt(2) to
51 			// have good results on rotated objects, too
52 			const basegfx::B2DVector aDistance(rViewInformation.getInverseObjectToViewTransformation() *
53                 basegfx::B2DVector(fDiscreteSize, fDiscreteSize));
54 			const basegfx::B2DVector aDiagonalDistance(aDistance * (1.0 / 1.44));
55 
56 			switch(getTextEffectStyle2D())
57 			{
58 				case TEXTEFFECTSTYLE2D_RELIEF_EMBOSSED:
59 				case TEXTEFFECTSTYLE2D_RELIEF_ENGRAVED:
60 				case TEXTEFFECTSTYLE2D_RELIEF_EMBOSSED_DEFAULT:
61 				case TEXTEFFECTSTYLE2D_RELIEF_ENGRAVED_DEFAULT:
62 				{
63 					// prepare transform of sub-group back to (0,0) and align to X-Axis
64                     basegfx::B2DHomMatrix aBackTransform(basegfx::tools::createTranslateB2DHomMatrix(
65                         -getRotationCenter().getX(), -getRotationCenter().getY()));
66 					aBackTransform.rotate(-getDirection());
67 
68 					// prepare transform of sub-group back to it's position and rotation
69                     basegfx::B2DHomMatrix aForwardTransform(basegfx::tools::createRotateB2DHomMatrix(getDirection()));
70 					aForwardTransform.translate(getRotationCenter().getX(), getRotationCenter().getY());
71 
72 					// create transformation for one discrete unit
73 					const bool bEmbossed(
74 						TEXTEFFECTSTYLE2D_RELIEF_EMBOSSED  == getTextEffectStyle2D()
75 						|| TEXTEFFECTSTYLE2D_RELIEF_EMBOSSED_DEFAULT == getTextEffectStyle2D());
76 					const bool bDefaultTextColor(
77 						TEXTEFFECTSTYLE2D_RELIEF_EMBOSSED_DEFAULT == getTextEffectStyle2D()
78 						|| TEXTEFFECTSTYLE2D_RELIEF_ENGRAVED_DEFAULT == getTextEffectStyle2D());
79 					basegfx::B2DHomMatrix aTransform(aBackTransform);
80 					aRetval.realloc(2);
81 
82 					if(bEmbossed)
83 					{
84 						// to bottom-right
85 						aTransform.translate(aDiagonalDistance.getX(), aDiagonalDistance.getY());
86 					}
87 					else
88 					{
89 						// to top-left
90 						aTransform.translate(-aDiagonalDistance.getX(), -aDiagonalDistance.getY());
91 					}
92 
93 					aTransform *= aForwardTransform;
94 
95 					if(bDefaultTextColor)
96 					{
97 						// emboss/engrave in black, original forced to white
98 						const basegfx::BColorModifier aBColorModifierToGray(basegfx::BColor(0.0));
99 						const Primitive2DReference xModifiedColor(new ModifiedColorPrimitive2D(getTextContent(), aBColorModifierToGray));
100 						aRetval[0] = Primitive2DReference(new TransformPrimitive2D(aTransform, Primitive2DSequence(&xModifiedColor, 1)));
101 
102 						// add original, too
103 						const basegfx::BColorModifier aBColorModifierToWhite(basegfx::BColor(1.0));
104 						aRetval[1] = Primitive2DReference(new ModifiedColorPrimitive2D(getTextContent(), aBColorModifierToWhite));
105 					}
106 					else
107 					{
108 						// emboss/engrave in gray, keep original's color
109 						const basegfx::BColorModifier aBColorModifierToGray(basegfx::BColor(0.75)); // 192
110 						const Primitive2DReference xModifiedColor(new ModifiedColorPrimitive2D(getTextContent(), aBColorModifierToGray));
111 						aRetval[0] = Primitive2DReference(new TransformPrimitive2D(aTransform, Primitive2DSequence(&xModifiedColor, 1)));
112 
113 						// add original, too
114 						aRetval[1] = Primitive2DReference(new GroupPrimitive2D(getTextContent()));
115 					}
116 
117 					break;
118 				}
119 				case TEXTEFFECTSTYLE2D_OUTLINE:
120 				{
121 					// create transform primitives in all directions
122 					basegfx::B2DHomMatrix aTransform;
123 					aRetval.realloc(9);
124 
125 					aTransform.set(0, 2, aDistance.getX());
126 					aTransform.set(1, 2, 0.0);
127 					aRetval[0] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
128 
129 					aTransform.set(0, 2, aDiagonalDistance.getX());
130 					aTransform.set(1, 2, aDiagonalDistance.getY());
131 					aRetval[1] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
132 
133 					aTransform.set(0, 2, 0.0);
134 					aTransform.set(1, 2, aDistance.getY());
135 					aRetval[2] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
136 
137 					aTransform.set(0, 2, -aDiagonalDistance.getX());
138 					aTransform.set(1, 2, aDiagonalDistance.getY());
139 					aRetval[3] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
140 
141 					aTransform.set(0, 2, -aDistance.getX());
142 					aTransform.set(1, 2, 0.0);
143 					aRetval[4] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
144 
145 					aTransform.set(0, 2, -aDiagonalDistance.getX());
146 					aTransform.set(1, 2, -aDiagonalDistance.getY());
147 					aRetval[5] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
148 
149 					aTransform.set(0, 2, 0.0);
150 					aTransform.set(1, 2, -aDistance.getY());
151 					aRetval[6] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
152 
153 					aTransform.set(0, 2, aDiagonalDistance.getX());
154 					aTransform.set(1, 2, -aDiagonalDistance.getY());
155 					aRetval[7] = Primitive2DReference(new TransformPrimitive2D(aTransform, getTextContent()));
156 
157 					// at last, place original over it, but force to white
158 					const basegfx::BColorModifier aBColorModifierToWhite(basegfx::BColor(1.0, 1.0, 1.0));
159 					aRetval[8] = Primitive2DReference(new ModifiedColorPrimitive2D(getTextContent(), aBColorModifierToWhite));
160 
161 					break;
162 				}
163 			}
164 
165             return aRetval;
166 		}
167 
168 		TextEffectPrimitive2D::TextEffectPrimitive2D(
169             const Primitive2DSequence& rTextContent,
170 			const basegfx::B2DPoint& rRotationCenter,
171 			double fDirection,
172             TextEffectStyle2D eTextEffectStyle2D)
173 		:	BufferedDecompositionPrimitive2D(),
174             maTextContent(rTextContent),
175 			maRotationCenter(rRotationCenter),
176 			mfDirection(fDirection),
177             meTextEffectStyle2D(eTextEffectStyle2D)
178 		{
179 		}
180 
181 		bool TextEffectPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
182 		{
183 			if(BasePrimitive2D::operator==(rPrimitive))
184 			{
185 				const TextEffectPrimitive2D& rCompare = (TextEffectPrimitive2D&)rPrimitive;
186 
187 				return (getTextContent() == rCompare.getTextContent()
188                     && getRotationCenter() == rCompare.getRotationCenter()
189 					&& getDirection() == rCompare.getDirection()
190 					&& getTextEffectStyle2D() == rCompare.getTextEffectStyle2D());
191 			}
192 
193 			return false;
194 		}
195 
196 		basegfx::B2DRange TextEffectPrimitive2D::getB2DRange(const geometry::ViewInformation2D& rViewInformation) const
197 		{
198 			// get range of content and grow by used fDiscreteSize. That way it is not necessary to ask
199 			// the whole decomposition for it's ranges (which may be expensive with outline mode which
200 			// then will ask 9 times at nearly the same content. This may even be refined here using the
201 			// TextEffectStyle information, e.g. for TEXTEFFECTSTYLE2D_RELIEF the grow needs only to
202 			// be in two directions
203 			basegfx::B2DRange aRetval(getB2DRangeFromPrimitive2DSequence(getTextContent(), rViewInformation));
204 			aRetval.grow(fDiscreteSize);
205 
206 			return aRetval;
207 		}
208 
209 		Primitive2DSequence TextEffectPrimitive2D::get2DDecomposition(const geometry::ViewInformation2D& rViewInformation) const
210 		{
211 			::osl::MutexGuard aGuard( m_aMutex );
212 
213 			if(getBuffered2DDecomposition().hasElements())
214 			{
215 				if(maLastObjectToViewTransformation != rViewInformation.getObjectToViewTransformation())
216 				{
217 					// conditions of last local decomposition have changed, delete
218 					const_cast< TextEffectPrimitive2D* >(this)->setBuffered2DDecomposition(Primitive2DSequence());
219 				}
220 			}
221 
222 			if(!getBuffered2DDecomposition().hasElements())
223 			{
224 				// remember ViewRange and ViewTransformation
225 				const_cast< TextEffectPrimitive2D* >(this)->maLastObjectToViewTransformation = rViewInformation.getObjectToViewTransformation();
226 			}
227 
228 			// use parent implementation
229 			return BufferedDecompositionPrimitive2D::get2DDecomposition(rViewInformation);
230 		}
231 
232 		// provide unique ID
233 		ImplPrimitrive2DIDBlock(TextEffectPrimitive2D, PRIMITIVE2D_ID_TEXTEFFECTPRIMITIVE2D)
234 
235 	} // end of namespace primitive2d
236 } // end of namespace drawinglayer
237 
238 //////////////////////////////////////////////////////////////////////////////
239 // eof
240