-
Notifications
You must be signed in to change notification settings - Fork 106
/
CubicVR.PDF.js
163 lines (135 loc) · 4.38 KB
/
CubicVR.PDF.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
CubicVR.RegisterModule("PDF", function (base) {
var undef = base.undef;
var GLCore = base.GLCore;
var enums = CubicVR.enums;
// Variant of pdf.js's getPdf
function getPdf(arg, callback) {
var params = arg;
if (typeof arg === 'string')
params = { url: arg };
//#if !B2G
var xhr = new XMLHttpRequest();
//#else
//var xhr = new XMLHttpRequest({mozSystem: true});
//#endif
xhr.open('GET', params.url);
var headers = params.headers;
if (headers) {
for (var property in headers) {
if (typeof headers[property] === 'undefined')
continue;
xhr.setRequestHeader(property, params.headers[property]);
}
}
xhr.mozResponseType = xhr.responseType = 'arraybuffer';
var protocol = params.url.substring(0, params.url.indexOf(':') + 1);
//XXXsecretrobotron: Need to interject here. Protocol could be '', but still need 200 status to continue
xhr.expected = (['http:', 'https:', ''].indexOf(protocol) > -1) ? 200 : 0;
if ('progress' in params)
xhr.onprogress = params.progress || undefined;
var calledErrorBack = false;
if ('error' in params) {
xhr.onerror = function errorBack() {
if (!calledErrorBack) {
calledErrorBack = true;
params.error();
}
};
}
xhr.onreadystatechange = function getPdfOnreadystatechange(e) {
if (xhr.readyState === 4) {
if (xhr.status === xhr.expected) {
var data = (xhr.mozResponseArrayBuffer || xhr.mozResponse ||
xhr.responseArrayBuffer || xhr.response);
callback(data);
} else if (params.error && !calledErrorBack) {
calledErrorBack = true;
params.error(e);
}
}
};
xhr.send(null);
}
function PDF(options) {
if (!options.src) {
throw("PDF Error: you must specify a src url for a PDF.");
}
var src = options.src,
width = options.width || null,
height = options.height || null,
callback = options.callback || function() {},
pdf,
pages = [],
thumbnails = [];
/**
* Number of pages, or 0 if not loaded yet.
*/
this.__defineGetter__('pages', function() {
return pdf ? pdf.numPages : 0;
});
this.getPage = function(n) {
// // Need a better solution here...
// if (!pages.length) {
// console.log("PDF Error: pdf not loaded yet...");
// return;
// }
var pageCount = pdf.numPages;
// Normalize n
n = n < 1 ? 1 : n;
n = n > pageCount ? pageCount : n;
n = n - 1;
return pages[n];
};
/**
* Get a PdfTexture for the given page. The texture is either
* page.width x page.height or width x height (width and height
* are optional).
*/
this.getPageTexture = function(n, width, height) {
var page = this.getPage(n);
var viewport = page.getViewport(1);
width = width || viewport.width;
height = height || viewport.height;
return new CubicVR.PdfTexture(page, {width: width, height: height, viewport: viewport});
};
var pdfParams = {
url: src,
progress: function(e){
},
error: function(e) {
console.log('PDF Error: error loading pdf `' + src + '`');
}
};
getPdf(pdfParams, function successCallback(data) {
PDFJS.getDocument({
data: data
}).then(
function(doc){
pdf = doc;
var i = 0;
// get pages in order
function getNextPage() {
if ( i++ >= doc.numPages ) {
callback();
}
else {
doc.getPage(i).then(function(page){
pages.push(page);
thumbnails.push(page);
getNextPage();
});
}
}
getNextPage();
},
function(msg, e){
console.warn(msg, e);
callback();
});
});
}
var extend = {
PDF: PDF
};
return extend;
});