Barcode2of5.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Drawing;
  5. using System.Text;
  6. namespace FastReport.Barcode
  7. {
  8. /// <summary>
  9. /// Generates the "2/5 Interleaved" barcode.
  10. /// </summary>
  11. public class Barcode2of5Interleaved : LinearBarcodeBase
  12. {
  13. internal static int[,] tabelle_2_5 = {
  14. {0, 0, 1, 1, 0}, // 0
  15. {1, 0, 0, 0, 1}, // 1
  16. {0, 1, 0, 0, 1}, // 2
  17. {1, 1, 0, 0, 0}, // 3
  18. {0, 0, 1, 0, 1}, // 4
  19. {1, 0, 1, 0, 0}, // 5
  20. {0, 1, 1, 0, 0}, // 6
  21. {0, 0, 0, 1, 1}, // 7
  22. {1, 0, 0, 1, 0}, // 8
  23. {0, 1, 0, 1, 0} // 9
  24. };
  25. internal override string GetPattern()
  26. {
  27. string text = base.text;
  28. string result = "5050"; //Startcode
  29. string c;
  30. if (CalcCheckSum)
  31. {
  32. if (text.Length % 2 == 0)
  33. text = text.Substring(1, text.Length - 1);
  34. text = DoCheckSumming(text);
  35. }
  36. else
  37. {
  38. if (text.Length % 2 != 0)
  39. text = "0" + text;
  40. }
  41. for (int i = 0; i < (text.Length / 2); i++)
  42. {
  43. for (int j = 0; j <= 4; j++)
  44. {
  45. if (tabelle_2_5[CharToInt(text[i * 2]), j] == 1)
  46. c = "6";
  47. else
  48. c = "5";
  49. result += c;
  50. if (tabelle_2_5[CharToInt(text[i * 2 + 1]), j] == 1)
  51. c = "1";
  52. else
  53. c = "0";
  54. result += c;
  55. }
  56. }
  57. result += "605"; // Stopcode
  58. return result;
  59. }
  60. /// <summary>
  61. /// Initializes a new instance of the <see cref="Barcode2of5Interleaved"/> class with default settings.
  62. /// </summary>
  63. public Barcode2of5Interleaved()
  64. {
  65. ratioMin = 2;
  66. ratioMax = 3;
  67. }
  68. }
  69. /// <summary>
  70. /// Generates the "Deutsche Identcode" barcode.
  71. /// </summary>
  72. public class BarcodeDeutscheIdentcode : Barcode2of5Interleaved
  73. {
  74. #region Properties
  75. /// <summary>
  76. /// Gets or sets a value that indicates that CheckSum should be printed.
  77. /// </summary>
  78. [System.ComponentModel.DefaultValue(true)]
  79. [System.ComponentModel.Category("Appearance")]
  80. public bool PrintCheckSum { get; set; }
  81. #endregion
  82. private string CheckSumModulo10(string data)
  83. {
  84. int sum = 0;
  85. int fak = data.Length;
  86. for (int i = 0; i < data.Length; i++)
  87. {
  88. if ((fak % 2) == 0)
  89. sum += int.Parse(data[i].ToString()) * 9;
  90. else
  91. sum += int.Parse(data[i].ToString()) * 4;
  92. fak--;
  93. }
  94. if ((sum % 10) == 0)
  95. return data + "0";
  96. return data + (10 - (sum % 10)).ToString();
  97. }
  98. internal override string GetPattern()
  99. {
  100. string result = "5050"; //Startcode
  101. string c;
  102. string text = base.text.Replace(".", "").Replace(" ", "");
  103. if(CalcCheckSum)
  104. {
  105. if (text.Length == 11)
  106. text = CheckSumModulo10(text);
  107. else if (text.Length != 12)
  108. throw new Exception(Res.Get("Messages,BarcodeLengthMismatch"));
  109. }
  110. else
  111. {
  112. if(text.Length != 12)
  113. throw new Exception(Res.Get("Messages,BarcodeLengthMismatch"));
  114. }
  115. for (int i = 0; i < (text.Length / 2); i++)
  116. {
  117. for (int j = 0; j <= 4; j++)
  118. {
  119. if (tabelle_2_5[CharToInt(text[i * 2]), j] == 1)
  120. c = "6";
  121. else
  122. c = "5";
  123. result += c;
  124. if (tabelle_2_5[CharToInt(text[i * 2 + 1]), j] == 1)
  125. c = "1";
  126. else
  127. c = "0";
  128. result += c;
  129. }
  130. }
  131. result += "605"; // Stopcode
  132. base.text = text.Insert(2, ".").Insert(6, " ").Insert(10, ".");
  133. if(!PrintCheckSum)
  134. {
  135. base.text = base.text.Substring(0, base.text.Length - 1);
  136. }
  137. else
  138. base.text = base.text.Insert(14, " ");
  139. return result;
  140. }
  141. /// <inheritdoc/>
  142. public override void Assign(BarcodeBase source)
  143. {
  144. base.Assign(source);
  145. BarcodeDeutscheIdentcode src = source as BarcodeDeutscheIdentcode;
  146. PrintCheckSum = src.PrintCheckSum;
  147. }
  148. internal override void Serialize(FRWriter writer, string prefix, BarcodeBase diff)
  149. {
  150. base.Serialize(writer, prefix, diff);
  151. BarcodeDeutscheIdentcode c = diff as BarcodeDeutscheIdentcode;
  152. if (c == null || PrintCheckSum != c.PrintCheckSum)
  153. writer.WriteValue(prefix + "DrawVerticalBearerBars", PrintCheckSum);
  154. }
  155. /// <summary>
  156. /// Initializes a new instance of the <see cref="BarcodeDeutscheIdentcode"/> class with default settings.
  157. /// </summary>
  158. public BarcodeDeutscheIdentcode()
  159. {
  160. ratioMin = 2.25F;
  161. ratioMax = 3.5F;
  162. WideBarRatio = 3F;
  163. PrintCheckSum = true;
  164. }
  165. /// <inheritdoc />
  166. public override string GetDefaultValue()
  167. {
  168. return "12345123456";
  169. }
  170. }
  171. /// <summary>
  172. /// Generates the "Deutsche Leitcode" barcode.
  173. /// </summary>
  174. public class BarcodeDeutscheLeitcode : Barcode2of5Interleaved
  175. {
  176. #region Properties
  177. /// <summary>
  178. /// Gets or sets a value that indicates that CheckSum should be printed.
  179. /// </summary>
  180. [System.ComponentModel.DefaultValue(true)]
  181. [System.ComponentModel.Category("Appearance")]
  182. public bool PrintCheckSum { get; set; }
  183. private string CheckSumModulo10(string data)
  184. {
  185. int sum = 0;
  186. int fak = data.Length;
  187. for (int i = 0; i < data.Length; i++)
  188. {
  189. if ((fak % 2) == 0)
  190. sum += int.Parse(data[i].ToString()) * 9;
  191. else
  192. sum += int.Parse(data[i].ToString()) * 4;
  193. fak--;
  194. }
  195. if ((sum % 10) == 0)
  196. return data + "0";
  197. return data + (10 - (sum % 10)).ToString();
  198. }
  199. #endregion
  200. internal override void Serialize(FRWriter writer, string prefix, BarcodeBase diff)
  201. {
  202. base.Serialize(writer, prefix, diff);
  203. BarcodeDeutscheLeitcode c = diff as BarcodeDeutscheLeitcode;
  204. if (c == null || PrintCheckSum != c.PrintCheckSum)
  205. writer.WriteValue(prefix + "DrawVerticalBearerBars", PrintCheckSum);
  206. }
  207. /// <inheritdoc/>
  208. public override void Assign(BarcodeBase source)
  209. {
  210. base.Assign(source);
  211. BarcodeDeutscheLeitcode src = source as BarcodeDeutscheLeitcode;
  212. PrintCheckSum = src.PrintCheckSum;
  213. }
  214. /// <inheritdoc />
  215. public override string GetDefaultValue()
  216. {
  217. return "1234512312312";
  218. }
  219. internal override string GetPattern()
  220. {
  221. string result = "5050"; //Startcode
  222. string c;
  223. string text = base.text.Replace(".", "").Replace(" ", "");
  224. if (CalcCheckSum)
  225. {
  226. if (text.Length == 13)
  227. text = CheckSumModulo10(text);
  228. else if (text.Length != 14)
  229. throw new Exception(Res.Get("Messages,BarcodeLengthMismatch"));
  230. }
  231. else
  232. {
  233. if (text.Length != 14)
  234. throw new Exception(Res.Get("Messages,BarcodeLengthMismatch"));
  235. }
  236. for (int i = 0; i < (text.Length / 2); i++)
  237. {
  238. for (int j = 0; j <= 4; j++)
  239. {
  240. if (tabelle_2_5[CharToInt(text[i * 2]), j] == 1)
  241. c = "6";
  242. else
  243. c = "5";
  244. result += c;
  245. if (tabelle_2_5[CharToInt(text[i * 2 + 1]), j] == 1)
  246. c = "1";
  247. else
  248. c = "0";
  249. result += c;
  250. }
  251. }
  252. result += "605"; // Stopcode
  253. base.text = text
  254. .Insert(5, ".")
  255. .Insert(6, " ")
  256. .Insert(10, ".")
  257. .Insert(11, " ")
  258. .Insert(15, ".")
  259. .Insert(16, " ")
  260. .Insert(19, " ");
  261. return result;
  262. }
  263. public BarcodeDeutscheLeitcode()
  264. {
  265. ratioMin = 2.25F;
  266. ratioMax = 3.5F;
  267. WideBarRatio = 3F;
  268. CalcCheckSum = true;
  269. }
  270. }
  271. /// <summary>
  272. /// Generates the "ITF-14" barcode.
  273. /// </summary>
  274. public class BarcodeITF14 : Barcode2of5Interleaved
  275. {
  276. #region Fields
  277. private bool drawVerticalBearerBars = true;
  278. #endregion
  279. #region Properties
  280. /// <summary>
  281. /// Gets or sets the value indicating that vertical bearer bars are needed to draw.
  282. /// </summary>
  283. [System.ComponentModel.DefaultValue(true)]
  284. [System.ComponentModel.Category("Appearance")]
  285. public bool DrawVerticalBearerBars
  286. {
  287. get
  288. {
  289. return this.drawVerticalBearerBars;
  290. }
  291. set
  292. {
  293. this.drawVerticalBearerBars = value;
  294. }
  295. }
  296. #endregion
  297. #region Internal Methods
  298. internal override string GetPattern()
  299. {
  300. string result = ""; // Startcode
  301. for (int i = 0; i < 14; i++)//10 for light margin and 4 for vertical bearer bar
  302. {
  303. result += "0";
  304. }
  305. result += "5050";
  306. string c;
  307. if (CalcCheckSum)
  308. {
  309. base.text = DoCheckSumming(base.text, 14);
  310. }
  311. else base.text = SetLen(14);
  312. for (int i = 0; i < (base.text.Length / 2); i++)
  313. {
  314. for (int j = 0; j <= 4; j++)
  315. {
  316. if (tabelle_2_5[CharToInt(base.text[i * 2]), j] == 1)
  317. c = "6";
  318. else
  319. c = "5";
  320. result += c;
  321. if (tabelle_2_5[CharToInt(base.text[i * 2 + 1]), j] == 1)
  322. c = "1";
  323. else
  324. c = "0";
  325. result += c;
  326. }
  327. }
  328. result += "605"; //Stopcode
  329. for (int i = 0; i < 14; i++)//10 for light margin and 4 for vertical bearer bar
  330. {
  331. result += "0";
  332. }
  333. return result;
  334. }
  335. internal override void DrawText(IGraphics g, string data)
  336. {
  337. data = StripControlCodes(data);
  338. DrawString(g, 0, drawArea.Width, data.Insert(1, " ").Insert(4, " ").Insert(10, " ").Insert(16, " "));
  339. }
  340. internal override void Serialize(FRWriter writer, string prefix, BarcodeBase diff)
  341. {
  342. base.Serialize(writer, prefix, diff);
  343. BarcodeITF14 c = diff as BarcodeITF14;
  344. if (c == null || DrawVerticalBearerBars != c.DrawVerticalBearerBars)
  345. writer.WriteValue(prefix + "DrawVerticalBearerBars", DrawVerticalBearerBars);
  346. }
  347. #endregion
  348. #region Public Methods
  349. /// <inheritdoc/>
  350. public override void Assign(BarcodeBase source)
  351. {
  352. base.Assign(source);
  353. BarcodeITF14 src = source as BarcodeITF14;
  354. DrawVerticalBearerBars = src.DrawVerticalBearerBars;
  355. }
  356. public override void DrawBarcode(IGraphics g, RectangleF displayRect)
  357. {
  358. base.DrawBarcode(g, displayRect);
  359. float bearerWidth = WideBarRatio * 2 * zoom;
  360. using (Pen pen = new Pen(Color, bearerWidth))
  361. {
  362. float x0 = displayRect.Left;
  363. float x01 = displayRect.Left + bearerWidth / 2;
  364. float y0 = displayRect.Top;
  365. float y01 = displayRect.Top + bearerWidth / 2;
  366. float x1 = displayRect.Left + displayRect.Width;
  367. float x11 = displayRect.Left + displayRect.Width - bearerWidth / 2;
  368. float y1 = displayRect.Top + barArea.Bottom * zoom;
  369. float y11 = displayRect.Top + barArea.Bottom * zoom - bearerWidth / 2;
  370. g.DrawLine(pen, x0, y01 - 0.5F, x1, y01 - 0.5F);
  371. g.DrawLine(pen, x0, y11, x1, y11);
  372. if (this.drawVerticalBearerBars)
  373. {
  374. g.DrawLine(pen, x01 - 0.5F, y0, x01 - 0.5F, y1);
  375. g.DrawLine(pen, x11, y0, x11, y1);
  376. }
  377. }
  378. }
  379. #endregion
  380. /// <summary>
  381. /// Initializes a new instance of the <see cref="BarcodeITF14"/> class with default settings.
  382. /// </summary>
  383. public BarcodeITF14()
  384. {
  385. ratioMin = 2.25F;
  386. ratioMax = 3.0F;
  387. WideBarRatio = 2.25F;
  388. }
  389. }
  390. /// <summary>
  391. /// Generates the "2/5 Industrial" barcode.
  392. /// </summary>
  393. public class Barcode2of5Industrial : Barcode2of5Interleaved
  394. {
  395. internal override string GetPattern()
  396. {
  397. string text = base.text;
  398. string result = "606050"; // Startcode
  399. if (CalcCheckSum)
  400. {
  401. text = DoCheckSumming(text);
  402. }
  403. for (int i = 0; i < text.Length; i++)
  404. {
  405. for (int j = 0; j <= 4; j++)
  406. {
  407. if (tabelle_2_5[CharToInt(text[i]), j] == 1)
  408. result += "60";
  409. else
  410. result += "50";
  411. }
  412. }
  413. result += "605060"; //Stopcode
  414. return result;
  415. }
  416. }
  417. /// <summary>
  418. /// Generates the "2/5 Matrix" barcode.
  419. /// </summary>
  420. public class Barcode2of5Matrix : Barcode2of5Interleaved
  421. {
  422. internal override string GetPattern()
  423. {
  424. string text = base.text;
  425. string result = "705050"; // Startcode
  426. char c;
  427. if (CalcCheckSum)
  428. {
  429. text = DoCheckSumming(text);
  430. }
  431. for (int i = 0; i < text.Length; i++)
  432. {
  433. for (int j = 0; j <= 4; j++)
  434. {
  435. if (tabelle_2_5[CharToInt(text[i]), j] == 1)
  436. c = '1';
  437. else
  438. c = '0';
  439. if ((j % 2) == 0)
  440. c = (char)((int)c + 5);
  441. result += c;
  442. }
  443. result += '0';
  444. }
  445. result = result + "70505"; // Stopcode
  446. return result;
  447. }
  448. /// <summary>
  449. /// Initializes a new instance of the <see cref="Barcode2of5Matrix"/> class with default settings.
  450. /// </summary>
  451. public Barcode2of5Matrix()
  452. {
  453. ratioMin = 2.25f;
  454. ratioMax = 3;
  455. WideBarRatio = 2.25f;
  456. }
  457. }
  458. }