Refactoring text layer builder

This commit is contained in:
Tim van der Meij 2014-06-21 22:53:26 +02:00
parent 456d219f2a
commit 2a052c4aad

View File

@ -19,25 +19,22 @@
var FIND_SCROLL_OFFSET_TOP = -50; var FIND_SCROLL_OFFSET_TOP = -50;
var FIND_SCROLL_OFFSET_LEFT = -400; var FIND_SCROLL_OFFSET_LEFT = -400;
var MAX_TEXT_DIVS_TO_RENDER = 100000;
var RENDER_DELAY = 200; // ms
/** /**
* TextLayerBuilder provides text-selection * TextLayerBuilder provides text-selection functionality for the PDF.
* functionality for the PDF. It does this * It does this by creating overlay divs over the PDF text. These divs
* by creating overlay divs over the PDF * contain text that matches the PDF text they are overlaying. This object
* text. This divs contain text that matches * also provides a way to highlight text that is being searched for.
* the PDF text they are overlaying. This
* object also provides for a way to highlight
* text that is being searched for.
*/ */
var TextLayerBuilder = function textLayerBuilder(options) { var TextLayerBuilder = function textLayerBuilder(options) {
var textLayerFrag = document.createDocumentFragment();
this.textLayerDiv = options.textLayerDiv; this.textLayerDiv = options.textLayerDiv;
this.layoutDone = false; this.layoutDone = false;
this.divContentDone = false; this.divContentDone = false;
this.pageIdx = options.pageIndex; this.pageIdx = options.pageIndex;
this.matches = []; this.matches = [];
this.lastScrollSource = options.lastScrollSource; this.lastScrollSource = options.lastScrollSource || null;
this.viewport = options.viewport; this.viewport = options.viewport;
this.isViewerInPresentationMode = options.isViewerInPresentationMode; this.isViewerInPresentationMode = options.isViewerInPresentationMode;
this.textDivs = []; this.textDivs = [];
@ -46,31 +43,27 @@ var TextLayerBuilder = function textLayerBuilder(options) {
window.PDFFindController = null; window.PDFFindController = null;
} }
if (typeof this.lastScrollSource === 'undefined') { this.renderLayer = function textLayerBuilder_renderLayer() {
this.lastScrollSource = null; var textLayerFrag = document.createDocumentFragment();
}
this.renderLayer = function textLayerBuilderRenderLayer() {
var textDivs = this.textDivs; var textDivs = this.textDivs;
var textDivsLength = textDivs.length;
var canvas = document.createElement('canvas'); var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
// No point in rendering so many divs as it'd make the browser unusable // No point in rendering many divs as it would make the browser
// even after the divs are rendered // unusable even after the divs are rendered.
var MAX_TEXT_DIVS_TO_RENDER = 100000; if (textDivsLength > MAX_TEXT_DIVS_TO_RENDER) {
if (textDivs.length > MAX_TEXT_DIVS_TO_RENDER) {
return; return;
} }
for (var i = 0, ii = textDivs.length; i < ii; i++) { for (var i = 0; i < textDivsLength; i++) {
var textDiv = textDivs[i]; var textDiv = textDivs[i];
if ('isWhitespace' in textDiv.dataset) { if (textDiv.dataset.isWhitespace !== undefined) {
continue; continue;
} }
ctx.font = textDiv.style.fontSize + ' ' + textDiv.style.fontFamily; ctx.font = textDiv.style.fontSize + ' ' + textDiv.style.fontFamily;
var width = ctx.measureText(textDiv.textContent).width; var width = ctx.measureText(textDiv.textContent).width;
if (width > 0) { if (width > 0) {
textLayerFrag.appendChild(textDiv); textLayerFrag.appendChild(textDiv);
var textScale = textDiv.dataset.canvasWidth / width; var textScale = textDiv.dataset.canvasWidth / width;
@ -87,19 +80,17 @@ var TextLayerBuilder = function textLayerBuilder(options) {
this.updateMatches(); this.updateMatches();
}; };
this.setupRenderLayoutTimer = function textLayerSetupRenderLayoutTimer() { this.setupRenderLayoutTimer =
// Schedule renderLayout() if user has been scrolling, otherwise function textLayerBuilder_setupRenderLayoutTimer() {
// run it right away // Schedule renderLayout() if the user has been scrolling,
var RENDER_DELAY = 200; // in ms // otherwise run it right away.
var self = this; var self = this;
var lastScroll = (this.lastScrollSource === null ? var lastScroll = (this.lastScrollSource === null ?
0 : this.lastScrollSource.lastScroll); 0 : this.lastScrollSource.lastScroll);
if (Date.now() - lastScroll > RENDER_DELAY) { if (Date.now() - lastScroll > RENDER_DELAY) { // Render right away
// Render right away
this.renderLayer(); this.renderLayer();
} else { } else { // Schedule
// Schedule
if (this.renderTimer) { if (this.renderTimer) {
clearTimeout(this.renderTimer); clearTimeout(this.renderTimer);
} }
@ -109,7 +100,7 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} }
}; };
this.appendText = function textLayerBuilderAppendText(geom, styles) { this.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);
@ -140,35 +131,31 @@ 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 textLayerBuilderSetTextContent(textContent) { this.setTextContent = function textLayerBuilder_setTextContent(textContent) {
this.textContent = textContent; this.textContent = textContent;
var textItems = textContent.items; var textItems = textContent.items;
for (var i = 0; i < textItems.length; i++) { for (var i = 0, len = textItems.length; i < len; i++) {
this.appendText(textItems[i], textContent.styles); this.appendText(textItems[i], textContent.styles);
} }
this.divContentDone = true; this.divContentDone = true;
this.setupRenderLayoutTimer(); this.setupRenderLayoutTimer();
}; };
this.convertMatches = function textLayerBuilderConvertMatches(matches) { this.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;
var end = bidiTexts.length - 1; var end = bidiTexts.length - 1;
var queryLen = (PDFFindController === null ? var queryLen = (PDFFindController === null ?
0 : PDFFindController.state.query.length); 0 : PDFFindController.state.query.length);
var ret = []; var ret = [];
// Loop over all the matches. for (var m = 0, len = matches.length; m < len; m++) {
for (var m = 0; m < matches.length; m++) { // Calculate the start position.
var matchIdx = matches[m]; var matchIdx = matches[m];
// # Calculate the begin position.
// Loop over the divIdxs. // Loop over the divIdxs.
while (i !== end && matchIdx >= (iIndex + bidiTexts[i].str.length)) { while (i !== end && matchIdx >= (iIndex + bidiTexts[i].str.length)) {
@ -176,9 +163,8 @@ var TextLayerBuilder = function textLayerBuilder(options) {
i++; i++;
} }
// TODO: Do proper handling here if something goes wrong. if (i === bidiTexts.length) {
if (i == bidiTexts.length) { console.error('Could not find a matching mapping');
console.error('Could not find matching mapping');
} }
var match = { var match = {
@ -188,11 +174,11 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} }
}; };
// # Calculate the end position. // Calculate the end position.
matchIdx += queryLen; matchIdx += queryLen;
// Somewhat same array as above, but use a > instead of >= to get the end // Somewhat the same array as above, but use > instead of >= to get
// position right. // the end position right.
while (i !== end && matchIdx > (iIndex + bidiTexts[i].str.length)) { while (i !== end && matchIdx > (iIndex + bidiTexts[i].str.length)) {
iIndex += bidiTexts[i].str.length; iIndex += bidiTexts[i].str.length;
i++; i++;
@ -219,32 +205,23 @@ var TextLayerBuilder = function textLayerBuilder(options) {
var prevEnd = null; var prevEnd = null;
var isSelectedPage = (PDFFindController === null ? var isSelectedPage = (PDFFindController === null ?
false : (this.pageIdx === PDFFindController.selected.pageIdx)); false : (this.pageIdx === PDFFindController.selected.pageIdx));
var selectedMatchIdx = (PDFFindController === null ? var selectedMatchIdx = (PDFFindController === null ?
-1 : PDFFindController.selected.matchIdx); -1 : PDFFindController.selected.matchIdx);
var highlightAll = (PDFFindController === null ? var highlightAll = (PDFFindController === null ?
false : PDFFindController.state.highlightAll); false : PDFFindController.state.highlightAll);
var infinity = {
var infty = {
divIdx: -1, divIdx: -1,
offset: undefined offset: undefined
}; };
function beginText(begin, className) { function beginText(begin, className) {
var divIdx = begin.divIdx; var divIdx = begin.divIdx;
var div = textDivs[divIdx]; textDivs[divIdx].textContent = '';
div.textContent = '';
appendTextToDiv(divIdx, 0, begin.offset, className); appendTextToDiv(divIdx, 0, begin.offset, className);
} }
function appendText(from, to, className) {
appendTextToDiv(from.divIdx, from.offset, to.offset, className);
}
function appendTextToDiv(divIdx, fromOffset, toOffset, className) { function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
var div = textDivs[divIdx]; var div = textDivs[divIdx];
var content = bidiTexts[divIdx].str.substring(fromOffset, toOffset); var content = bidiTexts[divIdx].str.substring(fromOffset, toOffset);
var node = document.createTextNode(content); var node = document.createTextNode(content);
if (className) { if (className) {
@ -257,12 +234,7 @@ var TextLayerBuilder = function textLayerBuilder(options) {
div.appendChild(node); div.appendChild(node);
} }
function highlightDiv(divIdx, className) { var i0 = selectedMatchIdx, i1 = i0 + 1;
textDivs[divIdx].className = className;
}
var i0 = selectedMatchIdx, i1 = i0 + 1, i;
if (highlightAll) { if (highlightAll) {
i0 = 0; i0 = 0;
i1 = matches.length; i1 = matches.length;
@ -271,36 +243,39 @@ var TextLayerBuilder = function textLayerBuilder(options) {
return; return;
} }
for (i = i0; i < i1; i++) { for (var i = i0; i < i1; i++) {
var match = matches[i]; var match = matches[i];
var begin = match.begin; var begin = match.begin;
var end = match.end; var end = match.end;
var isSelected = (isSelectedPage && i === selectedMatchIdx);
var isSelected = isSelectedPage && i === selectedMatchIdx;
var highlightSuffix = (isSelected ? ' selected' : ''); var highlightSuffix = (isSelected ? ' selected' : '');
if (isSelected && !this.isViewerInPresentationMode) { if (isSelected && !this.isViewerInPresentationMode) {
scrollIntoView(textDivs[begin.divIdx], { top: FIND_SCROLL_OFFSET_TOP, scrollIntoView(textDivs[begin.divIdx],
{ top: FIND_SCROLL_OFFSET_TOP,
left: FIND_SCROLL_OFFSET_LEFT }); left: FIND_SCROLL_OFFSET_LEFT });
} }
// Match inside new div. // Match inside new div.
if (!prevEnd || begin.divIdx !== prevEnd.divIdx) { if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
// If there was a previous div, then add the text at the end // If there was a previous div, then add the text at the end.
if (prevEnd !== null) { if (prevEnd !== null) {
appendText(prevEnd, infty); appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
} }
// clears the divs and set the content until the begin point. // Clear the divs and set the content until the starting point.
beginText(begin); beginText(begin);
} else { } else {
appendText(prevEnd, begin); appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
} }
if (begin.divIdx === end.divIdx) { if (begin.divIdx === end.divIdx) {
appendText(begin, end, 'highlight' + highlightSuffix); appendTextToDiv(begin.divIdx, begin.offset, end.offset,
'highlight' + highlightSuffix);
} else { } else {
appendText(begin, infty, 'highlight begin' + highlightSuffix); appendTextToDiv(begin.divIdx, begin.offset, infinity.offset,
for (var n = begin.divIdx + 1; n < end.divIdx; n++) { 'highlight begin' + highlightSuffix);
highlightDiv(n, 'highlight middle' + highlightSuffix); for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
textDivs[n0].className = 'highlight middle' + highlightSuffix;
} }
beginText(end, 'highlight end' + highlightSuffix); beginText(end, 'highlight end' + highlightSuffix);
} }
@ -308,27 +283,27 @@ var TextLayerBuilder = function textLayerBuilder(options) {
} }
if (prevEnd) { if (prevEnd) {
appendText(prevEnd, infty); appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
} }
}; };
this.updateMatches = function textLayerUpdateMatches() { this.updateMatches = function textLayerBuilder_updateMatches() {
// Only show matches, once all rendering is done. // Only show matches when all rendering is done.
if (!this.renderingDone) { if (!this.renderingDone) {
return; return;
} }
// Clear out all matches. // Clear all matches.
var matches = this.matches; var matches = this.matches;
var textDivs = this.textDivs; var textDivs = this.textDivs;
var bidiTexts = this.textContent.items; var bidiTexts = this.textContent.items;
var clearedUntilDivIdx = -1; var clearedUntilDivIdx = -1;
// Clear out all current matches. // Clear all current matches.
for (var i = 0; i < matches.length; i++) { for (var i = 0, len = matches.length; i < len; i++) {
var match = matches[i]; var match = matches[i];
var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx); var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
for (var n = begin; n <= match.end.divIdx; n++) { for (var n = begin, end = match.end.divIdx; n <= end; n++) {
var div = textDivs[n]; var div = textDivs[n];
div.textContent = bidiTexts[n].str; div.textContent = bidiTexts[n].str;
div.className = ''; div.className = '';
@ -340,11 +315,10 @@ var TextLayerBuilder = function textLayerBuilder(options) {
return; return;
} }
// Convert the matches on the page controller into the match format used // Convert the matches on the page controller into the match format
// for the textLayer. // used for the textLayer.
this.matches = 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);
}; };
}; };