MultiSignatureObject.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. using FastReport;
  2. using FastReport.DevComponents.DotNetBar;
  3. using FastReport.Utils;
  4. using InABox.Core;
  5. using InABox.Reports.CustomObjects;
  6. using netDxf;
  7. using Syncfusion.Windows.PdfViewer;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Drawing;
  12. using System.Drawing.Design;
  13. using System.Linq;
  14. using System.Text;
  15. using System.Threading.Tasks;
  16. namespace InABox.Reports
  17. {
  18. public class MultiSignatureObject : MultiItemObject
  19. {
  20. private FillBase textFill;
  21. private IDictionary<string, byte[]>? signatureData { get; set; }
  22. [Category("Data")]
  23. public string? DataColumn { get; set; }
  24. [Category("Data")]
  25. public Dictionary<string, Image>? Signatures { get; set; }
  26. [Category("Layout")]
  27. public float LabelHeight { get; set; }
  28. [Category("Appearance")]
  29. [Editor("FastReport.TypeEditors.FillEditor, FastReport", typeof(UITypeEditor))]
  30. public FillBase TextFill
  31. {
  32. get { return textFill; }
  33. set
  34. {
  35. if (value == null)
  36. throw new ArgumentNullException("TextFill");
  37. textFill = value;
  38. if (!String.IsNullOrEmpty(Style))
  39. Style = "";
  40. }
  41. }
  42. [Category("Appearance")]
  43. public Font Font { get; set; }
  44. public MultiSignatureObject()
  45. {
  46. LabelHeight = 20;
  47. textFill = new SolidFill(Color.Black);
  48. Font = DrawUtils.DefaultReportFont;
  49. }
  50. public override void GetData()
  51. {
  52. base.GetData();
  53. if (!string.IsNullOrEmpty(DataColumn))
  54. {
  55. signatureData = null;
  56. Signatures = null;
  57. object data = Report.GetColumnValueNullable(DataColumn);
  58. if (data is IDictionary<string, byte[]> dict)
  59. {
  60. SetSignatureData(dict);
  61. }
  62. }
  63. }
  64. private void SetSignatureData(IDictionary<string, byte[]> signatureData)
  65. {
  66. this.signatureData = signatureData;
  67. }
  68. private class SignatureItem : Item
  69. {
  70. public string Name;
  71. public Image Image;
  72. public float Width => Image.Width;
  73. public float Height { get; }
  74. public SignatureItem(string name, Image image, float labelHeight)
  75. {
  76. Name = name;
  77. Image = image;
  78. Height = image.Height + labelHeight;
  79. }
  80. }
  81. #region Serialization
  82. public override void Serialize(FRWriter writer)
  83. {
  84. base.Serialize(writer);
  85. MultiSignatureObject c = writer.DiffObject as MultiSignatureObject;
  86. if (DataColumn != c.DataColumn)
  87. writer.WriteValue("DataColumn", DataColumn);
  88. if ((writer.SerializeTo != SerializeTo.Preview || Font != c.Font) && writer.ItemName != "inherited")
  89. writer.WriteValue("Font", Font);
  90. if(TextFill != c.TextFill)
  91. TextFill.Serialize(writer, "TextFill", c.TextFill);
  92. if (writer.SerializeTo != SerializeTo.SourcePages)
  93. {
  94. if (writer.SerializeTo == SerializeTo.Preview || String.IsNullOrEmpty(DataColumn))
  95. {
  96. if (Signatures == null && signatureData != null)
  97. writer.WriteStr("Images", Serialization.Serialize(signatureData));
  98. else if (!writer.AreEqual(Signatures, c.Signatures))
  99. writer.WriteValue("Images", Signatures);
  100. }
  101. }
  102. }
  103. public override void Deserialize(FRReader reader)
  104. {
  105. base.Deserialize(reader);
  106. if (reader.HasProperty("TextFill"))
  107. {
  108. TextFill.Deserialize(reader, "TextFill");
  109. }
  110. if (reader.HasProperty("Images"))
  111. {
  112. SetSignatureData(Serialization.Deserialize<Dictionary<string, byte[]>>(reader.ReadStr("Images")));
  113. }
  114. switch (reader.DeserializeFrom)
  115. {
  116. case SerializeTo.Undo:
  117. case SerializeTo.Preview:
  118. case SerializeTo.Clipboard:
  119. // skip
  120. break;
  121. default:
  122. if (!reader.HasProperty("Font") && reader.ItemName != "inherited")
  123. {
  124. string creatorVersion = reader.Root.GetProp("ReportInfo.CreatorVersion");
  125. if (!String.IsNullOrEmpty(creatorVersion))
  126. {
  127. try
  128. {
  129. string[] versions = creatorVersion.Split('.');
  130. int major = 0;
  131. if (Int32.TryParse(versions[0], out major))
  132. {
  133. if (major < 2016)
  134. {
  135. Font = new Font("Arial", 10);
  136. }
  137. }
  138. }
  139. catch
  140. {
  141. }
  142. }
  143. }
  144. break;
  145. }
  146. }
  147. #endregion
  148. public override void Assign(Base source)
  149. {
  150. base.Assign(source);
  151. if (source is MultiSignatureObject src)
  152. {
  153. DataColumn = src.DataColumn;
  154. TextFill = src.TextFill;
  155. Font = src.Font;
  156. Signatures = src.Signatures == null ? null : src.Signatures.ToDictionary(x => x.Key, x => x.Value.Clone() as Image);
  157. if (src.Signatures == null && src.signatureData != null)
  158. {
  159. signatureData = src.signatureData;
  160. }
  161. }
  162. }
  163. public static Image? Load(byte[] bytes)
  164. {
  165. if (bytes != null && bytes.Length > 0)
  166. {
  167. try
  168. {
  169. #if CROSSPLATFORM
  170. // TODO memory leaks image converter
  171. return Image.FromStream(new MemoryStream(bytes));
  172. #else
  173. return new ImageConverter().ConvertFrom(bytes) as Image;
  174. #endif
  175. }
  176. catch
  177. {
  178. Bitmap errorBmp = new Bitmap(10, 10);
  179. using (Graphics g = Graphics.FromImage(errorBmp))
  180. {
  181. g.DrawLine(Pens.Red, 0, 0, 10, 10);
  182. g.DrawLine(Pens.Red, 0, 10, 10, 0);
  183. }
  184. return errorBmp;
  185. }
  186. }
  187. return null;
  188. }
  189. private void LoadSignatures()
  190. {
  191. if (signatureData == null) return;
  192. Signatures = new();
  193. var oldData = new Dictionary<string, byte[]>();
  194. foreach(var (name, data) in signatureData)
  195. {
  196. var saveImageData = data;
  197. // FImageData will be reset after this line, keep it
  198. var newImage = Load(data);
  199. if (newImage != null)
  200. {
  201. Signatures.Add(name, newImage);
  202. }
  203. oldData[name] = saveImageData;
  204. }
  205. signatureData = oldData;
  206. }
  207. internal StringFormat GetStringFormat(GraphicCache cache, StringFormatFlags flags, float scale)
  208. {
  209. StringAlignment align = StringAlignment.Near;
  210. if (HorizontalAlignment == System.Windows.HorizontalAlignment.Center)
  211. align = StringAlignment.Center;
  212. else if (HorizontalAlignment == System.Windows.HorizontalAlignment.Right)
  213. align = StringAlignment.Far;
  214. StringAlignment lineAlign = StringAlignment.Near;
  215. if (VerticalAlignment == System.Windows.VerticalAlignment.Center)
  216. lineAlign = StringAlignment.Center;
  217. else if (VerticalAlignment == System.Windows.VerticalAlignment.Bottom)
  218. lineAlign = StringAlignment.Far;
  219. return cache.GetStringFormat(align, lineAlign, StringTrimming.None, flags, 0f, 0f);
  220. }
  221. protected override void DrawItem(FRPaintEventArgs e, Item item, float x, float y, float w, float h)
  222. {
  223. if (item is SignatureItem signature)
  224. {
  225. var imageBoxWidth = w;
  226. var imageBoxHeight = h - LabelHeight;
  227. var aspectRatio = signature.Image.Width / signature.Image.Height;
  228. float imageWidth;
  229. float imageHeight;
  230. if (aspectRatio < imageBoxWidth / imageBoxHeight)
  231. {
  232. imageHeight = imageBoxHeight;
  233. imageWidth = imageHeight * aspectRatio;
  234. }
  235. else
  236. {
  237. imageWidth = imageBoxWidth;
  238. imageHeight = imageWidth / aspectRatio;
  239. }
  240. float imageX = x;
  241. float imageY = y;
  242. switch (HorizontalAlignment)
  243. {
  244. case System.Windows.HorizontalAlignment.Center:
  245. imageX += imageBoxWidth / 2 - imageWidth / 2;
  246. break;
  247. case System.Windows.HorizontalAlignment.Right:
  248. imageX += imageBoxWidth - imageWidth;
  249. break;
  250. case System.Windows.HorizontalAlignment.Stretch:
  251. imageWidth = imageBoxWidth;
  252. break;
  253. }
  254. switch (VerticalAlignment)
  255. {
  256. case System.Windows.VerticalAlignment.Center:
  257. imageY += imageBoxHeight / 2 - imageHeight / 2;
  258. break;
  259. case System.Windows.VerticalAlignment.Top:
  260. imageY += imageBoxHeight - imageHeight;
  261. break;
  262. case System.Windows.VerticalAlignment.Stretch:
  263. imageHeight = imageBoxHeight;
  264. break;
  265. }
  266. RectangleF textRect = new RectangleF(
  267. x * e.ScaleX,
  268. (y + h - LabelHeight) * e.ScaleY,
  269. w * e.ScaleX,
  270. h * e.ScaleY);
  271. Brush textBrush = null;
  272. if (TextFill is SolidFill)
  273. textBrush = e.Cache.GetBrush((TextFill as SolidFill).Color);
  274. else
  275. textBrush = TextFill.CreateBrush(textRect, e.ScaleX, e.ScaleY);
  276. Font font = e.Cache.GetFont(Font.FontFamily,
  277. IsPrinting ? Font.Size : Font.Size * e.ScaleX * 96f / DrawUtils.ScreenDpi,
  278. Font.Style);
  279. e.Graphics.DrawImage(signature.Image, imageX * e.ScaleX, imageY * e.ScaleY, imageWidth * e.ScaleX, imageHeight * e.ScaleY);
  280. var horz = HorizontalAlignment switch
  281. {
  282. System.Windows.HorizontalAlignment.Right => HorzAlign.Right,
  283. System.Windows.HorizontalAlignment.Center => HorzAlign.Center,
  284. System.Windows.HorizontalAlignment.Stretch => HorzAlign.Justify,
  285. _ => HorzAlign.Left
  286. };
  287. var vert = VerticalAlignment switch
  288. {
  289. System.Windows.VerticalAlignment.Bottom => VertAlign.Bottom,
  290. System.Windows.VerticalAlignment.Center => VertAlign.Center,
  291. System.Windows.VerticalAlignment.Stretch => VertAlign.Top,
  292. _ => VertAlign.Top
  293. };
  294. StringFormat format = GetStringFormat(e.Cache, 0, e.ScaleX);
  295. // use advanced rendering
  296. AdvancedTextRenderer advancedRenderer = new AdvancedTextRenderer(signature.Name, e.Graphics, font, textBrush,
  297. null, textRect, format, horz, vert, LabelHeight * e.ScaleY, 0,
  298. 1, false, false, false, false,
  299. e.ScaleX * 96f / DrawUtils.ScreenDpi,
  300. IsPrinting ? 1 : e.ScaleX * 96f / DrawUtils.ScreenDpi, null, IsPrinting);
  301. advancedRenderer.Draw();
  302. }
  303. }
  304. protected override IList<Item>? LoadItems()
  305. {
  306. if (Signatures == null)
  307. {
  308. LoadSignatures();
  309. }
  310. if (Signatures == null)
  311. {
  312. return null;
  313. }
  314. return Signatures.Select(x => new SignatureItem(x.Key, x.Value, LabelHeight) as Item).ToList();
  315. }
  316. }
  317. }