GraphicCache.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using FastReport.Utils;
  2. using System;
  3. using System.Collections;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. namespace FastReport
  7. {
  8. /// <summary>
  9. /// Represents a cache of graphics objects such as pens, brushes, fonts and text formats.
  10. /// </summary>
  11. /// <remarks>
  12. /// Cache holds all used graphics objects. There is no need to dispose objects returned
  13. /// by GetXXX calls.
  14. /// </remarks>
  15. /// <example>This example demonstrates how to use graphic cache.
  16. /// <code>
  17. /// public void Draw(FRPaintEventArgs e)
  18. /// {
  19. /// Brush brush = e.Cache.GetBrush(BackColor);
  20. /// Pen pen = e.Cache.GetPen(BorderColor, 1, BorderStyle);
  21. /// e.Graphics.FillRectangle(brush, Bounds);
  22. /// e.Graphics.DrawRectangle(pen, Bounds);
  23. /// }
  24. /// </code>
  25. /// </example>
  26. public class GraphicCache : IDisposable
  27. {
  28. private Hashtable pens;
  29. private Hashtable brushes;
  30. private Hashtable fonts;
  31. private Hashtable stringFormats;
  32. /// <summary>
  33. /// Gets a pen with specified settings.
  34. /// </summary>
  35. /// <param name="color">Color of a pen.</param>
  36. /// <param name="width">Width of a pen.</param>
  37. /// <param name="style">Dash style of a pen.</param>
  38. /// <returns>The <b>Pen</b> object.</returns>
  39. public Pen GetPen(Color color, float width, DashStyle style)
  40. {
  41. return GetPen(color, width, style, LineJoin.Miter);
  42. }
  43. /// <summary>
  44. /// Gets a pen with specified settings.
  45. /// </summary>
  46. /// <param name="color">Color of a pen.</param>
  47. /// <param name="width">Width of a pen.</param>
  48. /// <param name="style">Dash style of a pen.</param>
  49. /// <param name="lineJoin">Line join of a pen.</param>
  50. /// <returns>The <b>Pen</b> object.</returns>
  51. public Pen GetPen(Color color, float width, DashStyle style, LineJoin lineJoin)
  52. {
  53. int hash = color.GetHashCode() ^ width.GetHashCode() ^ style.GetHashCode() ^ lineJoin.GetHashCode();
  54. Pen result = pens[hash] as Pen;
  55. if (result == null)
  56. {
  57. result = new Pen(color, width);
  58. result.DashStyle = style;
  59. result.LineJoin = lineJoin;
  60. pens[hash] = result;
  61. }
  62. return result;
  63. }
  64. /// <summary>
  65. /// Gets a brush with specified color.
  66. /// </summary>
  67. /// <param name="color">Color of a brush.</param>
  68. /// <returns>The <b>SolidBrush</b> object.</returns>
  69. public SolidBrush GetBrush(Color color)
  70. {
  71. int hash = color.GetHashCode();
  72. SolidBrush result = brushes[hash] as SolidBrush;
  73. if (result == null)
  74. {
  75. result = new SolidBrush(color);
  76. brushes[hash] = result;
  77. }
  78. return result;
  79. }
  80. /// <summary>
  81. /// Gets a font with specified settings.
  82. /// </summary>
  83. /// <param name="name">Family of a font.</param>
  84. /// <param name="size">Size of a font.</param>
  85. /// <param name="style">Style of a font.</param>
  86. /// <returns>The <b>Font</b> object.</returns>
  87. public Font GetFont(FontFamily name, float size, FontStyle style)
  88. {
  89. int hash = name.GetHashCode() ^ size.GetHashCode() ^ style.GetHashCode();
  90. Font result = fonts[hash] as Font;
  91. if (result == null)
  92. {
  93. result = new Font(name, size, style);
  94. fonts[hash] = result;
  95. }
  96. return result;
  97. }
  98. /// <summary>
  99. /// Gets a string format with specified settings.
  100. /// </summary>
  101. /// <param name="align">Text alignment information on the vertical plane.</param>
  102. /// <param name="lineAlign">Line alignment on the horizontal plane.</param>
  103. /// <param name="trimming"><b>StringTrimming</b> enumeration.</param>
  104. /// <param name="flags"><b>StringFormatFlags</b> enumeration that contains formatting information.</param>
  105. /// <param name="firstTab">The number of spaces between the beginning of a line of text and the first tab stop.</param>
  106. /// <param name="tabWidth">Distance between tab stops.</param>
  107. /// <returns>The <b>StringFormat</b> object.</returns>
  108. public StringFormat GetStringFormat(StringAlignment align, StringAlignment lineAlign,
  109. StringTrimming trimming, StringFormatFlags flags, float firstTab, float tabWidth)
  110. {
  111. int hash = align.GetHashCode() ^ (lineAlign.GetHashCode() << 2) ^ (trimming.GetHashCode() << 5) ^
  112. (flags.GetHashCode() << 16) ^ (100 - firstTab).GetHashCode() ^ tabWidth.GetHashCode();
  113. StringFormat result = stringFormats[hash] as StringFormat;
  114. if (result == null)
  115. {
  116. result = new StringFormat();
  117. result.Alignment = align;
  118. result.LineAlignment = lineAlign;
  119. result.Trimming = trimming;
  120. result.FormatFlags = flags;
  121. float[] tabStops = new float[64];
  122. // fixed issue 2823
  123. tabStops[0] = firstTab;
  124. for (int i = 1; i < 64; i++)
  125. {
  126. tabStops[i] = tabWidth;
  127. }
  128. result.SetTabStops(0, tabStops);
  129. stringFormats[hash] = result;
  130. }
  131. return result;
  132. }
  133. /// <summary>
  134. /// Gets a string format with specified settings.
  135. /// </summary>
  136. /// <param name="align">Text alignment information on the vertical plane.</param>
  137. /// <param name="lineAlign">Line alignment on the horizontal plane.</param>
  138. /// <param name="trimming"><b>StringTrimming</b> enumeration.</param>
  139. /// <param name="flags"><b>StringFormatFlags</b> enumeration that contains formatting information.</param>
  140. /// <param name="firstTab">The number of spaces between the beginning of a line of text and the first tab stop.</param>
  141. /// <param name="tabWidth">Distance between tab stops.</param>
  142. /// <param name="defaultTab">Default distance between default tabs stops.</param>
  143. /// <returns>The <b>StringFormat</b> object.</returns>
  144. public StringFormat GetStringFormat(StringAlignment align, StringAlignment lineAlign,
  145. StringTrimming trimming, StringFormatFlags flags, float firstTab, FloatCollection tabWidth,
  146. float defaultTab = 48)
  147. {
  148. int hash = align.GetHashCode() ^ (lineAlign.GetHashCode() << 2) ^ (trimming.GetHashCode() << 5) ^
  149. (flags.GetHashCode() << 16) ^ (100 - firstTab).GetHashCode() ^ tabWidth.GetHashCode();
  150. StringFormat result = stringFormats[hash] as StringFormat;
  151. if (result == null)
  152. {
  153. result = new StringFormat();
  154. result.Alignment = align;
  155. result.LineAlignment = lineAlign;
  156. result.Trimming = trimming;
  157. result.FormatFlags = flags;
  158. float[] tabStops = new float[64];
  159. // fixed issue 2823
  160. tabStops[0] = firstTab;
  161. for (int i = 1; i < 64; i++)
  162. {
  163. if (i > tabWidth.Count)
  164. {
  165. tabStops[i] = defaultTab;
  166. continue;
  167. }
  168. tabStops[i] = tabWidth[i - 1];
  169. }
  170. result.SetTabStops(0, tabStops);
  171. stringFormats[hash] = result;
  172. }
  173. return result;
  174. }
  175. /// <summary>
  176. /// Disposes resources used by this object.
  177. /// </summary>
  178. public void Dispose()
  179. {
  180. foreach (Pen pen in pens.Values)
  181. {
  182. pen.Dispose();
  183. }
  184. foreach (Brush brush in brushes.Values)
  185. {
  186. brush.Dispose();
  187. }
  188. foreach (Font font in fonts.Values)
  189. {
  190. font.Dispose();
  191. }
  192. foreach (StringFormat format in stringFormats.Values)
  193. {
  194. format.Dispose();
  195. }
  196. pens.Clear();
  197. brushes.Clear();
  198. fonts.Clear();
  199. stringFormats.Clear();
  200. }
  201. /// <summary>
  202. /// Initializes a new instance of the <b>GraphicCache</b> class with default settings.
  203. /// </summary>
  204. public GraphicCache()
  205. {
  206. pens = new Hashtable();
  207. brushes = new Hashtable();
  208. fonts = new Hashtable();
  209. stringFormats = new Hashtable();
  210. }
  211. }
  212. }