diff options
Diffstat (limited to 'point/data/pdf.js')
-rw-r--r-- | point/data/pdf.js | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/point/data/pdf.js b/point/data/pdf.js new file mode 100644 index 0000000..f789bdf --- /dev/null +++ b/point/data/pdf.js | |||
@@ -0,0 +1,130 @@ | |||
1 | /* | ||
2 | * This file is part of "What's The Point" <https://github.com/Pacien/WhatsThePoint> | ||
3 | * Copyright (C) 2014 Pacien TRAN-GIRARD | ||
4 | * | ||
5 | * "What's The Point" is free software: you can redistribute it and/or modify | ||
6 | * it under the terms of the GNU Affero General Public License as | ||
7 | * published by the Free Software Foundation, either version 3 of the | ||
8 | * License, or (at your option) any later version. | ||
9 | * | ||
10 | * "What's The Point" is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | requirejs.config({ | ||
20 | shim: { | ||
21 | "pdfjs": { | ||
22 | exports: "PDFJS", | ||
23 | }, | ||
24 | }, | ||
25 | paths: { | ||
26 | "pdfjs": "libs/pdfjs/pdf", | ||
27 | }, | ||
28 | }); | ||
29 | |||
30 | define(["pdfjs"], function (pdfjs) { | ||
31 | |||
32 | var pdf = { | ||
33 | |||
34 | init: function (settings) { | ||
35 | this.fileUrl = settings.data.file; | ||
36 | this.width = settings.dimension.width; | ||
37 | this.height = settings.dimension.height; | ||
38 | |||
39 | this.slides = []; | ||
40 | |||
41 | pdfjs.disableRange = true; | ||
42 | pdfjs.workerSrc = "point/libs/pdfjs/pdf.worker.js"; | ||
43 | }, | ||
44 | |||
45 | makeCanvas: function () { | ||
46 | var canvas = document.createElement("canvas"); | ||
47 | canvas.width = pdf.width; | ||
48 | canvas.height = pdf.height; | ||
49 | return canvas; | ||
50 | }, | ||
51 | |||
52 | clearOutline: function (context, slideWidth, slideHeight, offsetX, offsetY) { | ||
53 | context.clearRect(0, 0, offsetX, pdf.height); | ||
54 | context.clearRect(offsetX + slideWidth, 0, offsetX, pdf.height); | ||
55 | context.clearRect(0, 0, pdf.width, offsetY); | ||
56 | context.clearRect(0, offsetY + slideHeight, pdf.width, offsetY); | ||
57 | return context; | ||
58 | }, | ||
59 | |||
60 | renderSlide: function (pdfDocument, slideIndex, callback) { | ||
61 | pdfDocument.getPage(slideIndex).then(function (pdfPage) { | ||
62 | |||
63 | var viewport = pdfPage.getViewport(1); | ||
64 | var slideWidth = viewport.width / 2; | ||
65 | var slideHeight = viewport.height; | ||
66 | |||
67 | var widthRatio = pdf.width / slideWidth; | ||
68 | var heightRatio = pdf.height / slideHeight; | ||
69 | var scale = Math.min(widthRatio, heightRatio); | ||
70 | slideWidth *= scale; | ||
71 | slideHeight *= scale; | ||
72 | |||
73 | var offsetX = (pdf.width - slideWidth) / 2; | ||
74 | var offsetY = (pdf.height - slideHeight) / 2; | ||
75 | |||
76 | var contentViewport = new pdfjs.PageViewport(pdfPage.view, scale, 0, offsetX, offsetY); | ||
77 | var notesViewport = new pdfjs.PageViewport(pdfPage.view, scale, 0, -slideWidth + offsetX, offsetY); | ||
78 | |||
79 | var contentCanvas = pdf.makeCanvas(); | ||
80 | var contentContext = contentCanvas.getContext("2d"); | ||
81 | |||
82 | var notesCanvas = pdf.makeCanvas(); | ||
83 | var notesContext = notesCanvas.getContext("2d"); | ||
84 | |||
85 | var contentRenderContext = { | ||
86 | canvasContext: contentContext, | ||
87 | viewport: contentViewport, | ||
88 | }; | ||
89 | |||
90 | var notesRenderContext = { | ||
91 | canvasContext: notesContext, | ||
92 | viewport: notesViewport, | ||
93 | }; | ||
94 | |||
95 | pdfPage.render(contentRenderContext).then(function () { | ||
96 | pdf.clearOutline(contentContext, slideWidth, slideHeight, offsetX, offsetY); | ||
97 | |||
98 | pdfPage.render(notesRenderContext).then(function () { | ||
99 | pdf.clearOutline(notesContext, slideWidth, slideHeight, offsetX, offsetY); | ||
100 | |||
101 | pdf.registerSlide(contentCanvas, notesCanvas, pdfDocument, slideIndex, callback); | ||
102 | |||
103 | }); | ||
104 | }); | ||
105 | }); | ||
106 | }, | ||
107 | |||
108 | registerSlide: function (contentCanvas, notesCanvas, pdfDocument, slideIndex, callback) { | ||
109 | pdf.slides[slideIndex - 1] = { | ||
110 | content: contentCanvas, | ||
111 | notes: notesCanvas, | ||
112 | }; | ||
113 | |||
114 | if (slideIndex < pdfDocument.pdfInfo.numPages) { | ||
115 | pdf.renderSlide(pdfDocument, slideIndex + 1, callback); | ||
116 | } else { | ||
117 | callback(pdf.slides); | ||
118 | } | ||
119 | }, | ||
120 | |||
121 | parse: function (rawData, callback) { | ||
122 | pdfjs.getDocument(require.toUrl(this.fileUrl)).then(function (pdfDocument) { | ||
123 | pdf.renderSlide(pdfDocument, 1, callback); | ||
124 | }); | ||
125 | }, | ||
126 | }; | ||
127 | |||
128 | return pdf; | ||
129 | |||
130 | }); | ||