TextRenderer.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. //
  2. // TextRenderer.cs
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. // Copyright (c) 2006 Novell, Inc.
  24. //
  25. // Authors:
  26. // Jonathan Pobst ([email protected])
  27. //
  28. using System.Drawing;
  29. using System.Runtime.InteropServices;
  30. using System.Text;
  31. using System.Drawing.Text;
  32. namespace System.Windows.Forms
  33. {
  34. #if NET_2_0
  35. public sealed
  36. #endif
  37. class TextRenderer
  38. {
  39. private static Bitmap measure_bitmap = new Bitmap (1, 1);
  40. private TextRenderer ()
  41. {
  42. }
  43. #region Public Methods
  44. #if NET_2_0
  45. public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor)
  46. {
  47. DrawTextInternal (dc, text, font, pt, foreColor, Color.Transparent, TextFormatFlags.Default, false);
  48. }
  49. public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor)
  50. {
  51. DrawTextInternal (dc, text, font, bounds, foreColor, Color.Transparent, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, false);
  52. }
  53. public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor)
  54. {
  55. DrawTextInternal (dc, text, font, pt, foreColor, backColor, TextFormatFlags.Default, false);
  56. }
  57. public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags)
  58. {
  59. DrawTextInternal (dc, text, font, pt, foreColor, Color.Transparent, flags, false);
  60. }
  61. public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor)
  62. {
  63. DrawTextInternal (dc, text, font, bounds, foreColor, backColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, false);
  64. }
  65. public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
  66. {
  67. DrawTextInternal (dc, text, font, bounds, foreColor, Color.Transparent, flags, false);
  68. }
  69. public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor, TextFormatFlags flags)
  70. {
  71. DrawTextInternal (dc, text, font, pt, foreColor, backColor, flags, false);
  72. }
  73. public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags)
  74. {
  75. DrawTextInternal (dc, text, font, bounds, foreColor, backColor, flags, false);
  76. }
  77. public static Size MeasureText (string text, Font font)
  78. {
  79. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, Size.Empty, TextFormatFlags.Default, false);
  80. }
  81. public static Size MeasureText (IDeviceContext dc, string text, Font font)
  82. {
  83. return MeasureTextInternal (dc, text, font, Size.Empty, TextFormatFlags.Default, false);
  84. }
  85. public static Size MeasureText (string text, Font font, Size proposedSize)
  86. {
  87. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, proposedSize, TextFormatFlags.Default, false);
  88. }
  89. public static Size MeasureText (IDeviceContext dc, string text, Font font, Size proposedSize)
  90. {
  91. return MeasureTextInternal (dc, text, font, proposedSize, TextFormatFlags.Default, false);
  92. }
  93. public static Size MeasureText (string text, Font font, Size proposedSize, TextFormatFlags flags)
  94. {
  95. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, proposedSize, flags, false);
  96. }
  97. public static Size MeasureText (IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags)
  98. {
  99. return MeasureTextInternal (dc, text, font, proposedSize, flags, false);
  100. }
  101. #endif
  102. #endregion
  103. #region Internal Methods That Do Stuff
  104. #if NET_2_0
  105. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags, bool useDrawString)
  106. #else
  107. internal static void DrawTextInternal (Graphics dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, TextFormatFlags flags, bool useDrawString)
  108. #endif
  109. {
  110. #if NET_2_0
  111. if (dc == null)
  112. throw new ArgumentNullException ("dc");
  113. if (string.IsNullOrEmpty (text))
  114. return;
  115. // We use MS GDI API's unless told not to, or we aren't on Windows
  116. if (!useDrawString && (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows)) {
  117. if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter || (flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom)
  118. flags |= TextFormatFlags.SingleLine;
  119. // Calculate the text bounds (there is often padding added)
  120. Rectangle new_bounds = PadRectangle (bounds, flags);
  121. new_bounds.Offset ((int)(dc as Graphics).Transform.OffsetX, (int)(dc as Graphics).Transform.OffsetY);
  122. IntPtr hdc = IntPtr.Zero;
  123. bool clear_clip_region = false;
  124. // If we need to use the graphics clipping region, add it to our hdc
  125. if ((flags & TextFormatFlags.PreserveGraphicsClipping) == TextFormatFlags.PreserveGraphicsClipping) {
  126. Graphics graphics = (Graphics)dc;
  127. Region clip_region = graphics.Clip;
  128. if (!clip_region.IsInfinite (graphics)) {
  129. IntPtr hrgn = clip_region.GetHrgn (graphics);
  130. hdc = dc.GetHdc ();
  131. SelectClipRgn (hdc, hrgn);
  132. clip_region.ReleaseHrgn (hrgn);
  133. clear_clip_region = true;
  134. }
  135. }
  136. if (hdc == IntPtr.Zero)
  137. hdc = dc.GetHdc ();
  138. // Set the fore color
  139. if (foreColor != Color.Empty)
  140. SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
  141. // Set the back color
  142. if (backColor != Color.Transparent && backColor != Color.Empty) {
  143. SetBkMode (hdc, 2); //1-Transparent, 2-Opaque
  144. SetBkColor (hdc, ColorTranslator.ToWin32 (backColor));
  145. }
  146. else {
  147. SetBkMode (hdc, 1); //1-Transparent, 2-Opaque
  148. }
  149. XplatUIWin32.RECT r = XplatUIWin32.RECT.FromRectangle (new_bounds);
  150. IntPtr prevobj;
  151. if (font != null) {
  152. prevobj = SelectObject (hdc, font.ToHfont ());
  153. Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
  154. prevobj = SelectObject (hdc, prevobj);
  155. DeleteObject (prevobj);
  156. }
  157. else {
  158. Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
  159. }
  160. if (clear_clip_region)
  161. SelectClipRgn (hdc, IntPtr.Zero);
  162. dc.ReleaseHdc ();
  163. }
  164. // Use Graphics.DrawString as a fallback method
  165. else {
  166. #endif
  167. Graphics g;
  168. #if NET_2_0
  169. if (dc is Graphics)
  170. g = (Graphics)dc;
  171. else
  172. g = Graphics.FromHdc (dc.GetHdc ());
  173. #else
  174. g = (Graphics)dc;
  175. #endif
  176. StringFormat sf = FlagsToStringFormat (flags);
  177. Rectangle new_bounds = PadDrawStringRectangle (bounds, flags);
  178. g.DrawString (text, font, ThemeEngine.Current.ResPool.GetSolidBrush (foreColor), new_bounds, sf);
  179. #if NET_2_0
  180. if (!(dc is Graphics)) {
  181. g.Dispose ();
  182. dc.ReleaseHdc ();
  183. }
  184. }
  185. #endif
  186. }
  187. #if NET_2_0
  188. internal static Size MeasureTextInternal (IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags, bool useMeasureString)
  189. #else
  190. internal static Size MeasureTextInternal (Graphics dc, string text, Font font, Size proposedSize, TextFormatFlags flags, bool useMeasureString)
  191. #endif
  192. {
  193. #if NET_2_0
  194. if (!useMeasureString && (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows)) {
  195. // Tell DrawText to calculate size instead of draw
  196. flags |= (TextFormatFlags)1024; // DT_CALCRECT
  197. IntPtr hdc = dc.GetHdc ();
  198. XplatUIWin32.RECT r = XplatUIWin32.RECT.FromRectangle (new Rectangle (Point.Empty, proposedSize));
  199. IntPtr prevobj;
  200. if (font != null) {
  201. prevobj = SelectObject (hdc, font.ToHfont ());
  202. Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
  203. prevobj = SelectObject (hdc, prevobj);
  204. DeleteObject (prevobj);
  205. }
  206. else {
  207. Win32DrawText (hdc, text, text.Length, ref r, (int)flags);
  208. }
  209. dc.ReleaseHdc ();
  210. // Really, I am just making something up here, which as far as I can tell, MS
  211. // just makes something up as well. This will require lots of tweaking to match MS. :(
  212. Size retval = r.ToRectangle ().Size;
  213. if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0) {
  214. retval.Width += 6;
  215. retval.Width += (int)retval.Height / 8;
  216. }
  217. return retval;
  218. }
  219. else {
  220. #endif
  221. Graphics g = Graphics.FromImage (measure_bitmap);
  222. StringFormat sf = FlagsToStringFormat (flags);
  223. Size retval = g.MeasureString (text, font, Int32.MaxValue, sf).ToSize ();
  224. if (retval.Width > 0 && (flags & TextFormatFlags.NoPadding) == 0)
  225. retval.Width += 9;
  226. return retval;
  227. }
  228. #if NET_2_0
  229. }
  230. #endif
  231. #endregion
  232. #region Internal Methods That Are Just Overloads
  233. #if NET_2_0
  234. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, bool useDrawString)
  235. {
  236. DrawTextInternal (dc, text, font, pt, foreColor, Color.Transparent, TextFormatFlags.Default, useDrawString);
  237. }
  238. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, bool useDrawString)
  239. {
  240. DrawTextInternal (dc, text, font, bounds, foreColor, Color.Transparent, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, useDrawString);
  241. }
  242. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor, bool useDrawString)
  243. {
  244. DrawTextInternal (dc, text, font, pt, foreColor, backColor, TextFormatFlags.Default, useDrawString);
  245. }
  246. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags, bool useDrawString)
  247. {
  248. DrawTextInternal (dc, text, font, pt, foreColor, Color.Transparent, flags, useDrawString);
  249. }
  250. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, Color backColor, bool useDrawString)
  251. {
  252. DrawTextInternal (dc, text, font, bounds, foreColor, backColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter, useDrawString);
  253. }
  254. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags, bool useDrawString)
  255. #else
  256. internal static void DrawTextInternal (Graphics dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags, bool useDrawString)
  257. #endif
  258. {
  259. DrawTextInternal (dc, text, font, bounds, foreColor, Color.Transparent, flags, useDrawString);
  260. }
  261. internal static Size MeasureTextInternal (string text, Font font, bool useMeasureString)
  262. {
  263. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, Size.Empty, TextFormatFlags.Default, useMeasureString);
  264. }
  265. #if NET_2_0
  266. internal static void DrawTextInternal (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor, TextFormatFlags flags, bool useDrawString)
  267. {
  268. Size sz = MeasureTextInternal (dc, text, font, useDrawString);
  269. DrawTextInternal (dc, text, font, new Rectangle (pt, sz), foreColor, backColor, flags, useDrawString);
  270. }
  271. internal static Size MeasureTextInternal (IDeviceContext dc, string text, Font font, bool useMeasureString)
  272. {
  273. return MeasureTextInternal (dc, text, font, Size.Empty, TextFormatFlags.Default, useMeasureString);
  274. }
  275. internal static Size MeasureTextInternal (string text, Font font, Size proposedSize, bool useMeasureString)
  276. {
  277. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, proposedSize, TextFormatFlags.Default, useMeasureString);
  278. }
  279. internal static Size MeasureTextInternal (IDeviceContext dc, string text, Font font, Size proposedSize, bool useMeasureString)
  280. {
  281. return MeasureTextInternal (dc, text, font, proposedSize, TextFormatFlags.Default, useMeasureString);
  282. }
  283. #endif
  284. internal static Size MeasureTextInternal (string text, Font font, Size proposedSize, TextFormatFlags flags, bool useMeasureString)
  285. {
  286. return MeasureTextInternal (Graphics.FromImage (measure_bitmap), text, font, proposedSize, flags, useMeasureString);
  287. }
  288. #endregion
  289. #region Private Methods
  290. private static StringFormat FlagsToStringFormat (TextFormatFlags flags)
  291. {
  292. StringFormat sf = new StringFormat ();
  293. // Translation table: http://msdn.microsoft.com/msdnmag/issues/06/03/TextRendering/default.aspx?fig=true#fig4
  294. // Horizontal Alignment
  295. if ((flags & TextFormatFlags.HorizontalCenter) == TextFormatFlags.HorizontalCenter)
  296. sf.Alignment = StringAlignment.Center;
  297. else if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
  298. sf.Alignment = StringAlignment.Far;
  299. else
  300. sf.Alignment = StringAlignment.Near;
  301. // Vertical Alignment
  302. if ((flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom)
  303. sf.LineAlignment = StringAlignment.Far;
  304. else if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter)
  305. sf.LineAlignment = StringAlignment.Center;
  306. else
  307. sf.LineAlignment = StringAlignment.Near;
  308. // Ellipsis
  309. if ((flags & TextFormatFlags.EndEllipsis) == TextFormatFlags.EndEllipsis)
  310. sf.Trimming = StringTrimming.EllipsisCharacter;
  311. else if ((flags & TextFormatFlags.PathEllipsis) == TextFormatFlags.PathEllipsis)
  312. sf.Trimming = StringTrimming.EllipsisPath;
  313. else if ((flags & TextFormatFlags.WordEllipsis) == TextFormatFlags.WordEllipsis)
  314. sf.Trimming = StringTrimming.EllipsisWord;
  315. else
  316. sf.Trimming = StringTrimming.Character;
  317. // Hotkey Prefix
  318. if ((flags & TextFormatFlags.NoPrefix) == TextFormatFlags.NoPrefix)
  319. sf.HotkeyPrefix = HotkeyPrefix.None;
  320. else if ((flags & TextFormatFlags.HidePrefix) == TextFormatFlags.HidePrefix)
  321. sf.HotkeyPrefix = HotkeyPrefix.Hide;
  322. else
  323. sf.HotkeyPrefix = HotkeyPrefix.Show;
  324. // Text Padding
  325. if ((flags & TextFormatFlags.NoPadding) == TextFormatFlags.NoPadding)
  326. sf.FormatFlags |= StringFormatFlags.FitBlackBox;
  327. // Text Wrapping
  328. if ((flags & TextFormatFlags.SingleLine) == TextFormatFlags.SingleLine)
  329. sf.FormatFlags |= StringFormatFlags.NoWrap;
  330. else if ((flags & TextFormatFlags.TextBoxControl) == TextFormatFlags.TextBoxControl)
  331. sf.FormatFlags |= StringFormatFlags.LineLimit;
  332. // Other Flags
  333. //if ((flags & TextFormatFlags.RightToLeft) == TextFormatFlags.RightToLeft)
  334. // sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  335. if ((flags & TextFormatFlags.NoClipping) == TextFormatFlags.NoClipping)
  336. sf.FormatFlags |= StringFormatFlags.NoClip;
  337. if ((flags & TextFormatFlags.WordBreak) == 0)
  338. sf.FormatFlags |= StringFormatFlags.LineLimit;
  339. return sf;
  340. }
  341. #if NET_2_0
  342. private static Rectangle PadRectangle (Rectangle r, TextFormatFlags flags)
  343. {
  344. if ((flags & TextFormatFlags.NoPadding) == 0 && (flags & TextFormatFlags.Right) == 0 && (flags & TextFormatFlags.HorizontalCenter) == 0) {
  345. r.X += 3;
  346. r.Width -= 3;
  347. }
  348. if ((flags & TextFormatFlags.NoPadding) == 0 && (flags & TextFormatFlags.Right) == TextFormatFlags.Right) {
  349. r.Width -= 4;
  350. }
  351. if ((flags & TextFormatFlags.LeftAndRightPadding) == TextFormatFlags.LeftAndRightPadding) {
  352. r.X += 2;
  353. r.Width -= 2;
  354. }
  355. if ((flags & TextFormatFlags.WordEllipsis) == TextFormatFlags.WordEllipsis || (flags & TextFormatFlags.EndEllipsis) == TextFormatFlags.EndEllipsis || (flags & TextFormatFlags.WordBreak) == TextFormatFlags.WordBreak) {
  356. r.Width -= 4;
  357. }
  358. if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter) {
  359. r.Y += 1;
  360. }
  361. return r;
  362. }
  363. #endif
  364. private static Rectangle PadDrawStringRectangle (Rectangle r, TextFormatFlags flags)
  365. {
  366. if ((flags & TextFormatFlags.NoPadding) == 0 && (flags & TextFormatFlags.Right) == 0 && (flags & TextFormatFlags.HorizontalCenter) == 0) {
  367. r.X += 1;
  368. r.Width -= 1;
  369. }
  370. if ((flags & TextFormatFlags.NoPadding) == 0 && (flags & TextFormatFlags.Right) == TextFormatFlags.Right) {
  371. r.Width -= 4;
  372. }
  373. if ((flags & TextFormatFlags.NoPadding) == TextFormatFlags.NoPadding) {
  374. r.X -= 2;
  375. }
  376. if ((flags & TextFormatFlags.NoPadding) == 0 && (flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom) {
  377. r.Y += 1;
  378. }
  379. if ((flags & TextFormatFlags.LeftAndRightPadding) == TextFormatFlags.LeftAndRightPadding) {
  380. r.X += 2;
  381. r.Width -= 2;
  382. }
  383. if ((flags & TextFormatFlags.WordEllipsis) == TextFormatFlags.WordEllipsis || (flags & TextFormatFlags.EndEllipsis) == TextFormatFlags.EndEllipsis || (flags & TextFormatFlags.WordBreak) == TextFormatFlags.WordBreak) {
  384. r.Width -= 4;
  385. }
  386. if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter) {
  387. r.Y += 1;
  388. }
  389. return r;
  390. }
  391. #endregion
  392. #region DllImports (Windows)
  393. #if NET_2_0
  394. [DllImport ("user32", CharSet = CharSet.Unicode, EntryPoint = "DrawText")]
  395. static extern int Win32DrawText (IntPtr hdc, string lpStr, int nCount, ref XplatUIWin32.RECT lpRect, int wFormat);
  396. [DllImport ("gdi32")]
  397. static extern int SetTextColor (IntPtr hdc, int crColor);
  398. [DllImport ("gdi32")]
  399. static extern IntPtr SelectObject (IntPtr hDC, IntPtr hObject);
  400. [DllImport ("gdi32")]
  401. static extern int SetBkColor (IntPtr hdc, int crColor);
  402. [DllImport ("gdi32")]
  403. static extern int SetBkMode (IntPtr hdc, int iBkMode);
  404. [DllImport ("gdi32")]
  405. static extern bool DeleteObject (IntPtr objectHandle);
  406. [DllImport("gdi32")]
  407. static extern bool SelectClipRgn(IntPtr hdc, IntPtr hrgn);
  408. #endif
  409. #endregion
  410. }
  411. }