Exemplo n.º 1
0
        public static IContainer GenerateContainer(ILog logger)
        {
            if (_testContainer != null)
            {
                return(_testContainer);
            }

            if (logger == null)
            {
                logger = new NoOpLog();
            }

            var headerParser   = new FileHeaderParser(logger);
            var trailerParser  = new FileTrailerParser();
            var filterProvider = new MemoryFilterProvider(new DecodeParameterResolver(logger), new PngPredictor(), logger);

            var cmapParser = new CMapParser();
            var afmParser  = new AdobeFontMetricsParser();

            var container = new Container();

            container.Register(headerParser);
            container.Register(trailerParser);
            container.Register(filterProvider);
            container.Register(cmapParser);
            container.Register(afmParser);
            container.Register(logger);

            return(container);
        }
Exemplo n.º 2
0
        public static IContainer GenerateContainer(ILog logger)
        {
            if (_testContainer != null)
            {
                return(_testContainer);
            }

            if (logger == null)
            {
                logger = new NoOpLog();
            }

            var headerParser         = new FileHeaderParser(logger);
            var trailerParser        = new FileTrailerParser();
            var nameParser           = new CosNameParser();
            var dictionaryParser     = new CosDictionaryParser(nameParser, logger);
            var baseParser           = new CosBaseParser(nameParser, new CosStringParser(), dictionaryParser, new CosArrayParser());
            var streamParser         = new CosStreamParser(logger);
            var filterProvider       = new MemoryFilterProvider(new DecodeParameterResolver(logger), new PngPredictor(), logger);
            var crossReferenceParser = new CrossReferenceStreamParser(filterProvider);
            var objectStreamParser   = new ObjectStreamParser(logger, filterProvider, baseParser);
            var dynamicParser        = new DynamicParser(logger, baseParser, streamParser, objectStreamParser);

            var crossReferenceTableParser = new CrossReferenceParser(logger, dictionaryParser, baseParser, streamParser, crossReferenceParser, new CrossReferenceTableParser(),
                                                                     new OldCrossReferenceTableParser(logger, dictionaryParser, baseParser));

            var cmapParser = new CMapParser();
            var afmParser  = new AdobeFontMetricsParser();

            var container = new Container();

            container.Register(headerParser);
            container.Register(trailerParser);
            container.Register(nameParser);
            container.Register(dictionaryParser);
            container.Register(baseParser);
            container.Register(streamParser);
            container.Register(crossReferenceParser);
            container.Register(crossReferenceTableParser);
            container.Register(dynamicParser);
            container.Register(objectStreamParser);
            container.Register(filterProvider);
            container.Register(cmapParser);
            container.Register(afmParser);
            container.Register(logger);

            return(container);
        }
Exemplo n.º 3
0
        private static PdfDocument OpenDocument(IInputBytes inputBytes, ISeekableTokenScanner scanner, ILog log, bool isLenientParsing, IReadOnlyList <string> passwords)
        {
            var filterProvider = new MemoryFilterProvider(new DecodeParameterResolver(log), new PngPredictor(), log);

            CrossReferenceTable crossReferenceTable = null;

            var bruteForceSearcher = new BruteForceSearcher(inputBytes);
            var xrefValidator      = new XrefOffsetValidator(log);
            var objectChecker      = new XrefCosOffsetChecker(log, bruteForceSearcher);

            // We're ok with this since our intent is to lazily load the cross reference table.
            // ReSharper disable once AccessToModifiedClosure
            var locationProvider = new ObjectLocationProvider(() => crossReferenceTable, bruteForceSearcher);
            var pdfScanner       = new PdfTokenScanner(inputBytes, locationProvider, filterProvider, NoOpEncryptionHandler.Instance);

            var crossReferenceStreamParser = new CrossReferenceStreamParser(filterProvider);
            var crossReferenceParser       = new CrossReferenceParser(log, xrefValidator, objectChecker, crossReferenceStreamParser, new CrossReferenceTableParser());

            var version = FileHeaderParser.Parse(scanner, isLenientParsing, log);

            var crossReferenceOffset = FileTrailerParser.GetFirstCrossReferenceOffset(inputBytes, scanner,
                                                                                      isLenientParsing) + version.OffsetInFile;

            // TODO: make this use the scanner.
            var validator = new CrossReferenceOffsetValidator(xrefValidator);

            crossReferenceOffset = validator.Validate(crossReferenceOffset, scanner, inputBytes, isLenientParsing);

            crossReferenceTable = crossReferenceParser.Parse(inputBytes, isLenientParsing,
                                                             crossReferenceOffset,
                                                             version.OffsetInFile,
                                                             pdfScanner,
                                                             scanner);

            var fontDescriptorFactory = new FontDescriptorFactory();

            var(rootReference, rootDictionary) = ParseTrailer(crossReferenceTable, isLenientParsing,
                                                              pdfScanner,
                                                              out var encryptionDictionary);

            var encryptionHandler = encryptionDictionary != null ?
                                    (IEncryptionHandler) new EncryptionHandler(encryptionDictionary, crossReferenceTable.Trailer, passwords)
                : NoOpEncryptionHandler.Instance;

            pdfScanner.UpdateEncryptionHandler(encryptionHandler);

            var cidFontFactory = new CidFontFactory(pdfScanner, fontDescriptorFactory, filterProvider);
            var encodingReader = new EncodingReader(pdfScanner);

            var type1Handler = new Type1FontHandler(pdfScanner, filterProvider, fontDescriptorFactory, encodingReader);

            var fontFactory = new FontFactory(log, new Type0FontHandler(cidFontFactory,
                                                                        filterProvider, pdfScanner),
                                              new TrueTypeFontHandler(log, pdfScanner, filterProvider, fontDescriptorFactory, encodingReader, new SystemFontFinder(),
                                                                      type1Handler),
                                              type1Handler,
                                              new Type3FontHandler(pdfScanner, filterProvider, encodingReader));

            var resourceContainer = new ResourceStore(pdfScanner, fontFactory);

            var information = DocumentInformationFactory.Create(pdfScanner, crossReferenceTable.Trailer);

            var catalog = CatalogFactory.Create(rootReference, rootDictionary, pdfScanner, isLenientParsing);

            var pageFactory = new PageFactory(pdfScanner, resourceContainer, filterProvider,
                                              new PageContentParser(new ReflectionGraphicsStateOperationFactory()),
                                              log);

            var caching = new ParsingCachingProviders(bruteForceSearcher, resourceContainer);

            var acroFormFactory   = new AcroFormFactory(pdfScanner, filterProvider);
            var bookmarksProvider = new BookmarksProvider(log, pdfScanner, isLenientParsing);

            return(new PdfDocument(log, inputBytes, version, crossReferenceTable, isLenientParsing, caching, pageFactory, catalog, information,
                                   encryptionDictionary,
                                   pdfScanner,
                                   filterProvider,
                                   acroFormFactory,
                                   bookmarksProvider));
        }