Пример #1
0
        public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("CompositionLayer.Draw");
            canvas.GetClipBounds(out _originalClipRect);
            RectExt.Set(ref _newClipRect, 0, 0, LayerModel.PreCompWidth, LayerModel.PreCompHeight);
            parentMatrix.MapRect(ref _newClipRect);

            for (var i = _layers.Count - 1; i >= 0; i--)
            {
                var nonEmptyClip = true;
                if (!_newClipRect.IsEmpty)
                {
                    nonEmptyClip = canvas.ClipRect(_newClipRect);
                }
                if (nonEmptyClip)
                {
                    var layer = _layers[i];
                    layer.Draw(canvas, parentMatrix, parentAlpha);
                }
            }
            if (!_originalClipRect.IsEmpty)
            {
                canvas.ClipReplaceRect(_originalClipRect);
            }
            LottieLog.EndSection("CompositionLayer.Draw");
        }
Пример #2
0
        public void Draw(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("GradientFillContent.Draw");
            _path.Reset();
            for (var i = 0; i < _paths.Count; i++)
            {
                _path.AddPath(_paths[i].Path, parentMatrix);
            }

            _path.ComputeBounds(out _boundsRect);

            Shader shader;

            if (_type == GradientType.Linear)
            {
                shader = LinearGradient;
            }
            else
            {
                shader = RadialGradient;
            }
            _shaderMatrix.Set(parentMatrix);
            shader.LocalMatrix = _shaderMatrix;
            _paint.Shader      = shader;

            var alpha = (byte)(parentAlpha / 255f * _opacityAnimation.Value / 100f * 255);

            _paint.Alpha = alpha;

            canvas.DrawPath(_path, _paint);
            LottieLog.EndSection("GradientFillContent.Draw");
        }
Пример #3
0
        public CanvasCommandList GetCanvasImage(ICanvasResourceCreator resourceCreator, float scaleX, float scaleY)
        {
            lock (this)
            {
                var commandList = new CanvasCommandList(resourceCreator);
                using (var session = commandList.CreateDrawingSession())
                {
                    var width  = _composition.Bounds.Width * scaleX;
                    var height = _composition.Bounds.Height * scaleY;
                    if (_bitmapCanvas == null || _bitmapCanvas.Width < width || _bitmapCanvas.Height < height)
                    {
                        _bitmapCanvas?.Dispose();
                        _bitmapCanvas = new BitmapCanvas(width, height);
                    }

                    using (_bitmapCanvas.CreateSession(resourceCreator.Device, (float)width,
                                                       (float)height, session))
                    {
                        _bitmapCanvas.Clear(Colors.Transparent);
                        LottieLog.BeginSection("Drawable.Draw");
                        if (_compositionLayer == null)
                        {
                            return(null);
                        }

                        _matrix.Reset();
                        _matrix = MatrixExt.PreScale(_matrix, scaleX, scaleY);
                        _compositionLayer.Draw(_bitmapCanvas, _matrix, _alpha);
                        LottieLog.EndSection("Drawable.Draw");
                    }
                }

                return(commandList);
            }
        }
 public override ICanvasBrush Apply(BitmapCanvas dst, ICanvasBrush brush)
 {
     //var originalColor = Colors.White;
     //if (brush is CompositionColorBrush compositionColorBrush)
     //    originalColor = compositionColorBrush.Color;
     //TODO
     return(brush);
 }
Пример #5
0
        private Rect DrawCharacterFromFont(char c, DocumentData documentData, BitmapCanvas canvas)
        {
            if (documentData.StrokeOverFill)
            {
                DrawCharacter(c, _fillPaint, canvas);
                return(DrawCharacter(c, _strokePaint, canvas));
            }

            DrawCharacter(c, _strokePaint, canvas);
            return(DrawCharacter(c, _fillPaint, canvas));
        }
Пример #6
0
        private static void Draw(CanvasDevice device, BitmapCanvas bitmapCanvas, CompositionLayer compositionLayer, Rect bounds, float scale, byte alpha, Matrix3X3 matrix, double width, double height, CanvasDrawingSession canvasDrawingSession)
        {
            using (bitmapCanvas.CreateSession(device, width, height, canvasDrawingSession))
            {
                bitmapCanvas.Clear(Colors.Transparent);
                LottieLog.BeginSection("Drawable.Draw");
                if (compositionLayer == null)
                {
                    return;
                }

                var   localScale = scale;
                float extraScale = 1f;

                float maxScale = GetMaxScale(bitmapCanvas, bounds);
                if (localScale > maxScale)
                {
                    localScale = maxScale;
                    extraScale = scale / localScale;
                }

                if (extraScale > 1)
                {
                    // This is a bit tricky...
                    // We can't draw on a canvas larger than ViewConfiguration.get(context).getScaledMaximumDrawingCacheSize()
                    // which works out to be roughly the size of the screen because Android can't generate a
                    // bitmap large enough to render to.
                    // As a result, we cap the scale such that it will never be wider/taller than the screen
                    // and then only render in the top left corner of the canvas. We then use extraScale
                    // to scale up the rest of the scale. However, since we rendered the animation to the top
                    // left corner, we need to scale up and translate the canvas to zoom in on the top left
                    // corner.
                    bitmapCanvas.Save();
                    float halfWidth        = (float)bounds.Width / 2f;
                    float halfHeight       = (float)bounds.Height / 2f;
                    float scaledHalfWidth  = halfWidth * localScale;
                    float scaledHalfHeight = halfHeight * localScale;
                    bitmapCanvas.Translate(
                        scale * halfWidth - scaledHalfWidth,
                        scale * halfHeight - scaledHalfHeight);
                    bitmapCanvas.Scale(extraScale, extraScale, scaledHalfWidth, scaledHalfHeight);
                }

                matrix.Reset();
                matrix = MatrixExt.PreScale(matrix, localScale, localScale);
                compositionLayer.Draw(bitmapCanvas, matrix, alpha);
                LottieLog.EndSection("Drawable.Draw");

                if (extraScale > 1)
                {
                    bitmapCanvas.Restore();
                }
            }
        }
Пример #7
0
 private void DrawGlyph(Path path, Paint paint, BitmapCanvas canvas)
 {
     if (paint.Color == Colors.Transparent)
     {
         return;
     }
     if (paint.Style == Paint.PaintStyle.Stroke && paint.StrokeWidth == 0)
     {
         return;
     }
     canvas.DrawPath(path, paint);
 }
Пример #8
0
 private void DrawCharacterFromFont(char c, DocumentData documentData, BitmapCanvas canvas)
 {
     if (documentData.StrokeOverFill)
     {
         DrawCharacter(c, _fillPaint, canvas);
         DrawCharacter(c, _strokePaint, canvas);
     }
     else
     {
         DrawCharacter(c, _strokePaint, canvas);
         DrawCharacter(c, _fillPaint, canvas);
     }
 }
        public override void Draw(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            GetBounds(out _boundsRect, parentMatrix);
            if (_type == GradientType.Linear)
            {
                Paint.Shader = LinearGradient;
            }
            else
            {
                Paint.Shader = RadialGradient;
            }

            base.Draw(canvas, parentMatrix, parentAlpha);
        }
Пример #10
0
        public void Draw(BitmapCanvas canvas, DenseMatrix parentMatrix, byte alpha)
        {
            var copies       = _copies.Value.Value;
            var offset       = _offset.Value.Value;
            var startOpacity = _transform.StartOpacity.Value.Value / 100f;
            var endOpacity   = _transform.EndOpacity.Value.Value / 100f;

            for (var i = (int)copies - 1; i >= 0; i--)
            {
                _matrix.Set(parentMatrix);
                _matrix = MatrixExt.PreConcat(_matrix, _transform.GetMatrixForRepeater(i + offset));
                float newAlpha = alpha * MiscUtils.Lerp(startOpacity, endOpacity, i / copies);
                _contentGroup.Draw(canvas, _matrix, (byte)newAlpha);
            }
        }
Пример #11
0
        public virtual void Draw(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            _paint.Color = _colorAnimation.Value;
            var alpha = (byte)(parentAlpha / 255f * _opacityAnimation.Value / 100f * 255);

            _paint.Alpha = alpha;

            _path.Reset();
            for (var i = 0; i < _paths.Count; i++)
            {
                _path.AddPath(_paths[i].Path, parentMatrix);
            }

            canvas.DrawPath(_path, _paint);
        }
Пример #12
0
        public override void DrawLayer(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            var bitmap = Bitmap;

            if (bitmap == null)
            {
                return;
            }
            _paint.Alpha = parentAlpha;
            canvas.Save();
            canvas.Concat(parentMatrix);
            RectExt.Set(ref _src, 0, 0, PixelWidth, PixelHeight);
            RectExt.Set(ref _dst, 0, 0, (int)(PixelWidth * _density), (int)(PixelHeight * _density));
            canvas.DrawBitmap(bitmap, _src, _dst, _paint);
            canvas.Restore();
        }
Пример #13
0
        private void UserControl_Unloaded(object sender, RoutedEventArgs e)
        {
            // Explicitly remove references to allow the Win2D controls to get garbage collected
            if (_canvasControl != null)
            {
                _canvasControl.RemoveFromVisualTree();
                _canvasControl = null;
            }

            if (_bitmapCanvas != null)
            {
                _bitmapCanvas.Dispose();
                _bitmapCanvas = null;
            }

            ClearComposition();
        }
Пример #14
0
        public virtual void Draw(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            LottieLog.BeginSection("FillContent.Draw");
            _paint.Color = _colorAnimation.Value;
            var alpha = (byte)(parentAlpha / 255f * _opacityAnimation.Value / 100f * 255);

            _paint.Alpha = alpha;

            _path.Reset();
            for (var i = 0; i < _paths.Count; i++)
            {
                _path.AddPath(_paths[i].Path, parentMatrix);
            }

            canvas.DrawPath(_path, _paint);
            LottieLog.EndSection("FillContent.Draw");
        }
Пример #15
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        public void Draw(BitmapCanvas canvas)
        {
            if (_compositionLayer == null)
            {
                return;
            }
            var scale = _scale;

            if (_compositionLayer.HasMatte())
            {
                scale = Math.Min(_scale, GetMaxScale(canvas));
            }

            _matrix.Reset();
            _matrix = MatrixExt.PreScale(_matrix, scale, scale);
            _compositionLayer.Draw(canvas, _matrix, _alpha);
        }
Пример #16
0
        public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            int backgroundAlpha = LayerModel.SolidColor.A;

            if (backgroundAlpha == 0)
            {
                return;
            }

            var alpha = (byte)(backgroundAlpha / 255f * Transform.Opacity.Value / 100f * 255);

            _paint.Alpha = alpha;
            if (alpha > 0)
            {
                UpdateRect(parentMatrix);
                canvas.DrawRect(Rect, _paint);
            }
        }
Пример #17
0
        //public int Opacity
        //{
        //    get
        //    {
        //        return PixelFormat.TRANSLUCENT;
        //    }
        //}

        public void Draw(BitmapCanvas canvas)
        {
            LottieLog.BeginSection("Drawable.Draw");
            if (_compositionLayer == null)
            {
                return;
            }
            var scale = _scale;

            if (_compositionLayer.HasMatte())
            {
                scale = Math.Min(_scale, GetMaxScale(canvas));
            }

            _matrix.Reset();
            _matrix = MatrixExt.PreScale(_matrix, scale, scale);
            _compositionLayer.Draw(canvas, _matrix, _alpha);
            LottieLog.EndSection("Drawable.Draw");
        }
Пример #18
0
        public virtual void Draw(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            _matrix.Set(parentMatrix);
            byte alpha;

            if (_transformAnimation != null)
            {
                _matrix = MatrixExt.PreConcat(_matrix, _transformAnimation.Matrix);
                alpha   = (byte)(_transformAnimation.Opacity.Value / 100f * parentAlpha / 255f * 255);
            }
            else
            {
                alpha = parentAlpha;
            }

            for (var i = _contents.Count - 1; i >= 0; i--)
            {
                var drawingContent = _contents[i] as IDrawingContent;
                drawingContent?.Draw(canvas, _matrix, alpha);
            }
        }
Пример #19
0
        public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
        {
            canvas.Save();
            if (!_lottieDrawable.UseTextGlyphs())
            {
                canvas.SetMatrix(parentMatrix);
            }
            var documentData = _textAnimation.Value;

            if (!_composition.Fonts.TryGetValue(documentData.FontName, out var font))
            {
                // Something is wrong.
                return;
            }

            _fillPaint.Color   = _colorAnimation?.Value ?? documentData.Color;
            _strokePaint.Color = _strokeAnimation?.Value ?? documentData.StrokeColor;
            if (_strokeWidthAnimation?.Value != null)
            {
                _strokePaint.StrokeWidth = _strokeWidthAnimation.Value.Value;
            }
            else
            {
                var parentScale = Utils.GetScale(parentMatrix);
                _strokePaint.StrokeWidth = documentData.StrokeWidth * _composition.DpScale * parentScale;
            }

            if (_lottieDrawable.UseTextGlyphs())
            {
                DrawTextGlyphs(documentData, parentMatrix, font, canvas);
            }
            else
            {
                DrawTextWithFont(documentData, font, parentMatrix, canvas);
            }

            canvas.Restore();
        }
Пример #20
0
        public override void DrawLayer(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
        {
            canvas.GetClipBounds(out _originalClipRect);
            RectExt.Set(ref _newClipRect, 0, 0, _layerModel.PreCompWidth, _layerModel.PreCompHeight);
            parentMatrix.MapRect(ref _newClipRect);

            for (var i = _layers.Count - 1; i >= 0; i--)
            {
                var nonEmptyClip = true;
                if (!_newClipRect.IsEmpty)
                {
                    nonEmptyClip = canvas.ClipRect(_newClipRect);
                }
                if (nonEmptyClip)
                {
                    _layers[i].Draw(canvas, parentMatrix, parentAlpha);
                }
            }
            if (!_originalClipRect.IsEmpty)
            {
                canvas.ClipRect(_originalClipRect, Region.Op.Replace);
            }
        }
Пример #21
0
 private void DrawCharacter(char character, Paint paint, BitmapCanvas canvas)
 {
     canvas.DrawText(character, paint);
 }
Пример #22
0
 public override void DrawLayer(BitmapCanvas canvas, DenseMatrix parentMatrix, byte parentAlpha)
 {
     // Do nothing.
 }
Пример #23
0
 private Rect DrawCharacter(char character, Paint paint, BitmapCanvas canvas)
 {
     return(canvas.DrawText(character, paint));
 }
Пример #24
0
        private void DrawCharacterAsGlyph(FontCharacter character, Matrix3X3 parentMatrix, float fontScale, DocumentData documentData, BitmapCanvas canvas)
        {
            var contentGroups = GetContentsForCharacter(character);

            for (var j = 0; j < contentGroups.Count; j++)
            {
                var path = contentGroups[j].Path;
                //path.ComputeBounds(out _rectF);
                Matrix.Set(parentMatrix);
                Matrix = MatrixExt.PreScale(Matrix, fontScale, fontScale);
                path.Transform(Matrix);
                if (documentData.StrokeOverFill)
                {
                    DrawGlyph(path, _fillPaint, canvas);
                    DrawGlyph(path, _strokePaint, canvas);
                }
                else
                {
                    DrawGlyph(path, _strokePaint, canvas);
                    DrawGlyph(path, _fillPaint, canvas);
                }
            }
        }
Пример #25
0
        private void DrawTextWithFont(DocumentData documentData, Font font, Matrix3X3 parentMatrix, BitmapCanvas canvas)
        {
            var parentScale = Utils.GetScale(parentMatrix);
            var typeface    = _lottieDrawable.GetTypeface(font.Family, font.Style);

            if (typeface == null)
            {
                return;
            }
            var text         = documentData.Text;
            var textDelegate = _lottieDrawable.TextDelegate;

            if (textDelegate != null)
            {
                text = textDelegate.GetTextInternal(text);
            }
            _fillPaint.Typeface   = typeface;
            _fillPaint.TextSize   = documentData.Size * _composition.DpScale;
            _strokePaint.Typeface = _fillPaint.Typeface;
            _strokePaint.TextSize = _fillPaint.TextSize;
            for (var i = 0; i < text.Length; i++)
            {
                var character = text[i];
                var size      = DrawCharacterFromFont(character, documentData, canvas);

                // Add tracking
                var tracking = documentData.Tracking / 10f;
                if (_trackingAnimation?.Value != null)
                {
                    tracking += _trackingAnimation.Value.Value;
                }
                var tx = (float)size.Width + tracking * parentScale;
                canvas.Translate(tx, 0);
            }
        }
Пример #26
0
        private void DrawTextGlyphs(DocumentData documentData, Matrix3X3 parentMatrix, Font font, BitmapCanvas canvas)
        {
            var fontScale   = (float)documentData.Size / 100;
            var parentScale = Utils.GetScale(parentMatrix);
            var text        = documentData.Text;

            for (var i = 0; i < text.Length; i++)
            {
                var c             = text[i];
                var characterHash = FontCharacter.HashFor(c, font.Family, font.Style);
                if (!_composition.Characters.TryGetValue(characterHash, out var character))
                {
                    // Something is wrong. Potentially, they didn't export the text as a glyph.
                    continue;
                }
                DrawCharacterAsGlyph(character, parentMatrix, fontScale, documentData, canvas);
                var tx = (float)character.Width * fontScale * _composition.DpScale * parentScale;
                // Add tracking
                var tracking = documentData.Tracking / 10f;
                if (_trackingAnimation?.Value != null)
                {
                    tracking += _trackingAnimation.Value.Value;
                }
                tx += tracking * parentScale;
                canvas.Translate(tx, 0);
            }
        }
Пример #27
0
 public override void DrawLayer(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
 {
     _contentGroup.Draw(canvas, parentMatrix, parentAlpha);
 }
Пример #28
0
 public override void Draw(BitmapCanvas canvas, Matrix3X3 parentMatrix, byte parentAlpha)
 {
     Paint.Color = _colorAnimation.Value;
     base.Draw(canvas, parentMatrix, parentAlpha);
 }
Пример #29
0
 public abstract ICanvasBrush Apply(BitmapCanvas dst, ICanvasBrush brush);