BarcodeEAN.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.ComponentModel;
  6. using FastReport.Utils;
  7. namespace FastReport.Barcode
  8. {
  9. /// <summary>
  10. /// The base class for EAN barcodes.
  11. /// </summary>
  12. public abstract class BarcodeEAN : LinearBarcodeBase
  13. {
  14. // Pattern for Barcode EAN Charset C
  15. // S1 L1 S2 L2
  16. internal static string[] tabelle_EAN_C = {
  17. "7150", // 0
  18. "6160", // 1
  19. "6061", // 2
  20. "5350", // 3
  21. "5071", // 4
  22. "5170", // 5
  23. "5053", // 6
  24. "5251", // 7
  25. "5152", // 8
  26. "7051" // 9
  27. };
  28. // Pattern for Barcode EAN Charset A
  29. // L1 S1 L2 S2
  30. internal static string[] tabelle_EAN_A = {
  31. "2605", //0
  32. "1615", //1
  33. "1516", //2
  34. "0805", //3
  35. "0526", //4
  36. "0625", //5
  37. "0508", //6
  38. "0706", //7
  39. "0607", //8
  40. "2506" //9
  41. };
  42. // Pattern for Barcode EAN Zeichensatz B}
  43. //L1 S1 L2 S2
  44. internal static string[] tabelle_EAN_B = {
  45. "0517", // 0
  46. "0616", // 1
  47. "1606", // 2
  48. "0535", // 3
  49. "1705", // 4
  50. "0715", // 5
  51. "3505", // 6
  52. "1525", // 7
  53. "2515", // 8
  54. "1507" // 9
  55. };
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="BarcodeEAN"/> class with default settings.
  58. /// </summary>
  59. public BarcodeEAN()
  60. {
  61. ratioMin = 2;
  62. ratioMax = 3;
  63. }
  64. }
  65. /// <summary>
  66. /// Generates the EAN8 barcode.
  67. /// </summary>
  68. public class BarcodeEAN8 : BarcodeEAN
  69. {
  70. internal override void DrawText(IGraphics g, string barData)
  71. {
  72. // parts of pattern: 3 + 16 + 5 + 16 + 3
  73. float x1 = GetWidth(pattern.Substring(0, 3));
  74. float x2 = GetWidth(pattern.Substring(0, 3 + 16 + 1));
  75. DrawString(g, x1, x2, barData.Substring(0, 4));
  76. x1 = GetWidth(pattern.Substring(0, 3 + 16 + 5 - 1));
  77. x2 = GetWidth(pattern.Substring(0, 3 + 16 + 5 + 16));
  78. DrawString(g, x1, x2, barData.Substring(4, 4));
  79. }
  80. internal override string GetPattern()
  81. {
  82. text = DoCheckSumming(text, 8);
  83. string result = "A0A"; // Startcode
  84. for (int i = 0; i <= 3; i++)
  85. {
  86. result += tabelle_EAN_A[CharToInt(text[i])];
  87. }
  88. result += "0A0A0"; // Center Guard Pattern
  89. for (int i = 4; i <= 7; i++)
  90. {
  91. result += tabelle_EAN_C[CharToInt(text[i])];
  92. }
  93. result += "A0A"; // Stopcode
  94. return result;
  95. }
  96. }
  97. /// <summary>
  98. /// Generates the EAN13 barcode.
  99. /// </summary>
  100. public class BarcodeEAN13 : BarcodeEAN
  101. {
  102. //Zuordung der Paraitaetsfolgen f¹r EAN13
  103. private static string[,] tabelle_ParityEAN13 = {
  104. {"A", "A", "A", "A", "A", "A"}, // 0
  105. {"A", "A", "B", "A", "B", "B"}, // 1
  106. {"A", "A", "B", "B", "A", "B"}, // 2
  107. {"A", "A", "B", "B", "B", "A"}, // 3
  108. {"A", "B", "A", "A", "B", "B"}, // 4
  109. {"A", "B", "B", "A", "A", "B"}, // 5
  110. {"A", "B", "B", "B", "A", "A"}, // 6
  111. {"A", "B", "A", "B", "A", "B"}, // 7
  112. {"A", "B", "A", "B", "B", "A"}, // 8
  113. {"A", "B", "B", "A", "B", "A"} // 9
  114. };
  115. internal override void DrawText(IGraphics g, string barData)
  116. {
  117. DrawString(g, -8, -2, barData.Substring(0, 1));
  118. // parts of pattern: 3 + 24 + 5 + 24 + 3
  119. float x1 = GetWidth(pattern.Substring(0, 3));
  120. float x2 = GetWidth(pattern.Substring(0, 3 + 24 + 1));
  121. DrawString(g, x1, x2, barData.Substring(1, 6));
  122. x1 = GetWidth(pattern.Substring(0, 3 + 24 + 5 - 1));
  123. x2 = GetWidth(pattern.Substring(0, 3 + 24 + 5 + 24));
  124. DrawString(g, x1, x2, barData.Substring(7, 6));
  125. }
  126. internal override string GetPattern()
  127. {
  128. int LK;
  129. text = DoCheckSumming(text, 13);
  130. string tmp = text;
  131. LK = CharToInt(tmp[0]);
  132. tmp = tmp.Substring(1, 12);
  133. string result = "A0A"; // Startcode
  134. for (int i = 0; i <= 5; i++)
  135. {
  136. int idx = CharToInt(tmp[i]);
  137. switch (tabelle_ParityEAN13[LK, i])
  138. {
  139. case "A":
  140. result += tabelle_EAN_A[idx];
  141. break;
  142. case "B":
  143. result += tabelle_EAN_B[idx];
  144. break;
  145. case "C":
  146. result += tabelle_EAN_C[idx];
  147. break;
  148. }
  149. }
  150. result += "0A0A0"; //Center Guard Pattern
  151. for (int i = 6; i < 12; i++)
  152. {
  153. result += tabelle_EAN_C[CharToInt(tmp[i])];
  154. }
  155. result += "A0A"; // Stopcode
  156. return result;
  157. }
  158. /// <summary>
  159. /// Initializes a new instance of the <see cref="BarcodeEAN13"/> class with default settings.
  160. /// </summary>
  161. public BarcodeEAN13()
  162. {
  163. extra1 = 8;
  164. }
  165. }
  166. /// <summary>
  167. /// Generates the GS1-128 (formerly known as UCC-128 or EAN-128) barcode.
  168. /// </summary>
  169. public class BarcodeEAN128 : Barcode128
  170. {
  171. /// <summary>
  172. /// Initializes a new instance of the <see cref="BarcodeEAN128"/> class with default settings.
  173. /// </summary>
  174. public BarcodeEAN128() : base()
  175. {
  176. }
  177. internal override string GetPattern()
  178. {
  179. string snapshot = text;
  180. if (text.StartsWith("("))
  181. {
  182. text = GS1Helper.ParseGS1(text);
  183. if (string.IsNullOrEmpty(text))
  184. {
  185. text = "&C;&1;" + snapshot.Replace("(", "&A;").Replace(")", "").Replace(" ", "").Substring(3);
  186. }
  187. else
  188. {
  189. text = "&C;" + text;
  190. }
  191. }
  192. else
  193. text = "&C;&1;" + text.Replace("(", "&A;").Replace(")", "").Replace(" ", "");
  194. string result = base.GetPattern();
  195. text = snapshot;
  196. return result;
  197. }
  198. }
  199. }