LinearBarcodeBase.cs 17 KB

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