Esempio n. 1
0
        /// <summary>Calculate margin box rectangles.</summary>
        /// <param name="resolvedPageMarginBoxes">the resolved page margin boxes</param>
        /// <returns>
        /// an array of
        /// <see cref="iText.Kernel.Geom.Rectangle"/>
        /// values
        /// </returns>
        private Rectangle[] CalculateMarginBoxRectangles(IList <PageMarginBoxContextNode> resolvedPageMarginBoxes)
        {
            // TODO It's a very basic implementation for now. In future resolve rectangles based on presence of certain margin boxes,
            //      also height and width properties should be taken into account.
            float     topMargin      = margins[0];
            float     rightMargin    = margins[1];
            float     bottomMargin   = margins[2];
            float     leftMargin     = margins[3];
            Rectangle withoutMargins = pageSize.Clone().ApplyMargins(topMargin, rightMargin, bottomMargin, leftMargin,
                                                                     false);
            float topBottomMarginWidth  = withoutMargins.GetWidth() / 3;
            float leftRightMarginHeight = withoutMargins.GetHeight() / 3;

            Rectangle[] hardcodedBoxRectangles = new Rectangle[] { new Rectangle(0, withoutMargins.GetTop(), leftMargin
                                                                                 , topMargin), new Rectangle(rightMargin, withoutMargins.GetTop(), topBottomMarginWidth, topMargin), new
                                                                   Rectangle(rightMargin + topBottomMarginWidth, withoutMargins.GetTop(), topBottomMarginWidth, topMargin
                                                                             ), new Rectangle(withoutMargins.GetRight() - topBottomMarginWidth, withoutMargins.GetTop(), topBottomMarginWidth
                                                                                              , topMargin), new Rectangle(withoutMargins.GetRight(), withoutMargins.GetTop(), topBottomMarginWidth,
                                                                                                                          topMargin), new Rectangle(withoutMargins.GetRight(), withoutMargins.GetTop() - leftRightMarginHeight,
                                                                                                                                                    rightMargin, leftRightMarginHeight), new Rectangle(withoutMargins.GetRight(), withoutMargins.GetBottom
                                                                                                                                                                                                           () + leftRightMarginHeight, rightMargin, leftRightMarginHeight), new Rectangle(withoutMargins.GetRight
                                                                                                                                                                                                                                                                                              (), withoutMargins.GetBottom(), rightMargin, leftRightMarginHeight), new Rectangle(withoutMargins.GetRight
                                                                                                                                                                                                                                                                                                                                                                                     (), 0, rightMargin, bottomMargin), new Rectangle(withoutMargins.GetRight() - topBottomMarginWidth, 0,
                                                                                                                                                                                                                                                                                                                                                                                                                                      topBottomMarginWidth, bottomMargin), new Rectangle(rightMargin + topBottomMarginWidth, 0, topBottomMarginWidth
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         , bottomMargin), new Rectangle(rightMargin, 0, topBottomMarginWidth, bottomMargin), new Rectangle(0, 0
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           , leftMargin, bottomMargin), new Rectangle(0, withoutMargins.GetBottom(), leftMargin, leftRightMarginHeight
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      ), new Rectangle(0, withoutMargins.GetBottom() + leftRightMarginHeight, leftMargin, leftRightMarginHeight
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ), new Rectangle(0, withoutMargins.GetTop() - leftRightMarginHeight, leftMargin, leftRightMarginHeight
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ) };
            return(hardcodedBoxRectangles);
        }
Esempio n. 2
0
        // TODO may be use here TABLOID? based on w3c tests, ledger in html is interpreted as portrait-oriented page
        /// <summary>Fetch the page size.</summary>
        /// <param name="pageSizeStr">the name of the page size ("a4", "letter",...)</param>
        /// <param name="em">the em value</param>
        /// <param name="rem">the root em value</param>
        /// <param name="defaultPageSize">the default page size</param>
        /// <returns>the page size</returns>
        internal static PageSize FetchPageSize(String pageSizeStr, float em, float rem, PageSize defaultPageSize)
        {
            PageSize pageSize = (PageSize)defaultPageSize.Clone();

            if (pageSizeStr == null || CssConstants.AUTO.Equals(pageSizeStr))
            {
                return(pageSize);
            }
            String[] pageSizeChunks = iText.IO.Util.StringUtil.Split(pageSizeStr, " ");
            String   firstChunk     = pageSizeChunks[0];

            if (IsLengthValue(firstChunk))
            {
                PageSize pageSizeBasedOnLength = ParsePageLengthValue(pageSizeChunks, em, rem);
                if (pageSizeBasedOnLength != null)
                {
                    pageSize = pageSizeBasedOnLength;
                }
                else
                {
                    ILog logger = LogManager.GetLogger(typeof(PageSizeParser));
                    logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.PAGE_SIZE_VALUE_IS_INVALID, pageSizeStr
                                                          ));
                }
            }
            else
            {
                bool?    landscape     = null;
                PageSize namedPageSize = null;
                if (IsLandscapePortraitValue(firstChunk))
                {
                    landscape = CssConstants.LANDSCAPE.Equals(firstChunk);
                }
                else
                {
                    namedPageSize = pageSizeConstants.Get(firstChunk);
                }
                if (pageSizeChunks.Length > 1)
                {
                    String secondChunk = pageSizeChunks[1];
                    if (IsLandscapePortraitValue(secondChunk))
                    {
                        landscape = CssConstants.LANDSCAPE.Equals(secondChunk);
                    }
                    else
                    {
                        namedPageSize = pageSizeConstants.Get(secondChunk);
                    }
                }
                bool b1 = pageSizeChunks.Length == 1 && (namedPageSize != null || landscape != null);
                bool b2 = namedPageSize != null && landscape != null;
                if (b1 || b2)
                {
                    if (namedPageSize != null)
                    {
                        pageSize = namedPageSize;
                    }
                    if (true.Equals(landscape))
                    {
                        pageSize = pageSize.Rotate();
                    }
                }
                else
                {
                    ILog logger = LogManager.GetLogger(typeof(PageSizeParser));
                    logger.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.PAGE_SIZE_VALUE_IS_INVALID, pageSizeStr
                                                          ));
                }
            }
            return(pageSize);
        }
Esempio n. 3
0
        private void DetermineSizes(int side)
        {
            float[][] marginsBordersPaddingsWidths = new float[][] { new float[4], new float[4], new float[4] };
            for (int i = 0; i < 3; i++)
            {
                if (renderers[side * 4 + i + 1] != null)
                {
                    marginsBordersPaddingsWidths[i][0] = GetSizeOfOneSide(renderers[side * 4 + i + 1], Property.MARGIN_TOP, Property
                                                                          .BORDER_TOP, Property.PADDING_TOP);
                    marginsBordersPaddingsWidths[i][1] = GetSizeOfOneSide(renderers[side * 4 + i + 1], Property.MARGIN_RIGHT,
                                                                          Property.BORDER_RIGHT, Property.PADDING_RIGHT);
                    marginsBordersPaddingsWidths[i][2] = GetSizeOfOneSide(renderers[side * 4 + i + 1], Property.MARGIN_BOTTOM,
                                                                          Property.BORDER_BOTTOM, Property.PADDING_BOTTOM);
                    marginsBordersPaddingsWidths[i][3] = GetSizeOfOneSide(renderers[side * 4 + i + 1], Property.MARGIN_LEFT, Property
                                                                          .BORDER_LEFT, Property.PADDING_LEFT);
                }
            }
            Rectangle withoutMargins = pageSize.Clone().ApplyMargins(margins[0], margins[1], margins[2], margins[3], false
                                                                     );
            IDictionary <String, PageMarginBoxContextNode> resolvedPMBMap = new Dictionary <String, PageMarginBoxContextNode
                                                                                            >();

            for (int i = side * 4 + 1; i < side * 4 + 4; i++)
            {
                if (nodes[i] != null)
                {
                    resolvedPMBMap.Put(nodes[i].GetMarginBoxName(), nodes[i]);
                }
            }
            DimensionContainer[] dims         = new DimensionContainer[3];
            String[]             cssRuleName  = GetRuleNames(side);
            float withoutMarginsWidthOrHeight = side % 2 == 0 ? withoutMargins.GetWidth() : withoutMargins.GetHeight();

            for (int i = 0; i < 3; i++)
            {
                if (side % 2 == 0)
                {
                    dims[i] = RetrievePageMarginBoxWidths(resolvedPMBMap.Get(cssRuleName[i]), renderers[side * 4 + i + 1], withoutMarginsWidthOrHeight
                                                          , marginsBordersPaddingsWidths[i][1] + marginsBordersPaddingsWidths[i][3]);
                }
                else
                {
                    dims[i] = RetrievePageMarginBoxHeights(resolvedPMBMap.Get(cssRuleName[i]), renderers[side * 4 + i + 1], margins
                                                           [side], withoutMarginsWidthOrHeight, marginsBordersPaddingsWidths[i][0] + marginsBordersPaddingsWidths
                                                           [i][2]);
                }
            }
            float centerOrMiddleCoord;

            float[] widthOrHeightResults;
            widthOrHeightResults = CalculatePageMarginBoxDimensions(dims[0], dims[1], dims[2], withoutMarginsWidthOrHeight
                                                                    );
            if (side % 2 == 0)
            {
                centerOrMiddleCoord = GetStartCoordForCenterOrMiddleBox(withoutMarginsWidthOrHeight, widthOrHeightResults[
                                                                            1], withoutMargins.GetLeft());
            }
            else
            {
                centerOrMiddleCoord = GetStartCoordForCenterOrMiddleBox(withoutMarginsWidthOrHeight, widthOrHeightResults[
                                                                            1], withoutMargins.GetBottom());
            }
            Rectangle[] result = GetRectangles(side, withoutMargins, centerOrMiddleCoord, widthOrHeightResults);
            for (int i = 0; i < 3; i++)
            {
                if (nodes[side * 4 + i + 1] != null)
                {
                    nodes[side * 4 + i + 1].SetPageMarginBoxRectangle(new Rectangle(result[i]).IncreaseHeight(EPSILON));
                    UnitValue width = UnitValue.CreatePointValue(result[i].GetWidth() - marginsBordersPaddingsWidths[i][1] - marginsBordersPaddingsWidths
                                                                 [i][3]);
                    UnitValue height = UnitValue.CreatePointValue(result[i].GetHeight() - marginsBordersPaddingsWidths[i][0] -
                                                                  marginsBordersPaddingsWidths[i][2]);
                    if (Math.Abs(width.GetValue()) < EPSILON || Math.Abs(height.GetValue()) < EPSILON)
                    {
                        renderers[side * 4 + i + 1] = null;
                    }
                    else
                    {
                        renderers[side * 4 + i + 1].SetProperty(Property.WIDTH, width);
                        renderers[side * 4 + i + 1].SetProperty(Property.HEIGHT, height);
                    }
                }
            }
        }