pdf.js/web/text_layer_builder.js

326 lines
11 KiB
JavaScript
Raw Normal View History

2013-06-19 01:05:55 +09:00
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* globals CustomStyle, PDFFindController, scrollIntoView, PDFJS */
2013-06-19 01:05:55 +09:00
'use strict';
var FIND_SCROLL_OFFSET_TOP = -50;
var FIND_SCROLL_OFFSET_LEFT = -400;
2014-06-22 05:53:26 +09:00
var MAX_TEXT_DIVS_TO_RENDER = 100000;
var RENDER_DELAY = 200; // ms
2013-06-19 01:05:55 +09:00
/**
2014-06-22 05:53:26 +09:00
* TextLayerBuilder provides text-selection functionality for the PDF.
* It does this by creating overlay divs over the PDF text. These divs
* contain text that matches the PDF text they are overlaying. This object
* also provides a way to highlight text that is being searched for.
2013-06-19 01:05:55 +09:00
*/
var TextLayerBuilder = function textLayerBuilder(options) {
this.textLayerDiv = options.textLayerDiv;
this.layoutDone = false;
this.divContentDone = false;
this.pageIdx = options.pageIndex;
this.matches = [];
2014-06-22 05:53:26 +09:00
this.lastScrollSource = options.lastScrollSource || null;
this.viewport = options.viewport;
this.isViewerInPresentationMode = options.isViewerInPresentationMode;
this.textDivs = [];
2013-06-19 01:05:55 +09:00
if (typeof PDFFindController === 'undefined') {
window.PDFFindController = null;
2013-06-19 01:05:55 +09:00
}
2014-06-22 05:53:26 +09:00
this.renderLayer = function textLayerBuilder_renderLayer() {
var textLayerFrag = document.createDocumentFragment();
2013-06-19 01:05:55 +09:00
var textDivs = this.textDivs;
2014-06-22 05:53:26 +09:00
var textDivsLength = textDivs.length;
2013-06-19 01:05:55 +09:00
var canvas = document.createElement('canvas');
2014-04-12 04:07:36 +09:00
var ctx = canvas.getContext('2d');
2013-06-19 01:05:55 +09:00
2014-06-22 05:53:26 +09:00
// No point in rendering many divs as it would make the browser
// unusable even after the divs are rendered.
if (textDivsLength > MAX_TEXT_DIVS_TO_RENDER) {
2013-06-19 01:05:55 +09:00
return;
}
2013-06-19 01:05:55 +09:00
2014-06-22 05:53:26 +09:00
for (var i = 0; i < textDivsLength; i++) {
2013-06-19 01:05:55 +09:00
var textDiv = textDivs[i];
2014-06-22 05:53:26 +09:00
if (textDiv.dataset.isWhitespace !== undefined) {
2013-06-19 01:05:55 +09:00
continue;
}
ctx.font = textDiv.style.fontSize + ' ' + textDiv.style.fontFamily;
var width = ctx.measureText(textDiv.textContent).width;
if (width > 0) {
2013-11-27 04:01:28 +09:00
textLayerFrag.appendChild(textDiv);
2013-06-19 01:05:55 +09:00
var textScale = textDiv.dataset.canvasWidth / width;
2013-06-21 07:03:30 +09:00
var rotation = textDiv.dataset.angle;
2013-06-19 01:05:55 +09:00
var transform = 'scale(' + textScale + ', 1)';
2013-06-21 07:03:30 +09:00
transform = 'rotate(' + rotation + 'deg) ' + transform;
2013-06-19 01:05:55 +09:00
CustomStyle.setProp('transform' , textDiv, transform);
CustomStyle.setProp('transformOrigin' , textDiv, '0% 0%');
}
}
this.textLayerDiv.appendChild(textLayerFrag);
2013-06-19 01:05:55 +09:00
this.renderingDone = true;
this.updateMatches();
};
2014-06-22 05:53:26 +09:00
this.setupRenderLayoutTimer =
function textLayerBuilder_setupRenderLayoutTimer() {
// Schedule renderLayout() if the user has been scrolling,
// otherwise run it right away.
2013-06-19 01:05:55 +09:00
var self = this;
var lastScroll = (this.lastScrollSource === null ?
0 : this.lastScrollSource.lastScroll);
2013-06-19 01:05:55 +09:00
2014-06-22 05:53:26 +09:00
if (Date.now() - lastScroll > RENDER_DELAY) { // Render right away
2013-06-19 01:05:55 +09:00
this.renderLayer();
2014-06-22 05:53:26 +09:00
} else { // Schedule
if (this.renderTimer) {
2013-06-19 01:05:55 +09:00
clearTimeout(this.renderTimer);
}
2013-06-19 01:05:55 +09:00
this.renderTimer = setTimeout(function() {
self.setupRenderLayoutTimer();
}, RENDER_DELAY);
}
};
2014-06-22 05:53:26 +09:00
this.appendText = function textLayerBuilder_appendText(geom, styles) {
var style = styles[geom.fontName];
2013-06-19 01:05:55 +09:00
var textDiv = document.createElement('div');
this.textDivs.push(textDiv);
if (!/\S/.test(geom.str)) {
textDiv.dataset.isWhitespace = true;
return;
}
var tx = PDFJS.Util.transform(this.viewport.transform, geom.transform);
var angle = Math.atan2(tx[1], tx[0]);
if (style.vertical) {
angle += Math.PI / 2;
}
var fontHeight = Math.sqrt((tx[2] * tx[2]) + (tx[3] * tx[3]));
var fontAscent = (style.ascent ? style.ascent * fontHeight :
(style.descent ? (1 + style.descent) * fontHeight : fontHeight));
2013-06-19 01:05:55 +09:00
textDiv.style.position = 'absolute';
textDiv.style.left = (tx[4] + (fontAscent * Math.sin(angle))) + 'px';
textDiv.style.top = (tx[5] - (fontAscent * Math.cos(angle))) + 'px';
2013-06-19 01:05:55 +09:00
textDiv.style.fontSize = fontHeight + 'px';
textDiv.style.fontFamily = style.fontFamily;
2013-06-19 01:05:55 +09:00
textDiv.textContent = geom.str;
textDiv.dataset.fontName = geom.fontName;
textDiv.dataset.angle = angle * (180 / Math.PI);
if (style.vertical) {
textDiv.dataset.canvasWidth = geom.height * this.viewport.scale;
} else {
textDiv.dataset.canvasWidth = geom.width * this.viewport.scale;
}
2013-06-19 01:05:55 +09:00
};
2014-06-22 05:53:26 +09:00
this.setTextContent = function textLayerBuilder_setTextContent(textContent) {
this.textContent = textContent;
2013-06-19 01:05:55 +09:00
var textItems = textContent.items;
2014-06-22 05:53:26 +09:00
for (var i = 0, len = textItems.length; i < len; i++) {
this.appendText(textItems[i], textContent.styles);
2013-06-19 01:05:55 +09:00
}
this.divContentDone = true;
2013-06-19 01:05:55 +09:00
this.setupRenderLayoutTimer();
};
2014-06-22 05:53:26 +09:00
this.convertMatches = function textLayerBuilder_convertMatches(matches) {
2013-06-19 01:05:55 +09:00
var i = 0;
var iIndex = 0;
var bidiTexts = this.textContent.items;
2013-06-19 01:05:55 +09:00
var end = bidiTexts.length - 1;
var queryLen = (PDFFindController === null ?
0 : PDFFindController.state.query.length);
2013-06-19 01:05:55 +09:00
var ret = [];
2014-06-22 05:53:26 +09:00
for (var m = 0, len = matches.length; m < len; m++) {
// Calculate the start position.
2013-06-19 01:05:55 +09:00
var matchIdx = matches[m];
// Loop over the divIdxs.
while (i !== end && matchIdx >= (iIndex + bidiTexts[i].str.length)) {
iIndex += bidiTexts[i].str.length;
i++;
}
2014-06-22 05:53:26 +09:00
if (i === bidiTexts.length) {
console.error('Could not find a matching mapping');
2013-06-19 01:05:55 +09:00
}
var match = {
begin: {
divIdx: i,
offset: matchIdx - iIndex
}
};
2014-06-22 05:53:26 +09:00
// Calculate the end position.
2013-06-19 01:05:55 +09:00
matchIdx += queryLen;
2014-06-22 05:53:26 +09:00
// Somewhat the same array as above, but use > instead of >= to get
// the end position right.
2013-06-19 01:05:55 +09:00
while (i !== end && matchIdx > (iIndex + bidiTexts[i].str.length)) {
iIndex += bidiTexts[i].str.length;
i++;
}
match.end = {
divIdx: i,
offset: matchIdx - iIndex
};
ret.push(match);
}
return ret;
};
this.renderMatches = function textLayerBuilder_renderMatches(matches) {
// Early exit if there is nothing to render.
if (matches.length === 0) {
return;
}
var bidiTexts = this.textContent.items;
2013-06-19 01:05:55 +09:00
var textDivs = this.textDivs;
var prevEnd = null;
var isSelectedPage = (PDFFindController === null ?
false : (this.pageIdx === PDFFindController.selected.pageIdx));
var selectedMatchIdx = (PDFFindController === null ?
-1 : PDFFindController.selected.matchIdx);
var highlightAll = (PDFFindController === null ?
false : PDFFindController.state.highlightAll);
2014-06-22 05:53:26 +09:00
var infinity = {
2013-06-19 01:05:55 +09:00
divIdx: -1,
offset: undefined
};
function beginText(begin, className) {
var divIdx = begin.divIdx;
2014-06-22 05:53:26 +09:00
textDivs[divIdx].textContent = '';
appendTextToDiv(divIdx, 0, begin.offset, className);
2013-06-19 01:05:55 +09:00
}
function appendTextToDiv(divIdx, fromOffset, toOffset, className) {
2013-06-19 01:05:55 +09:00
var div = textDivs[divIdx];
var content = bidiTexts[divIdx].str.substring(fromOffset, toOffset);
2013-06-19 01:05:55 +09:00
var node = document.createTextNode(content);
if (className) {
var span = document.createElement('span');
span.className = className;
span.appendChild(node);
div.appendChild(span);
return;
}
div.appendChild(node);
}
2014-06-22 05:53:26 +09:00
var i0 = selectedMatchIdx, i1 = i0 + 1;
2013-06-19 01:05:55 +09:00
if (highlightAll) {
i0 = 0;
i1 = matches.length;
} else if (!isSelectedPage) {
// Not highlighting all and this isn't the selected page, so do nothing.
return;
}
2014-06-22 05:53:26 +09:00
for (var i = i0; i < i1; i++) {
2013-06-19 01:05:55 +09:00
var match = matches[i];
var begin = match.begin;
var end = match.end;
2014-06-22 05:53:26 +09:00
var isSelected = (isSelectedPage && i === selectedMatchIdx);
2013-06-19 01:05:55 +09:00
var highlightSuffix = (isSelected ? ' selected' : '');
2014-06-22 05:53:26 +09:00
if (isSelected && !this.isViewerInPresentationMode) {
2014-06-22 05:53:26 +09:00
scrollIntoView(textDivs[begin.divIdx],
{ top: FIND_SCROLL_OFFSET_TOP,
left: FIND_SCROLL_OFFSET_LEFT });
}
2013-06-19 01:05:55 +09:00
// Match inside new div.
if (!prevEnd || begin.divIdx !== prevEnd.divIdx) {
2014-06-22 05:53:26 +09:00
// If there was a previous div, then add the text at the end.
2013-06-19 01:05:55 +09:00
if (prevEnd !== null) {
2014-06-22 05:53:26 +09:00
appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
2013-06-19 01:05:55 +09:00
}
2014-06-22 05:53:26 +09:00
// Clear the divs and set the content until the starting point.
2013-06-19 01:05:55 +09:00
beginText(begin);
} else {
2014-06-22 05:53:26 +09:00
appendTextToDiv(prevEnd.divIdx, prevEnd.offset, begin.offset);
2013-06-19 01:05:55 +09:00
}
if (begin.divIdx === end.divIdx) {
2014-06-22 05:53:26 +09:00
appendTextToDiv(begin.divIdx, begin.offset, end.offset,
'highlight' + highlightSuffix);
2013-06-19 01:05:55 +09:00
} else {
2014-06-22 05:53:26 +09:00
appendTextToDiv(begin.divIdx, begin.offset, infinity.offset,
'highlight begin' + highlightSuffix);
for (var n0 = begin.divIdx + 1, n1 = end.divIdx; n0 < n1; n0++) {
textDivs[n0].className = 'highlight middle' + highlightSuffix;
2013-06-19 01:05:55 +09:00
}
beginText(end, 'highlight end' + highlightSuffix);
}
prevEnd = end;
}
if (prevEnd) {
2014-06-22 05:53:26 +09:00
appendTextToDiv(prevEnd.divIdx, prevEnd.offset, infinity.offset);
2013-06-19 01:05:55 +09:00
}
};
2014-06-22 05:53:26 +09:00
this.updateMatches = function textLayerBuilder_updateMatches() {
// Only show matches when all rendering is done.
if (!this.renderingDone) {
2013-06-19 01:05:55 +09:00
return;
}
2013-06-19 01:05:55 +09:00
2014-06-22 05:53:26 +09:00
// Clear all matches.
2013-06-19 01:05:55 +09:00
var matches = this.matches;
var textDivs = this.textDivs;
var bidiTexts = this.textContent.items;
2013-06-19 01:05:55 +09:00
var clearedUntilDivIdx = -1;
2014-06-22 05:53:26 +09:00
// Clear all current matches.
for (var i = 0, len = matches.length; i < len; i++) {
2013-06-19 01:05:55 +09:00
var match = matches[i];
var begin = Math.max(clearedUntilDivIdx, match.begin.divIdx);
2014-06-22 05:53:26 +09:00
for (var n = begin, end = match.end.divIdx; n <= end; n++) {
2013-06-19 01:05:55 +09:00
var div = textDivs[n];
div.textContent = bidiTexts[n].str;
div.className = '';
}
clearedUntilDivIdx = match.end.divIdx + 1;
}
if (PDFFindController === null || !PDFFindController.active) {
2013-06-19 01:05:55 +09:00
return;
}
2013-06-19 01:05:55 +09:00
2014-06-22 05:53:26 +09:00
// Convert the matches on the page controller into the match format
// used for the textLayer.
this.matches = this.convertMatches(PDFFindController === null ?
[] : (PDFFindController.pageMatches[this.pageIdx] || []));
2013-06-19 01:05:55 +09:00
this.renderMatches(this.matches);
};
};