UNPKG

@syncfusion/ej2-pdf

Version:

Feature-rich JavaScript PDF library with built-in support for loading and manipulating PDF document.

187 lines (186 loc) 8.21 kB
import { _UnicodeLine } from './../../fonts/pdf-standard-font'; import { _ArabicShapeRenderer } from './../../graphics/rightToLeft/text-shape'; import { _Bidirectional } from './../../graphics/rightToLeft/bidirectional'; import { PdfTextDirection } from './../../enumerator'; import { _stringToUnicodeArray, _bytesToString } from './../../utils'; /** * Renderer for right to left (RTL) scripts that handles BiDi reordering, * Arabic shaping, and TrueType glyph encoding needed for PDF text output. * * @private */ var _RtlRenderer = /** @class */ (function () { function _RtlRenderer() { /** * Opening bracket character for bracket pairing resolution. * * @private */ this._openBracket = '('; /** * Closing bracket character for bracket pairing resolution. * * @private */ this._closeBracket = ')'; } /** * Produces a shaped and visually ordered representation of the input line for the * specified font and layout settings, returning encoded chunks ready for drawing. * For non Unicode fonts, the input line is returned as a single chunk. * * @private * @param {string} line The input text line (logical order). * @param {PdfTrueTypeFont} font The TTF font used for shaping/encoding. * @param {boolean} rtl Paragraph base direction (true = RTL). * @param {boolean} wordSpace When true, returns an array split into characters; otherwise a single item. * @param {PdfStringFormat} format String format providing text direction. * @returns {string[]} The shaped and reordered text chunks encoded for the font. */ _RtlRenderer.prototype._layout = function (line, font, rtl, wordSpace, format) { var result = []; if (font !== null && typeof font !== 'undefined' && line !== null && typeof line !== 'undefined') { if (font._isUnicode) { result = this._customLayout(line, rtl, format, font, wordSpace); } else { result = []; result[0] = line; } } return result; }; /** * Splits the shaped and visually ordered result into character sized chunks, * suitable for per glyph placement and spacing logic. * * @private * @param {string} line The input text line (logical order). * @param {PdfTrueTypeFont} font The TTF font used for shaping/encoding. * @param {boolean} rtl Paragraph base direction (true = RTL). * @param {boolean} wordSpace When true, returns an array of single character chunks. * @param {PdfStringFormat} format String format providing text direction. * @returns {string[]} An array of encoded character chunks. */ _RtlRenderer.prototype._splitLayout = function (line, font, rtl, wordSpace, format) { var words = []; if (font !== null && typeof font !== 'undefined' && line !== null && typeof line !== 'undefined') { var system = false; if (!system) { words = this._customSplitLayout(line, font, rtl, wordSpace, format); } } return words; }; /** * Shapes the input line, resolves glyph indices via the font's * TrueType reader, and returns the glyph index array packaged in `_UnicodeLine`. * * @private * @param {string} line The input text line to map to glyphs. * @param {PdfTrueTypeFont} font The TTF font used to resolve glyph indices. * @param {number[]} glyphs Output glyph index array (ignored on input). * @returns {_UnicodeLine} A result object containing the glyph index list. */ _RtlRenderer.prototype._getGlyphIndex = function (line, font, glyphs) { glyphs = []; if (font !== null && typeof font !== 'undefined' && line !== null && typeof line !== 'undefined') { if (line.length === 0) { return { _result: false, _glyphIndex: glyphs }; } var renderer = new _ArabicShapeRenderer(); var text = renderer._shape(line); var internalFont = font._fontInternal; var ttfReader = internalFont._ttfReader; glyphs = [text.length]; var i = 0; for (var k = 0, len = text.length; k < len; k++) { var ch = text[k]; var glyphInfo = ttfReader._getGlyph(ch); if (glyphInfo !== null && typeof glyphInfo !== 'undefined') { glyphs[i++] = (glyphInfo)._index; } } } var unicodeLine = new _UnicodeLine(); unicodeLine._result = true; unicodeLine._glyphIndex = glyphs; return unicodeLine; }; _RtlRenderer.prototype._customLayout = function (line, rtl, format, font, wordSpace) { var _this = this; if (wordSpace === null || typeof wordSpace === 'undefined') { var result = null; if (line !== null && typeof line !== 'undefined') { if (format !== null && typeof format !== 'undefined' && format.textDirection !== PdfTextDirection.none) { var bidi = new _Bidirectional(); result = bidi._getLogicalToVisualString(line, rtl); } } return result; } else { var layouted = ''; var result = []; if (line !== null && typeof line !== 'undefined' && font !== null && typeof font !== 'undefined') { if (format !== null && typeof format !== 'undefined' && format.textDirection !== PdfTextDirection.none) { var renderer = new _ArabicShapeRenderer(); var txt = renderer._shape(line); layouted = this._customLayout(txt, rtl, format); } if (wordSpace) { var words = layouted.split(''); words = words.map(function (word) { return _this._addCharacter(font, word); }); result = words; } else { result = []; result[0] = this._addCharacter(font, layouted); } } return result; } }; /** * Encodes a string into the fonts TrueType encoding by setting symbols, * converting text through the TTF reader, and returning a PDF safe byte string. * * @private * @param {PdfTrueTypeFont} font The TrueType font used for encoding. * @param {string} glyphs The input text (already shaped/reordered). * @returns {string} The encoded string suitable for PDF content streams. */ _RtlRenderer.prototype._addCharacter = function (font, glyphs) { if (font !== null && typeof font !== 'undefined' && glyphs !== null && typeof glyphs !== 'undefined') { var internalFont = font._fontInternal; var ttfReader = internalFont._ttfReader; font._setSymbols(glyphs); glyphs = ttfReader._convertString(glyphs); var bytes = _stringToUnicodeArray(glyphs); glyphs = _bytesToString(bytes); } return glyphs; }; /** * A convenience wrapper that runs RTL reordering + shaping and splits the * result into per character segments. * * @private * @param {string} line The input text line. * @param {PdfTrueTypeFont} font The TrueType font. * @param {boolean} rtl Paragraph base direction. * @param {boolean} wordSpace Whether to split into single character segments. * @param {PdfStringFormat} format String format providing text direction. * @returns {string[]} The visually ordered characters. */ _RtlRenderer.prototype._customSplitLayout = function (line, font, rtl, wordSpace, format) { var words = []; if (line !== null && typeof line !== 'undefined') { var reversedLine = this._customLayout(line, rtl, format); words = reversedLine.split(''); } return words; }; return _RtlRenderer; }()); export { _RtlRenderer };