private void ApplyUserSpaceScaling(SvgDrawContext context)
 {
     if (!this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.MARKER_UNITS) || SvgConstants.Values.STROKEWIDTH
         .Equals(this.attributesAndStyles.Get(SvgConstants.Attributes.MARKER_UNITS)))
     {
         String parentValue = this.GetParent().GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
         if (parentValue != null)
         {
             float strokeWidthScale;
             if (CssUtils.IsPercentageValue(parentValue))
             {
                 // If stroke width is a percentage value is always computed as a percentage of the normalized viewBox diagonal length.
                 double rootViewPortHeight    = context.GetRootViewPort().GetHeight();
                 double rootViewPortWidth     = context.GetRootViewPort().GetWidth();
                 double viewBoxDiagonalLength = Math.Sqrt(rootViewPortHeight * rootViewPortHeight + rootViewPortWidth * rootViewPortWidth
                                                          );
                 strokeWidthScale = CssUtils.ParseRelativeValue(parentValue, (float)viewBoxDiagonalLength);
             }
             else
             {
                 strokeWidthScale = SvgCssUtils.ConvertPtsToPx(ParseFontRelativeOrAbsoluteLengthOnMarker(parentValue));
             }
             context.GetCurrentCanvas().ConcatMatrix(AffineTransform.GetScaleInstance(strokeWidthScale, strokeWidthScale
                                                                                      ));
         }
     }
 }
Example #2
0
 /// <summary><inheritDoc/></summary>
 public virtual bool IsValid(String objectString)
 {
     if (objectString == null)
     {
         return(false);
     }
     if (CommonCssConstants.INITIAL.Equals(objectString) || CommonCssConstants.INHERIT.Equals(objectString) ||
         CommonCssConstants.UNSET.Equals(objectString))
     {
         return(true);
     }
     if (CommonCssConstants.NORMAL.Equals(objectString))
     {
         return(this.allowedNormal);
     }
     if (!CssUtils.IsValidNumericValue(objectString))
     {
         return(false);
     }
     if (CssUtils.IsPercentageValue(objectString))
     {
         return(this.allowedPercent);
     }
     return(true);
 }
        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);
                if (CssUtils.IsPercentageValue(refX))
                {
                    moveX = CssUtils.ParseRelativeValue(refX, context.GetRootViewPort().GetWidth());
                }
                else
                {
                    moveX = ParseFontRelativeOrAbsoluteLengthOnMarker(refX);
                }
                //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);
                if (CssUtils.IsPercentageValue(refY))
                {
                    moveY = CssUtils.ParseRelativeValue(refY, context.GetRootViewPort().GetHeight());
                }
                else
                {
                    moveY = ParseFontRelativeOrAbsoluteLengthOnMarker(refY);
                }
                moveY *= -1 * yScale;
            }
            AffineTransform translation = AffineTransform.GetTranslateInstance(moveX, moveY);

            if (!translation.IsIdentity())
            {
                context.GetCurrentCanvas().ConcatMatrix(translation);
            }
        }
Example #4
0
        /// <summary>Evaluates the stop color offset value</summary>
        /// <returns>the stop color offset value in [0, 1] range</returns>
        public virtual double GetOffset()
        {
            double?offset          = null;
            String offsetAttribute = GetAttribute(SvgConstants.Attributes.OFFSET);

            if (CssUtils.IsPercentageValue(offsetAttribute))
            {
                offset = (double)CssUtils.ParseRelativeValue(offsetAttribute, 1);
            }
            else
            {
                if (CssUtils.IsNumericValue(offsetAttribute))
                {
                    offset = CssUtils.ParseDouble(offsetAttribute);
                }
            }
            double result = offset != null ? offset.Value : 0d;

            return(result > 1d ? 1d : result > 0d ? result : 0d);
        }
 /// <summary>Parse absolute length.</summary>
 /// <param name="length">
 ///
 /// <see cref="System.String"/>
 /// for parsing
 /// </param>
 /// <param name="percentRelativeValue">the value on which percent length is based on</param>
 /// <param name="defaultValue">default value if length is not recognized</param>
 /// <param name="context">
 /// current
 /// <see cref="iText.Svg.Renderers.SvgDrawContext"/>
 /// </param>
 /// <returns>absolute value in points</returns>
 protected internal virtual float ParseAbsoluteLength(String length, float percentRelativeValue, float defaultValue
                                                      , SvgDrawContext context)
 {
     if (CssUtils.IsPercentageValue(length))
     {
         return(CssUtils.ParseRelativeValue(length, percentRelativeValue));
     }
     else
     {
         float     em        = GetCurrentFontSize();
         float     rem       = context.GetRemValue();
         UnitValue unitValue = CssUtils.ParseLengthValueToPt(length, em, rem);
         if (unitValue != null && unitValue.IsPointValue())
         {
             return(unitValue.GetValue());
         }
         else
         {
             return(defaultValue);
         }
     }
 }
Example #6
0
        private double GetCoordinateForObjectBoundingBox(String attributeName, double defaultValue)
        {
            String attributeValue = GetAttribute(attributeName);
            double absoluteValue  = defaultValue;

            if (CssUtils.IsPercentageValue(attributeValue))
            {
                absoluteValue = CssUtils.ParseRelativeValue(attributeValue, 1);
            }
            else
            {
                if (CssUtils.IsNumericValue(attributeValue) || CssUtils.IsMetricValue(attributeValue) || CssUtils.IsRelativeValue
                        (attributeValue))
                {
                    // if there is incorrect value metric, then we do not need to parse the value
                    int unitsPosition = CssUtils.DeterminePositionBetweenValueAndUnit(attributeValue);
                    if (unitsPosition > 0)
                    {
                        // We want to ignore the unit type. From the svg specification:
                        // "the normal of the linear gradient is perpendicular to the gradient vector in
                        // object bounding box space (i.e., the abstract coordinate system where (0,0)
                        // is at the top/left of the object bounding box and (1,1) is at the bottom/right
                        // of the object bounding box)".
                        // Different browsers treats this differently. We chose the "Google Chrome" approach
                        // which treats the "abstract coordinate system" in the coordinate metric measure,
                        // i.e. for value '0.5cm' the top/left of the object bounding box would be (1cm, 1cm),
                        // for value '0.5em' the top/left of the object bounding box would be (1em, 1em) and etc.
                        // no null pointer should be thrown as determine
                        absoluteValue = CssUtils.ParseDouble(attributeValue.JSubstring(0, unitsPosition)).Value;
                    }
                }
            }
            // need to multiply by 0.75 as further the (top, right) coordinates of the object bbox
            // would be transformed into (0.75, 0.75) point instead of (1, 1). The reason described
            // as a comment inside the method constructing the gradient transformation
            return(absoluteValue * 0.75);
        }