TextBoxTextRenderer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Permission is hereby granted, free of charge, to any person obtaining
  2. // a copy of this software and associated documentation files (the
  3. // "Software"), to deal in the Software without restriction, including
  4. // without limitation the rights to use, copy, modify, merge, publish,
  5. // distribute, sublicense, and/or sell copies of the Software, and to
  6. // permit persons to whom the Software is furnished to do so, subject to
  7. // the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be
  10. // included in all copies or substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  13. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  15. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  16. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  17. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  18. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. //
  20. // Copyright (c) 2007 Novell, Inc. (http://www.novell.com)
  21. //
  22. // Authors:
  23. // Jonathan Pobst [email protected]
  24. //
  25. //
  26. using System;
  27. using System.Drawing;
  28. using System.Drawing.Text;
  29. using System.Text;
  30. using System.Collections;
  31. namespace System.Windows.Forms
  32. {
  33. internal class TextBoxTextRenderer
  34. {
  35. private static Size max_size;
  36. private static bool use_textrenderer;
  37. private static StringFormat sf_nonprinting;
  38. private static StringFormat sf_printing;
  39. private static Hashtable measure_cache;
  40. static TextBoxTextRenderer ()
  41. {
  42. // On Windows, we want to use TextRenderer (GDI)
  43. // On Linux, we want to use DrawString (GDI+)
  44. // TextRenderer provides translation from TextRenderer to
  45. // DrawString, but I doubt it's exact enough.
  46. // Another option would be to put Pango here for Linux.
  47. int platform = (int)Environment.OSVersion.Platform;
  48. if (platform == 4 || platform == 128 || platform == 6)
  49. use_textrenderer = false;
  50. else
  51. use_textrenderer = true;
  52. // windows 2000 doesn't draw with gdi if bounds are In32.MaxValue
  53. max_size = new Size (Int16.MaxValue, Int16.MaxValue);
  54. sf_nonprinting = new StringFormat (StringFormat.GenericTypographic);
  55. sf_nonprinting.Trimming = StringTrimming.None;
  56. sf_nonprinting.FormatFlags = StringFormatFlags.DisplayFormatControl;
  57. sf_nonprinting.HotkeyPrefix = HotkeyPrefix.None;
  58. sf_printing = StringFormat.GenericTypographic;
  59. sf_printing.HotkeyPrefix = HotkeyPrefix.None;
  60. measure_cache = new Hashtable ();
  61. }
  62. public static void DrawText (Graphics g, string text, Font font, Color color, float x, float y, bool showNonPrint)
  63. {
  64. if (!use_textrenderer) {
  65. if (showNonPrint)
  66. g.DrawString (text, font, ThemeEngine.Current.ResPool.GetSolidBrush (color), x, y, sf_nonprinting);
  67. else
  68. g.DrawString (text, font, ThemeEngine.Current.ResPool.GetSolidBrush (color), x, y, sf_printing);
  69. } else {
  70. if (showNonPrint)
  71. TextRenderer.DrawTextInternal (g, text, font, new Rectangle (new Point ((int)x, (int)y), max_size), color, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false);
  72. else
  73. TextRenderer.DrawTextInternal (g, text, font, new Rectangle (new Point ((int)x, (int)y), max_size), color, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false);
  74. }
  75. }
  76. public static SizeF MeasureText (Graphics g, string text, Font font)
  77. {
  78. // Due to the way the TextBox currently works, it measures each
  79. // character one at a time. And it does this alot. So here we
  80. // are implementing a cache for each font/character combination
  81. // measurement. Since the number of fonts and number of characters
  82. // used tends to be small, this is a good performance gain for
  83. // not too much memory.
  84. if (text.Length == 1) {
  85. string key = font.GetHashCode ().ToString () + "|" + text;
  86. if (measure_cache.ContainsKey (key)) {
  87. return (SizeF)measure_cache[key];
  88. } else {
  89. SizeF size;
  90. if (!use_textrenderer)
  91. size = g.MeasureString (text, font, 10000, sf_nonprinting);
  92. else
  93. size = TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false);
  94. measure_cache[key] = size;
  95. return size;
  96. }
  97. }
  98. if (!use_textrenderer)
  99. return g.MeasureString (text, font, 10000, sf_nonprinting);
  100. else
  101. return TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false);
  102. }
  103. }
  104. }