MeasureString() public method

public MeasureString ( string text, Font font ) : SizeF
text string
font System.Drawing.Font
return System.Drawing.SizeF
示例#1
0
文件: SvgText.cs 项目: messani/SVG
        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);
                }
            }
        }