UNPKG

@magic-xpa/utils

Version:

magic utils package

1,449 lines (1,447 loc) 852 kB
import { Encoding, StringBuilder, Hashtable, NChar, NNumber, Stack, NString, RefParam, DateTime, ApplicationException, ISO_8859_1_Encoding, Int32, Exception, Debug, Thread, List } from '@magic-xpa/mscorelib'; import { parseString } from 'xml2js'; import { isNullOrUndefined } from 'util'; /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ var UtilStrByteMode = /** @class */ (function () { function UtilStrByteMode() { } /// <summary> Checks the environment whether it is running on DBCS environment /// Returns true if the language code for the current default Locale is /// DBCS language (in non-Unicode encoding). /// /// </summary> /// <returns> true if DBCS, or false if SBCS /// </returns> /** * @return {?} */ UtilStrByteMode.isLocaleDefLangDBCS = /** * @return {?} */ function () { return UtilStrByteMode._bLocaleDefLangJPN || UtilStrByteMode._bLocaleDefLangCHN || UtilStrByteMode._bLocaleDefLangKOR; }; /// <summary> Checks whether the language code for the current default Locale /// is JAPANESE. /// /// </summary> /// <returns> true if JAPANESE, or false if not /// </returns> /** * @return {?} */ UtilStrByteMode.isLocaleDefLangJPN = /** * @return {?} */ function () { return UtilStrByteMode._bLocaleDefLangJPN; }; /// <summary> Checks whether the language code for the current default Locale /// is KOREAN. /// /// </summary> /// <returns> true if KOREAN, or false if not /// </returns> /** * @return {?} */ UtilStrByteMode.isLocaleDefLangKOR = /** * @return {?} */ function () { return UtilStrByteMode._bLocaleDefLangKOR; }; /** * @param {?} c * @return {?} */ UtilStrByteMode.isKoreanCharacter = /** * @param {?} c * @return {?} */ function (c) { return (44032 <= /*'가'*/ c && c <= 55203 /*'힣'*/) || (4352 <= /*'ᄀ'*/ c && c <= 4607 /*'ᇿ'*/) || (12592 <= /*'㄰'*/ c && c <= 12687 /*'㆏'*/) || (43360 <= /*'ꥠ'*/ c && c <= 43391 /*'꥿'*/) || (55216 <= /*'ힰ'*/ c && c <= 55295 /*'퟿'*/); }; /// <summary> Length of String /// Returns the number of bytes (in default encoding). /// /// </summary> /// <param name="strVal:">string (in Unicode) /// </param> /// <returns> the number of bytes (in default encoding) /// /// Example: lenB("abXYc") /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// 7. /// </returns> /** * @param {?} strVal * @return {?} */ UtilStrByteMode.lenB = /** * @param {?} strVal * @return {?} */ function (strVal) { // convert to byte[] by default-encoding return UtilStrByteMode.Encoding.GetByteCount(strVal); }; /// <summary> Substring of String /// Extracts a specified number of characters (a substring) from a string. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strVal:">string (in Unicode) /// </param> /// <param name="ofs:">starting position (byte) of the substring /// </param> /// <param name="len:">number of bytes to be extracted (i.e. bytes of substring) /// </param> /// <returns> substring /// /// Example: midB("abXYc", 2, 4) /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// "bX ". /// </returns> /** * @param {?} strVal * @param {?} ofs * @param {?} len * @return {?} */ UtilStrByteMode.midB = /** * @param {?} strVal * @param {?} ofs * @param {?} len * @return {?} */ function (strVal, ofs, len) { /** @type {?} */ var intValidMaxIndex = -1; /** @type {?} */ var intValidMinIndex = -1; /** @type {?} */ var bHeadSpace = false; /** @type {?} */ var bEndSpace = false; /** @type {?} */ var strRet; // check and modify ofs & len if (len <= 0) return ""; if (ofs <= 0) { ofs = 0; intValidMinIndex = 0; bHeadSpace = false; } /** @type {?} */ var intByteLength = UtilStrByteMode.lenB(strVal); if (intByteLength < ofs) return ""; /** @type {?} */ var LenMax = intByteLength - ofs; if (LenMax < len) len = LenMax; // set MinIndex and MaxIndex for substring intByteLength = 0; for (var intIndex = 0; intIndex < strVal.length; intIndex = intIndex + 1) { /** @type {?} */ var s = strVal.substr(intIndex, 1); intByteLength = intByteLength + UtilStrByteMode.Encoding.GetByteCount(s); if (intValidMinIndex === -1) { if (intByteLength === ofs) { intValidMinIndex = intIndex + 1; bHeadSpace = false; } else if (intByteLength > ofs) { intValidMinIndex = intIndex + 1; bHeadSpace = true; } } if (intValidMaxIndex === -1) { if (intByteLength === ofs + len) { intValidMaxIndex = intIndex; bEndSpace = false; break; } else if (intByteLength > ofs + len) { intValidMaxIndex = intIndex - 1; bEndSpace = true; break; } } } /** @type {?} */ var strbufAddingBuf = new StringBuilder(len); // execute Mid if (bHeadSpace) { strbufAddingBuf.Append(' '); } if (intValidMinIndex <= intValidMaxIndex) { strbufAddingBuf.Append(strVal.substr(intValidMinIndex, intValidMaxIndex + 1 - intValidMinIndex)); } if (bEndSpace) { strbufAddingBuf.Append(' '); } strRet = strbufAddingBuf.ToString(); strbufAddingBuf = null; return strRet; }; /// <summary> Get Characters from Left of String /// Returns a specified number of bytes from the left side of a string. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strVal:">string (in Unicode) /// </param> /// <param name="len:">number of bytes to be retured /// </param> /// <returns> output string /// /// Example: leftB("abXYc", 4) /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// "abX". /// </returns> /** * @param {?} strVal * @param {?} len * @return {?} */ UtilStrByteMode.leftB = /** * @param {?} strVal * @param {?} len * @return {?} */ function (strVal, len) { return UtilStrByteMode.midB(strVal, 0, len); }; /// <summary> Get Characters from Right of String /// Returns a specified number of bytes from the right side of a string. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strVal:">string (in Unicode) /// </param> /// <param name="len:">number of bytes to be retured /// </param> /// <returns> output string /// /// Example: rightB("abXYc", 4) /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// " Yc". /// </returns> /** * @param {?} strVal * @param {?} len * @return {?} */ UtilStrByteMode.rightB = /** * @param {?} strVal * @param {?} len * @return {?} */ function (strVal, len) { /** @type {?} */ var byteFldsValLen = UtilStrByteMode.lenB(strVal); if (len < 0) { len = 0; } /** @type {?} */ var ofs = byteFldsValLen - len; if (ofs < 0) { ofs = 0; } return UtilStrByteMode.midB(strVal, ofs, len); }; /// <summary> Insert String /// Inserts one string into another. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strTarget:">A string that represents the target string. /// </param> /// <param name="strSource:">A string that represents the source string. /// </param> /// <param name="ofs:">A number that represents the starting position (byte) in /// the target. /// </param> /// <param name="len:">A number that represents the number of bytes from the /// source that will be inserted into the target. /// </param> /// <returns> output string /// /// Example: insB("abXYc", "de", 4, 1) /// Where 'a', 'b', 'c', 'd' and 'e' are SBCS, and 'X' and 'Y' are DBCS, /// it returns "ab d Yc". /// </returns> /** * @param {?} strTarget * @param {?} strSource * @param {?} ofs * @param {?} len * @return {?} */ UtilStrByteMode.insB = /** * @param {?} strTarget * @param {?} strSource * @param {?} ofs * @param {?} len * @return {?} */ function (strTarget, strSource, ofs, len) { if (ofs < 0) { ofs = 0; } else { if (ofs >= 1) { ofs = ofs - 1; } } if (len < 0) { len = 0; } /** @type {?} */ var intTargetLenB = UtilStrByteMode.lenB(strTarget); /** @type {?} */ var intSourceLenB = UtilStrByteMode.lenB(strSource); /** @type {?} */ var strbufRetVal = new StringBuilder(ofs + len); strbufRetVal.Append(UtilStrByteMode.leftB(strTarget, ofs)); for (var intAddSpaceLen = ofs - intTargetLenB; intAddSpaceLen > 0; intAddSpaceLen = intAddSpaceLen - 1) { strbufRetVal.Append(' '); } strbufRetVal.Append(UtilStrByteMode.leftB(strSource, len)); for (var i = len - intSourceLenB; i > 0; i = i - 1) { strbufRetVal.Append(' '); } strbufRetVal.Append(UtilStrByteMode.rightB(strTarget, intTargetLenB - ofs)); return strbufRetVal.ToString(); }; /// <summary> Delete Characters /// Delete characters from a string. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strVal:">string (in Unicode) /// </param> /// <param name="ofs:">The position (byte) of the first character to be deleted. /// </param> /// <param name="len:">The number of characters to be deleted, beginning with /// position start and proceeding rightward. /// </param> /// <returns> output string /// /// Example: delB("abXYc", 2, 4) /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// "a c". /// </returns> /** * @param {?} strVal * @param {?} ofs * @param {?} len * @return {?} */ UtilStrByteMode.delB = /** * @param {?} strVal * @param {?} ofs * @param {?} len * @return {?} */ function (strVal, ofs, len) { if (ofs < 0) { ofs = 0; } else { if (ofs >= 1) { ofs = ofs - 1; } } /** @type {?} */ var intValLenB = UtilStrByteMode.lenB(strVal); if (ofs + len > intValLenB) { len = intValLenB - ofs; } /** @type {?} */ var strRet; if (len <= 0) { strRet = strVal; } else { /** @type {?} */ var intRightSideLenB = intValLenB - ofs - len; if (intRightSideLenB < 0) { strRet = strVal; } else { /** @type {?} */ var strbufRetVal = new StringBuilder(ofs + intRightSideLenB); strbufRetVal.Append(UtilStrByteMode.leftB(strVal, ofs)); strbufRetVal.Append(UtilStrByteMode.rightB(strVal, intRightSideLenB)); strRet = strbufRetVal.ToString(); } } return strRet; }; /// <summary> In-String Search /// Returns a number that represents the first position (byte) of a /// substring within a string. /// /// </summary> /// <param name="strTarget:">string (in Unicode) /// </param> /// <param name="strSearch:">string which will be the search argument in string /// </param> /// <returns> number, 0 if not found /// /// Example: instrB("abXYc", "Y") /// Where 'a', 'b' and 'c' are SBCS, and 'X' and 'Y' are DBCS, it returns /// 5. /// </returns> /** * @param {?} strTarget * @param {?} strSearch * @return {?} */ UtilStrByteMode.instrB = /** * @param {?} strTarget * @param {?} strSearch * @return {?} */ function (strTarget, strSearch) { if (strSearch.length === 0) { // nothing to look for return 0; } /** @type {?} */ var ofs = strTarget.indexOf(strSearch); if (ofs < 0) { // not found return 0; } return UtilStrByteMode.lenB(strTarget.substr(0, ofs)) + 1; }; /// <summary> Replace Substring Within a String (Byte Mode) /// Replaces a substring within a string with another substring. /// If a DBCS character is divided in two, it will be replace to a space. /// /// </summary> /// <param name="strTarget:">target string where the replacement will take place. /// </param> /// <param name="strOrigin:">string that provides the substring to be copied to /// target. /// </param> /// <param name="ofs:">the first position (byte) in the target string that will /// receive the substring from origin. /// </param> /// <param name="len:">the number of bytes that will be moved from origin to /// target, starting from the leftmost character of origin. /// </param> /// <returns> string containing modified target string /// /// Example: repB("abXYc", "de", 4, 2) /// Where 'a', 'b', 'c', 'd' and 'e' are SBCS, and 'X' and 'Y' are DBCS, /// it returns "ab de c". /// </returns> /** * @param {?} strTarget * @param {?} strOrigin * @param {?} ofs * @param {?} len * @return {?} */ UtilStrByteMode.repB = /** * @param {?} strTarget * @param {?} strOrigin * @param {?} ofs * @param {?} len * @return {?} */ function (strTarget, strOrigin, ofs, len) { /** @type {?} */ var strbufAddingBuf = new StringBuilder(); if (ofs < 0) { ofs = 0; } else { if (ofs >= 1) { ofs = ofs - 1; } } if (len < 0) { len = 0; } strbufAddingBuf.Append(UtilStrByteMode.leftB(strTarget, ofs)); /** @type {?} */ var intAddSpaceLen = ofs - UtilStrByteMode.lenB(strTarget); for (; intAddSpaceLen > 0; intAddSpaceLen--) strbufAddingBuf.Append(' '); strbufAddingBuf.Append(UtilStrByteMode.leftB(strOrigin, len)); /** @type {?} */ var intRightLen = UtilStrByteMode.lenB(strTarget) - (ofs + len); if (intRightLen > 0) { strbufAddingBuf.Append(UtilStrByteMode.rightB(strTarget, intRightLen)); } // add blanks to the end intAddSpaceLen = len - UtilStrByteMode.lenB(strOrigin); for (; intAddSpaceLen > 0; intAddSpaceLen--) { strbufAddingBuf.Append(' '); } return strbufAddingBuf.ToString(); }; /// <summary> Replace Substring Within a String (Character Mode) /// Replaces a substring within a string with another substring. /// /// </summary> /// <param name="strTarget:">target string where the replacement will take place. /// </param> /// <param name="strOrigin:">string that provides the substring to be copied to /// target. /// </param> /// <param name="ofs:">the first position (character) in the target string that /// will receive the substring from origin. /// </param> /// <param name="len:">the number of characters that will be moved from origin /// to target, starting from the leftmost character of origin. /// </param> /// <returns> string containing modified target string /// /// Example: repB("abXYc", "de", 4, 2) /// Whether each character is SBCS or DBCS, it returns "abXde". /// </returns> /** * @param {?} strTarget * @param {?} strOrigin * @param {?} ofs * @param {?} len * @return {?} */ UtilStrByteMode.repC = /** * @param {?} strTarget * @param {?} strOrigin * @param {?} ofs * @param {?} len * @return {?} */ function (strTarget, strOrigin, ofs, len) { /** @type {?} */ var strbufAddingBuf = new StringBuilder(); if (ofs < 0) { ofs = 0; } else { if (ofs >= 1) { ofs = ofs - 1; } } if (len < 0) { len = 0; } strbufAddingBuf.Append(strTarget.substr(0, ofs)); /** @type {?} */ var intAddSpaceLen = ofs - strTarget.length; for (; intAddSpaceLen > 0; intAddSpaceLen--) strbufAddingBuf.Append(' '); strbufAddingBuf.Append(strOrigin.substr(0, len)); /** @type {?} */ var intRightLen = strTarget.length - (ofs + len); if (intRightLen > 0) { strbufAddingBuf.Append(strTarget.substr(ofs + len)); } for (var i = len - strOrigin.length; i > 0; i = i - 1) { strbufAddingBuf.Append(' '); } return strbufAddingBuf.ToString(); }; /// <summary> Checks whether a character is 1 byte (halfwidth) or not (fullwidth) /// Returns true if the character is represented by 1 byte in non-Unicode /// encoding. /// </summary> /// <param name="letter:">a character to be checked. /// </param> /// <returns> true if the character is halfwidth (SBCS), or false if it is /// fullwidth (DBCS). /// </returns> /** * @param {?} str * @return {?} */ UtilStrByteMode.isHalfWidth = /** * @param {?} str * @return {?} */ function (str) { /** @type {?} */ var letter = str.charCodeAt(0); if (32 <= /*' '*/ letter && letter <= 126 /*'~'*/) { return true; } else { /** @type {?} */ var len = UtilStrByteMode.lenB(str); if (len === 1) return true; } return false; }; /// <summary> Checks whether a character is halfwidth digit letter /// Do not use "Character.isDigit" which cannot distinguish between /// halfwidth digit letter(SBCS) and fullwidth difit letter(DBCS). /// </summary> /// <param name="letter:">a character to be checked. /// </param> /// <returns> true if the character is halfwidth digit letter, or /// false if it is DBCS or not digit letter. /// </returns> /** * @param {?} letter * @return {?} */ UtilStrByteMode.isDigit = /** * @param {?} letter * @return {?} */ function (letter) { return 48 <= /*'0'*/ /*'0'*/ letter.charCodeAt(0) && letter.charCodeAt(0) <= 57 /*'9'*/; }; /// <summary>Checks whether a character is one of those supported for # Alpha Mask</summary> /// <param name="letter:">a character to be checked. /// </param> /// <returns> true if the character is halfwidth digit letter, or /// false if it is DBCS or not digit letter. /// </returns> /** * @param {?} letter * @return {?} */ UtilStrByteMode.asNumeric = /** * @param {?} letter * @return {?} */ function (letter) { /** @type {?} */ var result; switch (letter.charCodeAt(0)) { case 42: /*'*'*/ case 43: /*'+'*/ case 44: /*','*/ case 45: /*'-'*/ case 46: /*'.'*/ case 47: /*'/'*/ result = true; break; default: result = false; break; } return result; }; /// <summary> Converts a position for the 1st string (Source) to a position for /// the 2nd string (Dest). /// If a double byte character exists in the strings, the position for the /// Source could be different from the position for the Dest. /// (DBCS Support) /// /// </summary> /// <param name="strSource:">Source string /// </param> /// <param name="strDest:">Dest string /// </param> /// <param name="pos:">position in the Source string /// </param> /// <param name="isAdvance:">advance or retreat the ret pos if a DBCS char is split /// </param> /// <returns> position in the Dest string /// /// Example: convPos("abcYZ", "YZabc", 4) /// It returns 4, if the all characters in the strings are SBCS. /// /// If 'a', 'b' and 'c' are SBCS, and 'Y' and 'Z' are DBCS, it /// returns 3. /// pos /// Unicode index 0 1 2 3 [4] /// +-------------+ /// Source string |a|b|c| Y | Z | /// +-------------+ /// ANSI index 0 1 2 3 4[5]6 /// +-------------+ /// Dest string | Y | Z |a|b|c| /// +-------------+ /// Unicode index 0 1 2[3]4 /// ret /// </returns> /** * @param {?} strSource * @param {?} strDest * @param {?} pos * @param {?} isAdvance * @return {?} */ UtilStrByteMode.convPos = /** * @param {?} strSource * @param {?} strDest * @param {?} pos * @param {?} isAdvance * @return {?} */ function (strSource, strDest, pos, isAdvance) { /** @type {?} */ var retPos; if (pos < 0) return 0; if (pos > strSource.length) pos = strSource.length; /** @type {?} */ var diffLen = UtilStrByteMode.lenB(strSource) - UtilStrByteMode.lenB(strDest); if (diffLen > 0) { /** @type {?} */ var stringBuilder = new StringBuilder(strDest); for (; diffLen > 0; diffLen--) stringBuilder.Append(' '); strDest = stringBuilder.ToString(); } /** @type {?} */ var byteSource = UtilStrByteMode.Encoding.GetBytes(strSource.substr(0, pos)); /** @type {?} */ var strLeftB = UtilStrByteMode.leftB(strDest, byteSource.length); retPos = strLeftB.length; if (!isAdvance && retPos > 0 && strLeftB.charCodeAt(retPos - 1) === 32 /*' '*/ && strDest.charCodeAt(retPos - 1) !== 32 /*' '*/) { retPos = retPos - 1; } return retPos; }; /// <summary> return the number of characters of picture which corresponds to /// given string. /// </summary> /// <param name="str:">given string /// </param> /// <param name="picture:">picture /// </param> /// <returns> minimal length of picture /// Example: getMinLenPicture("ZZ20/11/", "JJJJYY/MM/DD") [ZZ is DBCS] /// It returns 10. /// </returns> /// (DBCS Support) /** * @param {?} str * @param {?} picture * @return {?} */ UtilStrByteMode.getMinLenPicture = /** * @param {?} str * @param {?} picture * @return {?} */ function (str, picture) { /** @type {?} */ var len = 0; if (UtilStrByteMode.lenB(picture) - UtilStrByteMode.lenB(str) > 0) { len = UtilStrByteMode.convPos(str, picture, str.length, false); } else len = picture.length; return len; }; /// <summary> /// </summary> Compares two specified strings in the DBCS sort order and returns an integer /// that indicates their relative position. /// <param name="str1:">The first string to compare. /// </param> /// <param name="str2:">The second string to compare. /// </param> /// <returns>an integer that indicates the lexical relationship between the two strings. /// -1: str1 is less than str2. /// 0: str1 equals str2. /// 1: str1 is greater than str2. /// </returns> /** * @param {?} str1 * @param {?} str2 * @return {?} */ UtilStrByteMode.strcmp = /** * @param {?} str1 * @param {?} str2 * @return {?} */ function (str1, str2) { /** @type {?} */ var array1 = UtilStrByteMode.Encoding.GetBytes(str1); /** @type {?} */ var array2 = UtilStrByteMode.Encoding.GetBytes(str2); for (var i = 0; i < array1.length && i < array2.length; i++) { if (array1[i] > array2[i]) { return 1; } else if (array1[i] < array2[i]) { return -1; } } if (array1.length > array2.length) return 1; if (array1.length < array2.length) return -1; else return 0; }; UtilStrByteMode.Encoding = Encoding.UTF8; // TODO : need to check what to do with CultureInfo UtilStrByteMode.twoLetterISOLanguageName = " "; UtilStrByteMode._bLocaleDefLangJPN = (UtilStrByteMode.twoLetterISOLanguageName === "ja"); UtilStrByteMode._bLocaleDefLangCHN = (UtilStrByteMode.twoLetterISOLanguageName === "zh"); UtilStrByteMode._bLocaleDefLangKOR = (UtilStrByteMode.twoLetterISOLanguageName === "ko"); return UtilStrByteMode; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,extraRequire,uselessCode} checked by tsc */ var Rtf_SYMBOL = /** @class */ (function () { /* index into property table if kwd == kwdProp */ /* index into destination table if kwd == kwdDest */ /* character to print if kwd == kwdChar */ function Rtf_SYMBOL(keyWord, kwd, idxInRgprop) { this.szKeyword = null; /* RTF keyword */ this.kwd = null; /* base action to take */ this.idxInRgprop = null; this.szKeyword = keyWord; this.kwd = kwd; this.idxInRgprop = idxInRgprop; } return Rtf_SYMBOL; }()); var Rtf_PROP = /** @class */ (function () { /* structure containing value */ function Rtf_PROP(actn, prop) { this.actn = null; /* size of value */ this.prop = null; this.actn = actn; this.prop = prop; } return Rtf_PROP; }()); var Rtf_StackSave = /** @class */ (function () { function Rtf_StackSave() { this.rds = null; this.ris = null; } return Rtf_StackSave; }()); /** @enum {number} */ var Rtf_KWD = { CHAR: 0, DEST: 1, PROP: 2, SPEC: 3, }; Rtf_KWD[Rtf_KWD.CHAR] = 'CHAR'; Rtf_KWD[Rtf_KWD.DEST] = 'DEST'; Rtf_KWD[Rtf_KWD.PROP] = 'PROP'; Rtf_KWD[Rtf_KWD.SPEC] = 'SPEC'; /** @enum {number} */ var Rtf_PROPTYPE = { CHP: 0, PAP: 1, SEP: 2, DOP: 3, }; Rtf_PROPTYPE[Rtf_PROPTYPE.CHP] = 'CHP'; Rtf_PROPTYPE[Rtf_PROPTYPE.PAP] = 'PAP'; Rtf_PROPTYPE[Rtf_PROPTYPE.SEP] = 'SEP'; Rtf_PROPTYPE[Rtf_PROPTYPE.DOP] = 'DOP'; /** @enum {number} */ var Rtf_ACTN = { SPEC: 0, BYTE: 1, WORD: 2, }; Rtf_ACTN[Rtf_ACTN.SPEC] = 'SPEC'; Rtf_ACTN[Rtf_ACTN.BYTE] = 'BYTE'; Rtf_ACTN[Rtf_ACTN.WORD] = 'WORD'; /** @enum {number} */ var Rtf_IPFN = { BIN: 0, HEX: 1, SKIP_DEST: 2, BREAK: 3, NEW: 4, FONT: 5, CHARSET: 6, UNICODE: 7, }; Rtf_IPFN[Rtf_IPFN.BIN] = 'BIN'; Rtf_IPFN[Rtf_IPFN.HEX] = 'HEX'; Rtf_IPFN[Rtf_IPFN.SKIP_DEST] = 'SKIP_DEST'; Rtf_IPFN[Rtf_IPFN.BREAK] = 'BREAK'; Rtf_IPFN[Rtf_IPFN.NEW] = 'NEW'; Rtf_IPFN[Rtf_IPFN.FONT] = 'FONT'; Rtf_IPFN[Rtf_IPFN.CHARSET] = 'CHARSET'; Rtf_IPFN[Rtf_IPFN.UNICODE] = 'UNICODE'; /** @enum {number} */ var Rtf_IDEST = { PICT: 0, COLOR: 1, SKIP: 2, }; Rtf_IDEST[Rtf_IDEST.PICT] = 'PICT'; Rtf_IDEST[Rtf_IDEST.COLOR] = 'COLOR'; Rtf_IDEST[Rtf_IDEST.SKIP] = 'SKIP'; /** @enum {number} */ var Rtf_IPROP = { BOLD: 0, ITALIC: 1, UNDERLINE: 2, FONT: 3, SIZE: 4, COLOR: 5, RED: 6, GREEN: 7, BLUE: 8, LEFT_IND: 9, RIGHT_IND: 10, FIRST_IND: 11, COLS: 12, PGN_X: 13, PGN_Y: 14, XA_PAGE: 15, YA_PAGE: 16, XA_LEFT: 17, XA_RIGHT: 18, YA_TOP: 19, YA_BOTTOM: 20, PGN_START: 21, SBK: 22, PGN_FORMAT: 23, FACING_P: 24, LANDSCAPE: 25, JUST: 26, PARD: 27, PLAIN: 28, SECTD: 29, BULLET: 30, XA_BULLET: 31, MAX: 32, }; Rtf_IPROP[Rtf_IPROP.BOLD] = 'BOLD'; Rtf_IPROP[Rtf_IPROP.ITALIC] = 'ITALIC'; Rtf_IPROP[Rtf_IPROP.UNDERLINE] = 'UNDERLINE'; Rtf_IPROP[Rtf_IPROP.FONT] = 'FONT'; Rtf_IPROP[Rtf_IPROP.SIZE] = 'SIZE'; Rtf_IPROP[Rtf_IPROP.COLOR] = 'COLOR'; Rtf_IPROP[Rtf_IPROP.RED] = 'RED'; Rtf_IPROP[Rtf_IPROP.GREEN] = 'GREEN'; Rtf_IPROP[Rtf_IPROP.BLUE] = 'BLUE'; Rtf_IPROP[Rtf_IPROP.LEFT_IND] = 'LEFT_IND'; Rtf_IPROP[Rtf_IPROP.RIGHT_IND] = 'RIGHT_IND'; Rtf_IPROP[Rtf_IPROP.FIRST_IND] = 'FIRST_IND'; Rtf_IPROP[Rtf_IPROP.COLS] = 'COLS'; Rtf_IPROP[Rtf_IPROP.PGN_X] = 'PGN_X'; Rtf_IPROP[Rtf_IPROP.PGN_Y] = 'PGN_Y'; Rtf_IPROP[Rtf_IPROP.XA_PAGE] = 'XA_PAGE'; Rtf_IPROP[Rtf_IPROP.YA_PAGE] = 'YA_PAGE'; Rtf_IPROP[Rtf_IPROP.XA_LEFT] = 'XA_LEFT'; Rtf_IPROP[Rtf_IPROP.XA_RIGHT] = 'XA_RIGHT'; Rtf_IPROP[Rtf_IPROP.YA_TOP] = 'YA_TOP'; Rtf_IPROP[Rtf_IPROP.YA_BOTTOM] = 'YA_BOTTOM'; Rtf_IPROP[Rtf_IPROP.PGN_START] = 'PGN_START'; Rtf_IPROP[Rtf_IPROP.SBK] = 'SBK'; Rtf_IPROP[Rtf_IPROP.PGN_FORMAT] = 'PGN_FORMAT'; Rtf_IPROP[Rtf_IPROP.FACING_P] = 'FACING_P'; Rtf_IPROP[Rtf_IPROP.LANDSCAPE] = 'LANDSCAPE'; Rtf_IPROP[Rtf_IPROP.JUST] = 'JUST'; Rtf_IPROP[Rtf_IPROP.PARD] = 'PARD'; Rtf_IPROP[Rtf_IPROP.PLAIN] = 'PLAIN'; Rtf_IPROP[Rtf_IPROP.SECTD] = 'SECTD'; Rtf_IPROP[Rtf_IPROP.BULLET] = 'BULLET'; Rtf_IPROP[Rtf_IPROP.XA_BULLET] = 'XA_BULLET'; Rtf_IPROP[Rtf_IPROP.MAX] = 'MAX'; /** @enum {number} */ var Rtf_RDS = { NORM: 0, COLOR: 1, SKIP: 2, NEW: 3, }; Rtf_RDS[Rtf_RDS.NORM] = 'NORM'; Rtf_RDS[Rtf_RDS.COLOR] = 'COLOR'; Rtf_RDS[Rtf_RDS.SKIP] = 'SKIP'; Rtf_RDS[Rtf_RDS.NEW] = 'NEW'; /** @enum {number} */ var Rtf_ErrorRtf = { OK: 0, STACK_UNDERFLOW: 1, STACK_OVERFLOW: 2, UNMATCHED_BRACE: 3, INVALID_HEX: 4, BAD_TABLE: 5, ASSERTION: 6, END_OF_FILE: 7, BUFFER_TOO_SMALL: 8, }; Rtf_ErrorRtf[Rtf_ErrorRtf.OK] = 'OK'; Rtf_ErrorRtf[Rtf_ErrorRtf.STACK_UNDERFLOW] = 'STACK_UNDERFLOW'; Rtf_ErrorRtf[Rtf_ErrorRtf.STACK_OVERFLOW] = 'STACK_OVERFLOW'; Rtf_ErrorRtf[Rtf_ErrorRtf.UNMATCHED_BRACE] = 'UNMATCHED_BRACE'; Rtf_ErrorRtf[Rtf_ErrorRtf.INVALID_HEX] = 'INVALID_HEX'; Rtf_ErrorRtf[Rtf_ErrorRtf.BAD_TABLE] = 'BAD_TABLE'; Rtf_ErrorRtf[Rtf_ErrorRtf.ASSERTION] = 'ASSERTION'; Rtf_ErrorRtf[Rtf_ErrorRtf.END_OF_FILE] = 'END_OF_FILE'; Rtf_ErrorRtf[Rtf_ErrorRtf.BUFFER_TOO_SMALL] = 'BUFFER_TOO_SMALL'; var Rtf_RtfChar = /** @class */ (function () { function Rtf_RtfChar() { } Rtf_RtfChar.CR = String.fromCharCode(0x0d); Rtf_RtfChar.LF = String.fromCharCode(0x0A); Rtf_RtfChar.TAB = String.fromCharCode(0x09); Rtf_RtfChar.BULLET = String.fromCharCode(0x95); Rtf_RtfChar.TILDA = String.fromCharCode(0xA0); Rtf_RtfChar.DASH = String.fromCharCode(0xAD); Rtf_RtfChar.DASH_CHAR = '-'; Rtf_RtfChar.QUOTE = '\''; Rtf_RtfChar.DBLQUOTE = '"'; Rtf_RtfChar.OPENINGBRACE = '{'; Rtf_RtfChar.CLOSINGBRACE = '}'; Rtf_RtfChar.BACKSLASH = '\\'; return Rtf_RtfChar; }()); /** @enum {number} */ var Rtf_RIS = { NORM: 0, BIN: 1, HEX: 2, UNICODE: 3, }; Rtf_RIS[Rtf_RIS.NORM] = 'NORM'; Rtf_RIS[Rtf_RIS.BIN] = 'BIN'; Rtf_RIS[Rtf_RIS.HEX] = 'HEX'; Rtf_RIS[Rtf_RIS.UNICODE] = 'UNICODE'; var Rtf = /** @class */ (function () { /// <summary> Constructor /// </summary> function Rtf() { this._group = 0; this._cbBin = 0; this._lParam = 0; this._skipDestIfUnk = false; this._outputOnce = false; this._processCrlfSpecial = false; this._destState = null; this._internalState = null; this._stack = null; this._index = 0; this._fontNum = 0; this._charsetTable = new Hashtable(); this._codePageTable = new Hashtable(); this._stack = new Stack(); this._group = 0; this._cbBin = 0; this._lParam = 0; this._outputOnce = false; this._skipDestIfUnk = false; this._processCrlfSpecial = false; this._destState = Rtf_RDS.NORM; this._internalState = Rtf_RIS.NORM; this._fontNum = 0; if (UtilStrByteMode.isLocaleDefLangDBCS()) { this.setCodePageTable(); } } /// <summary> Checks if the blob has a Rtf data or not /// /// </summary> /// <param name="str"> /// </param> /// <returns> /// </returns> /** * @param {?} str * @return {?} */ Rtf.isRtf = /** * @param {?} str * @return {?} */ function (str) { /** @type {?} */ var isRtf = false; if (str !== null && str.startsWith(this.RTF_PREFIX)) { isRtf = true; } return isRtf; }; /** * @param {?} rtfTxt * @param {?} outputTxt * @return {?} */ Rtf.prototype.toTxt = /** * @param {?} rtfTxt * @param {?} outputTxt * @return {?} */ function (rtfTxt, outputTxt) { /** @type {?} */ var cNibble = 2; /** @type {?} */ var b = 0; /** @type {?} */ var currPos = 0; /** @type {?} */ var skipNewline = false; /** @type {?} */ var blobStrLen; /** @type {?} */ var blobChar; /** @type {?} */ var ec; /** @type {?} */ var dbcsBytes = new Uint8Array(2); /** @type {?} */ var skipParseChar = false; /** @type {?} */ var charset = 0; /** @type {?} */ var codePage = 0; this._outputOnce = false; this._processCrlfSpecial = false; blobStrLen = rtfTxt.length; this._index = 0; this._destState = Rtf_RDS.NORM; if (rtfTxt === null || blobStrLen === 0 || !Rtf.isRtf(rtfTxt)) { return Rtf_ErrorRtf.OK; } while (this._index < blobStrLen) { blobChar = rtfTxt[this._index]; this._index++; if (this._group < 0) return Rtf_ErrorRtf.STACK_UNDERFLOW; /* if we're parsing binary data, handle it directly */ if (this._internalState === Rtf_RIS.BIN) { if ((ec = this.ParseChar(blobChar, outputTxt)) !== Rtf_ErrorRtf.OK) return ec; } else { switch (blobChar) { case Rtf_RtfChar.OPENINGBRACE: skipNewline = false; if ((ec = this.PushState()) !== Rtf_ErrorRtf.OK) return ec; break; case Rtf_RtfChar.CLOSINGBRACE: skipNewline = true; if ((ec = this.PopState()) !== Rtf_ErrorRtf.OK) return ec; break; case Rtf_RtfChar.BACKSLASH: skipNewline = false; if ((ec = this.ParseKeyword(rtfTxt, outputTxt)) !== Rtf_ErrorRtf.OK) return ec; break; case Rtf_RtfChar.LF: case Rtf_RtfChar.CR: /* cr and lf are noise characters... */ if (this._processCrlfSpecial) { /* Once we reach the 0x0a while ProcessCRLFSpecial_, reset the ProcessCRLFSpecial_ */ if (blobChar === Rtf_RtfChar.LF) { this._processCrlfSpecial = false; } } else { /*---------------------------------------------------------------*/ /* skip new lines coming only from the RTF header 1/1/98 - #2390 */ /*---------------------------------------------------------------*/ /* Skip the LF (0x0a) if we are not in the ProcessCRLFSpecial_ */ if (blobChar === Rtf_RtfChar.LF || (blobChar === Rtf_RtfChar.CR && skipNewline && !this._outputOnce)) break; } /* falls through */ default: if (blobChar !== Rtf_RtfChar.CR) skipNewline = false; if (this._internalState === Rtf_RIS.NORM) { if ((ec = this.ParseChar(blobChar, outputTxt)) !== Rtf_ErrorRtf.OK) return ec; } else if (this._internalState === Rtf_RIS.UNICODE) { if ((ec = this.ParseChar(String.fromCharCode(this._lParam), outputTxt)) !== Rtf_ErrorRtf.OK) return ec; this._internalState = Rtf_RIS.NORM; } else { /* parsing hex data */ if (this._internalState !== Rtf_RIS.HEX) return Rtf_ErrorRtf.ASSERTION; b = b << 4; if (NChar.IsDigit(blobChar)) b += blobChar.charCodeAt(0) - '0'.charCodeAt(0); else { if (NChar.IsLower(blobChar)) { if (blobChar < 'a' || blobChar > 'f') return Rtf_ErrorRtf.INVALID_HEX; b += 10 + blobChar.charCodeAt(0) - 'a'.charCodeAt(0); } else { if (blobChar < 'A' || blobChar > 'F') return Rtf_ErrorRtf.INVALID_HEX; b += 10 + blobChar.charCodeAt(0) - 'A'.charCodeAt(0); } } cNibble--; if (cNibble === 0) { if (UtilStrByteMode.isLocaleDefLangDBCS()) { charset = this.getCharset(this._fontNum); // leading byte of a double-byte character if (!skipParseChar && Rtf.is1stByte(b, charset)) { dbcsBytes[0] = b; dbcsBytes[1] = 0; skipParseChar = true; } else { // trailing byte of a double-byte character if (skipParseChar && Rtf.is2ndByte(b, charset)) dbcsBytes[1] = b; else { dbcsBytes[0] = b; dbcsBytes[1] = 0; } // convert DBCS to Unicode codePage = this.getCodePage(charset); /** @type {?} */ var workStr = Encoding.GetEncoding(codePage).GetString(dbcsBytes, 0, 2); b = workStr.charCodeAt(0); skipParseChar = false; } } if (!skipParseChar) { if ((ec = this.ParseChar(String.fromCharCode(b), outputTxt)) !== Rtf_ErrorRtf.OK) return ec; } cNibble = 2; b = 0; this._internalState = Rtf_RIS.NORM; } } /* end else (ris != risNorm) */ break; } /* switch */ } /* else (ris != risBin) */ } /* while */ if (this._group < 0) return Rtf_ErrorRtf.STACK_UNDERFLOW; if (this._group > 0) return Rtf_ErrorRtf.UNMATCHED_BRACE; /*-------------------------------------------------------------------*/ /* Eliminate suffix of carrige return + line feed */ /* (Check last characters - just in case format is not the expected) */ /*-------------------------------------------------------------------*/ currPos = outputTxt.Length; if (currPos >= 3 && (outputTxt.get_Item(currPos - 3) === Rtf_RtfChar.CR && outputTxt.get_Item(currPos - 2) === Rtf_RtfChar.LF && outputTxt.get_Item(currPos - 1) === Rtf_RtfChar.CR || outputTxt.get_Item(currPos - 3) === Rtf_RtfChar.LF && outputTxt.get_Item(currPos - 2) === Rtf_RtfChar.CR && outputTxt.get_Item(currPos - 1) === Rtf_RtfChar.CR)) outputTxt.Remove(currPos - 3, 3); return Rtf_ErrorRtf.OK; }; /** * @param {?} ch * @param {?} outputTxt * @return {?} */ Rtf.prototype.ParseChar = /** * @param {?} ch * @param {?} outputTxt * @return {?} */ function (ch, outputTxt) { /** @type {?} */ var ret = Rtf_ErrorRtf.OK; if (this._internalState === Rtf_RIS.BIN && --this._cbBin <= 0) { this._internalState = Rtf_RIS.NORM; } if (this._destState === Rtf_RDS.SKIP) ; else if (this._destState === Rtf_RDS.NORM) { /* Output a character. Properties are valid at this point. */ ret = this.PrintChar(ch, outputTxt); } return ret; }; /** * @param {?} ch * @param {?} outputTxt * @return {?} */ Rtf.prototype.PrintChar = /** * @param {?} ch * @param {?} outputTxt * @return {?} */ function (ch, outputTxt) { /* Allow carrige return + line feed in text, but remove bullet sign */ /*------------------------------------------------------------------*/ if ((ch >= ' ' || ch === Rtf_RtfChar.CR || ch === Rtf_RtfChar.LF) && ch !== String.fromCharCode(183)) { outputTxt.Append(ch); } if (ch >= ' ') { this._outputOnce = true; } return Rtf_ErrorRtf.OK; }; /** * @return {?} */ Rtf.prototype.PushState = /** * @return {?} */ function () { /** @type {?} */ var stackSave = new Rtf_StackSave(); if (stackSave === null) { return Rtf_ErrorRtf.STACK_OVERFLOW; } stackSave.rds = this._destState; stackSave.ris = this._internalState; this._internalState = Rtf_RIS.NORM; this._stack.push(stackSave); this._group++; return Rtf_ErrorRtf.OK; }; /** * @return {?} */ Rtf.prototype.PopState = /** * @return {?} */ function () { /** @type {?} */ var savedPop = this._stack.pop(); if (savedPop === null) { return Rtf_ErrorRtf.STACK_UNDERFLOW; } this._destState = savedPop.rds; this._internalState = savedPop.ris; this._group--; return Rtf_ErrorRtf.OK; }; /** * @param {?} rtfTxt * @param {?} outputTxt * @return {?} */ Rtf.prototype.ParseKeyword = /** * @param {?} rtfTxt * @param {?} outputTxt * @return {?} */ function (rtfTxt, outputTxt) { /** @type {?} */ var ch; /** @type {?} */ var fNeg = false; /** @type {?} */ var szKeyword = ""; /** @type {?} */ var szParameter = ""; if ((ch = rtfTxt[this._index++]) === String.fromCharCode(0)) { return Rtf_ErrorRtf.END_OF_FILE; } /* a control symbol; no delimiter. */ if (!NChar.IsLetter(ch)) { szKeyword = szKeyword + ch; return this.TranslateKeyword(szKeyword, outputTxt); } for (; NChar.IsLetter(ch); ch = rtfTxt[this._index++]) szKeyword = szKeyword + ch; if (ch === '-') { fNeg = true; if ((ch = rtfTxt[this._index++]) === String.fromCharCode(0)) return Rtf_ErrorRtf.END_OF_FILE; } if (NChar.IsDigit(ch)) { for (; NChar.IsDigit(ch); ch = rtfTxt[this._index++]) szParameter = szParameter + ch; this._lParam = NNumber.Parse(szParameter); if (fNeg) this._lParam = -this._lParam; } if (ch !== ' ') this._index--; if (szKeyword === Rtf.CHAR_PAR) { /* if we get a RTF sequence of \par[0xd][0xa], ie a \par kwd followed */ /* immidiately by the CR and LF, then ignore the \par kwd. otherwise */ /* we will translate the \par - which translates to a LF (0xa) and also */ /* the following 0x0d is translated to 0x0a, thus resulting in TWO LF's */ /* being inserted instead of just one LF. So by skipping [\par] and */ /* translating only the [0xd 0xa] will result in only one LF appearing */ /* - which is the desired behaviour */ if (rtfTxt[this._index] === Rtf_RtfChar.CR && rtfTxt[this._index + 1] === Rtf_RtfChar.LF) this._processCrlfSpecial = true; } if (this._processCrlfSpecial) { return Rtf_ErrorRtf.OK; } else { return this.TranslateKeyword(szKeyword, outputTxt); } }; /** * @param {?} szKeyword * @param {?} outputTxt * @return {?} */ Rtf.prototype.TranslateKeyword = /** * @param {?} szKeyword * @param {?} outputTxt * @return {?} */ function (szKeyword, outputTxt) { /** @type {?} */ var result = Rtf_ErrorRtf.OK; /** @type {?} */ var isym; /* search for szKeyword in rgsymRtf */ for (isym = 0; isym < Rtf.rgsymRtf.length; isym++) { if (szKeyword === Rtf.rgsymRtf[isym].szKeyword) { break; } } /* control word not found */ if (isym === Rtf.rgsymRtf.length) { if (this._skipDestIfUnk) { /* if this is a new destination */ this._destState = Rtf_RDS.SKIP; /* skip the destination */ } /* else just discard it */ this._skipDestIfUnk = false; } else { result = Rtf_ErrorRtf.BAD_TABLE; /* found it! use kwd and idxInRgprop to determine what to do with it. */ this._skipDestIfUnk = false; if (Rtf.rgsymRtf[isym].kwd === Rtf_KWD.PROP) { result = this.validateProp(/** @type {?} */ (Rtf.rgsymRtf[isym].idxInRgprop)); } else if (Rtf.rgsymRtf[isym].kwd === Rtf_KWD.CHAR) { result = this.ParseChar((Rtf.rgsymRtf[isym].idxInRgprop), outputTxt); } else if (Rtf.rgsymRtf[isym].kwd === Rtf_KWD.DEST) { result = this.changeDestState(); } else if (Rtf.rgsymRtf[isym].kwd === Rtf_KWD.SPEC) { result = this.ParseSpecialKeyword(/** @type {?} */ (Rtf.rgsymRtf[isym].idxInRgprop)); } } return result; }; /** * @param {?} iprop * @return {?} */ Rtf.prototype.validateProp = /** * @param {?} i