Пример #1
0
        internal virtual AffineTransform CalculateTransformation(SvgDrawContext context)
        {
            Rectangle viewPort   = context.GetCurrentViewPort();
            float     horizontal = viewPort.GetX();
            float     vertical   = viewPort.GetY() + viewPort.GetHeight();
            // flip coordinate space vertically and translate on the y axis with the viewport height
            AffineTransform transform = AffineTransform.GetTranslateInstance(0, 0);

            //Identity-transform
            transform.Concatenate(AffineTransform.GetTranslateInstance(horizontal, vertical));
            transform.Concatenate(new AffineTransform(1, 0, 0, -1, 0, 0));
            return(transform);
        }
Пример #2
0
        private void ApplyCoordinatesTranslation(SvgDrawContext context)
        {
            float xScale = 1;
            float yScale = 1;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.VIEWBOX))
            {
                //Parse viewbox parameters stuff
                String         viewBoxValues = attributesAndStyles.Get(SvgConstants.Attributes.VIEWBOX);
                IList <String> valueStrings  = SvgCssUtils.SplitValueList(viewBoxValues);
                float[]        viewBox       = GetViewBoxValues();
                xScale = context.GetCurrentViewPort().GetWidth() / viewBox[2];
                yScale = context.GetCurrentViewPort().GetHeight() / viewBox[3];
            }
            float moveX = DEFAULT_REF_X;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.REFX))
            {
                String refX = this.attributesAndStyles.Get(SvgConstants.Attributes.REFX);
                moveX = ParseAbsoluteLength(refX, context.GetRootViewPort().GetWidth(), moveX, context);
                //Apply scale
                moveX *= -1 * xScale;
            }
            float moveY = DEFAULT_REF_Y;

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.REFY))
            {
                String refY = this.attributesAndStyles.Get(SvgConstants.Attributes.REFY);
                moveY  = ParseAbsoluteLength(refY, context.GetRootViewPort().GetHeight(), moveY, context);
                moveY *= -1 * yScale;
            }
            AffineTransform translation = AffineTransform.GetTranslateInstance(moveX, moveY);

            if (!translation.IsIdentity())
            {
                context.GetCurrentCanvas().ConcatMatrix(translation);
            }
        }
 // transformation already happened in AbstractSvgNodeRenderer, so no need to do a transformation here
 /// <summary>Applies a transformation based on a viewBox for a given branch node.</summary>
 /// <param name="context">current svg draw context</param>
 private void ApplyViewBox(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.VIEWBOX))
         {
             //Parse aspect ratio related stuff
             String         viewBoxValues = attributesAndStyles.Get(SvgConstants.Attributes.VIEWBOX);
             IList <String> valueStrings  = SvgCssUtils.SplitValueList(viewBoxValues);
             float[]        values        = new float[valueStrings.Count];
             for (int i = 0; i < values.Length; i++)
             {
                 values[i] = CssUtils.ParseAbsoluteLength(valueStrings[i]);
             }
             Rectangle       currentViewPort = context.GetCurrentViewPort();
             float           scaleWidth      = currentViewPort.GetWidth() / values[2];
             float           scaleHeight     = currentViewPort.GetHeight() / values[3];
             AffineTransform scale           = AffineTransform.GetScaleInstance(scaleWidth, scaleHeight);
             if (!scale.IsIdentity())
             {
                 context.GetCurrentCanvas().ConcatMatrix(scale);
                 //Inverse scaling needs to be applied to viewport dimensions
                 context.GetCurrentViewPort().SetWidth(currentViewPort.GetWidth() / scaleWidth);
                 context.GetCurrentViewPort().SetX(currentViewPort.GetX() / scaleWidth);
                 context.GetCurrentViewPort().SetHeight(currentViewPort.GetHeight() / scaleHeight);
                 context.GetCurrentViewPort().SetY(currentViewPort.GetY() / scaleHeight);
             }
             AffineTransform transform = ProcessAspectRatio(context, values);
             if (!transform.IsIdentity())
             {
                 //TODO (RND-876)
                 context.GetCurrentCanvas().WriteLiteral("% applying viewbox aspect ratio correction (not correct) \n");
             }
         }
     }
 }
 //context.getCurrentCanvas().concatMatrix(transform);
 /// <summary>Applies a clipping operation based on the view port.</summary>
 /// <param name="context">the svg draw context</param>
 private void ApplyViewport(SvgDrawContext context)
 {
     if (GetParent() != null && GetParent() is AbstractSvgNodeRenderer)
     {
         AbstractSvgNodeRenderer parent = (AbstractSvgNodeRenderer)GetParent();
         PdfCanvas currentCanvas        = context.GetCurrentCanvas();
         currentCanvas.Rectangle(context.GetCurrentViewPort());
         currentCanvas.Clip();
         currentCanvas.NewPath();
         if (parent.CanConstructViewPort())
         {
             currentCanvas.ConcatMatrix(parent.CalculateViewPortTranslation(context));
         }
     }
 }
Пример #5
0
 /// <summary>
 /// Method that will set properties to be inherited by this branch renderer's
 /// children and will iterate over all children in order to draw them.
 /// </summary>
 /// <param name="context">
 /// the object that knows the place to draw this element and
 /// maintains its state
 /// </param>
 protected internal override void DoDraw(SvgDrawContext context)
 {
     // if branch has no children, don't do anything
     if (GetChildren().Count > 0)
     {
         PdfStream stream = new PdfStream();
         stream.Put(PdfName.Type, PdfName.XObject);
         stream.Put(PdfName.Subtype, PdfName.Form);
         PdfFormXObject xObject   = (PdfFormXObject)PdfXObject.MakeXObject(stream);
         PdfCanvas      newCanvas = new PdfCanvas(xObject, context.GetCurrentCanvas().GetDocument());
         ApplyViewBox(context);
         bool overflowVisible = IsOverflowVisible(this);
         // TODO (DEVSIX-3482) Currently overflow logic works only for markers.  Update this code after the ticket will be finished.
         if (this is MarkerSvgNodeRenderer && overflowVisible)
         {
             WriteBBoxAccordingToVisibleOverflow(context, stream);
         }
         else
         {
             Rectangle bbBox = context.GetCurrentViewPort().Clone();
             stream.Put(PdfName.BBox, new PdfArray(bbBox));
         }
         if (this is MarkerSvgNodeRenderer)
         {
             ((MarkerSvgNodeRenderer)this).ApplyMarkerAttributes(context);
         }
         context.PushCanvas(newCanvas);
         // TODO (DEVSIX-3482) Currently overflow logic works only for markers. Update this code after the ticket will be finished.
         if (!(this is MarkerSvgNodeRenderer) || !overflowVisible)
         {
             ApplyViewportClip(context);
         }
         ApplyViewportTranslationCorrection(context);
         foreach (ISvgNodeRenderer child in GetChildren())
         {
             if (!(child is MarkerSvgNodeRenderer))
             {
                 newCanvas.SaveState();
                 child.Draw(context);
                 newCanvas.RestoreState();
             }
         }
         CleanUp(context);
         // transformation already happened in AbstractSvgNodeRenderer, so no need to do a transformation here
         context.GetCurrentCanvas().AddXObject(xObject, 0, 0);
     }
 }
Пример #6
0
        /// <summary>Calculate the viewport based on the context.</summary>
        /// <param name="context">the SVG draw context</param>
        /// <returns>the viewport that applies to this renderer</returns>
        internal virtual Rectangle CalculateViewPort(SvgDrawContext context)
        {
            Rectangle currentViewPort = context.GetCurrentViewPort();
            float     portX           = 0f;
            float     portY           = 0f;
            float     portWidth       = 0f;
            float     portHeight      = 0f;

            // set default values to parent viewport in the case of a nested svg tag
            portX     = currentViewPort.GetX();
            portY     = currentViewPort.GetY();
            portWidth = currentViewPort.GetWidth();
            // default should be parent portWidth if not outermost
            portHeight = currentViewPort.GetHeight();
            // default should be parent height if not outermost
            if (attributesAndStyles != null)
            {
                if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.X))
                {
                    portX = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.X));
                }
                if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y))
                {
                    portY = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.Y));
                }
                if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.WIDTH))
                {
                    portWidth = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.WIDTH));
                }
                if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.HEIGHT))
                {
                    portHeight = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.HEIGHT));
                }
            }
            return(new Rectangle(portX, portY, portWidth, portHeight));
        }
Пример #7
0
        /// <summary>If present, process the preserveAspectRatio position.</summary>
        /// <param name="context">the svg draw context</param>
        /// <param name="viewBoxValues">the four values depicting the viewbox [min-x min-y width height]</param>
        /// <param name="align">alignment method to use</param>
        /// <param name="scaleWidth">the multiplier for scaling width</param>
        /// <param name="scaleHeight">the multiplier for scaling height</param>
        /// <returns>the transformation based on the preserveAspectRatio value</returns>
        internal virtual AffineTransform ProcessAspectRatioPosition(SvgDrawContext context, float[] viewBoxValues,
                                                                    String align, float scaleWidth, float scaleHeight)
        {
            AffineTransform transform       = new AffineTransform();
            Rectangle       currentViewPort = context.GetCurrentViewPort();
            float           midXBox         = viewBoxValues[0] + (viewBoxValues[2] / 2);
            float           midYBox         = viewBoxValues[1] + (viewBoxValues[3] / 2);
            float           midXPort        = currentViewPort.GetX() + (currentViewPort.GetWidth() / 2);
            float           midYPort        = currentViewPort.GetY() + (currentViewPort.GetHeight() / 2);
            float           x = 0f;
            float           y = 0f;

            // if x attribute of svg is present, then x value of current viewport should be set according to it
            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.X))
            {
                x = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.X));
            }
            // if y attribute of svg is present, then y value of current viewport should be set according to it
            if (attributesAndStyles.ContainsKey(SvgConstants.Attributes.Y))
            {
                y = CssUtils.ParseAbsoluteLength(attributesAndStyles.Get(SvgConstants.Attributes.Y));
            }
            // need to consider previous (parent) translation before applying the current one
            x -= currentViewPort.GetX();
            y -= currentViewPort.GetY();
            switch (align.ToLowerInvariant())
            {
            case SvgConstants.Values.NONE: {
                break;
            }

            case SvgConstants.Values.XMIN_YMIN: {
                x -= viewBoxValues[0];
                y -= viewBoxValues[1];
                break;
            }

            case SvgConstants.Values.XMIN_YMID: {
                x -= viewBoxValues[0];
                y += (midYPort - midYBox);
                break;
            }

            case SvgConstants.Values.XMIN_YMAX: {
                x -= viewBoxValues[0];
                y += (currentViewPort.GetHeight() - viewBoxValues[3]);
                break;
            }

            case SvgConstants.Values.XMID_YMIN: {
                x += (midXPort - midXBox);
                y -= viewBoxValues[1];
                break;
            }

            case SvgConstants.Values.XMID_YMAX: {
                x += (midXPort - midXBox);
                y += (currentViewPort.GetHeight() - viewBoxValues[3]);
                break;
            }

            case SvgConstants.Values.XMAX_YMIN: {
                x += (currentViewPort.GetWidth() - viewBoxValues[2]);
                y -= viewBoxValues[1];
                break;
            }

            case SvgConstants.Values.XMAX_YMID: {
                x += (currentViewPort.GetWidth() - viewBoxValues[2]);
                y += (midYPort - midYBox);
                break;
            }

            case SvgConstants.Values.XMAX_YMAX: {
                x += (currentViewPort.GetWidth() - viewBoxValues[2]);
                y += (currentViewPort.GetHeight() - viewBoxValues[3]);
                break;
            }

            case SvgConstants.Values.DEFAULT_ASPECT_RATIO:
            default: {
                x += (midXPort - midXBox);
                y += (midYPort - midYBox);
                break;
            }
            }
            //Rescale x and y
            x /= scaleWidth;
            y /= scaleHeight;
            transform.Translate(x, y);
            return(transform);
        }
        /// <summary>If present, process the preserveAspectRatio.</summary>
        /// <param name="context">the svg draw context</param>
        /// <param name="viewBoxValues">the four values depicting the viewbox [min-x min-y width height]</param>
        /// <returns>the transformation based on the preserveAspectRatio value</returns>
        private AffineTransform ProcessAspectRatio(SvgDrawContext context, float[] viewBoxValues)
        {
            AffineTransform transform = new AffineTransform();

            if (this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.PRESERVE_ASPECT_RATIO))
            {
                Rectangle currentViewPort          = context.GetCurrentViewPort();
                String    preserveAspectRatioValue = this.attributesAndStyles.Get(SvgConstants.Attributes.PRESERVE_ASPECT_RATIO
                                                                                  );
                IList <String> values = SvgCssUtils.SplitValueList(preserveAspectRatioValue);
                if (SvgConstants.Values.DEFER.EqualsIgnoreCase(values[0]))
                {
                    values.JRemoveAt(0);
                }
                String align    = values[0];
                float  x        = 0f;
                float  y        = 0f;
                float  midXBox  = viewBoxValues[0] + (viewBoxValues[2] / 2);
                float  midYBox  = viewBoxValues[1] + (viewBoxValues[3] / 2);
                float  midXPort = currentViewPort.GetX() + (currentViewPort.GetWidth() / 2);
                float  midYPort = currentViewPort.GetY() + (currentViewPort.GetHeight() / 2);
                switch (align.ToLowerInvariant())
                {
                case SvgConstants.Values.NONE: {
                    break;
                }

                case SvgConstants.Values.XMIN_YMIN: {
                    x = -viewBoxValues[0];
                    y = -viewBoxValues[1];
                    break;
                }

                case SvgConstants.Values.XMIN_YMID: {
                    x = -viewBoxValues[0];
                    y = midYPort - midYBox;
                    break;
                }

                case SvgConstants.Values.XMIN_YMAX: {
                    x = -viewBoxValues[0];
                    y = currentViewPort.GetHeight() - viewBoxValues[3];
                    break;
                }

                case SvgConstants.Values.XMID_YMIN: {
                    x = midXPort - midXBox;
                    y = -viewBoxValues[1];
                    break;
                }

                case SvgConstants.Values.XMID_YMAX: {
                    x = midXPort - midXBox;
                    y = currentViewPort.GetHeight() - viewBoxValues[3];
                    break;
                }

                case SvgConstants.Values.XMAX_YMIN: {
                    x = currentViewPort.GetWidth() - viewBoxValues[2];
                    y = -viewBoxValues[1];
                    break;
                }

                case SvgConstants.Values.XMAX_YMID: {
                    x = currentViewPort.GetWidth() - viewBoxValues[2];
                    y = midYPort - midYBox;
                    break;
                }

                case SvgConstants.Values.XMAX_YMAX: {
                    x = currentViewPort.GetWidth() - viewBoxValues[2];
                    y = currentViewPort.GetHeight() - viewBoxValues[3];
                    break;
                }

                case SvgConstants.Values.DEFAULT_ASPECT_RATIO:
                default: {
                    x = midXPort - midXBox;
                    y = midYPort - midYBox;
                    break;
                }
                }
                transform.Translate(x, y);
            }
            return(transform);
        }