protected override void OnLoad(
            )
        {
            LoadEncoding();

            // Glyph widths.
            {
                glyphWidths = new Dictionary <int, int>();
                PdfArray glyphWidthObjects = (PdfArray)CIDFontDictionary.Resolve(PdfName.W);
                if (glyphWidthObjects != null)
                {
                    for (IEnumerator <PdfDirectObject> iterator = glyphWidthObjects.GetEnumerator(); iterator.MoveNext();)
                    {
                        //TODO: this algorithm is valid only in case cid-to-gid mapping is identity (see cidtogid map)!!

                        /*
                         * NOTE: Font widths are grouped in one of the following formats [PDF:1.6:5.6.3]:
                         *  1. startCID [glyphWidth1 glyphWidth2 ... glyphWidthn]
                         *  2. startCID endCID glyphWidth
                         */
                        int startCID = ((PdfInteger)iterator.Current).RawValue;
                        iterator.MoveNext();
                        PdfDirectObject glyphWidthObject2 = iterator.Current;
                        if (glyphWidthObject2 is PdfArray) // Format 1: startCID [glyphWidth1 glyphWidth2 ... glyphWidthn].
                        {
                            int cID = startCID;
                            foreach (PdfDirectObject glyphWidthObject in (PdfArray)glyphWidthObject2)
                            {
                                glyphWidths[cID++] = ((IPdfNumber)glyphWidthObject).IntValue;
                            }
                        }
                        else // Format 2: startCID endCID glyphWidth.
                        {
                            int endCID = ((PdfInteger)glyphWidthObject2).RawValue;
                            iterator.MoveNext();
                            int glyphWidth = ((IPdfNumber)iterator.Current).IntValue;
                            for (int cID = startCID; cID <= endCID; cID++)
                            {
                                glyphWidths[cID] = glyphWidth;
                            }
                        }
                    }
                }
            }
            // Default glyph width.
            {
                // PK 2017-03-08 - Changed type from PdfInteger to IPdfNumber to avoid ClassCastException and allow for more granular font sizes
                IPdfNumber defaultWidthObject = (IPdfNumber)BaseDataObject[PdfName.DW];
                if (defaultWidthObject != null)
                {
                    DefaultWidth = defaultWidthObject.IntValue;
                }
            }
        }
Exemple #2
0
        protected override void OnLoad(
            )
        {
            LoadEncoding();

            // Glyph widths.
            if (glyphWidths == null)
            {
                glyphWidths = new Dictionary <int, int>();
                PdfArray glyphWidthObjects = (PdfArray)BaseDataObject.Resolve(PdfName.Widths);
                if (glyphWidthObjects != null)
                {
                    ByteArray charCode = new ByteArray(
                        new byte[]
                        { (byte)((PdfInteger)BaseDataObject[PdfName.FirstChar]).IntValue }
                        );
                    foreach (PdfDirectObject glyphWidthObject in glyphWidthObjects)
                    {
                        int glyphWidth = ((IPdfNumber)glyphWidthObject).IntValue;
                        if (glyphWidth > 0)
                        {
                            int code;
                            if (codes.TryGetValue(charCode, out code))
                            {   // [Zmiana]
                                try
                                {
                                    glyphWidths[glyphIndexes[code]] = glyphWidth;
                                }
                                catch (Exception)
                                {
                                }
                            }
                        }
                        charCode.Data[0]++;
                    }
                }
            }
            // Default glyph width.
            {
                IPdfNumber widthObject = (IPdfNumber)GetDescriptorValue(PdfName.AvgWidth);
                if (widthObject != null)
                {
                    AverageWidth = widthObject.IntValue;
                }
                widthObject = (IPdfNumber)GetDescriptorValue(PdfName.MissingWidth);
                if (widthObject != null)
                {
                    DefaultWidth = widthObject.IntValue;
                }
            }
        }
        /**
         * <summary>Gets the direction corresponding to the given value.</summary>
         */
        public static RotationEnum Get(IPdfNumber value)
        {
            if (value == null)
            {
                return(RotationEnum.Downward);
            }

            int normalizedValue = (int)(Math.Round(value.DoubleValue / 90) % 4) * 90;

            if (normalizedValue < 0)
            {
                normalizedValue += 360 * (int)Math.Ceiling(-normalizedValue / 360d);
            }
            return((RotationEnum)normalizedValue);
        }
Exemple #4
0
        public bool Remove(
            Page page
            )
        {
            PdfDictionary pageData = page.BaseDataObject;
            // Get the parent tree node!
            PdfDirectObject parent     = pageData[PdfName.Parent];
            PdfDictionary   parentData = (PdfDictionary)File.Resolve(parent);
            // Get the parent's page collection!
            PdfDirectObject kids     = parentData[PdfName.Kids];
            PdfArray        kidsData = (PdfArray)File.Resolve(kids);

            // Remove the page!
            kidsData.Remove(page.BaseObject);
            bool updateParent = !File.Update(kids); // Try to update the page collection.

            // Unbind the page from its parent!
            pageData[PdfName.Parent] = null;
            page.Update();
            // Decrementing the pages counters...
            do
            {
                // Get the page collection counter!
                PdfDirectObject count     = parentData[PdfName.Count];
                IPdfNumber      countData = (IPdfNumber)File.Resolve(count);
                // Decrement the counter at the current level!
                countData.RawValue--;
                updateParent |= !File.Update(count); // Try to update the counter.
                // Is the parent tree node to be updated?

                /*
                 * NOTE: It avoids to update the parent tree node if its modified fields are all
                 * indirect objects which perform independent updates.
                 */
                if (updateParent)
                {
                    File.Update(parent);
                    updateParent = false; // Reset.
                }

                // Iterate upward!
                parent     = parentData[PdfName.Parent];
                parentData = (PdfDictionary)File.Resolve(parent);
            } while(parent != null);

            return(true);
        }
Exemple #5
0
        /**
         * <summary>Gets the pattern corresponding to the specified components.</summary>
         */
        public static LineDash Get(PdfArray dashArray, IPdfNumber dashPhase)
        {
            if (dashArray == null)
            {
                return(null);
            }

            // Dash array.
            double[] dashArrayValue = new double[dashArray.Count];
            for (int index = 0, length = dashArrayValue.Length; index < length; index++)
            {
                dashArrayValue[index] = ((IPdfNumber)dashArray[index]).DoubleValue;
            }
            // Dash phase.
            double dashPhaseValue = dashPhase != null ? ((IPdfNumber)dashPhase).DoubleValue : 0;

            return(new LineDash(dashArrayValue, dashPhaseValue));
        }
Exemple #6
0
        /**
         * <summary>Gets the pattern corresponding to the specified components.</summary>
         */
        public static LineDash Get(PdfArray dashArray, IPdfNumber dashPhase)
        {
            if (dashArray == null)
            {
                return(null);
            }

            // Dash array.
            var dashArrayValue = new float[dashArray.Count];

            for (int index = 0, length = dashArrayValue.Length; index < length; index++)
            {
                dashArrayValue[index] = dashArray.GetFloat(index);
            }
            // Dash phase.
            var dashPhaseValue = dashPhase != null ? dashPhase.FloatValue : 0;

            return(new LineDash(dashArrayValue, dashPhaseValue));
        }
Exemple #7
0
        protected override void OnLoad(
            )
        {
            LoadEncoding();

            // Glyph widths.
            if (glyphWidths == null)
            {
                glyphWidths = new Dictionary <int, int>();
                PdfArray glyphWidthObjects = (PdfArray)BaseDataObject.Resolve(PdfName.Widths);
                if (glyphWidthObjects != null)
                {
                    ByteArray charCode = new ByteArray(
                        new byte[]
                        { (byte)((PdfInteger)BaseDataObject[PdfName.FirstChar]).IntValue }
                        );
                    foreach (PdfDirectObject glyphWidthObject in glyphWidthObjects)
                    {
                        int glyphWidth = ((IPdfNumber)glyphWidthObject).IntValue;
                        if (glyphWidth > 0)
                        {
                            int code;
                            if (codes.TryGetValue(charCode, out code))
                            {
                                glyphWidths[glyphIndexes[code]] = glyphWidth;
                            }
                        }
                        charCode.Data[0]++;
                    }
                }
            }
            // Default glyph width.
            {
                PdfDictionary descriptor = Descriptor;
                if (descriptor != null)
                {
                    IPdfNumber defaultGlyphWidthObject = (IPdfNumber)descriptor[PdfName.MissingWidth];
                    defaultGlyphWidth = (defaultGlyphWidthObject != null ? defaultGlyphWidthObject.IntValue : 0);
                }
            }
        }
Exemple #8
0
        /**
         * Add a collection of pages at the specified position.
         * <param name="index">Addition position. To append, use value -1.</param>
         * <param name="pages">Collection of pages to add.</param>
         */
        private void CommonAddAll <TPage>(
            int index,
            ICollection <TPage> pages
            )
            where TPage : Page
        {
            PdfDirectObject parent;
            PdfDictionary   parentData;
            PdfDirectObject kids;
            PdfArray        kidsData;
            int             offset;

            // Append operation?
            if (index == -1) // Append operation.
            {
                // Get the parent tree node!
                parent     = BaseObject;
                parentData = BaseDataObject;
                // Get the parent's page collection!
                kids     = parentData[PdfName.Kids];
                kidsData = (PdfArray)File.Resolve(kids);
                offset   = 0; // Not used.
            }
            else // Insert operation.
            {
                // Get the page currently at the specified position!
                Page pivotPage = this[index];
                // Get the parent tree node!
                parent     = pivotPage.BaseDataObject[PdfName.Parent];
                parentData = (PdfDictionary)File.Resolve(parent);
                // Get the parent's page collection!
                kids     = parentData[PdfName.Kids];
                kidsData = (PdfArray)File.Resolve(kids);
                // Get the insertion's relative position within the parent's page collection!
                offset = kidsData.IndexOf(pivotPage.BaseObject);
            }

            // Adding the pages...
            foreach (Page page in pages)
            {
                // Append?
                if (index == -1) // Append.
                {
                    // Append the page to the collection!
                    kidsData.Add(page.BaseObject);
                }
                else // Insert.
                {
                    // Insert the page into the collection!
                    kidsData.Insert(
                        offset++,
                        page.BaseObject
                        );
                }
                // Bind the page to the collection!
                page.BaseDataObject[PdfName.Parent] = parent;
                page.Update();
            }
            bool updateParent = !File.Update(kids); // Try to update the page collection.

            // Incrementing the pages counters...
            do
            {
                // Get the page collection counter!
                PdfDirectObject count     = parentData[PdfName.Count];
                IPdfNumber      countData = (IPdfNumber)File.Resolve(count);
                // Increment the counter at the current level!
                countData.RawValue += pages.Count;
                updateParent       |= !File.Update(count); // Try to update the page counter.
                // Is the parent tree node to be updated?

                /*
                 * NOTE: It avoids to update the parent tree node if its modified fields are all
                 * indirect objects which perform independent updates.
                 */
                if (updateParent)
                {
                    File.Update(parent);
                    updateParent = false; // Reset.
                }

                // Iterate upward!
                parent     = parentData[PdfName.Parent];
                parentData = (PdfDictionary)File.Resolve(parent);
            } while(parent != null);
        }