Barcode93.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. using System;
  2. using System.Text;
  3. using FastReport.Utils;
  4. namespace FastReport.Barcode
  5. {
  6. /// <summary>
  7. /// Generates the Code93 barcode.
  8. /// </summary>
  9. public class Barcode93 : LinearBarcodeBase
  10. {
  11. #if READONLY_STRUCTS
  12. private readonly struct Code93
  13. #else
  14. private struct Code93
  15. #endif
  16. {
  17. public readonly string data;
  18. #pragma warning disable FR0006 // Field name of struct must be longer than 2 characters.
  19. public readonly string c;
  20. #pragma warning restore FR0006 // Field name of struct must be longer than 2 characters.
  21. public Code93(string _c, string _data)
  22. {
  23. data = _data;
  24. c = _c;
  25. }
  26. }
  27. private static Code93[] tabelle_93 = {
  28. new Code93("0", "131112"),
  29. new Code93("1", "111213"),
  30. new Code93("2", "111312"),
  31. new Code93("3", "111411"),
  32. new Code93("4", "121113"),
  33. new Code93("5", "121212"),
  34. new Code93("6", "121311"),
  35. new Code93("7", "111114"),
  36. new Code93("8", "131211"),
  37. new Code93("9", "141111"),
  38. new Code93("A", "211113"),
  39. new Code93("B", "211212"),
  40. new Code93("C", "211311"),
  41. new Code93("D", "221112"),
  42. new Code93("E", "221211"),
  43. new Code93("F", "231111"),
  44. new Code93("G", "112113"),
  45. new Code93("H", "112212"),
  46. new Code93("I", "112311"),
  47. new Code93("J", "122112"),
  48. new Code93("K", "132111"),
  49. new Code93("L", "111123"),
  50. new Code93("M", "111222"),
  51. new Code93("N", "111321"),
  52. new Code93("O", "121122"),
  53. new Code93("P", "131121"),
  54. new Code93("Q", "212112"),
  55. new Code93("R", "212211"),
  56. new Code93("S", "211122"),
  57. new Code93("T", "211221"),
  58. new Code93("U", "221121"),
  59. new Code93("V", "222111"),
  60. new Code93("W", "112122"),
  61. new Code93("X", "112221"),
  62. new Code93("Y", "122121"),
  63. new Code93("Z", "123111"),
  64. new Code93("-", "121131"),
  65. new Code93(".", "311112"),
  66. new Code93(" ", "311211"),
  67. new Code93("$", "321111"),
  68. new Code93("/", "112131"),
  69. new Code93("+", "113121"),
  70. new Code93("%", "211131"),
  71. new Code93("[", "121221"), // only used for Extended Code 93
  72. new Code93("]", "312111"), // only used for Extended Code 93}
  73. new Code93("{", "311121"), // only used for Extended Code 93}
  74. new Code93("}", "122211") // only used for Extended Code 93}
  75. };
  76. /// <inheritdoc/>
  77. public override bool IsNumeric
  78. {
  79. get { return false; }
  80. }
  81. private int FindBarItem(string c)
  82. {
  83. for (int i = 0; i < tabelle_93.Length; i++)
  84. {
  85. if (c == tabelle_93[i].c)
  86. return i;
  87. }
  88. return -1;
  89. }
  90. internal override string GetPattern()
  91. {
  92. string result = "111141"; // Startcode
  93. foreach (char c in text)
  94. {
  95. int idx = FindBarItem(c.ToString());
  96. if (idx < 0)
  97. throw new Exception(Res.Get("Messages,InvalidBarcode2"));
  98. result += tabelle_93[idx].data;
  99. }
  100. // Checksums
  101. if (CalcCheckSum)
  102. {
  103. int checkC = 0;
  104. int checkK = 0;
  105. int weightC = 1;
  106. int weightK = 2;
  107. for (int i = text.Length - 1; i >= 0; i--)
  108. {
  109. int idx = FindBarItem(text[i].ToString());
  110. checkC += idx * weightC;
  111. checkK += idx * weightK;
  112. weightC++;
  113. if (weightC > 20)
  114. weightC = 1;
  115. weightK++;
  116. if (weightK > 15)
  117. weightC = 1;
  118. };
  119. checkK += checkC;
  120. checkC = checkC % 47;
  121. checkK = checkK % 47;
  122. result += tabelle_93[checkC].data + tabelle_93[checkK].data;
  123. }
  124. // Stopcode
  125. result += "1111411";
  126. return DoConvert(result);
  127. }
  128. }
  129. /// <summary>
  130. /// Generates the Code93 extended barcode.
  131. /// </summary>
  132. public class Barcode93Extended : Barcode93
  133. {
  134. private static string[] code93x = {
  135. "]U", "[A", "[B", "[C", "[D", "[E", "[F", "[G",
  136. "[H", "[I", "[J", "[K", "[L", "[M", "[N", "[O",
  137. "[P", "[Q", "[R", "[S", "[T", "[U", "[V", "[W",
  138. "[X", "[Y", "[Z", "]A", "]B", "]C", "]D", "]E",
  139. " ", "{A", "{B", "{C", "{D", "{E", "{F", "{G",
  140. "{H", "{I", "{J", "{K", "{L", "{M", "{N", "{O",
  141. "0", "1", "2", "3", "4", "5", "6", "7",
  142. "8", "9", "{Z", "]F", "]G", "]H", "]I", "]J",
  143. "]V", "A", "B", "C", "D", "E", "F", "G",
  144. "H", "I", "J", "K", "L", "M", "N", "O",
  145. "P", "Q", "R", "S", "T", "U", "V", "W",
  146. "X", "Y", "Z", "]K", "]L", "]M", "]N", "]O",
  147. "]W", "}A", "}B", "}C", "}D", "}E", "}F", "}G",
  148. "}H", "}I", "}J", "}K", "}L", "}M", "}N", "}O",
  149. "}P", "}Q", "}R", "}S", "}T", "}U", "}V", "}W",
  150. "}X", "}Y", "}Z", "]P", "]Q", "]R", "]S", "]T"
  151. };
  152. internal override string GetPattern()
  153. {
  154. string saveText = text;
  155. text = "";
  156. for (int i = 0; i < saveText.Length; i++)
  157. {
  158. if (saveText[i] <= (char)127)
  159. text += code93x[saveText[i]];
  160. }
  161. string pattern = base.GetPattern();
  162. text = saveText;
  163. return pattern;
  164. }
  165. }
  166. }