/// <summary>
        /// Measures the size of the specified substring of text when rendered using this font.
        /// </summary>
        /// <param name="text">The text to measure.</param>
        /// <param name="start">The index of the first character of the substring to measure.</param>
        /// <param name="count">The number of characters in the substring to measure.</param>
        /// <returns>The size of the specified substring of text when rendered using this font.</returns>
        public Size2 MeasureString(String text, Int32 start, Int32 count)
        {
            Contract.EnsureRange(start >= 0 && start < text.Length, "start");
            Contract.EnsureRange(count >= 0 && start + count <= text.Length, "count");

            var source = new StringSource(text);
            return MeasureString(ref source, start, count);
        }
Example #2
0
        /// <summary>
        /// Measures the size of the specified substring of text when rendered using this font.
        /// </summary>
        /// <param name="source">The text to measure.</param>
        /// <param name="start">The index of the first character of the substring to measure.</param>
        /// <param name="count">The number of characters in the substring to measure.</param>
        /// <returns>The size of the specified substring of text when rendered using this font.</returns>
        internal Size2 MeasureString(ref StringSource source, Int32 start, Int32 count)
        {
            var cx = 0;
            var cy = 0;
            for (int i = 0; i < count; i++)
            {
                var character = source[start + i];
                switch (character)
                {
                    case '\r':
                        continue;

                    case '\n':
                        cx = 0;
                        cy = cy + LineSpacing;
                        continue;

                    case '\t':
                        cx = cx + TabWidth;
                        continue;
                }
                cx += MeasureGlyph(ref source, start + i).Width;
            }

            return new Size2(cx, cy + LineSpacing);
        }
Example #3
0
        /// <summary>
        /// Measures the specified glyph in a string, taking kerning into account.
        /// </summary>
        /// <param name="source">The text that contains the glyph to measure.</param>
        /// <param name="ix">The index of the glyph to measure.</param>
        /// <returns>The size of the specified glyph.</returns>
        internal Size2 MeasureGlyph(ref StringSource source, Int32 ix)
        {
            var c1 = source[ix];
            switch (c1)
            {
                case '\n':
                    return new Size2(0, LineSpacing);

                case '\t':
                    return new Size2(TabWidth, LineSpacing);

                default:
                    var c2 = (ix + 1 < source.Length) ? source[ix + 1] : (Char?)null;
                    var glyph = glyphs[c1];
                    var offset = c2.HasValue ? kerning.Get(c1, c2.GetValueOrDefault()) : 0;
                    return new Size2(glyph.Width + offset, glyph.Height);
            }
        }
Example #4
0
 /// <summary>
 /// Measures the size of the specified string of text when rendered using this font.
 /// </summary>
 /// <param name="text">The text to measure.</param>
 /// <returns>The size of the specified string of text when rendered using this font.</returns>
 public Size2 MeasureString(StringSegment text)
 {
     var source = new StringSource(text);
     return MeasureString(ref source, 0, text.Length);
 }
Example #5
0
 /// <summary>
 /// Measures the specified glyph in a string, taking kerning into account.
 /// </summary>
 /// <param name="text">The text that contains the glyph to measure.</param>
 /// <param name="ix">The index of the glyph to measure.</param>
 /// <returns>The size of the specified glyph.</returns>
 public Size2 MeasureGlyph(StringSegment text, Int32 ix)
 {
     var source = new StringSource(text);
     return MeasureGlyph(ref source, ix);
 }
Example #6
0
        /// <summary>
        /// Measures the size of the specified string of text when rendered using this font.
        /// </summary>
        /// <param name="text">The text to measure.</param>
        /// <returns>The size of the specified string of text when rendered using this font.</returns>
        public Size2 MeasureString(StringBuilder text)
        {
            var source = new StringSource(text);

            return(MeasureString(ref source, 0, text.Length));
        }
Example #7
0
        /// <summary>
        /// Measures the specified glyph in a string, taking kerning into account.
        /// </summary>
        /// <param name="text">The text that contains the glyph to measure.</param>
        /// <param name="ix">The index of the glyph to measure.</param>
        /// <returns>The size of the specified glyph.</returns>
        public Size2 MeasureGlyph(StringSegment text, Int32 ix)
        {
            var source = new StringSource(text);

            return(MeasureGlyph(ref source, ix));
        }