Best JavaScript code snippet using wpt
text_layer_builder.js
Source:text_layer_builder.js
1/**2 * @licstart The following is the entire license notice for the3 * JavaScript code in this page4 *5 * Copyright 2022 Mozilla Foundation6 *7 * Licensed under the Apache License, Version 2.0 (the "License");8 * you may not use this file except in compliance with the License.9 * You may obtain a copy of the License at10 *11 * http://www.apache.org/licenses/LICENSE-2.012 *13 * Unless required by applicable law or agreed to in writing, software14 * distributed under the License is distributed on an "AS IS" BASIS,15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16 * See the License for the specific language governing permissions and17 * limitations under the License.18 *19 * @licend The above is the entire license notice for the20 * JavaScript code in this page21 */22"use strict";23Object.defineProperty(exports, "__esModule", {24 value: true25});26exports.TextLayerBuilder = void 0;27var _pdf = require("../pdf");28const EXPAND_DIVS_TIMEOUT = 300;29class TextLayerBuilder {30 constructor({31 textLayerDiv,32 eventBus,33 pageIndex,34 viewport,35 highlighter = null,36 enhanceTextSelection = false37 }) {38 this.textLayerDiv = textLayerDiv;39 this.eventBus = eventBus;40 this.textContent = null;41 this.textContentItemsStr = [];42 this.textContentStream = null;43 this.renderingDone = false;44 this.pageNumber = pageIndex + 1;45 this.viewport = viewport;46 this.textDivs = [];47 this.textLayerRenderTask = null;48 this.highlighter = highlighter;49 this.enhanceTextSelection = enhanceTextSelection;50 this._bindMouse();51 }52 _finishRendering() {53 this.renderingDone = true;54 if (!this.enhanceTextSelection) {55 const endOfContent = document.createElement("div");56 endOfContent.className = "endOfContent";57 this.textLayerDiv.append(endOfContent);58 }59 this.eventBus.dispatch("textlayerrendered", {60 source: this,61 pageNumber: this.pageNumber,62 numTextDivs: this.textDivs.length63 });64 }65 render(timeout = 0) {66 if (!(this.textContent || this.textContentStream) || this.renderingDone) {67 return;68 }69 this.cancel();70 this.textDivs.length = 0;71 this.highlighter?.setTextMapping(this.textDivs, this.textContentItemsStr);72 const textLayerFrag = document.createDocumentFragment();73 this.textLayerRenderTask = (0, _pdf.renderTextLayer)({74 textContent: this.textContent,75 textContentStream: this.textContentStream,76 container: textLayerFrag,77 viewport: this.viewport,78 textDivs: this.textDivs,79 textContentItemsStr: this.textContentItemsStr,80 timeout,81 enhanceTextSelection: this.enhanceTextSelection82 });83 this.textLayerRenderTask.promise.then(() => {84 this.textLayerDiv.append(textLayerFrag);85 this._finishRendering();86 this.highlighter?.enable();87 }, function (reason) {});88 }89 cancel() {90 if (this.textLayerRenderTask) {91 this.textLayerRenderTask.cancel();92 this.textLayerRenderTask = null;93 }94 this.highlighter?.disable();95 }96 setTextContentStream(readableStream) {97 this.cancel();98 this.textContentStream = readableStream;99 }100 setTextContent(textContent) {101 this.cancel();102 this.textContent = textContent;103 }104 _bindMouse() {105 const div = this.textLayerDiv;106 let expandDivsTimer = null;107 div.addEventListener("mousedown", evt => {108 if (this.enhanceTextSelection && this.textLayerRenderTask) {109 this.textLayerRenderTask.expandTextDivs(true);110 if (expandDivsTimer) {111 clearTimeout(expandDivsTimer);112 expandDivsTimer = null;113 }114 return;115 }116 const end = div.querySelector(".endOfContent");117 if (!end) {118 return;119 }120 let adjustTop = evt.target !== div;121 adjustTop = adjustTop && window.getComputedStyle(end).getPropertyValue("-moz-user-select") !== "none";122 if (adjustTop) {123 const divBounds = div.getBoundingClientRect();124 const r = Math.max(0, (evt.pageY - divBounds.top) / divBounds.height);125 end.style.top = (r * 100).toFixed(2) + "%";126 }127 end.classList.add("active");128 });129 div.addEventListener("mouseup", () => {130 if (this.enhanceTextSelection && this.textLayerRenderTask) {131 expandDivsTimer = setTimeout(() => {132 if (this.textLayerRenderTask) {133 this.textLayerRenderTask.expandTextDivs(false);134 }135 expandDivsTimer = null;136 }, EXPAND_DIVS_TIMEOUT);137 return;138 }139 const end = div.querySelector(".endOfContent");140 if (!end) {141 return;142 }143 end.style.top = "";144 end.classList.remove("active");145 });146 }147}...
pdfjs-text-layer.component.ts
Source:pdfjs-text-layer.component.ts
...38 }39 ngOnInit() {40 }41 ngOnDestroy() {42 this.cancelTextLayerRenderTask();43 }44 private cancelTextLayerRenderTask() {45 if (!!this.textLayerRenderTask && this.textLayerRenderTask.cancel) {46 this.textLayerRenderTask.cancel();47 }48 }49 private writeTextLayer() {50 this.cancelTextLayerRenderTask();51 if (!!this.viewport && !!this.pdfPageProxy) {52 const container = this.document.createDocumentFragment();53 this.pdfPageProxy.getTextContent().then((textContent: TextContent) => {54 this.textLayerRenderTask = this.API.renderTextLayer({55 textContent,56 container,57 viewport: this.viewport58 });59 this.textLayerRenderTask.promise.then(() => {60 this.clear();61 (this.elementRef.nativeElement as HTMLElement).insertBefore(container, this.elementRef.nativeElement.firstChild);62 }, (error: any) => {63 console.log('error', error);64 if (error.name !== 'RenderingCancelledException') {...
Using AI Code Generation
1var task = new TextLayerRenderTask({2 container: document.getElementById('textLayer'),3 viewport: pdfPage.getViewport(scale),4});5task.promise.then(function () {6 console.log('Text rendered');7});
Using AI Code Generation
1var textLayer = new TextLayerBuilder({2 textLayerDiv: document.getElementById('textLayer'),3 viewport: pdfPage.getViewport(1.0)4});5pdfPage.getTextContent().then(function(textContent) {6 textLayer.setTextContent(textContent);7 textLayer.render();8});9var TextLayerBuilder = function TextLayerBuilder(options) {10 this.textLayerDiv = options.textLayerDiv;11 this.layoutDone = false;12 this.divContentDone = false;13 this.pageIdx = options.pageIndex;14 this.pageNumber = this.pageIdx + 1;15 this.matches = [];16 this.viewport = options.viewport;17 this.textDivs = [];18};19TextLayerBuilder.prototype = {20 setTextContent: function TextLayerBuilder_setTextContent(textContent) {21 this.textContent = textContent;22 this.render(TEXT_LAYER_RENDER_DELAY);23 },24 render: function TextLayerBuilder_render(timeout) {25 var self = this;26 if (!timeout) {27 clearTimeout(this.renderTimer);28 this.renderTimer = null;29 this._render();30 } else {31 if (!this.renderTimer) {32 this.renderTimer = setTimeout(function() {33 self.renderTimer = null;34 self._render();35 }, timeout);36 }37 }38 },39 _render: function TextLayerBuilder_render() {40 var textLayerFrag = document.createDocumentFragment();41 this.textDivs = [];42 var textItems = this.textContent.items;43 var textDivs = this.textDivs;44 var viewport = this.viewport;45 for (var i = 0, len = textItems.length; i < len; i++) {46 var textItem = textItems[i];47 var transform = viewport.transform;48 var tx = PDFJS.Util.transform(49 [textItem.transform[4], textItem.transform[5], 1]);50 var style = 'position: absolute; left: ' + tx[0] + 'px; top: ' +51 tx[1] + 'px; font-size: ' + textItem.transform[0] + 'px;';52 var textDiv = document.createElement('div');53 textDiv.setAttribute('style', style);54 textDiv.textContent = textItem.str;55 textLayerFrag.appendChild(textDiv);56 textDivs.push(textDiv);57 }
Using AI Code Generation
1var textLayerDiv = document.getElementById('textLayer');2var textLayer = new TextLayerBuilder({3});4pdfPage.getTextContent().then(function(textContent) {5 textLayer.setTextContent(textContent);6 textLayer.render();7});8var TextLayerBuilder = function TextLayerBuilder(options) {9 this.textLayerDiv = options.textLayerDiv;10 this.textContent = null;11 this.renderingDone = false;12 this.pageIdx = options.pageIndex;13 this.pageNumber = this.pageIdx + 1;14 this.matches = [];15 this.viewport = options.viewport;16 this.textDivs = [];17};18TextLayerBuilder.prototype = {19 setTextContent: function TextLayerBuilder_setTextContent(textContent) {20 this.textContent = textContent;21 },22 render: function TextLayerBuilder_render(timeout) {23 var textLayerFrag = document.createDocumentFragment();24 this.textDivs = [];25 var textItems = this.textContent.items;26 var textDivs = this.textDivs;27 var viewport = this.viewport;28 for (var i = 0, len = textItems.length; i < len; i++) {29 var textItem = textItems[i];30 var transform = viewport.transform;31 var div = document.createElement('div');32 div.setAttribute('dir', textItem.dir);33 this.textDivs.push(div);34 var tx = PDFJS.Util.transform(35 [textItem.transform[4], textItem.transform[5], 0]);36 PDFJS.Util.applyStyles(div, {37 transform: PDFJS.Util.transform(38 left: Math.floor(tx[0]) + 'px',39 top: Math.floor(tx[1]) + 'px',40 fontSize: Math.floor(textItem.transform[3] * 100) + '%'41 });42 if (textItem.fontName !== PDFJS.FontInspector.getSerifFontName()) {43 div.style.fontFamily = textItem.fontName;44 }45 div.textContent = textItem.str;46 textLayerFrag.appendChild(div);47 }48 this.textLayerDiv.appendChild(textLayerFrag);49 this.renderingDone = true;50 }51};
Using AI Code Generation
1var task = new TextLayerRenderTask({2 container: document.getElementById("div"),3});4task.promise.then(function () {5 console.log("done");6});7var TextLayerRenderTask = (function TextLayerRenderTaskClosure() {8 function TextLayerRenderTask(textLayerRenderParameters) {9 this._textContent = textLayerRenderParameters.textContent;10 this._container = textLayerRenderParameters.container;11 this._viewport = textLayerRenderParameters.viewport;12 this._textDivs = textLayerRenderParameters.textDivs;13 this._enhanceTextSelection = textLayerRenderParameters.enhanceTextSelection;14 }15 TextLayerRenderTask.prototype = {16 promise: function TextLayerRenderTask_promise() {17 return Promise.resolve();18 }19 };20 return TextLayerRenderTask;21})();22 canvas = document.getElementById('the-canvas'),23 ctx = canvas.getContext('2d');24pdfjsLib.getDocument(url).promise.then(function(pdfDoc_) {25 pdfDoc = pdfDoc_;26 renderPage(pageNum);27});28function renderPage(num) {29 pageRendering = true;30 pdfDoc.getPage(num).then(function(page) {31 var viewport = page.getViewport({scale: scale});32 canvas.height = viewport.height;33 canvas.width = viewport.width;34 var renderContext = {35 };36 var renderTask = page.render(renderContext);37 renderTask.promise.then(function () {38 pageRendering = false;39 if (pageNumPending !== null) {40 renderPage(pageNumPending);41 pageNumPending = null;42 }43 });44 });45 pdfDoc.getPage(num).then(function(page) {46 var textContent = page.getTextContent();47 textContent.then(function(text){48 console.log(text);49 });50 });51}
Using AI Code Generation
1var pdfjsLib = require('pdfjs-dist');2pdfjsLib.getDocument('file.pdf').then(function(pdf) {3 pdf.getPage(1).then(function(page) {4 var viewport = page.getViewport(1.0);5 var textContent = page.getTextContent();6 textContent.then(function(textContent) {7 var textLayer = new TextLayerBuilder({8 textLayerDiv: document.getElementById("textLayer"),9 });10 textLayer.setTextContent(textContent);11 textLayer.render();12 });13 });14});15var pdfjsLib = require('pdfjs-dist');16pdfjsLib.getDocument('file.pdf').then(function(pdf) {17 pdf.getPage(1).then(function(page) {18 var viewport = page.getViewport(1.0);19 var textContent = page.getTextContent();20 textContent.then(function(textContent) {21 var textLayer = new TextLayerBuilder({22 textLayerDiv: document.getElementById("textLayer"),23 });24 textLayer.setTextContent(textContent);25 textLayer.render();26 });27 });28});29 at TextLayerBuilder.setTextContent (C:\Users\user\Documents\pdfjs\wptextlayer.js:72:27)
Using AI Code Generation
1var pdfjsLib = require('pdfjs-dist');2var fs = require('fs');3var loadingTask = pdfjsLib.getDocument(pdfPath);4loadingTask.promise.then(function(pdf) {5 console.log('PDF loaded');6 var pageNumber = 1;7 pdf.getPage(pageNumber).then(function(page) {8 console.log('Page loaded');9 var viewport = page.getViewport(1.0);10 var canvas = document.createElement('canvas');11 var context = canvas.getContext('2d');12 canvas.height = viewport.height;13 canvas.width = viewport.width;14 var renderContext = {15 };16 var renderTask = page.render(renderContext);17 renderTask.promise.then(function () {18 console.log('Page rendered');19 var task = page.getTextContent();20 task.then(function (textContent) {21 console.log('Text content loaded');22 var textLayerDiv = document.getElementById('textLayer');23 var textLayer = new TextLayerBuilder({24 });25 textLayer.setTextContent(textContent);26 textLayer.render();27 });28 });29 });30}, function (reason) {31 console.error(reason);32});33'use strict';34var TextLayerRenderTask = (function TextLayerRenderTaskClosure() {35 function TextLayerRenderTask(textLayer) {36 this._textLayer = textLayer;37 this._renderingDone = false;38 this._capability = createPromiseCapability();39 this._renderTimer = null;40 this._renderingTimer = null;41 this._resumeTimer = null;42 }43 TextLayerRenderTask.prototype = {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!