xref: /AOO41X/main/svx/source/svdraw/svdotxtr.cxx (revision 8e8ee8fefdac26d905672cc573c35fd0ae1f9356)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include <svx/svdotext.hxx>
28 #include "svx/svditext.hxx"
29 #include <svx/svdtrans.hxx>
30 #include <svx/svdogrp.hxx>
31 #include <svx/svdopath.hxx>
32 #include <svx/svdoutl.hxx>
33 #include <svx/svdpage.hxx>   // fuer Convert
34 #include <svx/svdmodel.hxx>  // fuer Convert
35 #include <editeng/outliner.hxx>
36 #include <svx/sdr/properties/itemsettools.hxx>
37 #include <svx/sdr/properties/properties.hxx>
38 #include <basegfx/polygon/b2dpolypolygontools.hxx>
39 #include <svl/itemset.hxx>
40 #include <svx/svditer.hxx>
41 #include <drawinglayer/processor2d/textaspolygonextractor2d.hxx>
42 #include <svx/sdr/contact/viewcontact.hxx>
43 #include <svx/xflclit.hxx>
44 #include <svx/xlnclit.hxx>
45 #include <svx/xlnwtit.hxx>
46 
47 ////////////////////////////////////////////////////////////////////////////////////////////////////
48 //
49 //  @@@@@@ @@@@@ @@   @@ @@@@@@  @@@@  @@@@@  @@@@@@
50 //    @@   @@    @@@ @@@   @@   @@  @@ @@  @@     @@
51 //    @@   @@     @@@@@    @@   @@  @@ @@  @@     @@
52 //    @@   @@@@    @@@     @@   @@  @@ @@@@@      @@
53 //    @@   @@     @@@@@    @@   @@  @@ @@  @@     @@
54 //    @@   @@    @@@ @@@   @@   @@  @@ @@  @@ @@  @@
55 //    @@   @@@@@ @@   @@   @@    @@@@  @@@@@   @@@@
56 //
57 //  Transformationen
58 //
59 ////////////////////////////////////////////////////////////////////////////////////////////////////
60 
NbcSetSnapRect(const Rectangle & rRect)61 void SdrTextObj::NbcSetSnapRect(const Rectangle& rRect)
62 {
63     if (aGeo.nDrehWink!=0 || aGeo.nShearWink!=0) {
64         Rectangle aSR0(GetSnapRect());
65         long nWdt0=aSR0.Right()-aSR0.Left();
66         long nHgt0=aSR0.Bottom()-aSR0.Top();
67         long nWdt1=rRect.Right()-rRect.Left();
68         long nHgt1=rRect.Bottom()-rRect.Top();
69         SdrTextObj::NbcResize(maSnapRect.TopLeft(),Fraction(nWdt1,nWdt0),Fraction(nHgt1,nHgt0));
70         SdrTextObj::NbcMove(Size(rRect.Left()-aSR0.Left(),rRect.Top()-aSR0.Top()));
71     } else {
72         long nHDist=GetTextLeftDistance()+GetTextRightDistance();
73         long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
74         long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
75         long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
76         long nTWdt1=rRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
77         long nTHgt1=rRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
78         aRect=rRect;
79         ImpJustifyRect(aRect);
80 
81         // #115391#
82         AdaptTextMinSize();
83 
84         if (bTextFrame && (pModel==NULL || !pModel->IsPasteResize()))
85         {
86             if(SDRTEXTFIT_RESIZEATTR == GetFitToSize())
87             {
88                 NbcResizeTextAttributes(Fraction(nTWdt1,nTWdt0),Fraction(nTHgt1,nTHgt0));
89             }
90 
91             NbcAdjustTextFrameWidthAndHeight();
92         }
93 
94         ImpCheckShear();
95         SetRectsDirty();
96     }
97 }
98 
GetLogicRect() const99 const Rectangle& SdrTextObj::GetLogicRect() const
100 {
101     return aRect;
102 }
103 
NbcSetLogicRect(const Rectangle & rRect)104 void SdrTextObj::NbcSetLogicRect(const Rectangle& rRect)
105 {
106     long nHDist=GetTextLeftDistance()+GetTextRightDistance();
107     long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
108     long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
109     long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
110     long nTWdt1=rRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
111     long nTHgt1=rRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
112     aRect=rRect;
113     ImpJustifyRect(aRect);
114 
115     // #115391#
116     AdaptTextMinSize();
117 
118     if(bTextFrame)
119     {
120         if(SDRTEXTFIT_RESIZEATTR == GetFitToSize())
121         {
122             NbcResizeTextAttributes(Fraction(nTWdt1,nTWdt0),Fraction(nTHgt1,nTHgt0));
123         }
124 
125         NbcAdjustTextFrameWidthAndHeight();
126     }
127 
128     SetRectsDirty();
129 }
130 
GetRotateAngle() const131 long SdrTextObj::GetRotateAngle() const
132 {
133     return aGeo.nDrehWink;
134 }
135 
GetShearAngle(FASTBOOL) const136 long SdrTextObj::GetShearAngle(FASTBOOL /*bVertical*/) const
137 {
138     return aGeo.nShearWink;
139 }
140 
NbcMove(const Size & rSiz)141 void SdrTextObj::NbcMove(const Size& rSiz)
142 {
143     MoveRect(aRect,rSiz);
144     MoveRect(aOutRect,rSiz);
145     MoveRect(maSnapRect,rSiz);
146     SetRectsDirty(sal_True);
147 }
148 
NbcResize(const Point & rRef,const Fraction & xFact,const Fraction & yFact)149 void SdrTextObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
150 {
151     FASTBOOL bNoShearMerk=aGeo.nShearWink==0;
152     FASTBOOL bRota90Merk=bNoShearMerk && aGeo.nDrehWink % 9000 ==0;
153     long nHDist=GetTextLeftDistance()+GetTextRightDistance();
154     long nVDist=GetTextUpperDistance()+GetTextLowerDistance();
155     long nTWdt0=aRect.GetWidth ()-1-nHDist; if (nTWdt0<0) nTWdt0=0;
156     long nTHgt0=aRect.GetHeight()-1-nVDist; if (nTHgt0<0) nTHgt0=0;
157     FASTBOOL bXMirr=(xFact.GetNumerator()<0) != (xFact.GetDenominator()<0);
158     FASTBOOL bYMirr=(yFact.GetNumerator()<0) != (yFact.GetDenominator()<0);
159     if (bXMirr || bYMirr) {
160         Point aRef1(GetSnapRect().Center());
161         if (bXMirr) {
162             Point aRef2(aRef1);
163             aRef2.Y()++;
164             NbcMirrorGluePoints(aRef1,aRef2);
165         }
166         if (bYMirr) {
167             Point aRef2(aRef1);
168             aRef2.X()++;
169             NbcMirrorGluePoints(aRef1,aRef2);
170         }
171     }
172 
173     if (aGeo.nDrehWink==0 && aGeo.nShearWink==0) {
174         ResizeRect(aRect,rRef,xFact,yFact);
175         if (bYMirr) {
176             aRect.Justify();
177             aRect.Move(aRect.Right()-aRect.Left(),aRect.Bottom()-aRect.Top());
178             aGeo.nDrehWink=18000;
179             aGeo.RecalcSinCos();
180         }
181     }
182     else
183     {
184         // #100663# aRect is NOT initialized for lines (polgon objects with two
185         // exceptionally handled points). Thus, after this call the text rotaion is
186         // gone. This error must be present since day one of this old drawing layer.
187         // It's astonishing that noone discovered it earlier.
188         // Polygon aPol(Rect2Poly(aRect,aGeo));
189         // Polygon aPol(Rect2Poly(GetSnapRect(), aGeo));
190 
191         // #101412# go back to old method, side effects are impossible
192         // to calculate.
193         Polygon aPol(Rect2Poly(aRect,aGeo));
194 
195         for(sal_uInt16 a(0); a < aPol.GetSize(); a++)
196         {
197              ResizePoint(aPol[a], rRef, xFact, yFact);
198         }
199 
200         if(bXMirr != bYMirr)
201         {
202             // Polygon wenden und etwas schieben
203             Polygon aPol0(aPol);
204 
205             aPol[0] = aPol0[1];
206             aPol[1] = aPol0[0];
207             aPol[2] = aPol0[3];
208             aPol[3] = aPol0[2];
209             aPol[4] = aPol0[1];
210         }
211 
212         Poly2Rect(aPol, aRect, aGeo);
213     }
214 
215     if (bRota90Merk) {
216         FASTBOOL bRota90=aGeo.nDrehWink % 9000 ==0;
217         if (!bRota90) { // Scheinbar Rundungsfehler: Korregieren
218             long a=NormAngle360(aGeo.nDrehWink);
219             if (a<4500) a=0;
220             else if (a<13500) a=9000;
221             else if (a<22500) a=18000;
222             else if (a<31500) a=27000;
223             else a=0;
224             aGeo.nDrehWink=a;
225             aGeo.RecalcSinCos();
226         }
227         if (bNoShearMerk!=(aGeo.nShearWink==0)) { // Shear ggf. korregieren wg. Rundungsfehler
228             aGeo.nShearWink=0;
229             aGeo.RecalcTan();
230         }
231     }
232 
233     ImpJustifyRect(aRect);
234 
235     long nTWdt1=aRect.GetWidth ()-1-nHDist; if (nTWdt1<0) nTWdt1=0;
236     long nTHgt1=aRect.GetHeight()-1-nVDist; if (nTHgt1<0) nTHgt1=0;
237 
238     // #115391#
239     AdaptTextMinSize();
240 
241     if(bTextFrame && (!pModel || !pModel->IsPasteResize()))
242     {
243         if(SDRTEXTFIT_RESIZEATTR == GetFitToSize())
244         {
245             NbcResizeTextAttributes(Fraction(nTWdt1,nTWdt0),Fraction(nTHgt1,nTHgt0));
246         }
247 
248         NbcAdjustTextFrameWidthAndHeight();
249     }
250 
251     ImpCheckShear();
252     SetRectsDirty();
253 }
254 
NbcRotate(const Point & rRef,long nWink,double sn,double cs)255 void SdrTextObj::NbcRotate(const Point& rRef, long nWink, double sn, double cs)
256 {
257     SetGlueReallyAbsolute(sal_True);
258     long dx=aRect.Right()-aRect.Left();
259     long dy=aRect.Bottom()-aRect.Top();
260     Point aP(aRect.TopLeft());
261     RotatePoint(aP,rRef,sn,cs);
262     aRect.Left()=aP.X();
263     aRect.Top()=aP.Y();
264     aRect.Right()=aRect.Left()+dx;
265     aRect.Bottom()=aRect.Top()+dy;
266     if (aGeo.nDrehWink==0) {
267         aGeo.nDrehWink=NormAngle360(nWink);
268         aGeo.nSin=sn;
269         aGeo.nCos=cs;
270     } else {
271         aGeo.nDrehWink=NormAngle360(aGeo.nDrehWink+nWink);
272         aGeo.RecalcSinCos();
273     }
274     SetRectsDirty();
275     NbcRotateGluePoints(rRef,nWink,sn,cs);
276     SetGlueReallyAbsolute(sal_False);
277 }
278 
NbcShear(const Point & rRef,long nWink,double tn,FASTBOOL bVShear)279 void SdrTextObj::NbcShear(const Point& rRef, long nWink, double tn, FASTBOOL bVShear)
280 {
281     SetGlueReallyAbsolute(sal_True);
282 
283     // #75889# when this is a SdrPathObj aRect maybe not initialized
284     Polygon aPol(Rect2Poly(aRect.IsEmpty() ? GetSnapRect() : aRect, aGeo));
285 
286     sal_uInt16 nPointCount=aPol.GetSize();
287     for (sal_uInt16 i=0; i<nPointCount; i++) {
288          ShearPoint(aPol[i],rRef,tn,bVShear);
289     }
290     Poly2Rect(aPol,aRect,aGeo);
291     ImpJustifyRect(aRect);
292     if (bTextFrame) {
293         NbcAdjustTextFrameWidthAndHeight();
294     }
295     ImpCheckShear();
296     SetRectsDirty();
297     NbcShearGluePoints(rRef,nWink,tn,bVShear);
298     SetGlueReallyAbsolute(sal_False);
299 }
300 
NbcMirror(const Point & rRef1,const Point & rRef2)301 void SdrTextObj::NbcMirror(const Point& rRef1, const Point& rRef2)
302 {
303     SetGlueReallyAbsolute(sal_True);
304     FASTBOOL bNoShearMerk=aGeo.nShearWink==0;
305     FASTBOOL bRota90Merk=sal_False;
306     if (bNoShearMerk &&
307         (rRef1.X()==rRef2.X() || rRef1.Y()==rRef2.Y() ||
308          Abs(rRef1.X()-rRef2.X())==Abs(rRef1.Y()-rRef2.Y()))) {
309         bRota90Merk=aGeo.nDrehWink % 9000 ==0;
310     }
311     Polygon aPol(Rect2Poly(aRect,aGeo));
312     sal_uInt16 i;
313     sal_uInt16 nPntAnz=aPol.GetSize();
314     for (i=0; i<nPntAnz; i++) {
315          MirrorPoint(aPol[i],rRef1,rRef2);
316     }
317     // Polygon wenden und etwas schieben
318     Polygon aPol0(aPol);
319     aPol[0]=aPol0[1];
320     aPol[1]=aPol0[0];
321     aPol[2]=aPol0[3];
322     aPol[3]=aPol0[2];
323     aPol[4]=aPol0[1];
324     Poly2Rect(aPol,aRect,aGeo);
325 
326     if (bRota90Merk) {
327         FASTBOOL bRota90=aGeo.nDrehWink % 9000 ==0;
328         if (bRota90Merk && !bRota90) { // Scheinbar Rundungsfehler: Korregieren
329             long a=NormAngle360(aGeo.nDrehWink);
330             if (a<4500) a=0;
331             else if (a<13500) a=9000;
332             else if (a<22500) a=18000;
333             else if (a<31500) a=27000;
334             else a=0;
335             aGeo.nDrehWink=a;
336             aGeo.RecalcSinCos();
337         }
338     }
339     if (bNoShearMerk!=(aGeo.nShearWink==0)) { // Shear ggf. korregieren wg. Rundungsfehler
340         aGeo.nShearWink=0;
341         aGeo.RecalcTan();
342     }
343 
344     ImpJustifyRect(aRect);
345     if (bTextFrame) {
346         NbcAdjustTextFrameWidthAndHeight();
347     }
348     ImpCheckShear();
349     SetRectsDirty();
350     NbcMirrorGluePoints(rRef1,rRef2);
351     SetGlueReallyAbsolute(sal_False);
352 }
353 
354 //////////////////////////////////////////////////////////////////////////////
355 
ImpConvertContainedTextToSdrPathObjs(bool bToPoly) const356 SdrObject* SdrTextObj::ImpConvertContainedTextToSdrPathObjs(bool bToPoly) const
357 {
358     SdrObject* pRetval = 0;
359 
360     if(!ImpCanConvTextToCurve())
361     {
362         // suppress HelpTexts from PresObj's
363         return 0;
364     }
365 
366     // get primitives
367     const drawinglayer::primitive2d::Primitive2DSequence xSequence(GetViewContact().getViewIndependentPrimitive2DSequence());
368 
369     if(xSequence.hasElements())
370     {
371         // create an extractor with neutral ViewInformation
372         const drawinglayer::geometry::ViewInformation2D aViewInformation2D;
373         drawinglayer::processor2d::TextAsPolygonExtractor2D aExtractor(aViewInformation2D);
374 
375         // extract text as polygons
376         aExtractor.process(xSequence);
377 
378         // get results
379         const drawinglayer::processor2d::TextAsPolygonDataNodeVector& rResult = aExtractor.getTarget();
380         const sal_uInt32 nResultCount(rResult.size());
381 
382         if(nResultCount)
383         {
384             // prepare own target
385             SdrObjGroup* pGroup = new SdrObjGroup();
386             SdrObjList* pObjectList = pGroup->GetSubList();
387 
388             // process results
389             for(sal_uInt32 a(0); a < nResultCount; a++)
390             {
391                 const drawinglayer::processor2d::TextAsPolygonDataNode& rCandidate = rResult[a];
392                 basegfx::B2DPolyPolygon aPolyPolygon(rCandidate.getB2DPolyPolygon());
393 
394                 if(aPolyPolygon.count())
395                 {
396                     // take care of wanted polygon type
397                     if(bToPoly)
398                     {
399                         if(aPolyPolygon.areControlPointsUsed())
400                         {
401                             aPolyPolygon = basegfx::tools::adaptiveSubdivideByAngle(aPolyPolygon);
402                         }
403                     }
404                     else
405                     {
406                         if(!aPolyPolygon.areControlPointsUsed())
407                         {
408                             aPolyPolygon = basegfx::tools::expandToCurve(aPolyPolygon);
409                         }
410                     }
411 
412                     // create ItemSet with object attributes
413                     SfxItemSet aAttributeSet(GetObjectItemSet());
414                     SdrPathObj* pPathObj = 0;
415 
416                     // always clear objectshadow; this is included in the extraction
417                     aAttributeSet.Put(SdrShadowItem(false));
418 
419                     if(rCandidate.getIsFilled())
420                     {
421                         // set needed items
422                         aAttributeSet.Put(XFillColorItem(String(), Color(rCandidate.getBColor())));
423                         aAttributeSet.Put(XLineStyleItem(XLINE_NONE));
424                         aAttributeSet.Put(XFillStyleItem(XFILL_SOLID));
425 
426                         // create filled SdrPathObj
427                         pPathObj = new SdrPathObj(OBJ_PATHFILL, aPolyPolygon);
428                     }
429                     else
430                     {
431                         // set needed items
432                         aAttributeSet.Put(XLineColorItem(String(), Color(rCandidate.getBColor())));
433                         aAttributeSet.Put(XLineStyleItem(XLINE_SOLID));
434                         aAttributeSet.Put(XLineWidthItem(0));
435                         aAttributeSet.Put(XFillStyleItem(XFILL_NONE));
436 
437                         // create line SdrPathObj
438                         pPathObj = new SdrPathObj(OBJ_PATHLINE, aPolyPolygon);
439                     }
440 
441                     // copy basic information from original
442                     pPathObj->ImpSetAnchorPos(GetAnchorPos());
443                     pPathObj->NbcSetLayer(GetLayer());
444 
445                     if(GetModel())
446                     {
447                         pPathObj->SetModel(GetModel());
448                         pPathObj->NbcSetStyleSheet(GetStyleSheet(), true);
449                     }
450 
451                     // apply prepared ItemSet and add to target
452                     pPathObj->SetMergedItemSet(aAttributeSet);
453                     pObjectList->InsertObject(pPathObj);
454                 }
455             }
456 
457             // postprocess; if no result and/or only one object, simplify
458             if(!pObjectList->GetObjCount())
459             {
460                 delete pGroup;
461             }
462             else if(1 == pObjectList->GetObjCount())
463             {
464                 pRetval = pObjectList->RemoveObject(0);
465                 delete pGroup;
466             }
467             else
468             {
469                 pRetval = pGroup;
470             }
471         }
472     }
473 
474     return pRetval;
475 }
476 
477 //////////////////////////////////////////////////////////////////////////////
478 
DoConvertToPolyObj(sal_Bool bBezier,bool bAddText) const479 SdrObject* SdrTextObj::DoConvertToPolyObj(sal_Bool bBezier, bool bAddText) const
480 {
481     if(bAddText)
482     {
483         return ImpConvertContainedTextToSdrPathObjs(!bBezier);
484     }
485 
486     return 0;
487 }
488 
ImpCanConvTextToCurve() const489 bool SdrTextObj::ImpCanConvTextToCurve() const
490 {
491     return !IsOutlText();
492 }
493 
ImpConvertMakeObj(const basegfx::B2DPolyPolygon & rPolyPolygon,sal_Bool bClosed,sal_Bool bBezier,sal_Bool bNoSetAttr) const494 SdrObject* SdrTextObj::ImpConvertMakeObj(const basegfx::B2DPolyPolygon& rPolyPolygon, sal_Bool bClosed, sal_Bool bBezier, sal_Bool bNoSetAttr) const
495 {
496     SdrObjKind ePathKind = bClosed ? OBJ_PATHFILL : OBJ_PATHLINE;
497     basegfx::B2DPolyPolygon aB2DPolyPolygon(rPolyPolygon);
498 
499     // #i37011#
500     if(!bBezier)
501     {
502         aB2DPolyPolygon = basegfx::tools::adaptiveSubdivideByAngle(aB2DPolyPolygon);
503         ePathKind = bClosed ? OBJ_POLY : OBJ_PLIN;
504     }
505 
506     SdrPathObj* pPathObj = new SdrPathObj(ePathKind, aB2DPolyPolygon);
507 
508     if(bBezier)
509     {
510         // create bezier curves
511         pPathObj->SetPathPoly(basegfx::tools::expandToCurve(pPathObj->GetPathPoly()));
512     }
513 
514     if(pPathObj)
515     {
516         pPathObj->ImpSetAnchorPos(aAnchor);
517         pPathObj->NbcSetLayer(SdrLayerID(GetLayer()));
518 
519         if(pModel)
520         {
521             pPathObj->SetModel(pModel);
522 
523             if(!bNoSetAttr)
524             {
525                 sdr::properties::ItemChangeBroadcaster aC(*pPathObj);
526 
527                 pPathObj->ClearMergedItem();
528                 pPathObj->SetMergedItemSet(GetObjectItemSet());
529                 pPathObj->GetProperties().BroadcastItemChange(aC);
530                 pPathObj->NbcSetStyleSheet(GetStyleSheet(), sal_True);
531             }
532         }
533     }
534 
535     return pPathObj;
536 }
537 
ImpConvertAddText(SdrObject * pObj,FASTBOOL bBezier) const538 SdrObject* SdrTextObj::ImpConvertAddText(SdrObject* pObj, FASTBOOL bBezier) const
539 {
540     if(!ImpCanConvTextToCurve())
541     {
542         return pObj;
543     }
544 
545     SdrObject* pText = ImpConvertContainedTextToSdrPathObjs(!bBezier);
546 
547     if(!pText)
548     {
549         return pObj;
550     }
551 
552     if(!pObj)
553     {
554         return pText;
555     }
556 
557     if(pText->IsGroupObject())
558     {
559         // is already group object, add partial shape in front
560         SdrObjList* pOL=pText->GetSubList();
561         pOL->InsertObject(pObj,0);
562 
563         return pText;
564     }
565     else
566     {
567         // not yet a group, create one and add partial and new shapes
568         SdrObjGroup* pGrp=new SdrObjGroup;
569         SdrObjList* pOL=pGrp->GetSubList();
570         pOL->InsertObject(pObj);
571         pOL->InsertObject(pText);
572 
573         return pGrp;
574     }
575 }
576 
577 //////////////////////////////////////////////////////////////////////////////
578 // eof
579