1. 程式人生 > >Unity DF-GUI 中文輸入以及自動換行

Unity DF-GUI 中文輸入以及自動換行

public class dfDynamicFont : dfFontBase{    public class DynamicFontRenderer : dfFontRendererBase    {        private dfList<LineRenderInfo> calculateLinebreaks()        {            try            {                //@Profiler.BeginSample( "Calculate line breaks" );                if( lines != null )                {                    return lines;                }                lines = dfList<LineRenderInfo>.Obtain();                var font = (dfDynamicFont)Font;                var lastBreak = 0;                var startIndex = 0;                var index = 0;                var lineWidth = 0;                var lineHeight = font.Baseline * TextScale;                while( index < tokens.Count && lines.Count * lineHeight <= MaxSize.y + lineHeight )                {                    var token = tokens[ index ];                    var type = token.TokenType;                    if( type == dfMarkupTokenType.Newline )                    {                        lines.Add( LineRenderInfo.Obtain( startIndex, index ) );                        startIndex = lastBreak = ++index;                        lineWidth = 0;                        continue;                    }                    var tokenWidth = Mathf.CeilToInt( token.Width );                    var canWrap =                        WordWrap &&                        lastBreak > startIndex &&                        (                            type == dfMarkupTokenType.Text ||                            ( type == dfMarkupTokenType.StartTag && token.Matches( "sprite" ) )                        );                    if( canWrap && lineWidth + tokenWidth >= MaxSize.x )                    {                        if( lastBreak > startIndex )                        {                            //lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) );                            // 中文不要減1,不然會缺字                            lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak ) );                            startIndex = index = ++lastBreak;                            lineWidth = 0;                        }                        else                        {                            lines.Add( LineRenderInfo.Obtain( startIndex, lastBreak - 1 ) );                            startIndex = lastBreak = ++index;                            lineWidth = 0;                        }                        continue;                    }                    if( type == dfMarkupTokenType.Whitespace )                    {                        lastBreak = index;                    }                    // 增加中文換行                    else if ( type == dfMarkupTokenType.Text && token.Length == 1 && token.Value[0].IsChinese() )                    {                        lastBreak = index;                    }
                    lineWidth += tokenWidth;                    index += 1;                }                if( startIndex < tokens.Count )                {                    lines.Add( LineRenderInfo.Obtain( startIndex, tokens.Count - 1 ) );                }                for( int i = 0; i < lines.Count; i++ )                {                    calculateLineSize( lines[ i ] );                }                return lines;            }            finally            {                //@Profiler.EndSample();            }        }    }}