public static UIWordWrapOutput WordWrap(string text, int width, TextStyle style, Vector2 scale)
        {
            var result = new UIWordWrapOutput();
            result.Lines = new List<string>();
            var textLines = new string[] {text}; //only support single line for now, since we're only using this utility function for captions
            int maxWidth = 0;
            int curpos = 0;
            var positions = new List<int>();
            for (var l=0; l<textLines.Length; l++) {
                List<string> words = textLines[l].Split(' ').ToList();

                while (words.Count > 0) {
                    var lineBuffer = new List<string>();
                    int i = 0;
                    for (i=0; i<words.Count; i++) {
                        lineBuffer.Add(words[i]);
                        var str = JoinWordList(lineBuffer);      //(lineBuffer.concat([words[i]])).join(" ");
                        int w = (int)(style.SpriteFont.MeasureString(str).X * scale.X);
                        if (w > width) {
                            lineBuffer.RemoveAt(lineBuffer.Count-1);
                            if (lineBuffer.Count == 0) {
                                for (var j=words[i].Length-1; j>0; j--) {
                                    var str2 = words[i].Substring(0, j);
                                    var w2 = (int)(style.SpriteFont.MeasureString(str2).X * scale.X);
                                    if (w2 <= width) {
                                        curpos += j;
                                        lineBuffer.Add(words[i].Substring(0, j));
                                        words[i] = words[i].Substring(j);
                                        if (w > maxWidth) maxWidth = w;
                                        break;
                                    }
                                }
                            }
                            break;
                        } else {
                            if (w > maxWidth) maxWidth = w;
                            curpos += words[i].Length + 1;
                        }
                    }
                    result.Lines.Add(JoinWordList(lineBuffer));
                    positions.Add(curpos);
                    words.RemoveRange(0, i);

                }
                //curpos++;
            }
            result.Positions = positions;
            result.MaxWidth = maxWidth;
            return result;
        }
Exemple #2
0
        public static UIWordWrapOutput WordWrap(string text, int width, TextStyle style, Vector2 scale)
        {
            var result = new UIWordWrapOutput();

            result.Lines = new List <string>();
            var textLines = new string[] { text };       //only support single line for now, since we're only using this utility function for captions
            int maxWidth  = 0;
            int curpos    = 0;
            var positions = new List <int>();

            for (var l = 0; l < textLines.Length; l++)
            {
                List <string> words = textLines[l].Split(' ').ToList();

                while (words.Count > 0)
                {
                    var lineBuffer = new List <string>();
                    int i          = 0;
                    for (i = 0; i < words.Count; i++)
                    {
                        lineBuffer.Add(words[i]);
                        var str = JoinWordList(lineBuffer);                          //(lineBuffer.concat([words[i]])).join(" ");
                        int w   = (int)(style.SpriteFont.MeasureString(str).X *scale.X);
                        if (w > width)
                        {
                            lineBuffer.RemoveAt(lineBuffer.Count - 1);
                            if (lineBuffer.Count == 0)
                            {
                                for (var j = words[i].Length - 1; j > 0; j--)
                                {
                                    var str2 = words[i].Substring(0, j);
                                    var w2   = (int)(style.SpriteFont.MeasureString(str2).X *scale.X);
                                    if (w2 <= width)
                                    {
                                        curpos += j;
                                        lineBuffer.Add(words[i].Substring(0, j));
                                        words[i] = words[i].Substring(j);
                                        if (w > maxWidth)
                                        {
                                            maxWidth = w;
                                        }
                                        break;
                                    }
                                }
                            }
                            break;
                        }
                        else
                        {
                            if (w > maxWidth)
                            {
                                maxWidth = w;
                            }
                            curpos += words[i].Length + 1;
                        }
                    }
                    result.Lines.Add(JoinWordList(lineBuffer));
                    positions.Add(curpos);
                    words.RemoveRange(0, i);
                }
                //curpos++;
            }
            result.Positions = positions;
            result.MaxWidth  = maxWidth;
            return(result);
        }