BarcodeUPC.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Text;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. namespace FastReport.Barcode
  6. {
  7. /// <summary>
  8. /// Generates the UPC E0 barcode.
  9. /// </summary>
  10. public class BarcodeUPC_E0 : BarcodeEAN
  11. {
  12. // UPC E Parity Pattern Table , Number System 0
  13. internal static string[,] tabelle_UPC_E0 = {
  14. {"E", "E", "E", "o", "o", "o" }, // 0
  15. {"E", "E", "o", "E", "o", "o" }, // 1
  16. {"E", "E", "o", "o", "E", "o" }, // 2
  17. {"E", "E", "o", "o", "o", "E" }, // 3
  18. {"E", "o", "E", "E", "o", "o" }, // 4
  19. {"E", "o", "o", "E", "E", "o" }, // 5
  20. {"E", "o", "o", "o", "E", "E" }, // 6
  21. {"E", "o", "E", "o", "E", "o" }, // 7
  22. {"E", "o", "E", "o", "o", "E" }, // 8
  23. {"E", "o", "o", "E", "o", "E" } // 9
  24. };
  25. internal override void DrawText(IGraphics g, string barData)
  26. {
  27. DrawString(g, -8, -2, "0", true);
  28. // parts of pattern: 3 + 24 + 6
  29. float x1 = GetWidth(pattern.Substring(0, 3));
  30. float x2 = GetWidth(pattern.Substring(0, 3 + 24));
  31. DrawString(g, x1, x2, barData.Substring(0, 6));
  32. x1 = GetWidth(pattern) + 1;
  33. x2 = x1 + 7;
  34. DrawString(g, x1, x2, barData.Substring(6, 1), true);
  35. }
  36. internal override string GetPattern()
  37. {
  38. text = DoCheckSumming(text, 7);
  39. int c = CharToInt(text[6]);
  40. // Startcode
  41. string result = "A0A";
  42. for (int i = 0; i <= 5; i++)
  43. {
  44. if (tabelle_UPC_E0[c, i] == "E")
  45. {
  46. for (int j = 0; j <= 3; j++)
  47. result += tabelle_EAN_C[CharToInt(text[i])][3 - j];
  48. }
  49. else
  50. result += tabelle_EAN_A[CharToInt(text[i])];
  51. }
  52. // Stopcode
  53. result += "0A0A0A";
  54. return result;
  55. }
  56. /// <summary>
  57. /// Initializes a new instance of the <see cref="BarcodeUPC_E0"/> class with default settings.
  58. /// </summary>
  59. public BarcodeUPC_E0()
  60. {
  61. extra1 = 8;
  62. extra2 = 8;
  63. }
  64. }
  65. /// <summary>
  66. /// Generates the UPC E1 barcode.
  67. /// </summary>
  68. public class BarcodeUPC_E1 : BarcodeUPC_E0
  69. {
  70. internal override string GetPattern()
  71. {
  72. text = DoCheckSumming(text, 7);
  73. int c = CharToInt(text[6]);
  74. // Startcode
  75. string result = "A0A";
  76. for (int i = 0; i <= 5; i++)
  77. {
  78. if (tabelle_UPC_E0[c, i] == "E")
  79. result += tabelle_EAN_A[CharToInt(text[i])];
  80. else
  81. {
  82. for (int j = 0; j <= 3; j++)
  83. result += tabelle_EAN_C[CharToInt(text[i])][3 - j];
  84. }
  85. }
  86. // Stopcode
  87. result += "0A0A0A";
  88. return result;
  89. }
  90. }
  91. /// <summary>
  92. /// Generates the UPC A barcode.
  93. /// </summary>
  94. public class BarcodeUPC_A : BarcodeUPC_E0
  95. {
  96. internal override void DrawText(IGraphics g, string barData)
  97. {
  98. DrawString(g, -8, -2, barData.Substring(0, 1), true);
  99. // parts of pattern: 7 + 20 + 5 + 20 + 7
  100. float x1 = GetWidth(pattern.Substring(0, 7));
  101. float x2 = GetWidth(pattern.Substring(0, 7 + 20));
  102. DrawString(g, x1, x2, barData.Substring(1, 5));
  103. x1 = GetWidth(pattern.Substring(0, 7 + 20 + 5));
  104. x2 = GetWidth(pattern.Substring(0, 7 + 20 + 5 + 20));
  105. DrawString(g, x1, x2, barData.Substring(6, 5));
  106. x1 = GetWidth(pattern) + 1;
  107. x2 = x1 + 7;
  108. DrawString(g, x1, x2, barData.Substring(11, 1), true);
  109. }
  110. internal override string GetPattern()
  111. {
  112. text = DoCheckSumming(text, 12);
  113. //Startcode
  114. string result = "A0A";
  115. for (int i = 0; i <= 5; i++)
  116. {
  117. string code = tabelle_EAN_A[CharToInt(text[i])];
  118. result += i == 0 ? MakeLong(code) : code;
  119. }
  120. //Trennzeichen
  121. result += "0A0A0";
  122. for (int i = 6; i <= 11; i++)
  123. {
  124. string code = tabelle_EAN_C[CharToInt(text[i])];
  125. result += i == 11 ? MakeLong(code) : code;
  126. }
  127. //Stopcode
  128. return result + "A0A";
  129. }
  130. }
  131. /// <summary>
  132. /// Generates the 2-digit supplement barcode.
  133. /// </summary>
  134. public class BarcodeSupplement2 : BarcodeUPC_E0
  135. {
  136. internal string getSupp(string Nr)
  137. {
  138. int sum = 0;
  139. string tmp = Nr.Substring(0, Nr.Length - 1);
  140. int fak = tmp.Length;
  141. for (int i = 0; i < tmp.Length; i++)
  142. {
  143. sum += int.Parse(tmp[i].ToString()) * ((fak % 2) == 0 ? 9 : 3);
  144. fak--;
  145. }
  146. sum = ((sum % 10) % 10) % 10;
  147. return tmp + sum.ToString();
  148. }
  149. internal override void DrawText(IGraphics g, string barData)
  150. {
  151. DrawString(g, 0, drawArea.Width, barData);
  152. }
  153. internal override string GetPattern()
  154. {
  155. text = SetLen(2);
  156. int i32 = int.Parse(text);
  157. string mS;
  158. switch (i32 % 4)
  159. {
  160. case 0:
  161. mS = "oo";
  162. break;
  163. case 1:
  164. mS = "oE";
  165. break;
  166. case 2:
  167. mS = "Eo";
  168. break;
  169. case 3:
  170. mS = "EE";
  171. break;
  172. default:
  173. mS = "";
  174. break;
  175. }
  176. string tmp = getSupp(text + "0");
  177. string result = "506"; // Startcode
  178. for (int i = 0; i <= 1; i++)
  179. {
  180. if (mS[i] == 'E')
  181. {
  182. for (int j = 0; j <= 3; j++)
  183. result += tabelle_EAN_C[CharToInt(tmp[i])][3 - j];
  184. }
  185. else
  186. result += tabelle_EAN_A[CharToInt(tmp[i])];
  187. if (i < 1)
  188. result += "05"; // character delineator
  189. }
  190. return result;
  191. }
  192. /// <summary>
  193. /// Initializes a new instance of the <see cref="BarcodeSupplement2"/> class with default settings.
  194. /// </summary>
  195. public BarcodeSupplement2()
  196. {
  197. textUp = true;
  198. extra1 = 0;
  199. extra2 = 0;
  200. }
  201. }
  202. /// <summary>
  203. /// Generates the 5-digit supplement barcode.
  204. /// </summary>
  205. internal class BarcodeSupplement5 : BarcodeSupplement2
  206. {
  207. internal override string GetPattern()
  208. {
  209. text = SetLen(5);
  210. string tmp = getSupp(text + "0");
  211. char c = tmp[5];
  212. string result = "506"; // Startcode
  213. for (int i = 0; i <= 4; i++)
  214. {
  215. if (tabelle_UPC_E0[CharToInt(c), 1 + i] == "E")
  216. {
  217. for (int j = 0; j <= 3; j++)
  218. result += tabelle_EAN_C[CharToInt(tmp[i])][3 - j];
  219. }
  220. else
  221. result += tabelle_EAN_A[CharToInt(tmp[i])];
  222. if (i < 4)
  223. result += "05"; // character delineator
  224. }
  225. return result;
  226. }
  227. }
  228. }