示例#1
0
        void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (!needsResourceRecreation)
            {
                return;
            }

            if (textLayout != null)
            {
                textLayout.Dispose();
                textReference.Dispose();
                textBrush.Dispose();
            }

            textLayout = CreateTextLayout(resourceCreator, (float)targetSize.Width, (float)targetSize.Height);

            textReference = CanvasGeometry.CreateText(textLayout);

            textBrush = new CanvasSolidColorBrush(resourceCreator, Colors.LightBlue);

            needsResourceRecreation = false;
        }
示例#2
0
        private void EnsureResources(ICanvasResourceCreatorWithDpi resourceCreator, Size targetSize)
        {
            if (_needsTextFormatRecreation)
            {
                _textFormat?.Dispose();
                _textFormat = new CanvasTextFormat()
                {
                    HorizontalAlignment = _horizontalAlignment,
                    FontFamily          = FontFamily.Source,
                    FontSize            = (float)FontSize,
                    FontStretch         = FontStretch,
                    FontStyle           = FontStyle,
                    FontWeight          = FontWeight,
                };
            }

            if (_needsTextLayoutRecreation)
            {
                _textLayout?.Dispose();
                _textLayout = new CanvasTextLayout(resourceCreator, Text, _textFormat, (float)targetSize.Width - _marginWidth - _marginPadding, (float)targetSize.Height);
            }

            if (_needsForegroundBrushRecreation)
            {
                _foregroundBrush?.Dispose();
                _foregroundBrush = new CanvasSolidColorBrush(resourceCreator, (Foreground as SolidColorBrush).Color);
            }

            if (_needsBackgroundBrushRecreation)
            {
                _backgroundBrush?.Dispose();
                _backgroundBrush = new CanvasSolidColorBrush(resourceCreator, (Background as SolidColorBrush).Color);
            }

            if (_needsSelectionHighlightColorBrushRecreation)
            {
                _selectionHighlightColorBrush?.Dispose();
                _selectionHighlightColorBrush = new CanvasSolidColorBrush(resourceCreator, SelectionHighlightColor.Color);
            }

            if (_needsCursorColorBrushRecreation)
            {
                _cursorColorBrush?.Dispose();
                _cursorColorBrush = new CanvasSolidColorBrush(resourceCreator, CursorColor.Color);
            }

            if (_needsSelectionForegroundBrushRecreation)
            {
                _selectionForegroundBrush?.Dispose();
                _selectionForegroundBrush = new CanvasSolidColorBrush(resourceCreator, Colors.White);
            }

            if (_needsLineNumberTextFormatRecreation)
            {
                _lineNumberTextFormat?.Dispose();
                _lineNumberTextFormat = new CanvasTextFormat()
                {
                    HorizontalAlignment = CanvasHorizontalAlignment.Right,
                    FontFamily          = FontFamily.Source,
                    FontSize            = (float)FontSize,
                };
            }

            if (_needsLineNumberTextLayoutRecreation)
            {
                _lineNumberTextLayout?.Dispose();
                var lineNumberSb   = new StringBuilder();
                var characterCount = 0;
                var lineNumber     = 1;
                lineNumberSb.AppendLine(lineNumber++.ToString());
                foreach (var lineMetric in _textLayout.LineMetrics)
                {
                    characterCount += lineMetric.CharacterCount;
                    if (Text[characterCount - 1] == '\n' || Text[characterCount - 1] == '\r')
                    {
                        lineNumberSb.AppendLine(lineNumber++.ToString());
                    }
                    else
                    {
                        lineNumberSb.AppendLine();
                    }
                }
                _lineNumberTextLayout = new CanvasTextLayout(resourceCreator, lineNumberSb.ToString(), _lineNumberTextFormat, _marginWidth - _marginPadding, (float)targetSize.Height);
            }
        }
示例#3
0
        private void RenderGeometory(CanvasDrawingSession session, CanvasGeometry geometry, SvgMatrix transform, CssStyleDeclaration style)
        {
            bool change    = false;
            var  geometry2 = geometry;
            CanvasSolidColorBrush opacityBrush = null;

            try
            {
                using (var t = TransformSession.CreateTransformSession(session, transform))
                {
                    var clipPath = style.ClipPath;
                    if (clipPath != null)
                    {
                        if (clipPath.Uri[0] != '#')
                        {
                            throw new ArgumentException();
                        }
                        var clipPathElement = (SvgClipPathElement)this.TargetDocument.GetElementById(clipPath.Uri.Substring(1));
                        var clipGeometory   = this.CreateClipPath(session, clipPathElement);
                        geometry2 = geometry.CombineWith(
                            clipGeometory,
                            new Matrix3x2 {
                            M11 = 1.0F, M12 = 0.0F, M21 = 0.0F, M22 = 1.0F, M31 = 0.0F, M32 = 0.0F
                        },
                            CanvasGeometryCombine.Intersect,
                            CanvasGeometry.ComputeFlatteningTolerance(session.Dpi, 1.0F, session.Transform));
                        change = true;
                    }

                    var area = geometry2.ComputeBounds();

                    var opacity = style.Opacity;
                    if (opacity != null)
                    {
                        opacityBrush = this.CreateOpacity(session, opacity.Value);
                    }

                    var fill = style.Fill;
                    if (fill == null || fill != null && fill.PaintType != SvgPaintType.None)
                    {
                        var pen = this.CreatePaint(session, area, fill, style.FillOpacity);
                        if (opacityBrush == null)
                        {
                            session.FillGeometry(geometry2, pen);
                        }
                        else
                        {
                            session.FillGeometry(geometry2, pen, opacityBrush);
                        }
                    }

                    var stroke = style.Stroke;
                    if (stroke != null && stroke.PaintType != SvgPaintType.None)
                    {
                        var pen         = this.CreatePaint(session, area, stroke, style.StrokeOpacity);
                        var strokeWidth = style.StrokeWidth;
                        session.DrawGeometry(geometry2, pen, strokeWidth.HasValue ? this.LengthConverter.Convert(strokeWidth.Value) : 1.0F);
                    }
                }
            }
            finally
            {
                if (change)
                {
                    geometry2.Dispose();
                }
                if (opacityBrush != null)
                {
                    opacityBrush.Dispose();
                }
            }
        }