public int Add(TextRun textRun)
        {
            if (textRun == null)
            {
                throw new ArgumentNullException("textRun");
            }

            var result = this._textRuns.Add(textRun);

            this._textFlow.InvalidateMeasure();
            return(result);
        }
        public override void OnRender(DrawingContext dc)
        {
            if ((this._lineCache != null) && (this._lineCache.Count != 0))
            {
                int num3;
                int num4;
                int count = this._lineCache.Count;
                int num2  = 0;
                base.GetRenderSize(out num3, out num4);
                for (int i = this._currentLine; i < count; i++)
                {
                    int      num6;
                    TextLine line = (TextLine)this._lineCache[i];
                    if ((num2 + line.Height) > num4)
                    {
                        break;
                    }
                    TextRun[] runs = line.Runs;
                    switch (this._alignment)
                    {
                    case GHIElectronics.TinyCLR.UI.Media.TextAlignment.Left:
                        num6 = 0;
                        break;

                    case GHIElectronics.TinyCLR.UI.Media.TextAlignment.Center:
                        num6 = (num3 - line.Width) >> 1;
                        break;

                    case GHIElectronics.TinyCLR.UI.Media.TextAlignment.Right:
                        num6 = num3 - line.Width;
                        break;

                    default:
                        throw new NotSupportedException();
                    }
                    for (int j = 0; j < runs.Length; j++)
                    {
                        int     num8;
                        int     num9;
                        TextRun run = runs[j];
                        run.GetSize(out num8, out num9);
                        int y = (num2 + line.Baseline) - run.Font.Ascent;
                        dc.DrawText(run.Text, run.Font, run.ForeColor, num6, y);
                        num6 += num8;
                    }
                    num2 += line.Height;
                }
            }
        }
        internal ArrayList SplitLines(int availableWidth)
        {
            int       num  = 0;
            ArrayList list = new ArrayList();

            for (int i = 0; i < this.TextRuns.Count; i++)
            {
                list.Add(this.TextRuns[i]);
            }
            ArrayList list2 = new ArrayList();
            ArrayList runs  = new ArrayList();

            while (list.Count > 0)
            {
                bool    flag = false;
                TextRun run  = (TextRun)list[0];
                list.RemoveAt(0);
                if (run.IsEndOfLine)
                {
                    flag = true;
                }
                else
                {
                    int num3;
                    int num4;
                    run.GetSize(out num3, out num4);
                    num += num3;
                    runs.Add(run);
                    if (num > availableWidth)
                    {
                        bool emergencyBreak = runs.Count == 1;
                        if (run.Text.Length > 1)
                        {
                            TextRun run2;
                            TextRun run3;
                            runs.Remove(run);
                            if (run.Break(num3 - (num - availableWidth), out run2, out run3, emergencyBreak))
                            {
                                if (run2 != null)
                                {
                                    runs.Add(run2);
                                }
                                if (run3 != null)
                                {
                                    list.Insert(0, run3);
                                }
                            }
                            else if (!emergencyBreak)
                            {
                                list.Insert(0, run);
                            }
                        }
                        else if (!emergencyBreak)
                        {
                            runs.Remove(run);
                            list.Insert(0, run);
                        }
                        flag = true;
                    }
                    if ((num >= availableWidth) || (list.Count == 0))
                    {
                        flag = true;
                    }
                }
                if (flag)
                {
                    int height   = 0;
                    int baseline = 0;
                    int count    = runs.Count;
                    if (count > 0)
                    {
                        for (int j = 0; j < count; j++)
                        {
                            Font font = ((TextRun)runs[j]).Font;
                            int  num9 = font.Height + font.ExternalLeading;
                            if (num9 > height)
                            {
                                height   = num9;
                                baseline = font.Ascent;
                            }
                        }
                        list2.Add(new TextLine(runs, height, baseline));
                    }
                    else
                    {
                        height = (list2.Count > 0) ? ((TextLine)list2[list2.Count - 1]).Height : 10;
                        list2.Add(new TextLine(height));
                    }
                    runs.Clear();
                    num = 0;
                }
            }
            return(list2);
        }
        internal bool Break(int availableWidth, out TextRun run1, out TextRun run2, bool emergencyBreak)
        {
            Debug.Assert(availableWidth > 0);
            Debug.Assert(availableWidth < this._width);
            Debug.Assert(this.Text.Length > 1);

            var leftBreak  = -1;
            var rightBreak = -1;

            // Try to find a candidate position for breaking
            //
            var foundBreak = false;

            while (!foundBreak)
            {
                // Try adding a word
                //
                var indexOfNextSpace = this.Text.IndexOf(' ', leftBreak + 1);

                foundBreak = (indexOfNextSpace == -1);

                if (!foundBreak)
                {
                    this.Font.ComputeExtent(this.Text.Substring(0, indexOfNextSpace), out var w, out var h);
                    foundBreak = (w >= availableWidth);
                    if (w == availableWidth)
                    {
                        leftBreak = indexOfNextSpace;
                    }
                }

                if (foundBreak)
                {
                    if (leftBreak >= 0)
                    {
                        rightBreak = leftBreak + 1;
                    }
                    else if (emergencyBreak)
                    {
                        leftBreak  = EmergencyBreak(availableWidth);
                        rightBreak = leftBreak;
                    }
                    else
                    {
                        run1 = run2 = null;
                        return(false);
                    }
                }
                else
                {
                    leftBreak = indexOfNextSpace;
                }
            }

            var first = this.Text.Substring(0, leftBreak).TrimEnd(' ');

            // Split the text run
            //
            run1 = null;
            if (first.Length > 0)
            {
                run1 = new TextRun(first, this.Font, this.ForeColor);
            }

            run2 = null;
            if (rightBreak < this.Text.Length)
            {
                var run2String = this.Text.Substring(rightBreak).TrimStart(' ');

                // if run2 is all spaces (length == 0 after trim), we'll leave run2 as null
                if (run2String.Length > 0)
                {
                    run2 = new TextRun(run2String, this.Font, this.ForeColor);
                }
            }

            return(true);
        }
 public void Remove(TextRun run)
 {
     this._textRuns.Remove(run);
     this._textFlow.InvalidateMeasure();
 }
 public void Insert(int index, TextRun run)
 {
     this._textRuns.Insert(index, run);
     this._textFlow.InvalidateMeasure();
 }
 public int IndexOf(TextRun run) => this._textRuns.IndexOf(run);
 public bool Contains(TextRun run) => this._textRuns.Contains(run);
        internal bool Break(int availableWidth, out TextRun run1, out TextRun run2, bool emergencyBreak)
        {
            int  length     = -1;
            int  startIndex = -1;
            bool flag       = false;

            while (!flag)
            {
                int index = this.Text.IndexOf(' ', length + 1);
                flag = index == -1;
                if (!flag)
                {
                    int num4;
                    int num5;
                    this.Font.ComputeExtent(this.Text.Substring(0, index), out num4, out num5);
                    flag = num4 >= availableWidth;
                    if (num4 == availableWidth)
                    {
                        length = index;
                    }
                }
                if (flag)
                {
                    if (length < 0)
                    {
                        if (!emergencyBreak)
                        {
                            TextRun run;
                            run2 = (TextRun)(run = null);
                            run1 = run;
                            return(false);
                        }
                        length     = this.EmergencyBreak(availableWidth);
                        startIndex = length;
                    }
                    else
                    {
                        startIndex = length + 1;
                    }
                }
                else
                {
                    length = index;
                }
            }
            char[] trimChars = new char[] { ' ' };
            string text      = this.Text.Substring(0, length).TrimEnd(trimChars);

            run1 = null;
            if (text.Length > 0)
            {
                run1 = new TextRun(text, this.Font, this.ForeColor);
            }
            run2 = null;
            if (startIndex < this.Text.Length)
            {
                char[] chArray2 = new char[] { ' ' };
                string str2     = this.Text.Substring(startIndex).TrimStart(chArray2);
                if (str2.Length > 0)
                {
                    run2 = new TextRun(str2, this.Font, this.ForeColor);
                }
            }
            return(true);
        }
Exemple #10
0
 public int IndexOf(TextRun run)
 {
     return(this._textRuns.IndexOf(run));
 }
Exemple #11
0
 public bool Contains(TextRun run)
 {
     return(this._textRuns.Contains(run));
 }