示例#1
0
        /// <summary>
        /// Renders the layout to an image
        /// </summary>
        /// <param name="Settings">Rendering settings.</param>
        /// <param name="Maps">Generated maps</param>
        /// <returns></returns>
        public SKImage Render(RenderSettings Settings, out Map[] Maps)
        {
            Maps = null;                // TODO: Generate maps.

            int Width;
            int Height;

            switch (Settings.ImageSize)
            {
            case RenderedImageSize.ResizeImage:
                Width = Height = 10;
                break;

            case RenderedImageSize.ScaleToFit:
            default:
                Width  = Settings.Width;
                Height = Settings.Height;
                break;
            }

            SKSurface    Surface = SKSurface.Create(new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul));
            DrawingState State   = null;

            try
            {
                SKCanvas Canvas = Surface.Canvas;
                State = new DrawingState(Canvas, Settings, this.session);

                if (Settings.BackgroundColor != SKColor.Empty)
                {
                    Canvas.Clear(Settings.BackgroundColor);
                }

                int Limit = 100;

                while (this.root?.MeasureDimensions(State) ?? false)
                {
                    if (--Limit <= 0)
                    {
                        break;
                    }
                }

                this.root?.MeasurePositions(State);

                switch (Settings.ImageSize)
                {
                case RenderedImageSize.ResizeImage:
                    Surface.Dispose();
                    Surface = null;

                    if (!(this.root is null))
                    {
                        Width  = (int)this.root.Right - (int)this.root.Left + 1;
                        Height = (int)this.root.Bottom - (int)this.root.Top - 1;
                    }

                    Surface      = SKSurface.Create(new SKImageInfo(Width, Height, SKImageInfo.PlatformColorType, SKAlphaType.Premul));
                    Canvas       = Surface.Canvas;
                    State.Canvas = Canvas;

                    if (Settings.BackgroundColor != SKColor.Empty)
                    {
                        Canvas.Clear(Settings.BackgroundColor);
                    }

                    if (!(this.root is null) && this.root.Left.HasValue && this.root.Top.HasValue)
                    {
                        State.Canvas.Translate(-this.root.Left.Value, -this.root.Top.Value);
                    }
                    break;

                case RenderedImageSize.ScaleToFit:
                    if (!(this.root is null))
                    {
                        if (this.root.Width.HasValue && this.root.Height.HasValue)
                        {
                            float Width2  = this.root.Width.Value + 1;
                            float Height2 = this.root.Height.Value + 1;
                            float ScaleX  = Width / Width2;
                            float ScaleY  = Height / Height2;

                            if (ScaleX < ScaleY)
                            {
                                State.Canvas.Translate(0, (Height - Height2 * ScaleX) / 2);
                                State.Canvas.Scale(ScaleX);
                            }
                            else if (ScaleY < ScaleX)
                            {
                                State.Canvas.Translate((Width - Width2 * ScaleY) / 2, 0);
                                State.Canvas.Scale(ScaleY);
                            }
                        }

                        if (this.root.Left.HasValue && this.root.Top.HasValue)
                        {
                            State.Canvas.Translate(-this.root.Left.Value, -this.root.Top.Value);
                        }
                    }
                    break;
                }

                this.root?.Draw(State);

                return(Surface.Snapshot());
            }
            finally
            {
                State?.Dispose();
                Surface?.Dispose();
            }
        }