Esempio n. 1
0
 public AndroidFontMetrics(Paint paint)
 {
     _widths = new float[NumWidths];
     paint.GetTextWidths (_chars, 0, NumWidths, _widths);
     Ascent = (int)(Math.Abs (paint.Ascent ()) + 0.5f);
     Descent = (int)(paint.Descent ()/2 + 0.5f);
     Height = Ascent;
 }
Esempio n. 2
0
        public void Wrap(DroidTextLayoutBackend layout, AG.Paint paint)
        {
            var text = layout.Text;

            var metrics = paint.GetFontMetrics();

            Baseline   = -metrics.Top;
            LineHeight = -metrics.Ascent + metrics.Descent;
            LineY      = -metrics.Ascent;         //- (metrics.Ascent + metrics.Descent);

            var charWidths = new float[text.Length];

            paint.GetTextWidths(text, charWidths);

            var iLf = text.IndexOfAny(new char[] { '\n', '\r' });

            HasLineFeed = iLf >= 0;

            var textWidth = charWidths.Sum();

            if (!HasLineFeed && (layout.Height <= 0 || layout.Height <= LineHeight) && (layout.Width <= 0 || textWidth <= layout.Width))
            {
                LineWidth = textWidth;
                SingleLine(this);
            }
            else
            {
                MaxHeight = PreferedSize.Height > 0 ? PreferedSize.Height : (layout.Height <= 0 ? float.MaxValue : layout.Height);
                MaxWidth  = PreferedSize.Width > 0 ? PreferedSize.Width : (layout.Width <= 0 ? textWidth : layout.Width);

                CursorPos = 0;
                LineStart = 0;

                while (LineY <= MaxHeight && CursorPos < text.Length)
                {
                    LineWidth = 0f;
                    var whitePosY = -1f;
                    var whitepos  = -1;
                    var newLine   = false;

                    while (CursorPos < text.Length)
                    {
                        newLine = text [CursorPos] == '\n';

                        if (text [CursorPos] == '\r')
                        {
                            CursorPos++;
                            continue;
                        }

                        if (newLine)
                        {
                            break;
                        }

                        if (char.IsWhiteSpace(text [CursorPos]))
                        {
                            whitepos  = CursorPos;
                            whitePosY = LineWidth;
                        }
                        LineWidth += charWidths [CursorPos];

                        if (LineWidth > MaxWidth)
                        {
                            if (whitepos > 0)
                            {
                                CursorPos = whitepos;
                                LineWidth = whitePosY;
                            }
                            break;
                        }
                        CursorPos++;
                    }

                    Action line = () => {
                        MultiLine(this);
                        LineY += LineHeight;
                    };

                    line();

                    CursorPos++;
                    LineStart = CursorPos;

                    if (newLine && CursorPos == text.Length)
                    {
                        line();
                    }
                }
            }
        }