Barcode2DBase.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using FastReport.Utils;
  7. namespace FastReport.Barcode
  8. {
  9. /// <summary>
  10. /// The base class for 2D-barcodes such as PDF417 and Datamatrix.
  11. /// </summary>
  12. public abstract class Barcode2DBase : BarcodeBase
  13. {
  14. private static Font FFont = new Font("Arial", 8);
  15. private void DrawBarcode(IGraphics g, float width, float height)
  16. {
  17. SizeF originalSize = CalcBounds();
  18. float kx = width / originalSize.Width;
  19. float ky = height / originalSize.Height;
  20. Draw2DBarcode(g, kx, ky);
  21. //If swiss qr, draw the swiss cross
  22. if (text.StartsWith("SPC"))
  23. {
  24. float top = showText ? height - 21 : height;
  25. g.FillRectangle(Brushes.White, width / 2 - width / 100f * 7, top / 2 - top / 100 * 7, width / 100f * 14, top / 100 * 14);
  26. g.FillRectangle(Brushes.Black, width / 2 - width / 100f * 6, top / 2 - top / 100 * 6, width / 100f * 12, top / 100 * 12);
  27. g.FillRectangle(Brushes.White, width / 2 - width / 100f * 4, top / 2 - top / 100 * 1.5f, width / 100f * 8, top / 100 * 3);
  28. g.FillRectangle(Brushes.White, width / 2 - width / 100f * 1.5f, top / 2 - top / 100 * 4, width / 100f * 3, top / 100 * 8);
  29. }
  30. if (text.StartsWith("ST"))
  31. {
  32. Pen skyBluePen = new Pen(Brushes.Black);
  33. skyBluePen.Width = (kx * 4 * zoom) / 2;
  34. g.DrawLine(skyBluePen, width - 2, height / 2, width - 2, height - 2);
  35. g.DrawLine(skyBluePen, width / 2, height - 2, width - 2, height - 2);
  36. }
  37. // draw the text.
  38. if (showText)
  39. {
  40. string data = StripControlCodes(text);
  41. if (data.Length > 0)
  42. {
  43. // When we print, .Net automatically scales the font. However, we need to handle this process.
  44. // Downscale the font to the screen resolution, then scale by required value (ky).
  45. float fontZoom = 18f / (int)g.MeasureString(data, FFont).Height * ky;
  46. using (Font drawFont = new Font(FFont.FontFamily, FFont.Size * fontZoom, FFont.Style))
  47. {
  48. g.DrawString(data, drawFont, Brushes.Black, new RectangleF(0, height - 18 * ky, width, 18 * ky));
  49. }
  50. }
  51. }
  52. }
  53. internal virtual void Draw2DBarcode(IGraphics g, float kx, float ky)
  54. {
  55. }
  56. public override void DrawBarcode(IGraphics g, RectangleF displayRect)
  57. {
  58. float width = angle == 90 || angle == 270 ? displayRect.Height : displayRect.Width;
  59. float height = angle == 90 || angle == 270 ? displayRect.Width : displayRect.Height;
  60. IGraphicsState state = g.Save();
  61. try
  62. {
  63. // rotate
  64. g.TranslateTransform(displayRect.Left, displayRect.Top);
  65. g.RotateTransform(angle);
  66. switch (angle)
  67. {
  68. case 90:
  69. g.TranslateTransform(0, -displayRect.Width);
  70. break;
  71. case 180:
  72. g.TranslateTransform(-displayRect.Width, -displayRect.Height);
  73. break;
  74. case 270:
  75. g.TranslateTransform(-displayRect.Height, 0);
  76. break;
  77. }
  78. DrawBarcode(g, width, height);
  79. }
  80. finally
  81. {
  82. g.Restore(state);
  83. }
  84. }
  85. }
  86. }