コード例 #1
0
 /// <summary>Applies opacity to an element.</summary>
 /// <param name="cssProps">the CSS properties</param>
 /// <param name="context">the processor context</param>
 /// <param name="container">the container element</param>
 public static void ApplyOpacity(IDictionary<String, String> cssProps, ProcessorContext context, IPropertyContainer
      container) {
     float? opacity = CssDimensionParsingUtils.ParseFloat(cssProps.Get(CssConstants.OPACITY));
     if (opacity != null) {
         container.SetProperty(Property.OPACITY, opacity);
     }
 }
コード例 #2
0
        /// <summary>Resolves a normalized form name.</summary>
        /// <param name="name">the proposed name</param>
        /// <returns>the resolved name</returns>
        private String ResolveNormalizedFormName(String name)
        {
            int separatorIndex = name.LastIndexOf(NAME_COUNT_SEPARATOR);
            int?nameIndex      = null;

            if (separatorIndex != -1 && separatorIndex < name.Length)
            {
                String numberString = name.Substring(separatorIndex + 1);
                nameIndex = CssDimensionParsingUtils.ParseInteger(numberString);
                //Treat number as index only in case it is positive
                if (nameIndex != null && nameIndex > 0)
                {
                    name = name.JSubstring(0, separatorIndex);
                }
            }
            int?savedIndex  = names.Get(name);
            int indexToSave = savedIndex != null ? savedIndex.Value + 1 : 0;

            if (nameIndex != null && indexToSave < nameIndex.Value)
            {
                indexToSave = nameIndex.Value;
            }
            names.Put(name, indexToSave);
            return(indexToSave == 0 ? name : name + NAME_COUNT_SEPARATOR + indexToSave.ToString());
        }
コード例 #3
0
        /// <summary>
        /// Creates a new
        /// <see cref="ColTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public ColTagWorker(IElementNode element, ProcessorContext context)
        {
            int?span = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.SPAN));

            col = new ColWrapper(span != null ? (int)span : 1);
            col.SetLang(element.GetAttribute(AttributeConstants.LANG));
        }
コード例 #4
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 DEVSIX-4104 support "fixed" value of position property
        /// <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  = CssDimensionParsingUtils.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);
        }
コード例 #5
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?)CssDimensionParsingUtils.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 = CssDimensionParsingUtils.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);
 }
コード例 #6
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      = CssDimensionParsingUtils.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 = CssDimensionParsingUtils.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());
                        }
                    }
                }
            }
        }
コード例 #7
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           = CssDimensionParsingUtils.ParseAbsoluteLength(css.Get(CssConstants.FONT_SIZE));
                float     rem          = context.GetCssContext().GetRootFontSize();
                UnitValue startPadding = CssDimensionParsingUtils.ParseLengthValueToPt(css.Get(CssConstants.PADDING_INLINE_START
                                                                                               ), em, rem);
                list.SetProperty(isRtl ? Property.PADDING_RIGHT : Property.PADDING_LEFT, startPadding);
            }
        }
コード例 #8
0
        /// <summary>Gets the array that defines the borders.</summary>
        /// <param name="styles">the styles mapping</param>
        /// <param name="em">the em value</param>
        /// <param name="rem">the root em value</param>
        /// <returns>the borders array</returns>
        public static BorderRadius[] GetBorderRadiiArray(IDictionary <String, String> styles, float em, float rem)
        {
            BorderRadius[] borderRadii    = new BorderRadius[4];
            BorderRadius   borderRadius   = null;
            UnitValue      borderRadiusUV = CssDimensionParsingUtils.ParseLengthValueToPt(styles.Get(CssConstants.BORDER_RADIUS
                                                                                                     ), em, rem);

            if (null != borderRadiusUV)
            {
                borderRadius = new BorderRadius(borderRadiusUV);
            }
            UnitValue[] borderTopLeftRadiusUV = CssDimensionParsingUtils.ParseSpecificCornerBorderRadius(styles.Get(CssConstants
                                                                                                                    .BORDER_TOP_LEFT_RADIUS), em, rem);
            borderRadii[0] = null == borderTopLeftRadiusUV ? borderRadius : new BorderRadius(borderTopLeftRadiusUV[0],
                                                                                             borderTopLeftRadiusUV[1]);
            UnitValue[] borderTopRightRadiusUV = CssDimensionParsingUtils.ParseSpecificCornerBorderRadius(styles.Get(CssConstants
                                                                                                                     .BORDER_TOP_RIGHT_RADIUS), em, rem);
            borderRadii[1] = null == borderTopRightRadiusUV ? borderRadius : new BorderRadius(borderTopRightRadiusUV[0
                                                                                              ], borderTopRightRadiusUV[1]);
            UnitValue[] borderBottomRightRadiusUV = CssDimensionParsingUtils.ParseSpecificCornerBorderRadius(styles.Get
                                                                                                                 (CssConstants.BORDER_BOTTOM_RIGHT_RADIUS), em, rem);
            borderRadii[2] = null == borderBottomRightRadiusUV ? borderRadius : new BorderRadius(borderBottomRightRadiusUV
                                                                                                 [0], borderBottomRightRadiusUV[1]);
            UnitValue[] borderBottomLeftRadiusUV = CssDimensionParsingUtils.ParseSpecificCornerBorderRadius(styles.Get
                                                                                                                (CssConstants.BORDER_BOTTOM_LEFT_RADIUS), em, rem);
            borderRadii[3] = null == borderBottomLeftRadiusUV ? borderRadius : new BorderRadius(borderBottomLeftRadiusUV
                                                                                                [0], borderBottomLeftRadiusUV[1]);
            return(borderRadii);
        }
コード例 #9
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           = CssDimensionParsingUtils.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);
                }
            }
        }
コード例 #10
0
        private static void ApplyBackgroundPositionY(BackgroundPosition position, String yPosition, float em, float
                                                     rem)
        {
            foreach (String value in iText.IO.Util.StringUtil.Split(yPosition, " "))
            {
                switch (value)
                {
                case CommonCssConstants.TOP: {
                    position.SetPositionY(BackgroundPosition.PositionY.TOP);
                    break;
                }

                case CommonCssConstants.BOTTOM: {
                    position.SetPositionY(BackgroundPosition.PositionY.BOTTOM);
                    break;
                }

                case CommonCssConstants.CENTER: {
                    position.SetPositionY(BackgroundPosition.PositionY.CENTER);
                    break;
                }

                default: {
                    UnitValue unitValue = CssDimensionParsingUtils.ParseLengthValueToPt(value, em, rem);
                    if (unitValue != null)
                    {
                        position.SetYShift(unitValue);
                    }
                    break;
                }
                }
            }
        }
コード例 #11
0
        /// <summary>Calculates the text rise value for &lt;sup&gt; and &lt;sub&gt; tags.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <param name="vAlignVal">the vertical alignment value</param>
        /// <returns>the calculated text rise</returns>
        private static float CalcTextRiseForSupSub(IStylesContainer stylesContainer, String vAlignVal)
        {
            float  parentFontSize      = GetParentFontSize(stylesContainer);
            String superscriptPosition = "33%";
            String subscriptPosition   = "-20%";
            String relativeValue       = CssConstants.SUPER.Equals(vAlignVal) ? superscriptPosition : subscriptPosition;

            return(CssDimensionParsingUtils.ParseRelativeValue(relativeValue, parentFontSize));
        }
コード例 #12
0
        /// <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    = CssDimensionParsingUtils.ParseAbsoluteLength(resolvedCssProps.Get(CssConstants.FONT_SIZE));
            String width = resolvedCssProps.Get(CssConstants.WIDTH);

            return(width != null?CssDimensionParsingUtils.ParseLengthValueToPt(width, em, context.GetCssContext().GetRootFontSize
                                                                                   ()) : null);
        }
コード例 #13
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              = CssDimensionParsingUtils.ParseAbsoluteLength(ownFontSizeStr);
            String lineHeightStr         = stylesContainer.GetStyles().Get(CssConstants.LINE_HEIGHT);
            float  lineHeightActualValue = GetLineHeightActualValue(fontSize, rootFontSize, lineHeightStr);

            return(CssDimensionParsingUtils.ParseRelativeValue(vAlignVal, lineHeightActualValue));
        }
コード例 #14
0
        /// <summary>Try to parse a page length value.</summary>
        /// <param name="valueChunk">a string containing a value</param>
        /// <param name="em">the em value</param>
        /// <param name="rem">the root em value</param>
        /// <returns>the value as a float</returns>
        private static float?TryParsePageLengthValue(String valueChunk, float em, float rem)
        {
            UnitValue unitValue = CssDimensionParsingUtils.ParseLengthValueToPt(valueChunk, em, rem);

            if (unitValue == null || unitValue.IsPercentValue())
            {
                return(null);
            }
            return(unitValue.GetValue());
        }
コード例 #15
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              = CssDimensionParsingUtils.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);
        }
コード例 #16
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              = CssDimensionParsingUtils.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);
        }
コード例 #17
0
 private static void ApplyBackgroundColor(String backgroundColorStr, IPropertyContainer element, BackgroundBox
                                          clip)
 {
     if (backgroundColorStr != null && !CssConstants.TRANSPARENT.Equals(backgroundColorStr))
     {
         float[]    rgbaColor       = CssDimensionParsingUtils.ParseRgbaColor(backgroundColorStr);
         Color      color           = new DeviceRgb(rgbaColor[0], rgbaColor[1], rgbaColor[2]);
         float      opacity         = rgbaColor[3];
         Background backgroundColor = new Background(color, opacity, clip);
         element.SetProperty(Property.BACKGROUND, backgroundColor);
     }
 }
コード例 #18
0
        /// <summary>Applies column styles.</summary>
        /// <param name="node">the node</param>
        /// <param name="rowColHelper">the helper class to keep track of the position inside the table</param>
        private void ApplyColStyles(INode node, RowColHelper rowColHelper)
        {
            int          col;
            IElementNode element;

            foreach (INode child in node.ChildNodes())
            {
                if (child is IElementNode)
                {
                    element = (IElementNode)child;
                    if (TagConstants.TR.Equals(element.Name()))
                    {
                        ApplyColStyles(element, rowColHelper);
                        rowColHelper.NewRow();
                    }
                    else
                    {
                        if (TagConstants.TH.Equals(element.Name()) || TagConstants.TD.Equals(element.Name()))
                        {
                            int?colspan = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLSPAN));
                            int?rowspan = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWSPAN));
                            colspan = colspan != null ? colspan : 1;
                            rowspan = rowspan != null ? rowspan : 1;
                            col     = rowColHelper.MoveToNextEmptyCol();
                            if (GetColWrapper(col) != null)
                            {
                                ColWrapper colWrapper = GetColWrapper(col);
                                if (colWrapper.GetCellCssProps() != null)
                                {
                                    element.AddAdditionalHtmlStyles(colWrapper.GetCellCssProps());
                                }
                                String elemLang = element.GetAttribute(AttributeConstants.LANG);
                                String trLang   = null;
                                if (node is IElementNode)
                                {
                                    trLang = ((IElementNode)node).GetAttribute(AttributeConstants.LANG);
                                }
                                if (trLang == null && colWrapper.GetLang() != null && elemLang == null)
                                {
                                    element.GetAttributes().SetAttribute(AttributeConstants.LANG, colWrapper.GetLang());
                                }
                            }
                            rowColHelper.UpdateCurrentPosition((int)colspan, (int)rowspan);
                        }
                        else
                        {
                            ApplyColStyles(child, rowColHelper);
                        }
                    }
                }
            }
        }
コード例 #19
0
        /// <summary>
        /// Creates a new
        /// <see cref="TdTagWorker"/>
        /// instance.
        /// </summary>
        /// <param name="element">the element</param>
        /// <param name="context">the context</param>
        public TdTagWorker(IElementNode element, ProcessorContext context)
        {
            int?colspan = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.COLSPAN));
            int?rowspan = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.ROWSPAN));

            colspan = colspan != null ? colspan : 1;
            rowspan = rowspan != null ? rowspan : 1;
            cell    = new Cell((int)rowspan, (int)colspan);
            cell.SetPadding(0);
            inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                           GetStyles().Get(CssConstants.TEXT_TRANSFORM));
            display = element.GetStyles() != null?element.GetStyles().Get(CssConstants.DISPLAY) : null;

            AccessiblePropHelper.TrySetLangAttribute(cell, element);
        }
コード例 #20
0
            /* (non-Javadoc)
             * @see com.itextpdf.html2pdf.css.resolve.HtmlStylesToCssConverter.IAttributeConverter#convert(com.itextpdf.styledxmlparser.html.node.IElementNode, java.lang.String)
             */
            public virtual IList <CssDeclaration> Convert(IElementNode element, String value)
            {
                float?cellPadding = CssDimensionParsingUtils.ParseFloat(value);

                if (cellPadding != null)
                {
                    if (TagConstants.TABLE.Equals(element.Name()))
                    {
                        IDictionary <String, String> styles = new Dictionary <String, String>();
                        styles.Put(CssConstants.PADDING, value + "px");
                        ApplyPaddingsToTableCells(element, styles);
                    }
                }
                return(JavaCollectionsUtil.EmptyList <CssDeclaration>());
            }
コード例 #21
0
 /// <summary>
 /// Creates a new
 /// <see cref="UlOlTagWorker"/>
 /// instance.
 /// </summary>
 /// <param name="element">the element</param>
 /// <param name="context">the context</param>
 public UlOlTagWorker(IElementNode element, ProcessorContext context)
 {
     list = new List().SetListSymbol("");
     //In the case of an ordered list, see if the start attribute can be found
     if (element.GetAttribute(AttributeConstants.START) != null)
     {
         int?startValue = CssDimensionParsingUtils.ParseInteger(element.GetAttribute(AttributeConstants.START));
         if (startValue != null)
         {
             list.SetItemStartIndex((int)startValue);
         }
     }
     inlineHelper = new WaitingInlineElementsHelper(element.GetStyles().Get(CssConstants.WHITE_SPACE), element.
                                                    GetStyles().Get(CssConstants.TEXT_TRANSFORM));
     AccessiblePropHelper.TrySetLangAttribute(list, element);
 }
コード例 #22
0
        protected override void ComparePdf(String outPath, String dest, String cmp)
        {
            CompareTool compareTool = new CompareTool();

            for (int i = 0; i < PdfHtmlResponsiveDesign.pageSizes.Length; i++)
            {
                float  width       = CssDimensionParsingUtils.ParseAbsoluteLength(PdfHtmlResponsiveDesign.pageSizes[i].GetWidth().ToString());
                String currentDest = dest.Replace("<filename>",
                                                  "responsive_" + width.ToString("0.0", CultureInfo.InvariantCulture) + ".pdf");
                String currentCmp = cmp.Replace("<filename>",
                                                "responsive_" + width.ToString("0.0", CultureInfo.InvariantCulture) + ".pdf");

                AddError(compareTool.CompareByContent(currentDest, currentCmp, outPath, "diff_"));
                AddError(compareTool.CompareDocumentInfo(currentDest, currentCmp));
            }
        }
コード例 #23
0
        /// <summary>Parses the box value.</summary>
        /// <param name="em">a measurement expressed in em</param>
        /// <param name="rem">a measurement expressed in rem (root em)</param>
        /// <param name="dimensionSize">the dimension size</param>
        /// <returns>a float value</returns>
        private static float?ParseBoxValue(String valString, float em, float rem, float dimensionSize)
        {
            UnitValue marginUnitVal = CssDimensionParsingUtils.ParseLengthValueToPt(valString, em, rem);

            if (marginUnitVal != null)
            {
                if (marginUnitVal.IsPointValue())
                {
                    return(marginUnitVal.GetValue());
                }
                if (marginUnitVal.IsPercentValue())
                {
                    return(marginUnitVal.GetValue() * dimensionSize / 100);
                }
            }
            return(null);
        }
コード例 #24
0
 private static void ApplyBackgroundWidth(String widthValue, BackgroundImage image, float em, float rem)
 {
     if (CommonCssConstants.BACKGROUND_SIZE_VALUES.Contains(widthValue))
     {
         if (widthValue.Equals(CommonCssConstants.CONTAIN))
         {
             image.GetBackgroundSize().SetBackgroundSizeToContain();
         }
         if (widthValue.Equals(CommonCssConstants.COVER))
         {
             image.GetBackgroundSize().SetBackgroundSizeToCover();
         }
         return;
     }
     image.GetBackgroundSize().SetBackgroundSizeToValues(CssDimensionParsingUtils.ParseLengthValueToPt(widthValue
                                                                                                       , em, rem), null);
 }
コード例 #25
0
        internal virtual float ParseDimension(CssContextNode node, String content, float maxAvailableDimension, float
                                              additionalWidthFix)
        {
            float fontSize = CssDimensionParsingUtils.ParseAbsoluteFontSize(node.GetStyles().Get(CssConstants.FONT_SIZE
                                                                                                 ));
            UnitValue unitValue = CssDimensionParsingUtils.ParseLengthValueToPt(content, fontSize, 0);

            if (unitValue == null)
            {
                return(0);
            }
            if (unitValue.IsPointValue())
            {
                return(unitValue.GetValue() + additionalWidthFix);
            }
            return(maxAvailableDimension * unitValue.GetValue() / 100f);
        }
コード例 #26
0
        /// <summary>Applies margins to an element.</summary>
        /// <param name="cssProps">the CSS properties</param>
        /// <param name="context">the processor context</param>
        /// <param name="element">the element</param>
        /// <param name="baseValueHorizontal">value used by default for horizontal dimension</param>
        /// <param name="baseValueVertical">value used by default for vertical dimension</param>
        public static void ApplyMargins(IDictionary <String, String> cssProps, ProcessorContext context, IPropertyContainer
                                        element, float baseValueVertical, float baseValueHorizontal)
        {
            String marginTop    = cssProps.Get(CssConstants.MARGIN_TOP);
            String marginBottom = cssProps.Get(CssConstants.MARGIN_BOTTOM);
            String marginLeft   = cssProps.Get(CssConstants.MARGIN_LEFT);
            String marginRight  = cssProps.Get(CssConstants.MARGIN_RIGHT);
            // The check for display is useful at least for images
            bool  isBlock = element is IBlockElement || CssConstants.BLOCK.Equals(cssProps.Get(CssConstants.DISPLAY));
            bool  isImage = element is Image;
            float em      = CssDimensionParsingUtils.ParseAbsoluteLength(cssProps.Get(CssConstants.FONT_SIZE));
            float rem     = context.GetCssContext().GetRootFontSize();

            if (isBlock || isImage)
            {
                TrySetMarginIfNotAuto(Property.MARGIN_TOP, marginTop, element, em, rem, baseValueVertical);
                TrySetMarginIfNotAuto(Property.MARGIN_BOTTOM, marginBottom, element, em, rem, baseValueVertical);
            }
            bool isLeftAuto = !TrySetMarginIfNotAuto(Property.MARGIN_LEFT, marginLeft, element, em, rem, baseValueHorizontal
                                                     );
            bool isRightAuto = !TrySetMarginIfNotAuto(Property.MARGIN_RIGHT, marginRight, element, em, rem, baseValueHorizontal
                                                      );

            if (isBlock)
            {
                if (isLeftAuto && isRightAuto)
                {
                    element.SetProperty(Property.HORIZONTAL_ALIGNMENT, HorizontalAlignment.CENTER);
                }
                else
                {
                    if (isLeftAuto)
                    {
                        element.SetProperty(Property.HORIZONTAL_ALIGNMENT, HorizontalAlignment.RIGHT);
                    }
                    else
                    {
                        if (isRightAuto)
                        {
                            element.SetProperty(Property.HORIZONTAL_ALIGNMENT, HorizontalAlignment.LEFT);
                        }
                    }
                }
            }
        }
コード例 #27
0
        public static void Main(string[] args)
        {
            FileInfo file = new FileInfo(DEST.Replace("<filename>", ""));

            file.Directory.Create();
            string htmlSource = SRC + "responsive.html";
            PdfHtmlResponsiveDesign runner = new PdfHtmlResponsiveDesign();

            // Create a pdf for each page size
            for (int i = 0; i < pageSizes.Length; i++)
            {
                float  width = CssDimensionParsingUtils.ParseAbsoluteLength(pageSizes[i].GetWidth().ToString(CultureInfo.InvariantCulture));
                string dest  = DEST.Replace("<filename>",
                                            "responsive_" + width.ToString("0.0", CultureInfo.InvariantCulture) + ".pdf");

                runner.ManipulatePdf(htmlSource, dest, SRC, pageSizes[i], width);
            }
        }
コード例 #28
0
        /// <summary>Gets the parent font size.</summary>
        /// <param name="stylesContainer">the styles container</param>
        /// <returns>the parent font size</returns>
        private static float GetParentFontSize(IStylesContainer stylesContainer)
        {
            float parentFontSize;

            if (stylesContainer is INode && ((IElementNode)stylesContainer).ParentNode() is IStylesContainer)
            {
                INode  parent            = ((IElementNode)stylesContainer).ParentNode();
                String parentFontSizeStr = ((IStylesContainer)parent).GetStyles().Get(CssConstants.FONT_SIZE);
                parentFontSize = CssDimensionParsingUtils.ParseAbsoluteLength(parentFontSizeStr);
            }
            else
            {
                // let's take own font size for this unlikely case
                String ownFontSizeStr = stylesContainer.GetStyles().Get(CssConstants.FONT_SIZE);
                parentFontSize = CssDimensionParsingUtils.ParseAbsoluteLength(ownFontSizeStr);
            }
            return(parentFontSize);
        }
コード例 #29
0
 private static void SetLineHeight(IPropertyContainer elementToSet, String lineHeight, float em, float rem)
 {
     if (lineHeight != null && !CssConstants.NORMAL.Equals(lineHeight) && !CssConstants.AUTO.Equals(lineHeight)
         )
     {
         if (CssTypesValidationUtils.IsNumericValue(lineHeight))
         {
             float?number = CssDimensionParsingUtils.ParseFloat(lineHeight);
             if (number != null)
             {
                 elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateMultipliedValue((float)number));
             }
             else
             {
                 elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue());
             }
         }
         else
         {
             UnitValue lineHeightValue = CssDimensionParsingUtils.ParseLengthValueToPt(lineHeight, em, rem);
             if (lineHeightValue != null && lineHeightValue.IsPointValue())
             {
                 elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateFixedValue(lineHeightValue.GetValue()));
             }
             else
             {
                 if (lineHeightValue != null)
                 {
                     elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateMultipliedValue(lineHeightValue.GetValue()
                                                                                                     / 100f));
                 }
                 else
                 {
                     elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue());
                 }
             }
         }
     }
     else
     {
         elementToSet.SetProperty(Property.LINE_HEIGHT, LineHeight.CreateNormalValue());
     }
 }
コード例 #30
0
        public virtual void ResponsiveIText()
        {
            PageSize[] pageSizes = new PageSize[] { null, new PageSize(PageSize.A3.GetHeight(), PageSize.A4.GetHeight(
                                                                           )), new PageSize(760, PageSize.A4.GetHeight()), new PageSize(PageSize.A5.GetWidth(), PageSize.A4.GetHeight
                                                                                                                                            ()) };
            String htmlSource = sourceFolder + "responsiveIText.html";

            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssDimensionParsingUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String              outName             = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                PdfWriter           writer              = new PdfWriter(destinationFolder + outName);
                PdfDocument         pdfDoc              = new PdfDocument(writer);
                ConverterProperties converterProperties = new ConverterProperties();
                if (pageSize != null)
                {
                    pdfDoc.SetDefaultPageSize(pageSize);
                    MediaDeviceDescription mediaDescription = new MediaDeviceDescription(MediaType.SCREEN);
                    mediaDescription.SetWidth((float)pxWidth);
                    converterProperties.SetMediaDeviceDescription(mediaDescription);
                }
                using (FileStream fileInputStream = new FileStream(htmlSource, FileMode.Open, FileAccess.Read)) {
                    HtmlConverter.ConvertToPdf(fileInputStream, pdfDoc, converterProperties);
                }
                pdfDoc.Close();
            }
            foreach (PageSize pageSize in pageSizes)
            {
                float?pxWidth = null;
                if (pageSize != null)
                {
                    pxWidth = CssDimensionParsingUtils.ParseAbsoluteLength(pageSize.GetWidth().ToString());
                }
                String outName = "responsiveIText" + (pxWidth != null ? "_" + (int)(float)pxWidth : "") + ".pdf";
                String cmpName = "cmp_" + outName;
                NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(destinationFolder + outName, sourceFolder
                                                                                 + cmpName, destinationFolder, "diffResponsive_"));
            }
        }