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
|
//
// System.Drawing.Drawing2D.ExtendedGeneralPath.cs
//
// Author:
// Bors Kirzner <boris@mainsoft.com>
//
// Copyright (C) 2005 Mainsoft Corporation, (http://www.mainsoft.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using java.awt;
using java.awt.geom;
using java.lang;
namespace System.Drawing.Drawing2D
{
internal class ExtendedGeneralPath : Shape, ICloneable
{
#region Fields
public const int WIND_EVEN_ODD = 0; //PathIterator__Finals.WIND_EVEN_ODD;
public const int WIND_NON_ZERO = 1; //PathIterator__Finals.WIND_NON_ZERO;
public const sbyte SEG_MOVETO = 0; //(byte) PathIterator__Finals.SEG_MOVETO;
public const sbyte SEG_LINETO = 1; //(byte) PathIterator__Finals.SEG_LINETO;
public const sbyte SEG_QUADTO = 2; //(byte) PathIterator__Finals.SEG_QUADTO;
public const sbyte SEG_CUBICTO = 3; //(byte) PathIterator__Finals.SEG_CUBICTO;
public const sbyte SEG_CLOSE = 4; //(byte) PathIterator__Finals.SEG_CLOSE;
public const sbyte SEG_START = 16; // segment start
public const sbyte SEG_MASK = SEG_MOVETO | SEG_LINETO | SEG_QUADTO | SEG_CUBICTO | SEG_CLOSE; // mask to eliminate SEG_CLOSE and SEG_MARKER
private const sbyte SEG_MARKER = 32; // path marker
private sbyte [] _types;
private float [] _coords;
private int _typesCount;
private int _coordsCount;
private int _windingRule;
private PathData _pathData;
private GeneralPath _generalPath;
const int INIT_SIZE = 20;
const int EXPAND_MAX = 500;
#endregion // Fileds
#region Constructors
public ExtendedGeneralPath() : this (WIND_NON_ZERO, INIT_SIZE, INIT_SIZE)
{
}
public ExtendedGeneralPath(int rule) : this (rule, INIT_SIZE, INIT_SIZE)
{
}
public ExtendedGeneralPath(int rule, int initialCapacity) : this (rule, initialCapacity, initialCapacity)
{
}
public ExtendedGeneralPath(Shape s) : this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE)
{
PathIterator pi = s.getPathIterator (null);
setWindingRule (pi.getWindingRule ());
append (pi, false);
}
private ExtendedGeneralPath(int rule, int initialTypes, int initialCoords)
{
setWindingRule(rule);
Reset (initialTypes, initialCoords);
}
#endregion // Constructors
#region Properties
private GeneralPath GeneralPath
{
get {
if (_generalPath == null) {
_generalPath = GetGeneralPath ();
}
return _generalPath;
}
}
public sbyte [] Types
{
get { return _types; }
}
public float [] Coords
{
get { return _coords; }
}
public int TypesCount
{
get { return _typesCount; }
}
public int CoordsCount
{
get { return _coordsCount; }
}
public bool LastFigureClosed
{
get {
return ((TypesCount == 0) ||
((Types [TypesCount - 1] & ExtendedGeneralPath.SEG_CLOSE) != 0) ||
((Types [TypesCount - 1] & ExtendedGeneralPath.SEG_START) != 0));
}
}
public int PointCount
{
get {
return CoordsCount / 2;
}
}
public PathData PathData
{
get
{
if (_pathData == null)
_pathData = GetPathData ();
return _pathData;
}
}
#endregion // Properties
#region Methods
#region CachedData
private void ClearCache ()
{
_pathData = null;
_generalPath = null;
}
private GeneralPath GetGeneralPath ()
{
PathIterator iter = getPathIterator (null);
GeneralPath path = new GeneralPath ();
path.append (iter, false);
return path;
}
private PathData GetPathData ()
{
PathData pathData = new PathData();
int nPts = PointCount;
for (int i = 0; i < TypesCount; i++)
if ((Types [i] & SEG_MASK) == SEG_QUADTO)
nPts++;
pathData.Types = new byte [nPts];
pathData.Points = new PointF [nPts];
int tpos = 0;
int ppos = 0;
int cpos = 0;
byte marker;
bool start;
for (int i = 0; i < TypesCount; i++) {
sbyte segmentType = (sbyte)(Types [i] & SEG_MASK);
// set the masks and the markers
marker = ((Types [i] & SEG_MARKER) != 0) ? (byte)PathPointType.PathMarker : (byte)0;
start = ((Types [i] & SEG_START) != 0);
switch (segmentType) {
case SEG_CLOSE:
pathData.Types [tpos - 1] = (byte) (pathData.Types [tpos - 1] | (byte) PathPointType.CloseSubpath | marker);
break;
case SEG_MOVETO:
pathData.Types [tpos++] = (byte)((byte) PathPointType.Start | marker);
pathData.Points [ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
break;
case SEG_LINETO:
pathData.Types [tpos++] = (byte) ((byte) PathPointType.Line | marker);
pathData.Points [ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
break;
case SEG_QUADTO:
/*
.net does not support Quadratic curves, so convert to Cubic according to http://pfaedit.sourceforge.net/bezier.html
The end points of the cubic will be the same as the quadratic's.
CP0 = QP0
CP3 = QP2
The two control points for the cubic are:
CP1 = QP0 + 2/3 *(QP1-QP0)
CP2 = CP1 + 1/3 *(QP2-QP0)
*/
float x0 = Coords[cpos-2]; //QP0
float y0 = Coords[cpos-1]; //QP0
float x1 = x0 + (2/3 * (Coords [cpos++]-x0));
float y1 = y0 + (2/3 * (Coords [cpos++]-y0));
float x3 = Coords [cpos++]; //QP2
float y3 = Coords [cpos++]; //QP2
float x2 = x1 + (1/3 * (x3-x0));
float y2 = y1 + (1/3 * (y3-y0));
pathData.Types [tpos++] = (byte)(byte) PathPointType.Bezier;
pathData.Points [ppos++] = new PointF (x1, y1);
pathData.Types [tpos++] = (byte)(byte) PathPointType.Bezier;
pathData.Points [ppos++] = new PointF (x2, y2);
pathData.Types [tpos++] = (byte) ((byte)PathPointType.Bezier | marker);
pathData.Points [ppos++] = new PointF (x3, y3);
break;
case SEG_CUBICTO:
pathData.Types [tpos++] = (byte)(byte) PathPointType.Bezier3;
pathData.Points [ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
pathData.Types [tpos++] = (byte) PathPointType.Bezier3;
pathData.Points [ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
pathData.Types [tpos++] = (byte) ((byte)PathPointType.Bezier3 | marker);
pathData.Points [ppos++] = new PointF (Coords [cpos++], Coords [cpos++]);
break;
}
}
return pathData;
}
#endregion // CachedData
public void append(Shape s)
{
append (s, !LastFigureClosed);
}
#region GeneralPath
public void append(PathIterator pi, bool connect)
{
ClearCache ();
float [] coords = new float [6];
while (!pi.isDone ()) {
switch (pi.currentSegment (coords)) {
case SEG_MOVETO:
if (!connect || _typesCount < 1 || _coordsCount < 2) {
moveTo (coords [0], coords [1]);
break;
}
if (_types [_typesCount - 1] != SEG_CLOSE &&
_coords [_coordsCount - 2] == coords [0] &&
_coords [_coordsCount - 1] == coords [1])
break;
goto case SEG_LINETO;
case SEG_LINETO:
lineTo (coords [0], coords [1]);
break;
case SEG_QUADTO:
quadTo (coords [0], coords [1], coords [2], coords [3]);
break;
case SEG_CUBICTO:
curveTo (coords [0], coords [1], coords [2], coords [3], coords [4], coords [5]);
break;
case SEG_CLOSE:
closePath ();
break;
}
pi.next ();
connect = false;
}
}
public void append(Shape s, bool connect)
{
PathIterator pi = s.getPathIterator (null);
append (pi,connect);
}
public object Clone()
{
ExtendedGeneralPath copy = (ExtendedGeneralPath)MemberwiseClone ();
copy._types = (sbyte []) _types.Clone ();
copy._coords = (float []) _coords.Clone ();
return copy;
}
public void closePath()
{
ClearCache ();
if (_typesCount == 0 || _types[_typesCount - 1] != SEG_CLOSE) {
needRoom (1, 0, true);
_types [_typesCount++] = SEG_CLOSE;
}
}
public bool contains(double x, double y)
{
return GeneralPath.contains (x, y);
}
public bool contains(double x, double y, double w, double h)
{
return GeneralPath.contains (x, y, w, h);
}
public bool contains(Point2D p)
{
return contains (p.getX (), p.getY ());
}
public bool contains(Rectangle2D r)
{
return contains (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
}
public Shape createTransformedShape(AffineTransform at)
{
ExtendedGeneralPath gp = (ExtendedGeneralPath) Clone ();
if (at != null) {
gp.transform (at);
}
return gp;
}
public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
{
ClearCache ();
needRoom (1, 6, true);
_types [_typesCount++] = SEG_CUBICTO;
_coords [_coordsCount++] = x1;
_coords [_coordsCount++] = y1;
_coords [_coordsCount++] = x2;
_coords [_coordsCount++] = y2;
_coords [_coordsCount++] = x3;
_coords [_coordsCount++] = y3;
}
public java.awt.Rectangle getBounds()
{
return getBounds2D ().getBounds ();
}
public Rectangle2D getBounds2D()
{
float x1, y1, x2, y2;
int i = _coordsCount;
if (i > 0) {
y1 = y2 = _coords [--i];
x1 = x2 = _coords [--i];
while (i > 0) {
float y = _coords [--i];
float x = _coords [--i];
if (x < x1) x1 = x;
if (y < y1) y1 = y;
if (x > x2) x2 = x;
if (y > y2) y2 = y;
}
}
else {
x1 = y1 = x2 = y2 = 0f;
}
return new Rectangle2D.Float (x1, y1, x2 - x1, y2 - y1);
}
public Point2D getCurrentPoint()
{
if (_typesCount < 1 || _coordsCount < 2)
return null;
int index = _coordsCount;
if (_types [_typesCount - 1] == SEG_CLOSE)
for (int i = _typesCount - 2; i > 0; i--) {
switch (_types [i]) {
case SEG_MOVETO:
//break loop;
goto loopend;
case SEG_LINETO:
index -= 2;
break;
case SEG_QUADTO:
index -= 4;
break;
case SEG_CUBICTO:
index -= 6;
break;
case SEG_CLOSE:
break;
}
}
loopend:
return new Point2D.Float (_coords [index - 2], _coords [index - 1]);
}
public PathIterator getPathIterator(AffineTransform at) {
return new GeneralPathIterator (this, at);
}
public PathIterator getPathIterator(AffineTransform at, double flatness) {
return new FlatteningPathIterator (getPathIterator (at), flatness);
}
public int getWindingRule()
{
return _windingRule;
}
public bool intersects(double x, double y, double w, double h)
{
return GeneralPath.intersects (x, y, w, h);
}
public bool intersects(Rectangle2D r)
{
return intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
}
public void lineTo(float x, float y)
{
ClearCache ();
needRoom (1, 2, true);
_types [_typesCount++] = SEG_LINETO;
_coords [_coordsCount++] = x;
_coords [_coordsCount++] = y;
}
public void moveTo(float x, float y)
{
ClearCache ();
if (_typesCount > 0 && _types [_typesCount - 1] == SEG_MOVETO) {
_coords [_coordsCount - 2] = x;
_coords [_coordsCount - 1] = y;
}
else {
needRoom (1, 2, false);
_types [_typesCount++] = SEG_MOVETO;
_coords [_coordsCount++] = x;
_coords [_coordsCount++] = y;
}
}
public void quadTo(float x1, float y1, float x2, float y2)
{
// restore quadTo as cubic affects quality
ClearCache ();
needRoom (1, 4, true);
_types [_typesCount++] = SEG_QUADTO;
_coords [_coordsCount++] = x1;
_coords [_coordsCount++] = y1;
_coords [_coordsCount++] = x2;
_coords [_coordsCount++] = y2;
}
public void reset()
{
ClearCache ();
_typesCount = 0;
_coordsCount = 0;
}
public void setWindingRule(int rule)
{
if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO) {
throw new IllegalArgumentException ("winding rule must be WIND_EVEN_ODD or WIND_NON_ZERO");
}
_windingRule = rule;
}
public void transform(AffineTransform at)
{
transform(at, 0, CoordsCount);
}
public void transform(AffineTransform at, int startCoord, int numCoords) {
ClearCache ();
at.transform (_coords, startCoord, _coords, startCoord, numCoords/2);
}
private void needRoom(int newTypes, int newCoords, bool needMove)
{
if (needMove && _typesCount == 0)
throw new IllegalPathStateException ("missing initial moveto in path definition");
int size = _coords.Length;
if (_coordsCount + newCoords > size) {
int grow = size;
if (grow > EXPAND_MAX * 2)
grow = EXPAND_MAX * 2;
if (grow < newCoords)
grow = newCoords;
float [] arr = new float [size + grow];
Array.Copy (_coords, 0, arr, 0, _coordsCount);
_coords = arr;
}
size = _types.Length;
if (_typesCount + newTypes > size) {
int grow = size;
if (grow > EXPAND_MAX)
grow = EXPAND_MAX;
if (grow < newTypes)
grow = newTypes;
sbyte [] arr = new sbyte [size + grow];
Array.Copy (_types, 0, arr, 0, _typesCount);
_types = arr;
}
}
#endregion // GeneralPath
public void SetMarkers()
{
ClearCache ();
if (TypesCount > 0)
Types [ TypesCount - 1] |= SEG_MARKER;
}
public void ClearMarkers()
{
ClearCache ();
for (int i = 0; i < TypesCount; i++)
Types [i] &= ~SEG_MARKER;
}
public void StartFigure ()
{
ClearCache ();
if (TypesCount > 0)
Types [TypesCount - 1] |= ExtendedGeneralPath.SEG_START;
}
private void Reset (int initialTypes, int initialCoords)
{
ClearCache ();
_types = new sbyte [initialTypes];
_coords = new float [initialCoords * 2];
_typesCount = 0;
_coordsCount = 0;
}
internal void Clear ()
{
Reset (INIT_SIZE, INIT_SIZE);
}
internal void Reverse ()
{
ClearCache ();
// revert coordinates
for (int i=0, max = CoordsCount / 2; i < max;) {
int ix = i++;
int iy = i++;
int rix = CoordsCount - i;
int riy = rix + 1;
float tmpx = Coords [ix];
float tmpy = Coords [iy];
Coords [ix] = Coords [rix];
Coords [iy] = Coords [riy];
Coords [rix] = tmpx;
Coords [riy] = tmpy;
}
// revert types
sbyte [] newTypes = new sbyte [TypesCount];
int oldIdx = 0;
int newIdx = TypesCount - 1;
int copyStart;
int copyEnd;
sbyte mask1 = 0;
sbyte mask2 = 0;
sbyte closeMask = 0;
bool closedFigure = false;
while (oldIdx < TypesCount) {
// start copying after moveto
copyStart = ++oldIdx;
// continue to the next figure start
while ((Types [oldIdx] != SEG_MOVETO) && (oldIdx < TypesCount))
oldIdx++;
copyEnd = oldIdx - 1;
// check whenever current figure is closed
if ((Types [oldIdx - 1] & SEG_CLOSE) != 0) {
closedFigure = true;
// close figure
newTypes [newIdx--] = (sbyte)(SEG_CLOSE | mask1);
mask1 = 0;
mask2 = 0;
// end copy one cell earlier
copyEnd--;
closeMask = (sbyte)(Types [oldIdx - 1] & (sbyte)SEG_MARKER);
}
else {
mask2 = mask1;
mask1 = 0;
}
// copy reverted "inner" types
for(int i = copyStart; i <= copyEnd; i++) {
newTypes [newIdx--] = (sbyte)((Types [i] & SEG_MASK) | mask2);
mask2 = mask1;
mask1 = (sbyte)(Types [i] & (sbyte)SEG_MARKER);
}
// copy moveto
newTypes [newIdx--] = SEG_MOVETO;
// pass close mask to the nex figure
if (closedFigure) {
mask1 = closeMask;
closedFigure = false;
}
}
_types = newTypes;
}
public PointF GetLastPoint ()
{
if (CoordsCount == 0)
throw new System.ArgumentException ("Invalid parameter used.");
return new PointF (Coords [CoordsCount - 2], Coords [CoordsCount - 1]);
}
#endregion //Methods
#region Private helpers
#if DEBUG
private void Print()
{
Console.WriteLine ("\n\n");
float [] fpoints = _coords;
int cpos = 0;
for (int i=0; i < _typesCount; i++) {
sbyte type = _types [i];
string marker = String.Empty;
if ((type & SEG_MARKER) != 0)
marker = " | MARKER";
switch (type & SEG_MASK) {
case SEG_CLOSE:
Console.WriteLine ("CLOSE {0}",marker);
break;
case SEG_MOVETO:
Console.WriteLine("{0}{3} ({1},{2})","MOVETO", fpoints[cpos++], fpoints[cpos++], marker);
break;
case SEG_LINETO:
Console.WriteLine("{0}{3} ({1},{2})","LINETO", fpoints[cpos++], fpoints[cpos++], marker);
break;
case SEG_QUADTO:
Console.WriteLine("{0}{3} ({1},{2})","QUADTO", fpoints[cpos++], fpoints[cpos++], marker);
Console.WriteLine(" ({1},{2})","QUADTO", fpoints[cpos++], fpoints[cpos++]);
break;
case SEG_CUBICTO:
Console.WriteLine("{0}{3} ({1},{2})","CUBICTO", fpoints[cpos++], fpoints[cpos++], marker);
Console.WriteLine(" ({1},{2})","CUBICTO", fpoints[cpos++], fpoints[cpos++]);
Console.WriteLine(" ({1},{2})","CUBICTO", fpoints[cpos++], fpoints[cpos++]);
break;
}
}
}
#endif
#endregion // Private helpers
}
}
|