Exemplo n.º 1
0
        public override void Render(AnnContainerMapper mapper, AnnObject annObject)
        {
            if (mapper == null)
            {
                ExceptionHelper.ArgumentNullException("mapper");
            }
            if (annObject == null)
            {
                ExceptionHelper.ArgumentNullException("annObject");
            }

            base.Render(mapper, annObject);
            AnnRichTextObject annRichTextObject = annObject as AnnRichTextObject;

            if (annRichTextObject != null && !String.IsNullOrEmpty(annRichTextObject.Rtf))
            {
                AnnWinFormsRenderingEngine engine = RenderingEngine as AnnWinFormsRenderingEngine;
                if (engine != null && engine.Context != null)
                {
                    AnnRectangleObject tempRect = new AnnRectangleObject();
                    tempRect.Points.Clear();
                    foreach (LeadPointD pt in GetRenderPoints(mapper, annRichTextObject))
                    {
                        tempRect.Points.Add(pt);
                    }

                    double rotation = tempRect.Angle;
                    if (rotation == 180)
                    {
                        rotation = 0;
                    }

                    LeadRectD          boundsPixels   = tempRect.Rect.Clone();
                    AnnContainerMapper identityMapper = mapper.Clone();
                    identityMapper.UpdateTransform(LeadMatrix.Identity);
                    identityMapper.MapResolutions(mapper.SourceDpiX, mapper.SourceDpiY, mapper.SourceDpiX, mapper.SourceDpiY);
                    boundsPixels = identityMapper.RectFromContainerCoordinates(boundsPixels, annRichTextObject.FixedStateOperations);
                    if (tempRect.Stroke != null)
                    {
                        boundsPixels.Inflate(-tempRect.Stroke.StrokeThickness.Value, -tempRect.Stroke.StrokeThickness.Value);
                    }

                    string rtf = annRichTextObject.Rtf;
                    IntPtr hemfDC;
                    if (_richTextBox == null)
                    {
                        _richTextBox = new InternalRichTextEdit();
                    }

                    try
                    {
                        _richTextBox.Rtf = rtf;
                    }
                    catch
                    {
                        using (RichTextBox richTextBox = new RichTextBox())
                        {
                            richTextBox.Text      = rtf;
                            annRichTextObject.Rtf = richTextBox.Rtf;
                            _richTextBox.Rtf      = richTextBox.Rtf;
                        }
                    }

                    Graphics graphics = engine.Context;
                    double   dpiX     = 96;
                    double   dpiY     = 96;

                    _richTextBox.Location = new Point((int)boundsPixels.Location.X, (int)boundsPixels.Location.Y);
                    _richTextBox.Size     = new Size((int)boundsPixels.Size.Width, (int)boundsPixels.Size.Height);
                    IntPtr hdc = graphics.GetHdc();

                    Win32.RECT rc = new Win32.RECT();

                    rc.left   = _richTextBox.ClientRectangle.Left;
                    rc.top    = _richTextBox.ClientRectangle.Top;
                    rc.right  = (int)boundsPixels.Width;
                    rc.bottom = (int)boundsPixels.Height;

                    int iWidthMM    = SafeNativeMethods.GetDeviceCaps(hdc, Win32.HORZSIZE);
                    int iHeightMM   = SafeNativeMethods.GetDeviceCaps(hdc, Win32.VERTSIZE);
                    int iWidthPels  = SafeNativeMethods.GetDeviceCaps(hdc, Win32.HORZRES);
                    int iHeightPels = SafeNativeMethods.GetDeviceCaps(hdc, Win32.VERTRES);

                    rc.left   = (rc.left * iWidthMM * 100) / iWidthPels;
                    rc.top    = (rc.top * iHeightMM * 100) / iHeightPels;
                    rc.right  = (rc.right * iWidthMM * 100) / iWidthPels;
                    rc.bottom = (rc.bottom * iHeightMM * 100) / iHeightPels;

                    hemfDC = SafeNativeMethods.CreateEnhMetaFile(hdc, null, ref rc, null);

                    Win32.RECT emfRect = new Win32.RECT();
                    emfRect.right  = (int)boundsPixels.Width;
                    emfRect.bottom = (int)boundsPixels.Height;

                    IntPtr brush = SafeNativeMethods.GetStockObject(5);
                    SafeNativeMethods.SetBkMode(hemfDC, 1);
                    SafeNativeMethods.FillRect(hemfDC, ref emfRect, brush);
                    SafeNativeMethods.DeleteObject(brush);

                    Print(_richTextBox, _richTextBox.ClientRectangle, hemfDC, (int)dpiX, (int)dpiY, false);
                    IntPtr hemf = SafeNativeMethods.CloseEnhMetaFile(hemfDC);

                    using (Metafile metaFile = new Metafile(hemf, true))
                    {
                        graphics.ReleaseHdc();
                        GraphicsState state = graphics.Save();

                        //the mapper transform dosent contain dpi effect so we will add dpi effect .
                        LeadMatrix matrix = mapper.Transform;
                        //add dpi effect to the transform
                        matrix.Scale((float)(mapper.TargetDpiX / mapper.SourceDpiX), (float)(mapper.TargetDpiY / mapper.SourceDpiY));

                        if ((annRichTextObject.FixedStateOperations & AnnFixedStateOperations.Scrolling) == AnnFixedStateOperations.Scrolling)
                        {
                            matrix.Translate(-matrix.OffsetX, -matrix.OffsetY);
                        }

                        if ((annRichTextObject.FixedStateOperations & AnnFixedStateOperations.Zooming) == AnnFixedStateOperations.Zooming)
                        {
                            double offsetX = matrix.OffsetX;
                            double offsetY = matrix.OffsetY;
                            double scaleX  = 1.0;
                            double scaleY  = 1.0;
                            if (matrix.M11 != 0 && matrix.M22 != 0)
                            {
                                scaleX = 1.0 / Math.Abs(matrix.M11);
                                scaleY = 1.0 / Math.Abs(matrix.M22);
                            }

                            matrix.Scale(scaleX, scaleY);
                        }

                        using (Matrix transform = new Matrix((float)matrix.M11, (float)matrix.M12, (float)matrix.M21, (float)matrix.M22, (float)matrix.OffsetX, (float)matrix.OffsetY))
                        {
                            graphics.MultiplyTransform(transform);
                            graphics.DrawImage(metaFile, new Point((int)boundsPixels.Left, (int)boundsPixels.Top));
                        }
                        graphics.Restore(state);
                    }
                }

                //EndDraw(graphics, gState);
            }
        }