Exemple #1
0
        private TextBoxContext GetParagraphAndRunIndex(Graphics g, Paragraph paragraph, int leftIndent, int rightIndent, int hangingIndent, int x, int y, bool isLTR, bool lastParagraph, out TextRun run, out int runX, out bool atEndOfLine)
        {
            Win32DCSafeHandle win32DCSafeHandle = null;
            bool flag = false;

            try
            {
                if (g == null)
                {
                    flag = true;
                    g    = Graphics.FromHwnd(IntPtr.Zero);
                }
                win32DCSafeHandle = new Win32DCSafeHandle(g.GetHdc(), false);
                runX        = x;
                run         = null;
                atEndOfLine = false;
                int num = 0;
                while (num < paragraph.TextLines.Count)
                {
                    TextLine textLine = paragraph.TextLines[num];
                    y -= textLine.GetHeight(win32DCSafeHandle, this.FontCache);
                    if (textLine.FirstLine)
                    {
                        y -= TextBox.ConvertToPixels(paragraph.ParagraphProps.SpaceBefore, this.Dpi);
                    }
                    else if (textLine.LastLine)
                    {
                        y -= TextBox.ConvertToPixels(paragraph.ParagraphProps.SpaceAfter, this.Dpi);
                    }
                    if (y >= 0 && num + 1 != paragraph.TextLines.Count)
                    {
                        num++;
                        continue;
                    }
                    int width = textLine.GetWidth(win32DCSafeHandle, this.FontCache);
                    if (x > this.WidthInPX)
                    {
                        atEndOfLine = (width > 0);
                        int  num2  = default(int);
                        bool flag2 = default(bool);
                        run = this.GetLastNonLineBreakRun(textLine.LogicalRuns, out num2, out flag2);
                        TextBoxContext textBoxContext = new TextBoxContext();
                        textBoxContext.TextRunIndex = run.TextRunProperties.IndexInParagraph;
                        if (flag2 && run.CharacterCount > 0)
                        {
                            textBoxContext.TextRunCharacterIndex = run.CharacterIndexInOriginal + num2 + 1;
                        }
                        else
                        {
                            textBoxContext.TextRunCharacterIndex = run.CharacterIndexInOriginal;
                        }
                        run = null;
                        return(textBoxContext);
                    }
                    if (x < 0)
                    {
                        atEndOfLine = false;
                        int num3 = default(int);
                        run = this.GetFirstNonLineBreakRun(textLine.LogicalRuns, out num3);
                        TextBoxContext textBoxContext2 = new TextBoxContext();
                        textBoxContext2.TextRunIndex          = run.TextRunProperties.IndexInParagraph;
                        textBoxContext2.TextRunCharacterIndex = run.CharacterIndexInOriginal + num3;
                        run = null;
                        return(textBoxContext2);
                    }
                    RPLFormat.TextAlignments textAlignments = paragraph.ParagraphProps.Alignment;
                    if (textAlignments == RPLFormat.TextAlignments.General)
                    {
                        textAlignments = this.m_rttextbox.TextBoxProps.DefaultAlignment;
                        if (!isLTR)
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Left:
                                textAlignments = RPLFormat.TextAlignments.Right;
                                break;

                            case RPLFormat.TextAlignments.Right:
                                textAlignments = RPLFormat.TextAlignments.Left;
                                break;
                            }
                        }
                    }
                    switch (textAlignments)
                    {
                    case RPLFormat.TextAlignments.Center:
                        runX = x - (leftIndent + (this.WidthInPX - leftIndent - rightIndent) / 2 - width / 2);
                        break;

                    case RPLFormat.TextAlignments.Right:
                        runX = x - (this.WidthInPX - width - rightIndent);
                        break;

                    default:
                        runX = x - leftIndent;
                        break;
                    }
                    if (textLine.FirstLine && hangingIndent != 0)
                    {
                        if (isLTR)
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Left:
                                runX -= hangingIndent;
                                break;

                            case RPLFormat.TextAlignments.Center:
                                runX -= hangingIndent / 2;
                                break;
                            }
                        }
                        else
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Right:
                                runX += hangingIndent;
                                break;

                            case RPLFormat.TextAlignments.Center:
                                runX += hangingIndent / 2;
                                break;
                            }
                        }
                    }
                    runX = Math.Max(0, runX);
                    return(this.GetParagraphAndRunIndex(win32DCSafeHandle, paragraph, textLine, x, width, lastParagraph, out run, ref runX, out atEndOfLine));
                }
                return(null);
            }
            finally
            {
                if (!win32DCSafeHandle.IsInvalid)
                {
                    g.ReleaseHdc();
                }
                if (flag)
                {
                    g.Dispose();
                    g = null;
                }
            }
        }
Exemple #2
0
        private Point GetParagraphAndRunCoordinates(Win32DCSafeHandle hdc, TextBoxContext location, bool moveCaretToNextLine, out int lineYOffset, out int lineHeight, out TextRun textRun, out int textRunCharacterIndex, out bool isFirstLine, out bool isLastLine)
        {
            int num          = 0;
            int textRunIndex = location.TextRunIndex;

            textRunCharacterIndex = location.TextRunCharacterIndex;
            lineYOffset           = 0;
            lineHeight            = 0;
            textRun     = null;
            isFirstLine = true;
            isLastLine  = true;
            List <Paragraph> rTParagraphs = this.RTParagraphs;

            if (rTParagraphs != null && location.ParagraphIndex < rTParagraphs.Count)
            {
                Paragraph paragraph = rTParagraphs[location.ParagraphIndex];
                int       num2      = paragraph.OffsetY + TextBox.ConvertToPixels(paragraph.ParagraphProps.SpaceBefore, this.Dpi);
                int       num3      = TextBox.ConvertToPixels(paragraph.ParagraphProps.LeftIndent, this.Dpi);
                int       num4      = TextBox.ConvertToPixels(paragraph.ParagraphProps.RightIndent, this.Dpi);
                int       num5      = TextBox.ConvertToPixels(paragraph.ParagraphProps.HangingIndent, this.Dpi);
                bool      flag      = this.m_rttextbox.TextBoxProps.Direction == RPLFormat.Directions.LTR;
                if (num5 < 0)
                {
                    if (flag)
                    {
                        num3 -= num5;
                    }
                    else
                    {
                        num4 -= num5;
                    }
                }
                int listLevel = paragraph.ParagraphProps.ListLevel;
                if (listLevel > 0)
                {
                    int num6 = listLevel * TextBox.ConvertToPixels(10.583333f, this.Dpi);
                    if (flag)
                    {
                        num3 += num6;
                    }
                    else
                    {
                        num4 += num6;
                    }
                }
                for (int i = 0; i < paragraph.TextLines.Count; i++)
                {
                    TextLine textLine = paragraph.TextLines[i];
                    int      descent  = textLine.GetDescent(hdc, this.FontCache);
                    lineHeight  = textLine.GetHeight(hdc, this.FontCache);
                    lineYOffset = num2;
                    num2       += lineHeight;
                    RPLFormat.TextAlignments textAlignments = paragraph.ParagraphProps.Alignment;
                    if (textAlignments == RPLFormat.TextAlignments.General)
                    {
                        textAlignments = this.m_rttextbox.TextBoxProps.DefaultAlignment;
                        if (!flag)
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Left:
                                textAlignments = RPLFormat.TextAlignments.Right;
                                break;

                            case RPLFormat.TextAlignments.Right:
                                textAlignments = RPLFormat.TextAlignments.Left;
                                break;
                            }
                        }
                    }
                    switch (textAlignments)
                    {
                    case RPLFormat.TextAlignments.Center:
                        num = num3 + (this.WidthInPX - num3 - num4) / 2 - textLine.GetWidth(hdc, this.FontCache) / 2;
                        break;

                    case RPLFormat.TextAlignments.Right:
                        num = this.WidthInPX - textLine.GetWidth(hdc, this.FontCache) - num4;
                        break;

                    default:
                        num = num3;
                        break;
                    }
                    if (textLine.FirstLine && num5 != 0)
                    {
                        if (flag)
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Left:
                                num += num5;
                                break;

                            case RPLFormat.TextAlignments.Center:
                                num += num5 / 2;
                                break;
                            }
                        }
                        else
                        {
                            switch (textAlignments)
                            {
                            case RPLFormat.TextAlignments.Right:
                                num -= num5;
                                break;

                            case RPLFormat.TextAlignments.Center:
                                num -= num5 / 2;
                                break;
                            }
                        }
                    }
                    int count = textLine.VisualRuns.Count;
                    for (int j = 0; j < count; num += textRun.GetWidth(hdc, this.FontCache), j++)
                    {
                        textRun = textLine.VisualRuns[j];
                        if (textRun.TextRunProperties.IndexInParagraph == textRunIndex && textRunCharacterIndex >= textRun.CharacterIndexInOriginal)
                        {
                            bool flag2 = (moveCaretToNextLine || textRun.CharacterCount <= 0 || textRun.Text[textRun.CharacterCount - 1] != '\n') ? (textRunCharacterIndex <= textRun.CharacterIndexInOriginal + textRun.CharacterCount) : (textRunCharacterIndex < textRun.CharacterIndexInOriginal + textRun.CharacterCount);
                            if (!flag2 && (i + 1 != paragraph.TextLines.Count || j + 1 != count))
                            {
                                continue;
                            }
                            if (moveCaretToNextLine && textRunCharacterIndex == textRun.CharacterIndexInOriginal + textRun.CharacterCount && j + 1 == count && i + 1 < paragraph.TextLines.Count)
                            {
                                location = location.Clone();
                                if (paragraph.TextLines[i + 1].VisualRuns[0].TextRunProperties.IndexInParagraph != textRunIndex)
                                {
                                    location.TextRunIndex++;
                                    location.TextRunCharacterIndex = 0;
                                }
                                Point paragraphAndRunCoordinates = this.GetParagraphAndRunCoordinates(hdc, location, false, out lineYOffset, out lineHeight, out textRun, out textRunCharacterIndex, out isFirstLine, out isLastLine);
                                textRunCharacterIndex = Math.Max(textRunCharacterIndex - 1, 0);
                                return(paragraphAndRunCoordinates);
                            }
                            textRunCharacterIndex -= textRun.CharacterIndexInOriginal;
                            isFirstLine            = textLine.FirstLine;
                            isLastLine             = textLine.LastLine;
                            return(new Point(num, num2 - descent));
                        }
                    }
                }
                textRun = null;
                return(Point.Empty);
            }
            return(Point.Empty);
        }
Exemple #3
0
        private static void RenderParagraph(TextBox textBox, Paragraph paragraph, Win32DCSafeHandle hdc, FontCache fontCache, int offsetX, ref int offsetY, Rectangle layoutRectangle, float dpiX)
        {
            List <TextLine> textLines      = paragraph.TextLines;
            IParagraphProps paragraphProps = paragraph.ParagraphProps;
            bool            flag           = textBox.TextBoxProps.Direction == RPLFormat.Directions.LTR;

            RPLFormat.TextAlignments textAlignments = paragraphProps.Alignment;
            if (textAlignments == RPLFormat.TextAlignments.General)
            {
                textAlignments = textBox.TextBoxProps.DefaultAlignment;
                if (!flag)
                {
                    switch (textAlignments)
                    {
                    case RPLFormat.TextAlignments.Right:
                        textAlignments = RPLFormat.TextAlignments.Left;
                        break;

                    case RPLFormat.TextAlignments.Left:
                        textAlignments = RPLFormat.TextAlignments.Right;
                        break;
                    }
                }
            }
            int num  = TextBox.ConvertToPixels(paragraphProps.LeftIndent, dpiX);
            int num2 = TextBox.ConvertToPixels(paragraphProps.RightIndent, dpiX);
            int num3 = TextBox.ConvertToPixels(paragraphProps.HangingIndent, dpiX);

            if (num3 < 0)
            {
                if (flag)
                {
                    num -= num3;
                }
                else
                {
                    num2 -= num3;
                }
            }
            if (paragraphProps.ListLevel > 0)
            {
                int num4 = paragraphProps.ListLevel * TextBox.ConvertToPixels(10.583333f, dpiX);
                if (flag)
                {
                    num += num4;
                }
                else
                {
                    num2 += num4;
                }
            }
            if (textLines == null || textLines.Count == 0)
            {
                offsetY += TextBox.ConvertToPixels(paragraphProps.SpaceBefore, dpiX);
                offsetY += TextBox.ConvertToPixels(paragraphProps.SpaceAfter, dpiX);
            }
            else
            {
                Graphics graphics = null;
                try
                {
                    for (int i = 0; i < textLines.Count; i++)
                    {
                        TextLine textLine = textLines[i];
                        int      ascent   = textLine.GetAscent(hdc, fontCache);
                        textLine.GetDescent(hdc, fontCache);
                        int height = textLine.GetHeight(hdc, fontCache);
                        if (textLine.FirstLine)
                        {
                            offsetY += TextBox.ConvertToPixels(paragraphProps.SpaceBefore, dpiX);
                        }
                        int baselineY = offsetY + ascent;
                        offsetY += height;
                        int num5;
                        switch (textAlignments)
                        {
                        case RPLFormat.TextAlignments.Left:
                            num5 = num;
                            break;

                        case RPLFormat.TextAlignments.Center:
                        {
                            int num6 = 0;
                            num6 = ((!textBox.HorizontalText) ? layoutRectangle.Height : layoutRectangle.Width);
                            num5 = num + (num6 - num - num2) / 2 - textLine.GetWidth(hdc, fontCache) / 2;
                            break;
                        }

                        default:
                            num5 = ((!textBox.HorizontalText) ? (layoutRectangle.Height - num2 - textLine.GetWidth(hdc, fontCache)) : (layoutRectangle.Width - num2 - textLine.GetWidth(hdc, fontCache)));
                            break;
                        }
                        if (textLine.Prefix != null && textLine.Prefix.Count > 0)
                        {
                            int num7 = (!flag) ? (num5 + textLine.GetWidth(hdc, fontCache) + TextBox.ConvertToPixels(4.233333f, dpiX)) : (num5 - TextBox.ConvertToPixels(4.233333f, dpiX) - textLine.GetPrefixWidth(hdc, fontCache));
                            if (num3 < 0)
                            {
                                if (flag && textAlignments == RPLFormat.TextAlignments.Left)
                                {
                                    num7 += num3;
                                }
                                else if (!flag && textAlignments == RPLFormat.TextAlignments.Right)
                                {
                                    num7 -= num3;
                                }
                            }
                            for (int j = 0; j < textLine.Prefix.Count; j++)
                            {
                                TextRun textRun = textLine.Prefix[j];
                                textBox.TextBoxProps.DrawTextRun(textRun, paragraph, hdc, dpiX, fontCache, num7, offsetY, baselineY, height, layoutRectangle);
                                num7 += textRun.GetWidth(hdc, fontCache);
                            }
                        }
                        if (textLine.FirstLine && num3 != 0)
                        {
                            if (flag)
                            {
                                switch (textAlignments)
                                {
                                case RPLFormat.TextAlignments.Left:
                                    num5 += num3;
                                    break;

                                case RPLFormat.TextAlignments.Center:
                                    num5 += num3 / 2;
                                    break;
                                }
                            }
                            else
                            {
                                switch (textAlignments)
                                {
                                case RPLFormat.TextAlignments.Right:
                                    num5 -= num3;
                                    break;

                                case RPLFormat.TextAlignments.Center:
                                    num5 -= num3 / 2;
                                    break;
                                }
                            }
                        }
                        int     prevRunWidth = 0;
                        int     prevRunX     = 0;
                        TextRun prevRun      = null;
                        int     count        = textLine.VisualRuns.Count;
                        for (int k = 0; k < count; k++)
                        {
                            TextRun textRun2 = textLine.VisualRuns[k];
                            int     width    = textRun2.GetWidth(hdc, fontCache, k == count - 1);
                            if (!textRun2.IsHighlightTextRun)
                            {
                                if (width > 0)
                                {
                                    textBox.TextBoxProps.DrawTextRun(textRun2, paragraph, hdc, dpiX, fontCache, num5, offsetY, baselineY, height, layoutRectangle);
                                }
                            }
                            else
                            {
                                bool flag2 = (flag && k + 1 == count) || (!flag && k == 0);
                                if (width > 0 || flag2)
                                {
                                    if (graphics == null)
                                    {
                                        graphics = Graphics.FromHdc(hdc.Handle);
                                    }
                                    TextBox.RenderHighlightedTextRun(textBox, paragraph, textRun2, prevRun, hdc, graphics, fontCache, dpiX, num5, offsetY, baselineY, height, layoutRectangle, width, prevRunWidth, prevRunX, flag2, textLine.LastLine);
                                }
                            }
                            prevRunX     = num5;
                            prevRunWidth = width;
                            num5        += width;
                            prevRun      = textRun2;
                        }
                        if (textLine.LastLine)
                        {
                            offsetY += TextBox.ConvertToPixels(paragraphProps.SpaceAfter, dpiX);
                        }
                    }
                }
                finally
                {
                    if (graphics != null)
                    {
                        graphics.Dispose();
                        graphics = null;
                    }
                }
            }
        }