public override void Draw(PdfCanvas canvas)
        {
            float x = CssUtils.ParseAbsoluteLength(coordinates[0]);
            float y = CssUtils.ParseAbsoluteLength(coordinates[1]);

            canvas.LineTo(x, y);
        }
        protected internal override void DoDraw(SvgDrawContext context)
        {
            if (this.attributesAndStyles != null && this.attributesAndStyles.ContainsKey(SvgConstants.Attributes.TEXT_CONTENT
                                                                                         ))
            {
                PdfCanvas      currentCanvas    = context.GetCurrentCanvas();
                String         xRawValue        = this.attributesAndStyles.Get(SvgConstants.Attributes.X);
                String         yRawValue        = this.attributesAndStyles.Get(SvgConstants.Attributes.Y);
                String         fontSizeRawValue = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_SIZE);
                IList <String> xValuesList      = SvgCssUtils.SplitValueList(xRawValue);
                IList <String> yValuesList      = SvgCssUtils.SplitValueList(yRawValue);
                float          x        = 0f;
                float          y        = 0f;
                float          fontSize = 0f;
                if (fontSizeRawValue != null && !String.IsNullOrEmpty(fontSizeRawValue))
                {
                    fontSize = CssUtils.ParseAbsoluteLength(fontSizeRawValue, CommonCssConstants.PT);
                }
                if (!xValuesList.IsEmpty())
                {
                    x = CssUtils.ParseAbsoluteLength(xValuesList[0]);
                }
                if (!yValuesList.IsEmpty())
                {
                    y = CssUtils.ParseAbsoluteLength(yValuesList[0]);
                }
                currentCanvas.BeginText();
                FontProvider provider  = context.GetFontProvider();
                FontSet      tempFonts = context.GetTempFonts();
                PdfFont      font      = null;
                if (!provider.GetFontSet().IsEmpty() || (tempFonts != null && !tempFonts.IsEmpty()))
                {
                    String fontFamily = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_FAMILY);
                    String fontWeight = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_WEIGHT);
                    String fontStyle  = this.attributesAndStyles.Get(SvgConstants.Attributes.FONT_STYLE);
                    fontFamily = fontFamily != null?fontFamily.Trim() : "";

                    FontInfo fontInfo = ResolveFontName(fontFamily, fontWeight, fontStyle, provider, tempFonts);
                    font = provider.GetPdfFont(fontInfo, tempFonts);
                }
                if (font == null)
                {
                    try {
                        // TODO (DEVSIX-2057)
                        // TODO each call of createFont() create a new instance of PdfFont.
                        // TODO FontProvider shall be used instead.
                        font = PdfFontFactory.CreateFont();
                    }
                    catch (System.IO.IOException e) {
                        throw new SvgProcessingException(SvgLogMessageConstant.FONT_NOT_FOUND, e);
                    }
                }
                currentCanvas.SetFontAndSize(font, fontSize);
                //Current transformation matrix results in the character glyphs being mirrored, correct with inverse tf
                currentCanvas.SetTextMatrix(1, 0, 0, -1, x, y);
                currentCanvas.SetColor(ColorConstants.BLACK, true);
                currentCanvas.ShowText(this.attributesAndStyles.Get(SvgConstants.Attributes.TEXT_CONTENT));
                currentCanvas.EndText();
            }
        }
Beispiel #3
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();
            // Set default values to parent viewport in the case of a nested svg tag
            float portX = currentViewPort.GetX();
            float portY = currentViewPort.GetY();
            // Default should be parent portWidth if not outermost
            float portWidth = currentViewPort.GetWidth();
            // Default should be parent height if not outermost
            float portHeight = currentViewPort.GetHeight();

            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));
        }
Beispiel #4
0
 /// <summary>
 /// Creates a new
 /// <see cref="LiTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public LiTagWorker(IElementNode element, ProcessorContext context)
 {
     listItem = new ListItem();
     if (element.GetAttribute(AttributeConstants.VALUE) != null)
     {
         int?indexValue = (int?)CssUtils.ParseInteger(element.GetAttribute(AttributeConstants.VALUE));
         if (indexValue != null)
         {
             listItem.SetListSymbolOrdinalValue(indexValue.Value);
         }
     }
     if (!(context.GetState().Top() is UlOlTagWorker))
     {
         listItem.SetProperty(Property.LIST_SYMBOL_POSITION, ListSymbolPosition.INSIDE);
         float em = CssUtils.ParseAbsoluteLength(element.GetStyles().Get(CssConstants.FONT_SIZE));
         if (TagConstants.LI.Equals(element.Name()))
         {
             ListStyleApplierUtil.SetDiscStyle(listItem, em);
         }
         else
         {
             listItem.SetProperty(Property.LIST_SYMBOL, null);
         }
         list = new List();
         list.Add(listItem);
     }
     inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                    GetStyles().Get(CssConstants.TEXT_TRANSFORM));
     AccessiblePropHelper.TrySetLangAttribute(listItem, element);
 }
Beispiel #5
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.apply.impl.BlockCssApplier#apply(com.itextpdf.html2pdf.attach.ProcessorContext, com.itextpdf.html2pdf.html.node.IStylesContainer, com.itextpdf.html2pdf.attach.ITagWorker)
         */
        public override void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
                                   )
        {
            if (!(tagWorker.GetElementResult() is List))
            {
                return;
            }
            IDictionary <String, String> css = stylesContainer.GetStyles();
            List list = (List)tagWorker.GetElementResult();

            if (CssConstants.INSIDE.Equals(css.Get(CssConstants.LIST_STYLE_POSITION)))
            {
                list.SetProperty(Property.LIST_SYMBOL_POSITION, ListSymbolPosition.INSIDE);
            }
            else
            {
                list.SetProperty(Property.LIST_SYMBOL_POSITION, ListSymbolPosition.OUTSIDE);
            }
            ListStyleApplierUtil.ApplyListStyleTypeProperty(stylesContainer, css, context, list);
            ListStyleApplierUtil.ApplyListStyleImageProperty(css, context, list);
            base.Apply(context, stylesContainer, tagWorker);
            // process the padding considering the direction
            bool isRtl = BaseDirection.RIGHT_TO_LEFT.Equals(list.GetProperty <BaseDirection?>(Property.BASE_DIRECTION));

            if ((isRtl && !list.HasProperty(Property.PADDING_RIGHT)) || (!isRtl && !list.HasProperty(Property.PADDING_LEFT
                                                                                                     )))
            {
                float     em           = CssUtils.ParseAbsoluteLength(css.Get(CssConstants.FONT_SIZE));
                float     rem          = context.GetCssContext().GetRootFontSize();
                UnitValue startPadding = CssUtils.ParseLengthValueToPt(css.Get(CssConstants.PADDING_INLINE_START), em, rem
                                                                       );
                list.SetProperty(isRtl ? Property.PADDING_RIGHT : Property.PADDING_LEFT, startPadding);
            }
        }
Beispiel #6
0
        //            element.setProperty(Property.POSITION, LayoutPosition.FIXED);
        //            float em = CssUtils.parseAbsoluteLength(cssProps.get(CommonCssConstants.FONT_SIZE));
        //            applyLeftProperty(cssProps, element, em, Property.X);
        //            applyTopProperty(cssProps, element, em, Property.Y);
        // TODO
        /// <summary>Applies left, right, top, and bottom properties.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="element">the element</param>
        /// <param name="position">the position</param>
        private static void ApplyLeftRightTopBottom(IDictionary <String, String> cssProps, ProcessorContext context
                                                    , IPropertyContainer element, String position)
        {
            float em  = CssUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE));
            float rem = context.GetCssContext().GetRootFontSize();

            if (CssConstants.RELATIVE.Equals(position) && cssProps.ContainsKey(CssConstants.LEFT) && cssProps.ContainsKey
                    (CssConstants.RIGHT))
            {
                // When both the right CSS property and the left CSS property are defined, the position of the element is overspecified.
                // In that case, the left value has precedence when the container is left-to-right (that is that the right computed value is set to -left),
                // and the right value has precedence when the container is right-to-left (that is that the left computed value is set to -right).
                bool isRtl = CssConstants.RTL.Equals(cssProps.Get(CssConstants.DIRECTION));
                if (isRtl)
                {
                    ApplyRightProperty(cssProps, element, em, rem, Property.RIGHT);
                }
                else
                {
                    ApplyLeftProperty(cssProps, element, em, rem, Property.LEFT);
                }
            }
            else
            {
                ApplyLeftProperty(cssProps, element, em, rem, Property.LEFT);
                ApplyRightProperty(cssProps, element, em, rem, Property.RIGHT);
            }
            ApplyTopProperty(cssProps, element, em, rem, Property.TOP);
            ApplyBottomProperty(cssProps, element, em, rem, Property.BOTTOM);
        }
 /// <summary>
 /// Fetches a map of String values by calling getAttribute(Strng s) method
 /// and maps it's values to arc parmateter cx, cy , rx, ry respectively
 /// </summary>
 /// <returns>boolean values to indicate whether all values exit or not</returns>
 protected internal virtual bool SetParameters()
 {
     cx = 0;
     cy = 0;
     if (GetAttribute(SvgConstants.Attributes.CX) != null)
     {
         cx = CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes.CX));
     }
     if (GetAttribute(SvgConstants.Attributes.CY) != null)
     {
         cy = CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes.CY));
     }
     if (GetAttribute(SvgConstants.Attributes.RX) != null && CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes
                                                                                                       .RX)) > 0)
     {
         rx = CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes.RX));
     }
     else
     {
         return(false);
     }
     //No drawing if rx is absent
     if (GetAttribute(SvgConstants.Attributes.RY) != null && CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes
                                                                                                       .RY)) > 0)
     {
         ry = CssUtils.ParseAbsoluteLength(GetAttribute(SvgConstants.Attributes.RY));
     }
     else
     {
         return(false);
     }
     //No drawing if ry is absent
     return(true);
 }
Beispiel #8
0
        /* (non-Javadoc)
         * @see com.itextpdf.html2pdf.css.apply.impl.BlockCssApplier#apply(com.itextpdf.html2pdf.attach.ProcessorContext, com.itextpdf.html2pdf.html.node.IStylesContainer, com.itextpdf.html2pdf.attach.ITagWorker)
         */
        public override void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker worker)
        {
            base.Apply(context, stylesContainer, worker);
            IPropertyContainer cell = worker.GetElementResult();

            if (cell != null)
            {
                IDictionary <String, String> cssProps = stylesContainer.GetStyles();
                VerticalAlignmentApplierUtil.ApplyVerticalAlignmentForCells(cssProps, context, cell);
                float    em           = CssUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE));
                float    rem          = context.GetCssContext().GetRootFontSize();
                Border[] bordersArray = BorderStyleApplierUtil.GetBordersArray(cssProps, em, rem);
                if (bordersArray[0] == null)
                {
                    cell.SetProperty(Property.BORDER_TOP, Border.NO_BORDER);
                }
                if (bordersArray[1] == null)
                {
                    cell.SetProperty(Property.BORDER_RIGHT, Border.NO_BORDER);
                }
                if (bordersArray[2] == null)
                {
                    cell.SetProperty(Property.BORDER_BOTTOM, Border.NO_BORDER);
                }
                if (bordersArray[3] == null)
                {
                    cell.SetProperty(Property.BORDER_LEFT, Border.NO_BORDER);
                }
            }
        }
Beispiel #9
0
        /// <summary>Applies outlines to an element.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the Processor context</param>
        /// <param name="element">the element</param>
        public static void ApplyOutlines(IDictionary <String, String> cssProps, ProcessorContext context, IPropertyContainer
                                         element)
        {
            float  em      = CssUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE));
            float  rem     = context.GetCssContext().GetRootFontSize();
            Border outline = GetCertainBorder(cssProps.Get(CssConstants.OUTLINE_WIDTH), cssProps.Get(CssConstants.OUTLINE_STYLE
                                                                                                     ), GetSpecificOutlineColorOrDefaultColor(cssProps, CssConstants.OUTLINE_COLOR), em, rem);

            if (outline != null)
            {
                element.SetProperty(Property.OUTLINE, outline);
            }
            if (cssProps.Get(CssConstants.OUTLINE_OFFSET) != null && element.GetProperty <Border>(Property.OUTLINE) !=
                null)
            {
                UnitValue unitValue = CssUtils.ParseLengthValueToPt(cssProps.Get(CssConstants.OUTLINE_OFFSET), em, rem);
                if (unitValue != null)
                {
                    if (unitValue.IsPercentValue())
                    {
                        LOGGER.Error("outline-width in percents is not supported");
                    }
                    else
                    {
                        if (unitValue.GetValue() != 0)
                        {
                            element.SetProperty(Property.OUTLINE_OFFSET, unitValue.GetValue());
                        }
                    }
                }
            }
        }
Beispiel #10
0
        /// <summary>
        /// Creates a new
        /// <see cref="BodyTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public BodyTagWorker(IElementNode element, ProcessorContext context)
            : base(element, context)
        {
            parentTagWorker = context.GetState().Empty() ? null : context.GetState().Top();
            if (parentTagWorker != null && parentTagWorker.GetElementResult() != null)
            {
                // TODO this is not in css applier because css applier is called after the elements are added to the document
                // We need to apply font styles here specifically to set font-size to the document because this is needed for
                // inline-blocks with fixed height when the height is smaller than the defined font size
                float em = CssUtils.ParseAbsoluteLength(element.GetStyles().Get(CssConstants.FONT_SIZE));
                if (em != 0)
                {
                    parentTagWorker.GetElementResult().SetProperty(Property.FONT_SIZE, UnitValue.CreatePointValue(em));
                }
            }
            PdfDocument pdfDocument = context.GetPdfDocument();

            if (pdfDocument != null)
            {
                lang = element.GetAttribute(AttributeConstants.LANG);
                if (lang != null)
                {
                    pdfDocument.GetCatalog().SetLang(new PdfString(lang, PdfEncodings.UNICODE_BIG));
                }
            }
            else
            {
                lang = element.GetLang();
            }
        }
Beispiel #11
0
        public virtual void ThreeRotateValuesTest()
        {
            AffineTransform expected = AffineTransform.GetRotateInstance(MathUtil.ToRadians(23), CssUtils.ParseAbsoluteLength
                                                                             ("58"), CssUtils.ParseAbsoluteLength("57"));
            AffineTransform actual = TransformUtils.ParseTransform("rotate(23, 58, 57)");

            NUnit.Framework.Assert.AreEqual(expected, actual);
        }
Beispiel #12
0
        public virtual void NormalRotateTest()
        {
            AffineTransform expected = AffineTransform.GetRotateInstance(MathUtil.ToRadians(10), CssUtils.ParseAbsoluteLength
                                                                             ("5"), CssUtils.ParseAbsoluteLength("10"));
            AffineTransform actual = TransformUtils.ParseTransform("rotate(10, 5, 10)");

            NUnit.Framework.Assert.AreEqual(expected, actual);
        }
        /// <summary>Draws a quadratic Bezier curve from the current point to (x,y) using (x1,y1) as the control point
        ///     </summary>
        public override void Draw(PdfCanvas canvas)
        {
            float x1 = CssUtils.ParseAbsoluteLength(coordinates[0]);
            float y1 = CssUtils.ParseAbsoluteLength(coordinates[1]);
            float x  = CssUtils.ParseAbsoluteLength(coordinates[2]);
            float y  = CssUtils.ParseAbsoluteLength(coordinates[3]);

            canvas.CurveTo(x1, y1, x, y);
        }
        /*TODO Note: visibility doesn't work on "chrome" or "safari" and though it technically works on "firefox" and "edge" the results differ,
         * with "edge" surprisingly giving the closest result to expected one.
         * The supported values are 'collapse' and 'visible'. The expected behaviour for 'collapse' is not to render those cols
         * (the table layout should change ann the width should be diminished), and to clip cells that are spaned to none-collapsed one.
         * The state of the content in clipped cells is not specified*/
        /// <summary>Gets the width.</summary>
        /// <param name="resolvedCssProps">the resolved CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <returns>the width</returns>
        public static UnitValue GetWidth(IDictionary <String, String> resolvedCssProps, ProcessorContext context)
        {
            //The Width is a special case, casue it should be transferred from <colgroup> to <col> but it not applied to <td> or <th>
            float  em    = CssUtils.ParseAbsoluteLength(resolvedCssProps.Get(CssConstants.FONT_SIZE));
            String width = resolvedCssProps.Get(CssConstants.WIDTH);

            return(width != null?CssUtils.ParseLengthValueToPt(width, em, context.GetCssContext().GetRootFontSize())
                       : null);
        }
Beispiel #15
0
        /// <summary>Parses the absolute font size.</summary>
        /// <param name="fontSizeValue">
        /// the font size value as a
        /// <see cref="System.String"/>
        /// </param>
        /// <returns>
        /// the font size value as a
        /// <c>float</c>
        /// </returns>
        public static float ParseAbsoluteFontSize(String fontSizeValue)
        {
            if (CssConstants.FONT_ABSOLUTE_SIZE_KEYWORDS.Contains(fontSizeValue))
            {
                switch (fontSizeValue)
                {
                case CssConstants.XX_SMALL: {
                    fontSizeValue = "9px";
                    break;
                }

                case CssConstants.X_SMALL: {
                    fontSizeValue = "10px";
                    break;
                }

                case CssConstants.SMALL: {
                    fontSizeValue = "13px";
                    break;
                }

                case CssConstants.MEDIUM: {
                    fontSizeValue = "16px";
                    break;
                }

                case CssConstants.LARGE: {
                    fontSizeValue = "18px";
                    break;
                }

                case CssConstants.X_LARGE: {
                    fontSizeValue = "24px";
                    break;
                }

                case CssConstants.XX_LARGE: {
                    fontSizeValue = "32px";
                    break;
                }

                default: {
                    fontSizeValue = "16px";
                    break;
                }
                }
            }
            try {
                /* Styled XML Parser will throw an exception when it can't parse the given value
                 * but in html2pdf, we want to fall back to the default value of 0
                 */
                return(CssUtils.ParseAbsoluteLength(fontSizeValue));
            }
            catch (StyledXMLParserException) {
                return(0f);
            }
        }
Beispiel #16
0
 public override void Draw(PdfCanvas canvas)
 {
     for (int i = 0; i < coordinates.Length; i++)
     {
         float x = CssUtils.ParseAbsoluteLength(coordinates[i][0]);
         float y = CssUtils.ParseAbsoluteLength(coordinates[i][1]);
         canvas.LineTo(x, y);
     }
 }
Beispiel #17
0
        /// <summary>Calculates text rise for percentage value text rise.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="rootFontSize">the root font size</param>
        /// <param name="vAlignVal">the vertical alignment value</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForPercentageValue(IStylesContainer stylesContainer, float rootFontSize,
                                                            String vAlignVal)
        {
            String ownFontSizeStr        = stylesContainer.GetStyles().Get(CssConstants.FONT_SIZE);
            float  fontSize              = CssUtils.ParseAbsoluteLength(ownFontSizeStr);
            String lineHeightStr         = stylesContainer.GetStyles().Get(CssConstants.LINE_HEIGHT);
            float  lineHeightActualValue = GetLineHeightActualValue(fontSize, rootFontSize, lineHeightStr);

            return(CssUtils.ParseRelativeValue(vAlignVal, lineHeightActualValue));
        }
        internal virtual float GetAttribute(IDictionary <String, String> attributes, String key)
        {
            String value = attributes.Get(key);

            if (value != null && !String.IsNullOrEmpty(value))
            {
                return(CssUtils.ParseAbsoluteLength(attributes.Get(key)));
            }
            return(0);
        }
Beispiel #19
0
 // 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>
 internal virtual void ApplyViewBox(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null && 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();
         String[]  alignAndMeet        = RetrieveAlignAndMeet();
         String    align               = alignAndMeet[0];
         String    meetOrSlice         = alignAndMeet[1];
         float     scaleWidth          = currentViewPort.GetWidth() / values[2];
         float     scaleHeight         = currentViewPort.GetHeight() / values[3];
         bool      forceUniformScaling = !(SvgConstants.Values.NONE.Equals(align));
         if (forceUniformScaling)
         {
             //Scaling should preserve aspect ratio
             if (SvgConstants.Values.MEET.Equals(meetOrSlice))
             {
                 scaleWidth = Math.Min(scaleWidth, scaleHeight);
             }
             else
             {
                 scaleWidth = Math.Max(scaleWidth, scaleHeight);
             }
             scaleHeight = scaleWidth;
         }
         AffineTransform scale = AffineTransform.GetScaleInstance(scaleWidth, scaleHeight);
         float[]         scaledViewBoxValues = ScaleViewBoxValues(values, scaleWidth, scaleHeight);
         AffineTransform transform           = ProcessAspectRatioPosition(context, scaledViewBoxValues, align, scaleWidth, scaleHeight
                                                                          );
         if (!scale.IsIdentity())
         {
             context.GetCurrentCanvas().ConcatMatrix(scale);
             //Inverse scaling needs to be applied to viewport dimensions
             context.GetCurrentViewPort().SetWidth(currentViewPort.GetWidth() / scaleWidth).SetX(currentViewPort.GetX()
                                                                                                 / scaleWidth).SetHeight(currentViewPort.GetHeight() / scaleHeight).SetY(currentViewPort.GetY() / scaleHeight
                                                                                                                                                                         );
         }
         if (!transform.IsIdentity())
         {
             context.GetCurrentCanvas().ConcatMatrix(transform);
             //Apply inverse translation to viewport to make it line up nicely
             context.GetCurrentViewPort().SetX(currentViewPort.GetX() + -1 * (float)transform.GetTranslateX()).SetY(currentViewPort
                                                                                                                    .GetY() + -1 * (float)transform.GetTranslateY());
         }
     }
 }
 /// <summary>Operations to perform before drawing an element.</summary>
 /// <remarks>
 /// Operations to perform before drawing an element.
 /// This includes setting stroke color and width, fill color.
 /// </remarks>
 /// <param name="context">the svg draw context</param>
 internal virtual void PreDraw(SvgDrawContext context)
 {
     if (this.attributesAndStyles != null)
     {
         PdfCanvas currentCanvas = context.GetCurrentCanvas();
         if (!partOfClipPath)
         {
             {
                 // fill
                 String fillRawValue = GetAttribute(SvgConstants.Attributes.FILL);
                 this.doFill = !SvgConstants.Values.NONE.EqualsIgnoreCase(fillRawValue);
                 if (doFill && CanElementFill())
                 {
                     Color color = ColorConstants.BLACK;
                     if (fillRawValue != null)
                     {
                         color = WebColors.GetRGBColor(fillRawValue);
                     }
                     currentCanvas.SetFillColor(color);
                 }
             }
             {
                 // stroke
                 String strokeRawValue = GetAttribute(SvgConstants.Attributes.STROKE);
                 if (!SvgConstants.Values.NONE.EqualsIgnoreCase(strokeRawValue))
                 {
                     DeviceRgb rgbColor = WebColors.GetRGBColor(strokeRawValue);
                     if (strokeRawValue != null && rgbColor != null)
                     {
                         currentCanvas.SetStrokeColor(rgbColor);
                         String strokeWidthRawValue = GetAttribute(SvgConstants.Attributes.STROKE_WIDTH);
                         float  strokeWidth         = 1f;
                         if (strokeWidthRawValue != null)
                         {
                             strokeWidth = CssUtils.ParseAbsoluteLength(strokeWidthRawValue);
                         }
                         currentCanvas.SetLineWidth(strokeWidth);
                         doStroke = true;
                     }
                 }
             }
             {
                 // opacity
                 String opacityValue = GetAttribute(SvgConstants.Attributes.FILL_OPACITY);
                 if (opacityValue != null && !SvgConstants.Values.NONE.EqualsIgnoreCase(opacityValue))
                 {
                     PdfExtGState gs1 = new PdfExtGState();
                     gs1.SetFillOpacity(float.Parse(opacityValue, System.Globalization.CultureInfo.InvariantCulture));
                     currentCanvas.SetExtGState(gs1);
                 }
             }
         }
     }
 }
        /// <summary>Creates a translate transformation.</summary>
        /// <param name="values">values of the transformation</param>
        /// <returns>AffineTransform for the translate operation</returns>
        private static AffineTransform CreateTranslateTransformation(IList <String> values)
        {
            if (values.Count == 0 || values.Count > 2)
            {
                throw new SvgProcessingException(SvgLogMessageConstant.TRANSFORM_INCORRECT_NUMBER_OF_VALUES);
            }
            float translateX = CssUtils.ParseAbsoluteLength(values[0]);
            float translateY = values.Count == 2 ? CssUtils.ParseAbsoluteLength(values[1]) : 0;

            return(AffineTransform.GetTranslateInstance(translateX, translateY));
        }
Beispiel #22
0
        internal virtual float[] GetViewBoxValues()
        {
            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]);
            }
            return(values);
        }
Beispiel #23
0
        /// <summary>Calculates the text rise for middle alignment.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForMiddle(IStylesContainer stylesContainer)
        {
            String ownFontSizeStr        = stylesContainer.GetStyles().Get(CssConstants.FONT_SIZE);
            float  fontSize              = CssUtils.ParseAbsoluteLength(ownFontSizeStr);
            float  parentFontSize        = GetParentFontSize(stylesContainer);
            double fontMiddleCoefficient = 0.3;
            float  elementMidPoint       = (float)(fontSize * fontMiddleCoefficient);
            // shift to element mid point from the baseline
            float xHeight = parentFontSize / 4;

            return(xHeight - elementMidPoint);
        }
Beispiel #24
0
        /// <summary>Calculates the text rise for top alignment.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="rootFontSize">the root font size</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForTextTop(IStylesContainer stylesContainer, float rootFontSize)
        {
            String ownFontSizeStr        = stylesContainer.GetStyles().Get(CssConstants.FONT_SIZE);
            float  fontSize              = CssUtils.ParseAbsoluteLength(ownFontSizeStr);
            String lineHeightStr         = stylesContainer.GetStyles().Get(CssConstants.LINE_HEIGHT);
            float  lineHeightActualValue = GetLineHeightActualValue(fontSize, rootFontSize, lineHeightStr);
            float  parentFontSize        = GetParentFontSize(stylesContainer);
            float  elementTopEdge        = (float)(fontSize * ASCENDER_COEFFICIENT + (lineHeightActualValue - fontSize) / 2);
            float  parentTextTop         = (float)(parentFontSize * ASCENDER_COEFFICIENT);

            return(parentTextTop - elementTopEdge);
        }
Beispiel #25
0
 /// <summary>Parses an absolute length.</summary>
 /// <param name="value">
 /// the absolute length as a
 /// <see cref="System.String"/>
 /// value
 /// </param>
 /// <returns>
 /// the absolute length as a
 /// <c>float</c>
 /// value
 /// </returns>
 private static float ParseAbsoluteLength(String value)
 {
     if (CssUtils.IsRelativeValue(value))
     {
         // TODO here should be used default font size of the browser, it probably should be fetched from the more generic place than private class constant
         return(CssUtils.ParseRelativeValue(value, DEFAULT_FONT_SIZE));
     }
     else
     {
         return(CssUtils.ParseAbsoluteLength(value));
     }
 }
        internal override void PreDraw(SvgDrawContext context)
        {
            base.PreDraw(context);
            float[]   markerWidthHeight = GetMarkerWidthHeightValues();
            float     markerWidth       = markerWidthHeight[0];
            float     markerHeight      = markerWidthHeight[1];
            Rectangle markerViewport    = new Rectangle(CssUtils.ParseAbsoluteLength(this.GetAttribute(SvgConstants.Attributes
                                                                                                       .X)), CssUtils.ParseAbsoluteLength(this.GetAttribute(SvgConstants.Attributes.Y)), markerWidth, markerHeight
                                                        );

            context.AddViewPort(markerViewport);
        }
        // TODO (DEVSIX-3596) Add support of 'lh' 'ch' units and viewport-relative units
        private float ParseFontRelativeOrAbsoluteLengthOnMarker(String length)
        {
            float value = 0f;

            if (CssUtils.IsMetricValue(length) || CssUtils.IsNumericValue(length))
            {
                value = CssUtils.ParseAbsoluteLength(length);
            }
            else
            {
                if (CssUtils.IsFontRelativeValue(length))
                {
                    // Defaut font-size is medium
                    value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(CommonCssConstants.MEDIUM));
                    // Different browsers process font-relative units for markers differently.
                    // We do it according to the css specification.
                    if (CssUtils.IsRemValue(length))
                    {
                        ISvgNodeRenderer rootElement = GetSvgRootElement(GetParent());
                        if (rootElement != null && rootElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                        {
                            value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(rootElement.GetAttribute(CommonCssConstants
                                                                                                                                .FONT_SIZE)));
                        }
                    }
                    else
                    {
                        if (CssUtils.IsEmValue(length))
                        {
                            ISvgNodeRenderer parentElement = this.GetParent();
                            if (parentElement != null && parentElement.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                            {
                                value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(parentElement.GetAttribute(CommonCssConstants
                                                                                                                                      .FONT_SIZE)));
                            }
                        }
                        else
                        {
                            if (CssUtils.IsExValue(length))
                            {
                                if (this.GetAttribute(CommonCssConstants.FONT_SIZE) != null)
                                {
                                    value = CssUtils.ParseRelativeValue(length, CssUtils.ParseAbsoluteFontSize(this.GetAttribute(CommonCssConstants
                                                                                                                                 .FONT_SIZE)));
                                }
                            }
                        }
                    }
                }
            }
            return(value);
        }
Beispiel #28
0
 public override void Draw(PdfCanvas canvas)
 {
     for (int i = 0; i < coordinates.Length; i++)
     {
         float x1 = CssUtils.ParseAbsoluteLength(coordinates[i][0]);
         float y1 = CssUtils.ParseAbsoluteLength(coordinates[i][1]);
         float x2 = CssUtils.ParseAbsoluteLength(coordinates[i][2]);
         float y2 = CssUtils.ParseAbsoluteLength(coordinates[i][3]);
         float x  = CssUtils.ParseAbsoluteLength(coordinates[i][4]);
         float y  = CssUtils.ParseAbsoluteLength(coordinates[i][5]);
         canvas.CurveTo(x1, y1, x2, y2, x, y);
     }
 }
Beispiel #29
0
        /// <summary>Extract and parse the font-size declaration stored inside the attributes of the passed renderer</summary>
        /// <param name="renderer">renderer to extract font-size declaration from</param>
        /// <returns>a float containing the font-size interpreted as pt, or NaN if the font-size was not specified in the passed renderer
        ///     </returns>
        private static float ExtractFontSize(ISvgTextNodeRenderer renderer)
        {
            float fontSize = float.NaN;

            if (renderer.GetAttribute(SvgConstants.Attributes.FONT_SIZE) != null)
            {
                String fontSizeRawValue = renderer.GetAttribute(SvgConstants.Attributes.FONT_SIZE);
                if (fontSizeRawValue != null && !String.IsNullOrEmpty(fontSizeRawValue))
                {
                    fontSize = CssUtils.ParseAbsoluteLength(fontSizeRawValue, CommonCssConstants.PT);
                }
            }
            return(fontSize);
        }
        private static float[] GetPositionsFromString(String rawValuesString)
        {
            float[]        result     = null;
            IList <String> valuesList = SvgCssUtils.SplitValueList(rawValuesString);

            if (!valuesList.IsEmpty())
            {
                result = new float[valuesList.Count];
                for (int i = 0; i < valuesList.Count; i++)
                {
                    result[i] = CssUtils.ParseAbsoluteLength(valuesList[i]);
                }
            }
            return(result);
        }