Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiline text blocks #4629

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/text-selection/js/minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ window.onload = function () {

var scale = 1.5; //Set this to whatever you want. This is basically the "zoom" factor for the PDF.
PDFJS.workerSrc = '../../build/generic/build/pdf.worker.js';
var fontMetrics = new FontMetrics();

function loadPdf(pdfPath) {
var pdf = PDFJS.getDocument(pdfPath);
Expand Down Expand Up @@ -74,7 +75,8 @@ window.onload = function () {
var textLayer = new TextLayerBuilder({
textLayerDiv: $textLayerDiv.get(0),
viewport: viewport,
pageIndex: 0
pageIndex: 0,
fontMetrics: fontMetrics
});
textLayer.setTextContent(textContent);

Expand Down
3 changes: 2 additions & 1 deletion make.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,8 @@ function cleanupJSSource(file) {
var content = cat(file);

// Strip out all the vim/license headers.
var reg = /\n\/\* -\*- Mode(.|\n)*?Mozilla Foundation(.|\n)*?'use strict';/g;
var reg = new RegExp('\\n\\/\\* -\\*- Mode(.|\\n)*?' +
'(Mozilla Foundation|Opera Software ASA)(.|\\n)*?\'use strict\';', 'g');
content = content.replace(reg, '');

content.to(file);
Expand Down
10 changes: 10 additions & 0 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ PDFJS.disableWebGL = (PDFJS.disableWebGL === undefined ?
PDFJS.verbosity = (PDFJS.verbosity === undefined ?
PDFJS.VERBOSITY_LEVELS.warnings : PDFJS.verbosity);

/**
* Disables creation of multi-line text blocks in the text layer.
* Currently disabled by default.
* @var {boolean}
*/

PDFJS.disableMultilineTextLayer =
(PDFJS.disableMultilineTextLayer === undefined ?
true : PDFJS.disableMultilineTextLayer);

/**
* Document initialization / loading parameters object.
*
Expand Down
82 changes: 82 additions & 0 deletions web/font_metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */

/* Copyright 2014 Opera Software ASA
*
* 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.
*/

'use strict';

var FontMetrics = (function FontMetricsClosure () {

function FontMetrics () {
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
this.canvas.width = 100;
this.canvas.height = 100;
this.cache = Object.create(null);
}

FontMetrics.prototype = {
getFirstNoneWhitePixelTop: function FontMetricsGetFirstNoneWhitePixelTop() {
var x = 0;
var y = 0;
var width = this.canvas.width;
var height = this.canvas.height;
var pixels = null;
while (y < height) {
pixels = this.ctx.getImageData(0, y, width, 1).data;
for (x = 0; x < width * 4; x += 4) {
if (pixels[x] === 0) {
break;
}
}

if (x < width) {
break;
}
y++;
}
return y;
},

getFontAscent: function FontMetricsDetFontAscent(font, str) {
if (this.cache[font]) {
return this.cache[font];
}

if (!str) {
str = 'The quick brown fox jumps over the lazy dog';
}
var height = parseFloat(font);
var textBaselines = ['alphabetic', 'top'];
var results = [];
for (var i = 0; i < 2; i++) {
this.ctx.fillStyle = '#fff';
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
this.ctx.textBaseline = textBaselines[i];
this.ctx.fillStyle = '#000';
this.ctx.font = font;
this.ctx.fillText(str, 0, height);
results[i] = this.getFirstNoneWhitePixelTop();
}
var ascent = results[1] - results[0];
this.cache[font] = ascent;
return ascent;
}
};

return FontMetrics;

})();
3 changes: 2 additions & 1 deletion web/page_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ var PageView = function pageView(container, id, scale,
pageIndex: this.id - 1,
lastScrollSource: PDFView,
viewport: this.viewport,
isViewerInPresentationMode: PresentationMode.active
isViewerInPresentationMode: PresentationMode.active,
fontMetrics: PDFView.fontMetrics
}) : null;
// TODO(mack): use data attributes to store these
ctx._scaleX = outputScale.sx;
Expand Down
Loading