Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / svx / source / svdraw / svdotxtr.cxx
blob13b35beb2740a5e4fcbb626a69b227cc5589eb01
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include <svx/svdotext.hxx>
31 #include "svx/svditext.hxx"
32 #include <svx/svdtrans.hxx>
33 #include <svx/svdogrp.hxx>
34 #include <svx/svdopath.hxx>
35 #include <svx/svdoutl.hxx>
36 #include <svx/svdpage.hxx> // for Convert
37 #include <svx/svdmodel.hxx> // for Convert
38 #include <editeng/outliner.hxx>
39 #include <svx/sdr/properties/itemsettools.hxx>
40 #include <svx/sdr/properties/properties.hxx>
41 #include <basegfx/polygon/b2dpolypolygontools.hxx>
42 #include <svl/itemset.hxx>
43 #include <svx/svditer.hxx>
44 #include <drawinglayer/processor2d/textaspolygonextractor2d.hxx>
45 #include <svx/sdr/contact/viewcontact.hxx>
46 #include <svx/xflclit.hxx>
47 #include <svx/xlnclit.hxx>
48 #include <svx/xlnwtit.hxx>
51 void SdrTextObj::NbcSetSnapRect(const Rectangle& rRect)
53 if (aGeo.nDrehWink!=0 || aGeo.nShearWink!=0) {
54 Rectangle aSR0(GetSnapRect());
55 long nWdt0=aSR0.Right()-aSR0.Left();
56 long nHgt0=aSR0.Bottom()-aSR0.Top();
57 long nWdt1=rRect.Right()-rRect.Left();
58 long nHgt1=rRect.Bottom()-rRect.Top();
59 SdrTextObj::NbcResize(maSnapRect.TopLeft(),Fraction(nWdt1,nWdt0),Fraction(nHgt1,nHgt0));
60 SdrTextObj::NbcMove(Size(rRect.Left()-aSR0.Left(),rRect.Top()-aSR0.Top()));
61 } else {
62 long nHDist=GetTextLeftDistance()+GetTextRightDistance();
63 long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
64 long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
65 long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
66 long nTWdt1=rRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
67 long nTHgt1=rRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
68 aRect=rRect;
69 ImpJustifyRect(aRect);
70 if (bTextFrame && (pModel==NULL || !pModel->IsPasteResize())) {
71 if (nTWdt0!=nTWdt1 && IsAutoGrowWidth() ) NbcSetMinTextFrameWidth(nTWdt1);
72 if (nTHgt0!=nTHgt1 && IsAutoGrowHeight()) NbcSetMinTextFrameHeight(nTHgt1);
73 NbcAdjustTextFrameWidthAndHeight();
75 ImpCheckShear();
76 SetRectsDirty();
80 const Rectangle& SdrTextObj::GetLogicRect() const
82 return aRect;
85 void SdrTextObj::NbcSetLogicRect(const Rectangle& rRect)
87 long nHDist=GetTextLeftDistance()+GetTextRightDistance();
88 long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
89 long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
90 long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
91 long nTWdt1=rRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
92 long nTHgt1=rRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
93 aRect=rRect;
94 ImpJustifyRect(aRect);
95 if (bTextFrame) {
96 if (nTWdt0!=nTWdt1 && IsAutoGrowWidth() ) NbcSetMinTextFrameWidth(nTWdt1);
97 if (nTHgt0!=nTHgt1 && IsAutoGrowHeight()) NbcSetMinTextFrameHeight(nTHgt1);
98 NbcAdjustTextFrameWidthAndHeight();
100 SetRectsDirty();
103 long SdrTextObj::GetRotateAngle() const
105 return aGeo.nDrehWink;
108 long SdrTextObj::GetShearAngle(bool /*bVertical*/) const
110 return aGeo.nShearWink;
113 void SdrTextObj::NbcMove(const Size& rSiz)
115 MoveRect(aRect,rSiz);
116 MoveRect(aOutRect,rSiz);
117 MoveRect(maSnapRect,rSiz);
118 SetRectsDirty(sal_True);
121 void SdrTextObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
123 bool bNoShearMerk=aGeo.nShearWink==0;
124 bool bRota90Merk=bNoShearMerk && aGeo.nDrehWink % 9000 ==0;
125 long nHDist=GetTextLeftDistance()+GetTextRightDistance();
126 long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
127 long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
128 long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
129 bool bXMirr=(xFact.GetNumerator()<0) != (xFact.GetDenominator()<0);
130 bool bYMirr=(yFact.GetNumerator()<0) != (yFact.GetDenominator()<0);
131 if (bXMirr || bYMirr) {
132 Point aRef1(GetSnapRect().Center());
133 if (bXMirr) {
134 Point aRef2(aRef1);
135 aRef2.Y()++;
136 NbcMirrorGluePoints(aRef1,aRef2);
138 if (bYMirr) {
139 Point aRef2(aRef1);
140 aRef2.X()++;
141 NbcMirrorGluePoints(aRef1,aRef2);
145 if (aGeo.nDrehWink==0 && aGeo.nShearWink==0) {
146 ResizeRect(aRect,rRef,xFact,yFact);
147 if (bYMirr) {
148 aRect.Justify();
149 aRect.Move(aRect.Right()-aRect.Left(),aRect.Bottom()-aRect.Top());
150 aGeo.nDrehWink=18000;
151 aGeo.RecalcSinCos();
154 else
156 Polygon aPol(Rect2Poly(aRect,aGeo));
158 for(sal_uInt16 a(0); a < aPol.GetSize(); a++)
160 ResizePoint(aPol[a], rRef, xFact, yFact);
163 if(bXMirr != bYMirr)
165 // turn polygon and move it a little
166 Polygon aPol0(aPol);
168 aPol[0] = aPol0[1];
169 aPol[1] = aPol0[0];
170 aPol[2] = aPol0[3];
171 aPol[3] = aPol0[2];
172 aPol[4] = aPol0[1];
175 Poly2Rect(aPol, aRect, aGeo);
178 if (bRota90Merk) {
179 bool bRota90=aGeo.nDrehWink % 9000 ==0;
180 if (!bRota90) { // there's seems to be a rounding error occurring: correct it
181 long a=NormAngle360(aGeo.nDrehWink);
182 if (a<4500) a=0;
183 else if (a<13500) a=9000;
184 else if (a<22500) a=18000;
185 else if (a<31500) a=27000;
186 else a=0;
187 aGeo.nDrehWink=a;
188 aGeo.RecalcSinCos();
190 if (bNoShearMerk!=(aGeo.nShearWink==0)) { // correct a rounding error occurring with Shear
191 aGeo.nShearWink=0;
192 aGeo.RecalcTan();
196 ImpJustifyRect(aRect);
197 long nTWdt1=aRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
198 long nTHgt1=aRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
199 if (bTextFrame && (pModel==NULL || !pModel->IsPasteResize())) {
200 if (nTWdt0!=nTWdt1 && IsAutoGrowWidth() ) NbcSetMinTextFrameWidth(nTWdt1);
201 if (nTHgt0!=nTHgt1 && IsAutoGrowHeight()) NbcSetMinTextFrameHeight(nTHgt1);
202 NbcAdjustTextFrameWidthAndHeight();
204 ImpCheckShear();
205 SetRectsDirty();
208 void SdrTextObj::NbcRotate(const Point& rRef, long nWink, double sn, double cs)
210 SetGlueReallyAbsolute(sal_True);
211 long dx=aRect.Right()-aRect.Left();
212 long dy=aRect.Bottom()-aRect.Top();
213 Point aP(aRect.TopLeft());
214 RotatePoint(aP,rRef,sn,cs);
215 aRect.Left()=aP.X();
216 aRect.Top()=aP.Y();
217 aRect.Right()=aRect.Left()+dx;
218 aRect.Bottom()=aRect.Top()+dy;
219 if (aGeo.nDrehWink==0) {
220 aGeo.nDrehWink=NormAngle360(nWink);
221 aGeo.nSin=sn;
222 aGeo.nCos=cs;
223 } else {
224 aGeo.nDrehWink=NormAngle360(aGeo.nDrehWink+nWink);
225 aGeo.RecalcSinCos();
227 SetRectsDirty();
228 NbcRotateGluePoints(rRef,nWink,sn,cs);
229 SetGlueReallyAbsolute(sal_False);
232 void SdrTextObj::NbcShear(const Point& rRef, long nWink, double tn, bool bVShear)
234 SetGlueReallyAbsolute(sal_True);
236 // when this is a SdrPathObj, aRect may be uninitialized
237 Polygon aPol(Rect2Poly(aRect.IsEmpty() ? GetSnapRect() : aRect, aGeo));
239 sal_uInt16 nPointCount=aPol.GetSize();
240 for (sal_uInt16 i=0; i<nPointCount; i++) {
241 ShearPoint(aPol[i],rRef,tn,bVShear);
243 Poly2Rect(aPol,aRect,aGeo);
244 ImpJustifyRect(aRect);
245 if (bTextFrame) {
246 NbcAdjustTextFrameWidthAndHeight();
248 ImpCheckShear();
249 SetRectsDirty();
250 NbcShearGluePoints(rRef,nWink,tn,bVShear);
251 SetGlueReallyAbsolute(sal_False);
254 void SdrTextObj::NbcMirror(const Point& rRef1, const Point& rRef2)
256 SetGlueReallyAbsolute(sal_True);
257 bool bNoShearMerk=aGeo.nShearWink==0;
258 bool bRota90Merk = false;
259 if (bNoShearMerk &&
260 (rRef1.X()==rRef2.X() || rRef1.Y()==rRef2.Y() ||
261 Abs(rRef1.X()-rRef2.X())==Abs(rRef1.Y()-rRef2.Y()))) {
262 bRota90Merk=aGeo.nDrehWink % 9000 ==0;
264 Polygon aPol(Rect2Poly(aRect,aGeo));
265 sal_uInt16 i;
266 sal_uInt16 nPntAnz=aPol.GetSize();
267 for (i=0; i<nPntAnz; i++) {
268 MirrorPoint(aPol[i],rRef1,rRef2);
270 // turn polygon and move it a little
271 Polygon aPol0(aPol);
272 aPol[0]=aPol0[1];
273 aPol[1]=aPol0[0];
274 aPol[2]=aPol0[3];
275 aPol[3]=aPol0[2];
276 aPol[4]=aPol0[1];
277 Poly2Rect(aPol,aRect,aGeo);
279 if (bRota90Merk) {
280 bool bRota90=aGeo.nDrehWink % 9000 ==0;
281 if (bRota90Merk && !bRota90) { // there's seems to be a rounding error occurring: correct it
282 long a=NormAngle360(aGeo.nDrehWink);
283 if (a<4500) a=0;
284 else if (a<13500) a=9000;
285 else if (a<22500) a=18000;
286 else if (a<31500) a=27000;
287 else a=0;
288 aGeo.nDrehWink=a;
289 aGeo.RecalcSinCos();
292 if (bNoShearMerk!=(aGeo.nShearWink==0)) { // correct a rounding error occurring with Shear
293 aGeo.nShearWink=0;
294 aGeo.RecalcTan();
297 ImpJustifyRect(aRect);
298 if (bTextFrame) {
299 NbcAdjustTextFrameWidthAndHeight();
301 ImpCheckShear();
302 SetRectsDirty();
303 NbcMirrorGluePoints(rRef1,rRef2);
304 SetGlueReallyAbsolute(sal_False);
307 //////////////////////////////////////////////////////////////////////////////
309 SdrObject* SdrTextObj::ImpConvertContainedTextToSdrPathObjs(bool bToPoly) const
311 SdrObject* pRetval = 0;
313 if(!ImpCanConvTextToCurve())
315 // suppress HelpTexts from PresObj's
316 return 0;
319 // get primitives
320 const drawinglayer::primitive2d::Primitive2DSequence xSequence(GetViewContact().getViewIndependentPrimitive2DSequence());
322 if(xSequence.hasElements())
324 // create an extractor with neutral ViewInformation
325 const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
326 drawinglayer::processor2d::TextAsPolygonExtractor2D aExtractor(aViewInformation2D);
328 // extract text as polygons
329 aExtractor.process(xSequence);
331 // get results
332 const drawinglayer::processor2d::TextAsPolygonDataNodeVector& rResult = aExtractor.getTarget();
333 const sal_uInt32 nResultCount(rResult.size());
335 if(nResultCount)
337 // prepare own target
338 SdrObjGroup* pGroup = new SdrObjGroup();
339 SdrObjList* pObjectList = pGroup->GetSubList();
341 // process results
342 for(sal_uInt32 a(0); a < nResultCount; a++)
344 const drawinglayer::processor2d::TextAsPolygonDataNode& rCandidate = rResult[a];
345 basegfx::B2DPolyPolygon aPolyPolygon(rCandidate.getB2DPolyPolygon());
347 if(aPolyPolygon.count())
349 // take care of wanted polygon type
350 if(bToPoly)
352 if(aPolyPolygon.areControlPointsUsed())
354 aPolyPolygon = basegfx::tools::adaptiveSubdivideByAngle(aPolyPolygon);
357 else
359 if(!aPolyPolygon.areControlPointsUsed())
361 aPolyPolygon = basegfx::tools::expandToCurve(aPolyPolygon);
365 // create ItemSet with object attributes
366 SfxItemSet aAttributeSet(GetObjectItemSet());
367 SdrPathObj* pPathObj = 0;
369 // always clear objectshadow; this is included in the extraction
370 aAttributeSet.Put(SdrShadowItem(false));
372 if(rCandidate.getIsFilled())
374 // set needed items
375 aAttributeSet.Put(XFillColorItem(String(), Color(rCandidate.getBColor())));
376 aAttributeSet.Put(XLineStyleItem(XLINE_NONE));
377 aAttributeSet.Put(XFillStyleItem(XFILL_SOLID));
379 // create filled SdrPathObj
380 pPathObj = new SdrPathObj(OBJ_PATHFILL, aPolyPolygon);
382 else
384 // set needed items
385 aAttributeSet.Put(XLineColorItem(String(), Color(rCandidate.getBColor())));
386 aAttributeSet.Put(XLineStyleItem(XLINE_SOLID));
387 aAttributeSet.Put(XLineWidthItem(0));
388 aAttributeSet.Put(XFillStyleItem(XFILL_NONE));
390 // create line SdrPathObj
391 pPathObj = new SdrPathObj(OBJ_PATHLINE, aPolyPolygon);
394 // copy basic information from original
395 pPathObj->ImpSetAnchorPos(GetAnchorPos());
396 pPathObj->NbcSetLayer(GetLayer());
398 if(GetModel())
400 pPathObj->SetModel(GetModel());
401 pPathObj->NbcSetStyleSheet(GetStyleSheet(), true);
404 // apply prepared ItemSet and add to target
405 pPathObj->SetMergedItemSet(aAttributeSet);
406 pObjectList->InsertObject(pPathObj);
410 // postprocess; if no result and/or only one object, simplify
411 if(!pObjectList->GetObjCount())
413 delete pGroup;
415 else if(1 == pObjectList->GetObjCount())
417 pRetval = pObjectList->RemoveObject(0);
418 delete pGroup;
420 else
422 pRetval = pGroup;
427 return pRetval;
430 //////////////////////////////////////////////////////////////////////////////
432 SdrObject* SdrTextObj::DoConvertToPolyObj(sal_Bool bBezier) const
434 return ImpConvertContainedTextToSdrPathObjs(!bBezier);
437 bool SdrTextObj::ImpCanConvTextToCurve() const
439 return !IsOutlText();
442 SdrObject* SdrTextObj::ImpConvertMakeObj(const basegfx::B2DPolyPolygon& rPolyPolygon, sal_Bool bClosed, sal_Bool bBezier, sal_Bool bNoSetAttr) const
444 SdrObjKind ePathKind = bClosed ? OBJ_PATHFILL : OBJ_PATHLINE;
445 basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPolygon);
447 // #i37011#
448 if(!bBezier)
450 aB2DPolyPolygon = basegfx::tools::adaptiveSubdivideByAngle(aB2DPolyPolygon);
451 ePathKind = bClosed ? OBJ_POLY : OBJ_PLIN;
454 SdrPathObj* pPathObj = new SdrPathObj(ePathKind, aB2DPolyPolygon);
456 if(bBezier)
458 // create bezier curves
459 pPathObj->SetPathPoly(basegfx::tools::expandToCurve(pPathObj->GetPathPoly()));
462 if(pPathObj)
464 pPathObj->ImpSetAnchorPos(aAnchor);
465 pPathObj->NbcSetLayer(SdrLayerID(GetLayer()));
467 if(pModel)
469 pPathObj->SetModel(pModel);
471 if(!bNoSetAttr)
473 sdr::properties::ItemChangeBroadcaster aC(*pPathObj);
475 pPathObj->ClearMergedItem();
476 pPathObj->SetMergedItemSet(GetObjectItemSet());
477 pPathObj->GetProperties().BroadcastItemChange(aC);
478 pPathObj->NbcSetStyleSheet(GetStyleSheet(), sal_True);
483 return pPathObj;
486 SdrObject* SdrTextObj::ImpConvertAddText(SdrObject* pObj, bool bBezier) const
488 if(!ImpCanConvTextToCurve())
490 return pObj;
493 SdrObject* pText = ImpConvertContainedTextToSdrPathObjs(!bBezier);
495 if(!pText)
497 return pObj;
500 if(!pObj)
502 return pText;
505 if(pText->IsGroupObject())
507 // is already group object, add partial shape in front
508 SdrObjList* pOL=pText->GetSubList();
509 pOL->InsertObject(pObj,0);
511 return pText;
513 else
515 // not yet a group, create one and add partial and new shapes
516 SdrObjGroup* pGrp=new SdrObjGroup;
517 SdrObjList* pOL=pGrp->GetSubList();
518 pOL->InsertObject(pObj);
519 pOL->InsertObject(pText);
521 return pGrp;
525 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */