Пример #1
0
        public void Render(Graphics g, RectangleF rectangle, PointF offset, IEnumerable <RTSelectionHighlight> highlights, bool unitsInMM)
        {
            List <Paragraph> rTParagraphs = this.RTParagraphs;

            if (rTParagraphs != null && rTParagraphs.Count != 0)
            {
                using (RevertingDeviceContext revertingDeviceContext = new RevertingDeviceContext(g, this.Dpi))
                {
                    Win32DCSafeHandle hdc = revertingDeviceContext.Hdc;
                    if (highlights != null)
                    {
                        RPLFormat.Directions direction = this.RTTextbox.TextBoxProps.Direction;
                        foreach (RTSelectionHighlight highlight in highlights)
                        {
                            if (!RichTextRenderer.HighlightStartLessThanOrEqualToEnd(highlight.SelectionStart, highlight.SelectionEnd))
                            {
                                TextBoxContext selectionStart = highlight.SelectionStart;
                                highlight.SelectionStart = highlight.SelectionEnd;
                                highlight.SelectionEnd   = selectionStart;
                            }
                            TextRun   textRun    = default(TextRun);
                            CaretInfo caretInfo  = this.MapLocation(hdc, highlight.SelectionStart, true, true, out textRun);
                            TextRun   textRun2   = default(TextRun);
                            CaretInfo caretInfo2 = this.MapLocation(hdc, highlight.SelectionEnd, true, true, out textRun2);
                            if (caretInfo != null && caretInfo2 != null && textRun != null && textRun2 != null)
                            {
                                this.SetHighlighting(rTParagraphs, hdc, highlight, textRun, textRun2, caretInfo.Position.X, caretInfo2.Position.X);
                            }
                        }
                    }
                    Rectangle rectangle2 = (!unitsInMM) ? Rectangle.Round(rectangle) : new Rectangle(TextBox.ConvertToPixels(rectangle.X, this.m_dpi), TextBox.ConvertToPixels(rectangle.Y, this.m_dpi), TextBox.ConvertToPixels(rectangle.Width, this.m_dpi), TextBox.ConvertToPixels(rectangle.Height, this.m_dpi));
                    revertingDeviceContext.XForm.Transform(ref rectangle2);
                    Win32ObjectSafeHandle win32ObjectSafeHandle = Win32.CreateRectRgn(rectangle2.Left - 1, rectangle2.Top - 1, rectangle2.Right + 1, rectangle2.Bottom + 1);
                    if (!win32ObjectSafeHandle.IsInvalid)
                    {
                        try
                        {
                            if (Win32.SelectClipRgn(hdc, win32ObjectSafeHandle) == 0)
                            {
                                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                            }
                        }
                        finally
                        {
                            win32ObjectSafeHandle.Close();
                        }
                    }
                    TextBox.Render(this.RTTextbox, rTParagraphs, hdc, this.FontCache, offset, rectangle, this.m_dpi, unitsInMM);
                    if (Win32.SelectClipRgn(hdc, Win32ObjectSafeHandle.Zero) == 0)
                    {
                        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
                    }
                }
            }
        }
Пример #2
0
        private CaretInfo MapLocation(Win32DCSafeHandle hdc, TextBoxContext location, bool relativeToRun, bool moveCaretToNextLine, out TextRun run)
        {
            CaretInfo caretInfo = null;

            run = null;
            int   lineYOffset = default(int);
            int   lineHeight  = default(int);
            int   iCP         = default(int);
            bool  isFirstLine = default(bool);
            bool  isLastLine  = default(bool);
            Point paragraphAndRunCoordinates = this.GetParagraphAndRunCoordinates(hdc, location, moveCaretToNextLine, out lineYOffset, out lineHeight, out run, out iCP, out isFirstLine, out isLastLine);

            if (run != null)
            {
                GlyphData      glyphData            = run.GlyphData;
                GlyphShapeData glyphScriptShapeData = glyphData.GlyphScriptShapeData;
                int            num = 0;
                if (glyphData != null && run.CharacterCount > 0)
                {
                    int num2 = Win32.ScriptCPtoX(iCP, false, run.CharacterCount, glyphScriptShapeData.GlyphCount, glyphScriptShapeData.Clusters, glyphScriptShapeData.VisAttrs, glyphData.Advances, ref run.SCRIPT_ANALYSIS, ref num);
                    if (Win32.Failed(num2))
                    {
                        Marshal.ThrowExceptionForHR(num2);
                    }
                }
                caretInfo = new CaretInfo();
                CachedFont cachedFont = run.GetCachedFont(hdc, this.FontCache);
                caretInfo.Height      = cachedFont.GetHeight(hdc, this.FontCache);
                caretInfo.Ascent      = cachedFont.GetAscent(hdc, this.FontCache);
                caretInfo.Descent     = cachedFont.GetDescent(hdc, this.FontCache);
                caretInfo.LineHeight  = lineHeight;
                caretInfo.LineYOffset = lineYOffset;
                caretInfo.IsFirstLine = isFirstLine;
                caretInfo.IsLastLine  = isLastLine;
                List <Paragraph> rTParagraph = this.RTParagraphs;
                int y = paragraphAndRunCoordinates.Y - caretInfo.Ascent;
                if (relativeToRun)
                {
                    caretInfo.Position = new Point(num, y);
                }
                else
                {
                    caretInfo.Position = new Point(paragraphAndRunCoordinates.X + num, y);
                }
            }
            return(caretInfo);
        }