1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <svx/svdtrans.hxx>
#include <math.h>
#include <svx/xpoly.hxx>
#include <rtl/ustrbuf.hxx>
#include <vcl/virdev.hxx>
#include <tools/bigint.hxx>
#include <tools/UnitConversion.hxx>
#include <unotools/syslocale.hxx>
#include <unotools/localedatawrapper.hxx>
#include <sal/log.hxx>
void MoveXPoly(XPolygon& rPoly, const Size& S)
{
rPoly.Move(S.Width(),S.Height());
}
void ResizeRect(tools::Rectangle& rRect, const Point& rRef, const Fraction& rxFact, const Fraction& ryFact)
{
Fraction aXFact(rxFact);
Fraction aYFact(ryFact);
if (!aXFact.IsValid()) {
SAL_WARN( "svx.svdraw", "invalid fraction xFract, using Fraction(1,1)" );
aXFact = Fraction(1,1);
tools::Long nWdt = rRect.Right() - rRect.Left();
if (nWdt == 0) rRect.AdjustRight( 1 );
}
rRect.SetLeft( rRef.X() + FRound( (rRect.Left() - rRef.X()) * double(aXFact) ) );
rRect.SetRight( rRef.X() + FRound( (rRect.Right() - rRef.X()) * double(aXFact) ) );
if (!aYFact.IsValid()) {
SAL_WARN( "svx.svdraw", "invalid fraction yFract, using Fraction(1,1)" );
aYFact = Fraction(1,1);
tools::Long nHgt = rRect.Bottom() - rRect.Top();
if (nHgt == 0) rRect.AdjustBottom( 1 );
}
rRect.SetTop( rRef.Y() + FRound( (rRect.Top() - rRef.Y()) * double(aYFact) ) );
rRect.SetBottom( rRef.Y() + FRound( (rRect.Bottom() - rRef.Y()) * double(aYFact) ) );
rRect.Normalize();
}
void ResizePoly(tools::Polygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact)
{
sal_uInt16 nCount=rPoly.GetSize();
for (sal_uInt16 i=0; i<nCount; i++) {
ResizePoint(rPoly[i],rRef,xFact,yFact);
}
}
void ResizeXPoly(XPolygon& rPoly, const Point& rRef, const Fraction& xFact, const Fraction& yFact)
{
sal_uInt16 nCount=rPoly.GetPointCount();
for (sal_uInt16 i=0; i<nCount; i++) {
ResizePoint(rPoly[i],rRef,xFact,yFact);
}
}
void RotatePoly(tools::Polygon& rPoly, const Point& rRef, double sn, double cs)
{
sal_uInt16 nCount=rPoly.GetSize();
for (sal_uInt16 i=0; i<nCount; i++) {
RotatePoint(rPoly[i],rRef,sn,cs);
}
}
void RotateXPoly(XPolygon& rPoly, const Point& rRef, double sn, double cs)
{
sal_uInt16 nCount=rPoly.GetPointCount();
for (sal_uInt16 i=0; i<nCount; i++) {
RotatePoint(rPoly[i],rRef,sn,cs);
}
}
void RotateXPoly(XPolyPolygon& rPoly, const Point& rRef, double sn, double cs)
{
sal_uInt16 nCount=rPoly.Count();
for (sal_uInt16 i=0; i<nCount; i++) {
RotateXPoly(rPoly[i],rRef,sn,cs);
}
}
void MirrorPoint(Point& rPnt, const Point& rRef1, const Point& rRef2)
{
tools::Long mx=rRef2.X()-rRef1.X();
tools::Long my=rRef2.Y()-rRef1.Y();
if (mx==0) { // vertical axis
tools::Long dx=rRef1.X()-rPnt.X();
rPnt.AdjustX(2*dx );
} else if (my==0) { // horizontal axis
tools::Long dy=rRef1.Y()-rPnt.Y();
rPnt.AdjustY(2*dy );
} else if (mx==my) { // diagonal axis '\'
tools::Long dx1=rPnt.X()-rRef1.X();
tools::Long dy1=rPnt.Y()-rRef1.Y();
rPnt.setX(rRef1.X()+dy1 );
rPnt.setY(rRef1.Y()+dx1 );
} else if (mx==-my) { // diagonal axis '/'
tools::Long dx1=rPnt.X()-rRef1.X();
tools::Long dy1=rPnt.Y()-rRef1.Y();
rPnt.setX(rRef1.X()-dy1 );
rPnt.setY(rRef1.Y()-dx1 );
} else { // arbitrary axis
// TODO: Optimize this! Raise perpendicular on the mirroring axis..?
Degree100 nRefAngle=GetAngle(rRef2-rRef1);
rPnt-=rRef1;
Degree100 nPntAngle=GetAngle(rPnt);
Degree100 nAngle=2_deg100*(nRefAngle-nPntAngle);
double a = toRadians(nAngle);
double nSin=sin(a);
double nCos=cos(a);
RotatePoint(rPnt,Point(),nSin,nCos);
rPnt+=rRef1;
}
}
void MirrorXPoly(XPolygon& rPoly, const Point& rRef1, const Point& rRef2)
{
sal_uInt16 nCount=rPoly.GetPointCount();
for (sal_uInt16 i=0; i<nCount; i++) {
MirrorPoint(rPoly[i],rRef1,rRef2);
}
}
void ShearPoly(tools::Polygon& rPoly, const Point& rRef, double tn)
{
sal_uInt16 nCount=rPoly.GetSize();
for (sal_uInt16 i=0; i<nCount; i++) {
ShearPoint(rPoly[i],rRef,tn);
}
}
void ShearXPoly(XPolygon& rPoly, const Point& rRef, double tn, bool bVShear)
{
sal_uInt16 nCount=rPoly.GetPointCount();
for (sal_uInt16 i=0; i<nCount; i++) {
ShearPoint(rPoly[i],rRef,tn,bVShear);
}
}
double CrookRotateXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
const Point& rRad, double& rSin, double& rCos, bool bVert)
{
bool bC1=pC1!=nullptr;
bool bC2=pC2!=nullptr;
tools::Long x0=rPnt.X();
tools::Long y0=rPnt.Y();
tools::Long cx=rCenter.X();
tools::Long cy=rCenter.Y();
double nAngle=GetCrookAngle(rPnt,rCenter,rRad,bVert);
double sn=sin(nAngle);
double cs=cos(nAngle);
RotatePoint(rPnt,rCenter,sn,cs);
if (bC1) {
if (bVert) {
// move into the direction of the center, as a basic position for the rotation
pC1->AdjustY( -y0 );
// resize, account for the distance from the center
pC1->setY(FRound(static_cast<double>(pC1->Y()) /rRad.X()*(cx-pC1->X())) );
pC1->AdjustY(cy );
} else {
// move into the direction of the center, as a basic position for the rotation
pC1->AdjustX( -x0 );
// resize, account for the distance from the center
tools::Long nPntRad=cy-pC1->Y();
double nFact=static_cast<double>(nPntRad)/static_cast<double>(rRad.Y());
pC1->setX(FRound(static_cast<double>(pC1->X())*nFact) );
pC1->AdjustX(cx );
}
RotatePoint(*pC1,rCenter,sn,cs);
}
if (bC2) {
if (bVert) {
// move into the direction of the center, as a basic position for the rotation
pC2->AdjustY( -y0 );
// resize, account for the distance from the center
pC2->setY(FRound(static_cast<double>(pC2->Y()) /rRad.X()*(rCenter.X()-pC2->X())) );
pC2->AdjustY(cy );
} else {
// move into the direction of the center, as a basic position for the rotation
pC2->AdjustX( -x0 );
// resize, account for the distance from the center
tools::Long nPntRad=rCenter.Y()-pC2->Y();
double nFact=static_cast<double>(nPntRad)/static_cast<double>(rRad.Y());
pC2->setX(FRound(static_cast<double>(pC2->X())*nFact) );
pC2->AdjustX(cx );
}
RotatePoint(*pC2,rCenter,sn,cs);
}
rSin=sn;
rCos=cs;
return nAngle;
}
double CrookSlantXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
const Point& rRad, double& rSin, double& rCos, bool bVert)
{
bool bC1=pC1!=nullptr;
bool bC2=pC2!=nullptr;
tools::Long x0=rPnt.X();
tools::Long y0=rPnt.Y();
tools::Long dx1=0,dy1=0;
tools::Long dxC1=0,dyC1=0;
tools::Long dxC2=0,dyC2=0;
if (bVert) {
tools::Long nStart=rCenter.X()-rRad.X();
dx1=rPnt.X()-nStart;
rPnt.setX(nStart );
if (bC1) {
dxC1=pC1->X()-nStart;
pC1->setX(nStart );
}
if (bC2) {
dxC2=pC2->X()-nStart;
pC2->setX(nStart );
}
} else {
tools::Long nStart=rCenter.Y()-rRad.Y();
dy1=rPnt.Y()-nStart;
rPnt.setY(nStart );
if (bC1) {
dyC1=pC1->Y()-nStart;
pC1->setY(nStart );
}
if (bC2) {
dyC2=pC2->Y()-nStart;
pC2->setY(nStart );
}
}
double nAngle=GetCrookAngle(rPnt,rCenter,rRad,bVert);
double sn=sin(nAngle);
double cs=cos(nAngle);
RotatePoint(rPnt,rCenter,sn,cs);
if (bC1) { if (bVert) pC1->AdjustY( -(y0-rCenter.Y()) ); else pC1->AdjustX( -(x0-rCenter.X()) ); RotatePoint(*pC1,rCenter,sn,cs); }
if (bC2) { if (bVert) pC2->AdjustY( -(y0-rCenter.Y()) ); else pC2->AdjustX( -(x0-rCenter.X()) ); RotatePoint(*pC2,rCenter,sn,cs); }
if (bVert) {
rPnt.AdjustX(dx1 );
if (bC1) pC1->AdjustX(dxC1 );
if (bC2) pC2->AdjustX(dxC2 );
} else {
rPnt.AdjustY(dy1 );
if (bC1) pC1->AdjustY(dyC1 );
if (bC2) pC2->AdjustY(dyC2 );
}
rSin=sn;
rCos=cs;
return nAngle;
}
double CrookStretchXPoint(Point& rPnt, Point* pC1, Point* pC2, const Point& rCenter,
const Point& rRad, double& rSin, double& rCos, bool bVert,
const tools::Rectangle& rRefRect)
{
tools::Long y0=rPnt.Y();
CrookSlantXPoint(rPnt,pC1,pC2,rCenter,rRad,rSin,rCos,bVert);
if (bVert) {
} else {
tools::Long nTop=rRefRect.Top();
tools::Long nBtm=rRefRect.Bottom();
tools::Long nHgt=nBtm-nTop;
tools::Long dy=rPnt.Y()-y0;
double a=static_cast<double>(y0-nTop)/nHgt;
a*=dy;
rPnt.setY(y0+FRound(a) );
}
return 0.0;
}
void CrookRotatePoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert)
{
double nSin,nCos;
sal_uInt16 nPointCnt=rPoly.GetPointCount();
sal_uInt16 i=0;
while (i<nPointCnt) {
Point* pPnt=&rPoly[i];
Point* pC1=nullptr;
Point* pC2=nullptr;
if (i+1<nPointCnt && rPoly.IsControl(i)) { // control point to the left
pC1=pPnt;
i++;
pPnt=&rPoly[i];
}
i++;
if (i<nPointCnt && rPoly.IsControl(i)) { // control point to the right
pC2=&rPoly[i];
i++;
}
CrookRotateXPoint(*pPnt,pC1,pC2,rCenter,rRad,nSin,nCos,bVert);
}
}
void CrookSlantPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert)
{
double nSin,nCos;
sal_uInt16 nPointCnt=rPoly.GetPointCount();
sal_uInt16 i=0;
while (i<nPointCnt) {
Point* pPnt=&rPoly[i];
Point* pC1=nullptr;
Point* pC2=nullptr;
if (i+1<nPointCnt && rPoly.IsControl(i)) { // control point to the left
pC1=pPnt;
i++;
pPnt=&rPoly[i];
}
i++;
if (i<nPointCnt && rPoly.IsControl(i)) { // control point to the right
pC2=&rPoly[i];
i++;
}
CrookSlantXPoint(*pPnt,pC1,pC2,rCenter,rRad,nSin,nCos,bVert);
}
}
void CrookStretchPoly(XPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect)
{
double nSin,nCos;
sal_uInt16 nPointCnt=rPoly.GetPointCount();
sal_uInt16 i=0;
while (i<nPointCnt) {
Point* pPnt=&rPoly[i];
Point* pC1=nullptr;
Point* pC2=nullptr;
if (i+1<nPointCnt && rPoly.IsControl(i)) { // control point to the left
pC1=pPnt;
i++;
pPnt=&rPoly[i];
}
i++;
if (i<nPointCnt && rPoly.IsControl(i)) { // control point to the right
pC2=&rPoly[i];
i++;
}
CrookStretchXPoint(*pPnt,pC1,pC2,rCenter,rRad,nSin,nCos,bVert,rRefRect);
}
}
void CrookRotatePoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert)
{
sal_uInt16 nPolyCount=rPoly.Count();
for (sal_uInt16 nPolyNum=0; nPolyNum<nPolyCount; nPolyNum++) {
CrookRotatePoly(rPoly[nPolyNum],rCenter,rRad,bVert);
}
}
void CrookSlantPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert)
{
sal_uInt16 nPolyCount=rPoly.Count();
for (sal_uInt16 nPolyNum=0; nPolyNum<nPolyCount; nPolyNum++) {
CrookSlantPoly(rPoly[nPolyNum],rCenter,rRad,bVert);
}
}
void CrookStretchPoly(XPolyPolygon& rPoly, const Point& rCenter, const Point& rRad, bool bVert, const tools::Rectangle& rRefRect)
{
sal_uInt16 nPolyCount=rPoly.Count();
for (sal_uInt16 nPolyNum=0; nPolyNum<nPolyCount; nPolyNum++) {
CrookStretchPoly(rPoly[nPolyNum],rCenter,rRad,bVert,rRefRect);
}
}
Degree100 GetAngle(const Point& rPnt)
{
Degree100 a;
if (rPnt.Y()==0) {
if (rPnt.X()<0) a=-18000_deg100;
} else if (rPnt.X()==0) {
if (rPnt.Y()>0) a=-9000_deg100;
else a=9000_deg100;
} else {
a = Degree100(FRound(basegfx::rad2deg<100>(atan2(static_cast<double>(-rPnt.Y()), static_cast<double>(rPnt.X())))));
}
return a;
}
Degree100 NormAngle18000(Degree100 a)
{
while (a<-18000_deg100) a+=36000_deg100;
while (a>=18000_deg100) a-=36000_deg100;
return a;
}
Degree100 NormAngle36000(Degree100 a)
{
a %= 36000_deg100;
if (a < 0_deg100)
a += 36000_deg100;
return a;
}
sal_uInt16 GetAngleSector(Degree100 nAngle) { return (NormAngle36000(nAngle) / 9000_deg100).get(); }
tools::Long GetLen(const Point& rPnt)
{
tools::Long x=std::abs(rPnt.X());
tools::Long y=std::abs(rPnt.Y());
if (x+y<0x8000) { // because 7FFF * 7FFF * 2 = 7FFE0002
x*=x;
y*=y;
x+=y;
x=FRound(sqrt(static_cast<double>(x)));
return x;
} else {
double nx=x;
double ny=y;
nx*=nx;
ny*=ny;
nx+=ny;
nx=sqrt(nx);
if (nx>0x7FFFFFFF) {
return 0x7FFFFFFF; // we can't go any further, for fear of an overrun!
} else {
return FRound(nx);
}
}
}
void GeoStat::RecalcSinCos()
{
if (m_nRotationAngle==0_deg100) {
mfSinRotationAngle=0.0;
mfCosRotationAngle=1.0;
} else {
double a = toRadians(m_nRotationAngle);
mfSinRotationAngle=sin(a);
mfCosRotationAngle=cos(a);
}
}
void GeoStat::RecalcTan()
{
if (m_nShearAngle==0_deg100) {
mfTanShearAngle=0.0;
} else {
double a = toRadians(m_nShearAngle);
mfTanShearAngle=tan(a);
}
}
tools::Polygon Rect2Poly(const tools::Rectangle& rRect, const GeoStat& rGeo)
{
tools::Polygon aPol(5);
aPol[0]=rRect.TopLeft();
aPol[1]=rRect.TopRight();
aPol[2]=rRect.BottomRight();
aPol[3]=rRect.BottomLeft();
aPol[4]=rRect.TopLeft();
if (rGeo.m_nShearAngle) ShearPoly(aPol,rRect.TopLeft(),rGeo.mfTanShearAngle);
if (rGeo.m_nRotationAngle) RotatePoly(aPol,rRect.TopLeft(),rGeo.mfSinRotationAngle,rGeo.mfCosRotationAngle);
return aPol;
}
namespace svx
{
tools::Rectangle polygonToRectangle(const tools::Polygon& rPolygon, GeoStat& rGeo)
{
rGeo.m_nRotationAngle = GetAngle(rPolygon[1] - rPolygon[0]);
rGeo.m_nRotationAngle = NormAngle36000(rGeo.m_nRotationAngle);
// rotation successful
rGeo.RecalcSinCos();
Point aPoint1(rPolygon[1] - rPolygon[0]);
if (rGeo.m_nRotationAngle)
RotatePoint(aPoint1, Point(0,0), -rGeo.mfSinRotationAngle, rGeo.mfCosRotationAngle); // -Sin to reverse rotation
tools::Long nWidth = aPoint1.X();
Point aPoint0(rPolygon[0]);
Point aPoint3(rPolygon[3] - rPolygon[0]);
if (rGeo.m_nRotationAngle)
RotatePoint(aPoint3, Point(0,0), -rGeo.mfSinRotationAngle, rGeo.mfCosRotationAngle); // -Sin to reverse rotation
tools::Long nHeight = aPoint3.Y();
Degree100 nShearAngle = GetAngle(aPoint3);
nShearAngle -= 27000_deg100; // ShearWink is measured against a vertical line
nShearAngle = -nShearAngle; // negating, because '+' is shearing clock-wise
bool bMirror = aPoint3.Y() < 0;
if (bMirror)
{ // "exchange of points" when mirroring
nHeight = -nHeight;
nShearAngle += 18000_deg100;
aPoint0 = rPolygon[3];
}
nShearAngle = NormAngle18000(nShearAngle);
if (nShearAngle < -9000_deg100 || nShearAngle > 9000_deg100)
{
nShearAngle = NormAngle18000(nShearAngle + 18000_deg100);
}
if (nShearAngle < -SDRMAXSHEAR)
nShearAngle = -SDRMAXSHEAR; // limit ShearWinkel (shear angle) to +/- 89.00 deg
if (nShearAngle > SDRMAXSHEAR)
nShearAngle = SDRMAXSHEAR;
rGeo.m_nShearAngle = nShearAngle;
rGeo.RecalcTan();
Point aRU(aPoint0);
aRU.AdjustX(nWidth);
aRU.AdjustY(nHeight);
return tools::Rectangle(aPoint0, aRU);
}
} // end svx
void OrthoDistance8(const Point& rPt0, Point& rPt, bool bBigOrtho)
{
tools::Long dx=rPt.X()-rPt0.X();
tools::Long dy=rPt.Y()-rPt0.Y();
tools::Long dxa=std::abs(dx);
tools::Long dya=std::abs(dy);
if (dx==0 || dy==0 || dxa==dya) return;
if (dxa>=dya*2) { rPt.setY(rPt0.Y() ); return; }
if (dya>=dxa*2) { rPt.setX(rPt0.X() ); return; }
if ((dxa<dya) != bBigOrtho) {
rPt.setY(rPt0.Y()+(dxa* (dy>=0 ? 1 : -1) ) );
} else {
rPt.setX(rPt0.X()+(dya* (dx>=0 ? 1 : -1) ) );
}
}
void OrthoDistance4(const Point& rPt0, Point& rPt, bool bBigOrtho)
{
tools::Long dx=rPt.X()-rPt0.X();
tools::Long dy=rPt.Y()-rPt0.Y();
tools::Long dxa=std::abs(dx);
tools::Long dya=std::abs(dy);
if ((dxa<dya) != bBigOrtho) {
rPt.setY(rPt0.Y()+(dxa* (dy>=0 ? 1 : -1) ) );
} else {
rPt.setX(rPt0.X()+(dya* (dx>=0 ? 1 : -1) ) );
}
}
tools::Long BigMulDiv(tools::Long nVal, tools::Long nMul, tools::Long nDiv)
{
if (!nDiv)
return 0x7fffffff;
return BigInt::Scale(nVal, nMul, nDiv);
}
static FrPair toPair(o3tl::Length eFrom, o3tl::Length eTo)
{
const auto& [nNum, nDen] = o3tl::getConversionMulDiv(eFrom, eTo);
return FrPair(nNum, nDen);
}
// How many eU units fit into a mm, respectively an inch?
// Or: How many mm, respectively inches, are there in an eU (and then give me the inverse)
static FrPair GetInchOrMM(MapUnit eU)
{
switch (eU) {
case MapUnit::Map1000thInch: return toPair(o3tl::Length::in, o3tl::Length::in1000);
case MapUnit::Map100thInch : return toPair(o3tl::Length::in, o3tl::Length::in100);
case MapUnit::Map10thInch : return toPair(o3tl::Length::in, o3tl::Length::in10);
case MapUnit::MapInch : return toPair(o3tl::Length::in, o3tl::Length::in);
case MapUnit::MapPoint : return toPair(o3tl::Length::in, o3tl::Length::pt);
case MapUnit::MapTwip : return toPair(o3tl::Length::in, o3tl::Length::twip);
case MapUnit::Map100thMM : return toPair(o3tl::Length::mm, o3tl::Length::mm100);
case MapUnit::Map10thMM : return toPair(o3tl::Length::mm, o3tl::Length::mm10);
case MapUnit::MapMM : return toPair(o3tl::Length::mm, o3tl::Length::mm);
case MapUnit::MapCM : return toPair(o3tl::Length::mm, o3tl::Length::cm);
case MapUnit::MapPixel : {
ScopedVclPtrInstance< VirtualDevice > pVD;
pVD->SetMapMode(MapMode(MapUnit::Map100thMM));
Point aP(pVD->PixelToLogic(Point(64,64))); // 64 pixels for more accuracy
return FrPair(6400,aP.X(),6400,aP.Y());
}
case MapUnit::MapAppFont: case MapUnit::MapSysFont: {
ScopedVclPtrInstance< VirtualDevice > pVD;
pVD->SetMapMode(MapMode(eU));
Point aP(pVD->LogicToPixel(Point(32,32))); // 32 units for more accuracy
pVD->SetMapMode(MapMode(MapUnit::Map100thMM));
aP=pVD->PixelToLogic(aP);
return FrPair(3200,aP.X(),3200,aP.Y());
}
default: break;
}
return Fraction(1,1);
}
// Calculate the factor that we need to convert units from eS to eD.
// e. g. GetMapFactor(UNIT_MM,UNIT_100TH_MM) => 100.
FrPair GetMapFactor(MapUnit eS, MapUnit eD)
{
if (eS==eD) return FrPair(1,1,1,1);
const auto eFrom = MapToO3tlLength(eS, o3tl::Length::invalid);
const auto eTo = MapToO3tlLength(eD, o3tl::Length::invalid);
if (eFrom != o3tl::Length::invalid && eTo != o3tl::Length::invalid)
return toPair(eFrom, eTo);
FrPair aS(GetInchOrMM(eS));
FrPair aD(GetInchOrMM(eD));
bool bSInch=IsInch(eS);
bool bDInch=IsInch(eD);
FrPair aRet(aD.X()/aS.X(),aD.Y()/aS.Y());
if (bSInch && !bDInch) { aRet.X()*=Fraction(127,5); aRet.Y()*=Fraction(127,5); }
if (!bSInch && bDInch) { aRet.X()*=Fraction(5,127); aRet.Y()*=Fraction(5,127); }
return aRet;
};
FrPair GetMapFactor(FieldUnit eS, FieldUnit eD)
{
if (eS==eD) return FrPair(1,1,1,1);
auto eFrom = FieldToO3tlLength(eS), eTo = FieldToO3tlLength(eD);
if (eFrom == o3tl::Length::invalid)
{
if (eTo == o3tl::Length::invalid)
return FrPair(1,1,1,1);
eFrom = IsInch(eD) ? o3tl::Length::in : o3tl::Length::mm;
}
else if (eTo == o3tl::Length::invalid)
eTo = IsInch(eS) ? o3tl::Length::in : o3tl::Length::mm;
return toPair(eFrom, eTo);
};
void SdrFormatter::Undirty()
{
const o3tl::Length eFrom = MapToO3tlLength(m_eSrcMU, o3tl::Length::invalid);
const o3tl::Length eTo = MapToO3tlLength(m_eDstMU, o3tl::Length::invalid);
if (eFrom != o3tl::Length::invalid && eTo != o3tl::Length::invalid)
{
const auto& [mul, div] = o3tl::getConversionMulDiv(eFrom, eTo);
sal_Int64 nMul = mul;
sal_Int64 nDiv = div;
short nComma = 0;
// shorten trailing zeros for dividend
while (0 == (nMul % 10))
{
nComma--;
nMul /= 10;
}
// shorten trailing zeros for divisor
while (0 == (nDiv % 10))
{
nComma++;
nDiv /= 10;
}
m_nMul = nMul;
m_nDiv = nDiv;
m_nComma = nComma;
}
else
{
m_nMul = m_nDiv = 1;
m_nComma = 0;
}
m_bDirty=false;
}
OUString SdrFormatter::GetStr(tools::Long nVal) const
{
static constexpr OUString aNullCode(u"0"_ustr);
if(!nVal)
{
return aNullCode;
}
// we may lose some decimal places here, because of MulDiv instead of Real
bool bNeg(nVal < 0);
SvtSysLocale aSysLoc;
const LocaleDataWrapper& rLoc = aSysLoc.GetLocaleData();
if (m_bDirty)
const_cast<SdrFormatter*>(this)->Undirty();
sal_Int16 nC(m_nComma);
if(bNeg)
nVal = -nVal;
while(nC <= -3)
{
nVal *= 1000;
nC += 3;
}
while(nC <= -1)
{
nVal *= 10;
nC++;
}
if(m_nMul != m_nDiv)
nVal = BigMulDiv(nVal, m_nMul, m_nDiv);
OUStringBuffer aStr = OUString::number(nVal);
if(nC > 0 && aStr.getLength() <= nC )
{
// decimal separator necessary
sal_Int32 nCount(nC - aStr.getLength());
if(nCount >= 0 && LocaleDataWrapper::isNumLeadingZero())
nCount++;
for(sal_Int32 i=0; i<nCount; i++)
aStr.insert(0, aNullCode);
// remove superfluous decimal points
sal_Int32 nNumDigits(LocaleDataWrapper::getNumDigits());
sal_Int32 nWeg(nC - nNumDigits);
if(nWeg > 0)
{
// TODO: we should round here
aStr.remove(aStr.getLength() - nWeg, nWeg);
nC = nNumDigits;
}
}
// remember everything before the decimal separator for later
sal_Int32 nForComma(aStr.getLength() - nC);
if(nC > 0)
{
// insert comma char (decimal separator)
// remove trailing zeros
while(nC > 0 && aStr[aStr.getLength() - 1] == aNullCode.getStr()[0])
{
aStr.remove(aStr.getLength() - 1, 1);
nC--;
}
if(nC > 0)
{
// do we still have decimal places?
sal_Unicode cDec(rLoc.getNumDecimalSep()[0]);
aStr.insert(nForComma, cDec);
}
}
// add in thousands separator (if necessary)
if( nForComma > 3 )
{
const OUString& aThoSep( rLoc.getNumThousandSep() );
if ( aThoSep.getLength() > 0 )
{
sal_Unicode cTho( aThoSep[0] );
sal_Int32 i(nForComma - 3);
while(i > 0)
{
aStr.insert(i, cTho);
i -= 3;
}
}
}
if(aStr.isEmpty())
aStr.append(aNullCode);
if(bNeg && (aStr.getLength() > 1 || aStr[0] != aNullCode.getStr()[0]))
{
aStr.insert(0, "-");
}
return aStr.makeStringAndClear();
}
OUString SdrFormatter::GetUnitStr(MapUnit eUnit)
{
switch(eUnit)
{
// metrically
case MapUnit::Map100thMM :
return "/100mm";
case MapUnit::Map10thMM :
return "/10mm";
case MapUnit::MapMM :
return "mm";
case MapUnit::MapCM :
return "cm";
// Inch
case MapUnit::Map1000thInch:
return "/1000\"";
case MapUnit::Map100thInch :
return "/100\"";
case MapUnit::Map10thInch :
return "/10\"";
case MapUnit::MapInch :
return "\"";
case MapUnit::MapPoint :
return "pt";
case MapUnit::MapTwip :
return "twip";
// others
case MapUnit::MapPixel :
return "pixel";
case MapUnit::MapSysFont :
return "sysfont";
case MapUnit::MapAppFont :
return "appfont";
case MapUnit::MapRelative :
return "%";
default:
return OUString();
}
}
OUString SdrFormatter::GetUnitStr(FieldUnit eUnit)
{
switch(eUnit)
{
default :
case FieldUnit::NONE :
case FieldUnit::CUSTOM :
return OUString();
// metrically
case FieldUnit::MM_100TH:
return "/100mm";
case FieldUnit::MM :
return "mm";
case FieldUnit::CM :
return "cm";
case FieldUnit::M :
return "m";
case FieldUnit::KM :
return "km";
// Inch
case FieldUnit::TWIP :
return "twip";
case FieldUnit::POINT :
return "pt";
case FieldUnit::PICA :
return "pica";
case FieldUnit::INCH :
return "\"";
case FieldUnit::FOOT :
return "ft";
case FieldUnit::MILE :
return "mile(s)";
// others
case FieldUnit::PERCENT:
return "%";
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|