LinearBarcodeBase.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. using System.Drawing.Drawing2D;
  8. namespace FastReport.Barcode
  9. {
  10. internal enum BarLineType
  11. {
  12. White,
  13. Black,
  14. // line with 2/5 height (used for PostNet)
  15. BlackHalf,
  16. // start/stop/middle lines in EAN, UPC codes
  17. BlackLong,
  18. // for Intelligent Mail Barcode
  19. // see https://upload.wikimedia.org/wikipedia/commons/7/7a/Four_State_Barcode.svg
  20. BlackTracker,
  21. BlackAscender,
  22. BlackDescender,
  23. }
  24. /// <summary>
  25. /// The base class for linear (1D) barcodes.
  26. /// </summary>
  27. public class LinearBarcodeBase : BarcodeBase
  28. {
  29. #region Fields
  30. private float wideBarRatio;
  31. private float[] modules;
  32. private bool calcCheckSum;
  33. private bool trim;
  34. internal RectangleF drawArea;
  35. internal RectangleF barArea;
  36. internal static Font FFont = new Font("Arial", 8);
  37. internal static Font FSmallFont = new Font("Arial", 6);
  38. internal bool textUp;
  39. internal float ratioMin;
  40. internal float ratioMax;
  41. internal float extra1;
  42. internal float extra2;
  43. internal string pattern;
  44. #endregion
  45. #region Properties
  46. /// <summary>
  47. /// Gets or sets a value that determines if the barcode object should calculate
  48. /// the check digit automatically.
  49. /// </summary>
  50. [DefaultValue(true)]
  51. public bool CalcCheckSum
  52. {
  53. get { return calcCheckSum; }
  54. set { calcCheckSum = value; }
  55. }
  56. /// <summary>
  57. /// Gets or sets a relative width of wide bars in the barcode.
  58. /// </summary>
  59. [DefaultValue(2f)]
  60. public float WideBarRatio
  61. {
  62. get { return wideBarRatio; }
  63. set
  64. {
  65. wideBarRatio = value;
  66. if (ratioMin != 0 && wideBarRatio < ratioMin)
  67. wideBarRatio = ratioMin;
  68. if (ratioMax != 0 && wideBarRatio > ratioMax)
  69. wideBarRatio = ratioMax;
  70. }
  71. }
  72. /// <summary>
  73. /// Gets the value indicating that the barcode is numeric.
  74. /// </summary>
  75. [Browsable(false)]
  76. public virtual bool IsNumeric
  77. {
  78. get { return true; }
  79. }
  80. /// <summary>
  81. /// Gets or sets a value indicating that leading/trailing whitespaces must be trimmed.
  82. /// </summary>
  83. /// <value>
  84. /// <c>true</c> if trim; otherwise, <c>false</c>.
  85. /// </value>
  86. [DefaultValue(true)]
  87. public bool Trim
  88. {
  89. get { return trim; }
  90. set { trim = value; }
  91. }
  92. private string Code
  93. {
  94. get
  95. {
  96. MakeModules();
  97. pattern = GetPattern();
  98. if(pattern == null)
  99. {
  100. MyRes res = new MyRes("Messages");
  101. throw new FormatException(res.Get("BarcodeManyError"));
  102. }
  103. return pattern;
  104. }
  105. }
  106. #endregion
  107. #region Private Methods
  108. private void CheckText(string text)
  109. {
  110. foreach (char i in text)
  111. {
  112. if (i < '0' || i > '9')
  113. throw new Exception(Res.Get("Messages,InvalidBarcode2"));
  114. }
  115. }
  116. private void MakeModules()
  117. {
  118. modules[0] = 1;
  119. modules[1] = modules[0] * WideBarRatio;
  120. modules[2] = modules[1] * 1.5f;
  121. modules[3] = modules[1] * 2;
  122. }
  123. internal virtual void DoLines(string data, IGraphics g, float zoom)
  124. {
  125. using (Pen pen = new Pen(Color))
  126. {
  127. float currentWidth = 0;
  128. foreach (char c in data)
  129. {
  130. float width;
  131. BarLineType lt;
  132. OneBarProps(c, out width, out lt);
  133. float heightStart = 0;
  134. float heightEnd = barArea.Height;
  135. if (lt == BarLineType.BlackHalf)
  136. {
  137. heightEnd = barArea.Height * 2 / 5;
  138. }
  139. else if (lt == BarLineType.BlackLong && showText)
  140. {
  141. heightEnd += 7;
  142. }
  143. else if (lt == BarLineType.BlackTracker)
  144. {
  145. heightStart = barArea.Height * 1 / 3;
  146. heightEnd = barArea.Height * 2 / 3;
  147. }
  148. else if (lt == BarLineType.BlackAscender)
  149. {
  150. heightEnd = barArea.Height * 2 / 3;
  151. }
  152. else if (lt == BarLineType.BlackDescender)
  153. {
  154. heightStart = barArea.Height * 1 / 3;
  155. }
  156. width *= zoom;
  157. heightStart *= zoom;
  158. heightEnd *= zoom;
  159. pen.Width = width;
  160. if (lt == BarLineType.BlackHalf)
  161. {
  162. g.DrawLine(pen,
  163. currentWidth + width / 2,
  164. barArea.Bottom * zoom,
  165. currentWidth + width / 2,
  166. barArea.Bottom * zoom - heightEnd);
  167. }
  168. else if (lt != BarLineType.White)
  169. {
  170. g.DrawLine(pen,
  171. currentWidth + width / 2,
  172. barArea.Top * zoom + heightStart,
  173. currentWidth + width / 2,
  174. barArea.Top * zoom + heightEnd);
  175. }
  176. currentWidth += width;
  177. }
  178. }
  179. }
  180. public string CheckSumModulo10(string data)
  181. {
  182. int sum = 0;
  183. int fak = data.Length;
  184. for (int i = 0; i < data.Length; i++)
  185. {
  186. if ((fak % 2) == 0)
  187. sum += int.Parse(data[i].ToString());
  188. else
  189. sum += int.Parse(data[i].ToString()) * 3;
  190. fak--;
  191. }
  192. if ((sum % 10) == 0)
  193. return data + "0";
  194. else
  195. return data + (10 - (sum % 10)).ToString();
  196. }
  197. private void OneBarProps(char code, out float width, out BarLineType lt)
  198. {
  199. switch (code)
  200. {
  201. case '0':
  202. width = modules[0];
  203. lt = BarLineType.White;
  204. break;
  205. case '1':
  206. width = modules[1];
  207. lt = BarLineType.White;
  208. break;
  209. case '2':
  210. width = modules[2];
  211. lt = BarLineType.White;
  212. break;
  213. case '3':
  214. width = modules[3];
  215. lt = BarLineType.White;
  216. break;
  217. case '5':
  218. width = modules[0];
  219. lt = BarLineType.Black;
  220. break;
  221. case '6':
  222. width = modules[1];
  223. lt = BarLineType.Black;
  224. break;
  225. case '7':
  226. width = modules[2];
  227. lt = BarLineType.Black;
  228. break;
  229. case '8':
  230. width = modules[3];
  231. lt = BarLineType.Black;
  232. break;
  233. case '9':
  234. width = modules[0];
  235. lt = BarLineType.BlackHalf;
  236. break;
  237. case 'A':
  238. width = modules[0];
  239. lt = BarLineType.BlackLong;
  240. break;
  241. case 'B':
  242. width = modules[1];
  243. lt = BarLineType.BlackLong;
  244. break;
  245. case 'C':
  246. width = modules[2];
  247. lt = BarLineType.BlackLong;
  248. break;
  249. case 'D':
  250. width = modules[3];
  251. lt = BarLineType.BlackLong;
  252. break;
  253. // E,F,G for Intelligent Mail Barcode
  254. case 'E':
  255. width = modules[1];
  256. lt = BarLineType.BlackTracker;
  257. break;
  258. case 'F':
  259. width = modules[1];
  260. lt = BarLineType.BlackAscender;
  261. break;
  262. case 'G':
  263. width = modules[1];
  264. lt = BarLineType.BlackDescender;
  265. break;
  266. default:
  267. // something went wrong :-(
  268. // mistyped pattern table
  269. throw new Exception("Incorrect barcode pattern code: " + code);
  270. }
  271. }
  272. #endregion
  273. #region Protected Methods
  274. internal int CharToInt(char c)
  275. {
  276. return int.Parse(Convert.ToString(c));
  277. }
  278. internal string SetLen(int len)
  279. {
  280. string result = "";
  281. for (int i = 0; i < len - text.Length; i++)
  282. {
  283. result = "0" + result;
  284. }
  285. result += text;
  286. return result.Substring(0, len);
  287. }
  288. internal string DoCheckSumming(string data)
  289. {
  290. return CheckSumModulo10(data);
  291. }
  292. internal string DoCheckSumming(string data, int len)
  293. {
  294. if (CalcCheckSum)
  295. return DoCheckSumming(SetLen(len - 1));
  296. return SetLen(len);
  297. }
  298. internal string DoConvert(string s)
  299. {
  300. StringBuilder builder = new StringBuilder(s);
  301. for (int i = 0; i < s.Length; i++)
  302. {
  303. int v = s[i] - 1;
  304. if ((i % 2) == 0)
  305. v += 5;
  306. builder[i] = (char)v;
  307. }
  308. return builder.ToString();
  309. }
  310. internal string MakeLong(string data)
  311. {
  312. StringBuilder builder = new StringBuilder(data);
  313. for (int i = 0; i < data.Length; i++)
  314. {
  315. char c = builder[i];
  316. if (c >= '5' && c <= '8')
  317. c = (char)((int)c - (int)'5' + (int)'A');
  318. builder[i] = c;
  319. }
  320. return builder.ToString();
  321. }
  322. internal virtual float GetWidth(string code)
  323. {
  324. float result = 0;
  325. float w;
  326. BarLineType lt;
  327. foreach (char c in code)
  328. {
  329. OneBarProps(c, out w, out lt);
  330. result += w;
  331. }
  332. return result;
  333. }
  334. internal virtual string GetPattern()
  335. {
  336. return "";
  337. }
  338. #endregion
  339. #region Public Methods
  340. /// <inheritdoc/>
  341. public override void Assign(BarcodeBase source)
  342. {
  343. base.Assign(source);
  344. LinearBarcodeBase src = source as LinearBarcodeBase;
  345. WideBarRatio = src.WideBarRatio;
  346. CalcCheckSum = src.CalcCheckSum;
  347. Trim = src.Trim;
  348. }
  349. internal override void Serialize(FRWriter writer, string prefix, BarcodeBase diff)
  350. {
  351. base.Serialize(writer, prefix, diff);
  352. LinearBarcodeBase c = diff as LinearBarcodeBase;
  353. if (c == null || WideBarRatio != c.WideBarRatio)
  354. writer.WriteValue(prefix + "WideBarRatio", WideBarRatio);
  355. if (c == null || CalcCheckSum != c.CalcCheckSum)
  356. writer.WriteBool(prefix + "CalcCheckSum", CalcCheckSum);
  357. if (c == null || Trim != c.Trim)
  358. writer.WriteBool(prefix + "Trim", Trim);
  359. }
  360. internal override void Initialize(string text, bool showText, int angle, float zoom)
  361. {
  362. if(trim)
  363. text = text.Trim();
  364. base.Initialize(text, showText, angle, zoom);
  365. }
  366. internal override SizeF CalcBounds()
  367. {
  368. float barWidth = GetWidth(Code);
  369. float extra1 = 0;
  370. float extra2 = 0;
  371. if (showText)
  372. {
  373. float txtWidth = 0;
  374. using (Bitmap bmp = new Bitmap(1, 1))
  375. {
  376. bmp.SetResolution(96, 96);
  377. using (Graphics g = Graphics.FromImage(bmp))
  378. {
  379. txtWidth = g.MeasureString(text, FFont, 100000).Width;
  380. }
  381. }
  382. if (barWidth < txtWidth)
  383. {
  384. extra1 = (txtWidth - barWidth) / 2 + 2;
  385. extra2 = extra1;
  386. }
  387. }
  388. if (this.extra1 != 0)
  389. extra1 = this.extra1;
  390. if (this.extra2 != 0)
  391. extra2 = this.extra2;
  392. drawArea = new RectangleF(0, 0, barWidth + extra1 + extra2, 0);
  393. barArea = new RectangleF(extra1, 0, barWidth, 0);
  394. return new SizeF(drawArea.Width * 1.25f, 0);
  395. }
  396. public override void DrawBarcode(IGraphics g, RectangleF displayRect)
  397. {
  398. float originalWidth = CalcBounds().Width / 1.25f;
  399. float width = angle == 90 || angle == 270 ? displayRect.Height : displayRect.Width;
  400. float height = angle == 90 || angle == 270 ? displayRect.Width : displayRect.Height;
  401. zoom = width / originalWidth;
  402. barArea.Height = height / zoom;
  403. if (showText)
  404. {
  405. barArea.Height -= 14;
  406. if (textUp)
  407. barArea.Y = 14;
  408. }
  409. drawArea.Height = height / zoom;
  410. IGraphicsState state = g.Save();
  411. try
  412. {
  413. // rotate
  414. g.TranslateTransform(displayRect.Left, displayRect.Top);
  415. g.RotateTransform(angle);
  416. switch (angle)
  417. {
  418. case 90:
  419. g.TranslateTransform(0, -displayRect.Width);
  420. break;
  421. case 180:
  422. g.TranslateTransform(-displayRect.Width, -displayRect.Height);
  423. break;
  424. case 270:
  425. g.TranslateTransform(-displayRect.Height, 0);
  426. break;
  427. }
  428. g.TranslateTransform(barArea.Left * zoom, 0);
  429. DoLines(pattern, g, zoom);
  430. if (showText)
  431. DrawText(g, text);
  432. }
  433. finally
  434. {
  435. g.Restore(state);
  436. }
  437. }
  438. internal void DrawString(IGraphics g, float x1, float x2, string s)
  439. {
  440. DrawString(g, x1, x2, s, false);
  441. }
  442. internal void DrawString(IGraphics g, float x1, float x2, string s, bool small)
  443. {
  444. if (String.IsNullOrEmpty(s))
  445. return;
  446. // when we print, .Net automatically scales the font. However, we need to handle this process.
  447. // Downscale the font to the screen resolution, then scale by required value (Zoom).
  448. float fontZoom = 14f / (int)g.MeasureString(s, FFont).Height * zoom;
  449. Font font = small ? FSmallFont : FFont;
  450. using (Font drawFont = new Font(font.FontFamily, font.Size * fontZoom, font.Style))
  451. {
  452. SizeF size = g.MeasureString(s, drawFont);
  453. size.Width /= zoom;
  454. size.Height /= zoom;
  455. g.DrawString(s, drawFont, new SolidBrush(Color),
  456. (x1 + (x2 - x1 - size.Width) / 2) * zoom,
  457. (textUp ? 0 : drawArea.Height - size.Height) * zoom);
  458. }
  459. }
  460. internal virtual void DrawText(IGraphics g, string data)
  461. {
  462. data = StripControlCodes(data);
  463. DrawString(g, 0, drawArea.Width, data);
  464. }
  465. #endregion
  466. /// <summary>
  467. /// Initializes a new instance of the <see cref="LinearBarcodeBase"/> class with default settings.
  468. /// </summary>
  469. public LinearBarcodeBase()
  470. {
  471. modules = new float[4];
  472. WideBarRatio = 2;
  473. calcCheckSum = true;
  474. trim = true;
  475. }
  476. }
  477. }