public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize) { if (dc == null) { throw new ArgumentNullException("dc"); } if (string.IsNullOrEmpty(text)) { return(Size.Empty); } WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics); IntPtr hdc = dc.GetHdc(); try { using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc)) { using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) { return(wg.MeasureText(text, wf, proposedSize)); } } } finally { dc.ReleaseHdc(); } }
/// <include file='doc\TextRenderer.uex' path='docs/doc[@for="TextRenderer.DrawText1"]/*' /> public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, Color backColor) { if (dc == null) { throw new ArgumentNullException("dc"); } WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics); IntPtr hdc = dc.GetHdc(); try { using (WindowsGraphics wg = WindowsGraphics.FromHdc(hdc)) { using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) { wg.DrawText(text, wf, pt, foreColor, backColor); } } } finally { dc.ReleaseHdc(); } }
public static WindowsFont FromFont(Font font, WindowsFontQuality fontQuality) { string name = font.FontFamily.Name; if (((name != null) && (name.Length > 1)) && (name[0] == '@')) { name = name.Substring(1); } return(new WindowsFont(name, font.SizeInPoints, font.Style, font.GdiCharSet, fontQuality)); }
/// <include file='doc\TextRenderer.uex' path='docs/doc[@for="TextRenderer.DrawText2"]/*' /> public static void DrawText(IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags) { if (dc == null) { throw new ArgumentNullException("dc"); } WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics); using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, flags)) { using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) { wgr.WindowsGraphics.DrawText(text, wf, pt, foreColor, GetIntTextFormatFlags(flags)); } } }
public WindowsFont(string faceName, float size, FontStyle style, byte charSet, WindowsFontQuality fontQuality) { this.fontSize = -1f; this.logFont = new IntNativeMethods.LOGFONT(); int num = (int) Math.Ceiling((double) ((WindowsGraphicsCacheManager.MeasurementGraphics.DeviceContext.DpiY * size) / 72f)); this.logFont.lfHeight = -num; this.logFont.lfFaceName = (faceName != null) ? faceName : "Microsoft Sans Serif"; this.logFont.lfCharSet = charSet; this.logFont.lfOutPrecision = 4; this.logFont.lfQuality = (byte) fontQuality; this.logFont.lfWeight = ((style & FontStyle.Bold) == FontStyle.Bold) ? 700 : 400; this.logFont.lfItalic = ((style & FontStyle.Italic) == FontStyle.Italic) ? ((byte) 1) : ((byte) 0); this.logFont.lfUnderline = ((style & FontStyle.Underline) == FontStyle.Underline) ? ((byte) 1) : ((byte) 0); this.logFont.lfStrikeOut = ((style & FontStyle.Strikeout) == FontStyle.Strikeout) ? ((byte) 1) : ((byte) 0); this.style = style; this.CreateFont(); }
public static WindowsFont FromFont(Font font, WindowsFontQuality fontQuality) { string familyName = font.FontFamily.Name; // Strip vertical-font mark from the name if needed. if (familyName != null && familyName.Length > 1 && familyName[0] == '@') { familyName = familyName.Substring(1); } // Note: Creating the WindowsFont from Font using a LOGFONT structure from GDI+ (Font.ToLogFont(logFont)) may sound like // a better choice (more accurate) for doing this but tests show that is not the case (see WindowsFontTests test suite), // the results are the same. Also, that approach has some issues when the Font is created in a different application // domain since the LOGFONT cannot be marshalled properly. // Now, creating it using the Font.SizeInPoints makes it GraphicsUnit-independent. return(new WindowsFont(familyName, font.SizeInPoints, font.Style, font.GdiCharSet, fontQuality)); }
/// <summary> /// Contructs a WindowsFont object from an existing System.Drawing.Font object (GDI+), based on the screen dc MapMode /// and resolution (normally: MM_TEXT and 96 dpi). /// </summary> public static WindowsFont FromFont(Font font, WindowsFontQuality fontQuality = WindowsFontQuality.Default) { string familyName = font.FontFamily.Name; // Strip vertical-font mark from the name if needed. if (familyName != null && familyName.Length > 1 && familyName[0] == '@') { familyName = familyName.Substring(1); } // Note: Creating the WindowsFont from Font using a LOGFONT structure from GDI+ (Font.ToLogFont(logFont)) may sound like // a better choice (more accurate) for doing this but tests show that is not the case (see WindowsFontTests test suite), // the results are the same. Also, that approach has some issues when the Font is created in a different application // domain since the LOGFONT cannot be marshalled properly. // Now, creating it using the Font.SizeInPoints makes it GraphicsUnit-independent. Debug.Assert(font.SizeInPoints > 0.0f, "size has a negative value."); // Get the font height from the specified size. size is in point units and height in logical // units (pixels when using MM_TEXT) so we need to make the conversion using the number of // pixels per logical inch along the screen height. (1 point = 1/72 inch.) int pixelsY = (int)Math.Ceiling(WindowsGraphicsCacheManager.MeasurementGraphics.DeviceContext.DpiY * font.SizeInPoints / 72); // The lfHeight represents the font cell height (line spacing) which includes the internal // leading; we specify a negative size value (in pixels) for the height so the font mapper // provides the closest match for the character height rather than the cell height (MSDN). NativeMethods.LOGFONTW logFont = new NativeMethods.LOGFONTW() { lfHeight = -pixelsY, lfCharSet = font.GdiCharSet, lfOutPrecision = IntNativeMethods.OUT_TT_PRECIS, lfQuality = (byte)fontQuality, lfWeight = (font.Style & FontStyle.Bold) == FontStyle.Bold ? IntNativeMethods.FW_BOLD : IntNativeMethods.FW_NORMAL, lfItalic = (font.Style & FontStyle.Italic) == FontStyle.Italic ? True : False, lfUnderline = (font.Style & FontStyle.Underline) == FontStyle.Underline ? True : False, lfStrikeOut = (font.Style & FontStyle.Strikeout) == FontStyle.Strikeout ? True : False, FaceName = familyName }; return(new WindowsFont(logFont, font.Style, createHandle: true)); }
public static Size MeasureText(IDeviceContext dc, string text, Font font, Size proposedSize, TextFormatFlags flags) { if (dc == null) { throw new ArgumentNullException("dc"); } if (string.IsNullOrEmpty(text)) { return(Size.Empty); } WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics); using (WindowsGraphicsWrapper wgr = new WindowsGraphicsWrapper(dc, flags)) { using (WindowsFont wf = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) { return(wgr.WindowsGraphics.MeasureText(text, wf, proposedSize, GetIntTextFormatFlags(flags))); } } }
public static void DrawText(IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor) { if (dc == null) { throw new ArgumentNullException("dc"); } WindowsFontQuality fontQuality = WindowsFont.WindowsFontQualityFromTextRenderingHint(dc as Graphics); IntPtr hdc = dc.GetHdc(); try { using (WindowsGraphics graphics = WindowsGraphics.FromHdc(hdc)) { using (WindowsFont font2 = WindowsGraphicsCacheManager.GetWindowsFont(font, fontQuality)) { graphics.DrawText(text, font2, bounds, foreColor); } } } finally { dc.ReleaseHdc(); } }
/// <summary> /// Initializes a new instance of the <see cref="WindowsFont"/> class. /// </summary> /// <param name="faceName">Name of the face.</param> /// <param name="size">The size.</param> /// <param name="style">The style.</param> /// <param name="quality">The quality.</param> public WindowsFont(string faceName, int size, FontStyle style, WindowsFontQuality quality) { hFont = NativeMethods.CreateWindowsFont(faceName, size, (int)style, (int)quality); color = Color.FromKnownColor(KnownColor.WindowText); Win32Color = ColorTranslator.ToWin32(color); }
public static WindowsFont GetWindowsFont(Font font, WindowsFontQuality fontQuality) { if (font == null) { return(null); } // First check if font is in the cache. int count = 0; int index = currentIndex; // Search by index of most recently added object. while (count < WindowsFontCache.Count) { if (WindowsFontCache[index].Key.Equals(font)) // don't do shallow comparison, we could miss cloned fonts. { // We got a Font in the cache, let's see if we have a WindowsFont with the same quality as required by the caller. // WARNING: It is not expected that the WindowsFont is disposed externally since it is created by this class. Debug.Assert(WindowsFontCache[index].Value.Hfont != IntPtr.Zero, "Cached WindowsFont was disposed, enable GDI_FINALIZATION_WATCH to track who did it!"); WindowsFont wf = WindowsFontCache[index].Value; if (wf.Quality == fontQuality) { return(wf); } } index--; count++; if (index < 0) { index = CacheSize - 1; } } // Font is not in the cache, let's add it. WindowsFont winFont = WindowsFont.FromFont(font, fontQuality); KeyValuePair <Font, WindowsFont> newEntry = new KeyValuePair <Font, WindowsFont>(font, winFont); currentIndex++; if (currentIndex == CacheSize) { currentIndex = 0; } if (WindowsFontCache.Count == CacheSize) // No more room, update current index. { WindowsFont wfont = null; // Go through the existing fonts in the cache, and see if any // are not in use by a DC. If one isn't, replace that. If // all are in use, new up a new font and do not cache it. bool finished = false; int startIndex = currentIndex; int loopIndex = startIndex + 1; while (!finished) { if (loopIndex >= CacheSize) { loopIndex = 0; } if (loopIndex == startIndex) { finished = true; } wfont = WindowsFontCache[loopIndex].Value; if (!DeviceContexts.IsFontInUse(wfont)) { currentIndex = loopIndex; finished = true; break; } else { loopIndex++; wfont = null; } } if (wfont != null) { WindowsFontCache[currentIndex] = newEntry; winFont.OwnedByCacheManager = true; #if GDI_FONT_CACHE_TRACK Debug.WriteLine("Removing from cache: " + wfont); Debug.WriteLine("Adding to cache: " + winFont); #endif wfont.OwnedByCacheManager = false; wfont.Dispose(); } else { // do not cache font - caller is ALWAYS responsible for // disposing now. If it is owned by the CM, it will not // disposed. winFont.OwnedByCacheManager = false; #if GDI_FONT_CACHE_TRACK Debug.WriteLine("Creating uncached font: " + winFont); #endif } } else { winFont.OwnedByCacheManager = true; WindowsFontCache.Add(newEntry); #if GDI_FONT_CACHE_TRACK Debug.WriteLine("Adding to cache: " + winFont); #endif } return(winFont); }
public WindowsFont( string faceName, float size, FontStyle style, byte charSet, WindowsFontQuality fontQuality ) { Debug.Assert( size > 0.0f, "size has a negative value." ); const byte True = 1; const byte False = 0; this.logFont = new IntNativeMethods.LOGFONT(); // // Get the font height from the specified size. size is in point units and height in logical // units (pixels when using MM_TEXT) so we need to make the conversion using the number of // pixels per logical inch along the screen height. // int pixelsY = (int) Math.Ceiling( WindowsGraphicsCacheManager.MeasurementGraphics.DeviceContext.DpiY * size / 72); // 1 point = 1/72 inch.; // // The lfHeight represents the font cell height (line spacing) which includes the internal // leading; we specify a negative size value (in pixels) for the height so the font mapper // provides the closest match for the character height rather than the cell height (MSDN). // this.logFont.lfHeight = -pixelsY; this.logFont.lfFaceName = faceName != null ? faceName : defaultFaceName; this.logFont.lfCharSet = charSet; this.logFont.lfOutPrecision = IntNativeMethods.OUT_TT_PRECIS; this.logFont.lfQuality = (byte) fontQuality; this.logFont.lfWeight = (style & FontStyle.Bold) == FontStyle.Bold ? IntNativeMethods.FW_BOLD : IntNativeMethods.FW_NORMAL; this.logFont.lfItalic = (style & FontStyle.Italic) == FontStyle.Italic ? True : False; this.logFont.lfUnderline = (style & FontStyle.Underline) == FontStyle.Underline ? True : False; this.logFont.lfStrikeOut = (style & FontStyle.Strikeout) == FontStyle.Strikeout ? True : False; // Let the Size be recomputed to be consistent with the Height (there may be some precision loss coming from size to height). // this.fontSize = size; this.style = style; CreateFont(); }
public static WindowsFont FromFont(Font font, WindowsFontQuality fontQuality) { string familyName = font.FontFamily.Name; // Strip vertical-font mark from the name if needed. if (familyName != null && familyName.Length > 1 && familyName[0] == '@') { familyName = familyName.Substring(1); } // Note: Creating the WindowsFont from Font using a LOGFONT structure from GDI+ (Font.ToLogFont(logFont)) may sound like // a better choice (more accurate) for doing this but tests show that is not the case (see WindowsFontTests test suite), // the results are the same. Also, that approach has some issues when the Font is created in a different application // domain since the LOGFONT cannot be marshalled properly, see VSW#451960. // Now, creating it using the Font.SizeInPoints makes it GraphicsUnit-independent. return new WindowsFont(familyName, font.SizeInPoints, font.Style, font.GdiCharSet, fontQuality); }
public static WindowsFont GetWindowsFont(Font font, WindowsFontQuality fontQuality) { if( font == null ) { return null; } // First check if font is in the cache. int count = 0; int index = currentIndex; // Search by index of most recently added object. while( count < WindowsFontCache.Count ) { if (WindowsFontCache[index].Key.Equals(font)) // don't do shallow comparison, we could miss cloned fonts. { // We got a Font in the cache, let's see if we have a WindowsFont with the same quality as required by the caller. // WARNING: It is not expected that the WindowsFont is disposed externally since it is created by this class. Debug.Assert(WindowsFontCache[index].Value.Hfont != IntPtr.Zero, "Cached WindowsFont was disposed, enable GDI_FINALIZATION_WATCH to track who did it!"); WindowsFont wf = WindowsFontCache[index].Value; if(wf.Quality == fontQuality) { return wf; } } index--; count++; if( index < 0 ) { index = CacheSize - 1; } } // Font is not in the cache, let's add it. WindowsFont winFont = WindowsFont.FromFont(font, fontQuality); KeyValuePair<Font, WindowsFont> newEntry = new KeyValuePair<Font, WindowsFont>(font, winFont); currentIndex++; if (currentIndex == CacheSize) { currentIndex = 0; } if (WindowsFontCache.Count == CacheSize) // No more room, update current index. { WindowsFont wfont = null; // Go through the existing fonts in the cache, and see if any // are not in use by a DC. If one isn't, replace that. If // all are in use, new up a new font and do not cache it. bool finished = false; int startIndex = currentIndex; int loopIndex = startIndex + 1; while (!finished) { if (loopIndex >= CacheSize) { loopIndex = 0; } if (loopIndex == startIndex) { finished = true; } wfont = WindowsFontCache[loopIndex].Value; if (!DeviceContexts.IsFontInUse(wfont)) { currentIndex = loopIndex; finished = true; break; } else { loopIndex++; wfont = null; } } if (wfont != null) { WindowsFontCache[currentIndex] = newEntry; winFont.OwnedByCacheManager = true; #if GDI_FONT_CACHE_TRACK Debug.WriteLine("Removing from cache: " + wfont); Debug.WriteLine( "Adding to cache: " + winFont ); #endif wfont.OwnedByCacheManager = false; wfont.Dispose(); } else { // do not cache font - caller is ALWAYS responsible for // disposing now. If it is owned by the CM, it will not // disposed. winFont.OwnedByCacheManager = false; #if GDI_FONT_CACHE_TRACK Debug.WriteLine("Creating uncached font: " + winFont); #endif } } else { winFont.OwnedByCacheManager = true; WindowsFontCache.Add(newEntry); #if GDI_FONT_CACHE_TRACK Debug.WriteLine( "Adding to cache: " + winFont ); #endif } return winFont; }
public static WindowsFont FromFont(Font font, WindowsFontQuality fontQuality) { string name = font.FontFamily.Name; if (((name != null) && (name.Length > 1)) && (name[0] == '@')) { name = name.Substring(1); } return new WindowsFont(name, font.SizeInPoints, font.Style, font.GdiCharSet, fontQuality); }
public WindowsFont(string faceName, float size, FontStyle style, byte charSet, WindowsFontQuality fontQuality) { this.fontSize = -1f; this.logFont = new IntNativeMethods.LOGFONT(); int num = (int)Math.Ceiling((double)((WindowsGraphicsCacheManager.MeasurementGraphics.DeviceContext.DpiY * size) / 72f)); this.logFont.lfHeight = -num; this.logFont.lfFaceName = (faceName != null) ? faceName : "Microsoft Sans Serif"; this.logFont.lfCharSet = charSet; this.logFont.lfOutPrecision = 4; this.logFont.lfQuality = (byte)fontQuality; this.logFont.lfWeight = ((style & FontStyle.Bold) == FontStyle.Bold) ? 700 : 400; this.logFont.lfItalic = ((style & FontStyle.Italic) == FontStyle.Italic) ? ((byte)1) : ((byte)0); this.logFont.lfUnderline = ((style & FontStyle.Underline) == FontStyle.Underline) ? ((byte)1) : ((byte)0); this.logFont.lfStrikeOut = ((style & FontStyle.Strikeout) == FontStyle.Strikeout) ? ((byte)1) : ((byte)0); this.style = style; this.CreateFont(); }
public static WindowsFont GetWindowsFont(Font font, WindowsFontQuality fontQuality) { if (font == null) { return(null); } int num = 0; int currentIndex = WindowsGraphicsCacheManager.currentIndex; while (num < WindowsFontCache.Count) { KeyValuePair <Font, WindowsFont> pair2 = WindowsFontCache[currentIndex]; if (pair2.Key.Equals(font)) { KeyValuePair <Font, WindowsFont> pair3 = WindowsFontCache[currentIndex]; WindowsFont font2 = pair3.Value; if (font2.Quality == fontQuality) { return(font2); } } currentIndex--; num++; if (currentIndex < 0) { currentIndex = 9; } } WindowsFont font3 = WindowsFont.FromFont(font, fontQuality); KeyValuePair <Font, WindowsFont> item = new KeyValuePair <Font, WindowsFont>(font, font3); WindowsGraphicsCacheManager.currentIndex++; if (WindowsGraphicsCacheManager.currentIndex == 10) { WindowsGraphicsCacheManager.currentIndex = 0; } if (WindowsFontCache.Count != 10) { font3.OwnedByCacheManager = true; WindowsFontCache.Add(item); return(font3); } WindowsFont wf = null; bool flag = false; int num3 = WindowsGraphicsCacheManager.currentIndex; int num4 = num3 + 1; while (!flag) { if (num4 >= 10) { num4 = 0; } if (num4 == num3) { flag = true; } KeyValuePair <Font, WindowsFont> pair4 = WindowsFontCache[num4]; wf = pair4.Value; if (!DeviceContexts.IsFontInUse(wf)) { WindowsGraphicsCacheManager.currentIndex = num4; flag = true; break; } num4++; wf = null; } if (wf != null) { WindowsFontCache[WindowsGraphicsCacheManager.currentIndex] = item; font3.OwnedByCacheManager = true; wf.OwnedByCacheManager = false; wf.Dispose(); return(font3); } font3.OwnedByCacheManager = false; return(font3); }
/// <summary> /// Contructor to construct font from a face name, a desired size in points and with the specified style /// and character set. The screen dc is used for calculating the font em height. /// </summary>> public WindowsFont(string faceName, float size, FontStyle style, byte charSet, WindowsFontQuality fontQuality) { Debug.Assert(size > 0.0f, "size has a negative value."); const byte True = 1; const byte False = 0; logFont = new IntNativeMethods.LOGFONT(); // // Get the font height from the specified size. size is in point units and height in logical // units (pixels when using MM_TEXT) so we need to make the conversion using the number of // pixels per logical inch along the screen height. // int pixelsY = (int)Math.Ceiling(WindowsGraphicsCacheManager.MeasurementGraphics.DeviceContext.DpiY * size / 72); // 1 point = 1/72 inch.; // // The lfHeight represents the font cell height (line spacing) which includes the internal // leading; we specify a negative size value (in pixels) for the height so the font mapper // provides the closest match for the character height rather than the cell height (MSDN). // logFont.lfHeight = -pixelsY; logFont.lfFaceName = faceName ?? defaultFaceName; logFont.lfCharSet = charSet; logFont.lfOutPrecision = IntNativeMethods.OUT_TT_PRECIS; logFont.lfQuality = (byte)fontQuality; logFont.lfWeight = (style & FontStyle.Bold) == FontStyle.Bold ? IntNativeMethods.FW_BOLD : IntNativeMethods.FW_NORMAL; logFont.lfItalic = (style & FontStyle.Italic) == FontStyle.Italic ? True : False; logFont.lfUnderline = (style & FontStyle.Underline) == FontStyle.Underline ? True : False; logFont.lfStrikeOut = (style & FontStyle.Strikeout) == FontStyle.Strikeout ? True : False; // Let the Size be recomputed to be consistent with the Height (there may be some precision loss coming from size to height). // this.fontSize = size; this.style = style; CreateFont(); }
public static WindowsFont GetWindowsFont(Font font, WindowsFontQuality fontQuality) { if (font == null) { return null; } int num = 0; int currentIndex = WindowsGraphicsCacheManager.currentIndex; while (num < WindowsFontCache.Count) { KeyValuePair<Font, WindowsFont> pair2 = WindowsFontCache[currentIndex]; if (pair2.Key.Equals(font)) { KeyValuePair<Font, WindowsFont> pair3 = WindowsFontCache[currentIndex]; WindowsFont font2 = pair3.Value; if (font2.Quality == fontQuality) { return font2; } } currentIndex--; num++; if (currentIndex < 0) { currentIndex = 9; } } WindowsFont font3 = WindowsFont.FromFont(font, fontQuality); KeyValuePair<Font, WindowsFont> item = new KeyValuePair<Font, WindowsFont>(font, font3); WindowsGraphicsCacheManager.currentIndex++; if (WindowsGraphicsCacheManager.currentIndex == 10) { WindowsGraphicsCacheManager.currentIndex = 0; } if (WindowsFontCache.Count != 10) { font3.OwnedByCacheManager = true; WindowsFontCache.Add(item); return font3; } WindowsFont wf = null; bool flag = false; int num3 = WindowsGraphicsCacheManager.currentIndex; int num4 = num3 + 1; while (!flag) { if (num4 >= 10) { num4 = 0; } if (num4 == num3) { flag = true; } KeyValuePair<Font, WindowsFont> pair4 = WindowsFontCache[num4]; wf = pair4.Value; if (!DeviceContexts.IsFontInUse(wf)) { WindowsGraphicsCacheManager.currentIndex = num4; flag = true; break; } num4++; wf = null; } if (wf != null) { WindowsFontCache[WindowsGraphicsCacheManager.currentIndex] = item; font3.OwnedByCacheManager = true; wf.OwnedByCacheManager = false; wf.Dispose(); return font3; } font3.OwnedByCacheManager = false; return font3; }