public static SizeF MeasureText (Graphics g, string text, Font font) { // Due to the way the TextBox currently works, it measures each // character one at a time. And it does this alot. So here we // are implementing a cache for each font/character combination // measurement. Since the number of fonts and number of characters // used tends to be small, this is a good performance gain for // not too much memory. if (text.Length == 1) { // If g.VisibleClipBounds is {X=0, Y=0, Width=1, Height=1}, then some characters // (in some fonts for some point sizes) return a different width then when the // VisibleClipBounds has a different (usually but not always more reasonable) value. // This state of the Graphics object can occur during initialization of text boxes // with preset Text values. See https://bugzilla.xamarin.com/show_bug.cgi?id=26258 // for more details. string sep; var bounds = g.VisibleClipBounds; if (bounds.Width == 1 && bounds.Height == 1 && bounds.X == 0 && bounds.Y == 0) sep = "-1x1|"; else sep = "|"; string key = font.GetHashCode ().ToString () + sep + text; if (measure_cache.ContainsKey (key)) { return (SizeF)measure_cache[key]; } else { SizeF size; if (!use_textrenderer) size = g.MeasureString (text, font, 10000, sf_nonprinting); else size = TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false); measure_cache[key] = size; return size; } } if (!use_textrenderer) return g.MeasureString (text, font, 10000, sf_nonprinting); else return TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false); }
public static SizeF MeasureText (Graphics g, string text, Font font) { // Due to the way the TextBox currently works, it measures each // character one at a time. And it does this alot. So here we // are implementing a cache for each font/character combination // measurement. Since the number of fonts and number of characters // used tends to be small, this is a good performance gain for // not too much memory. if (text.Length == 1) { string key = font.GetHashCode ().ToString () + "|" + text; if (measure_cache.ContainsKey (key)) { return (SizeF)measure_cache[key]; } else { SizeF size; if (!use_textrenderer) size = g.MeasureString (text, font, 10000, sf_nonprinting); else size = TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false); measure_cache[key] = size; return size; } } if (!use_textrenderer) return g.MeasureString (text, font, 10000, sf_nonprinting); else return TextRenderer.MeasureTextInternal (g, text, font, Size.Empty, TextFormatFlags.NoPadding | TextFormatFlags.NoPrefix, false); }
public void GetHashCode_NameDiffers_HashesNotEqual() { Font f1 = new Font("Arial", 8.25F, GraphicsUnit.Point); Font f2 = new Font("Courier New", 8.25F, GraphicsUnit.Point); Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(), "Hashcodes should differ if _name member differs"); }
public void GetHashCode_StyleEqualsGdiCharSet_HashesNotEqual() { Font f1 = new Font("Arial", 8.25F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0))); Font f2 = new Font("Arial", 8.25F, FontStyle.Bold, GraphicsUnit.Point, ((byte)(1))); Assert.IsFalse(f1.GetHashCode() == f2.GetHashCode(), "Hashcodes should differ if _style member differs"); }
public void FontUniqueHashCode () { Font f1 = new Font ("Arial", 14); Font f2 = new Font ("Arial", 12); Font f3 = new Font (f1, FontStyle.Bold); Assert.IsFalse (f1.GetHashCode () == f2.GetHashCode (), "1) Fonts with different sizes should have different HashCodes"); Assert.IsFalse (f1.GetHashCode () == f3.GetHashCode (), "2) Fonts with different styles should have different HashCodes"); }