Пример #1
0
        protected override void OnStart(AppHost host)
        {
            _appHost = host;//**

            //string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/cat_simple.svg";
            //string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/cat_complex.svg";
            //string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/lion.svg";
            string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/tiger.svg";

            //return VgVisualElemHelper.ReadSvgFile(svgfile);
            _rotationUI.AngleUpdated += _rotationUI_AngleUpdated;
            //string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/freepik/dog1.svg";
            //string svgfile = "1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f30b.svg";
            //string svgfile = "../Data/1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f370.svg";

            //_svgRenderVx = CreateTestRenderVx_FromSvg();
            //_svgRenderVx = CreateTestRenderVx_BasicShape();
            //_vgVisualElem = CreateTestRenderVx_FromImg("d:\\WImageTest\\alpha1.png");

            //string fontfile = "../Test8_HtmlRenderer.Demo/Samples/Fonts/SOV_Thanamas.ttf";
            //_vgVisualElem = VgVisualElemHelper.CreateVgVisualElementFromGlyph(fontfile, 256, 'a'); //create from glyph

            _vgVisualElem = CreateTestRenderVx_FromImg("d:\\WImageTest\\fenec.png");

            //PixelFarm.CpuBlit.RectD org_rectD = _svgRenderVx.GetBounds();
            //_svgRenderVx = CreateEllipseVxs(org_rectD);

            PixelFarm.CpuBlit.RectD org_rectD = _vgVisualElem.GetRectBounds();
            org_rectD.Offset(-org_rectD.Left, -org_rectD.Bottom);
            //
            _quadController.SetSrcRect(org_rectD.Left, org_rectD.Bottom, org_rectD.Right, org_rectD.Top);
            _quadController.SetDestQuad(
                org_rectD.Left, org_rectD.Top,
                org_rectD.Right, org_rectD.Top,
                org_rectD.Right, org_rectD.Bottom,
                org_rectD.Left, org_rectD.Bottom);
            //create control point
            _quadController.SetPolygonController(_quadPolygonController);
            _quadController.BuildControlBoxes();
            _quadController.UpdateTransformTarget += (s1, e1) =>
            {
                //after quadController is updated then
                //we use the coordTransformer to transform target uiSprite
                _uiSprite.SetTransformation(_quadController.GetCoordTransformer());
                _uiSprite.InvalidateOuterGraphics();
                if (_quadController.Left != 0 || _quadController.Top != 0)
                {
                    float xxdiff = _quadController.Left - _uiSprite.Left;
                    float yydiff = _quadController.Top - _uiSprite.Top;

                    _uiSprite.SetLocation(_quadController.Left, _quadController.Top);
                    _uiSprite.InvalidateOuterGraphics();


                    //_rotationUI.InvalidateGraphics();
                    //_rotationUI.SetLocation(
                    //    _rotationUI.Left + xxdiff,
                    //    _rotationUI.Top + yydiff);
                    //_rotationUI.InvalidateGraphics();

                    //_rotationControllerPointUI.InvalidateGraphics();
                    //_rotationControllerPointUI.SetPosition(
                    //   (int)(_rotationControllerPointUI.Left + xxdiff),
                    //   (int)(_rotationControllerPointUI.Top + yydiff));
                    //_rotationControllerPointUI.InvalidateGraphics();
                }
            };



            //_rectBoundsWidgetBox = new Box2(50, 50); //visual rect box
            //Color c = KnownColors.FromKnownColor(KnownColor.DeepSkyBlue);
            //_rectBoundsWidgetBox.BackColor = Color.FromArgb(100, c);
            //_rectBoundsWidgetBox.SetLocation(10, 10);
            /////box1.dbugTag = 1;
            //SetupActiveBoxProperties(_rectBoundsWidgetBox);
            //host.AddChild(_rectBoundsWidgetBox);
            //_quadController.Visible = _quadPolygonController.Visible = false;
            //_rectBoxController.Init();

            PixelFarm.CpuBlit.RectD svg_bounds = _vgVisualElem.GetRectBounds(); //bounds of graphic shape
            //ICoordTransformer tx = ((ICoordTransformer)_bilinearTx).MultiplyWith(scaleMat);
            ICoordTransformer tx = _quadController.GetCoordTransformer();

            //svgRenderVx._coordTx = tx;
            //svgRenderVx._coordTx = ((ICoordTransformer)_bilinearTx).MultiplyWith(scaleMat);
            //host.AddChild(_quadController);
            //host.AddChild(_quadPolygonController);
            //VgRenderVx svgRenderVx = CreateTestRenderVx();

            //test transform svgRenderVx

            _vgVisualElem.DisableBackingImage = true;


            //-----------------------------------------
            _uiSprite = new UISprite(10, 10); //init size = (10,10), location=(0,0)
            _uiSprite.DisableBmpCache = true;
            _uiSprite.LoadVg(_vgVisualElem);  //
            _uiSprite.SetTransformation(tx);  //set transformation
            host.AddChild(_uiSprite);
            //-----------------------------------------
            host.AddChild(_quadController);
            host.AddChild(_quadPolygonController);


            {
                PointControllerBox center = new PointControllerBox(10, 10);
                PointControllerBox radius = new PointControllerBox(10, 10);
                host.AddChild(center);
                host.AddChild(radius);
                _rotationUI.AddControlPoints(center, radius);
            }

            _rotationUI.SetReferenceOwner(_quadController);
            _rotationUI.SetCenter(svg_bounds.XCenter, svg_bounds.YCenter);
            _rotationUI.SetRadius(svg_bounds.XCenter + 200, svg_bounds.YCenter);
            host.AddChild(_rotationUI);



            _quadController.Drag += ev =>
            {
                _rotationUI.SetLocation(
                    _rotationUI.Left + ev.XDiff,
                    _rotationUI.Top + ev.YDiff);
            };

            var spriteEvListener = new GeneralEventListener();

            _uiSprite.AttachExternalEventListener(spriteEvListener);
            spriteEvListener.MouseMove += e1 =>
            {
                if (e1.IsDragging)
                {
                    //when drag on sprie


                    _uiSprite.InvalidateOuterGraphics();
                    _uiSprite.SetLocation(
                        _uiSprite.Left + e1.XDiff,
                        _uiSprite.Top + e1.YDiff
                        );
                    //we also move quadController and _quadPolygonController
                    //
                    _quadController.InvalidateOuterGraphics();
                    _quadController.SetLocation(
                        _quadController.Left + e1.XDiff,
                        _quadController.Top + e1.YDiff);
                    _quadController.InvalidateOuterGraphics();
                    //
                    _quadPolygonController.InvalidateOuterGraphics();
                    _quadPolygonController.SetLocation(
                        _quadPolygonController.Left + e1.XDiff,
                        _quadPolygonController.Top + e1.YDiff
                        );
                    _quadPolygonController.InvalidateOuterGraphics();
                    //

                    _rotationUI.InvalidateGraphics();
                    _rotationUI.SetLocation(
                        _rotationUI.Left + e1.XDiff,
                        _rotationUI.Top + e1.YDiff);
                    _rotationUI.InvalidateGraphics();

                    //_rotationControllerPointUI.InvalidateGraphics();
                    //_rotationControllerPointUI.SetPosition(
                    //   _rotationControllerPointUI.Left + e1.XDiff,
                    //   _rotationControllerPointUI.Top + e1.YDiff);
                    //_rotationControllerPointUI.InvalidateGraphics();
                }
            };
            spriteEvListener.MouseDown += e1 =>
            {
                //mousedown on ui sprite
                //find exact part ...

                _quadController.BringToTopMost();
                _quadController.Visible        = true;
                _quadPolygonController.Visible = true;
                _quadController.Focus();

                // _polygonController.BringToTopMost();

                if (_hitTestOnSubPath)
                {
                    //find which part ...
                    VgHitInfo hitInfo = _uiSprite.FindRenderElementAtPos(e1.X, e1.Y, true);

                    if (hitInfo.hitElem != null &&
                        hitInfo.hitElem.VxsPath != null)
                    {
                        PixelFarm.CpuBlit.RectD bounds = hitInfo.copyOfVxs.GetBoundingRect();

                        _quadPolygonController.ClearControlPoints(); //clear old control points
                        _quadPolygonController.UpdateControlPoints(  //create new control points
                            hitInfo.copyOfVxs,
                            _uiSprite.ActualXOffset, _uiSprite.ActualYOffset);

                        ////move redbox and its controller
                        //_rectBoundsWidgetBox.SetLocationAndSize(
                        //    (int)(bounds.Left + _uiSprite.ActualXOffset), (int)(bounds.Top - bounds.Height + _uiSprite.ActualYOffset),
                        //    (int)bounds.Width, (int)bounds.Height);
                        ////_rectBoxController.UpdateControllerBoxes(_rectBoundsWidgetBox);

                        //_rectBoundsWidgetBox.Visible = true;
                        ////_rectBoxController.Visible = true;
                        //show bounds
                    }
                    else
                    {
                        //_rectBoundsWidgetBox.Visible = false;
                        // _rectBoxController.Visible = false;
                    }
                }
                else
                {
                    //hit on sprite
                    if (e1.Ctrl)
                    {
                        //test***
                        //
                        _uiSprite.GetElementBounds(out float left, out float top, out float right, out float bottom);
                        //
                        using (VectorToolBox.Borrow(out SimpleRect s))
                            using (VxsTemp.Borrow(out var v1))
                            {
                                s.SetRect(left - _uiSprite.ActualXOffset,
                                          bottom - _uiSprite.ActualYOffset,
                                          right - _uiSprite.ActualXOffset,
                                          top - _uiSprite.ActualYOffset);
                                s.MakeVxs(v1);
                                //_polygonController.UpdateControlPoints(v1.CreateTrim());
                            }
                    }
                    else
                    {
                        //_rectBoundsWidgetBox.SetTarget(_uiSprite);
                        //_rectBoundsWidgetBox.SetLocationAndSize(    //blue
                        //      (int)_uiSprite.Left, (int)_uiSprite.Top,
                        //      (int)_uiSprite.Width, (int)_uiSprite.Height);
                        ////_rectBoxController.SetTarget(_uiSprite);

                        ////_rectBoxController.UpdateControllerBoxes(_rectBoundsWidgetBox);  //control 4 corners
                        //_rectBoundsWidgetBox.Visible = true;
                        ////_rectBoxController.Visible = true;

                        //UpdateTransformedShape2();
                    }
                }
            };
        }
Пример #2
0
        protected override void OnStart(AppHost host)
        {
            _host      = host;
            _vgVisElem = VgVisualDocHelper.CreateVgVisualDocFromFile(@"Samples\lion.svg").VgRootElem;
            _mySprite  = new MyTestSprite(_vgVisElem);
            var evListener = new GeneralEventListener();

            evListener.MouseDown += e =>
            {
                if (e.Button == UIMouseButtons.Right)
                {
                    VgVisualElement foundE = _mySprite.HitTest(e.X, e.Y, true);
                    if (foundE != null)
                    {
                        foundE.VisualSpec.FillColor = Color.Red;
                        _mySprite.InvalidateLayout();
                    }
                }
            };
            evListener.MouseMove += e =>
            {
                if (e.Button == UIMouseButtons.Left && e.IsDragging)
                {
                    if (e.Ctrl)
                    {
                        //TODO:
                        //classic Agg's move and rotate
                    }
                    else
                    {   //just move
                        _mySprite.SetLocation(_mySprite.X + e.XDiff, _mySprite.Y + e.YDiff);
                    }
                }
            };

            _mySprite.AttachExternalEventListener(evListener);

            host.AddChild(_mySprite);



            ////
            //var textbox = new LayoutFarm.CustomWidgets.TextBox(100, 30, false);
            //var textSpanStyle = new Text.TextSpanStyle();

            ////test with various font style
            //textSpanStyle.FontInfo = new PixelFarm.Drawing.RequestFont("tahoma", 18);
            //textSpanStyle.FontColor = new PixelFarm.Drawing.Color(255, 0, 0);
            ////set default style
            //textbox.DefaultSpanStyle = textSpanStyle;

            //host.AddChild(textbox);


            //var box1 = new LayoutFarm.CustomWidgets.Box(50, 50);
            //box1.BackColor = Color.Red;
            //box1.SetLocation(10, 10);
            //host.AddChild(box1);
            ////--------------------------------
            //var box2 = new LayoutFarm.CustomWidgets.Box(30, 30);
            //box2.SetLocation(50, 50);
            //host.AddChild(box2);
            ////1. mouse down
            //box1.MouseDown += (s, e) =>
            //{
            //    box1.BackColor = KnownColors.FromKnownColor(KnownColor.DeepSkyBlue);
            //    box2.Visible = false;
            //};
            //box1.MouseUp += (s, e) =>
            //{
            //    box1.BackColor = Color.Red;
            //    box2.Visible = true;
            //};
        }
Пример #3
0
        protected override void OnStart(AppHost host)
        {
            _vgVisualDocHost = new VgVisualDocHost();
            _vgVisualDocHost.SetInvalidateDelegate(vgElem =>
            {
                //invalidate graphic here
            });

            _backBoard           = new BackDrawBoardUI(800, 600);
            _backBoard.BackColor = Color.White;
            host.AddChild(_backBoard);

            //load lion svg

            string svgfile = "../Test8_HtmlRenderer.Demo/Samples/Svg/others/tiger.svg";
            //string svgfile = "1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f30b.svg";
            //string svgfile = "../Data/1f30b.svg";
            //string svgfile = "../Data/Svg/twemoji/1f370.svg";

            VgVisualDoc     vgDocRoot = ReadSvgFile(svgfile);
            VgVisualElement vgVisElem = vgDocRoot.VgRootElem;
            var             uiSprite  = new UISprite(10, 10);

            uiSprite.LoadVg(vgVisElem);
            _backBoard.AddChild(uiSprite);


            //
            _redBox           = new LayoutFarm.CustomWidgets.Box(50, 50); //visual rect box
            _redBox.BackColor = Color.Red;
            _redBox.SetLocation(10, 10);
            //box1.dbugTag = 1;
            SetupActiveBoxProperties(_redBox);
            _backBoard.AddChild(_redBox);
            //----------------------


            //--------
            _rectBoxController = new CustomWidgets.RectBoxController();
            _rectBoxController.Init();
            _backBoard.AddChild(_rectBoxController);

            //foreach (var ui in rectBoxController.GetControllerIter())
            //{
            //    viewport.AddContent(ui);
            //}

            //--------
            var svgEvListener = new GeneralEventListener();

            //uiSprite.AttachExternalEventListener(svgEvListener);
            svgEvListener.MouseDown += (e) =>
            {
                //e.MouseCursorStyle = MouseCursorStyle.Pointer;
                ////--------------------------------------------
                //e.SetMouseCapture(rectBoxController.ControllerBoxMain);
                _rectBoxController.UpdateControllerBoxes(_redBox);
                _rectBoxController.Focus();
                //System.Console.WriteLine("click :" + (count++));
            };
            _rectBoxController.ControllerBoxMain.KeyDown += (s1, e1) =>
            {
                if (e1.KeyCode == UIKeys.C && e1.Ctrl)
                {
                    //test copy back image buffer from current rect area

#if DEBUG
                    //int left = rectBoxController.ControllerBoxMain.Left;
                    //int top = rectBoxController.ControllerBoxMain.Top;
                    //int width = rectBoxController.ControllerBoxMain.Width;
                    //int height = rectBoxController.ControllerBoxMain.Height;

                    //using (DrawBoard drawBoard = DrawBoardCreator.CreateNewDrawBoard(1, width, height))
                    //{

                    //    //create new draw board
                    //    drawBoard.OffsetCanvasOrigin(left, top);
                    //    _backBoard.CurrentPrimaryRenderElement.CustomDrawToThisCanvas(drawBoard, new Rectangle(0, 0, width, height));
                    //    using (var img2 = new PixelFarm.CpuBlit.ActualBitmap(width, height))
                    //    {
                    //        //copy content from drawboard to target image and save
                    //        drawBoard.RenderTo(img2, 0, 0, width, height);

                    //        PixelFarm.CpuBlit.Imaging.PngImageWriter.SaveImgBufferToPngFile(
                    //            PixelFarm.CpuBlit.ActualBitmap.GetBufferPtr(img2),
                    //            img2.Stride,
                    //            img2.Width,
                    //            img2.Height,
                    //            "d:\\WImageTest\\tiger.png");
                    //    }
                    //    //copy content from drawboard to target image and save
                    //}
#endif
                }
            };
        }
Пример #4
0
        VgVisualElement CreateLinearGradient(VgVisualElement parentNode, SvgElement elem, SvgLinearGradientSpec spec)
        {
            //https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient

            //Categories Gradient element
            //Permitted content Any number of the following elements, in any order:
            //Descriptive elements
            //<animate>, <animateTransform>, <set>, <stop>

            //Attributes
            //Section
            //Global attributes
            //Section

            //    Core attributes
            //    Presentation attributes
            //    Xlink attributes
            //    class
            //    style
            //    externalResourcesRequired

            //Specific attributes
            //Section

            //    gradientUnits
            //    gradientTransform
            //    x1
            //    y1
            //    x2
            //    y2
            //    spreadMethod
            //    xlink:href



            VgVisualElement linearGrd = new VgVisualElement(WellknownSvgElementName.LinearGradient, spec, _vgVisualDoc);

            //read attribute
            RegisterElementById(elem, linearGrd);
            int childCount = elem.ChildCount;

            for (int i = 0; i < childCount; ++i)
            {
                SvgElement child = elem.GetChild(i);
                if (child.ElemName == "stop")
                {
                    //color stop
                    //TODO: implement this....
                    SvgColorStopSpec stopSpec = child.ElemSpec as SvgColorStopSpec;
                    if (stopSpec != null)
                    {
                        if (spec.StopList == null)
                        {
                            //TODO
                            spec.StopList = new List <SvgColorStopSpec>();
                        }
                        spec.StopList.Add(stopSpec);
                    }
                }
                else
                {
                }
            }

            return(linearGrd);
        }
Пример #5
0
        VgVisualElement CreateTextElem(VgVisualElement parentNode, SvgElement elem, SvgTextSpec textspec)
        {
            //text render element
            VgVisualElement vgVisElem = new VgVisualElement(WellknownSvgElementName.Text, textspec, _vgVisualDoc);

            vgVisElem.DomElem = elem;
            //some att

            if (textspec.Class != null && _activeSheet1 != null)
            {
                //resolve style definition
                LayoutFarm.WebDom.CssRuleSetGroup ruleSetGroup = _activeSheet1.GetRuleSetForClassName(textspec.Class);
                if (ruleSetGroup != null)
                {
                    //assign
                    foreach (LayoutFarm.WebDom.CssPropertyDeclaration propDecl in ruleSetGroup.GetPropertyDeclIter())
                    {
                        switch (propDecl.WellknownPropertyName)
                        {
                        case LayoutFarm.WebDom.WellknownCssPropertyName.Font:
                            //set font detail
                            break;

                        case LayoutFarm.WebDom.WellknownCssPropertyName.FontStyle:
                            //convert font style
                            break;

                        case LayoutFarm.WebDom.WellknownCssPropertyName.FontSize:
                            textspec.FontSize = propDecl.GetPropertyValue(0).AsLength();
                            break;

                        case LayoutFarm.WebDom.WellknownCssPropertyName.FontFamily:
                            textspec.FontFace = propDecl.GetPropertyValue(0).ToString();
                            break;

                        case LayoutFarm.WebDom.WellknownCssPropertyName.Fill:
                            textspec.FillColor = LayoutFarm.WebDom.Parser.CssValueParser.ParseCssColor(propDecl.GetPropertyValue(0).ToString());
                            break;

                        case LayoutFarm.WebDom.WellknownCssPropertyName.Unknown:
                        {
                            switch (propDecl.UnknownRawName)
                            {
                            case "fill":
                                //svg
                                break;
                            }
                        }
                        break;
                        }
                    }
                }
            }


            ReEvaluateArgs a = new ReEvaluateArgs(_containerWidth, _containerHeight, _emHeight); //temp fix

            textspec.ActualX = ConvertToPx(textspec.X, ref a);
            textspec.ActualY = ConvertToPx(textspec.Y, ref a);


            AssignAttributes(textspec);

            //text x,y


            parentNode.AddChildElement(vgVisElem);
            return(vgVisElem);
        }
Пример #6
0
        VgVisualElement CreateSvgVisualElement(VgVisualElement parentNode, SvgElement elem)
        {
            VgVisualElement vgVisElem        = null;
            bool            skipChildrenNode = false;

            switch (elem.WellknowElemName)
            {
            default:
                throw new NotSupportedException();

            //-----------------
            case WellknownSvgElementName.Unknown:
                return(null);

            case WellknownSvgElementName.FeColorMatrix:
                vgVisElem = CreateFeColorMatrix(parentNode, elem, (SvgFeColorMatrixSpec)elem.ElemSpec);    //no more child
                parentNode.AddChildElement(vgVisElem);
                return(vgVisElem);

            case WellknownSvgElementName.Filter:
                vgVisElem = CreateFilterElem(parentNode, elem, (SvgFilterSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.RadialGradient:
                //TODO: add radial grapdient support
                //this version not support linear gradient
                return(CreateRadialGradient(parentNode, elem, (SvgRadialGradientSpec)elem.ElemSpec));

            case WellknownSvgElementName.LinearGradient:
                //TODO: add linear grapdient support
                //this version not support linear gradient
                return(CreateLinearGradient(parentNode, elem, (SvgLinearGradientSpec)elem.ElemSpec));

            case WellknownSvgElementName.Defs:
                _defsList.Add(elem);
                return(null);

            case WellknownSvgElementName.Style:
                _styleList.Add(elem);
                return(null);

            case WellknownSvgElementName.Marker:
                vgVisElem = CreateMarker(parentNode, (SvgMarkerSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Mask:
                vgVisElem = CreateMask(parentNode, (SvgMaskSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Svg:
                vgVisElem = new VgVisualElement(WellknownSvgElementName.Svg, (SvgVisualSpec)elem.ElemSpec, _vgVisualDoc);
                break;

            case WellknownSvgElementName.Rect:
                vgVisElem = CreateRect(parentNode, (SvgRectSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Image:
                vgVisElem = CreateImage(parentNode, (SvgImageSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Polyline:
                vgVisElem = CreatePolyline(parentNode, (SvgPolylineSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Polygon:
                vgVisElem = CreatePolygon(parentNode, (SvgPolygonSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Ellipse:
                vgVisElem = CreateEllipse(parentNode, (SvgEllipseSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Circle:
                vgVisElem = CreateCircle(parentNode, (SvgCircleSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Line:
                vgVisElem = CreateLine(parentNode, (SvgLineSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.ClipPath:
                vgVisElem = CreateClipPath(parentNode, (SvgVisualSpec)elem.ElemSpec);
                break;

            case WellknownSvgElementName.Group:
                vgVisElem = CreateGroup(parentNode, (SvgVisualSpec)elem.ElemSpec);
                break;

            //---------------------------------------------
            case WellknownSvgElementName.Path:
                vgVisElem        = CreatePath(parentNode, (SvgPathSpec)elem.ElemSpec, elem);
                skipChildrenNode = true;    //***
                break;

            case WellknownSvgElementName.Text:
                vgVisElem        = CreateTextElem(parentNode, elem, (SvgTextSpec)elem.ElemSpec);
                skipChildrenNode = true;    //***
                break;

            case WellknownSvgElementName.Use:
                vgVisElem        = CreateUseElement(parentNode, (SvgUseSpec)elem.ElemSpec);
                skipChildrenNode = true;
                break;
            }

            if (vgVisElem == null)
            {
                //TODO: review here
                return(null);
            }
            //-----------------------------------
            vgVisElem.DomElem = elem;
            if (elem.ElemId != null)
            {
                //replace duplicated item ???
                _registeredElemsById[elem.ElemId] = vgVisElem;
            }

            vgVisElem.SetController(elem);


#if DEBUG
            if (skipChildrenNode && !vgVisElem.dbugHasParent)
            {
            }
#endif
            //-----------------------------------
            if (!skipChildrenNode)
            {
                parentNode.AddChildElement(vgVisElem);
                int childCount = elem.ChildCount;
                for (int i = 0; i < childCount; ++i)
                {
                    CreateSvgVisualElement(vgVisElem, elem.GetChild(i));
                }
            }

            return(vgVisElem);
        }
Пример #7
0
        VgVisualElement CreatePolyline(VgVisualElement parentNode, SvgPolylineSpec polylineSpec)
        {
            VgVisualElement vgPolyline = new VgVisualElement(WellknownSvgElementName.Polyline, polylineSpec, _vgVisualDoc);

            PointF[] points = polylineSpec.Points;
            int      j      = points.Length;

            if (j > 1)
            {
                using (VxsTemp.Borrow(out var v1))
                {
                    PointF p = points[0];
                    v1.AddMoveTo(p.X, p.Y);
                    for (int i = 1; i < j; ++i)
                    {
                        p = points[i];
                        v1.AddLineTo(p.X, p.Y);
                    }
                    vgPolyline.VxsPath = v1.CreateTrim();
                }
                AssignAttributes(polylineSpec);

                //--------------------------------------------------------------------
                ResolveMarkers(vgPolyline, polylineSpec);
                if (vgPolyline._pathMarkers != null)
                {
                    //create primary instance plan for this polyline
                    VgPathVisualMarkers pathMarkers = vgPolyline._pathMarkers;
                    pathMarkers.AllPoints = points;

                    //start, mid, end
                    if (pathMarkers.StartMarker != null)
                    {
                        //turn marker to the start direction
                        PointF p0 = points[0];
                        PointF p1 = points[1];
                        //find rotation angle
                        double        rotateRad  = Math.Atan2(p0.Y - p1.Y, p0.X - p1.X);
                        SvgMarkerSpec markerSpec = (SvgMarkerSpec)pathMarkers.StartMarker._visualSpec;

                        //create local-transformation matrix
                        pathMarkers.StartMarkerPos    = new PointF(p0.X, p0.Y);
                        pathMarkers.StartMarkerAffine = Affine.NewMatix(
                            AffinePlan.Translate(-markerSpec.RefX.Number, -markerSpec.RefY.Number), //move to the ref point
                            AffinePlan.Rotate(rotateRad)                                            //rotate
                            );
                    }
                    //-------------------------------
                    if (pathMarkers.MidMarker != null)
                    {
                        SvgMarkerSpec markerSpec = (SvgMarkerSpec)pathMarkers.StartMarker._visualSpec;
                        pathMarkers.MidMarkerAffine = Affine.NewTranslation(-markerSpec.RefX.Number, -markerSpec.RefY.Number);
                    }
                    //-------------------------------
                    if (pathMarkers.EndMarker != null)
                    {
                        //turn marker to the start direction
                        PointF p0 = points[j - 2]; //before the last one
                        PointF p1 = points[j - 1]; //the last one
                                                   //find rotation angle
                        double        rotateRad  = Math.Atan2(p1.Y - p0.Y, p1.X - p0.X);
                        SvgMarkerSpec markerSpec = (SvgMarkerSpec)pathMarkers.EndMarker._visualSpec;


                        //create local-transformation matrix
                        pathMarkers.EndMarkerPos    = new PointF(p1.X, p1.Y);
                        pathMarkers.EndMarkerAffine = Affine.NewMatix(
                            AffinePlan.Translate(-markerSpec.RefX.Number, -markerSpec.RefY.Number), //move to the ref point
                            AffinePlan.Rotate(rotateRad)                                            //rotate
                            );
                    }
                }
            }
            return(vgPolyline);
        }
Пример #8
0
 internal void InvalidateGraphics(VgVisualElement e)
 {
     //***
     _invalidate?.Invoke(e);
 }
Пример #9
0
 void ImgBinderLoadImg(PixelFarm.Drawing.ImageBinder reqImgBinder, VgVisualElement vgVisualE, object o)
 {
     PixelFarm.Drawing.Image img = _host.LoadImage(reqImgBinder.ImageSource);
     reqImgBinder.SetLocalImage(img);
     reqImgBinder.State = PixelFarm.Drawing.BinderState.Loaded;
 }
Пример #10
0
        UISprite CreateUISpriteFromVgVisualElem(VgVisualElement vgVisualElem, bool wrapWithVgVisualUse = false)
        {
            if (wrapWithVgVisualUse)
            {
                //then we create a 'svg use' element
                //and wrap the original vgVisualElement
                vgVisualElem = _vgVisualDoc.CreateVgUseVisualElement(vgVisualElem);
            }

            PixelFarm.CpuBlit.RectD org_rectD = vgVisualElem.GetRectBounds();
            org_rectD.Offset(-org_rectD.Left, -org_rectD.Bottom);
            vgVisualElem.DisableBackingImage = true;
            //-----------------------------------------
            UISprite uiSprite = new UISprite(10, 10); //init size = (10,10), location=(0,0)

            uiSprite.DisableBmpCache = true;



            uiSprite.LoadVg(vgVisualElem);//



            _appHost.AddChild(uiSprite);

            var spriteEvListener = new GeneralEventListener();

            uiSprite.AttachExternalEventListener(spriteEvListener);
            spriteEvListener.MouseMove += (s1, e1) =>
            {
                if (e1.IsDragging)
                {
                    //when drag on sprie
                    uiSprite.InvalidateOuterGraphics();
                    uiSprite.SetLocation(
                        uiSprite.Left + e1.XDiff,
                        uiSprite.Top + e1.YDiff);
                }
            };
            spriteEvListener.MouseDown += (s1, e1) =>
            {
                if (_hitTestOnSubPath)
                {
                    //find which part ...
                    VgHitInfo hitInfo = uiSprite.FindRenderElementAtPos(e1.X, e1.Y, true);

                    if (hitInfo.hitElem != null &&
                        hitInfo.hitElem.VxsPath != null)
                    {
                        PixelFarm.CpuBlit.RectD bounds = hitInfo.copyOfVxs.GetBoundingRect();
                    }
                    else
                    {
                        //_rectBoundsWidgetBox.Visible = false;
                        // _rectBoxController.Visible = false;
                    }
                }
                else
                {
                    //hit on sprite
                    if (e1.Ctrl)
                    {
                        //test***
                        //
                        uiSprite.GetElementBounds(out float left, out float top, out float right, out float bottom);
                        //
                        using (Tools.BorrowRect(out SimpleRect s))
                            using (Tools.BorrowVxs(out var v1))
                            {
                                s.SetRect(left - uiSprite.ActualXOffset,
                                          bottom - uiSprite.ActualYOffset,
                                          right - uiSprite.ActualXOffset,
                                          top - uiSprite.ActualYOffset);
                                //TODO: review here
                                //s.MakeVxs(v1);
                                //_polygonController.UpdateControlPoints(v1.CreateTrim());
                            }
                    }
                    else
                    {
                        //_rectBoundsWidgetBox.SetTarget(_uiSprite);
                        //_rectBoundsWidgetBox.SetLocationAndSize(    //blue
                        //      (int)_uiSprite.Left, (int)_uiSprite.Top,
                        //      (int)_uiSprite.Width, (int)_uiSprite.Height);
                        ////_rectBoxController.SetTarget(_uiSprite);

                        ////_rectBoxController.UpdateControllerBoxes(_rectBoundsWidgetBox);  //control 4 corners
                        //_rectBoundsWidgetBox.Visible = true;
                        ////_rectBoxController.Visible = true;

                        //UpdateTransformedShape2();
                    }
                }
            };

            return(uiSprite);
        }
Пример #11
0
 public dbugMySprite(int w, int h) : base(w, h)
 {
     _renderVx = VgVisualDocHelper.CreateVgVisualDocFromFile(@"lion.svg").VgRootElem;
 }
Пример #12
0
 public void AddHit(VgVisualElement svg, float x, float y, VertexStore copyOfVxs)
 {
     _vgHitList.Add(new VgHitInfo(svg, x, y, copyOfVxs));
 }
Пример #13
0
 public SpriteShape(VgVisualElement vgVisElem) //, RootGraphic root, int w, int h)
                                               //: base(root, w, h)
 {
     LoadFromSvg(vgVisElem);
 }
Пример #14
0
 internal virtual void Reset()
 {
     _currentTx = null;
     Current    = null;
 }
Пример #15
0
        protected override void OnStart(AppHost host)
        {
            _background           = new Box(800, 600);
            _background.BackColor = Color.White;
            host.AddChild(_background);



            //---------------------------
            _vgVisElem = VgVisualDocHelper.CreateVgVisualDocFromFile(@"Samples\lion.svg").VgRootElem;
            _mySprite  = new MyTestSprite(_vgVisElem);
            //var evListener = new GeneralEventListener();
            //evListener.MouseDrag += (s, e) =>
            //{
            //    if (e.Ctrl)
            //    {
            //        //TODO:
            //        //classic Agg's move and rotate

            //    }
            //    else
            //    {   //just move
            //        _mySprite.SetLocation(_mySprite.Left + e.XDiff, _mySprite.Top + e.YDiff);
            //    }
            //};
            //_mySprite.AttachExternalEventListener(evListener);


            var rectBounds = _vgVisElem.GetRectBounds();

            _quadControl = new QuadWidgetControl();
            _quadControl.ShapeUpdated += _quadControl_ShapeUpdated;
            //
            _quadControl.SetupCorners(
                new Quad2f(
                    (float)rectBounds.Left,
                    (float)rectBounds.Bottom,
                    (float)rectBounds.Width,
                    (float)rectBounds.Height));

            _lionBounds = new RectangleF((float)rectBounds.Left,
                                         (float)rectBounds.Bottom,
                                         (float)rectBounds.Width,
                                         (float)rectBounds.Height);

            host.AddChild(_mySprite);
            host.AddChild(_quadControl);


            //--------------------
            _cmdBiliear = new Box(30, 30);
            _cmdBiliear.SetLocation(400, 20);
            _cmdBiliear.BackColor  = Color.Yellow;
            _cmdBiliear.MouseDown += (s, e) =>
            {
                if (_useBilinear)
                {
                    _cmdBiliear.BackColor = Color.Yellow;
                    _useBilinear          = false;
                }
                else
                {
                    _cmdBiliear.BackColor = Color.Red;
                    _useBilinear          = true;
                }
            };
            host.AddChild(_cmdBiliear);
        }
Пример #16
0
        public static void ParseAndRenderSvg(PixelFarm.Drawing.SvgBmpBuilderReq req)
        {
            //----------
            //copy from HtmlRenderer's SvgViewer demo
            //----------
            var          docBuilder   = new VgDocBuilder();
            var          parser       = new SvgParser(docBuilder);
            TextSnapshot textSnapshot = new TextSnapshot(req.SvgContent.ToString());

            parser.ParseDocument(textSnapshot);

            VgVisualDocBuilder builder   = new VgVisualDocBuilder();
            VgVisualElement    vgVisElem = builder.CreateVgVisualDoc(docBuilder.ResultDocument, _vgDocHost).VgRootElem;

            PixelFarm.CpuBlit.VertexProcessing.Q1RectD bounds = vgVisElem.GetRectBounds();

            float actualXOffset = (float)-bounds.Left;
            float actualYOffset = (float)-bounds.Bottom;

            //original svg width, height
            int bmpW = (int)Math.Round(bounds.Width);
            int bmpH = (int)Math.Round(bounds.Height);

            if (bmpW == 0 || bmpH == 0)
            {
                return;
            }

            //scale svg to specific size
            float scale_w = req.ExpectedWidth / bmpW;

            //at this point, we have 2 options
            //1) create bitmap with original svg size and scale it down to expected size
            //2) scale svg to expected size and create a bitmap

            //we choose 2)

            int new_w = (int)Math.Round(bmpW * scale_w);
            int new_h = (int)Math.Round(bmpH * scale_w);

            MemBitmap memBitmap = new MemBitmap(new_w, new_h);

            using (Tools.BorrowAggPainter(memBitmap, out var p))
                using (Tools.More.BorrowVgPaintArgs(p, out VgPaintArgs paintArgs))
                {
                    //pass by

                    Affine tx = Affine.NewScaling(scale_w);
                    paintArgs._currentTx = tx;

                    float orgX = p.OriginX;
                    float orgY = p.OriginY;


                    p.SetOrigin(actualXOffset * scale_w, actualYOffset * scale_w);

                    p.Clear(req.DefaultBgColor);

                    p.FillColor = PixelFarm.Drawing.Color.Black;

                    double prevStrokeW = p.StrokeWidth;

                    vgVisElem.Paint(paintArgs);

                    p.StrokeWidth = prevStrokeW; //restore

                    p.SetOrigin(orgX, orgY);     //restore
                }

#if DEBUG
            //memBitmap.SaveImage("svg.png");
#endif
            req.Output = memBitmap;
        }