1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_drawinglayer.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include <drawinglayer/primitive2d/textprimitive2d.hxx>
32*cdf0e10cSrcweir #include <drawinglayer/primitive2d/textlayoutdevice.hxx>
33*cdf0e10cSrcweir #include <basegfx/polygon/b2dpolypolygon.hxx>
34*cdf0e10cSrcweir #include <drawinglayer/primitive2d/polypolygonprimitive2d.hxx>
35*cdf0e10cSrcweir #include <drawinglayer/primitive2d/drawinglayer_primitivetypes2d.hxx>
36*cdf0e10cSrcweir #include <drawinglayer/primitive2d/texteffectprimitive2d.hxx>
37*cdf0e10cSrcweir #include <basegfx/matrix/b2dhommatrixtools.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir using namespace com::sun::star;
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace
46*cdf0e10cSrcweir {
47*cdf0e10cSrcweir 	// adapts fontScale for usage with TextLayouter. Input is rScale which is the extracted
48*cdf0e10cSrcweir 	// scale from a text transformation. A copy is modified so that it contains only positive
49*cdf0e10cSrcweir     // scalings and XY-equal scalings to allow to get a non-X-scaled Vcl-Font for TextLayouter.
50*cdf0e10cSrcweir     // rScale is adapted accordingly to contain the corrected scale which would need to be
51*cdf0e10cSrcweir     // applied to e.g. outlines received from TextLayouter under usage of fontScale. This
52*cdf0e10cSrcweir     // includes Y-Scale, X-Scale-correction and mirrorings.
53*cdf0e10cSrcweir     basegfx::B2DVector getCorrectedScaleAndFontScale(basegfx::B2DVector& rScale)
54*cdf0e10cSrcweir 	{
55*cdf0e10cSrcweir         // copy input value
56*cdf0e10cSrcweir         basegfx::B2DVector aFontScale(rScale);
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir         // correct FontHeight settings
59*cdf0e10cSrcweir         if(basegfx::fTools::equalZero(aFontScale.getY()))
60*cdf0e10cSrcweir         {
61*cdf0e10cSrcweir             // no font height; choose one and adapt scale to get back to original scaling
62*cdf0e10cSrcweir             static double fDefaultFontScale(100.0);
63*cdf0e10cSrcweir             rScale.setY(1.0 / fDefaultFontScale);
64*cdf0e10cSrcweir             aFontScale.setY(fDefaultFontScale);
65*cdf0e10cSrcweir         }
66*cdf0e10cSrcweir         else if(basegfx::fTools::less(aFontScale.getY(), 0.0))
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             // negative font height; invert and adapt scale to get back to original scaling
69*cdf0e10cSrcweir             aFontScale.setY(-aFontScale.getY());
70*cdf0e10cSrcweir             rScale.setY(-1.0);
71*cdf0e10cSrcweir         }
72*cdf0e10cSrcweir         else
73*cdf0e10cSrcweir         {
74*cdf0e10cSrcweir             // positive font height; adapt scale; scaling will be part of the polygons
75*cdf0e10cSrcweir             rScale.setY(1.0);
76*cdf0e10cSrcweir         }
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir         // correct FontWidth settings
79*cdf0e10cSrcweir         if(basegfx::fTools::equal(aFontScale.getX(), aFontScale.getY()))
80*cdf0e10cSrcweir         {
81*cdf0e10cSrcweir             // no FontScale, adapt scale
82*cdf0e10cSrcweir             rScale.setX(1.0);
83*cdf0e10cSrcweir         }
84*cdf0e10cSrcweir         else
85*cdf0e10cSrcweir         {
86*cdf0e10cSrcweir             // If FontScale is used, force to no FontScale to get a non-scaled VCL font.
87*cdf0e10cSrcweir             // Adapt scaling in X accordingly.
88*cdf0e10cSrcweir             rScale.setX(aFontScale.getX() / aFontScale.getY());
89*cdf0e10cSrcweir             aFontScale.setX(aFontScale.getY());
90*cdf0e10cSrcweir         }
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir         return aFontScale;
93*cdf0e10cSrcweir 	}
94*cdf0e10cSrcweir } // end of anonymous namespace
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir namespace drawinglayer
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir 	namespace primitive2d
101*cdf0e10cSrcweir 	{
102*cdf0e10cSrcweir         void TextSimplePortionPrimitive2D::getTextOutlinesAndTransformation(basegfx::B2DPolyPolygonVector& rTarget, basegfx::B2DHomMatrix& rTransformation) const
103*cdf0e10cSrcweir         {
104*cdf0e10cSrcweir 			if(getTextLength())
105*cdf0e10cSrcweir 			{
106*cdf0e10cSrcweir 				// decompose object transformation to single values
107*cdf0e10cSrcweir 				basegfx::B2DVector aScale, aTranslate;
108*cdf0e10cSrcweir 				double fRotate, fShearX;
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 				// if decomposition returns false, create no geometry since e.g. scaling may
111*cdf0e10cSrcweir 				// be zero
112*cdf0e10cSrcweir 				if(getTextTransform().decompose(aScale, aTranslate, fRotate, fShearX))
113*cdf0e10cSrcweir 				{
114*cdf0e10cSrcweir 					// handle special case: If scale is negative in (x,y) (3rd quadrant), it can
115*cdf0e10cSrcweir 					// be expressed as rotation by PI
116*cdf0e10cSrcweir 					if(basegfx::fTools::less(aScale.getX(), 0.0) && basegfx::fTools::less(aScale.getY(), 0.0))
117*cdf0e10cSrcweir 					{
118*cdf0e10cSrcweir 						aScale = basegfx::absolute(aScale);
119*cdf0e10cSrcweir 						fRotate += F_PI;
120*cdf0e10cSrcweir 					}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir 					// for the TextLayouterDevice, it is necessary to have a scaling representing
123*cdf0e10cSrcweir 					// the font size. Since we want to extract polygons here, it is okay to
124*cdf0e10cSrcweir 					// work just with scaling and to ignore shear, rotation and translation,
125*cdf0e10cSrcweir 					// all that can be applied to the polygons later
126*cdf0e10cSrcweir 					const basegfx::B2DVector aFontScale(getCorrectedScaleAndFontScale(aScale));
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir 					// prepare textlayoutdevice
129*cdf0e10cSrcweir 					TextLayouterDevice aTextLayouter;
130*cdf0e10cSrcweir 					aTextLayouter.setFontAttribute(
131*cdf0e10cSrcweir                         getFontAttribute(),
132*cdf0e10cSrcweir                         aFontScale.getX(),
133*cdf0e10cSrcweir                         aFontScale.getY(),
134*cdf0e10cSrcweir                         getLocale());
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir                     // When getting outlines from stretched text (aScale.getX() != 1.0) it
137*cdf0e10cSrcweir                     // is necessary to inverse-scale the DXArray (if used) to not get the
138*cdf0e10cSrcweir                     // outlines already aligned to given, but wrong DXArray
139*cdf0e10cSrcweir                     if(getDXArray().size() && !basegfx::fTools::equal(aScale.getX(), 1.0))
140*cdf0e10cSrcweir                     {
141*cdf0e10cSrcweir             			::std::vector< double > aScaledDXArray = getDXArray();
142*cdf0e10cSrcweir                         const double fDXArrayScale(1.0 / aScale.getX());
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir                         for(sal_uInt32 a(0); a < aScaledDXArray.size(); a++)
145*cdf0e10cSrcweir                         {
146*cdf0e10cSrcweir                             aScaledDXArray[a] *= fDXArrayScale;
147*cdf0e10cSrcweir                         }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir                         // get the text outlines
150*cdf0e10cSrcweir 					    aTextLayouter.getTextOutlines(
151*cdf0e10cSrcweir                             rTarget,
152*cdf0e10cSrcweir                             getText(),
153*cdf0e10cSrcweir                             getTextPosition(),
154*cdf0e10cSrcweir                             getTextLength(),
155*cdf0e10cSrcweir                             aScaledDXArray);
156*cdf0e10cSrcweir                     }
157*cdf0e10cSrcweir                     else
158*cdf0e10cSrcweir                     {
159*cdf0e10cSrcweir                         // get the text outlines
160*cdf0e10cSrcweir 					    aTextLayouter.getTextOutlines(
161*cdf0e10cSrcweir                             rTarget,
162*cdf0e10cSrcweir                             getText(),
163*cdf0e10cSrcweir                             getTextPosition(),
164*cdf0e10cSrcweir                             getTextLength(),
165*cdf0e10cSrcweir                             getDXArray());
166*cdf0e10cSrcweir                     }
167*cdf0e10cSrcweir 
168*cdf0e10cSrcweir 					// create primitives for the outlines
169*cdf0e10cSrcweir 					const sal_uInt32 nCount(rTarget.size());
170*cdf0e10cSrcweir 
171*cdf0e10cSrcweir 					if(nCount)
172*cdf0e10cSrcweir 					{
173*cdf0e10cSrcweir 						// prepare object transformation for polygons
174*cdf0e10cSrcweir 						rTransformation = basegfx::tools::createScaleShearXRotateTranslateB2DHomMatrix(
175*cdf0e10cSrcweir 							aScale, fShearX, fRotate, aTranslate);
176*cdf0e10cSrcweir 					}
177*cdf0e10cSrcweir 				}
178*cdf0e10cSrcweir 			}
179*cdf0e10cSrcweir         }
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir         Primitive2DSequence TextSimplePortionPrimitive2D::create2DDecomposition(const geometry::ViewInformation2D& /*rViewInformation*/) const
182*cdf0e10cSrcweir 		{
183*cdf0e10cSrcweir 			Primitive2DSequence aRetval;
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 			if(getTextLength())
186*cdf0e10cSrcweir 			{
187*cdf0e10cSrcweir 				basegfx::B2DPolyPolygonVector aB2DPolyPolyVector;
188*cdf0e10cSrcweir 			    basegfx::B2DHomMatrix aPolygonTransform;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir                 // get text outlines and their object transformation
191*cdf0e10cSrcweir                 getTextOutlinesAndTransformation(aB2DPolyPolyVector, aPolygonTransform);
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir 				// create primitives for the outlines
194*cdf0e10cSrcweir 				const sal_uInt32 nCount(aB2DPolyPolyVector.size());
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 				if(nCount)
197*cdf0e10cSrcweir 				{
198*cdf0e10cSrcweir 					// alloc space for the primitives
199*cdf0e10cSrcweir 					aRetval.realloc(nCount);
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir 					// color-filled polypolygons
202*cdf0e10cSrcweir 					for(sal_uInt32 a(0L); a < nCount; a++)
203*cdf0e10cSrcweir 					{
204*cdf0e10cSrcweir 						// prepare polypolygon
205*cdf0e10cSrcweir 						basegfx::B2DPolyPolygon& rPolyPolygon = aB2DPolyPolyVector[a];
206*cdf0e10cSrcweir 						rPolyPolygon.transform(aPolygonTransform);
207*cdf0e10cSrcweir 						aRetval[a] = new PolyPolygonColorPrimitive2D(rPolyPolygon, getFontColor());
208*cdf0e10cSrcweir 					}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 					if(getFontAttribute().getOutline())
211*cdf0e10cSrcweir 					{
212*cdf0e10cSrcweir 				        // decompose polygon transformation to single values
213*cdf0e10cSrcweir 				        basegfx::B2DVector aScale, aTranslate;
214*cdf0e10cSrcweir 				        double fRotate, fShearX;
215*cdf0e10cSrcweir 				        aPolygonTransform.decompose(aScale, aTranslate, fRotate, fShearX);
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir                         // create outline text effect with current content and replace
218*cdf0e10cSrcweir 						Primitive2DReference aNewTextEffect(new TextEffectPrimitive2D(
219*cdf0e10cSrcweir 							aRetval,
220*cdf0e10cSrcweir 							aTranslate,
221*cdf0e10cSrcweir 							fRotate,
222*cdf0e10cSrcweir 							TEXTEFFECTSTYLE2D_OUTLINE));
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir 						aRetval = Primitive2DSequence(&aNewTextEffect, 1);
225*cdf0e10cSrcweir 					}
226*cdf0e10cSrcweir 				}
227*cdf0e10cSrcweir 			}
228*cdf0e10cSrcweir 
229*cdf0e10cSrcweir 			return aRetval;
230*cdf0e10cSrcweir 		}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 		TextSimplePortionPrimitive2D::TextSimplePortionPrimitive2D(
233*cdf0e10cSrcweir 			const basegfx::B2DHomMatrix& rNewTransform,
234*cdf0e10cSrcweir 			const String& rText,
235*cdf0e10cSrcweir 			xub_StrLen aTextPosition,
236*cdf0e10cSrcweir 			xub_StrLen aTextLength,
237*cdf0e10cSrcweir 			const ::std::vector< double >& rDXArray,
238*cdf0e10cSrcweir             const attribute::FontAttribute& rFontAttribute,
239*cdf0e10cSrcweir             const ::com::sun::star::lang::Locale& rLocale,
240*cdf0e10cSrcweir 			const basegfx::BColor& rFontColor)
241*cdf0e10cSrcweir 		:	BufferedDecompositionPrimitive2D(),
242*cdf0e10cSrcweir 			maTextTransform(rNewTransform),
243*cdf0e10cSrcweir 			maText(rText),
244*cdf0e10cSrcweir 			maTextPosition(aTextPosition),
245*cdf0e10cSrcweir 			maTextLength(aTextLength),
246*cdf0e10cSrcweir 			maDXArray(rDXArray),
247*cdf0e10cSrcweir 			maFontAttribute(rFontAttribute),
248*cdf0e10cSrcweir             maLocale(rLocale),
249*cdf0e10cSrcweir 			maFontColor(rFontColor),
250*cdf0e10cSrcweir 			maB2DRange()
251*cdf0e10cSrcweir 		{
252*cdf0e10cSrcweir #ifdef DBG_UTIL
253*cdf0e10cSrcweir 			const xub_StrLen aStringLength(getText().Len());
254*cdf0e10cSrcweir 			OSL_ENSURE(aStringLength >= getTextPosition() && aStringLength >= getTextPosition() + getTextLength(),
255*cdf0e10cSrcweir 				"TextSimplePortionPrimitive2D with text out of range (!)");
256*cdf0e10cSrcweir #endif
257*cdf0e10cSrcweir 		}
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir         bool LocalesAreEqual(const ::com::sun::star::lang::Locale& rA, const ::com::sun::star::lang::Locale& rB)
260*cdf0e10cSrcweir         {
261*cdf0e10cSrcweir             return (rA.Language == rB.Language
262*cdf0e10cSrcweir                 && rA.Country == rB.Country
263*cdf0e10cSrcweir                 && rA.Variant == rB.Variant);
264*cdf0e10cSrcweir         }
265*cdf0e10cSrcweir 
266*cdf0e10cSrcweir 		bool TextSimplePortionPrimitive2D::operator==(const BasePrimitive2D& rPrimitive) const
267*cdf0e10cSrcweir 		{
268*cdf0e10cSrcweir 			if(BufferedDecompositionPrimitive2D::operator==(rPrimitive))
269*cdf0e10cSrcweir 			{
270*cdf0e10cSrcweir 				const TextSimplePortionPrimitive2D& rCompare = (TextSimplePortionPrimitive2D&)rPrimitive;
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir 				return (getTextTransform() == rCompare.getTextTransform()
273*cdf0e10cSrcweir 					&& getText() == rCompare.getText()
274*cdf0e10cSrcweir 					&& getTextPosition() == rCompare.getTextPosition()
275*cdf0e10cSrcweir 					&& getTextLength() == rCompare.getTextLength()
276*cdf0e10cSrcweir 					&& getDXArray() == rCompare.getDXArray()
277*cdf0e10cSrcweir 					&& getFontAttribute() == rCompare.getFontAttribute()
278*cdf0e10cSrcweir                     && LocalesAreEqual(getLocale(), rCompare.getLocale())
279*cdf0e10cSrcweir 					&& getFontColor() == rCompare.getFontColor());
280*cdf0e10cSrcweir 			}
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir 			return false;
283*cdf0e10cSrcweir 		}
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir 		basegfx::B2DRange TextSimplePortionPrimitive2D::getB2DRange(const geometry::ViewInformation2D& /*rViewInformation*/) const
286*cdf0e10cSrcweir 		{
287*cdf0e10cSrcweir 			if(maB2DRange.isEmpty() && getTextLength())
288*cdf0e10cSrcweir 			{
289*cdf0e10cSrcweir 				// get TextBoundRect as base size
290*cdf0e10cSrcweir 				// decompose object transformation to single values
291*cdf0e10cSrcweir 				basegfx::B2DVector aScale, aTranslate;
292*cdf0e10cSrcweir 				double fRotate, fShearX;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir 				if(getTextTransform().decompose(aScale, aTranslate, fRotate, fShearX))
295*cdf0e10cSrcweir 				{
296*cdf0e10cSrcweir 					// for the TextLayouterDevice, it is necessary to have a scaling representing
297*cdf0e10cSrcweir 					// the font size. Since we want to extract polygons here, it is okay to
298*cdf0e10cSrcweir 					// work just with scaling and to ignore shear, rotation and translation,
299*cdf0e10cSrcweir 					// all that can be applied to the polygons later
300*cdf0e10cSrcweir 					const basegfx::B2DVector aFontScale(getCorrectedScaleAndFontScale(aScale));
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir 					// prepare textlayoutdevice
303*cdf0e10cSrcweir 					TextLayouterDevice aTextLayouter;
304*cdf0e10cSrcweir 					aTextLayouter.setFontAttribute(
305*cdf0e10cSrcweir                         getFontAttribute(),
306*cdf0e10cSrcweir                         aFontScale.getX(),
307*cdf0e10cSrcweir                         aFontScale.getY(),
308*cdf0e10cSrcweir                         getLocale());
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 					// get basic text range
311*cdf0e10cSrcweir 					basegfx::B2DRange aNewRange(aTextLayouter.getTextBoundRect(getText(), getTextPosition(), getTextLength()));
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir                     // #i104432#, #i102556# take empty results into account
314*cdf0e10cSrcweir                     if(!aNewRange.isEmpty())
315*cdf0e10cSrcweir                     {
316*cdf0e10cSrcweir 					    // prepare object transformation for range
317*cdf0e10cSrcweir 						const basegfx::B2DHomMatrix aRangeTransformation(basegfx::tools::createScaleShearXRotateTranslateB2DHomMatrix(
318*cdf0e10cSrcweir 							aScale, fShearX, fRotate, aTranslate));
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir 					    // apply range transformation to it
321*cdf0e10cSrcweir 					    aNewRange.transform(aRangeTransformation);
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir 					    // assign to buffered value
324*cdf0e10cSrcweir 					    const_cast< TextSimplePortionPrimitive2D* >(this)->maB2DRange = aNewRange;
325*cdf0e10cSrcweir                     }
326*cdf0e10cSrcweir 				}
327*cdf0e10cSrcweir 			}
328*cdf0e10cSrcweir 
329*cdf0e10cSrcweir 			return maB2DRange;
330*cdf0e10cSrcweir 		}
331*cdf0e10cSrcweir 
332*cdf0e10cSrcweir 		// provide unique ID
333*cdf0e10cSrcweir 		ImplPrimitrive2DIDBlock(TextSimplePortionPrimitive2D, PRIMITIVE2D_ID_TEXTSIMPLEPORTIONPRIMITIVE2D)
334*cdf0e10cSrcweir 
335*cdf0e10cSrcweir 	} // end of namespace primitive2d
336*cdf0e10cSrcweir } // end of namespace drawinglayer
337*cdf0e10cSrcweir 
338*cdf0e10cSrcweir //////////////////////////////////////////////////////////////////////////////
339*cdf0e10cSrcweir // eof
340