~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Globalization;
 
33
using System.ComponentModel;
 
34
using System.Runtime.InteropServices;
 
35
#if GDI
 
36
using System.Drawing;
 
37
#endif
 
38
#if WPF
 
39
using System.Windows;
 
40
#endif
 
41
using PdfSharp.Internal;
 
42
 
 
43
namespace PdfSharp.Drawing
 
44
{
 
45
#if true
 
46
  /// <summary>
 
47
  /// Represents a pair of floating-point numbers, typically the width and height of a
 
48
  /// graphical object.
 
49
  /// </summary>
 
50
  //[DebuggerDisplay("({Width}, {Height})")]
 
51
  [DebuggerDisplay("Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}, Height={Height.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}")]
 
52
  [Serializable, StructLayout(LayoutKind.Sequential)] //, ValueSerializer(typeof(SizeValueSerializer)), TypeConverter(typeof(SizeConverter))]
 
53
  public struct XSize : IFormattable
 
54
  {
 
55
    /// <summary>
 
56
    /// Initializes a new instance of the XPoint class with the specified values.
 
57
    /// </summary>
 
58
    public XSize(double width, double height)
 
59
    {
 
60
      if (width < 0 || height < 0)
 
61
        throw new ArgumentException("WidthAndHeightCannotBeNegative"); //SR.Get(SRID.Size_WidthAndHeightCannotBeNegative, new object[0]));
 
62
 
 
63
      this.width = width;
 
64
      this.height = height;
 
65
    }
 
66
 
 
67
    /// <summary>
 
68
    /// Initializes a new instance of the <see cref="XSize"/> class.
 
69
    /// </summary>
 
70
    [Obsolete("Use explicit conversion to make your code more readable.")]
 
71
    public XSize(XPoint pt)  // DELETE: 08-12-31
 
72
    {
 
73
      this.width = pt.X;
 
74
      this.height = pt.Y;
 
75
    }
 
76
 
 
77
    /// <summary>
 
78
    /// Determines whether two size objects are equal.
 
79
    /// </summary>
 
80
    public static bool operator ==(XSize size1, XSize size2)
 
81
    {
 
82
      return size1.Width == size2.Width && size1.Height == size2.Height;
 
83
    }
 
84
 
 
85
    /// <summary>
 
86
    /// Determines whether two size objects are not equal.
 
87
    /// </summary>
 
88
    public static bool operator !=(XSize size1, XSize size2)
 
89
    {
 
90
      return !(size1 == size2);
 
91
    }
 
92
 
 
93
    /// <summary>
 
94
    /// Indicates whether this tow instance are equal.
 
95
    /// </summary>
 
96
    public static bool Equals(XSize size1, XSize size2)
 
97
    {
 
98
      if (size1.IsEmpty)
 
99
        return size2.IsEmpty;
 
100
      return size1.Width.Equals(size2.Width) && size1.Height.Equals(size2.Height);
 
101
    }
 
102
 
 
103
    /// <summary>
 
104
    /// Indicates whether this instance and a specified object are equal.
 
105
    /// </summary>
 
106
    public override bool Equals(object o)
 
107
    {
 
108
      if (o == null || !(o is XSize))
 
109
        return false;
 
110
      XSize size = (XSize)o;
 
111
      return Equals(this, size);
 
112
    }
 
113
 
 
114
    /// <summary>
 
115
    /// Indicates whether this instance and a specified size are equal.
 
116
    /// </summary>
 
117
    public bool Equals(XSize value)
 
118
    {
 
119
      return Equals(this, value);
 
120
    }
 
121
 
 
122
    /// <summary>
 
123
    /// Returns the hash code for this instance.
 
124
    /// </summary>
 
125
    public override int GetHashCode()
 
126
    {
 
127
      if (this.IsEmpty)
 
128
        return 0;
 
129
      return this.Width.GetHashCode() ^ this.Height.GetHashCode();
 
130
    }
 
131
 
 
132
    /// <summary>
 
133
    /// Parses the size from a string.
 
134
    /// </summary>
 
135
    public static XSize Parse(string source)
 
136
    {
 
137
      XSize empty;
 
138
      CultureInfo cultureInfo = CultureInfo.InvariantCulture;
 
139
      TokenizerHelper helper = new TokenizerHelper(source, cultureInfo);
 
140
      string str = helper.NextTokenRequired();
 
141
      if (str == "Empty")
 
142
        empty = Empty;
 
143
      else
 
144
        empty = new XSize(Convert.ToDouble(str, cultureInfo), Convert.ToDouble(helper.NextTokenRequired(), cultureInfo));
 
145
      helper.LastTokenRequired();
 
146
      return empty;
 
147
    }
 
148
 
 
149
#if GDI
 
150
    /// <summary>
 
151
    /// Converts this XSize to a PointF.
 
152
    /// </summary>
 
153
    public PointF ToPointF()
 
154
    {
 
155
      return new PointF((float)this.width, (float)this.height);
 
156
    }
 
157
#endif
 
158
 
 
159
    /// <summary>
 
160
    /// Converts this XSize to an XPoint.
 
161
    /// </summary>
 
162
    public XPoint ToXPoint()
 
163
    {
 
164
      return new XPoint(this.width, this.height);
 
165
    }
 
166
 
 
167
    /// <summary>
 
168
    /// Converts this XSize to an XVector.
 
169
    /// </summary>
 
170
    public XVector ToXVector()
 
171
    {
 
172
      return new XVector(this.width, this.height);
 
173
    }
 
174
 
 
175
#if GDI
 
176
    /// <summary>
 
177
    /// Converts this XSize to a SizeF.
 
178
    /// </summary>
 
179
    public SizeF ToSizeF()
 
180
    {
 
181
      return new SizeF((float)this.width, (float)this.height);
 
182
    }
 
183
#endif
 
184
 
 
185
#if WPF
 
186
    /// <summary>
 
187
    /// Converts this XSize to a System.Windows.Size.
 
188
    /// </summary>
 
189
    public System.Windows.Size ToSize()
 
190
    {
 
191
      return new System.Windows.Size(this.width, this.height);
 
192
    }
 
193
#endif
 
194
 
 
195
#if GDI
 
196
    /// <summary>
 
197
    /// Creates an XSize from a System.Drawing.Size.
 
198
    /// </summary>
 
199
    public static XSize FromSize(System.Drawing.Size size)
 
200
    {
 
201
      return new XSize(size.Width, size.Height);
 
202
    }
 
203
 
 
204
    /// <summary>
 
205
    /// Implicit conversion from XSize to System.Drawing.Size. The conversion must be implicit because the
 
206
    /// WinForms designer uses it.
 
207
    /// </summary>
 
208
    public static implicit operator XSize(System.Drawing.Size size)
 
209
    {
 
210
      return new XSize(size.Width, size.Height);
 
211
    }
 
212
#endif
 
213
 
 
214
#if WPF
 
215
    /// <summary>
 
216
    /// Creates an XSize from a System.Drawing.Size.
 
217
    /// </summary>
 
218
    public static XSize FromSize(System.Windows.Size size)
 
219
    {
 
220
      return new XSize(size.Width, size.Height);
 
221
    }
 
222
#endif
 
223
 
 
224
#if GDI
 
225
    /// <summary>
 
226
    /// Creates an XSize from a System.Drawing.Size.
 
227
    /// </summary>
 
228
    public static XSize FromSizeF(SizeF size)
 
229
    {
 
230
      return new XSize(size.Width, size.Height);
 
231
    }
 
232
#endif
 
233
 
 
234
    /// <summary>
 
235
    /// Converts this XSize to a human readable string.
 
236
    /// </summary>
 
237
    public override string ToString()
 
238
    {
 
239
      return this.ConvertToString(null, null);
 
240
    }
 
241
 
 
242
    /// <summary>
 
243
    /// Converts this XSize to a human readable string.
 
244
    /// </summary>
 
245
    public string ToString(IFormatProvider provider)
 
246
    {
 
247
      return this.ConvertToString(null, provider);
 
248
    }
 
249
 
 
250
    /// <summary>
 
251
    /// Converts this XSize to a human readable string.
 
252
    /// </summary>
 
253
    string IFormattable.ToString(string format, IFormatProvider provider)
 
254
    {
 
255
      return this.ConvertToString(format, provider);
 
256
    }
 
257
 
 
258
    internal string ConvertToString(string format, IFormatProvider provider)
 
259
    {
 
260
      if (IsEmpty)
 
261
        return "Empty";
 
262
 
 
263
      char numericListSeparator = TokenizerHelper.GetNumericListSeparator(provider);
 
264
      return string.Format(provider, "{1:" + format + "}{0}{2:" + format + "}", new object[] { numericListSeparator, this.width, this.height });
 
265
    }
 
266
 
 
267
    /// <summary>
 
268
    /// Returns an empty size, i.e. a size with a width or height less than 0.
 
269
    /// </summary>
 
270
    public static XSize Empty
 
271
    {
 
272
      get { return XSize.s_empty; }
 
273
    }
 
274
 
 
275
    /// <summary>
 
276
    /// Gets a value indicating whether this instance is empty.
 
277
    /// </summary>
 
278
    public bool IsEmpty
 
279
    {
 
280
      get { return this.width < 0; }
 
281
    }
 
282
 
 
283
    /// <summary>
 
284
    /// Gets or sets the width.
 
285
    /// </summary>
 
286
    public double Width
 
287
    {
 
288
      get { return this.width; }
 
289
      set
 
290
      {
 
291
        if (this.IsEmpty)
 
292
          throw new InvalidOperationException("CannotModifyEmptySize"); //SR.Get(SRID.Size_CannotModifyEmptySize, new object[0]));
 
293
        if (value < 0)
 
294
          throw new ArgumentException("WidthCannotBeNegative"); //SR.Get(SRID.Size_WidthCannotBeNegative, new object[0]));
 
295
        this.width = value;
 
296
      }
 
297
    }
 
298
 
 
299
    /// <summary>
 
300
    /// Gets or sets the height.
 
301
    /// </summary>
 
302
    public double Height
 
303
    {
 
304
      get { return this.height; }
 
305
      set
 
306
      {
 
307
        if (this.IsEmpty)
 
308
          throw new InvalidOperationException("CannotModifyEmptySize"); // SR.Get(SRID.Size_CannotModifyEmptySize, new object[0]));
 
309
        if (value < 0)
 
310
          throw new ArgumentException("HeightCannotBeNegative"); //SR.Get(SRID.Size_HeightCannotBeNegative, new object[0]));
 
311
        this.height = value;
 
312
      }
 
313
    }
 
314
 
 
315
    /// <summary>
 
316
    /// Performs an explicit conversion from XSize to XVector.
 
317
    /// </summary>
 
318
    public static explicit operator XVector(XSize size)
 
319
    {
 
320
      return new XVector(size.width, size.height);
 
321
    }
 
322
 
 
323
    /// <summary>
 
324
    /// Performs an explicit conversion from XSize to XPoint.
 
325
    /// </summary>
 
326
    public static explicit operator XPoint(XSize size)
 
327
    {
 
328
      return new XPoint(size.width, size.height);
 
329
    }
 
330
 
 
331
#if WPF
 
332
    /// <summary>
 
333
    /// Performs an explicit conversion from Size to XSize.
 
334
    /// </summary>
 
335
    public static explicit operator XSize(System.Windows.Size size)
 
336
    {
 
337
      return new XSize(size.Width, size.Height);
 
338
    }
 
339
#endif
 
340
 
 
341
    private static XSize CreateEmptySize()
 
342
    {
 
343
      XSize size = new XSize();
 
344
      size.width = double.NegativeInfinity;
 
345
      size.height = double.NegativeInfinity;
 
346
      return size;
 
347
    }
 
348
 
 
349
    static XSize()
 
350
    {
 
351
      s_empty = CreateEmptySize();
 
352
    }
 
353
 
 
354
    internal double width;
 
355
    internal double height;
 
356
    private static readonly XSize s_empty;
 
357
  }
 
358
 
 
359
#else
 
360
  // Old code, delete end of 2008
 
361
 
 
362
  /// <summary>
 
363
  /// Represents a pair of floating-point numbers, typically the width and height of a
 
364
  /// graphical object.
 
365
  /// </summary>
 
366
  //[DebuggerDisplay("({Width}, {Height})")]
 
367
  [DebuggerDisplay("Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}, Width={Width.ToString(\"0.####\",System.Globalization.CultureInfo.InvariantCulture)}")]
 
368
  public struct XSize
 
369
  {
 
370
    /// <summary>
 
371
    /// Initializes a new instance of the <see cref="XSize"/> class.
 
372
    /// </summary>
 
373
    public XSize(XSize size)
 
374
    {
 
375
      this.width = size.width;
 
376
      this.height = size.height;
 
377
    }
 
378
 
 
379
    /// <summary>
 
380
    /// Initializes a new instance of the <see cref="XSize"/> class.
 
381
    /// </summary>
 
382
    /// <param name="pt">The pt.</param>
 
383
    public XSize(XPoint pt)
 
384
    {
 
385
      this.width = pt.X;
 
386
      this.height = pt.Y;
 
387
    }
 
388
 
 
389
    /// <summary>
 
390
    /// Initializes a new instance of the <see cref="XSize"/> class.
 
391
    /// </summary>
 
392
    /// <param name="width">The width.</param>
 
393
    /// <param name="height">The height.</param>
 
394
    public XSize(double width, double height)
 
395
    {
 
396
      this.width = width;
 
397
      this.height = height;
 
398
    }
 
399
 
 
400
    /// <summary>
 
401
    /// Returns the hash code for this instance.
 
402
    /// </summary>
 
403
    public override int GetHashCode()
 
404
    {
 
405
      return this.width.GetHashCode() ^ this.height.GetHashCode();
 
406
    }
 
407
 
 
408
    /// <summary>
 
409
    /// Indicates whether this instance and a specified object are equal.
 
410
    /// </summary>
 
411
    public override bool Equals(object obj)
 
412
    {
 
413
      if (obj is XSize)
 
414
      {
 
415
        XSize size = (XSize)obj;
 
416
        return size.width == this.width && size.height == this.height;
 
417
      }
 
418
      return false;
 
419
    }
 
420
 
 
421
#if GDI
 
422
    /// <summary>
 
423
    /// Creates an XSize from a System.Drawing.Size.
 
424
    /// </summary>
 
425
    public static XSize FromSize(System.Drawing.Size size)
 
426
    {
 
427
      return new XSize(size.Width, size.Height);
 
428
    }
 
429
#endif
 
430
 
 
431
#if WPF
 
432
    /// <summary>
 
433
    /// Creates an XSize from a System.Drawing.Size.
 
434
    /// </summary>
 
435
    public static XSize FromSize(System.Windows.Size size)
 
436
    {
 
437
      return new XSize(size.Width, size.Height);
 
438
    }
 
439
#endif
 
440
 
 
441
#if GDI
 
442
    /// <summary>
 
443
    /// Creates an XSize from a System.Drawing.Size.
 
444
    /// </summary>
 
445
    public static XSize FromSizeF(SizeF size)
 
446
    {
 
447
      return new XSize(size.Width, size.Height);
 
448
    }
 
449
#endif
 
450
 
 
451
    /// <summary>
 
452
    /// Returns a string with the values of this instance.
 
453
    /// </summary>
 
454
    public override string ToString()
 
455
    {
 
456
      return string.Format("{{Width={0}, Height={1}}}", this.width, this.height);
 
457
    }
 
458
 
 
459
#if GDI
 
460
    /// <summary>
 
461
    /// Converts this XSize to a PointF.
 
462
    /// </summary>
 
463
    public PointF ToPointF()
 
464
    {
 
465
      return new PointF((float)this.width, (float)this.height);
 
466
    }
 
467
#endif
 
468
 
 
469
    /// <summary>
 
470
    /// Converts this XSize to an XPoint.
 
471
    /// </summary>
 
472
    public XPoint ToXPoint()
 
473
    {
 
474
      return new XPoint(this.width, this.height);
 
475
    }
 
476
 
 
477
#if GDI
 
478
    /// <summary>
 
479
    /// Converts this XSize to a SizeF.
 
480
    /// </summary>
 
481
    public SizeF ToSizeF()
 
482
    {
 
483
      return new SizeF((float)this.width, (float)this.height);
 
484
    }
 
485
#endif
 
486
 
 
487
    /// <summary>
 
488
    /// Gets a value indicating whether this instance is empty.
 
489
    /// </summary>
 
490
    [Browsable(false)]
 
491
    public bool IsEmpty
 
492
    {
 
493
      get { return this.width == 0 && this.height == 0; }
 
494
    }
 
495
 
 
496
    /// <summary>
 
497
    /// Gets or sets the width.
 
498
    /// </summary>
 
499
    public double Width
 
500
    {
 
501
      get { return this.width; }
 
502
      set { this.width = value; }
 
503
    }
 
504
 
 
505
    /// <summary>
 
506
    /// Gets or sets the height.
 
507
    /// </summary>
 
508
    public double Height
 
509
    {
 
510
      get { return this.height; }
 
511
      set { this.height = value; }
 
512
    }
 
513
 
 
514
    /// <summary>
 
515
    /// Adds two size objects.
 
516
    /// </summary>
 
517
    public static XSize operator +(XSize size1, XSize size2)
 
518
    {
 
519
      return new XSize(size1.width + size2.width, size1.height + size2.height);
 
520
    }
 
521
 
 
522
    /// <summary>
 
523
    /// Subtracts two size objects.
 
524
    /// </summary>
 
525
    public static XSize operator -(XSize size1, XSize size2)
 
526
    {
 
527
      return new XSize(size1.width - size2.width, size1.height - size2.height);
 
528
    }
 
529
 
 
530
    /// <summary>
 
531
    /// Multiplies a size with a scalar.
 
532
    /// </summary>
 
533
    public static XSize operator *(XSize size, double f)
 
534
    {
 
535
      return new XSize(size.width * f, size.height * f);
 
536
    }
 
537
 
 
538
    /// <summary>
 
539
    /// Multiplies a scalar with a size.
 
540
    /// </summary>
 
541
    public static XSize operator *(double f, XSize size)
 
542
    {
 
543
      return new XSize(f * size.width, f * size.height);
 
544
    }
 
545
 
 
546
    /// <summary>
 
547
    /// Divides a size by a scalar.
 
548
    /// </summary>
 
549
    public static XSize operator /(XSize size, double f)
 
550
    {
 
551
      if (f == 0)
 
552
        throw new DivideByZeroException("Divisor is zero.");
 
553
 
 
554
      return new XSize(size.width / f, size.height / f);
 
555
    }
 
556
 
 
557
    /// <summary>
 
558
    /// Determines whether two size objects are equal.
 
559
    /// </summary>
 
560
    public static bool operator ==(XSize left, XSize right)
 
561
    {
 
562
      return left.width == right.width && left.height == right.height;
 
563
    }
 
564
 
 
565
    /// <summary>
 
566
    /// Determines whether two size objects are not equal.
 
567
    /// </summary>
 
568
    public static bool operator !=(XSize left, XSize right)
 
569
    {
 
570
      return !(left == right);
 
571
    }
 
572
 
 
573
    /// <summary>
 
574
    /// Explicit conversion from XSize to XPoint.
 
575
    /// </summary>
 
576
    public static explicit operator XPoint(XSize size)
 
577
    {
 
578
      return new XPoint(size.width, size.height);
 
579
    }
 
580
 
 
581
    /// <summary>
 
582
    /// Explicit conversion from XSize to System.Drawing.Size.
 
583
    /// </summary>
 
584
    public static implicit operator XSize(System.Drawing.Size size)
 
585
    {
 
586
      return new XSize(size.Width, size.Height);
 
587
    }
 
588
 
 
589
    /// <summary>
 
590
    /// Represents the empty size.
 
591
    /// </summary>
 
592
    public static readonly XSize Empty = new XSize();
 
593
 
 
594
    internal double width;
 
595
    internal double height;
 
596
  }
 
597
#endif
 
598
}
 
 
b'\\ No newline at end of file'