Example #1
0
    // TODO I believe this function measures how many characters will fit into the current line given the bounds.
    public int MeasureLineWrap(TigFont font, TigTextStyle style, ReadOnlySpan <char> text, Rectangle bounds)
    {
        if (bounds.Width == 0)
        {
            return(text.Length);
        }

        Span <char> textCopy = stackalloc char[text.Length];

        text.CopyTo(textCopy);

        var iterator  = new LayoutRunIterator(textCopy, font, bounds, style);
        int endOfLine = 0;

        while (iterator.MoveToNextRun(out var run))
        {
            if (run.Y <= bounds.Y)
            {
                endOfLine = run.End;
            }
            else
            {
                break;
            }
        }

        return(endOfLine);
    }
Example #2
0
    private void LayoutAndDrawVanilla(
        Span <char> text,
        TigFont font,
        ref Rectangle extents,
        TigTextStyle style)
    {
        var extentsWidth  = extents.Width;
        var extentsHeight = extents.Height;

        if (extentsWidth == 0)
        {
            var metrics = new TigFontMetrics();
            metrics.width  = extents.Width;
            metrics.height = extents.Height;
            Tig.Fonts.Measure(style, text, ref metrics);

            extents.Width  = metrics.width;
            extents.Height = metrics.height;
            extentsWidth   = metrics.width;
            extentsHeight  = metrics.height;
        }

        if ((style.flags & (TigTextStyleFlag.TTSF_BACKGROUND | TigTextStyleFlag.TTSF_BORDER)) != 0)
        {
            var rect = new Rectangle(
                extents.X,
                extents.Y,
                Math.Max(extentsWidth, extents.Width),
                Math.Max(extentsHeight, extents.Height)
                );
            DrawBackgroundOrOutline(rect, style);
        }

        var iterator = new LayoutRunIterator(text, font, extents, style);

        while (iterator.MoveToNextRun(out var run))
        {
            if (run.Truncated)
            {
                _renderer.RenderRun(
                    "...",
                    run.X,
                    run.Y,
                    run.Bounds,
                    style,
                    font);
            }
            else
            {
                _renderer.RenderRun(
                    text.Slice(run.Start, run.End - run.Start),
                    run.X,
                    run.Y,
                    run.Bounds,
                    style,
                    font);
            }
        }
    }