2015-11-11 00:45:03 +09:00
|
|
|
/* Copyright 2015 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.
|
|
|
|
*/
|
|
|
|
|
2020-01-02 20:00:16 +09:00
|
|
|
import {
|
|
|
|
AbortException,
|
|
|
|
createPromiseCapability,
|
|
|
|
Util,
|
|
|
|
} from "../shared/util.js";
|
2015-11-22 01:32:47 +09:00
|
|
|
|
2015-11-11 00:45:03 +09:00
|
|
|
/**
|
|
|
|
* Text layer render parameters.
|
|
|
|
*
|
|
|
|
* @typedef {Object} TextLayerRenderParameters
|
2020-07-23 05:38:04 +09:00
|
|
|
* @property {import("./api").TextContent} [textContent] - Text content to
|
|
|
|
* render (the object is returned by the page's `getTextContent` method).
|
2019-10-12 23:30:32 +09:00
|
|
|
* @property {ReadableStream} [textContentStream] - Text content stream to
|
|
|
|
* render (the stream is returned by the page's `streamTextContent` method).
|
2015-11-11 00:45:03 +09:00
|
|
|
* @property {HTMLElement} container - HTML element that will contain text runs.
|
2020-07-23 05:38:04 +09:00
|
|
|
* @property {import("./display_utils").PageViewport} viewport - The target
|
|
|
|
* viewport to properly layout the text runs.
|
|
|
|
* @property {Array<HTMLElement>} [textDivs] - HTML elements that are correspond
|
|
|
|
* to the text items of the textContent input. This is output and shall be
|
2015-11-11 00:45:03 +09:00
|
|
|
* initially be set to empty array.
|
2020-07-23 05:38:04 +09:00
|
|
|
* @property {Array<string>} [textContentItemsStr] - Strings that correspond to
|
|
|
|
* the `str` property of the text items of textContent input. This is output
|
2017-04-17 21:46:53 +09:00
|
|
|
* and shall be initially be set to empty array.
|
2019-10-12 23:30:32 +09:00
|
|
|
* @property {number} [timeout] - Delay in milliseconds before rendering of the
|
|
|
|
* text runs occurs.
|
|
|
|
* @property {boolean} [enhanceTextSelection] - Whether to turn on the text
|
|
|
|
* selection enhancement.
|
2015-11-11 00:45:03 +09:00
|
|
|
*/
|
2020-07-23 05:38:04 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
const MAX_TEXT_DIVS_TO_RENDER = 100000;
|
|
|
|
const DEFAULT_FONT_SIZE = 30;
|
|
|
|
const DEFAULT_FONT_ASCENT = 0.8;
|
|
|
|
const ascentCache = new Map();
|
|
|
|
const AllWhitespaceRegexp = /^\s+$/g;
|
|
|
|
|
|
|
|
function getAscent(fontFamily, ctx) {
|
|
|
|
const cachedAscent = ascentCache.get(fontFamily);
|
|
|
|
if (cachedAscent) {
|
|
|
|
return cachedAscent;
|
|
|
|
}
|
2020-07-23 05:38:04 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
ctx.save();
|
|
|
|
ctx.font = `${DEFAULT_FONT_SIZE}px ${fontFamily}`;
|
|
|
|
const metrics = ctx.measureText("");
|
2021-01-24 02:21:48 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Both properties aren't available by default in Firefox.
|
|
|
|
let ascent = metrics.fontBoundingBoxAscent;
|
|
|
|
let descent = Math.abs(metrics.fontBoundingBoxDescent);
|
|
|
|
if (ascent) {
|
2021-01-24 02:21:48 +09:00
|
|
|
ctx.restore();
|
2021-05-05 23:36:18 +09:00
|
|
|
const ratio = ascent / (ascent + descent);
|
|
|
|
ascentCache.set(fontFamily, ratio);
|
|
|
|
return ratio;
|
|
|
|
}
|
2021-01-24 02:21:48 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Try basic heuristic to guess ascent/descent.
|
|
|
|
// Draw a g with baseline at 0,0 and then get the line
|
|
|
|
// number where a pixel has non-null red component (starting
|
|
|
|
// from bottom).
|
|
|
|
ctx.strokeStyle = "red";
|
|
|
|
ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
|
|
|
|
ctx.strokeText("g", 0, 0);
|
2021-05-16 17:58:34 +09:00
|
|
|
let pixels = ctx.getImageData(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
DEFAULT_FONT_SIZE,
|
|
|
|
DEFAULT_FONT_SIZE
|
|
|
|
).data;
|
2021-05-05 23:36:18 +09:00
|
|
|
descent = 0;
|
|
|
|
for (let i = pixels.length - 1 - 3; i >= 0; i -= 4) {
|
|
|
|
if (pixels[i] > 0) {
|
|
|
|
descent = Math.ceil(i / 4 / DEFAULT_FONT_SIZE);
|
|
|
|
break;
|
2021-01-24 02:21:48 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Draw an A with baseline at 0,DEFAULT_FONT_SIZE and then get the line
|
|
|
|
// number where a pixel has non-null red component (starting
|
|
|
|
// from top).
|
|
|
|
ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
|
|
|
|
ctx.strokeText("A", 0, DEFAULT_FONT_SIZE);
|
|
|
|
pixels = ctx.getImageData(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE).data;
|
|
|
|
ascent = 0;
|
|
|
|
for (let i = 0, ii = pixels.length; i < ii; i += 4) {
|
|
|
|
if (pixels[i] > 0) {
|
|
|
|
ascent = DEFAULT_FONT_SIZE - Math.floor(i / 4 / DEFAULT_FONT_SIZE);
|
|
|
|
break;
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
ctx.restore();
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (ascent) {
|
|
|
|
const ratio = ascent / (ascent + descent);
|
|
|
|
ascentCache.set(fontFamily, ratio);
|
|
|
|
return ratio;
|
|
|
|
}
|
2021-04-21 17:47:51 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
ascentCache.set(fontFamily, DEFAULT_FONT_ASCENT);
|
|
|
|
return DEFAULT_FONT_ASCENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
function appendText(task, geom, styles, ctx) {
|
|
|
|
// Initialize all used properties to keep the caches monomorphic.
|
|
|
|
const textDiv = document.createElement("span");
|
2021-09-05 19:12:34 +09:00
|
|
|
const textDivProperties = task._enhanceTextSelection
|
|
|
|
? {
|
|
|
|
angle: 0,
|
|
|
|
canvasWidth: 0,
|
|
|
|
hasText: geom.str !== "",
|
|
|
|
hasEOL: geom.hasEOL,
|
|
|
|
originalTransform: null,
|
|
|
|
paddingBottom: 0,
|
|
|
|
paddingLeft: 0,
|
|
|
|
paddingRight: 0,
|
|
|
|
paddingTop: 0,
|
|
|
|
scale: 1,
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
angle: 0,
|
|
|
|
canvasWidth: 0,
|
|
|
|
hasText: geom.str !== "",
|
|
|
|
hasEOL: geom.hasEOL,
|
|
|
|
};
|
2021-01-26 06:10:46 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
task._textDivs.push(textDiv);
|
|
|
|
|
|
|
|
const tx = Util.transform(task._viewport.transform, geom.transform);
|
|
|
|
let angle = Math.atan2(tx[1], tx[0]);
|
|
|
|
const style = styles[geom.fontName];
|
|
|
|
if (style.vertical) {
|
|
|
|
angle += Math.PI / 2;
|
|
|
|
}
|
|
|
|
const fontHeight = Math.hypot(tx[2], tx[3]);
|
|
|
|
const fontAscent = fontHeight * getAscent(style.fontFamily, ctx);
|
|
|
|
|
|
|
|
let left, top;
|
|
|
|
if (angle === 0) {
|
|
|
|
left = tx[4];
|
|
|
|
top = tx[5] - fontAscent;
|
|
|
|
} else {
|
|
|
|
left = tx[4] + fontAscent * Math.sin(angle);
|
|
|
|
top = tx[5] - fontAscent * Math.cos(angle);
|
|
|
|
}
|
|
|
|
// Setting the style properties individually, rather than all at once,
|
|
|
|
// should be OK since the `textDiv` isn't appended to the document yet.
|
|
|
|
textDiv.style.left = `${left}px`;
|
|
|
|
textDiv.style.top = `${top}px`;
|
|
|
|
textDiv.style.fontSize = `${fontHeight}px`;
|
|
|
|
textDiv.style.fontFamily = style.fontFamily;
|
|
|
|
|
|
|
|
// Keeps screen readers from pausing on every new text span.
|
|
|
|
textDiv.setAttribute("role", "presentation");
|
|
|
|
|
|
|
|
textDiv.textContent = geom.str;
|
|
|
|
// geom.dir may be 'ttb' for vertical texts.
|
|
|
|
textDiv.dir = geom.dir;
|
|
|
|
|
|
|
|
// `fontName` is only used by the FontInspector, and we only use `dataset`
|
|
|
|
// here to make the font name available in the debugger.
|
|
|
|
if (task._fontInspectorEnabled) {
|
|
|
|
textDiv.dataset.fontName = geom.fontName;
|
|
|
|
}
|
|
|
|
if (angle !== 0) {
|
|
|
|
textDivProperties.angle = angle * (180 / Math.PI);
|
|
|
|
}
|
|
|
|
// We don't bother scaling single-char text divs, because it has very
|
|
|
|
// little effect on text highlighting. This makes scrolling on docs with
|
|
|
|
// lots of such divs a lot faster.
|
|
|
|
let shouldScaleText = false;
|
|
|
|
if (
|
|
|
|
geom.str.length > 1 ||
|
|
|
|
(task._enhanceTextSelection && AllWhitespaceRegexp.test(geom.str))
|
|
|
|
) {
|
|
|
|
shouldScaleText = true;
|
2021-05-24 02:03:53 +09:00
|
|
|
} else if (geom.str !== " " && geom.transform[0] !== geom.transform[3]) {
|
2021-05-05 23:36:18 +09:00
|
|
|
const absScaleX = Math.abs(geom.transform[0]),
|
|
|
|
absScaleY = Math.abs(geom.transform[3]);
|
|
|
|
// When the horizontal/vertical scaling differs significantly, also scale
|
|
|
|
// even single-char text to improve highlighting (fixes issue11713.pdf).
|
2021-04-30 21:41:13 +09:00
|
|
|
if (
|
2021-05-05 23:36:18 +09:00
|
|
|
absScaleX !== absScaleY &&
|
|
|
|
Math.max(absScaleX, absScaleY) / Math.min(absScaleX, absScaleY) > 1.5
|
2021-04-30 21:41:13 +09:00
|
|
|
) {
|
2020-03-22 22:09:08 +09:00
|
|
|
shouldScaleText = true;
|
2017-04-17 21:46:53 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
|
|
|
if (shouldScaleText) {
|
|
|
|
if (style.vertical) {
|
|
|
|
textDivProperties.canvasWidth = geom.height * task._viewport.scale;
|
|
|
|
} else {
|
|
|
|
textDivProperties.canvasWidth = geom.width * task._viewport.scale;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
task._textDivProperties.set(textDiv, textDivProperties);
|
|
|
|
if (task._textContentStream) {
|
|
|
|
task._layoutText(textDiv);
|
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (task._enhanceTextSelection && textDivProperties.hasText) {
|
|
|
|
let angleCos = 1,
|
|
|
|
angleSin = 0;
|
|
|
|
if (angle !== 0) {
|
|
|
|
angleCos = Math.cos(angle);
|
|
|
|
angleSin = Math.sin(angle);
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
const divWidth =
|
|
|
|
(style.vertical ? geom.height : geom.width) * task._viewport.scale;
|
|
|
|
const divHeight = fontHeight;
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
let m, b;
|
|
|
|
if (angle !== 0) {
|
|
|
|
m = [angleCos, angleSin, -angleSin, angleCos, left, top];
|
|
|
|
b = Util.getAxialAlignedBoundingBox([0, 0, divWidth, divHeight], m);
|
|
|
|
} else {
|
|
|
|
b = [left, top, left + divWidth, top + divHeight];
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
task._bounds.push({
|
|
|
|
left: b[0],
|
|
|
|
top: b[1],
|
|
|
|
right: b[2],
|
|
|
|
bottom: b[3],
|
|
|
|
div: textDiv,
|
|
|
|
size: [divWidth, divHeight],
|
|
|
|
m,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function render(task) {
|
|
|
|
if (task._canceled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const textDivs = task._textDivs;
|
|
|
|
const capability = task._capability;
|
|
|
|
const textDivsLength = textDivs.length;
|
2017-04-17 21:46:53 +09:00
|
|
|
|
2021-05-05 23:36:18 +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) {
|
2016-08-17 08:06:35 +09:00
|
|
|
task._renderingDone = true;
|
|
|
|
capability.resolve();
|
2021-05-05 23:36:18 +09:00
|
|
|
return;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (!task._textContentStream) {
|
|
|
|
for (let i = 0; i < textDivsLength; i++) {
|
|
|
|
task._layoutText(textDivs[i]);
|
2020-03-21 07:00:36 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
task._renderingDone = true;
|
|
|
|
capability.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
function findPositiveMin(ts, offset, count) {
|
|
|
|
let result = 0;
|
|
|
|
for (let i = 0; i < count; i++) {
|
|
|
|
const t = ts[offset++];
|
|
|
|
if (t > 0) {
|
|
|
|
result = result ? Math.min(t, result) : t;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expand(task) {
|
|
|
|
const bounds = task._bounds;
|
|
|
|
const viewport = task._viewport;
|
|
|
|
|
|
|
|
const expanded = expandBounds(viewport.width, viewport.height, bounds);
|
|
|
|
for (let i = 0; i < expanded.length; i++) {
|
|
|
|
const div = bounds[i].div;
|
|
|
|
const divProperties = task._textDivProperties.get(div);
|
|
|
|
if (divProperties.angle === 0) {
|
|
|
|
divProperties.paddingLeft = bounds[i].left - expanded[i].left;
|
|
|
|
divProperties.paddingTop = bounds[i].top - expanded[i].top;
|
|
|
|
divProperties.paddingRight = expanded[i].right - bounds[i].right;
|
|
|
|
divProperties.paddingBottom = expanded[i].bottom - bounds[i].bottom;
|
|
|
|
task._textDivProperties.set(div, divProperties);
|
|
|
|
continue;
|
2021-04-24 19:36:01 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
// Box is rotated -- trying to find padding so rotated div will not
|
|
|
|
// exceed its expanded bounds.
|
|
|
|
const e = expanded[i],
|
|
|
|
b = bounds[i];
|
|
|
|
const m = b.m,
|
|
|
|
c = m[0],
|
|
|
|
s = m[1];
|
|
|
|
// Finding intersections with expanded box.
|
|
|
|
const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size];
|
|
|
|
const ts = new Float64Array(64);
|
|
|
|
for (let j = 0, jj = points.length; j < jj; j++) {
|
|
|
|
const t = Util.applyTransform(points[j], m);
|
|
|
|
ts[j + 0] = c && (e.left - t[0]) / c;
|
|
|
|
ts[j + 4] = s && (e.top - t[1]) / s;
|
|
|
|
ts[j + 8] = c && (e.right - t[0]) / c;
|
|
|
|
ts[j + 12] = s && (e.bottom - t[1]) / s;
|
|
|
|
|
|
|
|
ts[j + 16] = s && (e.left - t[0]) / -s;
|
|
|
|
ts[j + 20] = c && (e.top - t[1]) / c;
|
|
|
|
ts[j + 24] = s && (e.right - t[0]) / -s;
|
|
|
|
ts[j + 28] = c && (e.bottom - t[1]) / c;
|
|
|
|
|
|
|
|
ts[j + 32] = c && (e.left - t[0]) / -c;
|
|
|
|
ts[j + 36] = s && (e.top - t[1]) / -s;
|
|
|
|
ts[j + 40] = c && (e.right - t[0]) / -c;
|
|
|
|
ts[j + 44] = s && (e.bottom - t[1]) / -s;
|
|
|
|
|
|
|
|
ts[j + 48] = s && (e.left - t[0]) / s;
|
|
|
|
ts[j + 52] = c && (e.top - t[1]) / -c;
|
|
|
|
ts[j + 56] = s && (e.right - t[0]) / s;
|
|
|
|
ts[j + 60] = c && (e.bottom - t[1]) / -c;
|
2021-04-24 19:36:01 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
// Not based on math, but to simplify calculations, using cos and sin
|
|
|
|
// absolute values to not exceed the box (it can but insignificantly).
|
|
|
|
const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s));
|
|
|
|
divProperties.paddingLeft = findPositiveMin(ts, 32, 16) / boxScale;
|
|
|
|
divProperties.paddingTop = findPositiveMin(ts, 48, 16) / boxScale;
|
|
|
|
divProperties.paddingRight = findPositiveMin(ts, 0, 16) / boxScale;
|
|
|
|
divProperties.paddingBottom = findPositiveMin(ts, 16, 16) / boxScale;
|
|
|
|
task._textDivProperties.set(div, divProperties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandBounds(width, height, boxes) {
|
|
|
|
const bounds = boxes.map(function (box, i) {
|
|
|
|
return {
|
|
|
|
x1: box.left,
|
|
|
|
y1: box.top,
|
|
|
|
x2: box.right,
|
|
|
|
y2: box.bottom,
|
|
|
|
index: i,
|
|
|
|
x1New: undefined,
|
|
|
|
x2New: undefined,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
expandBoundsLTR(width, bounds);
|
|
|
|
|
|
|
|
const expanded = new Array(boxes.length);
|
|
|
|
for (const b of bounds) {
|
|
|
|
const i = b.index;
|
|
|
|
expanded[i] = {
|
|
|
|
left: b.x1New,
|
|
|
|
top: 0,
|
|
|
|
right: b.x2New,
|
|
|
|
bottom: 0,
|
|
|
|
};
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Rotating on 90 degrees and extending extended boxes. Reusing the bounds
|
|
|
|
// array and objects.
|
|
|
|
boxes.map(function (box, i) {
|
|
|
|
const e = expanded[i],
|
|
|
|
b = bounds[i];
|
|
|
|
b.x1 = box.top;
|
|
|
|
b.y1 = width - e.right;
|
|
|
|
b.x2 = box.bottom;
|
|
|
|
b.y2 = width - e.left;
|
|
|
|
b.index = i;
|
|
|
|
b.x1New = undefined;
|
|
|
|
b.x2New = undefined;
|
|
|
|
});
|
|
|
|
expandBoundsLTR(height, bounds);
|
|
|
|
|
|
|
|
for (const b of bounds) {
|
|
|
|
const i = b.index;
|
|
|
|
expanded[i].top = b.x1New;
|
|
|
|
expanded[i].bottom = b.x2New;
|
|
|
|
}
|
|
|
|
return expanded;
|
|
|
|
}
|
|
|
|
|
|
|
|
function expandBoundsLTR(width, bounds) {
|
|
|
|
// Sorting by x1 coordinate and walk by the bounds in the same order.
|
|
|
|
bounds.sort(function (a, b) {
|
|
|
|
return a.x1 - b.x1 || a.index - b.index;
|
|
|
|
});
|
|
|
|
|
|
|
|
// First we see on the horizon is a fake boundary.
|
|
|
|
const fakeBoundary = {
|
|
|
|
x1: -Infinity,
|
|
|
|
y1: -Infinity,
|
|
|
|
x2: 0,
|
|
|
|
y2: Infinity,
|
|
|
|
index: -1,
|
|
|
|
x1New: 0,
|
|
|
|
x2New: 0,
|
|
|
|
};
|
|
|
|
const horizon = [
|
|
|
|
{
|
|
|
|
start: -Infinity,
|
|
|
|
end: Infinity,
|
|
|
|
boundary: fakeBoundary,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const boundary of bounds) {
|
|
|
|
// Searching for the affected part of horizon.
|
|
|
|
// TODO red-black tree or simple binary search
|
|
|
|
let i = 0;
|
|
|
|
while (i < horizon.length && horizon[i].end <= boundary.y1) {
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
let j = horizon.length - 1;
|
|
|
|
while (j >= 0 && horizon[j].start >= boundary.y2) {
|
|
|
|
j--;
|
|
|
|
}
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
let horizonPart, affectedBoundary;
|
|
|
|
let q,
|
|
|
|
k,
|
|
|
|
maxXNew = -Infinity;
|
|
|
|
for (q = i; q <= j; q++) {
|
|
|
|
horizonPart = horizon[q];
|
|
|
|
affectedBoundary = horizonPart.boundary;
|
|
|
|
let xNew;
|
|
|
|
if (affectedBoundary.x2 > boundary.x1) {
|
|
|
|
// In the middle of the previous element, new x shall be at the
|
|
|
|
// boundary start. Extending if further if the affected boundary
|
|
|
|
// placed on top of the current one.
|
|
|
|
xNew =
|
|
|
|
affectedBoundary.index > boundary.index
|
|
|
|
? affectedBoundary.x1New
|
|
|
|
: boundary.x1;
|
|
|
|
} else if (affectedBoundary.x2New === undefined) {
|
|
|
|
// We have some space in between, new x in middle will be a fair
|
|
|
|
// choice.
|
|
|
|
xNew = (affectedBoundary.x2 + boundary.x1) / 2;
|
|
|
|
} else {
|
|
|
|
// Affected boundary has x2new set, using it as new x.
|
|
|
|
xNew = affectedBoundary.x2New;
|
2016-03-30 02:32:51 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
if (xNew > maxXNew) {
|
|
|
|
maxXNew = xNew;
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set new x1 for current boundary.
|
|
|
|
boundary.x1New = maxXNew;
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Adjusts new x2 for the affected boundaries.
|
|
|
|
for (q = i; q <= j; q++) {
|
|
|
|
horizonPart = horizon[q];
|
|
|
|
affectedBoundary = horizonPart.boundary;
|
|
|
|
if (affectedBoundary.x2New === undefined) {
|
|
|
|
// Was not set yet, choosing new x if possible.
|
2016-08-17 08:06:35 +09:00
|
|
|
if (affectedBoundary.x2 > boundary.x1) {
|
2021-05-05 23:36:18 +09:00
|
|
|
// Current and affected boundaries intersect. If affected boundary
|
|
|
|
// is placed on top of the current, shrinking the affected.
|
|
|
|
if (affectedBoundary.index > boundary.index) {
|
|
|
|
affectedBoundary.x2New = affectedBoundary.x2;
|
|
|
|
}
|
2016-08-17 08:06:35 +09:00
|
|
|
} else {
|
2021-05-05 23:36:18 +09:00
|
|
|
affectedBoundary.x2New = maxXNew;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
} else if (affectedBoundary.x2New > maxXNew) {
|
|
|
|
// Affected boundary is touching new x, pushing it back.
|
|
|
|
affectedBoundary.x2New = Math.max(maxXNew, affectedBoundary.x2);
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Fixing the horizon.
|
|
|
|
const changedHorizon = [];
|
|
|
|
let lastBoundary = null;
|
|
|
|
for (q = i; q <= j; q++) {
|
|
|
|
horizonPart = horizon[q];
|
|
|
|
affectedBoundary = horizonPart.boundary;
|
|
|
|
// Checking which boundary will be visible.
|
|
|
|
const useBoundary =
|
|
|
|
affectedBoundary.x2 > boundary.x2 ? affectedBoundary : boundary;
|
|
|
|
if (lastBoundary === useBoundary) {
|
|
|
|
// Merging with previous.
|
|
|
|
changedHorizon[changedHorizon.length - 1].end = horizonPart.end;
|
|
|
|
} else {
|
|
|
|
changedHorizon.push({
|
|
|
|
start: horizonPart.start,
|
|
|
|
end: horizonPart.end,
|
|
|
|
boundary: useBoundary,
|
|
|
|
});
|
|
|
|
lastBoundary = useBoundary;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
|
|
|
if (horizon[i].start < boundary.y1) {
|
|
|
|
changedHorizon[0].start = boundary.y1;
|
|
|
|
changedHorizon.unshift({
|
|
|
|
start: horizon[i].start,
|
|
|
|
end: boundary.y1,
|
|
|
|
boundary: horizon[i].boundary,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (boundary.y2 < horizon[j].end) {
|
|
|
|
changedHorizon[changedHorizon.length - 1].end = boundary.y2;
|
|
|
|
changedHorizon.push({
|
|
|
|
start: boundary.y2,
|
|
|
|
end: horizon[j].end,
|
|
|
|
boundary: horizon[j].boundary,
|
|
|
|
});
|
|
|
|
}
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Set x2 new of boundary that is no longer visible (see overlapping case
|
|
|
|
// above).
|
|
|
|
// TODO more efficient, e.g. via reference counting.
|
|
|
|
for (q = i; q <= j; q++) {
|
|
|
|
horizonPart = horizon[q];
|
|
|
|
affectedBoundary = horizonPart.boundary;
|
|
|
|
if (affectedBoundary.x2New !== undefined) {
|
|
|
|
continue;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
let used = false;
|
|
|
|
for (
|
|
|
|
k = i - 1;
|
|
|
|
!used && k >= 0 && horizon[k].start >= affectedBoundary.y1;
|
|
|
|
k--
|
|
|
|
) {
|
|
|
|
used = horizon[k].boundary === affectedBoundary;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
for (
|
|
|
|
k = j + 1;
|
|
|
|
!used && k < horizon.length && horizon[k].end <= affectedBoundary.y2;
|
|
|
|
k++
|
|
|
|
) {
|
|
|
|
used = horizon[k].boundary === affectedBoundary;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
for (k = 0; !used && k < changedHorizon.length; k++) {
|
|
|
|
used = changedHorizon[k].boundary === affectedBoundary;
|
|
|
|
}
|
|
|
|
if (!used) {
|
|
|
|
affectedBoundary.x2New = maxXNew;
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-04-24 19:36:01 +09:00
|
|
|
}
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
Array.prototype.splice.apply(
|
|
|
|
horizon,
|
|
|
|
[i, j - i + 1].concat(changedHorizon)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set new x2 for all unset boundaries.
|
|
|
|
for (const horizonPart of horizon) {
|
|
|
|
const affectedBoundary = horizonPart.boundary;
|
|
|
|
if (affectedBoundary.x2New === undefined) {
|
|
|
|
affectedBoundary.x2New = Math.max(width, affectedBoundary.x2);
|
2021-04-24 19:36:01 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
class TextLayerRenderTask {
|
|
|
|
constructor({
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
textContent,
|
|
|
|
textContentStream,
|
|
|
|
container,
|
|
|
|
viewport,
|
|
|
|
textDivs,
|
|
|
|
textContentItemsStr,
|
|
|
|
enhanceTextSelection,
|
|
|
|
}) {
|
2015-11-11 00:45:03 +09:00
|
|
|
this._textContent = textContent;
|
2017-04-17 21:46:53 +09:00
|
|
|
this._textContentStream = textContentStream;
|
2015-11-11 00:45:03 +09:00
|
|
|
this._container = container;
|
2020-07-28 02:22:45 +09:00
|
|
|
this._document = container.ownerDocument;
|
2015-11-11 00:45:03 +09:00
|
|
|
this._viewport = viewport;
|
2016-09-03 00:51:13 +09:00
|
|
|
this._textDivs = textDivs || [];
|
2017-04-17 21:46:53 +09:00
|
|
|
this._textContentItemsStr = textContentItemsStr || [];
|
|
|
|
this._enhanceTextSelection = !!enhanceTextSelection;
|
2020-11-06 22:36:16 +09:00
|
|
|
this._fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
|
2017-04-17 21:46:53 +09:00
|
|
|
|
|
|
|
this._reader = null;
|
|
|
|
this._layoutTextLastFontSize = null;
|
|
|
|
this._layoutTextLastFontFamily = null;
|
|
|
|
this._layoutTextCtx = null;
|
2016-09-03 01:10:37 +09:00
|
|
|
this._textDivProperties = new WeakMap();
|
2016-08-17 08:06:35 +09:00
|
|
|
this._renderingDone = false;
|
2015-11-11 00:45:03 +09:00
|
|
|
this._canceled = false;
|
|
|
|
this._capability = createPromiseCapability();
|
|
|
|
this._renderTimer = null;
|
2016-08-17 08:06:35 +09:00
|
|
|
this._bounds = [];
|
2019-03-01 11:32:40 +09:00
|
|
|
|
|
|
|
// Always clean-up the temporary canvas once rendering is no longer pending.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this._capability.promise
|
|
|
|
.finally(() => {
|
2021-09-05 19:12:34 +09:00
|
|
|
if (!this._enhanceTextSelection) {
|
|
|
|
// The `textDiv` properties are no longer needed.
|
|
|
|
this._textDivProperties = null;
|
|
|
|
}
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (this._layoutTextCtx) {
|
|
|
|
// Zeroing the width and height cause Firefox to release graphics
|
|
|
|
// resources immediately, which can greatly reduce memory consumption.
|
|
|
|
this._layoutTextCtx.canvas.width = 0;
|
|
|
|
this._layoutTextCtx.canvas.height = 0;
|
|
|
|
this._layoutTextCtx = null;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
2021-07-30 21:13:02 +09:00
|
|
|
// Avoid "Uncaught promise" messages in the console.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
});
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
/**
|
|
|
|
* Promise for textLayer rendering task completion.
|
|
|
|
* @type {Promise<void>}
|
|
|
|
*/
|
|
|
|
get promise() {
|
|
|
|
return this._capability.promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel rendering of the textLayer.
|
|
|
|
*/
|
|
|
|
cancel() {
|
|
|
|
this._canceled = true;
|
|
|
|
if (this._reader) {
|
2021-07-30 21:13:02 +09:00
|
|
|
this._reader
|
|
|
|
.cancel(new AbortException("TextLayer task cancelled."))
|
|
|
|
.catch(() => {
|
|
|
|
// Avoid "Uncaught promise" messages in the console.
|
|
|
|
});
|
2021-05-05 23:36:18 +09:00
|
|
|
this._reader = null;
|
|
|
|
}
|
|
|
|
if (this._renderTimer !== null) {
|
|
|
|
clearTimeout(this._renderTimer);
|
|
|
|
this._renderTimer = null;
|
|
|
|
}
|
|
|
|
this._capability.reject(new Error("TextLayer task cancelled."));
|
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_processItems(items, styleCache) {
|
|
|
|
for (let i = 0, len = items.length; i < len; i++) {
|
|
|
|
if (items[i].str === undefined) {
|
|
|
|
if (
|
|
|
|
items[i].type === "beginMarkedContentProps" ||
|
|
|
|
items[i].type === "beginMarkedContent"
|
|
|
|
) {
|
|
|
|
const parent = this._container;
|
|
|
|
this._container = document.createElement("span");
|
|
|
|
this._container.classList.add("markedContent");
|
|
|
|
if (items[i].id !== null) {
|
|
|
|
this._container.setAttribute("id", `${items[i].id}`);
|
2021-04-01 07:07:02 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
parent.appendChild(this._container);
|
|
|
|
} else if (items[i].type === "endMarkedContent") {
|
|
|
|
this._container = this._container.parentNode;
|
2021-04-01 07:07:02 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
continue;
|
2017-04-17 21:46:53 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
this._textContentItemsStr.push(items[i].str);
|
|
|
|
appendText(this, items[i], styleCache, this._layoutTextCtx);
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 21:46:53 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_layoutText(textDiv) {
|
|
|
|
const textDivProperties = this._textDivProperties.get(textDiv);
|
2019-08-28 23:08:06 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
let transform = "";
|
|
|
|
if (textDivProperties.canvasWidth !== 0 && textDivProperties.hasText) {
|
|
|
|
const { fontSize, fontFamily } = textDiv.style;
|
2019-08-28 23:08:06 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// Only build font string and set to context if different from last.
|
|
|
|
if (
|
|
|
|
fontSize !== this._layoutTextLastFontSize ||
|
|
|
|
fontFamily !== this._layoutTextLastFontFamily
|
|
|
|
) {
|
|
|
|
this._layoutTextCtx.font = `${fontSize} ${fontFamily}`;
|
|
|
|
this._layoutTextLastFontSize = fontSize;
|
|
|
|
this._layoutTextLastFontFamily = fontFamily;
|
2017-04-17 21:46:53 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
// Only measure the width for multi-char text divs, see `appendText`.
|
|
|
|
const { width } = this._layoutTextCtx.measureText(textDiv.textContent);
|
2021-04-30 21:41:13 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (width > 0) {
|
2021-09-05 19:12:34 +09:00
|
|
|
const scale = textDivProperties.canvasWidth / width;
|
|
|
|
if (this._enhanceTextSelection) {
|
|
|
|
textDivProperties.scale = scale;
|
|
|
|
}
|
|
|
|
transform = `scaleX(${scale})`;
|
2021-04-30 21:41:13 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
|
|
|
if (textDivProperties.angle !== 0) {
|
|
|
|
transform = `rotate(${textDivProperties.angle}deg) ${transform}`;
|
|
|
|
}
|
|
|
|
if (transform.length > 0) {
|
|
|
|
if (this._enhanceTextSelection) {
|
|
|
|
textDivProperties.originalTransform = transform;
|
2021-04-30 21:41:13 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
textDiv.style.transform = transform;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textDivProperties.hasText) {
|
|
|
|
this._container.appendChild(textDiv);
|
|
|
|
}
|
|
|
|
if (textDivProperties.hasEOL) {
|
|
|
|
const br = document.createElement("br");
|
|
|
|
br.setAttribute("role", "presentation");
|
|
|
|
this._container.appendChild(br);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_render(timeout = 0) {
|
|
|
|
const capability = createPromiseCapability();
|
|
|
|
let styleCache = Object.create(null);
|
2017-04-17 21:46:53 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
// The temporary canvas is used to measure text length in the DOM.
|
|
|
|
const canvas = this._document.createElement("canvas");
|
|
|
|
canvas.height = canvas.width = DEFAULT_FONT_SIZE;
|
2017-04-17 21:46:53 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (
|
|
|
|
typeof PDFJSDev === "undefined" ||
|
|
|
|
PDFJSDev.test("MOZCENTRAL || GENERIC")
|
|
|
|
) {
|
|
|
|
canvas.mozOpaque = true;
|
|
|
|
}
|
|
|
|
this._layoutTextCtx = canvas.getContext("2d", { alpha: false });
|
2021-01-24 02:21:48 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (this._textContent) {
|
|
|
|
const textItems = this._textContent.items;
|
|
|
|
const textStyles = this._textContent.styles;
|
|
|
|
this._processItems(textItems, textStyles);
|
|
|
|
capability.resolve();
|
|
|
|
} else if (this._textContentStream) {
|
|
|
|
const pump = () => {
|
|
|
|
this._reader.read().then(({ value, done }) => {
|
|
|
|
if (done) {
|
|
|
|
capability.resolve();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
Object.assign(styleCache, value.styles);
|
|
|
|
this._processItems(value.items, styleCache);
|
|
|
|
pump();
|
|
|
|
}, capability.reject);
|
|
|
|
};
|
2016-08-17 08:06:35 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
this._reader = this._textContentStream.getReader();
|
|
|
|
pump();
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
2021-07-30 21:13:02 +09:00
|
|
|
'Neither "textContent" nor "textContentStream" parameters specified.'
|
2021-05-05 23:36:18 +09:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
capability.promise.then(() => {
|
|
|
|
styleCache = null;
|
|
|
|
if (!timeout) {
|
|
|
|
// Render right away
|
|
|
|
render(this);
|
|
|
|
} else {
|
|
|
|
// Schedule
|
|
|
|
this._renderTimer = setTimeout(() => {
|
|
|
|
render(this);
|
|
|
|
this._renderTimer = null;
|
|
|
|
}, timeout);
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}, this._capability.reject);
|
|
|
|
}
|
2016-09-03 01:10:37 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
/**
|
|
|
|
* @param {boolean} [expandDivs]
|
|
|
|
*/
|
|
|
|
expandTextDivs(expandDivs = false) {
|
|
|
|
if (!this._enhanceTextSelection || !this._renderingDone) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._bounds !== null) {
|
|
|
|
expand(this);
|
|
|
|
this._bounds = null;
|
|
|
|
}
|
|
|
|
const transformBuf = [],
|
|
|
|
paddingBuf = [];
|
2016-09-03 01:10:37 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
for (let i = 0, ii = this._textDivs.length; i < ii; i++) {
|
|
|
|
const div = this._textDivs[i];
|
|
|
|
const divProps = this._textDivProperties.get(div);
|
2016-09-03 01:10:37 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (!divProps.hasText) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (expandDivs) {
|
|
|
|
transformBuf.length = 0;
|
|
|
|
paddingBuf.length = 0;
|
[EnhanceTextSelection] Make `expandTextDivs` more efficient by updating all styles at once instead of piecewise
I intended to provide proper benchmarking results here, as outlined in https://github.com/mozilla/pdf.js/wiki/Benchmarking-your-changes, but after wasting a couple of hours over the weekend getting weird results I gave up.
It appears that there's a lot of, i.e. way too much, variance between subsequent runs of `text` tests for the results to be meaningful.
(Previously I've only benchmarked `eq` tests, so I don't know if the `text` tests has never worked well or if it's a newer problem. For reference, please see the results of back-to-back benchmark runs on the current `master` with a *very* simple manifest file: [link here].)
Instead I used `console.time/timeEnd` in `appendText` and `expandTextDivs` to be able to compare the performance with/without the patch. The entire viewer was (skip-cache) reloaded between measurements, and the result are available here: [link here].
Given the troubles I've had with benchmarking, I've not yet computed any statistics on the results (e.g. mean, variance, confidence intervals, and so on).
However, just by looking at the data I think it's safe to say that this patch first of all doesn't seem to regress the current performance. Secondly it certainly looks *very* likely that this patch actually improves the performance, especially for the one-glyph-per-text-div case (cf. issue 7224).
Re: issue 7584.
2016-09-14 00:04:57 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
if (divProps.originalTransform) {
|
|
|
|
transformBuf.push(divProps.originalTransform);
|
|
|
|
}
|
|
|
|
if (divProps.paddingTop > 0) {
|
|
|
|
paddingBuf.push(`${divProps.paddingTop}px`);
|
|
|
|
transformBuf.push(`translateY(${-divProps.paddingTop}px)`);
|
2016-09-03 01:10:37 +09:00
|
|
|
} else {
|
2021-05-05 23:36:18 +09:00
|
|
|
paddingBuf.push(0);
|
|
|
|
}
|
|
|
|
if (divProps.paddingRight > 0) {
|
|
|
|
paddingBuf.push(`${divProps.paddingRight / divProps.scale}px`);
|
|
|
|
} else {
|
|
|
|
paddingBuf.push(0);
|
|
|
|
}
|
|
|
|
if (divProps.paddingBottom > 0) {
|
|
|
|
paddingBuf.push(`${divProps.paddingBottom}px`);
|
|
|
|
} else {
|
|
|
|
paddingBuf.push(0);
|
|
|
|
}
|
|
|
|
if (divProps.paddingLeft > 0) {
|
|
|
|
paddingBuf.push(`${divProps.paddingLeft / divProps.scale}px`);
|
|
|
|
transformBuf.push(
|
|
|
|
`translateX(${-divProps.paddingLeft / divProps.scale}px)`
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
paddingBuf.push(0);
|
2016-08-17 08:06:35 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
div.style.padding = paddingBuf.join(" ");
|
|
|
|
if (transformBuf.length) {
|
|
|
|
div.style.transform = transformBuf.join(" ");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
div.style.padding = null;
|
|
|
|
div.style.transform = divProps.originalTransform;
|
|
|
|
}
|
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
}
|
2021-05-05 23:36:18 +09:00
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
2021-05-05 23:36:18 +09:00
|
|
|
/**
|
|
|
|
* @param {TextLayerRenderParameters} renderParameters
|
|
|
|
* @returns {TextLayerRenderTask}
|
|
|
|
*/
|
|
|
|
function renderTextLayer(renderParameters) {
|
|
|
|
const task = new TextLayerRenderTask({
|
|
|
|
textContent: renderParameters.textContent,
|
|
|
|
textContentStream: renderParameters.textContentStream,
|
|
|
|
container: renderParameters.container,
|
|
|
|
viewport: renderParameters.viewport,
|
|
|
|
textDivs: renderParameters.textDivs,
|
|
|
|
textContentItemsStr: renderParameters.textContentItemsStr,
|
|
|
|
enhanceTextSelection: renderParameters.enhanceTextSelection,
|
|
|
|
});
|
|
|
|
task._render(renderParameters.timeout);
|
|
|
|
return task;
|
|
|
|
}
|
2015-11-11 00:45:03 +09:00
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { renderTextLayer };
|