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 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolypolygontools.hxx>
#include <basegfx/polygon/b2dpolygontools.hxx>
#include <basegfx/polygon/b2dpolypolygon.hxx>
#include <basegfx/matrix/b2dhommatrix.hxx>
#include <basegfx/matrix/b2dhommatrixtools.hxx>
#include <rtl/ustring.hxx>
#include <rtl/math.hxx>
namespace basegfx
{
namespace tools
{
namespace
{
void lcl_skipSpaces(sal_Int32& io_rPos,
const ::rtl::OUString& rStr,
const sal_Int32 nLen)
{
while( io_rPos < nLen &&
sal_Unicode(' ') == rStr[io_rPos] )
{
++io_rPos;
}
}
void lcl_skipSpacesAndCommas(sal_Int32& io_rPos,
const ::rtl::OUString& rStr,
const sal_Int32 nLen)
{
while(io_rPos < nLen
&& (sal_Unicode(' ') == rStr[io_rPos] || sal_Unicode(',') == rStr[io_rPos]))
{
++io_rPos;
}
}
inline bool lcl_isOnNumberChar(const sal_Unicode aChar, bool bSignAllowed = true)
{
const bool bPredicate( (sal_Unicode('0') <= aChar && sal_Unicode('9') >= aChar)
|| (bSignAllowed && sal_Unicode('+') == aChar)
|| (bSignAllowed && sal_Unicode('-') == aChar) );
return bPredicate;
}
inline bool lcl_isOnNumberChar(const ::rtl::OUString& rStr, const sal_Int32 nPos, bool bSignAllowed = true)
{
return lcl_isOnNumberChar(rStr[nPos],
bSignAllowed);
}
bool lcl_getDoubleChar(double& o_fRetval,
sal_Int32& io_rPos,
const ::rtl::OUString& rStr,
const sal_Int32 /*nLen*/)
{
sal_Unicode aChar( rStr[io_rPos] );
::rtl::OUStringBuffer sNumberString;
if(sal_Unicode('+') == aChar || sal_Unicode('-') == aChar)
{
sNumberString.append(rStr[io_rPos]);
aChar = rStr[++io_rPos];
}
while((sal_Unicode('0') <= aChar && sal_Unicode('9') >= aChar)
|| sal_Unicode('.') == aChar)
{
sNumberString.append(rStr[io_rPos]);
aChar = rStr[++io_rPos];
}
if(sal_Unicode('e') == aChar || sal_Unicode('E') == aChar)
{
sNumberString.append(rStr[io_rPos]);
aChar = rStr[++io_rPos];
if(sal_Unicode('+') == aChar || sal_Unicode('-') == aChar)
{
sNumberString.append(rStr[io_rPos]);
aChar = rStr[++io_rPos];
}
while(sal_Unicode('0') <= aChar && sal_Unicode('9') >= aChar)
{
sNumberString.append(rStr[io_rPos]);
aChar = rStr[++io_rPos];
}
}
if(sNumberString.getLength())
{
rtl_math_ConversionStatus eStatus;
o_fRetval = ::rtl::math::stringToDouble( sNumberString.makeStringAndClear(),
(sal_Unicode)('.'),
(sal_Unicode)(','),
&eStatus,
NULL );
return ( eStatus == rtl_math_ConversionStatus_Ok );
}
return false;
}
bool lcl_importDoubleAndSpaces( double& o_fRetval,
sal_Int32& io_rPos,
const ::rtl::OUString& rStr,
const sal_Int32 nLen )
{
if( !lcl_getDoubleChar(o_fRetval, io_rPos, rStr, nLen) )
return false;
lcl_skipSpacesAndCommas(io_rPos, rStr, nLen);
return true;
}
bool lcl_importFlagAndSpaces(sal_Int32& o_nRetval,
sal_Int32& io_rPos,
const ::rtl::OUString& rStr,
const sal_Int32 nLen)
{
sal_Unicode aChar( rStr[io_rPos] );
if(sal_Unicode('0') == aChar)
{
o_nRetval = 0;
++io_rPos;
}
else if (sal_Unicode('1') == aChar)
{
o_nRetval = 1;
++io_rPos;
}
else
return false;
lcl_skipSpacesAndCommas(io_rPos, rStr, nLen);
return true;
}
void lcl_putNumberChar( ::rtl::OUStringBuffer& rStr,
double fValue )
{
rStr.append( fValue );
}
void lcl_putNumberCharWithSpace( ::rtl::OUStringBuffer& rStr,
double fValue,
double fOldValue,
bool bUseRelativeCoordinates )
{
if( bUseRelativeCoordinates )
fValue -= fOldValue;
const sal_Int32 aLen( rStr.getLength() );
if(aLen > 0)
{
if( lcl_isOnNumberChar(rStr[aLen - 1], false) &&
fValue >= 0.0 )
{
rStr.append( sal_Unicode(' ') );
}
}
lcl_putNumberChar(rStr, fValue);
}
inline sal_Unicode lcl_getCommand( sal_Char cUpperCaseCommand,
sal_Char cLowerCaseCommand,
bool bUseRelativeCoordinates )
{
return bUseRelativeCoordinates ? cLowerCaseCommand : cUpperCaseCommand;
}
}
bool importFromSvgD(B2DPolyPolygon& o_rPolyPolygon, const ::rtl::OUString& rSvgDStatement, bool bWrongPositionAfterZ)
{
o_rPolyPolygon.clear();
const sal_Int32 nLen(rSvgDStatement.getLength());
sal_Int32 nPos(0);
bool bIsClosed(false);
double nLastX( 0.0 );
double nLastY( 0.0 );
B2DPolygon aCurrPoly;
// skip initial whitespace
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen)
{
bool bRelative(false);
bool bMoveTo(false);
const sal_Unicode aCurrChar(rSvgDStatement[nPos]);
switch(aCurrChar)
{
case 'z' :
case 'Z' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
// remember closed state of current polygon
bIsClosed = true;
// update current point - we're back at the start
if( aCurrPoly.count() && !bWrongPositionAfterZ)
{
const B2DPoint aFirst( aCurrPoly.getB2DPoint(0) );
nLastX = aFirst.getX();
nLastY = aFirst.getY();
}
break;
}
case 'm' :
case 'M' :
{
bMoveTo = true;
// FALLTHROUGH intended
}
case 'l' :
case 'L' :
{
if('m' == aCurrChar || 'l' == aCurrChar)
{
bRelative = true;
}
if(bMoveTo)
{
// new polygon start, finish old one
if(aCurrPoly.count())
{
// add current polygon
if(bIsClosed)
{
closeWithGeometryChange(aCurrPoly);
}
o_rPolyPolygon.append(aCurrPoly);
// reset import values
bIsClosed = false;
aCurrPoly.clear();
}
}
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX += nLastX;
nY += nLastY;
}
// set last position
nLastX = nX;
nLastY = nY;
// add point
aCurrPoly.append(B2DPoint(nX, nY));
}
break;
}
case 'h' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'H' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY(nLastY);
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX += nLastX;
}
// set last position
nLastX = nX;
// add point
aCurrPoly.append(B2DPoint(nX, nY));
}
break;
}
case 'v' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'V' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX(nLastX), nY;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nY += nLastY;
}
// set last position
nLastY = nY;
// add point
aCurrPoly.append(B2DPoint(nX, nY));
}
break;
}
case 's' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'S' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
double nX2, nY2;
if(!lcl_importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX2 += nLastX;
nY2 += nLastY;
nX += nLastX;
nY += nLastY;
}
// ensure existance of start point
if(!aCurrPoly.count())
{
aCurrPoly.append(B2DPoint(nLastX, nLastY));
}
// get first control point. It's the reflection of the PrevControlPoint
// of the last point. If not existent, use current point (see SVG)
B2DPoint aPrevControl(B2DPoint(nLastX, nLastY));
const sal_uInt32 nIndex(aCurrPoly.count() - 1);
if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
{
const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
// use mirrored previous control point
aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
}
// append curved edge
aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2, nY2), B2DPoint(nX, nY));
// set last position
nLastX = nX;
nLastY = nY;
}
break;
}
case 'c' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'C' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
double nX1, nY1;
double nX2, nY2;
if(!lcl_importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nX2, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY2, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX1 += nLastX;
nY1 += nLastY;
nX2 += nLastX;
nY2 += nLastY;
nX += nLastX;
nY += nLastY;
}
// ensure existance of start point
if(!aCurrPoly.count())
{
aCurrPoly.append(B2DPoint(nLastX, nLastY));
}
// append curved edge
aCurrPoly.appendBezierSegment(B2DPoint(nX1, nY1), B2DPoint(nX2, nY2), B2DPoint(nX, nY));
// set last position
nLastX = nX;
nLastY = nY;
}
break;
}
// #100617# quadratic beziers are imported as cubic ones
case 'q' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'Q' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
double nX1, nY1;
if(!lcl_importDoubleAndSpaces(nX1, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY1, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX1 += nLastX;
nY1 += nLastY;
nX += nLastX;
nY += nLastY;
}
// calculate the cubic bezier coefficients from the quadratic ones
const double nX1Prime((nX1 * 2.0 + nLastX) / 3.0);
const double nY1Prime((nY1 * 2.0 + nLastY) / 3.0);
const double nX2Prime((nX1 * 2.0 + nX) / 3.0);
const double nY2Prime((nY1 * 2.0 + nY) / 3.0);
// ensure existance of start point
if(!aCurrPoly.count())
{
aCurrPoly.append(B2DPoint(nLastX, nLastY));
}
// append curved edge
aCurrPoly.appendBezierSegment(B2DPoint(nX1Prime, nY1Prime), B2DPoint(nX2Prime, nY2Prime), B2DPoint(nX, nY));
// set last position
nLastX = nX;
nLastY = nY;
}
break;
}
// #100617# relative quadratic beziers are imported as cubic
case 't' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'T' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX += nLastX;
nY += nLastY;
}
// ensure existance of start point
if(!aCurrPoly.count())
{
aCurrPoly.append(B2DPoint(nLastX, nLastY));
}
// get first control point. It's the reflection of the PrevControlPoint
// of the last point. If not existent, use current point (see SVG)
B2DPoint aPrevControl(B2DPoint(nLastX, nLastY));
const sal_uInt32 nIndex(aCurrPoly.count() - 1);
const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(nIndex));
if(aCurrPoly.areControlPointsUsed() && aCurrPoly.isPrevControlPointUsed(nIndex))
{
const B2DPoint aPrevControlPoint(aCurrPoly.getPrevControlPoint(nIndex));
// use mirrored previous control point
aPrevControl.setX((2.0 * aPrevPoint.getX()) - aPrevControlPoint.getX());
aPrevControl.setY((2.0 * aPrevPoint.getY()) - aPrevControlPoint.getY());
}
if(!aPrevControl.equal(aPrevPoint))
{
// there is a prev control point, and we have the already mirrored one
// in aPrevControl. We also need the quadratic control point for this
// new quadratic segment to calculate the 2nd cubic control point
const B2DPoint aQuadControlPoint(
((3.0 * aPrevControl.getX()) - aPrevPoint.getX()) / 2.0,
((3.0 * aPrevControl.getY()) - aPrevPoint.getY()) / 2.0);
// calculate the cubic bezier coefficients from the quadratic ones.
const double nX2Prime((aQuadControlPoint.getX() * 2.0 + nX) / 3.0);
const double nY2Prime((aQuadControlPoint.getY() * 2.0 + nY) / 3.0);
// append curved edge, use mirrored cubic control point directly
aCurrPoly.appendBezierSegment(aPrevControl, B2DPoint(nX2Prime, nY2Prime), B2DPoint(nX, nY));
}
else
{
// when no previous control, SVG says to use current point -> straight line.
// Just add end point
aCurrPoly.append(B2DPoint(nX, nY));
}
// set last position
nLastX = nX;
nLastY = nY;
}
break;
}
case 'a' :
{
bRelative = true;
// FALLTHROUGH intended
}
case 'A' :
{
nPos++;
lcl_skipSpaces(nPos, rSvgDStatement, nLen);
while(nPos < nLen && lcl_isOnNumberChar(rSvgDStatement, nPos))
{
double nX, nY;
double fRX, fRY, fPhi;
sal_Int32 bLargeArcFlag, bSweepFlag;
if(!lcl_importDoubleAndSpaces(fRX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(fRY, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(fPhi, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importFlagAndSpaces(bLargeArcFlag, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importFlagAndSpaces(bSweepFlag, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgDStatement, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgDStatement, nLen)) return false;
if(bRelative)
{
nX += nLastX;
nY += nLastY;
}
const B2DPoint aPrevPoint(aCurrPoly.getB2DPoint(aCurrPoly.count() - 1));
if( nX == nLastX && nY == nLastY )
continue; // start==end -> skip according to SVG spec
if( fRX == 0.0 || fRY == 0.0 )
{
// straight line segment according to SVG spec
aCurrPoly.append(B2DPoint(nX, nY));
}
else
{
// normalize according to SVG spec
fRX=fabs(fRX); fRY=fabs(fRY);
// from the SVG spec, appendix F.6.4
// |x1'| |cos phi sin phi| |(x1 - x2)/2|
// |y1'| = |-sin phi cos phi| |(y1 - y2)/2|
const B2DPoint p1(nLastX, nLastY);
const B2DPoint p2(nX, nY);
B2DHomMatrix aTransform(basegfx::tools::createRotateB2DHomMatrix(-fPhi*M_PI/180));
const B2DPoint p1_prime( aTransform * B2DPoint(((p1-p2)/2.0)) );
// ______________________________________ rx y1'
// |cx'| + / rx^2 ry^2 - rx^2 y1'^2 - ry^2 x1^2 ry
// |cy'| =-/ rx^2y1'^2 + ry^2 x1'^2 - ry x1'
// rx
// chose + if f_A != f_S
// chose - if f_A = f_S
B2DPoint aCenter_prime;
const double fRadicant(
(fRX*fRX*fRY*fRY - fRX*fRX*p1_prime.getY()*p1_prime.getY() - fRY*fRY*p1_prime.getX()*p1_prime.getX())/
(fRX*fRX*p1_prime.getY()*p1_prime.getY() + fRY*fRY*p1_prime.getX()*p1_prime.getX()));
if( fRadicant < 0.0 )
{
// no solution - according to SVG
// spec, scale up ellipse
// uniformly such that it passes
// through end points (denominator
// of radicant solved for fRY,
// with s=fRX/fRY)
const double fRatio(fRX/fRY);
const double fRadicant2(
p1_prime.getY()*p1_prime.getY() +
p1_prime.getX()*p1_prime.getX()/(fRatio*fRatio));
if( fRadicant2 < 0.0 )
{
// only trivial solution, one
// of the axes 0 -> straight
// line segment according to
// SVG spec
aCurrPoly.append(B2DPoint(nX, nY));
continue;
}
fRY=sqrt(fRadicant2);
fRX=fRatio*fRY;
// keep center_prime forced to (0,0)
}
else
{
const double fFactor(
(bLargeArcFlag==bSweepFlag ? -1.0 : 1.0) *
sqrt(fRadicant));
// actually calculate center_prime
aCenter_prime = B2DPoint(
fFactor*fRX*p1_prime.getY()/fRY,
-fFactor*fRY*p1_prime.getX()/fRX);
}
// + u - v
// angle(u,v) = arccos( ------------ ) (take the sign of (ux vy - uy vx))
// - ||u|| ||v||
// 1 | (x1' - cx')/rx |
// theta1 = angle(( ), | | )
// 0 | (y1' - cy')/ry |
const B2DPoint aRadii(fRX,fRY);
double fTheta1(
B2DVector(1.0,0.0).angle(
(p1_prime-aCenter_prime)/aRadii));
// |1| | (-x1' - cx')/rx |
// theta2 = angle( | | , | | )
// |0| | (-y1' - cy')/ry |
double fTheta2(
B2DVector(1.0,0.0).angle(
(-p1_prime-aCenter_prime)/aRadii));
// map both angles to [0,2pi)
fTheta1 = fmod(2*M_PI+fTheta1,2*M_PI);
fTheta2 = fmod(2*M_PI+fTheta2,2*M_PI);
// make sure the large arc is taken
// (since
// createPolygonFromEllipseSegment()
// normalizes to e.g. cw arc)
if( !bSweepFlag )
std::swap(fTheta1,fTheta2);
// finally, create bezier polygon from this
B2DPolygon aSegment(
tools::createPolygonFromUnitEllipseSegment(
fTheta1, fTheta2 ));
// transform ellipse by rotation & move to final center
aTransform = basegfx::tools::createScaleB2DHomMatrix(fRX, fRY);
aTransform.translate(aCenter_prime.getX(),
aCenter_prime.getY());
aTransform.rotate(fPhi*M_PI/180);
const B2DPoint aOffset((p1+p2)/2.0);
aTransform.translate(aOffset.getX(),
aOffset.getY());
aSegment.transform(aTransform);
// createPolygonFromEllipseSegment()
// always creates arcs that are
// positively oriented - flip polygon
// if we swapped angles above
if( !bSweepFlag )
aSegment.flip();
aCurrPoly.append(aSegment);
}
// set last position
nLastX = nX;
nLastY = nY;
}
break;
}
default:
{
OSL_FAIL("importFromSvgD(): skipping tags in svg:d element (unknown)!");
OSL_TRACE("importFromSvgD(): skipping tags in svg:d element (unknown: \"%c\")!", aCurrChar);
++nPos;
break;
}
}
}
if(aCurrPoly.count())
{
// end-process last poly
if(bIsClosed)
{
closeWithGeometryChange(aCurrPoly);
}
o_rPolyPolygon.append(aCurrPoly);
}
return true;
}
bool importFromSvgPoints( B2DPolygon& o_rPoly,
const ::rtl::OUString& rSvgPointsAttribute )
{
o_rPoly.clear();
const sal_Int32 nLen(rSvgPointsAttribute.getLength());
sal_Int32 nPos(0);
double nX, nY;
// skip initial whitespace
lcl_skipSpaces(nPos, rSvgPointsAttribute, nLen);
while(nPos < nLen)
{
if(!lcl_importDoubleAndSpaces(nX, nPos, rSvgPointsAttribute, nLen)) return false;
if(!lcl_importDoubleAndSpaces(nY, nPos, rSvgPointsAttribute, nLen)) return false;
// add point
o_rPoly.append(B2DPoint(nX, nY));
// skip to next number, or finish
lcl_skipSpaces(nPos, rSvgPointsAttribute, nLen);
}
return true;
}
::rtl::OUString exportToSvgD(
const B2DPolyPolygon& rPolyPolygon,
bool bUseRelativeCoordinates,
bool bDetectQuadraticBeziers)
{
const sal_uInt32 nCount(rPolyPolygon.count());
::rtl::OUStringBuffer aResult;
B2DPoint aCurrentSVGPosition(0.0, 0.0); // SVG assumes (0,0) as the initial current point
for(sal_uInt32 i(0); i < nCount; i++)
{
const B2DPolygon aPolygon(rPolyPolygon.getB2DPolygon(i));
const sal_uInt32 nPointCount(aPolygon.count());
if(nPointCount)
{
const bool bPolyUsesControlPoints(aPolygon.areControlPointsUsed());
const sal_uInt32 nEdgeCount(aPolygon.isClosed() ? nPointCount : nPointCount - 1);
sal_Unicode aLastSVGCommand(' '); // last SVG command char
B2DPoint aLeft, aRight; // for quadratic bezier test
// handle polygon start point
B2DPoint aEdgeStart(aPolygon.getB2DPoint(0));
aResult.append(lcl_getCommand('M', 'm', bUseRelativeCoordinates));
lcl_putNumberCharWithSpace(aResult, aEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aLastSVGCommand = lcl_getCommand('L', 'l', bUseRelativeCoordinates);
aCurrentSVGPosition = aEdgeStart;
for(sal_uInt32 nIndex(0); nIndex < nEdgeCount; nIndex++)
{
// prepare access to next point
const sal_uInt32 nNextIndex((nIndex + 1) % nPointCount);
const B2DPoint aEdgeEnd(aPolygon.getB2DPoint(nNextIndex));
// handle edge from (aEdgeStart, aEdgeEnd) using indices (nIndex, nNextIndex)
const bool bEdgeIsBezier(bPolyUsesControlPoints
&& (aPolygon.isNextControlPointUsed(nIndex) || aPolygon.isPrevControlPointUsed(nNextIndex)));
if(bEdgeIsBezier)
{
// handle bezier edge
const B2DPoint aControlEdgeStart(aPolygon.getNextControlPoint(nIndex));
const B2DPoint aControlEdgeEnd(aPolygon.getPrevControlPoint(nNextIndex));
bool bIsQuadraticBezier(false);
// check continuity at current edge's start point. For SVG, do NOT use an
// existing continuity since no 'S' or 's' statement should be written. At
// import, that 'previous' control vector is not available. SVG documentation
// says for interpretation:
//
// "(If there is no previous command or if the previous command was
// not an C, c, S or s, assume the first control point is coincident
// with the current point.)"
//
// That's what is done from our import, so avoid exporting it as first statement
// is necessary.
const bool bSymmetricAtEdgeStart(
0 != nIndex
&& CONTINUITY_C2 == aPolygon.getContinuityInPoint(nIndex));
if(bDetectQuadraticBeziers)
{
// check for quadratic beziers - that's
// the case if both control points are in
// the same place when they are prolonged
// to the common quadratic control point
//
// Left: P = (3P1 - P0) / 2
// Right: P = (3P2 - P3) / 2
aLeft = B2DPoint((3.0 * aControlEdgeStart - aEdgeStart) / 2.0);
aRight= B2DPoint((3.0 * aControlEdgeEnd - aEdgeEnd) / 2.0);
bIsQuadraticBezier = aLeft.equal(aRight);
}
if(bIsQuadraticBezier)
{
// approximately equal, export as quadratic bezier
if(bSymmetricAtEdgeStart)
{
const sal_Unicode aCommand(lcl_getCommand('T', 't', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aLastSVGCommand = aCommand;
aCurrentSVGPosition = aEdgeEnd;
}
else
{
const sal_Unicode aCommand(lcl_getCommand('Q', 'q', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aLeft.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aLeft.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aLastSVGCommand = aCommand;
aCurrentSVGPosition = aEdgeEnd;
}
}
else
{
// export as cubic bezier
if(bSymmetricAtEdgeStart)
{
const sal_Unicode aCommand(lcl_getCommand('S', 's', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aLastSVGCommand = aCommand;
aCurrentSVGPosition = aEdgeEnd;
}
else
{
const sal_Unicode aCommand(lcl_getCommand('C', 'c', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aControlEdgeStart.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aControlEdgeStart.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aControlEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aControlEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aLastSVGCommand = aCommand;
aCurrentSVGPosition = aEdgeEnd;
}
}
}
else
{
// straight edge
if(0 == nNextIndex)
{
// it's a closed polygon's last edge and it's not a bezier edge, so there is
// no need to write it
}
else
{
const bool bXEqual(aEdgeStart.getX() == aEdgeEnd.getX());
const bool bYEqual(aEdgeStart.getY() == aEdgeEnd.getY());
if(bXEqual && bYEqual)
{
// point is a double point; do not export at all
}
else if(bXEqual)
{
// export as vertical line
const sal_Unicode aCommand(lcl_getCommand('V', 'v', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aCurrentSVGPosition = aEdgeEnd;
}
else if(bYEqual)
{
// export as horizontal line
const sal_Unicode aCommand(lcl_getCommand('H', 'h', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
aCurrentSVGPosition = aEdgeEnd;
}
else
{
// export as line
const sal_Unicode aCommand(lcl_getCommand('L', 'l', bUseRelativeCoordinates));
if(aLastSVGCommand != aCommand)
{
aResult.append(aCommand);
aLastSVGCommand = aCommand;
}
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getX(), aCurrentSVGPosition.getX(), bUseRelativeCoordinates);
lcl_putNumberCharWithSpace(aResult, aEdgeEnd.getY(), aCurrentSVGPosition.getY(), bUseRelativeCoordinates);
aCurrentSVGPosition = aEdgeEnd;
}
}
}
// prepare edge start for next loop step
aEdgeStart = aEdgeEnd;
}
// close path if closed poly (Z and z are equivalent here, but looks nicer when case is matched)
if(aPolygon.isClosed())
{
aResult.append(lcl_getCommand('Z', 'z', bUseRelativeCoordinates));
// return to first point
aCurrentSVGPosition = aPolygon.getB2DPoint(0);
}
}
}
return aResult.makeStringAndClear();
}
}
}
// eof
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|