| 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959 | 1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
1×
| define(["require", "exports", "../base-stream", "../content-parser", "../enumerator", "../pdf-document", "../pdf-primitives", "../utils", "./layer"], function (require, exports, base_stream_1, content_parser_1, enumerator_1, pdf_document_1, pdf_primitives_1, utils_1, layer_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var PdfLayerCollection = (function () {
function PdfLayerCollection(document, layer) {
var _this = this;
this._subLayer = false;
this._isLayerContainsResource = false;
this._layerDictionary = new Map();
this._bdcCount = 0;
if (!this._list) {
this._list = [];
}
this._crossReference = document._crossReference;
this._catalog = document._catalog;
this._document = document;
if (layer) {
this._parent = layer;
}
else {
var _layerDictionary_1;
var _layerReference_1;
if (this._document && this._document._catalog && this._document._catalog._catalogDictionary
&& this._document._catalog._catalogDictionary.has('OCProperties')) {
var ocProperties = this._document._catalog._catalogDictionary.get('OCProperties');
if (ocProperties && ocProperties.has('OCGs')) {
var ocGroup = ocProperties.get('OCGs');
if (ocGroup && Array.isArray(ocGroup)) {
ocGroup.forEach(function (item) {
_layerReference_1 = item;
if (_layerReference_1 instanceof pdf_primitives_1._PdfReference) {
_layerDictionary_1 = _this._crossReference._fetch(_layerReference_1);
var layer_2 = new layer_1.PdfLayer();
if (_layerDictionary_1) {
if (_layerDictionary_1.has('Name')) {
var layerName = _layerDictionary_1.get('Name');
layer_2.name = layerName;
layer_2._dictionary = _layerDictionary_1;
layer_2._crossReference = _this._document._crossReference;
layer_2._referenceHolder = _layerReference_1;
var layerId = _layerDictionary_1.get('LayerID');
if (layerId) {
layer_2._layerId = layerId.name;
}
var _print = _layerDictionary_1.getRaw('Usage');
if (_print && _print instanceof pdf_primitives_1._PdfDictionary) {
var printOption = _print.get('Print');
if (printOption && printOption instanceof pdf_primitives_1._PdfDictionary) {
layer_2._printOption = printOption;
if (printOption.has('PrintState')) {
_this._setPrintState(printOption, layer_2);
}
}
var viewState = _print.get('View');
if (viewState && viewState instanceof pdf_primitives_1._PdfDictionary && viewState.has('ViewState')) {
var view = viewState.get('ViewState');
if (view.name === 'OFF') {
layer_2.visible = false;
}
}
}
if (_print && _print instanceof pdf_primitives_1._PdfReference) {
var printRef = _this._crossReference._fetch(_print);
if (printRef && printRef instanceof pdf_primitives_1._PdfDictionary) {
var _printOptionReference = printRef.getRaw('Print');
if (_printOptionReference && _printOptionReference instanceof pdf_primitives_1._PdfReference) {
var subtytpe = _this._crossReference._fetch(_printOptionReference);
if (subtytpe && subtytpe instanceof pdf_primitives_1._PdfDictionary) {
layer_2._printOption = subtytpe;
if (subtytpe.has('PrintState')) {
_this._setPrintState(subtytpe, layer_2);
}
}
}
var viewStateReference = printRef.getRaw('View');
if (viewStateReference && viewStateReference instanceof pdf_primitives_1._PdfReference) {
var viewState = _this._crossReference._fetch(viewStateReference);
if (viewState && viewState instanceof pdf_primitives_1._PdfDictionary && viewState.has('ViewState')) {
var view = viewState.get('ViewState');
if (view.name === 'OFF') {
layer_2.visible = false;
}
}
}
}
}
}
layer_2._document = document;
layer_2._layer = layer_2;
_this._layerDictionary.set(_layerReference_1, layer_2);
_this._list.push(layer_2);
}
}
});
}
this._checkLayerLock(ocProperties);
this._checkLayerVisible(ocProperties);
this._checkParentLayer(ocProperties);
this._createLayerHierarchical(ocProperties);
}
}
}
}
Object.defineProperty(PdfLayerCollection.prototype, "_isSkip", {
get: function () {
return this._bdcCount > 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(PdfLayerCollection.prototype, "count", {
get: function () {
return this._list.length;
},
enumerable: true,
configurable: true
});
PdfLayerCollection.prototype.at = function (index) {
return this._list[index];
};
PdfLayerCollection.prototype.add = function (name, visible) {
var newLayer = new layer_1.PdfLayer();
newLayer._document = this._document;
newLayer._crossReference = this._document._crossReference;
newLayer.name = name;
if (visible !== null && typeof visible !== 'undefined') {
newLayer.visible = visible;
}
newLayer._layerId = 'OCG_' + utils_1._getNewGuidString();
newLayer._subLayerPosition = 0;
newLayer._layer = newLayer;
this._addLayer(newLayer);
return newLayer;
};
PdfLayerCollection.prototype.contains = function (arg) {
if (!arg) {
throw new Error('Layer cannot be null or undefined');
}
if (typeof arg === 'string') {
return this._list.some(function (layer) { return layer.name === arg; });
}
else if (arg instanceof layer_1.PdfLayer) {
if (this._list.indexOf(arg) !== -1) {
return true;
}
}
return false;
};
PdfLayerCollection.prototype.clear = function () {
for (var i = this._list.length - 1; i > -1; i--) {
var layer = this._list[i];
this._removeLayer(layer, true);
}
this._list.length = 0;
};
PdfLayerCollection.prototype.indexOf = function (layer) {
if (!layer) {
throw new Error('Layer cannot be null or undefined');
}
return this._list.indexOf(layer);
};
PdfLayerCollection.prototype.move = function (index, layer) {
if (index < 0 || index >= this._list.length) {
throw new Error('Index cannot be less than 0 or greater than array length');
}
if (!layer) {
throw new Error('Layer cannot be null or undefined');
}
var position;
for (var i = 0; i < this._list.length; i++) {
if (this._list[i] === layer) {
position = this.indexOf(this._list[i]);
break;
}
}
if (position !== null && typeof position !== 'undefined' && position !== index) {
this._list.splice(position, 1);
this._list.splice(index, 0, layer);
this._insertLayer(index, layer);
}
};
PdfLayerCollection.prototype.removeAt = function (arg1, arg2) {
if (arg1 < 0 || arg1 >= this._list.length) {
throw new Error('Index cannot be less than 0 or greater than array length');
}
var layer = this._list[arg1];
this._list.splice(arg1, 1);
if (layer) {
this._removeLayer(layer, arg2 || false);
if (layer._child.length > 0) {
for (var _i = 0, _a = layer._child; _i < _a.length; _i++) {
var child = _a[_i];
this._removeLayer(child, false);
var index = this._list.indexOf(child);
if (index !== -1) {
this._list.splice(index, 1);
}
}
}
}
};
PdfLayerCollection.prototype.remove = function (arg1, arg2) {
if (arg1 instanceof layer_1.PdfLayer) {
var layer = arg1;
var index = this._list.indexOf(layer);
if (index !== -1) {
this.removeAt(index, arg2 || false);
}
}
else if (typeof arg1 === 'string') {
for (var i = 0; i < this._list.length; i++) {
var layer = this._list[i];
var index = this._list.indexOf(layer);
if (layer.name === arg1 && index !== -1) {
this.removeAt(index, arg2 || false);
i = i - 1;
}
}
}
};
PdfLayerCollection.prototype._setPrintState = function (printOption, layer) {
var printState = printOption.get('PrintState');
if (printState && printState instanceof pdf_primitives_1._PdfName) {
if (printState.name === 'ON') {
layer.printState = enumerator_1.PdfPrintState.alwaysPrint;
}
else {
layer.printState = enumerator_1.PdfPrintState.neverPrint;
}
}
};
PdfLayerCollection.prototype._addLayer = function (layer) {
this._list.push(layer);
var index = this._list.length - 1;
if (this._document instanceof pdf_document_1.PdfDocument) {
this._createLayer(layer);
}
layer._layer = layer;
return index;
};
PdfLayerCollection.prototype._createLayer = function (layer) {
var _this = this;
var ocProperties = new pdf_primitives_1._PdfDictionary(this._crossReference);
var ocGroups = this._createOptionalContentDictionary(layer);
var isPresent = false;
if (this._document && this._document._catalog && this._document._catalog._catalogDictionary.has('OCProperties') && this._isLayerContainsResource) {
var _ocDictionary = this._document._catalog._catalogDictionary.get('OCProperties');
if (_ocDictionary && _ocDictionary.has('OCGs')) {
var ocgsList = _ocDictionary.get('OCGs');
if (ocgsList && ocGroups) {
isPresent = true;
for (var _i = 0, ocGroups_1 = ocGroups; _i < ocGroups_1.length; _i++) {
var obj = ocGroups_1[_i];
if (ocgsList.indexOf(obj) === -1) {
ocgsList.push(obj);
}
}
}
if (_ocDictionary.has('D')) {
var defaultView = _ocDictionary.get('D');
if (defaultView) {
var on = void 0;
var off = void 0;
var _usage = void 0;
if (!defaultView.has('Order')) {
defaultView.update('Order', this._document._order);
}
if (defaultView.has('OFF')) {
off = defaultView.get('OFF');
}
if (defaultView.has('ON')) {
on = defaultView.get('ON');
}
if (defaultView.has('AS')) {
_usage = defaultView.get('AS');
}
if (_usage) {
_usage.forEach(function (item) {
var usageDictionary;
var value = item;
if (value instanceof pdf_primitives_1._PdfReference) {
usageDictionary = _this._crossReference._fetch(value);
if (usageDictionary && usageDictionary instanceof pdf_primitives_1._PdfDictionary) {
var usageOcGroup = usageDictionary.get('OCGs');
if (usageOcGroup && ocGroups && usageOcGroup.indexOf(layer._referenceHolder) === -1) {
usageOcGroup.push(layer._referenceHolder);
}
}
}
});
}
if (layer.visible) {
if (on && ocGroups && on.indexOf(layer._referenceHolder) === -1) {
on.push(layer._referenceHolder);
}
}
else if (off && ocGroups && off.indexOf(layer._referenceHolder) === -1) {
off.push(layer._referenceHolder);
}
}
}
this._document._catalog._catalogDictionary._updated = true;
}
}
if (!isPresent) {
ocProperties.update('OCGs', ocGroups);
ocProperties.update('D', this._createOptionalContentViews());
this._catalog._catalogDictionary.update('OCProperties', ocProperties);
}
this._crossReference._allowCatalog = true;
};
PdfLayerCollection.prototype._createOptionalContentDictionary = function (layer) {
var _dictionary = new pdf_primitives_1._PdfDictionary(this._crossReference);
var reference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(reference, _dictionary);
_dictionary.update('Name', layer.name);
_dictionary.update('Type', new pdf_primitives_1._PdfName('OCG'));
_dictionary.update('LayerID', new pdf_primitives_1._PdfName(layer._layerId));
_dictionary.update('Visible', layer.visible);
if (layer.printState === enumerator_1.PdfPrintState.alwaysPrint ||
layer.printState === enumerator_1.PdfPrintState.neverPrint ||
layer.printState === enumerator_1.PdfPrintState.printWhenVisible) {
var UsageReference = this._setPrintOption(layer);
_dictionary.update('Usage', UsageReference);
this._document._printLayer.push(reference);
}
this._document._optionalContentDictionaries.push(reference);
layer._dictionary = _dictionary;
layer._referenceHolder = reference;
var ocProperties = this._document._catalog._catalogDictionary.get('OCProperties');
this._createSublayer(ocProperties, reference, layer);
if (layer.visible) {
this._document._on.push(reference);
}
else {
this._document._off.push(reference);
}
this._isLayerContainsResource = true;
return this._document._optionalContentDictionaries;
};
PdfLayerCollection.prototype._createOptionalContentViews = function () {
var _optionalContent = new pdf_primitives_1._PdfDictionary();
_optionalContent.update('Name', 'Layers');
_optionalContent.update('Order', this._document._order);
_optionalContent.update('ON', this._document._on);
_optionalContent.update('OFF', this._document._off);
var category = [];
category.push(new pdf_primitives_1._PdfName('Print'));
var _usageApplication = new pdf_primitives_1._PdfDictionary();
_usageApplication.update('Category', category);
_usageApplication.update('OCGs', this._document._printLayer);
_usageApplication.update('Event', new pdf_primitives_1._PdfName('Print'));
var reference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(reference, _usageApplication);
this._document._as.push(reference);
_optionalContent.update('AS', this._document._as);
return _optionalContent;
};
PdfLayerCollection.prototype._setPrintOption = function (layer) {
var _usage = new pdf_primitives_1._PdfDictionary();
var _print = new pdf_primitives_1._PdfDictionary();
var usageReference = this._crossReference._getNextReference();
var printReference = this._crossReference._getNextReference();
this._crossReference._cacheMap.set(usageReference, _usage);
this._crossReference._cacheMap.set(printReference, _print);
_print.update('Subtype', new pdf_primitives_1._PdfName('Print'));
if (layer.printState === enumerator_1.PdfPrintState.neverPrint) {
_print.update('PrintState', new pdf_primitives_1._PdfName('OFF'));
}
else if (layer.printState === enumerator_1.PdfPrintState.alwaysPrint) {
_print.update('PrintState', new pdf_primitives_1._PdfName('ON'));
}
layer._usage = _usage;
layer._printOption = _print;
_usage.update('Print', printReference);
return usageReference;
};
PdfLayerCollection.prototype._createSublayer = function (ocProperties, reference, layer) {
if (!this._subLayer) {
if (ocProperties) {
var order = void 0;
var defaultview = ocProperties.get('D');
if (defaultview) {
order = defaultview.get('Order');
}
if (order) {
this._document._order = order;
}
this._document._order.push(reference);
}
else {
this._document._order.push(reference);
}
}
else {
layer._parent = this._parent;
if (ocProperties) {
var order = void 0;
var defaultview = ocProperties.get('D');
if (defaultview) {
order = defaultview.get('Order');
}
if (this._document._order && order) {
this._document._order = order;
}
}
if (this._parent._child.length === 0) {
this._parent._subLayer.push(reference);
}
else if (this._document._order.indexOf(this._parent._referenceHolder) !== -1) {
var position = this._document._order.indexOf(this._parent._referenceHolder);
this._document._order.splice(position + 1, 1);
this._parent._subLayer.push(reference);
}
else {
this._parent._subLayer.push(reference);
}
if (this._document._order.indexOf(this._parent._referenceHolder) !== -1) {
var position = this._document._order.indexOf(this._parent._referenceHolder);
this._document._order.splice(position + 1, 0, this._parent._subLayer);
}
else {
if (this._parent._parent) {
if (this._parent._parent._subLayer.indexOf(this._parent._referenceHolder) !== -1) {
var position = this._parent._parent._subLayer.indexOf(this._parent._referenceHolder);
if (this._parent._subLayer.length === 1) {
this._parent._parent._subLayer.splice(position + 1, 0, this._parent._subLayer);
}
if (this._document._order.indexOf(this._parent._parent._referenceHolder) !== -1) {
var position_1 = this._document._order.indexOf(this._parent._parent._referenceHolder);
this._document._order.splice(position_1 + 1, 1);
this._document._order.splice(position_1 + 1, 0, this._parent._parent._subLayer);
}
}
}
else {
if (document instanceof pdf_document_1.PdfDocument) {
for (var i = 0; i < document._order.length; i++) {
if (Array.isArray(document._order[i])) {
var value = document._order[i];
var orderArray = value;
if (orderArray.indexOf(this._parent._referenceHolder) !== -1) {
var position = orderArray.indexOf(this._parent._referenceHolder);
if (this._parent._subLayer.length === 1) {
orderArray.splice(position + 1, 0, this._parent._subLayer);
break;
}
}
}
}
}
}
}
if (this._parent._child.indexOf(layer) === -1) {
this._parent._child.push(layer);
}
if (this._parent._parentLayer.length === 0) {
layer._parentLayer.push(this._parent);
}
else {
this._parent._parentLayer.forEach(function (element) {
if (layer._parentLayer.indexOf(element) === -1) {
layer._parentLayer.push(element);
}
});
if (layer._parentLayer.indexOf(this._parent) === -1) {
layer._parentLayer.push(this._parent);
}
}
}
};
PdfLayerCollection.prototype._checkLayerLock = function (ocProperties) {
var _this = this;
var locked;
var defaultView = ocProperties.get('D');
if (defaultView && defaultView.has('Locked')) {
locked = defaultView.get('Locked');
}
if (locked) {
locked.forEach(function (referenceHolder) {
if (referenceHolder && referenceHolder instanceof pdf_primitives_1._PdfReference) {
var pdfLayer = _this._layerDictionary.get(referenceHolder);
if (pdfLayer) {
pdfLayer.locked = true;
}
}
});
}
};
PdfLayerCollection.prototype._checkLayerVisible = function (ocProperties) {
var _this = this;
var _document = this._document;
var visible;
if (_document._catalog && _document._catalog._catalogDictionary.has('OCProperties')) {
var defaultView = ocProperties.get('D');
if (defaultView && defaultView.has('OFF')) {
visible = defaultView.get('OFF');
}
if (visible) {
visible.forEach(function (visibleReference) {
if (visibleReference instanceof pdf_primitives_1._PdfReference) {
var layerDictionary = _this._layerDictionary;
if (layerDictionary && layerDictionary.size > 0 && visibleReference && layerDictionary.has(visibleReference)) {
var pdfLayer = layerDictionary.get(visibleReference);
if (pdfLayer && pdfLayer._visible) {
pdfLayer._visible = false;
if (pdfLayer._dictionary) {
pdfLayer._dictionary.set('Visible', false);
}
}
}
}
});
}
}
};
PdfLayerCollection.prototype._checkParentLayer = function (ocProperties) {
var defaultView = ocProperties.get('D');
if (defaultView) {
var array = defaultView.get('Order');
if (array) {
this._parsingLayerOrder(null, array, this._layerDictionary);
}
}
};
PdfLayerCollection.prototype._parsingLayerOrder = function (parent, array, layerDictionary) {
var reference;
var layer;
for (var i = 0; i < array.length; i++) {
reference = array[i];
if (reference instanceof pdf_primitives_1._PdfReference) {
if (layerDictionary.has(reference)) {
layer = layerDictionary.get(reference);
}
if (layer) {
if (parent) {
if (parent._child.indexOf(layer) === -1) {
parent._child.push(layer);
}
if (parent._parentLayer.length === 0) {
layer._parentLayer.push(parent);
layer._parent = parent;
}
else {
parent._parentLayer.forEach(function (parentLayerElement) {
if (layer._parentLayer.indexOf(parentLayerElement) === -1) {
if (!(parentLayerElement instanceof layer_1.PdfLayer) && parentLayerElement) {
layer._parentLayer.push(parentLayerElement);
}
}
});
layer._parentLayer.push(parent);
layer._parent = parent;
}
}
if (array.length > i + 1 && (Array.isArray(array[i + 1]))) {
i++;
var pdfArray = array[i];
layer._subLayer = pdfArray;
this._parsingLayerOrder(layer, pdfArray, layerDictionary);
}
}
}
else if (Array.isArray(array[i])) {
var value = array[i];
var subArray = value;
if (!subArray || subArray.length === 0) {
return;
}
if (typeof subArray[0] === 'string') {
parent = null;
this._parsingLayerOrder(parent, subArray, layerDictionary);
}
else {
parent = null;
this._parsingLayerOrder(parent, array[i], layerDictionary);
}
}
}
};
PdfLayerCollection.prototype._createLayerHierarchical = function (ocProperties) {
var _this = this;
var defaultView = ocProperties.get('D');
if (defaultView && defaultView.has('Order')) {
if (this._layerDictionary && this._layerDictionary.size > 0) {
this._list.splice(0, this._list.length);
this._layerDictionary.forEach(function (_layer, _pdfReference) {
if (_layer instanceof layer_1.PdfLayer && _pdfReference instanceof pdf_primitives_1._PdfReference) {
if (!_layer._parent && _this._list.indexOf(_layer) === -1) {
_this._list.push(_layer);
}
else if (_layer._child.length > 0) {
_this._addChildLayer(_layer._parent);
}
else if (_layer._parent && _layer._child.length === 0 && !_layer._parent.layers.contains(_layer)) {
_layer._parent.layers._addNestedLayer(_layer);
}
}
});
}
}
};
PdfLayerCollection.prototype._addChildLayer = function (layer) {
layer._child.forEach(function (child) {
if (layer.layers.indexOf(child) === -1) {
layer.layers._addNestedLayer(child);
}
});
};
PdfLayerCollection.prototype._addNestedLayer = function (layer) {
this._list.push(layer);
var index = this._list.length - 1;
layer._layer = layer;
return index;
};
PdfLayerCollection.prototype._removeLayer = function (layer, removeGraphicalContent) {
var _dictionary;
if (layer && this._document) {
_dictionary = this._document._catalog._catalogDictionary;
if (_dictionary && _dictionary.has('OCProperties')) {
var ocProperties = _dictionary.get('OCProperties');
if (ocProperties) {
var ocGroup = ocProperties.get('OCGs');
if (ocGroup) {
this._removeOCG(layer, ocGroup);
}
if (ocProperties.has('D')) {
var defaultView = ocProperties.get('D');
if (defaultView) {
var on = void 0;
var off = void 0;
if (defaultView.has('Order')) {
var order = defaultView.get('Order');
if (order) {
var arrayList = [];
this._removeOrder(layer, order, arrayList);
}
}
if (defaultView.has('Locked')) {
var locked = defaultView.get('Locked');
if (locked) {
this._removeLocked(layer, locked);
}
}
if (defaultView.has('OFF')) {
off = defaultView.get('OFF');
}
if (defaultView.has('ON')) {
on = defaultView.get('ON');
}
if (defaultView.has('AS')) {
var _usage = defaultView.get('AS');
if (_usage) {
this._removeUsage(layer, _usage);
}
}
this._removeVisible(layer, on, off);
}
defaultView._updated = true;
}
var page = layer._layerPage;
if (page) {
var resource = layer._layerPage._pageDictionary.get('Resources');
if (resource.has('Properties')) {
var properties = resource.get('Properties');
if (properties && properties.has(layer._layerId)) {
delete properties._map[layer._layerId];
}
}
}
var chacheMap = this._crossReference._cacheMap;
if (chacheMap.has(layer._referenceHolder)) {
var dictionary = this._crossReference._cacheMap.get(layer._referenceHolder);
if (dictionary) {
var usage = dictionary.getRaw('Usage');
if (usage instanceof pdf_primitives_1._PdfReference) {
dictionary = this._crossReference._cacheMap.get(usage);
if (dictionary) {
var printReference = dictionary.getRaw('Print');
chacheMap.delete(layer._referenceHolder);
chacheMap.delete(usage);
chacheMap.delete(printReference);
}
}
}
}
ocProperties._updated = true;
_dictionary._updated = true;
}
this._crossReference._allowCatalog = true;
}
if (removeGraphicalContent) {
this._removeLayerContent(layer);
}
}
};
PdfLayerCollection.prototype._removeOCG = function (layer, ocGroup) {
if (ocGroup && ocGroup.indexOf(layer._referenceHolder) !== -1) {
ocGroup.splice(ocGroup.indexOf(layer._referenceHolder), 1);
}
};
PdfLayerCollection.prototype._removeUsage = function (layer, _usage) {
if (_usage) {
var isRemoved = false;
for (var i = 0; i < _usage.length; i++) {
var usage = _usage[i];
if (usage) {
var usageDictionary = void 0;
if (usage instanceof pdf_primitives_1._PdfReference) {
usageDictionary = this._crossReference._fetch(_usage[i]);
}
if (usage instanceof pdf_primitives_1._PdfDictionary) {
usageDictionary = usage;
}
if (usageDictionary) {
var usageOcGroup = usageDictionary.get('OCGs');
if (usageOcGroup) {
if (usageOcGroup.indexOf(layer._referenceHolder) !== -1) {
usageOcGroup.splice(usageOcGroup.indexOf(layer._referenceHolder), 1);
isRemoved = true;
}
if (isRemoved) {
break;
}
}
}
}
}
}
};
PdfLayerCollection.prototype._removeOrder = function (layer, order, arrayList) {
var isRemoveOrder = false;
if (order) {
for (var i = 0; i < order.length; i++) {
var entry = order[i];
if (entry && entry instanceof pdf_primitives_1._PdfReference && entry === layer._referenceHolder) {
if (i !== order.length - 1) {
if (Array.isArray(order[i + 1])) {
order.splice(i, 2);
isRemoveOrder = true;
break;
}
else {
order.splice(i, 1);
isRemoveOrder = true;
break;
}
}
else {
order.splice(i, 1);
isRemoveOrder = true;
break;
}
}
else if (Array.isArray(entry)) {
arrayList.push(order[i]);
}
}
}
if (!isRemoveOrder && arrayList) {
for (var i = 0; i < arrayList.length; i++) {
order = arrayList[i];
arrayList.splice(i, 1);
i -= 1;
this._removeOrder(layer, order, arrayList);
}
}
};
PdfLayerCollection.prototype._removeVisible = function (layer, on, off) {
if (layer.visible) {
if (on && on.indexOf(layer._referenceHolder) !== -1) {
var index = on.indexOf(layer._referenceHolder);
if (index > -1) {
on.splice(index, 1);
}
}
}
else {
if (off && off.indexOf(layer._referenceHolder) !== -1) {
var index = off.indexOf(layer._referenceHolder);
if (index > -1) {
off.splice(index, 1);
}
}
}
};
PdfLayerCollection.prototype._removeLocked = function (layer, locked) {
if (locked && locked.indexOf(layer._referenceHolder) !== -1) {
locked.splice(locked.indexOf(layer._referenceHolder), 1);
}
};
PdfLayerCollection.prototype._removeLayerContent = function (layer) {
var isSkip = false;
var _properties;
var _xObject;
if (layer._layerPage) {
for (var i = 0; i < layer._pages.length; i++) {
var _resource = layer._pages[i]._pageDictionary.get('Resources');
if (_resource) {
_properties = _resource.get('Properties');
_xObject = _resource.get('XObject');
if (_properties && layer._layerId.trim().length > 0 && _properties.has(layer._layerId)) {
delete _properties._map[layer._layerId];
}
if (_xObject && layer._xObject.length > 0) {
var map = _xObject._map;
for (var _i = 0, map_1 = map; _i < map_1.length; _i++) {
var key = map_1[_i];
if (layer._xObject.indexOf(map[String(key)]) !== -1) {
delete _xObject._map[String(key)];
break;
}
}
if (layer._xObject.indexOf(layer._layerId) !== -1) {
layer._xObject.splice(layer._xObject.indexOf(layer._layerId), 1);
}
}
}
var content = layer._pages[i]._pageDictionary.getArray('Contents');
for (var m = 0; m < content.length; m++) {
var data = new base_stream_1._PdfContentStream([]);
var stream = content[m];
var objID = stream.dictionary.objId;
var bytes = stream.getBytes();
var parser = void 0;
if (stream instanceof base_stream_1._PdfContentStream) {
parser = new content_parser_1._ContentParser(stream._bytes);
}
else {
parser = new content_parser_1._ContentParser(bytes);
}
var result = parser._readContent();
for (var j = 0; j < result.length; j++) {
var entry = result[j];
var _operator = entry._operator;
if (_operator === 'BMC' || _operator === 'EMC' || _operator === 'BDC') {
var operands = entry._operands;
this._processBeginMarkContent(layer, _operator, operands, data, objID);
isSkip = true;
}
if (_operator === 'Do' && layer._xObject.indexOf(entry._operands[0]) !== -1) {
isSkip = true;
}
if (_operator === 'q' || _operator === 'Q' ||
_operator === 'w' || _operator === 'J' || _operator === 'j' ||
_operator === 'M' || _operator === 'd' || _operator === 'ri' ||
_operator === 'i' || _operator === 'gs' || _operator === 'g' ||
_operator === 'cm' || _operator === 'G' || _operator === 'rg' ||
_operator === 'RG' || _operator === 'k' || _operator === 'K' ||
_operator === 'cs' || _operator === 'CS' || _operator === 'scn' ||
_operator === 'SCN' || _operator === 'sc' || _operator === 'SC') {
if (!isSkip) {
this._streamWrite(entry._operands, _operator, false, data);
}
isSkip = false;
}
else {
if (!isSkip) {
this._streamWrite(entry._operands, _operator, true, data);
}
isSkip = false;
}
}
if (data.length > 0 && !objID) {
var _pages = layer._pages[i];
var _reference = _pages._contents[m];
var contentStream = this._crossReference._fetch(_reference);
contentStream._bytes.length = 0;
contentStream.write(data.getString());
}
}
layer._pages[i]._pageDictionary._updated = true;
}
}
};
PdfLayerCollection.prototype._processBeginMarkContent = function (parser, operator, operands, data, id) {
if (operator === 'BDC') {
var operand = void 0;
if (operands.length > 1 && operands[0].substring(1) === 'OC') {
operand = operands[1].substring(1);
}
if (this._bdcCount > 0) {
this._bdcCount++;
return;
}
if (operand && operand === parser._layerId) {
this._bdcCount++;
var refArray = parser._pages[0]._pageDictionary.getRaw('Contents');
if (id) {
var strParts = id.split(' ');
var index = refArray.indexOf(pdf_primitives_1._PdfReference.get(Number(strParts[0]), Number(strParts[1])));
parser._pages[0]._pageDictionary.getRaw('Contents').splice(index, 1);
}
}
}
this._streamWrite(operands, operator, true, data);
if ('EMC' === operator && this._bdcCount > 0) {
this._bdcCount--;
}
};
PdfLayerCollection.prototype._streamWrite = function (operands, operator, skip, data) {
var pdfString;
if (skip && this._isSkip) {
return;
}
if (operands) {
for (var _i = 0, operands_1 = operands; _i < operands_1.length; _i++) {
var operand = operands_1[_i];
pdfString = operand;
data.write(pdfString);
data.write(' ');
}
}
pdfString = operator;
data.write(pdfString);
data.write('\r\n');
};
PdfLayerCollection.prototype._insertLayer = function (index, layer) {
var reference = layer._referenceHolder;
if (this._document) {
var catalog = this._document._catalog._catalogDictionary;
if (catalog.has('OCProperties')) {
var ocDictionary = catalog.get('OCProperties');
if (ocDictionary) {
var ocGroups = ocDictionary.get('OCGs');
if (ocDictionary.has('D')) {
var defaultView = ocDictionary.get('D');
if (defaultView) {
var order = defaultView.get('Order');
if (order && ocGroups && order.indexOf(reference) !== -1 && index < order.length) {
if (order[index] instanceof pdf_primitives_1._PdfReference) {
if (index + 1 < order.length && index + 2 < order.length) {
var first = index + 1;
var second = index + 2;
if (order[first] instanceof pdf_primitives_1._PdfReference
&& order[second] instanceof pdf_primitives_1._PdfReference) {
var position = order.indexOf(reference);
order.splice(position, 1);
order.splice(index, 0, reference);
if (ocGroups.indexOf(reference) !== -1) {
var position_2 = ocGroups.indexOf(reference);
ocGroups.splice(position_2, 1);
ocGroups.splice(index, 0, reference);
}
}
}
}
}
}
}
}
}
}
};
return PdfLayerCollection;
}());
exports.PdfLayerCollection = PdfLayerCollection;
});
|