UNPKG

@syncfusion/ej2-pdfviewer

Version:
811 lines 68.8 kB
/** *@returns {void} */ export function PdfiumRunner() { var moduleString = 'Module'; var pageLoaded = false; var moduleLoaded = false; var FPDF = {}; // eslint-disable-next-line var pdfiumWindow = pdfiumWindow ? pdfiumWindow : {}; var documentDetails; var PDFiumModule = typeof (pdfiumWindow["" + moduleString]) !== 'undefined' ? (pdfiumWindow["" + moduleString]) : {}; var F64 = Float64Array; var H = function (t, s, d) { return function (f) { var _a = pdfiumWindow.heap(t, s), m = _a[0], a = _a.slice(1); var v = f.apply(void 0, a.map(function (x) { return x.p; })); if (!v) { m.free(); return d; } var r = a.map(function (x) { return x.v; }); m.free(); return r; }; }; Object.assign(FPDF, { LCD_TEXT: 0x02, NO_NATIVETEXT: 0x04, GRAYSCALE: 0x08, DEBUG_INFO: 0x80, NO_CATCH: 0x100, RENDER_LIMITEDIMAGECACHE: 0x200, RENDER_FORCEHALFTONE: 0x400, PRINTING: 0x800, REVERSE_BYTE_ORDER: 0x10, // eslint-disable-next-line Bitmap_Gray: 1, // eslint-disable-next-line Bitmap_BGR: 2, // eslint-disable-next-line Bitmap_BGRx: 3, // eslint-disable-next-line Bitmap_BGRA: 4, LAST_ERROR: { SUCCESS: 0, UNKNOWN: 1, FILE: 2, FORMAT: 3, PASSWORD: 4, SECURITY: 5, PAGE: 6 } }); /** *@returns {void} */ function initializeFPDF() { FPDF.Init = PDFiumModule.cwrap('FPDF_InitLibrary'); FPDF.RenderPageBitmap = PDFiumModule.cwrap('FPDF_RenderPageBitmap', '', ['number', 'number', 'number', 'number', 'number', 'number', 'number', 'number']); // eslint-disable-next-line FPDF.Bitmap_FillRect = PDFiumModule.cwrap('FPDFBitmap_FillRect', '', ['number', 'number', 'number', 'number', 'number', 'number']); // eslint-disable-next-line FPDF.Bitmap_CreateEx = PDFiumModule.cwrap('FPDFBitmap_CreateEx', 'number', ['number', 'number', 'number', 'number', 'number']); // eslint-disable-next-line FPDF.Bitmap_Destroy = PDFiumModule.cwrap('FPDFBitmap_Destroy', '', ['number']); FPDF.LoadPage = PDFiumModule.cwrap('FPDF_LoadPage', 'number', ['number', 'number']); FPDF.ClosePage = PDFiumModule.cwrap('FPDF_ClosePage', '', ['number']); FPDF.LoadMemDocument = PDFiumModule.cwrap('FPDF_LoadMemDocument', 'number', ['number', 'number', 'string']); FPDF.GetPageSizeByIndex = PDFiumModule.cwrap('FPDF_GetPageSizeByIndex', 'number', ['number', 'number', 'number', 'number']); FPDF.GetLastError = PDFiumModule.cwrap('FPDF_GetLastError', 'number'); FPDF.GetPageCount = PDFiumModule.cwrap('FPDF_GetPageCount', 'number', ['number']); FPDF.CloseDocument = PDFiumModule.cwrap('FPDF_CloseDocument', '', ['number']); FPDF.DestroyLibrary = PDFiumModule.cwrap('FPDF_DestroyLibrary'); FPDF.LoadTextPage = PDFiumModule.cwrap('FPDFText_LoadPage', 'number', ['number']); FPDF.CloseTextPage = PDFiumModule.cwrap('FPDFText_ClosePage', '', ['number']); FPDF.TextCountChars = PDFiumModule.cwrap('FPDFText_CountChars', 'number', ['number']); FPDF.GetUnicodeChar = PDFiumModule.cwrap('FPDFText_GetUnicode', 'number', ['number']); FPDF.GetCharBox = PDFiumModule.cwrap('FPDFText_GetCharBox', 'number', ['number', 'number', 'number', 'number', 'number']); FPDF.GetPageRotation = PDFiumModule.cwrap('FPDFPage_GetRotation', 'number', ['number']); FPDF.GetCharAngle = PDFiumModule.cwrap('FPDFText_GetCharAngle', 'number', ['number']); FPDF.GetPageHeight = PDFiumModule.cwrap('FPDF_GetPageHeight', 'number', ['number']); FPDF.GetPageWidth = PDFiumModule.cwrap('FPDF_GetPageWidth', 'number', ['number']); pdfiumWindow.heap = function (J, s) { var E; switch (J) { case Int8Array: E = PDFiumModule.HEAP8; break; case Int16Array: E = PDFiumModule.HEAP16; break; case Int32Array: E = PDFiumModule.HEAP32; break; case Uint8Array: E = PDFiumModule.HEAPU8; break; case Uint16Array: E = PDFiumModule.HEAPU16; break; case Uint32Array: E = PDFiumModule.HEAPU32; break; case Float32Array: E = PDFiumModule.HEAPF32; break; case Float64Array: E = PDFiumModule.HEAPF64; break; } var Z = J.BYTES_PER_ELEMENT; var m = PDFiumModule.asm.malloc(s * Z); var a = Array(1 + s); a[0] = ({ s: s, J: J, Z: Z, E: E, m: m, free: function () { return PDFiumModule.asm.free(m); } }); var _loop_1 = function (i) { a[i + 1] = ({ p: m + (i * Z), get v() { return E[m / Z + i]; } }); }; for (var i = 0; i < s; i++) { _loop_1(i); } return a; }; } /** *@returns {void} */ function checkIfEverythingWasLoaded() { pageLoaded = true; if (pageLoaded || moduleLoaded) { startApp(); } } PDFiumModule.onRuntimeInitialized = function () { moduleLoaded = true; checkIfEverythingWasLoaded(); }; /** *@returns {void} */ function startApp() { initializeFPDF(); if (pdfiumWindow.loaded) { pdfiumWindow.loaded(); } } pdfiumWindow.onload = function () { pageLoaded = true; checkIfEverythingWasLoaded(); }; pdfiumWindow.loaded = function () { ctx.postMessage({ message: 'loaded' }); }; var ctx = self; ctx.onmessage = function (event) { if (event.data.message === 'initialLoading') { importScripts(event.data.url + '/pdfium.js'); PDFiumModule.url = event.data.url; PDFiumModule.onRuntimeInitialized = function () { moduleLoaded = true; checkIfEverythingWasLoaded(); if (event.data.fonts && Object.keys(event.data.fonts).length > 0) { var filePath = '/usr/share/fonts/'; PDFiumModule.FS.createPath('/', filePath, true, true); for (var key in event.data.fonts) { if (event.data.fonts["" + key] && key.indexOf('fallbackfonts') === -1) { PDFiumModule.FS.createDataFile(filePath + key, null, event.data.fonts["" + key], true, true, true); } } } }; this['PDFiumModule'](PDFiumModule); } else if (event.data.message === 'LoadPageCollection') { pdfiumWindow.fileByteArray = event.data.uploadedFile; var fileSize = pdfiumWindow.fileByteArray.length; FPDF.Init(); var wasmBuffer = PDFiumModule.asm.malloc(fileSize); PDFiumModule.HEAPU8.set(pdfiumWindow.fileByteArray, wasmBuffer); pdfiumWindow.fileByteArray = null; documentDetails = new DocumentInfo({ wasm: FPDF.LoadMemDocument(wasmBuffer, fileSize, event.data.password), wasmBuffer: wasmBuffer }); var pages = FPDF.GetPageCount(documentDetails.processor.wasmData.wasm); documentDetails.setPages(pages); documentDetails.createAllPages(); ctx.postMessage({ message: 'PageLoaded', pageIndex: event.data.pageIndex, isZoomMode: event.data.isZoomMode }); } else if (event.data.message === 'LoadPageStampCollection') { var fileSize = event.data.uploadedFile.length; FPDF.Init(); var wasmBuffer = PDFiumModule.asm.malloc(fileSize); PDFiumModule.HEAPU8.set(event.data.uploadedFile, wasmBuffer); var documentDetailsNew = new DocumentInfo({ wasm: FPDF.LoadMemDocument(wasmBuffer, fileSize, event.data.password), wasmBuffer: wasmBuffer }); var pages = FPDF.GetPageCount(documentDetailsNew.processor.wasmData.wasm); documentDetailsNew.setPages(pages); documentDetailsNew.createAllPages(); var firstPage = documentDetailsNew.getPage(event.data.pageIndex); var ImageData_1 = event.data; var data = firstPage.render(null, ImageData_1.zoomFactor, false, null, null, null, true); data.message = 'LoadedStamp'; data.annotName = event.data.AnnotName; data.rubberStampAnnotationPageNumber = event.data.rubberStampAnnotationPageNumber; data.annotationOrder = event.data.annotationOrder; data.collectionOrder = event.data.collectionOrder; data.isFormField = event.data.isFormField; if (data.isFormField) { data.message = 'LoadedStampForFormFields'; data.formFieldName = event.data.formFieldName; data.formFieldList = event.data.formFieldList; data.formFields = event.data.rubberStampAnnotation; data.PageIndex = event.data.PageIndex; } ctx.postMessage(data); } if (documentDetails) { if (event.data.message === 'renderPage') { var firstPage = documentDetails.getPage(event.data.pageIndex); var ImageData_2 = event.data; var data = firstPage.render(null, ImageData_2.zoomFactor, ImageData_2.isTextNeed, null, null, ImageData_2.textDetailsId, null, event.data.cropBoxRect, event.data.mediaBoxRect); ctx.postMessage(data); } else if (event.data.message === 'renderPageSearch') { var firstPage = documentDetails.getPage(event.data.pageIndex); var ImageData_3 = event.data; var data = firstPage.render(null, ImageData_3.zoomFactor, ImageData_3.isTextNeed, null, null, ImageData_3.textDetailsId, null, event.data.cropBoxRect); data.message = 'imageRenderedSearch'; ctx.postMessage(data); } else if (event.data.message === 'extractText') { var firstPage = documentDetails.getPage(event.data.pageIndex); var ImageData_4 = event.data; var data = firstPage.render('extractText', ImageData_4.zoomFactor, ImageData_4.isTextNeed, null, null, ImageData_4.textDetailsId); data.message = 'textExtracted'; data.isLayout = event.data.isLayout; data.isRenderText = event.data.isRenderText; data.jsonObject = event.data.jsonObject; data.requestType = event.data.requestType; data.annotationObject = event.data.annotationObject; ctx.postMessage(data); } else if (event.data.message === 'renderThumbnail') { // eslint-disable-next-line var thumbnail = new Promise(function (resolve, reject) { try { var firstPage = documentDetails.getPage(event.data.pageIndex); if (firstPage.processor !== null && firstPage.processor !== undefined) { var data = firstPage.render('thumbnail', null, false, null, null); resolve(data); } } catch (error) { reject(error); } }); thumbnail.then(function (results) { ctx.postMessage(results); }); } else if (event.data.message === 'renderPreviewTileImage') { var firstPage = documentDetails.getPage(event.data.pageIndex); var data = firstPage.render('thumbnail', null, false, null, null); data.message = 'renderPreviewTileImage'; data.startIndex = event.data.startIndex; data.endIndex = event.data.endIndex; ctx.postMessage(data); } else if (event.data.message === 'printImage') { var firstPage = documentDetails.getPage(event.data.pageIndex); var data = firstPage.render('print', null, false, event.data.printScaleFactor, event.data.printDevicePixelRatio); ctx.postMessage(data); } else if (event.data.message === 'extractImage') { var firstPage = documentDetails.getPage(event.data.pageIndex); var ImageData_5 = event.data; var data = firstPage.render(null, ImageData_5.zoomFactor, ImageData_5.isTextNeed, null, null, ImageData_5.textDetailsId, null, null, null, event.data.size); data.message = 'imageExtracted'; ctx.postMessage(data); } else if (event.data.message === 'renderImageAsTile') { var values = event.data; var firstPage = documentDetails.getPage(event.data.pageIndex); var data = firstPage.renderTileImage(values.tileX, values.tileY, values.tileXCount, values.tileYCount, values.zoomFactor, event.data.isTextNeed, event.data.textDetailsId, event.data.cropBoxRect, event.data.mediaBoxRect); ctx.postMessage(data); } else if (event.data.message === 'renderImageAsTileSearch') { var values = event.data; var firstPage = documentDetails.getPage(event.data.pageIndex); var data = firstPage.renderTileImage(values.tileX, values.tileY, values.tileXCount, values.tileYCount, values.zoomFactor, event.data.isTextNeed, event.data.textDetailsId, event.data.cropBoxRect, event.data.mediaBoxRect); data.message = 'renderTileImageSearch'; ctx.postMessage(data); } else if (event.data.message === 'unloadFPDF') { if (documentDetails) { PDFiumModule.asm.free(documentDetails.processor.wasmData.wasmBuffer); FPDF.CloseDocument(documentDetails.processor.wasmData.wasm); FPDF.DestroyLibrary(); } } } if (event.data.message === 'unloadFPDF') { ctx.postMessage({ message: '' }); } }; var Page = /** @class */ (function () { function Page(index, processor) { this.index = index; this.src = null; this.processor = processor; } Page.prototype.render = function (message, zoomFactor, isTextNeed, printScaleFactor, printDevicePixelRatio, textDetailsId, isTransparent, cropBoxRect, mediaBoxRect, size) { return this.processor.render(this.index, message, zoomFactor, isTextNeed, printScaleFactor, printDevicePixelRatio, textDetailsId, isTransparent, cropBoxRect, mediaBoxRect, size); }; Page.prototype.renderTileImage = function (x, y, tileX, tileY, zoomFactor, isTextNeed, textDetailsId, cropBoxRect, mediaBoxRect) { return this.processor.renderTileImage(this.index, x, y, tileX, tileY, zoomFactor, isTextNeed, textDetailsId, cropBoxRect, mediaBoxRect); }; return Page; }()); var RectAngle = /** @class */ (function () { function RectAngle(X, Y, Width, Height, Text, Rotation) { this.X = X; this.Y = Y; this.Width = Width; this.Height = Height; this.Bottom = this.Y + this.Height; this.Right = this.X + this.Width; this.Top = this.Y; this.Left = this.X; this.Rotation = Rotation; this.Text = Text; } return RectAngle; }()); var Processor = /** @class */ (function () { function Processor(wasmData) { this.TextBounds = []; this.TextContent = []; this.CharacterBounds = []; this.PageText = ''; this.wasmData = wasmData; } Processor.prototype.getPageSize = function (i) { var _this = this; if (i === void 0) { i = 0; } return H(F64, 2, [-1, -1])(function (w, h) { return FPDF.GetPageSizeByIndex(_this.wasmData.wasm, i, w, h); }). map(function (v) { return parseInt((v * (96 / 72)).toString(), 10); }); }; Processor.prototype.getCharBounds = function (pagePointer, i) { if (i === void 0) { i = 0; } return H(F64, 4, [-1, -1, -1, -1])(function (left, right, bottom, top) { return FPDF. GetCharBox(pagePointer, i, left, right, bottom, top); }); }; Processor.prototype.getRender = function (i, w, h, isTextNeed, isTransparent, cropBoxRect, mediaBoxRect) { if (i === void 0) { i = 0; } var flag = FPDF.REVERSE_BYTE_ORDER; var heap = PDFiumModule.asm.malloc(w * h * 4); PDFiumModule.HEAPU8.fill(0, heap, heap + (w * h * 4)); var bmap = FPDF.Bitmap_CreateEx(w, h, FPDF.Bitmap_BGRA, heap, w * 4); var page = FPDF.LoadPage(this.wasmData.wasm, i); FPDF.Bitmap_FillRect(bmap, 0, 0, w, h, isTransparent ? 0x00FFFFFF : 0xFFFFFFFF); FPDF.RenderPageBitmap(bmap, page, 0, 0, w, h, 0, flag); FPDF.Bitmap_Destroy(bmap); this.textExtraction(page, i, isTextNeed, cropBoxRect, mediaBoxRect); FPDF.ClosePage(page); return heap; }; Processor.prototype.textExtraction = function (pagePointer, pageIndex, isTextNeed, cropBoxRect, mediaBoxRect) { var _a; if (isTextNeed) { // eslint-disable-next-line var _b = this.getPageSize(pageIndex), pageWidth = _b[0], pageHeight = _b[1]; pageHeight = pageHeight + this.pointerToPixelConverter(mediaBoxRect && mediaBoxRect.y ? mediaBoxRect.y : 0); var textPage = FPDF.LoadTextPage(pagePointer, pageIndex); var pageRotation = FPDF.GetPageRotation(pagePointer); var totalCharacterCount = FPDF.TextCountChars(textPage); this.TextBounds = []; this.TextContent = []; this.CharacterBounds = []; var pageText = ''; var minTop = 0; var maxBottom = 0; var minLeft = 0; var maxRight = 0; var top_1 = []; var bottom = []; var left = []; var right = []; var wordBounds = []; var word = ''; var wordMinLeft = 0; var wordMaxRight = 0; var wordMinTop = 0; var wordMaxBottom = 0; var wordRotation = 0; var wordStart = true; var isZeroWidthSpace = false; var isPreviousSpace = false; var startNewLine = false; var maximumSpaceForNewLine = 11; for (var charCount = 0; charCount <= totalCharacterCount; charCount++) { var result = FPDF.GetUnicodeChar(textPage, charCount); var rotationRadian = FPDF.GetCharAngle(textPage, charCount); var character = String.fromCharCode(result); var _c = this.getCharBounds(textPage, charCount), charLeft = _c[0], charRight = _c[1], charBottom = _c[2], charTop = _c[3]; var X = this.pointerToPixelConverter(charLeft) - this.pointerToPixelConverter(cropBoxRect && cropBoxRect.x ? cropBoxRect.x : 0); var Y = (pageHeight + this.pointerToPixelConverter(cropBoxRect && cropBoxRect.y ? cropBoxRect.y : 0)) - this.pointerToPixelConverter(charTop); var Width = this.pointerToPixelConverter(charRight - charLeft); var Height = this.pointerToPixelConverter(charTop - charBottom); var rotationAngle = parseInt((rotationRadian * 180 / Math.PI).toString(), 10); if (charCount < totalCharacterCount) { pageText += character; var currentCharacterBounds = new RectAngle(X, Y, Width, Height, character, rotationAngle); this.CharacterBounds.push(currentCharacterBounds); } if (pageRotation === 1 || pageRotation === 3) { Y = (pageWidth) - this.pointerToPixelConverter(charTop); } switch (character) { case '\0': { // eslint-disable-next-line minTop = Math.min.apply(Math, top_1); // eslint-disable-next-line maxBottom = Math.max.apply(Math, bottom); // eslint-disable-next-line minLeft = Math.min.apply(Math, left); // eslint-disable-next-line maxRight = Math.max.apply(Math, right); var newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); this.textBoundsCalculation(wordBounds, minTop, maxBottom, maxRight, minLeft, pageRotation, pageWidth, pageHeight); wordBounds = []; wordStart = true; isPreviousSpace = false; word = ''; top_1 = []; left = []; bottom = []; right = []; minTop = 0; maxBottom = 0; minLeft = 0; maxRight = 0; break; } case '\r': if (charCount < totalCharacterCount) { var characterBounds = new RectAngle(X, Y, Width, Height, '\r\n', rotationAngle); top_1.push(characterBounds.Top); bottom.push(characterBounds.Bottom); left.push(characterBounds.Left); right.push(characterBounds.Right); // eslint-disable-next-line minTop = Math.min.apply(Math, top_1); // eslint-disable-next-line maxBottom = Math.max.apply(Math, bottom); // eslint-disable-next-line minLeft = Math.min.apply(Math, left); // eslint-disable-next-line maxRight = Math.max.apply(Math, right); var newWordBounds = void 0; if (wordStart === false) { newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); } wordBounds.push(characterBounds); this.textBoundsCalculation(wordBounds, minTop, maxBottom, maxRight, minLeft, pageRotation, pageWidth, pageHeight); wordBounds = []; wordStart = true; isPreviousSpace = false; word = ''; top_1 = []; left = []; bottom = []; right = []; minTop = 0; maxBottom = 0; minLeft = 0; maxRight = 0; pageText += '\n'; rotationRadian = FPDF.GetCharAngle(textPage, charCount); _a = this.getCharBounds(textPage, charCount), charLeft = _a[0], charRight = _a[1], charBottom = _a[2], charTop = _a[3]; X = this.pointerToPixelConverter(charLeft); Y = (pageHeight) - this.pointerToPixelConverter(charTop); Width = this.pointerToPixelConverter(charRight - charLeft); Height = this.pointerToPixelConverter(charTop - charBottom); rotationAngle = parseInt((rotationRadian * 180 / Math.PI).toString(), 10); var currentCharacterBounds = new RectAngle(X, Y, Width, Height, character, rotationAngle); this.CharacterBounds.push(currentCharacterBounds); charCount++; } break; case '\u0002': case '\ufffe': { var characterBounds = new RectAngle(X, Y, Width, Height, character, rotationAngle); top_1.push(characterBounds.Top); bottom.push(characterBounds.Bottom); left.push(characterBounds.Left); right.push(characterBounds.Right); // eslint-disable-next-line minTop = Math.min.apply(Math, top_1); // eslint-disable-next-line maxBottom = Math.max.apply(Math, bottom); // eslint-disable-next-line minLeft = Math.min.apply(Math, left); // eslint-disable-next-line maxRight = Math.max.apply(Math, right); var newWordBounds = void 0; if (wordStart === false) { newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); } if (character === '\u0002') { wordBounds.push(characterBounds); } this.textBoundsCalculation(wordBounds, minTop, maxBottom, maxRight, minLeft, pageRotation, pageWidth, pageHeight); wordBounds = []; wordStart = true; isPreviousSpace = false; word = ''; top_1 = []; left = []; bottom = []; right = []; minTop = 0; maxBottom = 0; minLeft = 0; maxRight = 0; } break; default: if (Width === 0 || Height === 0) { isZeroWidthSpace = true; // eslint-disable-next-line minTop = Math.min.apply(Math, top_1); // eslint-disable-next-line maxBottom = Math.max.apply(Math, bottom); // eslint-disable-next-line minLeft = Math.min.apply(Math, left); // eslint-disable-next-line maxRight = Math.max.apply(Math, right); var newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); var characterBounds = new RectAngle(X, Y, Width, Height, character, rotationAngle); wordMinTop = characterBounds.Top; wordMaxBottom = characterBounds.Bottom; wordMinLeft = characterBounds.Left; wordMaxRight = characterBounds.Right; word = character; wordRotation = wordBounds[wordBounds.length - 1].Rotation; newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); wordMinTop = 0; wordMaxBottom = 0; wordMinLeft = 0; wordMaxRight = 0; word = ''; wordRotation = 0; wordStart = true; isPreviousSpace = true; } else { if (wordStart === true) { wordMinTop = Y; wordMaxBottom = Y + Height; wordMinLeft = X; wordMaxRight = X + Width; } var characterBounds = new RectAngle(X, Y, Width, Height, character, rotationAngle); if (character !== ' ') { if (isPreviousSpace && wordBounds.length > 0 && (rotationAngle === wordBounds[0].Rotation)) { if ((rotationAngle === 180 || rotationAngle === 0) && (Math.abs(characterBounds.Y - wordBounds[0].Y) > maximumSpaceForNewLine)) { startNewLine = true; } if ((rotationAngle === 270 || rotationAngle === 90) && (Math.abs(characterBounds.X - wordBounds[0].X) > maximumSpaceForNewLine)) { startNewLine = true; } } if ((isZeroWidthSpace && wordBounds.length >= 1 && wordBounds[wordBounds.length - 1].Rotation !== characterBounds.Rotation) || startNewLine) { isZeroWidthSpace = false; startNewLine = false; // eslint-disable-next-line minTop = Math.min.apply(Math, top_1); // eslint-disable-next-line maxBottom = Math.max.apply(Math, bottom); // eslint-disable-next-line minLeft = Math.min.apply(Math, left); // eslint-disable-next-line maxRight = Math.max.apply(Math, right); var newWordBounds = void 0; if (wordStart === false) { newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); } this.textBoundsCalculation(wordBounds, minTop, maxBottom, maxRight, minLeft, pageRotation, pageWidth, pageHeight); wordBounds = []; wordStart = true; word = ''; top_1 = []; left = []; bottom = []; right = []; minTop = 0; maxBottom = 0; minLeft = 0; maxRight = 0; } top_1.push(characterBounds.Top); bottom.push(characterBounds.Bottom); left.push(characterBounds.Left); right.push(characterBounds.Right); wordMinTop = Math.min(wordMinTop, characterBounds.Top); wordMaxBottom = Math.max(wordMaxBottom, characterBounds.Bottom); wordMinLeft = Math.min(wordMinLeft, characterBounds.Left); wordMaxRight = Math.max(wordMaxRight, characterBounds.Right); word += character; wordRotation = characterBounds.Rotation; wordStart = false; isPreviousSpace = false; } else { var newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); wordMinTop = characterBounds.Top; wordMaxBottom = characterBounds.Bottom; wordMinLeft = characterBounds.Left; wordMaxRight = characterBounds.Right; word = character; wordRotation = characterBounds.Rotation; newWordBounds = new RectAngle(wordMinLeft, wordMinTop, wordMaxRight - wordMinLeft, wordMaxBottom - wordMinTop, word, wordRotation); wordBounds.push(newWordBounds); wordMinTop = 0; wordMaxBottom = 0; wordMinLeft = 0; wordMaxRight = 0; word = ''; wordRotation = 0; wordStart = true; isPreviousSpace = true; } } break; } } FPDF.CloseTextPage(textPage); this.Rotation = pageRotation; this.PageText = pageText; } }; Processor.prototype.pointerToPixelConverter = function (pointerValue) { return (pointerValue * (96 / 72)); }; Processor.prototype.textBoundsCalculation = function (wordBounds, minTop, maxBottom, maxRight, minLeft, pageRotation, pageWidth, pageHeight) { var newWordBounds; var hasInBetweenRotation = false; var inBetweenRotatedText = ''; var maximumSpaceBetweenWords = 30; var sentence = wordBounds.reduce(function (word, rect) { return word + rect.Text; }, ''); var isRTLText = this.checkIsRtlText(sentence); for (var count = 0; count < wordBounds.length; count++) { var textRotation = wordBounds[parseInt(count.toString(), 10)].Rotation; if (textRotation === 0 || textRotation === 180) { if (hasInBetweenRotation) { this.TextBounds.push(newWordBounds); this.TextContent.push(inBetweenRotatedText); inBetweenRotatedText = ''; } hasInBetweenRotation = false; if (pageRotation === 0) { newWordBounds = new RectAngle(wordBounds[parseInt(count.toString(), 10)].Left, minTop, wordBounds[parseInt(count.toString(), 10)].Width, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - minTop, wordBounds[parseInt(count.toString(), 10)].Left, wordBounds[parseInt(count.toString(), 10)].Width, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - wordBounds[parseInt(count.toString(), 10)].Left, pageHeight - minTop, wordBounds[parseInt(count.toString(), 10)].Width, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle(minTop, pageHeight - wordBounds[parseInt(count.toString(), 10)].Left, wordBounds[parseInt(count.toString(), 10)].Width, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } } else if (textRotation === 90 || textRotation === 270) { if (hasInBetweenRotation) { this.TextBounds.push(newWordBounds); this.TextContent.push(inBetweenRotatedText); inBetweenRotatedText = ''; } hasInBetweenRotation = false; if (pageRotation === 0) { newWordBounds = new RectAngle(minLeft, wordBounds[parseInt(count.toString(), 10)].Top, maxRight - minLeft, wordBounds[parseInt(count.toString(), 10)].Height, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - wordBounds[parseInt(count.toString(), 10)].Top, minLeft, maxRight - minLeft, wordBounds[parseInt(count.toString(), 10)].Height, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - minLeft, pageHeight - wordBounds[parseInt(count.toString(), 10)].Top, maxRight - minLeft, wordBounds[parseInt(count.toString(), 10)].Height, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle(wordBounds[parseInt(count.toString(), 10)].Top, pageHeight - minLeft, maxRight - minLeft, wordBounds[parseInt(count.toString(), 10)].Height, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } } else if (!hasInBetweenRotation) { hasInBetweenRotation = true; inBetweenRotatedText += wordBounds[parseInt(count.toString(), 10)].Text; if (pageRotation === 0) { newWordBounds = new RectAngle(wordBounds[parseInt(count.toString(), 10)].Left, minTop, maxRight - minLeft, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - minTop, wordBounds[parseInt(count.toString(), 10)].Left, maxRight - minLeft, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - wordBounds[parseInt(count.toString(), 10)].Left, pageHeight - minTop, maxRight - minLeft, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle(minTop, pageHeight - wordBounds[parseInt(count.toString(), 10)].Left, maxRight - minLeft, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } } else { inBetweenRotatedText += wordBounds[parseInt(count.toString(), 10)].Text; } if (!hasInBetweenRotation && wordBounds[parseInt(count.toString(), 10)].Text === ' ' && count !== 0 && count + 1 <= wordBounds.length) { if (!isRTLText) { if (count + 1 !== wordBounds.length) { var spaceWidth = 0; switch (textRotation) { case 0: spaceWidth = wordBounds[count + 1].Left - (wordBounds[count - 1].Left + wordBounds[count - 1].Width); if (maximumSpaceBetweenWords < spaceWidth || spaceWidth < 0) { spaceWidth = 0; } if (pageRotation === 0) { newWordBounds = new RectAngle(wordBounds[count - 1].Left + wordBounds[count - 1].Width, minTop, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - minTop, wordBounds[count - 1].Left + wordBounds[count - 1].Width, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - (wordBounds[count - 1].Left + wordBounds[count - 1].Width), pageHeight - minTop, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle(minTop, pageHeight - (wordBounds[count - 1].Left + wordBounds[count - 1].Width), spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } break; case 90: spaceWidth = wordBounds[count + 1].Top - (wordBounds[count - 1].Top + wordBounds[count - 1].Height); if (maximumSpaceBetweenWords < spaceWidth || spaceWidth < 0) { spaceWidth = 0; } if (pageRotation === 0) { newWordBounds = new RectAngle(minLeft, wordBounds[count - 1].Top + wordBounds[count - 1].Height, maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - (wordBounds[count - 1].Top + wordBounds[count - 1].Height), minLeft, maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - minLeft, pageHeight - (wordBounds[count - 1].Top + wordBounds[count - 1].Height), maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle((wordBounds[count - 1].Top + wordBounds[count - 1].Height), pageHeight - minLeft, maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } break; case 180: spaceWidth = wordBounds[count - 1].Left - (wordBounds[count + 1].Left + wordBounds[count + 1].Width); if (maximumSpaceBetweenWords < spaceWidth || spaceWidth < 0) { spaceWidth = 0; } if (pageRotation === 0) { newWordBounds = new RectAngle(wordBounds[count + 1].Left + wordBounds[count + 1].Width, minTop, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - minTop, wordBounds[count + 1].Left + wordBounds[count + 1].Width, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - (wordBounds[count + 1].Left + wordBounds[count + 1].Width), pageHeight - minTop, spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle(minTop, pageHeight - (wordBounds[count + 1].Left + wordBounds[count + 1].Width), spaceWidth, maxBottom - minTop, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } break; case 270: spaceWidth = wordBounds[count - 1].Top - (wordBounds[count + 1].Top + wordBounds[count + 1].Height); if (maximumSpaceBetweenWords < spaceWidth || spaceWidth < 0) { spaceWidth = 0; } if (pageRotation === 0) { newWordBounds = new RectAngle(minLeft, wordBounds[count + 1].Top + wordBounds[count + 1].Height, maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 1) { newWordBounds = new RectAngle(pageWidth - (wordBounds[count + 1].Top + wordBounds[count + 1].Height), minLeft, maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 2) { newWordBounds = new RectAngle(pageWidth - minLeft, pageHeight - (wordBounds[count + 1].Top + wordBounds[count + 1].Height), maxRight - minLeft, spaceWidth, wordBounds[parseInt(count.toString(), 10)].Text, textRotation); } else if (pageRotation === 3) { newWordBounds = new RectAngle((wordBounds[count + 1].Top + wordBounds[count + 1].Height), pageHeight - minLeft, maxRight - minLeft, spaceWidth,