|
12 | 12 | ; |
13 | 13 | (function(jsPDFAPI) { |
14 | 14 | 'use strict'; |
15 | | - |
16 | | - jsPDFAPI.initOutlinePlugin = function() { |
17 | | - |
18 | | - this.outline = {}; |
19 | | - this.outline.root = { |
20 | | - children : [] |
21 | | - }; |
22 | | - |
23 | | - this.internal.events.subscribe('postPutResources', function() { |
24 | | - if (this.outline.root.children.length > 0) { |
25 | | - var lines = this.outline.render().split(/\r\n/); |
26 | | - for (var int = 0; int < lines.length; int++) { |
27 | | - var line = lines[int]; |
28 | | - if (line.endsWith('obj')){ |
29 | | - var oid = line.split(' ')[0]; |
30 | | - this.internal.newObjectDeferredBegin(oid); |
| 15 | + var outline = { |
| 16 | + |
| 17 | + onInitialize : function(pdf) { |
| 18 | + |
| 19 | + this.installOutlinePlugin(pdf); |
| 20 | + |
| 21 | + pdf.internal.events.subscribe('postPutResources', function() { |
| 22 | + |
| 23 | + var rx = /^(\d+) 0 obj$/; |
| 24 | + |
| 25 | + if (this.outline.root.children.length > 0) { |
| 26 | + var lines = pdf.outline.render().split(/\r\n/); |
| 27 | + for (var i = 0; i < lines.length; i++) { |
| 28 | + var line = lines[i]; |
| 29 | + var m = rx.exec(line); |
| 30 | + if (m != null) { |
| 31 | + var oid = m[1]; |
| 32 | + pdf.internal.newObjectDeferredBegin(oid); |
| 33 | + } |
| 34 | + pdf.internal.write(line); |
31 | 35 | } |
32 | | - this.internal.write(line); |
33 | 36 | } |
34 | | - } |
35 | | - }); |
| 37 | + }); |
| 38 | + |
| 39 | + pdf.internal.events.subscribe('putCatalog', function() { |
| 40 | + if (pdf.outline.root.children.length > 0) { |
| 41 | + pdf.internal.write("/Outlines", this.outline.makeRef(this.outline.root)); |
| 42 | + } |
| 43 | + }); |
| 44 | + }, |
| 45 | + |
| 46 | + installOutlinePlugin : function(pdf) { |
36 | 47 |
|
37 | | - this.internal.events.subscribe('putCatalog', function() { |
38 | | - if (this.outline.root.children.length > 0) { |
39 | | - this.internal.write("/Outlines", this.outline.makeRef(this.outline.root)); |
40 | | - } |
41 | | - }); |
42 | | - |
43 | | - /** |
44 | | - * Options: pageNumber |
45 | | - */ |
46 | | - this.outline.add = function(parent,title,options) { |
47 | | - var item = { |
48 | | - title : title, |
49 | | - options : options, |
| 48 | + pdf.outline = {}; |
| 49 | + pdf.outline.root = { |
50 | 50 | children : [] |
51 | 51 | }; |
52 | | - if (parent == null) { |
53 | | - parent = this.root; |
| 52 | + |
| 53 | + /** |
| 54 | + * Options: pageNumber |
| 55 | + */ |
| 56 | + pdf.outline.add = function(parent,title,options) { |
| 57 | + var item = { |
| 58 | + title : title, |
| 59 | + options : options, |
| 60 | + children : [] |
| 61 | + }; |
| 62 | + if (parent == null) { |
| 63 | + parent = this.root; |
| 64 | + } |
| 65 | + parent.children.push(item); |
| 66 | + return item; |
54 | 67 | } |
55 | | - parent.children.push(item); |
56 | | - return item; |
57 | | - } |
58 | 68 |
|
59 | | - this.outline.render = function() { |
60 | | - this.outline.ctx = {}; |
61 | | - this.outline.nextId = 1000; |
62 | | - this.outline.ctx.val = ''; |
63 | | - this.outline.ctx.pdf = this; |
| 69 | + pdf.outline.render = function() { |
| 70 | + this.ctx = {}; |
| 71 | + this.ctx.val = ''; |
| 72 | + this.ctx.pdf = pdf; |
64 | 73 |
|
65 | | - this.outline.genIds_r(this.outline.root); |
66 | | - this.outline.renderRoot(this.outline.root); |
67 | | - this.outline.renderItems(this.outline.root); |
| 74 | + this.genIds_r(this.root); |
| 75 | + this.renderRoot(this.root); |
| 76 | + this.renderItems(this.root); |
68 | 77 |
|
69 | | - return this.outline.ctx.val; |
70 | | - }.bind(this); |
| 78 | + return this.ctx.val; |
| 79 | + }; |
71 | 80 |
|
72 | | - this.outline.genIds_r = function(node) { |
73 | | - node.id = this.internal.newObjectDeferred(); |
74 | | - for (var i = 0; i < node.children.length; i++) { |
75 | | - this.outline.genIds_r(node.children[i]); |
76 | | - } |
77 | | - }.bind(this); |
78 | | - |
79 | | - this.outline.renderRoot = function(node) { |
80 | | - this.objStart(node); |
81 | | - this.line('/Type /Outlines'); |
82 | | - if (node.children.length > 0) { |
83 | | - this.line('/First ' + this.makeRef(node.children[0])); |
84 | | - this.line('/Last ' + this.makeRef(node.children[node.children.length - 1])); |
85 | | - } |
86 | | - this.line('/Count ' + this.count_r({ |
87 | | - count : 0 |
88 | | - }, node)); |
89 | | - this.objEnd(); |
90 | | - } |
| 81 | + pdf.outline.genIds_r = function(node) { |
| 82 | + node.id = pdf.internal.newObjectDeferred(); |
| 83 | + for (var i = 0; i < node.children.length; i++) { |
| 84 | + this.genIds_r(node.children[i]); |
| 85 | + } |
| 86 | + }; |
91 | 87 |
|
92 | | - this.outline.renderItems = function(node) { |
93 | | - for (var i = 0; i < node.children.length; i++) { |
94 | | - var item = node.children[i]; |
95 | | - this.objStart(item); |
| 88 | + pdf.outline.renderRoot = function(node) { |
| 89 | + this.objStart(node); |
| 90 | + this.line('/Type /Outlines'); |
| 91 | + if (node.children.length > 0) { |
| 92 | + this.line('/First ' + this.makeRef(node.children[0])); |
| 93 | + this.line('/Last ' + this.makeRef(node.children[node.children.length - 1])); |
| 94 | + } |
| 95 | + this.line('/Count ' + this.count_r({ |
| 96 | + count : 0 |
| 97 | + }, node)); |
| 98 | + this.objEnd(); |
| 99 | + }; |
96 | 100 |
|
97 | | - this.line('/Title ' + this.makeString(item.title)); |
| 101 | + pdf.outline.renderItems = function(node) { |
| 102 | + for (var i = 0; i < node.children.length; i++) { |
| 103 | + var item = node.children[i]; |
| 104 | + this.objStart(item); |
98 | 105 |
|
99 | | - this.line('/Parent ' + this.makeRef(node)); |
100 | | - if (i > 0) { |
101 | | - this.line('/Prev ' + this.makeRef(node.children[i - 1])); |
102 | | - } |
103 | | - if (i < node.children.length - 1) { |
104 | | - this.line('/Next ' + this.makeRef(node.children[i + 1])); |
105 | | - } |
106 | | - if (item.children.length > 0) { |
107 | | - this.line('/First ' + this.makeRef(item.children[0])); |
108 | | - this.line('/Last ' + this.makeRef(item.children[item.children.length - 1])); |
109 | | - } |
| 106 | + this.line('/Title ' + this.makeString(item.title)); |
110 | 107 |
|
111 | | - var count = this.count = this.count_r({ |
112 | | - count : 0 |
113 | | - }, item); |
114 | | - if (count > 0) { |
115 | | - this.line('/Count ' + count); |
116 | | - } |
| 108 | + this.line('/Parent ' + this.makeRef(node)); |
| 109 | + if (i > 0) { |
| 110 | + this.line('/Prev ' + this.makeRef(node.children[i - 1])); |
| 111 | + } |
| 112 | + if (i < node.children.length - 1) { |
| 113 | + this.line('/Next ' + this.makeRef(node.children[i + 1])); |
| 114 | + } |
| 115 | + if (item.children.length > 0) { |
| 116 | + this.line('/First ' + this.makeRef(item.children[0])); |
| 117 | + this.line('/Last ' + this.makeRef(item.children[item.children.length - 1])); |
| 118 | + } |
| 119 | + |
| 120 | + var count = this.count = this.count_r({ |
| 121 | + count : 0 |
| 122 | + }, item); |
| 123 | + if (count > 0) { |
| 124 | + this.line('/Count ' + count); |
| 125 | + } |
117 | 126 |
|
118 | | - if (item.options) { |
119 | | - if (item.options.pageNumber) { |
120 | | - this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /XYZ 0 ' + this.ctx.pdf.internal.pageSize.height + ' 0]'); |
121 | | - //this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /Fit]'); |
| 127 | + if (item.options) { |
| 128 | + if (item.options.pageNumber) { |
| 129 | + this.line('/Dest ' + '[' + (item.options.pageNumber - 1) + ' /XYZ 0 ' + this.ctx.pdf.internal.pageSize.height + ' 0]'); |
| 130 | + // this.line('/Dest ' + '[' + |
| 131 | + // (item.options.pageNumber - |
| 132 | + // 1) + ' /Fit]'); |
| 133 | + } |
122 | 134 | } |
| 135 | + this.objEnd(); |
123 | 136 | } |
124 | | - this.objEnd(); |
125 | | - } |
126 | | - for (var i = 0; i < node.children.length; i++) { |
127 | | - var item = node.children[i]; |
128 | | - this.renderItems(item); |
129 | | - } |
130 | | - } |
| 137 | + for (var i = 0; i < node.children.length; i++) { |
| 138 | + var item = node.children[i]; |
| 139 | + this.renderItems(item); |
| 140 | + } |
| 141 | + }; |
131 | 142 |
|
132 | | - this.outline.line = function(text) { |
133 | | - this.ctx.val += text + '\r\n'; |
134 | | - } |
| 143 | + pdf.outline.line = function(text) { |
| 144 | + this.ctx.val += text + '\r\n'; |
| 145 | + }; |
135 | 146 |
|
136 | | - this.outline.makeRef = function(node) { |
137 | | - return node.id + ' 0 R'; |
138 | | - } |
| 147 | + pdf.outline.makeRef = function(node) { |
| 148 | + return node.id + ' 0 R'; |
| 149 | + }; |
139 | 150 |
|
140 | | - this.outline.makeString = function(val) { |
141 | | - return '(' + this.internal.pdfEscape(val) + ')'; |
142 | | - }.bind(this); |
| 151 | + pdf.outline.makeString = function(val) { |
| 152 | + return '(' + pdf.internal.pdfEscape(val) + ')'; |
| 153 | + }; |
143 | 154 |
|
144 | | - this.outline.objStart = function(node) { |
145 | | - this.ctx.val += '\r\n' + node.id + ' 0 obj' + '\r\n<<\r\n'; |
146 | | - } |
| 155 | + pdf.outline.objStart = function(node) { |
| 156 | + this.ctx.val += '\r\n' + node.id + ' 0 obj' + '\r\n<<\r\n'; |
| 157 | + }; |
147 | 158 |
|
148 | | - this.outline.objEnd = function(node) { |
149 | | - this.ctx.val += '>> \r\n' + 'endobj' + '\r\n'; |
150 | | - } |
| 159 | + pdf.outline.objEnd = function(node) { |
| 160 | + this.ctx.val += '>> \r\n' + 'endobj' + '\r\n'; |
| 161 | + }; |
151 | 162 |
|
152 | | - this.outline.count_r = function(ctx,node) { |
153 | | - for (var i = 0; i < node.children.length; i++) { |
154 | | - ctx.count++; |
155 | | - this.count_r(ctx, node.children[i]); |
156 | | - } |
157 | | - return ctx.count; |
| 163 | + pdf.outline.count_r = function(ctx,node) { |
| 164 | + for (var i = 0; i < node.children.length; i++) { |
| 165 | + ctx.count++; |
| 166 | + this.count_r(ctx, node.children[i]); |
| 167 | + } |
| 168 | + return ctx.count; |
| 169 | + }; |
158 | 170 | } |
159 | 171 | } |
| 172 | + jsPDF.API.outline = outline; |
| 173 | + jsPDF.plugins.register(outline); |
160 | 174 |
|
161 | 175 | return this; |
162 | 176 | })(jsPDF.API); |
0 commit comments