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
|
#region PDFsharp - A .NET library for processing PDF
//
// Authors:
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
//
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://sourceforge.net/projects/pdfsharp
//
// 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.
#endregion
using System;
using System.Diagnostics;
using System.Globalization;
namespace PdfSharp.Drawing
{
/// <summary>
/// Represents a value and its unit of measure. The structure converts implicitly from and to
/// double with a value measured in point.
/// </summary>
public struct XUnit : IFormattable
{
internal const double PointFactor = 1;
internal const double InchFactor = 72;
internal const double MillimeterFactor = 72 / 25.4;
internal const double CentimeterFactor = 72 / 2.54;
internal const double PresentationFactor = 72 / 96.0;
internal const double PointFactorWpf = 96 / 72.0;
internal const double InchFactorWpf = 96;
internal const double MillimeterFactorWpf = 96 / 25.4;
internal const double CentimeterFactorWpf = 96 / 2.54;
internal const double PresentationFactorWpf = 1;
/// <summary>
/// Initializes a new instance of the XUnit class with type set to point.
/// </summary>
public XUnit(double point)
{
this.value = point;
this.type = XGraphicsUnit.Point;
}
/// <summary>
/// Initializes a new instance of the XUnit class.
/// </summary>
public XUnit(double value, XGraphicsUnit type)
{
if (!Enum.IsDefined(typeof(XGraphicsUnit), type))
throw new System.ComponentModel.InvalidEnumArgumentException("type");
this.value = value;
this.type = type;
}
/// <summary>
/// Gets the raw value of the object without any conversion.
/// To determine the XGraphicsUnit use property <code>Type</code>.
/// To get the value in point use the implicit convertion to double.
/// </summary>
public double Value
{
get { return this.value; }
}
/// <summary>
/// Gets the unit of measure.
/// </summary>
public XGraphicsUnit Type
{
get { return (XGraphicsUnit)type; }
}
/// <summary>
/// Gets or sets the value in point.
/// </summary>
public double Point
{
get
{
switch (type)
{
case XGraphicsUnit.Point:
return this.value;
case XGraphicsUnit.Inch:
return this.value * 72;
case XGraphicsUnit.Millimeter:
return this.value * 72 / 25.4;
case XGraphicsUnit.Centimeter:
return this.value * 72 / 2.54;
case XGraphicsUnit.Presentation:
return this.value * 72 / 96;
default:
throw new InvalidCastException();
}
}
set
{
this.value = value;
this.type = XGraphicsUnit.Point;
}
}
/// <summary>
/// Gets or sets the value in inch.
/// </summary>
public double Inch
{
get
{
switch (type)
{
case XGraphicsUnit.Point:
return this.value / 72;
case XGraphicsUnit.Inch:
return this.value;
case XGraphicsUnit.Millimeter:
return this.value / 25.4;
case XGraphicsUnit.Centimeter:
return this.value / 2.54;
case XGraphicsUnit.Presentation:
return this.value / 96;
default:
throw new InvalidCastException();
}
}
set
{
this.value = value;
this.type = XGraphicsUnit.Inch;
}
}
/// <summary>
/// Gets or sets the value in millimeter.
/// </summary>
public double Millimeter
{
get
{
switch (this.type)
{
case XGraphicsUnit.Point:
return this.value * 25.4 / 72;
case XGraphicsUnit.Inch:
return this.value * 25.4;
case XGraphicsUnit.Millimeter:
return this.value;
case XGraphicsUnit.Centimeter:
return this.value * 10;
case XGraphicsUnit.Presentation:
return this.value * 25.4 / 96;
default:
throw new InvalidCastException();
}
}
set
{
this.value = value;
this.type = XGraphicsUnit.Millimeter;
}
}
/// <summary>
/// Gets or sets the value in centimeter.
/// </summary>
public double Centimeter
{
get
{
switch (type)
{
case XGraphicsUnit.Point:
return this.value * 2.54 / 72;
case XGraphicsUnit.Inch:
return this.value * 2.54;
case XGraphicsUnit.Millimeter:
return this.value / 10;
case XGraphicsUnit.Centimeter:
return this.value;
case XGraphicsUnit.Presentation:
return this.value * 2.54 / 96;
default:
throw new InvalidCastException();
}
}
set
{
this.value = value;
this.type = XGraphicsUnit.Centimeter;
}
}
/// <summary>
/// Gets or sets the value in presentation units (1/96 inch).
/// </summary>
public double Presentation
{
get
{
switch (type)
{
case XGraphicsUnit.Point:
return this.value * 96 / 72;
case XGraphicsUnit.Inch:
return this.value * 96;
case XGraphicsUnit.Millimeter:
return this.value * 96 / 25.4;
case XGraphicsUnit.Centimeter:
return this.value * 96 / 2.54;
case XGraphicsUnit.Presentation:
return this.value;
default:
throw new InvalidCastException();
}
}
set
{
this.value = value;
this.type = XGraphicsUnit.Point;
}
}
/// <summary>
/// Returns the object as string using the format information.
/// The unit of measure is appended to the end of the string.
/// </summary>
public string ToString(System.IFormatProvider formatProvider)
{
string valuestring;
valuestring = this.value.ToString(formatProvider) + GetSuffix();
return valuestring;
}
/// <summary>
/// Returns the object as string using the specified format and format information.
/// The unit of measure is appended to the end of the string.
/// </summary>
string System.IFormattable.ToString(string format, IFormatProvider formatProvider)
{
string valuestring;
valuestring = this.value.ToString(format, formatProvider) + GetSuffix();
return valuestring;
}
/// <summary>
/// Returns the object as string. The unit of measure is appended to the end of the string.
/// </summary>
public override string ToString()
{
string valuestring;
valuestring = this.value.ToString(CultureInfo.InvariantCulture) + GetSuffix();
return valuestring;
}
/// <summary>
/// Returns the unit of measure of the object as a string like 'pt', 'cm', or 'in'.
/// </summary>
string GetSuffix()
{
switch (type)
{
case XGraphicsUnit.Point:
return "pt";
case XGraphicsUnit.Inch:
return "in";
case XGraphicsUnit.Millimeter:
return "mm";
case XGraphicsUnit.Centimeter:
return "cm";
case XGraphicsUnit.Presentation:
return "pu";
//case XGraphicsUnit.Pica:
// return "pc";
//case XGraphicsUnit.Line:
// return "li";
default:
throw new InvalidCastException();
}
}
/// <summary>
/// Returns an XUnit object. Sets type to point.
/// </summary>
public static XUnit FromPoint(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Point;
return unit;
}
/// <summary>
/// Returns an XUnit object. Sets type to inch.
/// </summary>
public static XUnit FromInch(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Inch;
return unit;
}
/// <summary>
/// Returns an XUnit object. Sets type to millimeters.
/// </summary>
public static XUnit FromMillimeter(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Millimeter;
return unit;
}
/// <summary>
/// Returns an XUnit object. Sets type to centimeters.
/// </summary>
public static XUnit FromCentimeter(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Centimeter;
return unit;
}
/// <summary>
/// Returns an XUnit object. Sets type to Presentation.
/// </summary>
public static XUnit FromPresentation(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Presentation;
return unit;
}
#if deferred
///// <summary>
///// Returns an XUnit object. Sets type to pica.
///// </summary>
//public static XUnit FromPica(double val)
//{
// XUnit unit;
// unit.val = val;
// unit.type = XGraphicsUnit.Pica;
// return unit;
//}
//
///// <summary>
///// Returns an XUnit object. Sets type to line.
///// </summary>
//public static XUnit FromLine(double val)
//{
// XUnit unit;
// unit.val = val;
// unit.type = XGraphicsUnit.Line;
// return unit;
//}
#endif
/// <summary>
/// Converts a string to an XUnit object.
/// If the string contains a suffix like 'cm' or 'in' the object will be converted
/// to the appropriate type, otherwise point is assumed.
/// </summary>
public static implicit operator XUnit(string value)
{
XUnit unit;
value = value.Trim();
// HACK for Germans...
value = value.Replace(',', '.');
int count = value.Length;
int valLen = 0;
for (; valLen < count; )
{
char ch = value[valLen];
if (ch == '.' || ch == '-' || ch == '+' || Char.IsNumber(ch))
valLen++;
else
break;
}
try
{
unit.value = Double.Parse(value.Substring(0, valLen).Trim(), CultureInfo.InvariantCulture);
}
catch (Exception ex)
{
unit.value = 1;
string message = String.Format("String '{0}' is not a valid value for structure 'XUnit'.", value);
throw new ArgumentException(message, ex);
}
string typeStr = value.Substring(valLen).Trim().ToLower();
unit.type = XGraphicsUnit.Point;
switch (typeStr)
{
case "cm":
unit.type = XGraphicsUnit.Centimeter;
break;
case "in":
unit.type = XGraphicsUnit.Inch;
break;
case "mm":
unit.type = XGraphicsUnit.Millimeter;
break;
//case "pc":
// unit.type = XGraphicsUnit.Pica;
// break;
//
//case "li":
// unit.type = XGraphicsUnit.Line;
// break;
case "":
case "pt":
unit.type = XGraphicsUnit.Point;
break;
case "pu": // presentation units
unit.type = XGraphicsUnit.Presentation;
break;
default:
throw new ArgumentException("Unknown unit type: '" + typeStr + "'");
}
return unit;
}
/// <summary>
/// Converts an int to an XUnit object with type set to point.
/// </summary>
public static implicit operator XUnit(int value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Point;
return unit;
}
/// <summary>
/// Converts a double to an XUnit object with type set to point.
/// </summary>
public static implicit operator XUnit(double value)
{
XUnit unit;
unit.value = value;
unit.type = XGraphicsUnit.Point;
return unit;
}
/// <summary>
/// Returns a double value as point.
/// </summary>
public static implicit operator double(XUnit value)
{
return value.Point;
}
/// <summary>
/// Memberwise comparison. To compare by value,
/// use code like Math.Abs(a.Pt - b.Pt) < 1e5.
/// </summary>
public static bool operator ==(XUnit value1, XUnit value2)
{
return value1.type == value2.type && value1.value == value2.value;
}
/// <summary>
/// Memberwise comparison. To compare by value,
/// use code like Math.Abs(a.Pt - b.Pt) < 1e5.
/// </summary>
public static bool operator !=(XUnit value1, XUnit value2)
{
return !(value1 == value2);
}
/// <summary>
/// Calls base class Equals.
/// </summary>
public override bool Equals(Object obj)
{
if (obj is XUnit)
return this == (XUnit)obj;
return false;
}
/// <summary>
/// Returns the hash code for this instance.
/// </summary>
public override int GetHashCode()
{
return this.value.GetHashCode() ^ this.type.GetHashCode();
}
/// <summary>
/// This member is intended to be used by XmlDomainObjectReader only.
/// </summary>
public static XUnit Parse(string value)
{
XUnit unit = value;
return unit;
}
/// <summary>
/// Converts an existing object from one unit into another unit type.
/// </summary>
public void ConvertType(XGraphicsUnit type)
{
if (this.type == type)
return;
switch (type)
{
case XGraphicsUnit.Point:
this.value = Point;
this.type = XGraphicsUnit.Point;
break;
case XGraphicsUnit.Inch:
this.value = Inch;
this.type = XGraphicsUnit.Inch;
break;
case XGraphicsUnit.Centimeter:
this.value = Centimeter;
this.type = XGraphicsUnit.Centimeter;
break;
case XGraphicsUnit.Millimeter:
this.value = Millimeter;
this.type = XGraphicsUnit.Millimeter;
break;
case XGraphicsUnit.Presentation:
this.value = Presentation;
this.type = XGraphicsUnit.Presentation;
break;
// case XGraphicsUnit.Pica:
// this.value = this.Pc;
// this.type = XGraphicsUnit.Pica;
// break;
//
// case XGraphicsUnit.Line:
// this.value = this.Li;
// this.type = XGraphicsUnit.Line;
// break;
default:
throw new ArgumentException("Unknown unit type: '" + type.ToString() + "'");
}
}
/// <summary>
/// Represents a unit with all values zero.
/// </summary>
public static readonly XUnit Zero = new XUnit();
double value;
XGraphicsUnit type;
#if true_
/// <summary>
/// Some test code.
/// </summary>
[Conditional("DEBUG")]
public static void TestIt()
{
double v;
XUnit u1 = 1000;
v = u1;
v = u1.Point;
v = u1.Inch;
v = u1.Millimeter;
v = u1.Centimeter;
v = u1.Presentation;
u1 = "10cm";
v = u1.Point;
v.GetType();
}
#endif
}
}
|