DrawUtils.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Drawing;
  3. namespace FastReport.Utils
  4. {
  5. internal enum MonoRendering
  6. {
  7. Undefined,
  8. Pango,
  9. Cairo
  10. }
  11. public static partial class DrawUtils
  12. {
  13. private static Font FDefaultFont;
  14. private static Font FDefault96Font;
  15. private static Font FDefaultReportFont;
  16. private static Font FDefaultTextObjectFont;
  17. private static Font FFixedFont;
  18. private static int FScreenDpi;
  19. private static float FDpiFX;
  20. private static MonoRendering FMonoRendering = MonoRendering.Undefined;
  21. public static int ScreenDpi
  22. {
  23. get
  24. {
  25. if (FScreenDpi == 0)
  26. FScreenDpi = GetDpi();
  27. return FScreenDpi;
  28. }
  29. }
  30. public static float ScreenDpiFX
  31. {
  32. get
  33. {
  34. if (FDpiFX == 0)
  35. FDpiFX = 96f / DrawUtils.ScreenDpi;
  36. return FDpiFX;
  37. }
  38. }
  39. private static int GetDpi()
  40. {
  41. using (Bitmap bmp = new Bitmap(1, 1))
  42. using (Graphics g = Graphics.FromImage(bmp))
  43. {
  44. return (int)g.DpiX;
  45. }
  46. }
  47. public static Font DefaultFont
  48. {
  49. get
  50. {
  51. if (FDefaultFont == null)
  52. {
  53. switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
  54. {
  55. case "ja":
  56. FDefaultFont = new Font("MS UI Gothic", 9);
  57. break;
  58. case "zh":
  59. FDefaultFont = new Font("SimSun", 9);
  60. break;
  61. default:
  62. FDefaultFont = new Font("Tahoma", 8);
  63. break;
  64. }
  65. }
  66. return FDefaultFont;
  67. }
  68. }
  69. public static Font DefaultReportFont
  70. {
  71. get
  72. {
  73. if (FDefaultReportFont == null)
  74. {
  75. switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
  76. {
  77. case "ja":
  78. FDefaultReportFont = new Font("MS UI Gothic", 9);
  79. break;
  80. case "zh":
  81. FDefaultReportFont = new Font("SimSun", 9);
  82. break;
  83. default:
  84. FDefaultReportFont = new Font("Arial", 10);
  85. break;
  86. }
  87. }
  88. return FDefaultReportFont;
  89. }
  90. }
  91. public static Font DefaultTextObjectFont
  92. {
  93. get
  94. {
  95. if (FDefaultTextObjectFont == null)
  96. FDefaultTextObjectFont = new Font("Arial", 10);
  97. return FDefaultTextObjectFont;
  98. }
  99. }
  100. public static Font FixedFont
  101. {
  102. get
  103. {
  104. if (FFixedFont == null)
  105. FFixedFont = new Font("Courier New", 10);
  106. return FFixedFont;
  107. }
  108. }
  109. public static Font Default96Font
  110. {
  111. get
  112. {
  113. if (FDefault96Font == null)
  114. {
  115. float sz = 96f / ScreenDpi;
  116. switch (System.Globalization.CultureInfo.CurrentCulture.TwoLetterISOLanguageName)
  117. {
  118. case "ja":
  119. FDefault96Font = new Font("MS UI Gothic", 9 * sz);
  120. break;
  121. case "zh":
  122. FDefault96Font = new Font("SimSun", 9 * sz);
  123. break;
  124. default:
  125. FDefault96Font = new Font("Tahoma", 8 * sz);
  126. break;
  127. }
  128. }
  129. return FDefault96Font;
  130. }
  131. }
  132. public static SizeF MeasureString(string text)
  133. {
  134. return MeasureString(text, DefaultFont);
  135. }
  136. public static SizeF MeasureString(string text, Font font)
  137. {
  138. using (Bitmap bmp = new Bitmap(1, 1))
  139. using (StringFormat sf = new StringFormat())
  140. {
  141. Graphics g = Graphics.FromImage(bmp);
  142. return MeasureString(g, text, font, sf);
  143. }
  144. }
  145. public static SizeF MeasureString(Graphics g, string text, Font font, StringFormat format)
  146. {
  147. return MeasureString(g, text, font, new RectangleF(0, 0, 10000, 10000), format);
  148. }
  149. public static SizeF MeasureString(Graphics g, string text, Font font, RectangleF layoutRect, StringFormat format)
  150. {
  151. if (String.IsNullOrEmpty(text))
  152. return new SizeF(0, 0);
  153. CharacterRange[] characterRanges = { new CharacterRange(0, text.Length) };
  154. StringFormatFlags saveFlags = format.FormatFlags;
  155. format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
  156. format.SetMeasurableCharacterRanges(characterRanges);
  157. Region[] regions = g.MeasureCharacterRanges(text, font, layoutRect, format);
  158. format.FormatFlags = saveFlags;
  159. RectangleF rect = regions[0].GetBounds(g);
  160. regions[0].Dispose();
  161. return rect.Size;
  162. }
  163. public static void FloodFill(Bitmap bmp, int x, int y, Color color, Color replacementColor)
  164. {
  165. if (x < 0 || y < 0 || x >= bmp.Width || y >= bmp.Height || bmp.GetPixel(x, y) != color)
  166. return;
  167. bmp.SetPixel(x, y, replacementColor);
  168. FloodFill(bmp, x - 1, y, color, replacementColor);
  169. FloodFill(bmp, x + 1, y, color, replacementColor);
  170. FloodFill(bmp, x, y - 1, color, replacementColor);
  171. FloodFill(bmp, x, y + 1, color, replacementColor);
  172. }
  173. internal static MonoRendering GetMonoRendering(Graphics printerGraphics)
  174. {
  175. if (FMonoRendering == MonoRendering.Undefined)
  176. {
  177. GraphicsUnit savedUnit = printerGraphics.PageUnit;
  178. printerGraphics.PageUnit = GraphicsUnit.Point;
  179. const string s = "test string test string test string test string";
  180. float f1 = printerGraphics.MeasureString(s, DefaultReportFont).Width;
  181. FMonoRendering = f1 > 200 ? MonoRendering.Pango : MonoRendering.Cairo;
  182. printerGraphics.PageUnit = savedUnit;
  183. }
  184. return FMonoRendering;
  185. }
  186. }
  187. }