title: Paint a complex string
body: <p>I have such a code, that I try to use to paint regular text and url in it. First I extract urls from text and create Ranges for each part.</p>
  
  <pre><code>private static readonly Font TextFont = new Font("Verdana", 10);
  
          private readonly Regex RgUrl = new Regex(@"((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&amp;=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&amp;=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&amp;;%@.\w_]*)#?(?:[\w]*))?)",
      RegexOptions.Multiline | RegexOptions.Compiled);
  
  
          private void PaintComplexString(Graphics g)
          {
              List&lt;CharacterRange&gt; ranges = new List&lt;CharacterRange&gt;();
              int start = 0;
  
              // create separate ranges for URLs
              foreach (Match match in RgUrl.Matches(_text))
              {
                  if (start != match.Index)
                      ranges.Add(new CharacterRange(start, match.Index - start));
                  ranges.Add(new CharacterRange(match.Index, match.Length));
                  start = match.Index + match.Length;
              }
  
              if (start != _text.Length)
                  ranges.Add(new CharacterRange(start, _text.Length - start));
  
              StringFormat format = new StringFormat(StringFormat.GenericTypographic);
              format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoClip;
              format.SetMeasurableCharacterRanges(ranges.ToArray());
              Region[] regions = g.MeasureCharacterRanges(_text, TextFont, ClientRectangle, format);
  
              int index = 0;
              foreach (Region region in regions)
              {
                  string txt = _text.Substring(ranges[index].First, ranges[index].Length);
                  Debug.WriteLine(index + "*" + txt + "*");
                  Rectangle rc = Rectangle.Round(region.GetBounds(g));
                  g.DrawString(txt, TextFont, Brushes.Chocolate, rc, format);
                  g.DrawRectangle(Pens.BlueViolet, rc);
                  index++;
              }
          }
  </code></pre>
  
  <p>When the width of ClientRect is big enough to accommodate full text then everything paints ok. When I try to make form thinner and expecting the text to extent vertically everything works pretty bad. </p>
  
  <p>What could be wrong here?
  Thx</p>