Пример #1
0
        public PdfVector Transform(PdfVector original)
        {
            var x = A * original.X + C * original.Y + E;
            var y = B * original.X + D * original.Y + F;

            return(new PdfVector(x, y));
        }
Пример #2
0
        public void ConstructorSetsValues()
        {
            var vector = new PdfVector(5.2m, 6.9m);

            Assert.Equal(5.2m, vector.X);
            Assert.Equal(6.9m, vector.Y);
        }
Пример #3
0
        private VerticalWritingMetrics ReadVerticalDisplacements(PdfDictionary dict)
        {
            var verticalDisplacements = new Dictionary <int, decimal>();
            var positionVectors       = new Dictionary <int, PdfVector>();

            VerticalVectorComponents dw2;

            if (!dict.TryGetItemOfType(CosName.DW2, out COSArray arrayVerticalComponents))
            {
                dw2 = new VerticalVectorComponents(880, -1000);
            }
            else
            {
                var position     = ((ICosNumber)arrayVerticalComponents.get(0)).AsDecimal();
                var displacement = ((ICosNumber)arrayVerticalComponents.get(1)).AsDecimal();

                dw2 = new VerticalVectorComponents(position, displacement);
            }

            // vertical metrics for individual CIDs.
            if (dict.TryGetItemOfType(CosName.W2, out COSArray w2))
            {
                for (var i = 0; i < w2.size(); i++)
                {
                    var c    = (ICosNumber)w2.get(i);
                    var next = w2.get(++i);
                    if (next is COSArray array)
                    {
                        for (int j = 0; j < array.size(); j++)
                        {
                            int cid = c.AsInt() + j;
                            var w1y = (ICosNumber)array.get(j);
                            var v1x = (ICosNumber)array.get(++j);
                            var v1y = (ICosNumber)array.get(++j);

                            verticalDisplacements[cid] = w1y.AsDecimal();

                            positionVectors[cid] = new PdfVector(v1x.AsDecimal(), v1y.AsDecimal());
                        }
                    }
                    else
                    {
                        int first = c.AsInt();
                        int last  = ((ICosNumber)next).AsInt();
                        var w1y   = (ICosNumber)w2.get(++i);
                        var v1x   = (ICosNumber)w2.get(++i);
                        var v1y   = (ICosNumber)w2.get(++i);

                        for (var cid = first; cid <= last; cid++)
                        {
                            verticalDisplacements[cid] = w1y.AsDecimal();

                            positionVectors[cid] = new PdfVector(v1x.AsDecimal(), v1y.AsDecimal());
                        }
                    }
                }
            }

            return(new VerticalWritingMetrics(dw2, verticalDisplacements, positionVectors));
        }
Пример #4
0
        public ContentStreamProcessor(PdfRectangle cropBox, IResourceStore resourceStore, UserSpaceUnit userSpaceUnit, PageRotationDegrees rotation,
                                      IPdfTokenScanner pdfScanner,
                                      IPageContentParser pageContentParser,
                                      IFilterProvider filterProvider,
                                      ILog log,
                                      bool clipPaths,
                                      PdfVector pageSize)
        {
            this.resourceStore     = resourceStore;
            this.userSpaceUnit     = userSpaceUnit;
            this.rotation          = rotation;
            this.pdfScanner        = pdfScanner ?? throw new ArgumentNullException(nameof(pdfScanner));
            this.pageContentParser = pageContentParser ?? throw new ArgumentNullException(nameof(pageContentParser));
            this.filterProvider    = filterProvider ?? throw new ArgumentNullException(nameof(filterProvider));
            this.log       = log;
            this.clipPaths = clipPaths;
            this.pageSize  = pageSize;

            // initiate CurrentClippingPath to cropBox
            var clippingSubpath = new PdfSubpath();

            clippingSubpath.Rectangle(cropBox.BottomLeft.X, cropBox.BottomLeft.Y, cropBox.Width, cropBox.Height);
            var clippingPath = new PdfPath()
            {
                clippingSubpath
            };

            clippingPath.SetClipping(FillingRule.NonZeroWinding);

            graphicsStack.Push(new CurrentGraphicsState()
            {
                CurrentClippingPath = clippingPath
            });
            ColorSpaceContext = new ColorSpaceContext(GetCurrentState, resourceStore);
        }
Пример #5
0
 public FontMetrics(decimal afmVersion, IReadOnlyList <string> comments, int metricSets, string fontName, string fullName, string familyName, string weight, PdfRectangle boundingBox, string version, string notice, string encodingScheme, int mappingScheme, int escapeCharacter, string characterSet, int characters, bool isBaseFont, PdfVector vVector, bool isFixedV, decimal capHeight, decimal xHeight, decimal ascender, decimal descender, decimal underlinePosition, decimal underlineThickness, decimal italicAngle, CharacterWidth characterWidth, decimal horizontalStemWidth, decimal verticalStemWidth, IReadOnlyDictionary <string, IndividualCharacterMetric> characterMetrics)
 {
     AfmVersion          = afmVersion;
     Comments            = comments;
     MetricSets          = metricSets;
     FontName            = fontName;
     FullName            = fullName;
     FamilyName          = familyName;
     Weight              = weight;
     BoundingBox         = boundingBox;
     Version             = version;
     Notice              = notice;
     EncodingScheme      = encodingScheme;
     MappingScheme       = mappingScheme;
     EscapeCharacter     = escapeCharacter;
     CharacterSet        = characterSet;
     Characters          = characters;
     IsBaseFont          = isBaseFont;
     VVector             = vVector;
     IsFixedV            = isFixedV;
     CapHeight           = capHeight;
     XHeight             = xHeight;
     Ascender            = ascender;
     Descender           = descender;
     UnderlinePosition   = underlinePosition;
     UnderlineThickness  = underlineThickness;
     ItalicAngle         = italicAngle;
     CharacterWidth      = characterWidth;
     HorizontalStemWidth = horizontalStemWidth;
     VerticalStemWidth   = verticalStemWidth;
     CharacterMetrics    = characterMetrics;
 }
Пример #6
0
        internal PdfRectangle(PdfVector topLeft, PdfVector topRight, PdfVector bottomLeft, PdfVector bottomRight)
        {
            TopLeft  = topLeft.ToPoint();
            TopRight = topRight.ToPoint();

            BottomLeft  = bottomLeft.ToPoint();
            BottomRight = bottomRight.ToPoint();
        }
Пример #7
0
        public void ScaleMultipliesLeavesOriginalUnchanged()
        {
            var vector = new PdfVector(5.2m, 6.9m);

            var scaled = vector.Scale(0.7m);

            Assert.Equal(5.2m, vector.X);
            Assert.Equal(5.2m * 0.7m, scaled.X);

            Assert.Equal(6.9m, vector.Y);
            Assert.Equal(6.9m * 0.7m, scaled.Y);
        }
Пример #8
0
        public void ScaleMultipliesLeavesOriginalUnchanged()
        {
            var vector = new PdfVector(5.2d, 6.9d);

            var scaled = vector.Scale(0.7d);

            Assert.Equal(5.2d, vector.X);
            Assert.Equal(5.2d * 0.7d, scaled.X);

            Assert.Equal(6.9d, vector.Y);
            Assert.Equal(6.9d * 0.7d, scaled.Y);
        }
Пример #9
0
        internal PdfRectangle(PdfVector topLeft, PdfVector topRight, PdfVector bottomLeft, PdfVector bottomRight)
        {
            TopLeft  = topLeft.ToPoint();
            TopRight = topRight.ToPoint();

            BottomLeft  = bottomLeft.ToPoint();
            BottomRight = bottomRight.ToPoint();

            Width  = bottomRight.Subtract(bottomLeft).GetMagnitude();
            Height = topLeft.Subtract(bottomLeft).GetMagnitude();

            Area = Width * Height;
        }
Пример #10
0
        private VerticalWritingMetrics ReadVerticalDisplacements(DictionaryToken dict)
        {
            var verticalDisplacements = new Dictionary <int, decimal>();
            var positionVectors       = new Dictionary <int, PdfVector>();

            VerticalVectorComponents dw2;

            if (!dict.TryGet(NameToken.Dw2, out var dw2Token) || !(dw2Token is ArrayToken arrayVerticalComponents))
            {
                dw2 = new VerticalVectorComponents(880, -1000);
            }
            else
            {
                var position     = ((NumericToken)arrayVerticalComponents.Data[0]).Data;
                var displacement = ((NumericToken)arrayVerticalComponents.Data[1]).Data;

                dw2 = new VerticalVectorComponents(position, displacement);
            }

            // vertical metrics for individual CIDs.
            if (dict.TryGet(NameToken.W2, out var w2Token) && w2Token is ArrayToken w2)
            {
                for (var i = 0; i < w2.Data.Count; i++)
                {
                    var c    = (NumericToken)w2.Data[i];
                    var next = w2.Data[++i];

                    if (next is ArrayToken array)
                    {
                        for (int j = 0; j < array.Data.Count; j++)
                        {
                            int cid = c.Int + j;
                            var w1y = (NumericToken)array.Data[j];
                            var v1x = (NumericToken)array.Data[++j];
                            var v1y = (NumericToken)array.Data[++j];

                            verticalDisplacements[cid] = w1y.Data;

                            positionVectors[cid] = new PdfVector(v1x.Data, v1y.Data);
                        }
                    }
                    else
                    {
                        int first = c.Int;
                        int last  = ((NumericToken)next).Int;
                        var w1y   = (NumericToken)w2.Data[++i];
                        var v1x   = (NumericToken)w2.Data[++i];
                        var v1y   = (NumericToken)w2.Data[++i];

                        for (var cid = first; cid <= last; cid++)
                        {
                            verticalDisplacements[cid] = w1y.Data;

                            positionVectors[cid] = new PdfVector(v1x.Data, v1y.Data);
                        }
                    }
                }
            }

            return(new VerticalWritingMetrics(dw2, verticalDisplacements, positionVectors));
        }
Пример #11
0
 internal PdfRectangle(PdfVector topLeft, PdfVector topRight, PdfVector bottomLeft, PdfVector bottomRight)
     : this(topLeft.ToPoint(), topRight.ToPoint(), bottomLeft.ToPoint(), bottomRight.ToPoint())
 {
 }
Пример #12
0
        private static VerticalWritingMetrics ReadVerticalDisplacements(DictionaryToken dict)
        {
            var verticalDisplacements = new Dictionary <int, decimal>();
            var positionVectors       = new Dictionary <int, PdfVector>();

            // The default position vector and displacement vector are specified by the DW2 entry.
            VerticalVectorComponents dw2;

            if (!dict.TryGet(NameToken.Dw2, out var dw2Token) || !(dw2Token is ArrayToken arrayVerticalComponents))
            {
                dw2 = VerticalVectorComponents.Default;
            }
            else
            {
                var position     = ((NumericToken)arrayVerticalComponents.Data[0]).Data;
                var displacement = ((NumericToken)arrayVerticalComponents.Data[1]).Data;

                dw2 = new VerticalVectorComponents(position, displacement);
            }

            // vertical metrics for individual CIDs.
            if (dict.TryGet(NameToken.W2, out var w2Token) && w2Token is ArrayToken w2)
            {
                for (var i = 0; i < w2.Data.Count; i++)
                {
                    var c    = (NumericToken)w2.Data[i];
                    var next = w2.Data[++i];

                    if (next is ArrayToken array)
                    {
                        for (var j = 0; j < array.Data.Count; j++)
                        {
                            var cid = c.Int + j;
                            // ReSharper disable InconsistentNaming
                            var w1y = (NumericToken)array.Data[j];
                            var v1x = (NumericToken)array.Data[++j];
                            var v1y = (NumericToken)array.Data[++j];

                            verticalDisplacements[cid] = w1y.Data;

                            positionVectors[cid] = new PdfVector(v1x.Data, v1y.Data);
                        }
                    }
                    else
                    {
                        var first = c.Int;
                        var last  = ((NumericToken)next).Int;
                        var w1y   = (NumericToken)w2.Data[++i];
                        var v1x   = (NumericToken)w2.Data[++i];
                        var v1y   = (NumericToken)w2.Data[++i];
                        // ReSharper restore InconsistentNaming

                        for (var cid = first; cid <= last; cid++)
                        {
                            verticalDisplacements[cid] = w1y.Data;

                            positionVectors[cid] = new PdfVector(v1x.Data, v1y.Data);
                        }
                    }
                }
            }

            return(new VerticalWritingMetrics(dw2, verticalDisplacements, positionVectors));
        }
Пример #13
0
        internal PdfRectangle Rotate(PdfRectangle rectangle, PdfVector pageSize)
        {
            // TODO: this is a bit of a hack because I don't understand matrices

            /* There should be a single Affine Transform we can apply to any point resulting
             * from a content stream operation which will rotate the point and translate it back to
             * a point where the origin is in the page's lower left corner.
             *
             * For example this matrix represents a (clockwise) rotation and translation:
             * [  cos  sin  tx ]
             * [ -sin  cos  ty ]
             * [    0    0   1 ]
             *
             * The values of tx and ty are those required to move the origin back to the expected origin (lower-left).
             * The corresponding values should be:
             * Rotation:  0   90  180  270
             *       tx:  0    0    w    w
             *       ty:  0    h    h    0
             *
             * Where w and h are the page width and height after rotation.
             */
            double cos, sin;
            double dx = 0, dy = 0;

            switch (Value)
            {
            case 0:
                return(rectangle);

            case 90:
                cos = 0;
                sin = 1;
                dy  = pageSize.Y;
                break;

            case 180:
                cos = -1;
                sin = 0;
                dx  = pageSize.X;
                dy  = pageSize.Y;
                break;

            case 270:
                cos = 0;
                sin = -1;
                dx  = pageSize.X;
                break;

            default:
                throw new InvalidOperationException($"Invalid value for rotation: {Value}.");
            }

            PdfPoint Multiply(PdfPoint pt)
            {
                return(new PdfPoint((pt.X * cos) + (pt.Y * sin) + dx,
                                    (pt.X * -sin) + (pt.Y * cos) + dy));
            }

            return(new PdfRectangle(Multiply(rectangle.TopLeft), Multiply(rectangle.TopRight),
                                    Multiply(rectangle.BottomLeft), Multiply(rectangle.BottomRight)));
        }
Пример #14
0
 public static PdfMatrix3By2 CreateTranslation(PdfVector vector) => new PdfMatrix3By2(1, 0, 0, 1, vector.X, vector.Y);
Пример #15
0
 public void SetVVector(decimal x, decimal y)
 {
     VVector = new PdfVector(x, y);
 }