MultiSignatureObject.cs 13 KB

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