BarcodeBase.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Drawing;
  3. using System.ComponentModel;
  4. using FastReport.Utils;
  5. namespace FastReport.Barcode
  6. {
  7. /// <summary>
  8. /// The base class for all barcodes.
  9. /// </summary>
  10. [TypeConverter(typeof(FastReport.TypeConverters.BarcodeConverter))]
  11. public abstract class BarcodeBase
  12. {
  13. #region Fields
  14. internal string text;
  15. internal int angle;
  16. internal bool showText;
  17. internal float zoom;
  18. internal bool showMarker;
  19. private Color color;
  20. private Font font;
  21. private static readonly Font DefaultFont = new Font("Arial", 8);
  22. #endregion
  23. #region Properties
  24. /// <summary>
  25. /// Gets the name of barcode.
  26. /// </summary>
  27. [Browsable(false)]
  28. public string Name
  29. {
  30. get { return Barcodes.GetName(GetType()); }
  31. }
  32. /// <summary>
  33. /// Gets or sets the color of barcode.
  34. /// </summary>
  35. public Color Color
  36. {
  37. get { return color; }
  38. set { color = value; }
  39. }
  40. /// <summary>
  41. /// Gets or sets the font of barcode.
  42. /// </summary>
  43. public Font Font
  44. {
  45. get { return font; }
  46. set { font = value; }
  47. }
  48. #endregion
  49. #region Public Methods
  50. /// <summary>
  51. /// Creates the exact copy of this barcode.
  52. /// </summary>
  53. /// <returns>The copy of this barcode.</returns>
  54. public BarcodeBase Clone()
  55. {
  56. BarcodeBase result = Activator.CreateInstance(GetType()) as BarcodeBase;
  57. result.Assign(this);
  58. return result;
  59. }
  60. /// <summary>
  61. /// Assigns properties from other, similar barcode.
  62. /// </summary>
  63. /// <param name="source">Barcode object to assign properties from.</param>
  64. public virtual void Assign(BarcodeBase source)
  65. {
  66. Color = source.Color;
  67. Font = source.Font;
  68. }
  69. internal virtual void Serialize(FRWriter writer, string prefix, BarcodeBase diff)
  70. {
  71. if (diff.GetType() != GetType())
  72. writer.WriteStr("Barcode", Name);
  73. if (diff.Color != Color)
  74. writer.WriteValue(prefix + "Color", Color);
  75. if (diff.Font != Font)
  76. writer.WriteValue(prefix + "Font", Font);
  77. }
  78. internal virtual void Initialize(string text, bool showText, int angle, float zoom)
  79. {
  80. this.text = text;
  81. this.showText = showText;
  82. this.angle = (angle / 90 * 90) % 360;
  83. this.zoom = zoom;
  84. }
  85. internal virtual void Initialize(string text, bool showText, int angle, float zoom, bool showMarker)
  86. {
  87. this.text = text;
  88. this.showText = showText;
  89. this.angle = (angle / 90 * 90) % 360;
  90. this.zoom = zoom;
  91. this.showMarker = showMarker;
  92. }
  93. internal virtual SizeF CalcBounds()
  94. {
  95. return SizeF.Empty;
  96. }
  97. internal virtual string StripControlCodes(string data)
  98. {
  99. return data;
  100. }
  101. /// <summary>
  102. /// Draws a barcode.
  103. /// </summary>
  104. /// <param name="g">The graphic surface.</param>
  105. /// <param name="displayRect">Display rectangle.</param>
  106. public virtual void DrawBarcode(IGraphics g, RectangleF displayRect)
  107. {
  108. }
  109. #endregion
  110. /// <summary>
  111. /// Initializes a new instance of the <see cref="BarcodeBase"/> class with default settings.
  112. /// </summary>
  113. public BarcodeBase()
  114. {
  115. text = "";
  116. color = Color.Black;
  117. Font = DefaultFont;
  118. }
  119. /// <summary>
  120. /// Get default value of this barcode
  121. /// </summary>
  122. /// <returns></returns>
  123. public virtual string GetDefaultValue()
  124. {
  125. return "12345678";
  126. }
  127. }
  128. }