Beispiel #1
0
        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));
            }
        }
Beispiel #2
0
        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));
            }
        }