MultiSignatureObject.cs 13 KB

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