mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Merge pull request #2127 from jviereck/text-algo-3
Use the text extracted in the getTextContent function for the divs of the textLayer.
This commit is contained in:
commit
e98eba1b11
7 changed files with 233 additions and 96 deletions
|
@ -121,7 +121,7 @@ html[dir='rtl'] .innerCenter {
|
|||
-o-transition-timing-function: ease;
|
||||
transition-duration: 200ms;
|
||||
transition-timing-function: ease;
|
||||
|
||||
|
||||
}
|
||||
html[dir='ltr'] #sidebarContainer {
|
||||
-webkit-transition-property: left;
|
||||
|
@ -629,7 +629,7 @@ html[dir='rtl'] .toolbarButton:first-child {
|
|||
display: inline-block;
|
||||
content: url(images/toolbarButton-sidebarToggle.png);
|
||||
}
|
||||
|
||||
|
||||
html[dir='ltr'] .toolbarButton.pageUp::before {
|
||||
display: inline-block;
|
||||
content: url(images/toolbarButton-pageUp.png);
|
||||
|
@ -639,7 +639,7 @@ html[dir='rtl'] .toolbarButton.pageUp::before {
|
|||
display: inline-block;
|
||||
content: url(images/toolbarButton-pageUp-rtl.png);
|
||||
}
|
||||
|
||||
|
||||
html[dir='ltr'] .toolbarButton.pageDown::before {
|
||||
display: inline-block;
|
||||
content: url(images/toolbarButton-pageDown.png);
|
||||
|
@ -654,7 +654,7 @@ html[dir='rtl'] .toolbarButton.pageDown::before {
|
|||
display: inline-block;
|
||||
content: url(images/toolbarButton-zoomOut.png);
|
||||
}
|
||||
|
||||
|
||||
.toolbarButton.zoomIn::before {
|
||||
display: inline-block;
|
||||
content: url(images/toolbarButton-zoomIn.png);
|
||||
|
@ -691,12 +691,12 @@ html[dir='rtl'] .toolbarButton.pageDown::before {
|
|||
.toolbarButton.bookmark::before {
|
||||
content: url(images/toolbarButton-bookmark.png);
|
||||
}
|
||||
|
||||
|
||||
#viewThumbnail.toolbarButton::before {
|
||||
display: inline-block;
|
||||
content: url(images/toolbarButton-viewThumbnail.png);
|
||||
}
|
||||
|
||||
|
||||
#viewOutline.toolbarButton::before {
|
||||
display: inline-block;
|
||||
content: url(images/toolbarButton-viewOutline.png);
|
||||
|
@ -797,7 +797,7 @@ html[dir='rtl'] .toolbarButton.pageDown::before {
|
|||
padding: 7px;
|
||||
-moz-transition-duration: 150ms;
|
||||
}
|
||||
|
||||
|
||||
a:focus > .thumbnail > .thumbnailSelectionRing > .thumbnailImage,
|
||||
.thumbnail:hover > .thumbnailSelectionRing > .thumbnailImage {
|
||||
opacity: .9;
|
||||
|
@ -1016,7 +1016,7 @@ canvas {
|
|||
background: -moz-linear-gradient(top, #b2b2b2 0%,#898989 100%);
|
||||
background: -ms-linear-gradient(top, #b2b2b2 0%,#898989 100%);
|
||||
background: -o-linear-gradient(top, #b2b2b2 0%,#898989 100%);
|
||||
background: linear-gradient(top, #b2b2b2 0%,#898989 100%);
|
||||
background: linear-gradient(top, #b2b2b2 0%,#898989 100%);
|
||||
|
||||
border-top-left-radius: 2px;
|
||||
border-bottom-left-radius: 2px;
|
||||
|
@ -1066,6 +1066,7 @@ canvas {
|
|||
color: transparent;
|
||||
position: absolute;
|
||||
line-height:1.3;
|
||||
white-space:pre;
|
||||
}
|
||||
|
||||
/* TODO: file FF bug to support ::-moz-selection:window-inactive
|
||||
|
@ -1202,7 +1203,7 @@ canvas {
|
|||
|
||||
@page {
|
||||
margin: 0;
|
||||
}
|
||||
}
|
||||
|
||||
#printContainer {
|
||||
display: none;
|
||||
|
|
|
@ -1040,7 +1040,7 @@ var PDFView = {
|
|||
function extractPageText(pageIndex) {
|
||||
self.pages[pageIndex].pdfPage.getTextContent().then(
|
||||
function textContentResolved(textContent) {
|
||||
self.pageText[pageIndex] = textContent;
|
||||
self.pageText[pageIndex] = textContent.join('');
|
||||
self.search();
|
||||
if ((pageIndex + 1) < self.pages.length)
|
||||
extractPageText(pageIndex + 1);
|
||||
|
@ -1228,6 +1228,8 @@ var PageView = function pageView(container, pdfPage, id, scale,
|
|||
this.renderingState = RenderingStates.INITIAL;
|
||||
this.resume = null;
|
||||
|
||||
this.textContent = null;
|
||||
|
||||
var anchor = document.createElement('a');
|
||||
anchor.name = '' + this.id;
|
||||
|
||||
|
@ -1448,6 +1450,13 @@ var PageView = function pageView(container, pdfPage, id, scale,
|
|||
}, 0);
|
||||
};
|
||||
|
||||
this.getTextContent = function pageviewGetTextContent() {
|
||||
if (!this.textContent) {
|
||||
this.textContent = this.pdfPage.getTextContent();
|
||||
}
|
||||
return this.textContent;
|
||||
};
|
||||
|
||||
this.draw = function pageviewDraw(callback) {
|
||||
if (this.renderingState !== RenderingStates.INITIAL)
|
||||
error('Must be in new state before drawing');
|
||||
|
@ -1528,6 +1537,14 @@ var PageView = function pageView(container, pdfPage, id, scale,
|
|||
}
|
||||
);
|
||||
|
||||
if (textLayer) {
|
||||
this.getTextContent().then(
|
||||
function textContentResolved(textContent) {
|
||||
textLayer.setTextContent(textContent);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
setupAnnotations(this.pdfPage, this.viewport);
|
||||
div.setAttribute('data-loaded', true);
|
||||
};
|
||||
|
@ -1820,12 +1837,19 @@ var CustomStyle = (function CustomStyleClosure() {
|
|||
var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
|
||||
var textLayerFrag = document.createDocumentFragment();
|
||||
this.textLayerDiv = textLayerDiv;
|
||||
this.layoutDone = false;
|
||||
this.divContentDone = false;
|
||||
|
||||
this.beginLayout = function textLayerBuilderBeginLayout() {
|
||||
this.textDivs = [];
|
||||
this.textLayerQueue = [];
|
||||
};
|
||||
|
||||
this.endLayout = function textLayerBuilderEndLayout() {
|
||||
this.layoutDone = true;
|
||||
this.insertDivContent();
|
||||
},
|
||||
|
||||
this.renderLayer = function textLayerBuilderRenderLayer() {
|
||||
var self = this;
|
||||
var textDivs = this.textDivs;
|
||||
|
@ -1857,7 +1881,7 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
|
|||
textLayerDiv.appendChild(textLayerFrag);
|
||||
};
|
||||
|
||||
this.endLayout = function textLayerBuilderEndLayout() {
|
||||
this.setupRenderLayoutTimer = function textLayerSetupRenderLayoutTimer() {
|
||||
// Schedule renderLayout() if user has been scrolling, otherwise
|
||||
// run it right away
|
||||
var kRenderDelay = 200; // in ms
|
||||
|
@ -1870,27 +1894,56 @@ var TextLayerBuilder = function textLayerBuilder(textLayerDiv) {
|
|||
if (this.renderTimer)
|
||||
clearTimeout(this.renderTimer);
|
||||
this.renderTimer = setTimeout(function() {
|
||||
self.endLayout();
|
||||
self.setupRenderLayoutTimer();
|
||||
}, kRenderDelay);
|
||||
}
|
||||
}; // endLayout
|
||||
};
|
||||
|
||||
this.appendText = function textLayerBuilderAppendText(text,
|
||||
fontName, fontSize) {
|
||||
this.appendText = function textLayerBuilderAppendText(fontName, fontSize,
|
||||
geom) {
|
||||
var textDiv = document.createElement('div');
|
||||
|
||||
// vScale and hScale already contain the scaling to pixel units
|
||||
var fontHeight = fontSize * text.geom.vScale;
|
||||
textDiv.dataset.canvasWidth = text.canvasWidth * text.geom.hScale;
|
||||
var fontHeight = fontSize * geom.vScale;
|
||||
textDiv.dataset.canvasWidth = geom.canvasWidth * geom.hScale;
|
||||
textDiv.dataset.fontName = fontName;
|
||||
|
||||
textDiv.style.fontSize = fontHeight + 'px';
|
||||
textDiv.style.fontFamily = fontName;
|
||||
textDiv.style.left = text.geom.x + 'px';
|
||||
textDiv.style.top = (text.geom.y - fontHeight) + 'px';
|
||||
textDiv.textContent = PDFJS.bidi(text, -1);
|
||||
textDiv.dir = text.direction;
|
||||
textDiv.style.left = geom.x + 'px';
|
||||
textDiv.style.top = (geom.y - fontHeight) + 'px';
|
||||
|
||||
// The content of the div is set in the `setTextContent` function.
|
||||
|
||||
this.textDivs.push(textDiv);
|
||||
};
|
||||
|
||||
this.insertDivContent = function textLayerUpdateTextContent() {
|
||||
// Only set the content of the divs once layout has finished, the content
|
||||
// for the divs is available and content is not yet set on the divs.
|
||||
if (!this.layoutDone || this.divContentDone || !this.textContent)
|
||||
return;
|
||||
|
||||
this.divContentDone = true;
|
||||
|
||||
var textDivs = this.textDivs;
|
||||
var bidiTexts = this.textContent.bidiTexts;
|
||||
|
||||
for (var i = 0; i < bidiTexts.length; i++) {
|
||||
var bidiText = bidiTexts[i];
|
||||
var textDiv = textDivs[i];
|
||||
|
||||
textDiv.textContent = bidiText.str;
|
||||
textDiv.dir = bidiText.ltr ? 'ltr' : 'rtl';
|
||||
}
|
||||
|
||||
this.setupRenderLayoutTimer();
|
||||
};
|
||||
|
||||
this.setTextContent = function textLayerBuilderSetTextContent(textContent) {
|
||||
this.textContent = textContent;
|
||||
this.insertDivContent();
|
||||
};
|
||||
};
|
||||
|
||||
document.addEventListener('DOMContentLoaded', function webViewerLoad(evt) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue