Svg.SvgRenderer.MeasureString C# (CSharp) Method

MeasureString() public method

public MeasureString ( string text, Font font ) : SizeF
text string
font System.Drawing.Font
return System.Drawing.SizeF
        public SizeF MeasureString(string text, Font font)
        {
        	var ff = font.FontFamily;
        	float lineSpace = ff.GetLineSpacing(font.Style);
        	float ascent = ff.GetCellAscent(font.Style);
        	float baseline =  font.GetHeight(this._innerGraphics) * ascent / lineSpace;
        	
        	StringFormat format = StringFormat.GenericTypographic;
        	format.SetMeasurableCharacterRanges(new CharacterRange[]{new CharacterRange(0, text.Length)});
        	Region[] r = this._innerGraphics.MeasureCharacterRanges(text, font, new Rectangle(0, 0, 1000, 1000), format);
        	RectangleF rect = r[0].GetBounds(this._innerGraphics);
        	
        	return new SizeF(rect.Width, baseline);
        }
    }

Usage Example

Esempio n. 1
0
        private void DrawString(GraphicsPath path, SvgUnit x, SvgUnit y, SvgUnit dx, SvgUnit dy, Font font, float fontSize, string text)
        {
            PointF location = PointF.Empty;
            SizeF  stringBounds;

            lock (_stringMeasure)
            {
                stringBounds = _stringMeasure.MeasureString(text, font);
            }

            float xToDevice = x.ToDeviceValue(this) + dx.ToDeviceValue(this);
            float yToDevice = y.ToDeviceValue(this, true) + dy.ToDeviceValue(this, true);

            // Minus FontSize because the x/y coords mark the bottom left, not bottom top.
            switch (this.TextAnchor)
            {
            case SvgTextAnchor.Start:
                location = new PointF(xToDevice, yToDevice - stringBounds.Height);
                break;

            case SvgTextAnchor.Middle:
                location = new PointF(xToDevice - (stringBounds.Width / 2), yToDevice - stringBounds.Height);
                break;

            case SvgTextAnchor.End:
                location = new PointF(xToDevice - stringBounds.Width, yToDevice - stringBounds.Height);
                break;
            }

            // No way to do letter-spacing or word-spacing, so do manually
            if (this.LetterSpacing.Value > 0.0f || this.WordSpacing.Value > 0.0f)
            {
                // Cut up into words, or just leave as required
                string[] words         = (this.WordSpacing.Value > 0.0f) ? text.Split(' ') : new string[] { text };
                float    wordSpacing   = this.WordSpacing.ToDeviceValue(this);
                float    letterSpacing = this.LetterSpacing.ToDeviceValue(this);
                float    start         = this.X.ToDeviceValue(this);

                foreach (string word in words)
                {
                    // Only do if there is line spacing, just write the word otherwise
                    if (this.LetterSpacing.Value > 0.0f)
                    {
                        char[] characters = word.ToCharArray();
                        foreach (char currentCharacter in characters)
                        {
                            path.AddString(currentCharacter.ToString(), new FontFamily(this._fontFamily), (int)font.Style, fontSize, location, StringFormat.GenericTypographic);
                            location = new PointF(path.GetBounds().Width + start + letterSpacing, location.Y);
                        }
                    }
                    else
                    {
                        path.AddString(word, new FontFamily(this._fontFamily), (int)font.Style, fontSize, location, StringFormat.GenericTypographic);
                    }

                    // Move the location of the word to be written along
                    location = new PointF(path.GetBounds().Width + start + wordSpacing, location.Y);
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(text))
                {
                    path.AddString(text, new FontFamily(this._fontFamily), (int)font.Style, fontSize, location, StringFormat.GenericTypographic);
                }
            }
        }