~ubuntu-branches/ubuntu/natty/pdfmod/natty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Drawing/XRect.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-09-29 17:34:49 UTC
  • mfrom: (2.1.1 experimental)
  • Revision ID: james.westby@ubuntu.com-20100929173449-4ezagrzettatjk36
Tags: 0.9.0-1
* New upstream release
* debian/copyright: Document PdfSharp.SharpZipLib/*
* Drop all patches: committed upstream
* No change bump of Standards-Version from 3.8.4 to 3.9.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
// Authors:
4
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
5
5
//
6
 
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
6
// Copyright (c) 2005-2009 empira Software GmbH, Cologne (Germany)
7
7
//
8
8
// http://www.pdfsharp.com
9
9
// http://sourceforge.net/projects/pdfsharp
43
43
 
44
44
namespace PdfSharp.Drawing
45
45
{
46
 
#if true
47
46
  /// <summary>
48
47
  /// Stores a set of four floating-point numbers that represent the location and size of a rectangle.
49
48
  /// </summary>
71
70
    {
72
71
      this.x = Math.Min(point1.x, point2.x);
73
72
      this.y = Math.Min(point1.y, point2.y);
74
 
      this.width = Math.Max((double)(Math.Max(point1.x, point2.x) - this.x), 0);
75
 
      this.height = Math.Max((double)(Math.Max(point1.y, point2.y) - this.y), 0);
 
73
      this.width = Math.Max(Math.Max(point1.x, point2.x) - this.x, 0);
 
74
      this.height = Math.Max(Math.Max(point1.y, point2.y) - this.y, 0);
76
75
    }
77
76
 
78
77
    /// <summary>
212
211
    {
213
212
      if (IsEmpty)
214
213
        return 0;
215
 
      return this.X.GetHashCode() ^ this.Y.GetHashCode() ^ this.Width.GetHashCode() ^ this.Height.GetHashCode();
 
214
      return X.GetHashCode() ^ Y.GetHashCode() ^ Width.GetHashCode() ^ Height.GetHashCode();
216
215
    }
217
216
 
218
217
    /// <summary>
237
236
    /// </summary>
238
237
    public override string ToString()
239
238
    {
240
 
      return this.ConvertToString(null, null);
 
239
      return ConvertToString(null, null);
241
240
    }
242
241
 
243
242
    /// <summary>
245
244
    /// </summary>
246
245
    public string ToString(IFormatProvider provider)
247
246
    {
248
 
      return this.ConvertToString(null, provider);
 
247
      return ConvertToString(null, provider);
249
248
    }
250
249
 
251
250
    /// <summary>
253
252
    /// </summary>
254
253
    string IFormattable.ToString(string format, IFormatProvider provider)
255
254
    {
256
 
      return this.ConvertToString(format, provider);
 
255
      return ConvertToString(format, provider);
257
256
    }
258
257
 
259
258
    internal string ConvertToString(string format, IFormatProvider provider)
429
428
    /// </summary>
430
429
    public XPoint TopLeft
431
430
    {
432
 
      get { return new XPoint(this.Left, this.Top); }
 
431
      get { return new XPoint(Left, Top); }
433
432
    }
434
433
 
435
434
    /// <summary>
437
436
    /// </summary>
438
437
    public XPoint TopRight
439
438
    {
440
 
      get { return new XPoint(this.Right, this.Top); }
 
439
      get { return new XPoint(Right, Top); }
441
440
    }
442
441
 
443
442
    /// <summary>
445
444
    /// </summary>
446
445
    public XPoint BottomLeft
447
446
    {
448
 
      get { return new XPoint(this.Left, this.Bottom); }
 
447
      get { return new XPoint(Left, Bottom); }
449
448
    }
450
449
 
451
450
    /// <summary>
453
452
    /// </summary>
454
453
    public XPoint BottomRight
455
454
    {
456
 
      get { return new XPoint(this.Right, this.Bottom); }
 
455
      get { return new XPoint(Right, Bottom); }
457
456
    }
458
457
 
459
458
    /// <summary>
480
479
    {
481
480
      if (IsEmpty)
482
481
        return false;
483
 
      return this.ContainsInternal(x, y);
 
482
      return ContainsInternal(x, y);
484
483
    }
485
484
 
486
485
    /// <summary>
499
498
    public bool IntersectsWith(XRect rect)
500
499
    {
501
500
      return !IsEmpty && !rect.IsEmpty &&
502
 
        rect.Left <= this.Right && rect.Right >= this.Left &&
503
 
        rect.Top <= this.Bottom && rect.Bottom >= this.Top;
 
501
          rect.Left <= Right && rect.Right >= Left &&
 
502
          rect.Top <= Bottom && rect.Bottom >= Top;
504
503
    }
505
504
 
506
505
    /// <summary>
508
507
    /// </summary>
509
508
    public void Intersect(XRect rect)
510
509
    {
511
 
      if (!this.IntersectsWith(rect))
 
510
      if (!IntersectsWith(rect))
512
511
        this = Empty;
513
512
      else
514
513
      {
515
 
        double left = Math.Max(this.Left, rect.Left);
516
 
        double top = Math.Max(this.Top, rect.Top);
517
 
        this.width = Math.Max((double)(Math.Min(this.Right, rect.Right) - left), 0.0);
518
 
        this.height = Math.Max((double)(Math.Min(this.Bottom, rect.Bottom) - top), 0.0);
 
514
        double left = Math.Max(Left, rect.Left);
 
515
        double top = Math.Max(Top, rect.Top);
 
516
        this.width = Math.Max(Math.Min(Right, rect.Right) - left, 0.0);
 
517
        this.height = Math.Max(Math.Min(Bottom, rect.Bottom) - top, 0.0);
519
518
        this.x = left;
520
519
        this.y = top;
521
520
      }
539
538
        this = rect;
540
539
      else if (!rect.IsEmpty)
541
540
      {
542
 
        double left = Math.Min(this.Left, rect.Left);
543
 
        double top = Math.Min(this.Top, rect.Top);
544
 
        if (rect.Width == Double.PositiveInfinity || this.Width == Double.PositiveInfinity)
 
541
        double left = Math.Min(Left, rect.Left);
 
542
        double top = Math.Min(Top, rect.Top);
 
543
        if (rect.Width == Double.PositiveInfinity || Width == Double.PositiveInfinity)
545
544
          this.width = Double.PositiveInfinity;
546
545
        else
547
546
        {
548
 
          double right = Math.Max(this.Right, rect.Right);
549
 
          this.width = Math.Max((double)(right - left), 0.0);
 
547
          double right = Math.Max(Right, rect.Right);
 
548
          this.width = Math.Max(right - left, 0.0);
550
549
        }
551
550
 
552
551
        if (rect.Height == Double.PositiveInfinity || this.height == Double.PositiveInfinity)
553
552
          this.height = Double.PositiveInfinity;
554
553
        else
555
554
        {
556
 
          double bottom = Math.Max(this.Bottom, rect.Bottom);
557
 
          this.height = Math.Max((double)(bottom - top), 0.0);
 
555
          double bottom = Math.Max(Bottom, rect.Bottom);
 
556
          this.height = Math.Max(bottom - top, 0.0);
558
557
        }
559
558
        this.x = left;
560
559
        this.y = top;
628
627
    }
629
628
 
630
629
    /// <summary>
631
 
    /// Translates the rectangle by adding the specifed point.
 
630
    /// Translates the rectangle by adding the specified point.
632
631
    /// </summary>
633
632
    //[Obsolete("Use Offset.")]
634
633
    public static XRect operator +(XRect rect, XPoint point)
637
636
    }
638
637
 
639
638
    /// <summary>
640
 
    /// Translates the rectangle by subtracting the specifed point.
 
639
    /// Translates the rectangle by subtracting the specified point.
641
640
    /// </summary>
642
641
    //[Obsolete("Use Offset.")]
643
642
    public static XRect operator -(XRect rect, XPoint point)
693
692
    /// </summary>
694
693
    public static XRect Transform(XRect rect, XMatrix matrix)
695
694
    {
696
 
      XMatrix.MatrixUtil.TransformRect(ref rect, ref matrix);
 
695
      XMatrix.MatrixHelper.TransformRect(ref rect, ref matrix);
697
696
      return rect;
698
697
    }
699
698
 
702
701
    /// </summary>
703
702
    public void Transform(XMatrix matrix)
704
703
    {
705
 
      XMatrix.MatrixUtil.TransformRect(ref this, ref matrix);
 
704
      XMatrix.MatrixHelper.TransformRect(ref this, ref matrix);
706
705
    }
707
706
 
708
707
    /// <summary>
793
792
    internal double height;
794
793
    private static readonly XRect s_empty;
795
794
  }
796
 
 
797
 
#else
798
 
  // Old code, delete end of 2008
799
 
 
800
 
  /// <summary>
801
 
  /// Stores a set of four floating-point numbers that represent the location and size of a rectangle.
802
 
  /// </summary>
803
 
  [DebuggerDisplay("({X}, {Y}, {Width}, {Height})")]
804
 
  public struct XRect
805
 
  {
806
 
    // Called XRect and not XRectangle because XRectangle will get the name of a shape object
807
 
    // in a forthcoming extension.
808
 
 
809
 
    /// <summary>
810
 
    /// Initializes a new instance of the XRect class.
811
 
    /// </summary>
812
 
    public XRect(double x, double y, double width, double height)
813
 
    {
814
 
      this.x = x;
815
 
      this.y = y;
816
 
      this.width = width;
817
 
      this.height = height;
818
 
    }
819
 
 
820
 
    /// <summary>
821
 
    /// Initializes a new instance of the XRect class.
822
 
    /// </summary>
823
 
    public XRect(XPoint location, XSize size)
824
 
    {
825
 
      this.x = location.X;
826
 
      this.y = location.Y;
827
 
      this.width = size.Width;
828
 
      this.height = size.Height;
829
 
    }
830
 
 
831
 
#if GDI
832
 
    /// <summary>
833
 
    /// Initializes a new instance of the XRect class.
834
 
    /// </summary>
835
 
    public XRect(PointF location, SizeF size)
836
 
    {
837
 
      this.x = location.X;
838
 
      this.y = location.Y;
839
 
      this.width = size.Width;
840
 
      this.height = size.Height;
841
 
    }
842
 
#endif
843
 
 
844
 
#if GDI
845
 
    /// <summary>
846
 
    /// Initializes a new instance of the XRect class.
847
 
    /// </summary>
848
 
    public XRect(RectangleF rect)
849
 
    {
850
 
      this.x = rect.X;
851
 
      this.y = rect.Y;
852
 
      this.width = rect.Width;
853
 
      this.height = rect.Height;
854
 
    }
855
 
#endif
856
 
 
857
 
#if WPF
858
 
    /// <summary>
859
 
    /// Initializes a new instance of the XRect class.
860
 
    /// </summary>
861
 
    public XRect(Rect rect)
862
 
    {
863
 
      this.x = rect.X;
864
 
      this.y = rect.Y;
865
 
      this.width = rect.Width;
866
 
      this.height = rect.Height;
867
 
    }
868
 
#endif
869
 
 
870
 
    /// <summary>
871
 
    /// Creates a rectangle from for straight lines.
872
 
    /// </summary>
873
 
    public static XRect FromLTRB(double left, double top, double right, double bottom)
874
 
    {
875
 
      return new XRect(left, top, right - left, bottom - top);
876
 
    }
877
 
 
878
 
    /// <summary>
879
 
    /// Returns the hash code for this instance.
880
 
    /// </summary>
881
 
    public override int GetHashCode()
882
 
    {
883
 
      // Lutz Roeder's .NET Reflector proudly presents:
884
 
      //   �THE ART OF HASH CODE PROGRAMMING�
885
 
      //
886
 
      // .NET 1.1:
887
 
      //   return (int) (((((uint) this.X) ^ ((((uint) this.Y) << 13) | (((uint) this.Y) >> 0x13))) ^ ((((uint) this.Width) << 0x1a) | (((uint) this.Width) >> 6))) ^ ((((uint) this.Height) << 7) | (((uint) this.Height) >> 0x19)));
888
 
      // Mono:
889
 
      //   return (int) (x + y + width + height);
890
 
      return (int)(x + y + width + height);
891
 
    }
892
 
 
893
 
    /// <summary>
894
 
    /// Indicates whether this instance and a specified object are equal.
895
 
    /// </summary>
896
 
    public override bool Equals(object obj)
897
 
    {
898
 
      if (obj is XRect)
899
 
      {
900
 
        XRect rect = (XRect)obj;
901
 
        return rect.x == this.x && rect.y == this.y && rect.width == this.width && rect.height == this.height;
902
 
      }
903
 
      return false;
904
 
    }
905
 
 
906
 
    /// <summary>
907
 
    /// Returns a string with the values of this rectangle.
908
 
    /// </summary>
909
 
    public override string ToString()
910
 
    {
911
 
      return String.Format("{{X={0},Y={1},Width={2},Height={3}}}", this.x, this.y, this.width, this.height);
912
 
    }
913
 
 
914
 
#if GDI
915
 
    /// <summary>
916
 
    /// Converts this instance to a System.Drawing.RectangleF.
917
 
    /// </summary>
918
 
    public RectangleF ToRectangleF()
919
 
    {
920
 
      return new RectangleF((float)this.x, (float)this.y, (float)this.width, (float)this.height);
921
 
    }
922
 
#endif
923
 
 
924
 
    /// <summary>
925
 
    /// Gets a value indicating whether this rectangle is empty.
926
 
    /// </summary>
927
 
    [Browsable(false)]
928
 
    public bool IsEmpty
929
 
    {
930
 
      // The .NET documentation differs from the actual implemention, which differs from the Mono 
931
 
      // implementation. This is my recommendation what an empty rectangle means:
932
 
      get { return this.width <= 0.0 || this.height <= 0.0; }
933
 
    }
934
 
 
935
 
    /// <summary>
936
 
    /// Gets or sets the location of the rectangle.
937
 
    /// </summary>
938
 
    [Browsable(false)]
939
 
    public XPoint Location
940
 
    {
941
 
      get { return new XPoint(this.x, this.y); }
942
 
      set { this.x = value.X; this.y = value.Y; }
943
 
    }
944
 
 
945
 
    /// <summary>
946
 
    /// Gets or sets the size of the rectangle.
947
 
    /// </summary>
948
 
    [Browsable(false)]
949
 
    public XSize Size
950
 
    {
951
 
      get { return new XSize(this.width, this.height); }
952
 
      set { this.width = value.Width; this.height = value.Height; }
953
 
    }
954
 
 
955
 
    /// <summary>
956
 
    /// Gets or sets the X value.
957
 
    /// </summary>
958
 
    public double X
959
 
    {
960
 
      get { return this.x; }
961
 
      set { this.x = value; }
962
 
    }
963
 
 
964
 
    /// <summary>
965
 
    /// Gets or sets the Y value.
966
 
    /// </summary>
967
 
    public double Y
968
 
    {
969
 
      get { return this.y; }
970
 
      set { this.y = value; }
971
 
    }
972
 
 
973
 
    /// <summary>
974
 
    /// Gets or sets the width.
975
 
    /// </summary>
976
 
    public double Width
977
 
    {
978
 
      get { return this.width; }
979
 
      set { this.width = value; }
980
 
    }
981
 
 
982
 
    /// <summary>
983
 
    /// Gets or sets the height.
984
 
    /// </summary>
985
 
    public double Height
986
 
    {
987
 
      get { return this.height; }
988
 
      set { this.height = value; }
989
 
    }
990
 
 
991
 
    /// <summary>
992
 
    /// Gets the left.
993
 
    /// </summary>
994
 
    [Browsable(false)]
995
 
    public double Left
996
 
    {
997
 
      get { return this.x; }
998
 
    }
999
 
 
1000
 
    /// <summary>
1001
 
    /// Gets the top.
1002
 
    /// </summary>
1003
 
    [Browsable(false)]
1004
 
    public double Top
1005
 
    {
1006
 
      get { return this.y; }
1007
 
    }
1008
 
 
1009
 
    /// <summary>
1010
 
    /// Gets the right.
1011
 
    /// </summary>
1012
 
    [Browsable(false)]
1013
 
    public double Right
1014
 
    {
1015
 
      get { return this.x + this.width; }
1016
 
    }
1017
 
 
1018
 
    /// <summary>
1019
 
    /// Gets the bottom.
1020
 
    /// </summary>
1021
 
    [Browsable(false)]
1022
 
    public double Bottom
1023
 
    {
1024
 
      get { return this.y + this.height; }
1025
 
    }
1026
 
 
1027
 
    /// <summary>
1028
 
    /// Gets the center of the rectangle.
1029
 
    /// </summary>
1030
 
    [Browsable(false)]
1031
 
    public XPoint Center
1032
 
    {
1033
 
      get { return new XPoint(this.x + this.width / 2, this.y + this.height / 2); }
1034
 
    }
1035
 
 
1036
 
    /// <summary>
1037
 
    /// Determines whether the rectangle contains the specified point.
1038
 
    /// </summary>
1039
 
    public bool Contains(XPoint pt)
1040
 
    {
1041
 
      return Contains(pt.X, pt.Y);
1042
 
    }
1043
 
 
1044
 
    /// <summary>
1045
 
    /// Determines whether the rectangle contains the specified point.
1046
 
    /// </summary>
1047
 
    public bool Contains(double x, double y)
1048
 
    {
1049
 
      return this.x <= x && x < this.x + this.width && this.y <= y && y < this.y + this.height;
1050
 
    }
1051
 
 
1052
 
    /// <summary>
1053
 
    /// Determines whether the rectangle completely contains the specified rectangle.
1054
 
    /// </summary>
1055
 
    public bool Contains(XRect rect)
1056
 
    {
1057
 
      return this.x <= rect.x && rect.x + rect.width <= this.x + this.width &&
1058
 
             this.y <= rect.y && rect.y + rect.height <= this.y + this.height;
1059
 
    }
1060
 
 
1061
 
    /// <summary>
1062
 
    /// Inflates the rectangle by the specified size.
1063
 
    /// </summary>
1064
 
    public void Inflate(XSize size)
1065
 
    {
1066
 
      Inflate(size.Width, size.Height);
1067
 
    }
1068
 
 
1069
 
    /// <summary>
1070
 
    /// Inflates the rectangle by the specified size.
1071
 
    /// </summary>
1072
 
    public void Inflate(double x, double y)
1073
 
    {
1074
 
      this.x -= x;
1075
 
      this.y -= y;
1076
 
      this.width += x * 2;
1077
 
      this.height += y * 2;
1078
 
    }
1079
 
 
1080
 
    /// <summary>
1081
 
    /// Inflates the rectangle by the specified size.
1082
 
    /// </summary>
1083
 
    public static XRect Inflate(XRect rect, double x, double y)
1084
 
    {
1085
 
      rect.Inflate(x, y);
1086
 
      return rect;
1087
 
    }
1088
 
 
1089
 
    /// <summary>
1090
 
    /// Intersects the rectangle with the specified rectangle.
1091
 
    /// </summary>
1092
 
    public void Intersect(XRect rect)
1093
 
    {
1094
 
      rect = XRect.Intersect(rect, this);
1095
 
      this.x = rect.x;
1096
 
      this.y = rect.y;
1097
 
      this.width = rect.width;
1098
 
      this.height = rect.height;
1099
 
    }
1100
 
 
1101
 
    /// <summary>
1102
 
    /// Intersects the specified rectangles.
1103
 
    /// </summary>
1104
 
    public static XRect Intersect(XRect left, XRect right)
1105
 
    {
1106
 
      double l = Math.Max(left.x, right.x);
1107
 
      double r = Math.Min(left.x + left.width, right.x + right.width);
1108
 
      double t = Math.Max(left.y, right.y);
1109
 
      double b = Math.Min(left.y + left.height, right.y + right.height);
1110
 
      if ((r >= l) && (b >= t))
1111
 
        return new XRect(l, t, r - l, b - t);
1112
 
      return XRect.Empty;
1113
 
    }
1114
 
 
1115
 
    /// <summary>
1116
 
    /// Determines whether the rectangle intersects with the specified rectangle.
1117
 
    /// </summary>
1118
 
    public bool IntersectsWith(XRect rect)
1119
 
    {
1120
 
      return rect.x < this.x + this.width && this.x < rect.x + rect.width &&
1121
 
        rect.y < this.y + this.height && this.y < rect.y + rect.height;
1122
 
    }
1123
 
 
1124
 
    /// <summary>
1125
 
    /// Unites the specified rectangles.
1126
 
    /// </summary>
1127
 
    public static XRect Union(XRect left, XRect right)
1128
 
    {
1129
 
      double l = Math.Min(left.X, right.X);
1130
 
      double r = Math.Max(left.X + left.Width, right.X + right.Width);
1131
 
      double t = Math.Min(left.Y, right.Y);
1132
 
      double b = Math.Max(left.Y + left.Height, right.Y + right.Height);
1133
 
      return new XRect(l, t, r - l, b - t);
1134
 
    }
1135
 
 
1136
 
    /// <summary>
1137
 
    /// Translates the rectangle by the specifed offset.
1138
 
    /// </summary>
1139
 
    public void Offset(XPoint pt)
1140
 
    {
1141
 
      Offset(pt.X, pt.Y);
1142
 
    }
1143
 
 
1144
 
    /// <summary>
1145
 
    /// Translates the rectangle by the specifed offset.
1146
 
    /// </summary>
1147
 
    public void Offset(double x, double y)
1148
 
    {
1149
 
      this.x += x;
1150
 
      this.y += y;
1151
 
    }
1152
 
 
1153
 
    /// <summary>
1154
 
    /// Translates the rectangle by adding the specifed point.
1155
 
    /// </summary>
1156
 
    public static XRect operator +(XRect rect, XPoint point)
1157
 
    {
1158
 
      return new XRect(rect.x + point.x, rect.Y + point.y, rect.width, rect.height);
1159
 
    }
1160
 
 
1161
 
    /// <summary>
1162
 
    /// Translates the rectangle by subtracting the specifed point.
1163
 
    /// </summary>
1164
 
    public static XRect operator -(XRect rect, XPoint point)
1165
 
    {
1166
 
      return new XRect(rect.x - point.x, rect.Y - point.y, rect.width, rect.height);
1167
 
    }
1168
 
 
1169
 
#if GDI
1170
 
    /// <summary>
1171
 
    /// Implicit conversion from a System.Drawing.Rectangle to an XRect.
1172
 
    /// </summary>
1173
 
    public static implicit operator XRect(Rectangle rect)
1174
 
    {
1175
 
      return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1176
 
    }
1177
 
 
1178
 
    /// <summary>
1179
 
    /// Implicit conversion from a System.Drawing.RectangleF to an XRect.
1180
 
    /// </summary>
1181
 
    public static implicit operator XRect(RectangleF rect)
1182
 
    {
1183
 
      return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1184
 
    }
1185
 
#endif
1186
 
 
1187
 
#if WPF
1188
 
    public static implicit operator XRect(Rect rect)
1189
 
    {
1190
 
      return new XRect(rect.X, rect.Y, rect.Width, rect.Height);
1191
 
    }
1192
 
#endif
1193
 
 
1194
 
    /// <summary>
1195
 
    /// Determines whether the two rectangles are equal.
1196
 
    /// </summary>
1197
 
    public static bool operator ==(XRect left, XRect right)
1198
 
    {
1199
 
      return left.x == right.x && left.y == right.y && left.width == right.width && left.height == right.height;
1200
 
    }
1201
 
 
1202
 
    /// <summary>
1203
 
    /// Determines whether the two rectangles not are equal.
1204
 
    /// </summary>
1205
 
    public static bool operator !=(XRect left, XRect right)
1206
 
    {
1207
 
      return !(left == right);
1208
 
    }
1209
 
 
1210
 
    /// <summary>
1211
 
    /// Represents the empty rectangle.
1212
 
    /// </summary>
1213
 
    public static readonly XRect Empty = new XRect();
1214
 
 
1215
 
    internal double x;
1216
 
    internal double y;
1217
 
    internal double width;
1218
 
    internal double height;
1219
 
  }
1220
 
#endif
1221
795
}