all files / core/ pdf-section.js

10.45% Statements 7/67
0% Branches 0/24
50% Functions 2/4
10.45% Lines 7/67
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88                                                                                                                                                                  
define(["require", "exports", "./pdf-primitives", "./pdf-page", "./utils"], function (require, exports, pdf_primitives_1, pdf_page_1, utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var PdfSection = (function () {
        function PdfSection(document, settings) {
            this._pageCount = 0;
            this._document = document;
            this._crossReference = document._crossReference;
            var sectionDictionary = new pdf_primitives_1._PdfDictionary(this._crossReference);
            sectionDictionary.update('Type', pdf_primitives_1._PdfName.get('Pages'));
            this._pageSettings = settings;
            utils_1._updatePageSettings(sectionDictionary, settings);
            var sectionReference = this._crossReference._getNextReference();
            this._crossReference._cacheMap.set(sectionReference, sectionDictionary);
            sectionDictionary.objId = sectionReference.toString();
            sectionDictionary.update('Kids', []);
            sectionDictionary.update('Count', 0);
            this._dictionary = sectionDictionary;
            this._reference = sectionReference;
            var pageCount = this._document.pageCount;
            if (pageCount === 0) {
                var parentReference = this._document._catalog._catalogDictionary._get('Pages');
                var topPagesDictionary = this._document._catalog._topPagesDictionary;
                if (topPagesDictionary) {
                    if (topPagesDictionary.has('Kids')) {
                        var kids = topPagesDictionary.get('Kids');
                        if (kids) {
                            kids.push(sectionReference);
                            topPagesDictionary.update('Kids', kids);
                            sectionDictionary.update('Parent', parentReference);
                        }
                    }
                    else {
                        topPagesDictionary.update('Kids', [sectionReference]);
                        sectionDictionary.update('Parent', parentReference);
                    }
                }
            }
            else {
                var lastPage = this._document.getPage(pageCount - 1);
                if (lastPage && lastPage._pageDictionary) {
                    var parentReference = lastPage._pageDictionary._get('Parent');
                    var parentDictionary = this._crossReference._fetch(parentReference);
                    if (parentDictionary && parentDictionary.has('Kids')) {
                        var kids = parentDictionary.get('Kids');
                        if (kids) {
                            kids.push(sectionReference);
                            parentDictionary.update('Kids', kids);
                            sectionDictionary.update('Parent', parentReference);
                        }
                    }
                }
            }
        }
        PdfSection.prototype.addPage = function () {
            var pageIndex = this._document.pageCount === 0 ? 0 : (this._document.pageCount);
            var pageDictionary = new pdf_primitives_1._PdfDictionary(this._crossReference);
            pageDictionary.update('Type', pdf_primitives_1._PdfName.get('Page'));
            var pageReference = this._crossReference._getNextReference();
            this._crossReference._cacheMap.set(pageReference, pageDictionary);
            pageDictionary.objId = pageReference.toString();
            pageDictionary.update('Parent', this._reference);
            if (this._dictionary.has('Kids')) {
                var kids = this._dictionary.get('Kids');
                if (kids) {
                    kids.push(pageReference);
                    this._dictionary.update('Kids', kids);
                    utils_1._updatePageCount(this._dictionary, 1);
                    this._document._pageCount++;
                }
            }
            else {
                this._dictionary.update('Kids', [pageReference]);
                utils_1._updatePageCount(this._dictionary, 1);
                this._document._pageCount = 1;
            }
            this._pageCount++;
            var result = new pdf_page_1.PdfPage(this._crossReference, pageIndex, pageDictionary, pageReference);
            result._pageSettings = this._pageSettings;
            result._isNew = true;
            this._document._pages.set(pageIndex, result);
            return result;
        };
        return PdfSection;
    }());
    exports.PdfSection = PdfSection;
});