Barcode39.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Text;
  3. namespace FastReport.Barcode
  4. {
  5. /// <summary>
  6. /// Generates the Code39 barcode.
  7. /// </summary>
  8. public class Barcode39 : LinearBarcodeBase
  9. {
  10. #if READONLY_STRUCTS
  11. private readonly struct Code39
  12. #else
  13. private struct Code39
  14. #endif
  15. {
  16. #pragma warning disable FR0006 // Field name of struct must be longer than 2 characters.
  17. public readonly string c;
  18. #pragma warning restore FR0006 // Field name of struct must be longer than 2 characters.
  19. public readonly string data;
  20. public readonly short chk;
  21. public Code39(string c, string data, short chk)
  22. {
  23. this.c = c;
  24. this.data = data;
  25. this.chk = chk;
  26. }
  27. }
  28. private static Code39[] tabelle_39 = {
  29. new Code39("0", "505160605", 0),
  30. new Code39("1", "605150506", 1),
  31. new Code39("2", "506150506", 2),
  32. new Code39("3", "606150505", 3),
  33. new Code39("4", "505160506", 4),
  34. new Code39("5", "605160505", 5),
  35. new Code39("6", "506160505", 6),
  36. new Code39("7", "505150606", 7),
  37. new Code39("8", "605150605", 8),
  38. new Code39("9", "506150605", 9),
  39. new Code39("A", "605051506", 10),
  40. new Code39("B", "506051506", 11),
  41. new Code39("C", "606051505", 12),
  42. new Code39("D", "505061506", 13),
  43. new Code39("E", "605061505", 14),
  44. new Code39("F", "506061505", 15),
  45. new Code39("G", "505051606", 16),
  46. new Code39("H", "605051605", 17),
  47. new Code39("I", "506051605", 18),
  48. new Code39("J", "505061605", 19),
  49. new Code39("K", "605050516", 20),
  50. new Code39("L", "506050516", 21),
  51. new Code39("M", "606050515", 22),
  52. new Code39("N", "505060516", 23),
  53. new Code39("O", "605060515", 24),
  54. new Code39("P", "506060515", 25),
  55. new Code39("Q", "505050616", 26),
  56. new Code39("R", "605050615", 27),
  57. new Code39("S", "506050615", 28),
  58. new Code39("T", "505060615", 29),
  59. new Code39("U", "615050506", 30),
  60. new Code39("V", "516050506", 31),
  61. new Code39("W", "616050505", 32),
  62. new Code39("X", "515060506", 33),
  63. new Code39("Y", "615060505", 34),
  64. new Code39("Z", "516060505", 35),
  65. new Code39("-", "515050606", 36),
  66. new Code39(".", "615050605", 37),
  67. new Code39(" ", "516050605", 38),
  68. new Code39("*", "515060605", 0),
  69. new Code39("$", "515151505", 39),
  70. new Code39("/", "515150515", 40),
  71. new Code39("+", "515051515", 41),
  72. new Code39("%", "505151515", 42)
  73. };
  74. /// <inheritdoc/>
  75. public override bool IsNumeric
  76. {
  77. get { return false; }
  78. }
  79. private int FindBarItem(string c)
  80. {
  81. for (int i = 0; i < tabelle_39.Length; i++)
  82. {
  83. if (c == tabelle_39[i].c)
  84. return i;
  85. }
  86. return -1;
  87. }
  88. internal override string GetPattern()
  89. {
  90. int checksum = 0;
  91. // Startcode
  92. string result = tabelle_39[FindBarItem("*")].data + '0';
  93. foreach (char c in text)
  94. {
  95. int idx = FindBarItem(c.ToString());
  96. if (idx < 0)
  97. continue;
  98. result += tabelle_39[idx].data + '0';
  99. checksum += tabelle_39[idx].chk;
  100. }
  101. // Calculate Checksum Data
  102. if (CalcCheckSum)
  103. {
  104. checksum = checksum % 43;
  105. foreach (Code39 i in tabelle_39)
  106. {
  107. if (checksum == i.chk)
  108. {
  109. result += i.data + '0';
  110. break;
  111. }
  112. }
  113. }
  114. // Stopcode
  115. result += tabelle_39[FindBarItem("*")].data;
  116. return result;
  117. }
  118. /// <summary>
  119. /// Initializes a new instance of the <see cref="Barcode39"/> class with default settings.
  120. /// </summary>
  121. public Barcode39()
  122. {
  123. ratioMin = 2;
  124. ratioMax = 3;
  125. }
  126. }
  127. /// <summary>
  128. /// Generates the Code39 extended barcode.
  129. /// </summary>
  130. public class Barcode39Extended : Barcode39
  131. {
  132. private static string[] code39x = {
  133. "%U", "$A", "$B", "$C", "$D", "$E", "$F", "$G",
  134. "$H", "$I", "$J", "$K", "$L", "$M", "$N", "$O",
  135. "$P", "$Q", "$R", "$S", "$T", "$U", "$V", "$W",
  136. "$X", "$Y", "$Z", "%A", "%B", "%C", "%D", "%E",
  137. " ", "/A", "/B", "/C", "/D", "/E", "/F", "/G",
  138. "/H", "/I", "/J", "/K", "/L", "/M", "/N", "/O",
  139. "0", "1", "2", "3", "4", "5", "6", "7",
  140. "8", "9", "/Z", "%F", "%G", "%H", "%I", "%J",
  141. "%V", "A", "B", "C", "D", "E", "F", "G",
  142. "H", "I", "J", "K", "L", "M", "N", "O",
  143. "P", "Q", "R", "S", "T", "U", "V", "W",
  144. "X", "Y", "Z", "%K", "%L", "%M", "%N", "%O",
  145. "%W", "+A", "+B", "+C", "+D", "+E", "+F", "+G",
  146. "+H", "+I", "+J", "+K", "+L", "+M", "+N", "+O",
  147. "+P", "+Q", "+R", "+S", "+T", "+U", "+V", "+W",
  148. "+X", "+Y", "+Z", "%P", "%Q", "%R", "%S", "%T"
  149. };
  150. internal override string GetPattern()
  151. {
  152. string saveText = text;
  153. text = "";
  154. for (int i = 0; i < saveText.Length; i++)
  155. {
  156. if (saveText[i] <= (char)127)
  157. text += code39x[saveText[i]];
  158. }
  159. string pattern = base.GetPattern();
  160. text = saveText;
  161. return pattern;
  162. }
  163. }
  164. }