Convert the text layer builder to a class

This commit is contained in:
Tim van der Meij 2014-06-23 22:02:33 +02:00
parent 2a052c4aad
commit a968da8887

View File

@ -28,7 +28,8 @@ var RENDER_DELAY = 200; // ms
* contain text that matches the PDF text they are overlaying. This object * contain text that matches the PDF text they are overlaying. This object
* also provides a way to highlight text that is being searched for. * also provides a way to highlight text that is being searched for.
*/ */
var TextLayerBuilder = function textLayerBuilder(options) { var TextLayerBuilder = (function TextLayerBuilderClosure() {
function TextLayerBuilder(options) {
this.textLayerDiv = options.textLayerDiv; this.textLayerDiv = options.textLayerDiv;
this.layoutDone = false; this.layoutDone = false;
this.divContentDone = false; this.divContentDone = false;
@ -42,8 +43,10 @@ var TextLayerBuilder = function textLayerBuilder(options) {
if (typeof PDFFindController === 'undefined') { if (typeof PDFFindController === 'undefined') {
window.PDFFindController = null; window.PDFFindController = null;
} }
}
this.renderLayer = function textLayerBuilder_renderLayer() { TextLayerBuilder.prototype = {
renderLayer: function TextLayerBuilder_renderLayer() {
var textLayerFrag = document.createDocumentFragment(); var textLayerFrag = document.createDocumentFragment();
var textDivs = this.textDivs; var textDivs = this.textDivs;
var textDivsLength = textDivs.length; var textDivsLength = textDivs.length;
@ -78,10 +81,10 @@ var TextLayerBuilder = function textLayerBuilder(options) {
this.textLayerDiv.appendChild(textLayerFrag); this.textLayerDiv.appendChild(textLayerFrag);
this.renderingDone = true; this.renderingDone = true;
this.updateMatches(); this.updateMatches();
}; },
this.setupRenderLayoutTimer = setupRenderLayoutTimer:
function textLayerBuilder_setupRenderLayoutTimer() { function TextLayerBuilder_setupRenderLayoutTimer() {
// Schedule renderLayout() if the user has been scrolling, // Schedule renderLayout() if the user has been scrolling,
// otherwise run it right away. // otherwise run it right away.
var self = this; var self = this;
@ -98,9 +101,9 @@ var TextLayerBuilder = function textLayerBuilder(options) {
self.setupRenderLayoutTimer(); self.setupRenderLayoutTimer();
}, RENDER_DELAY); }, RENDER_DELAY);
} }
}; },
this.appendText = function textLayerBuilder_appendText(geom, styles) { appendText: function TextLayerBuilder_appendText(geom, styles) {
var style = styles[geom.fontName]; var style = styles[geom.fontName];
var textDiv = document.createElement('div'); var textDiv = document.createElement('div');
this.textDivs.push(textDiv); this.textDivs.push(textDiv);
@ -131,9 +134,9 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} else { } else {
textDiv.dataset.canvasWidth = geom.width * this.viewport.scale; textDiv.dataset.canvasWidth = geom.width * this.viewport.scale;
} }
}; },
this.setTextContent = function textLayerBuilder_setTextContent(textContent) { setTextContent: function TextLayerBuilder_setTextContent(textContent) {
this.textContent = textContent; this.textContent = textContent;
var textItems = textContent.items; var textItems = textContent.items;
@ -142,9 +145,9 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} }
this.divContentDone = true; this.divContentDone = true;
this.setupRenderLayoutTimer(); this.setupRenderLayoutTimer();
}; },
this.convertMatches = function textLayerBuilder_convertMatches(matches) { convertMatches: function TextLayerBuilder_convertMatches(matches) {
var i = 0; var i = 0;
var iIndex = 0; var iIndex = 0;
var bidiTexts = this.textContent.items; var bidiTexts = this.textContent.items;
@ -192,9 +195,9 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} }
return ret; return ret;
}; },
this.renderMatches = function textLayerBuilder_renderMatches(matches) { renderMatches: function TextLayerBuilder_renderMatches(matches) {
// Early exit if there is nothing to render. // Early exit if there is nothing to render.
if (matches.length === 0) { if (matches.length === 0) {
return; return;
@ -285,9 +288,9 @@ var TextLayerBuilder = function textLayerBuilder(options) {
if (prevEnd) { if (prevEnd) {
appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset); appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
} }
}; },
this.updateMatches = function textLayerBuilder_updateMatches() { updateMatches: function TextLayerBuilder_updateMatches() {
// Only show matches when all rendering is done. // Only show matches when all rendering is done.
if (!this.renderingDone) { if (!this.renderingDone) {
return; return;
@ -320,6 +323,8 @@ var TextLayerBuilder = function textLayerBuilder(options) {
this.matches = this.convertMatches(PDFFindController === null ? this.matches = this.convertMatches(PDFFindController === null ?
[] : (PDFFindController.pageMatches[this.pageIdx] || [])); [] : (PDFFindController.pageMatches[this.pageIdx] || []));
this.renderMatches(this.matches); this.renderMatches(this.matches);
}
}; };
}; return TextLayerBuilder;
})();