fpreportbarcode.pp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. {
  2. This file is part of the Free Component Library.
  3. Copyright (c) 2017 Michael Van Canneyt, member of the Free Pascal development team
  4. Barcode report element.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit fpreportbarcode;
  12. {$mode objfpc}
  13. {$H+}
  14. interface
  15. uses
  16. Classes, fpimage, fpexprpars, fpimgbarcode, fpbarcode, fpreport, fpreportstreamer;
  17. Type
  18. { TFPReportBarcode }
  19. TFPReportBarcode = Class(TFPReportElement)
  20. private
  21. FEncoding: TBarcodeEncoding;
  22. FExpression: String;
  23. FPadLength: Integer;
  24. FUnitWidth: Integer;
  25. FValue: String;
  26. FExprValue : String;
  27. FWeight: Double;
  28. Protected
  29. procedure BeforePrint; override;
  30. procedure RecalcLayout; override;
  31. Procedure DoWriteLocalProperties(AWriter: TFPReportStreamer; AOriginal: TFPReportElement=nil); override;
  32. Public
  33. procedure Assign(Source: TPersistent); override;
  34. Constructor Create(AOwner: TComponent); override;
  35. Class Function ElementType : String; override;
  36. // Will calculate the value to display. Either Value or evaluated expression.
  37. Function BarcodeValue : String;
  38. Procedure ReadElement(AReader: TFPReportStreamer); override;
  39. Published
  40. Property Encoding : TBarcodeEncoding Read FEncoding Write FEncoding;
  41. Property UnitWidth : Integer Read FUnitWidth Write FUnitWidth;
  42. Property Weight : Double Read FWeight Write FWeight;
  43. Property PadLength : Integer Read FPadLength Write FPadLength;
  44. // Expression takes precedence
  45. Property Value : String Read FValue Write FValue;
  46. Property Expression : String Read FExpression Write FExpression;
  47. end;
  48. Procedure RegisterReportBarcode;
  49. Procedure UnRegisterReportBarcode;
  50. implementation
  51. uses typinfo, strutils;
  52. { TFPReportBarcode }
  53. procedure TFPReportBarcode.RecalcLayout;
  54. begin
  55. // Do nothing for the moment.
  56. // We may consider adding a Boolean property FitWidth and calculating width based on value/expression when it is set to true
  57. end;
  58. procedure TFPReportBarcode.DoWriteLocalProperties(AWriter: TFPReportStreamer; AOriginal: TFPReportElement);
  59. begin
  60. inherited DoWriteLocalProperties(AWriter, AOriginal);
  61. AWriter.WriteString('Encoding',GetEnumName(TypeInfo(TBarcodeEncoding),Ord(FEncoding)));
  62. AWriter.WriteInteger('UnitWidth',UnitWidth);
  63. AWriter.WriteInteger('PadLength',PadLength);
  64. AWriter.WriteFloat('Weight',Weight);
  65. AWriter.WriteString('Value',Value);
  66. AWriter.WriteString('Expression',Expression);
  67. end;
  68. procedure TFPReportBarcode.Assign(Source: TPersistent);
  69. Var
  70. BC : TFPReportBarcode;
  71. begin
  72. if (Source is TFPReportBarcode) then
  73. begin
  74. BC:=TFPReportBarcode(Source);
  75. FValue:=BC.Value;
  76. FPadlength:=BC.PadLength;
  77. FExpression:=BC.Expression;
  78. FWeight:=BC.Weight;
  79. FUnitWidth:=BC.UnitWidth;
  80. FEncoding:=BC.Encoding;
  81. end;
  82. inherited Assign(Source);
  83. end;
  84. constructor TFPReportBarcode.Create(AOwner: TComponent);
  85. begin
  86. inherited Create(AOwner);
  87. FEncoding:=be128A;
  88. FUnitWidth:=1;
  89. FWeight:=2.0;
  90. end;
  91. class function TFPReportBarcode.ElementType: String;
  92. begin
  93. Result:='BarCode';
  94. end;
  95. procedure TFPReportBarcode.BeforePrint;
  96. begin
  97. Inherited;
  98. if (FExpression<>'') then
  99. FExprValue:=EvaluateExpressionAsText(FExpression)
  100. end;
  101. function TFPReportBarcode.BarcodeValue: String;
  102. begin
  103. if (FExpression<>'') then
  104. Result:=FExprValue // Calculated in beforeprint
  105. else
  106. Result:=FValue;
  107. Result:=AddChar('0',Result,PadLength);
  108. end;
  109. procedure TFPReportBarcode.ReadElement(AReader: TFPReportStreamer);
  110. Var
  111. S : String;
  112. I : Integer;
  113. begin
  114. inherited ReadElement(AReader);
  115. S:=AReader.ReadString('Encoding','beEan8');
  116. I:=GetEnumValue(TypeInfo(TBarcodeEncoding),S);
  117. if I<>-1 then
  118. FEncoding:=TBarcodeEncoding(I);
  119. UnitWidth:=AReader.ReadInteger('UnitWidth',UnitWidth);
  120. PadLength:=AReader.ReadInteger('UnitWidth',PadLength);
  121. Weight:=AReader.ReadFloat('Weight',Weight);
  122. Value:=AReader.ReadString('Value',Value);
  123. Expression:=AReader.ReadString('Expression',Expression);
  124. end;
  125. procedure RenderBarcode(aElement: TFPReportElement; aImage: TFPCustomImage);
  126. Var
  127. D : TFPDrawBarcode;
  128. B : TFPReportBarcode;
  129. begin
  130. B:=TFPReportBarcode(aElement);
  131. D:=TFPDrawBarcode.Create;
  132. try
  133. D.Image:=aImage;
  134. D.Weight:=B.Weight;
  135. D.UnitWidth:=B.UnitWidth;
  136. D.Rect:=Rect(0,0,aImage.Width-1,aImage.Height-1);
  137. D.Text:=B.BarcodeValue;
  138. // Writeln('Weight: ',D.Weight,' unitwidth:',D.UnitWidth,' ',aImage.Width-1,'x',aImage.Height-1,' Text: ',D.Text);
  139. D.Encoding:=B.Encoding;
  140. D.Clipping:=True;
  141. D.Draw;
  142. finally
  143. D.Free;
  144. end;
  145. end;
  146. Procedure RegisterReportBarcode;
  147. Const
  148. icon : Array[0..687] of byte = (
  149. 137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0,
  150. 0, 0, 16, 0, 0, 0, 16, 8, 6, 0, 0, 0, 31,243,255, 97, 0,
  151. 0, 2,119, 73, 68, 65, 84,120,156,141,147,189, 74, 92, 81, 20,133,
  152. 191,243,115,255,207,140,137,131, 78, 52, 97, 16, 11, 13, 72, 82, 72,
  153. 8, 24, 27, 11,155,188,131,149,181,146,202,135, 8, 41,132,188,129,
  154. 32,216, 8,118,226, 3,164, 21,196, 46, 54,153, 66, 13, 99, 38,113,
  155. 254,238,157,153,123,199,123, 82, 56,115, 73, 37, 89,176,224,176, 89,
  156. 103,237,195,222,103, 9,198, 56, 56, 56, 88, 95, 92, 92,124, 45,165,
  157. 20, 60, 1,107,173,173,215,235,223,183,182,182,190, 21,197,163,163,
  158. 163,207, 73,146,228, 73,146,216,255,100,126,120,120,248, 5, 64,108,
  159. 111,111,191, 92, 93, 93,173,135, 97,168,141, 49, 52, 26, 13,142,143,
  160. 143, 57, 61, 61,101,125,125,157, 82,169,196,206,206, 14,215,215,215,
  161. 156,156,156,176,187,187,203,205,205, 13,211,211,211, 15,151,151,151,
  162. 11,186, 82,169, 84,227, 56,214,214, 90,164,148,116,187, 93,126,253,
  163. 106, 2,112,119,119,199,112, 56, 36, 73, 18,226, 56,166,217,108,210,
  164. 235,245,136,227, 24,223,247, 85,181, 90,125, 33,133, 16, 76, 40,165,
  165. 68,107,141,235,186, 40,165,240, 60, 15,207,243, 80, 74,161,181,198,
  166. 113,156, 66, 7, 32,165, 20, 18, 40,138, 82,170,177,129, 83, 24,185,
  167. 174,139,214,186, 48,208, 90, 35,165, 66, 74,137,181, 22, 41, 30,157,
  168. 198, 93, 84,209, 77, 8, 81, 92, 82,106, 82,159,156,229,248, 21, 22,
  169. 141, 16,204,164, 41,230,247,111, 76,154,162, 90, 45,222,102, 25,226,
  170. 231, 79,222,141, 70,148, 71, 35,102, 91, 45,242, 78,135, 55, 89, 74,
  171. 165,217,100,116,127,143,137, 34,218, 8,180, 16,130, 56,138, 40, 55,
  172. 155, 68, 74, 97, 7, 3, 94,229, 57,162,223,103,193, 90, 76,158, 83,
  173. 74, 83,134, 89,198,124,158, 19,245,251,136, 52, 37, 14, 67, 72,146,
  174. 71, 3,171, 53,237, 90,141, 40,203,200,178,140,196,243,160, 82,161,
  175. 231,121, 8,207, 99, 84, 46,147,117,187,244, 93,151,254,212, 51,254,
  176. 184, 46,211, 74, 33,192, 74, 38, 51,240,125,226,106,149, 81,185, 76,
  177. 26,134, 48, 53,197, 32, 8, 24,134, 33,185, 49, 60, 24, 67, 28, 69,
  178. 244,102,103,208,227, 45, 33, 4, 18, 40,134,164, 28,135,158, 49, 12,
  179. 130, 0,164,100, 16, 4,164, 97,200,131,235, 50,116, 93, 26,229, 50,
  180. 74,235, 66,111,173, 69,118, 58,157,252,113,133,143,204,181,166, 21,
  181. 69, 0,164, 65, 64, 26, 4,228,142, 67,223,247, 17,142,195,191,218,
  182. 118,187,253,160, 47, 46, 46,126,172,172,172, 52,150,151,151,171, 65,
  183. 16, 96,140,225,253,135, 15, 0,172,110,108, 96, 74, 37,158,207,207,
  184. 99,125,159,181,181, 53,230,230,230,240, 60,143,219,219,219,198,249,
  185. 249,249, 15, 1,176,185,185,249,113,111,111,239,235,210,210,210,252,
  186. 83, 73,156,224,234,234,234,118,127,127,255,211,217,217,217,105, 17,
  187. 221, 90,173, 22,229,121,254,124,242, 77,159,136, 51, 74,169,251,122,
  188. 189, 30, 3,252, 5,131, 89, 18, 55,201,190,160,207, 0, 0, 0, 0,
  189. 73, 69, 78, 68,174, 66, 96,130);
  190. begin
  191. TFPReportBarcode.RegisterElement.SetIconFromBytes(Icon);
  192. // Fallback renderer
  193. gElementFactory.RegisterImageRenderer(TFPReportBarcode,@RenderBarcode);
  194. end;
  195. Procedure UnRegisterReportBarcode;
  196. begin
  197. TFPReportBarcode.UnRegisterElement;
  198. end;
  199. initialization
  200. RegisterReportBarcode;
  201. end.