Exemple #1
0
        public static bool GetBoundingRect(this VertexStore vxs, ref Q1RectD rect)
        {
            bool rValue = GetBoundingRectSingle(vxs,
                                                out double x1, out double y1,
                                                out double x2, out double y2);

            if (x1 < rect.Left)
            {
                rect.Left = x1;
            }
            if (y1 < rect.Bottom)
            {
                rect.Bottom = y1;
            }
            if (x2 > rect.Right)
            {
                rect.Right = x2;
            }

            if (y2 > rect.Top)
            {
                rect.Top = y2;
            }

            return(rValue);
        }
Exemple #2
0
        public static Q1RectD GetBoundingRect(this VertexStore vxs)
        {
            Q1RectD bounds = Q1RectD.ZeroIntersection();

            return(GetBoundingRect(vxs, ref bounds) ?
                   bounds :
                   new Q1RectD());
        }
Exemple #3
0
        public static void GetBoundingRect(this VertexStore vxs, ref Q1Rect rect)
        {
            Q1RectD rect1 = new Q1RectD();

            BoundingRect.GetBoundingRect(vxs, ref rect1);
            rect.Left   = (int)System.Math.Round(rect1.Left);
            rect.Bottom = (int)System.Math.Round(rect1.Bottom);
            rect.Right  = (int)System.Math.Round(rect1.Right);
            rect.Top    = (int)System.Math.Round(rect1.Top);
        }
Exemple #4
0
 public void SetRect(double left, double bottom, double right, double top)
 {
     _bounds = new Q1RectD(left, bottom, right, top);
     if (left > right)
     {
         _bounds.Left = right; _bounds.Right = left;
     }
     if (bottom > top)
     {
         _bounds.Bottom = top; _bounds.Top = bottom;
     }
 }
        VgVisualElement CreateEllipseVxs(PixelFarm.CpuBlit.VertexProcessing.Q1RectD newBounds)
        {
            using (Tools.BorrowVxs(out var v1))
                using (Tools.BorrowEllipse(out Ellipse ellipse))
                {
                    ellipse.Set((newBounds.Left + newBounds.Right) * 0.5,
                                (newBounds.Bottom + newBounds.Top) * 0.5,
                                (newBounds.Right - newBounds.Left) * 0.5,
                                (newBounds.Top - newBounds.Bottom) * 0.5);


                    var spec = new SvgPathSpec()
                    {
                        FillColor = Color.Red
                    };
                    VgVisualDoc     renderRoot = new VgVisualDoc();
                    VgVisualElement renderE    = new VgVisualElement(WellknownSvgElementName.Path, spec, renderRoot);
                    renderE.VxsPath = ellipse.MakeVxs(v1).CreateTrim();
                    return(renderE);
                }
        }
Exemple #6
0
        public RoundedRect(double left, double bottom, double right, double top, double radius)
        {
            _bounds              = new Q1RectD(left, bottom, right, top);
            _leftBottomRadius.x  = radius;
            _leftBottomRadius.y  = radius;
            _rightBottomRadius.x = radius;
            _rightBottomRadius.y = radius;
            _rightTopRadius.x    = radius;
            _rightTopRadius.y    = radius;
            _leftTopRadius.x     = radius;
            _leftTopRadius.y     = radius;
            if (left > right)
            {
                _bounds.Left  = right;
                _bounds.Right = left;
            }

            if (bottom > top)
            {
                _bounds.Bottom = top;
                _bounds.Top    = bottom;
            }
        }
Exemple #7
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.VertexProcessing.Q1RectD 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.InvalidateGraphics();
                    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);
        }
Exemple #8
0
 ////
 public bool Contains(Q1RectD innerRect) => Contains(innerRect.Left, innerRect.Bottom) && Contains(innerRect.Right, innerRect.Top);
        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");
            //_vgVisualElem = VgVisualElemHelper.ReadSvgFile(svgfile);


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

            PixelFarm.CpuBlit.VertexProcessing.Q1RectD 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.InvalidateGraphics();
                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.InvalidateGraphics();


                    //_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.VertexProcessing.Q1RectD 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);

            double x_center = (svg_bounds.Left + svg_bounds.Right) / 2;
            double y_center = (svg_bounds.Top + svg_bounds.Bottom) / 2;

            _rotationUI.SetCenter(x_center, y_center);
            _rotationUI.SetRadius(x_center + 200, y_center);

            host.AddChild(_rotationUI);



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


            var spriteEvListener = new GeneralEventListener();

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


                    _uiSprite.InvalidateGraphics();
                    _uiSprite.SetLocation(
                        _uiSprite.Left + e1.XDiff,
                        _uiSprite.Top + e1.YDiff
                        );
                    //we also move quadController and _quadPolygonController
                    //
                    _quadController.InvalidateGraphics();
                    _quadController.SetLocation(
                        _quadController.Left + e1.XDiff,
                        _quadController.Top + e1.YDiff);
                    _quadController.InvalidateGraphics();
                    //
                    _quadPolygonController.InvalidateGraphics();
                    _quadPolygonController.SetLocation(
                        _quadPolygonController.Left + e1.XDiff,
                        _quadPolygonController.Top + e1.YDiff
                        );
                    _quadPolygonController.InvalidateGraphics();
                    //
                    _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 += (s1, 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 ...

                    double            e1_x = e1.X;
                    double            e1_y = e1.Y;
                    ICoordTransformer tx1  = _quadController.GetCoordTransformer();
                    if (tx1 != null)
                    {
                        //if the sprite is transformed before render
                        //we must invert x,y back to
                        ICoordTransformer tx1_inv = tx1.CreateInvert();
                        tx1_inv.Transform(ref e1_x, ref e1_y);
                    }

                    VgHitInfo hitInfo = _uiSprite.FindRenderElementAtPos((float)e1_x, (float)e1_y, true);

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

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

                        ////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 (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);
                                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();
                    }
                }
            };
        }
Exemple #10
0
 public RoundedRect(Q1RectD bounds, double r)
     : this(bounds.Left, bounds.Bottom, bounds.Right, bounds.Top, r)
 {
 }
        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;
        }