-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpdfjs.js
8560 lines (7008 loc) · 357 KB
/
pdfjs.js
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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.not_webix_amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.pdfjs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
'use strict'
var BaseElement = module.exports = function(Node) {
this.Node = Node
}
BaseElement.prototype.createNode = function() {
var node = new this.Node(this)
return node
}
BaseElement.prototype.clone = function() {
return this.createNode()
}
},{}],2:[function(require,module,exports){
'use strict'
var BoxStyle = require('../style/box')
var Box = module.exports = function(style, opts) {
Box.super_.call(this, require('../pdf/node/box'))
this.style = new BoxStyle(style, opts)
}
require('../pdf/utils').inherits(Box, require('./container'))
Box.prototype.clone = function() {
var clone = new Box()
clone.style = this.style
clone.children = this.children.map(function(child) {
return child.clone()
})
return clone
}
},{"../pdf/node/box":22,"../pdf/utils":60,"../style/box":63,"./container":4}],3:[function(require,module,exports){
'use strict'
var TableStyle = require('../style/table')
var Cell = module.exports = function(str, style) {
Cell.super_.call(this, require('../pdf/node/cell'))
this.style = new TableStyle(style)
if (str !== undefined && str !== null) {
this.text(str, this.style)
}
}
require('../pdf/utils').inherits(Cell, require('./container'))
var Table = require('./table')
Cell.prototype.table = function(opts) {
var table = new Table(this.style.merge(TableStyle.reset), opts)
this.children.push(table)
return table
}
Cell.prototype.clone = function() {
var clone = new Cell()
clone.style = this.style
clone.children = this.children.map(function(child) {
return child.clone()
})
return clone
}
},{"../pdf/node/cell":23,"../pdf/utils":60,"../style/table":67,"./container":4,"./table":14}],4:[function(require,module,exports){
'use strict'
var ContainerStyle = require('../style/container')
var Container = module.exports = function(Node) {
Container.super_.call(this, Node)
this.children = []
}
require('../pdf/utils').inherits(Container, require('./base'))
var Text = require('./text')
Container.prototype.text = function(text, opts) {
if (text && typeof text === 'object') {
opts = text
text = undefined
}
var child = new Text(text, this.style.merge(opts))
this.children.push(child)
return child
}
var Box = require('./box')
Container.prototype.box = function(opts) {
var box = new Box(this.style.merge(ContainerStyle.reset), opts)
this.children.push(box)
return box
}
var Table = require('./table')
Container.prototype.table = function(opts) {
var table = new Table(this.style.merge(ContainerStyle.reset), opts)
this.children.push(table)
return table
}
var Image = require('./image')
Container.prototype.image = function(img, opts) {
var image = new Image(img, opts)
this.children.push(image)
return image
}
var Operations = require('./operations')
Container.prototype.ops = function() {
var ops = new Operations()
this.children.push(ops)
return ops
}
var ForceBreak = require('./forcebreak')
Container.prototype.pageBreak = function() {
this.children.push(new ForceBreak())
return this
}
},{"../pdf/utils":60,"../style/container":64,"./base":1,"./box":2,"./forcebreak":7,"./image":8,"./operations":10,"./table":14,"./text":15}],5:[function(require,module,exports){
'use strict'
var DocumentStyle = require('../style/document')
var PDF = require('../pdf')
var Document = module.exports = function(opts) {
Document.super_.call(this, require('../pdf/node/document'))
opts = opts || {}
opts.width = opts.width || 595.296
opts.height = opts.height || 841.896
this.style = new DocumentStyle(opts)
this._header = null
this._footer = null
}
require('../pdf/utils').inherits(Document, require('./container'))
var Box = require('./box')
Document.prototype.header = function(opts) {
if (this._header) {
return this._header
}
if (opts && !opts.width) {
opts.width = '100%'
}
this._header = new Box(this.style.merge(DocumentStyle.super_.reset), opts)
return this._header
}
Document.prototype.footer = function(opts) {
if (this._footer) {
return this._footer
}
if (opts && !opts.width) {
opts.width = '100%'
}
this._footer = new Box(this.style.merge(DocumentStyle.super_.reset), opts)
return this._footer
}
Document.prototype.render = function() {
return new PDF(this.createNode(this))
}
},{"../pdf":20,"../pdf/node/document":25,"../pdf/utils":60,"../style/document":65,"./box":2,"./container":4}],6:[function(require,module,exports){
'use strict'
var TTFFont = require('ttfjs')
var utils = require('../../pdf/utils')
var uuid = require('node-uuid')
var Font = module.exports = function(src) {
this.uuid = uuid.v4()
this.ttf = new TTFFont(utils.toArrayBuffer(src))
}
Font.prototype.subset = function() {
return this.ttf.subset({ remap: true, trimNames: true })
}
Font.prototype.stringWidth = function(string, size) {
return this.ttf.stringWidth(string, size)
}
Font.prototype.lineHeight = function(size, includeGap) {
return this.ttf.lineHeight(size, includeGap)
}
Font.prototype.lineDescent = function(size) {
return this.ttf.lineDescent(size)
}
},{"../../pdf/utils":60,"node-uuid":80,"ttfjs":93}],7:[function(require,module,exports){
'use strict'
var ForceBreak = module.exports = function() {
ForceBreak.super_.call(this, require('../pdf/node/forcebreak'))
}
require('../pdf/utils').inherits(ForceBreak, require('./base'))
},{"../pdf/node/forcebreak":26,"../pdf/utils":60,"./base":1}],8:[function(require,module,exports){
'use strict'
var ImageStyle = require('../style/image')
var Image = module.exports = function(img, style, opts) {
this.uuid = img.uuid
this.img = img
Image.super_.call(this, require('../pdf/node/image'))
this.style = new ImageStyle(style, opts)
}
require('../pdf/utils').inherits(Image, require('./base'))
Image.prototype.clone = function() {
var clone = new Image(this.img)
clone.style = this.style
return clone
}
},{"../pdf/node/image":27,"../pdf/utils":60,"../style/image":66,"./base":1}],9:[function(require,module,exports){
'use strict'
var LineBreak = module.exports = function(style) {
LineBreak.super_.call(this, require('../pdf/node/linebreak'))
this.style = style
}
require('../pdf/utils').inherits(LineBreak, require('./base'))
Object.defineProperties(LineBreak.prototype, {
width: {
enumerable: true,
get: function() {
return 0
}
},
height: {
enumerable: true,
get: function() {
var height = this.style.font.lineHeight(this.style.fontSize, true) * this.style.lineHeight
return height
}
},
spacing: {
enumerable: true,
get: function() {
return 0
}
}
})
LineBreak.prototype.clone = function() {
return new LineBreak(this.style)
}
},{"../pdf/node/linebreak":28,"../pdf/utils":60,"./base":1}],10:[function(require,module,exports){
'use strict'
var Operations = module.exports = function() {
Operations.super_.call(this, require('../pdf/node/operations'))
this.ops = []
}
require('../pdf/utils').inherits(Operations, require('./base'))
Operations.prototype.op = function(fn) {
if (fn && typeof fn === 'function') {
this.ops.push(fn)
} else {
this.ops.push(Array.prototype.slice.call(arguments))
}
}
Operations.prototype.clone = function() {
var clone = new Operations
clone.ops = this.ops.slice()
return clone
}
},{"../pdf/node/operations":29,"../pdf/utils":60,"./base":1}],11:[function(require,module,exports){
'use strict'
var Word = require('./word')
var PageCount = module.exports = function(style) {
Word.call(this, '0', style)
Word.super_.call(this, require('../pdf/node/page-count'))
}
require('../pdf/utils').inherits(PageCount, Word)
PageCount.prototype.clone = function() {
return this
}
},{"../pdf/node/page-count":30,"../pdf/utils":60,"./word":16}],12:[function(require,module,exports){
'use strict'
var Word = require('./word')
var PageNumber = module.exports = function(style) {
Word.call(this, '0', style)
Word.super_.call(this, require('../pdf/node/page-number'))
}
require('../pdf/utils').inherits(PageNumber, Word)
PageNumber.prototype.clone = function() {
var clone = new PageNumber(this.style)
clone.style = this.style
clone.children = [clone].concat(this.children.slice(1).map(function(child) {
return child.clone()
}))
return clone
}
},{"../pdf/node/page-number":31,"../pdf/utils":60,"./word":16}],13:[function(require,module,exports){
'use strict'
var TableStyle = require('../style/table')
var Row = module.exports = function(style) {
Row.super_.call(this, require('../pdf/node/row'))
this.style = new TableStyle(style)
this.children = []
}
require('../pdf/utils').inherits(Row, require('./base'))
var Cell = require('./cell')
Row.prototype.td = function(text, opts) {
if (text && typeof text === 'object') {
opts = text
text = undefined
}
var td = new Cell(text, this.style.merge(opts))
this.children.push(td)
return td
}
Row.prototype.clone = function() {
var clone = new Row()
clone.style = this.style
clone.children = this.children.map(function(child) {
return child.clone()
})
return clone
}
},{"../pdf/node/row":33,"../pdf/utils":60,"../style/table":67,"./base":1,"./cell":3}],14:[function(require,module,exports){
'use strict'
var TableStyle = require('../style/table')
var Table = module.exports = function(style, opts) {
Table.super_.call(this, require('../pdf/node/table'))
this.style = new TableStyle(style, opts)
this.children = []
this.beforeBreakChildren = []
}
require('../pdf/utils').inherits(Table, require('./base'))
var Row = require('./row')
Table.prototype.tr = function(opts) {
var tr = new Row(this.style.merge(TableStyle.reset, opts))
this.children.push(tr)
return tr
}
Table.prototype.beforeBreak = function(fnOrOpts, opts) {
if (typeof fnOrOpts === 'function') {
fnOrOpts.opts = this.style.merge(TableStyle.reset, opts)
this.beforeBreakChildren.push(fnOrOpts)
} else {
var tr = new Row(this.style.merge(TableStyle.reset, fnOrOpts))
this.beforeBreakChildren.push(tr)
return tr
}
}
Table.prototype.clone = function() {
var clone = new Table()
clone.style = this.style
clone.children = this.children.map(function(child) {
return child.clone()
})
clone.beforeBreakChildren = this.beforeBreakChildren.map(function(child) {
return child.clone()
})
return clone
}
},{"../pdf/node/table":35,"../pdf/utils":60,"../style/table":67,"./base":1,"./row":13}],15:[function(require,module,exports){
'use strict'
var TextStyle = require('../style/text')
var Text = module.exports = function(text, style, opts) {
Text.super_.call(this, require('../pdf/node/text'))
this.style = new TextStyle(style, opts)
this.children = []
if (text !== undefined && text !== null) {
this.add(text)
}
}
require('../pdf/utils').inherits(Text, require('./base'))
var LineBreaker = require('linebreak')
var Word = require('./word')
var LineBreak = require('./linebreak')
var PageNumber = require('./page-number')
var PageCount = require('./page-count')
Text.prototype.add = function(str, opts, append) {
var style = this.style.merge(opts)
var lastChild = this.children[this.children.length - 1]
var appendTo = append && lastChild && lastChild.children ? lastChild : this
if (str instanceof Word) {
appendTo.children.push(str)
return this
}
str = String(str)
var breaker = new LineBreaker(str)
var last = 0, bk
while ((bk = breaker.nextBreak())) {
// get the string between the last break and this one
var word = str.slice(last, bk.position)
var linebreaks = 0
while (!bk.required && word.match(/(\r\n|\n|\r)$/)) {
word = word.replace(/(\r\n|\n|\r)$/, '')
linebreaks++
}
// separate words, if has whitespace,is add the end of the string
// or ends with a whitespace
if (linebreaks > 0 || bk.position === str.length || str[bk.position - 1].match(/\s/)) {
last = bk.position
} else {
continue
}
// remove trailing whitespaces if white-space style is set to normal
if (style.whiteSpace === 'normal') {
word = word.replace(/^\s+/, '').replace(/\s+$/, '')
}
// remove newline characters
if (bk.required) {
word = word.replace(/(\r\n|\n|\r)/, '')
}
if (word.length) {
appendTo.children.push(new Word(word, style))
}
appendTo = this
// add linebreak
if (bk.required) {
this.children.push(new LineBreak(style))
}
// add trailing line breaks
for (var i = 0; i < linebreaks; ++i) {
this.children.push(new LineBreak(style))
}
}
return this
}
Text.prototype.line = function(str, opts) {
return this.add(str + '\n', opts)
}
Text.prototype.append = function(str, opts) {
return this.add(str, opts, true)
}
Text.prototype.br = function() {
this.children.push(new LineBreak(this.style))
return this
}
Text.prototype.pageNumber = function(opts, append) {
return this.add(new PageNumber(this.style.merge(opts)), opts, append)
}
Text.prototype.appendPageNumber = function(opts) {
return this.pageNumber(opts, true)
}
Text.prototype.pageCount = function(opts, append) {
return this.add(new PageCount(this.style.merge(opts)), opts, append)
}
Text.prototype.appendPageCount = function(opts) {
return this.pageCount(opts, true)
}
Text.prototype.clone = function() {
var clone = new Text()
clone.style = this.style
clone.children = this.children.map(function(child) {
return child.clone()
})
return clone
}
},{"../pdf/node/text":36,"../pdf/utils":60,"../style/text":68,"./base":1,"./linebreak":9,"./page-count":11,"./page-number":12,"./word":16,"linebreak":78}],16:[function(require,module,exports){
'use strict'
var unorm = require('unorm')
var Word = module.exports = function(word, style) {
Word.super_.call(this, require('../pdf/node/word'))
if (!style.font) {
throw new TypeError('Text must have a font set or inherited')
}
this.word = unorm.nfc(word)
this.style = style
this.children = [this]
}
require('../pdf/utils').inherits(Word, require('./base'))
Object.defineProperties(Word.prototype, {
width: {
enumerable: true,
get: function() {
var width = this.children.map(function(word) {
return this.style.font.stringWidth(word.word, word.style.fontSize)
}, this).reduce(function(lhs, rhs) {
return lhs + rhs
}, 0)
return width
}
},
height: {
enumerable: true,
get: function() {
var height = Math.max.apply(Math, this.children.map(function(word) {
return word.style.font.lineHeight(word.style.fontSize, true) * word.style.lineHeight
}, this))
return height
}
},
spacing: {
enumerable: true,
get: function() {
var last = this.children[this.children.length - 1]
var spacing = last.style.font.stringWidth(' ', last.style.fontSize)
return spacing
}
}
})
Word.prototype.clone = function() {
var clone = new Word(this.word, this.style)
clone.style = this.style
clone.children = [clone].concat(this.children.slice(1).map(function(child) {
return child.clone()
}))
return clone
}
Word.prototype.toString = function() {
return this.children.map(function(word) {
return word.word
}, this).reduce(function(lhs, rhs) {
return lhs + rhs
}, '')
}
},{"../pdf/node/word":37,"../pdf/utils":60,"./base":1,"unorm":102}],17:[function(require,module,exports){
'use strict'
var utils = require('./pdf/utils')
var uuid = require('node-uuid')
module.exports = function(src) {
this.uuid = uuid.v4()
this.src = utils.toArrayBuffer(src)
this.type = parseType(this.src)
switch (this.type) {
case 'jpeg':
this.info = parseJpegInfo(this.src)
this.width = this.info.width
this.height = this.info.height
switch (this.info.colorSpace) {
case 3:
this.colorSpace = 'DeviceRGB'
break
case 1:
this.colorSpace = 'DeviceGRAY'
break
default:
break
}
break
case 'pdf':
this.info = parsePDFInfo(this.src)
break
}
}
function parseType(buffer) {
var pdf = String.fromCharCode.apply(null, new Uint8Array(buffer, 0, 5))
if (pdf === '%PDF-') {
return 'pdf'
}
var view = new DataView(buffer)
if (view.getUint8(0) === 0xff || view.getUint8(1) === 0xd8) {
return 'jpeg'
}
throw new TypeError('Unsupported image type')
}
function parseJpegInfo(buffer) {
var view = new DataView(buffer)
if (view.getUint8(0) !== 0xff || view.getUint8(1) !== 0xd8) {
throw new Error('Invalid JPEG image.')
}
var blockLength = view.getUint8(4) * 256 + view.getUint8(5)
var i = 4, len = view.byteLength
while ( i < len ) {
i += blockLength
if (view.getUint8(i) !== 0xff) {
throw new Error('Could not read JPEG the image size')
}
if (
view.getUint8(i + 1) === 0xc0 || //(SOF) Huffman - Baseline DCT
view.getUint8(i + 1) === 0xc1 || //(SOF) Huffman - Extended sequential DCT
view.getUint8(i + 1) === 0xc2 || // Progressive DCT (SOF2)
view.getUint8(i + 1) === 0xc3 || // Spatial (sequential) lossless (SOF3)
view.getUint8(i + 1) === 0xc4 || // Differential sequential DCT (SOF5)
view.getUint8(i + 1) === 0xc5 || // Differential progressive DCT (SOF6)
view.getUint8(i + 1) === 0xc6 || // Differential spatial (SOF7)
view.getUint8(i + 1) === 0xc7
) {
return {
height: view.getUint8(i + 5) * 256 + view.getUint8(i + 6),
width: view.getUint8(i + 7) * 256 + view.getUint8(i + 8),
colorSpace: view.getUint8(i + 9)
}
} else {
i += 2
blockLength = view.getUint8(i) * 256 + view.getUint8(i + 1)
}
}
}
var Parser = require('./pdf/parser/document')
function parsePDFInfo(buffer) {
var parser = new Parser(buffer)
parser.parse()
var catalog = parser.trailer.get('Root').object.properties
var pages = catalog.get('Pages').object.properties
var first = pages.get('Kids')[0].object.properties
var mediaBox = first.get('MediaBox') || pages.get('MediaBox')
return {
page: first,
width: mediaBox[2],
height: mediaBox[3]
}
}
},{"./pdf/parser/document":57,"./pdf/utils":60,"node-uuid":80}],18:[function(require,module,exports){
'use strict'
var Document = exports.Document = require('./element/document')
exports.createDocument = function(style) {
return new Document(style)
}
var TTFFont = exports.TTFFont = require('./element/font/ttf')
exports.createTTFFont = function(src) {
return new TTFFont(src)
}
var Image = exports.Image = require('./image')
exports.createImage = function(src) {
return new Image(src)
}
exports.Parser = require('./pdf/parser/document')
var isClient = typeof window !== 'undefined' && !!window.document
// trick browserify
var fs = (require)('fs')
exports.load = function(path, callback) {
if (isClient) {
var xhr = new XMLHttpRequest()
xhr.open('GET', path, true)
xhr.responseType = 'arraybuffer'
if (xhr.overrideMimeType) {
xhr.overrideMimeType('text/plain; charset=x-user-defined')
} else {
xhr.setRequestHeader('Accept-Charset', 'x-user-defined')
}
xhr.onload = function() {
if (xhr.status === 200) {
callback(null, xhr.response)
} else {
callback(new Error(xhr.statusText), null)
}
}
xhr.send(null)
} else {
fs.readFile(path, callback)
}
}
},{"./element/document":5,"./element/font/ttf":6,"./image":17,"./pdf/parser/document":57,"fs":70}],19:[function(require,module,exports){
'use strict'
var Page = function(nr, header, footer, afterBreak) {
this.nr = nr
this.header = header
this.footer = footer
this.afterBreak = afterBreak
this.top = this.bottom = 0
}
Page.prototype.setup = function(cursor) {
var style = cursor.style
var top = this.top = cursor.y = style.height - style.paddingTop
cursor.y -= cursor.offset || 0
var y = cursor.y
var x = cursor.x
cursor.x = style.paddingLeft
var shift = style.paddingTop
if (this.header) {
this.header.compute(cursor)
this.top -= this.header.height
shift += this.header.height
}
if (this.afterBreak && this.afterBreak.length) {
this.afterBreak.forEach(function(node) {
cursor.x = node.x.val
node.compute(cursor)
this.top -= node.height
shift += node.height
}, this)
}
if (this.nr > 1) {
if (this.header) {
this.header.shift(shift * -1)
}
if (this.afterBreak && this.afterBreak.length) {
this.afterBreak.forEach(function(node) {
node.shift(shift * -1)
}, this)
}
}
this.bottom = style.paddingBottom
if (this.footer) {
cursor.y = y
cursor.x = style.paddingLeft
this.footer.compute(cursor)
this.bottom += this.footer.height
var offset = top - this.bottom
if (this.nr > 1) {
offset += shift * -1
}
this.footer.shift(offset)
}
cursor.x = x
}
var CursorFactory = module.exports = function(doc) {
this.doc = doc
this.style = doc.style
this.header = doc.doc._header
this.footer = doc.doc._footer
this.afterBreak = []
this.innerWidth = this.style.width - this.style.paddingLeft - this.style.paddingRight
this.currentPage = 1
this.pages = []
this.pageBreaks = []
this.addPage(doc)
this.nextPageBreakId = 1
this.validBreaks = Object.create(null)
this.reset()
this.force = true
this.x = this.style.paddingLeft
this.y = this.top
}
Object.defineProperties(CursorFactory.prototype, {
top: {
enumerable: true,
get: function() {
return this.pages[this.currentPage - 1].top
}
},
bottom: {
enumerable: true,
get: function() { return this.pages[this.currentPage - 1].bottom }
}
})
CursorFactory.prototype.create = function(w, h) {
if (!w) {
w = this.innerWidth
}
var cutAt = h ? this.y - h : undefined
var self = this
return {
get style() {
return self.style
},
get width() {
return w
},
get x() {
return self.x
},
set x(val) {
self.x = val
},
get y() {
return self.y
},
set y(val) {
self.y = val
},
get bottom() {
return self.bottom
},
get right() {
return self.style.width - self.style.paddingRight
},
get pageHeight() {
return self.top - self.bottom
},
get force() {
return self.force
},
set force(val) {
self.force = val
},
get currentPage() {
return self.currentPage
},
get pageCount() {
return self.pages.length
},
get offset() {
return self.offset
},
set offset(val) {
self.offset = val
},
setPage: function(page) {
if (page < 1) {
return 1
}
return self.currentPage = page
},
mustBreak: function(node) {
if (node.type === 'ForceBreakNode' && !node.valid) {
node.valid = false
return true
}
if (node.allowBreak) {
return false
}
var offset = self.offset
var mustBreak = (node.y.val - node.height) + offset < self.bottom
if (!mustBreak) {
return false
}
if (node.height > this.pageHeight) {
return false
}
return true
},
isVisible: function(node) {
return !cutAt || node.y.val - node.height >= cutAt
},
create: function(width, height) {
return self.create(width || w, height)
},
reset: this.reset.bind(this),
pageBreak: this.pageBreak.bind(this),
applyBreak: this.applyBreak.bind(this),
beforeContent: function(node) {
if (node.afterBreak) {
self.afterBreak.push(node.afterBreak)
}
var cursor = node.beforeContent(this)
return cursor
},
afterContent: function(node) {
if (node.afterBreak) {
self.afterBreak.pop()
}
node.afterContent(this)
}
}
}
CursorFactory.prototype.reset = function() {
this.force = false
this.y = this.style.height
this.offset = 0
this.currentPage = 1
this.visitedBreaks = Object.create(null)
this.highestVisitedBreak = 0