/// <summary> /// Method to handle inline replacement of style tag by opening style definition. /// </summary> /// <param name="sourceText"></param> /// <param name="srcIndex"></param> /// <param name="srcOffset"></param> /// <param name="charBuffer"></param> /// <param name="writeIndex"></param> /// <param name="styleStack"></param> /// <returns></returns> static bool ReplaceOpeningStyleTag(ref string sourceText, int srcIndex, out int srcOffset, ref int[] charBuffer, ref int writeIndex, ref TextProcessingStack <int> styleStack, ref TextGenerationSettings generationSettings) { // Validate <style> tag. int hashCode = GetTagHashCode(ref sourceText, srcIndex + 7, out srcOffset); TextStyle style = GetStyle(generationSettings, hashCode); // Return if we don't have a valid style. if (style == null || srcOffset == 0) { return(false); } styleStack.Add(style.hashCode); int styleLength = style.styleOpeningTagArray.Length; // Replace <style> tag with opening definition int[] openingTagArray = style.styleOpeningTagArray; for (int i = 0; i < styleLength; i++) { int c = openingTagArray[i]; if (c == 60) { if (IsTagName(ref openingTagArray, "<BR>", i)) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = 10; writeIndex += 1; i += 3; continue; } if (IsTagName(ref openingTagArray, "<STYLE=", i)) { int offset; if (ReplaceOpeningStyleTag(ref openingTagArray, i, out offset, ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings)) { i = offset; continue; } } else if (IsTagName(ref openingTagArray, "</STYLE>", i)) { ReplaceClosingStyleTag(ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings); // Strip </style> even if style is invalid. i += 7; continue; } } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = c; writeIndex += 1; } return(true); }
/// <summary> /// Method to handle inline replacement of style tag by closing style definition. /// </summary> /// <param name="charBuffer"></param> /// <param name="writeIndex"></param> /// <param name="styleStack"></param> /// <returns></returns> static void ReplaceClosingStyleTag(ref int[] charBuffer, ref int writeIndex, ref TextProcessingStack <int> styleStack, ref TextGenerationSettings generationSettings) { // Get style from the Style Stack int hashCode = styleStack.CurrentItem(); TextStyle style = GetStyle(generationSettings, hashCode); styleStack.Remove(); // Return if we don't have a valid style. if (style == null) { return; } int styleLength = style.styleClosingTagArray.Length; // Replace <style> tag with opening definition int[] closingTagArray = style.styleClosingTagArray; for (int i = 0; i < styleLength; i++) { int c = closingTagArray[i]; if (c == 60) { if (IsTagName(ref closingTagArray, "<BR>", i)) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = 10; writeIndex += 1; i += 3; continue; } if (IsTagName(ref closingTagArray, "<STYLE=", i)) { int offset; if (ReplaceOpeningStyleTag(ref closingTagArray, i, out offset, ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings)) { i = offset; continue; } } else if (IsTagName(ref closingTagArray, "</STYLE>", i)) { ReplaceClosingStyleTag(ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings); // Strip </style> even if style is invalid. i += 7; continue; } } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = c; writeIndex += 1; } }
/// <summary> /// Method to store the content of a string into an integer array. /// </summary> /// <param name="sourceText"></param> /// <param name="charBuffer"></param> /// <param name="styleStack"></param> /// <param name="generationSettings"></param> public static void StringToCharArray(string sourceText, ref int[] charBuffer, ref TextProcessingStack <int> styleStack, TextGenerationSettings generationSettings) { if (sourceText == null) { charBuffer[0] = 0; return; } if (charBuffer == null) { charBuffer = new int[8]; } // Clear the Style stack. styleStack.SetDefault(0); int writeIndex = 0; for (int i = 0; i < sourceText.Length; i++) { if (sourceText[i] == 92 && sourceText.Length > i + 1) { switch ((int)sourceText[i + 1]) { case 85: // \U00000000 for UTF-32 Unicode if (sourceText.Length > i + 9) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = GetUtf32(sourceText, i + 2); i += 9; writeIndex += 1; continue; } break; case 92: // \ escape if (!generationSettings.parseControlCharacters) { break; } if (sourceText.Length <= i + 2) { break; } if (writeIndex + 2 > charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = sourceText[i + 1]; charBuffer[writeIndex + 1] = sourceText[i + 2]; i += 2; writeIndex += 2; continue; case 110: // \n LineFeed if (!generationSettings.parseControlCharacters) { break; } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = (char)10; i += 1; writeIndex += 1; continue; case 114: // \r if (!generationSettings.parseControlCharacters) { break; } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = (char)13; i += 1; writeIndex += 1; continue; case 116: // \t Tab if (!generationSettings.parseControlCharacters) { break; } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = (char)9; i += 1; writeIndex += 1; continue; case 117: // \u0000 for UTF-16 Unicode if (sourceText.Length > i + 5) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = (char)GetUtf16(sourceText, i + 2); i += 5; writeIndex += 1; continue; } break; } } // Handle UTF-32 in the input text (string). // Not sure this is needed // if (Char.IsHighSurrogate(sourceText[i]) && Char.IsLowSurrogate(sourceText[i + 1])) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = Char.ConvertToUtf32(sourceText[i], sourceText[i + 1]); i += 1; writeIndex += 1; continue; } //// Handle inline replacement of <stlye> and <br> tags. if (sourceText[i] == 60 && generationSettings.richText) { if (IsTagName(ref sourceText, "<BR>", i)) { if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = 10; writeIndex += 1; i += 3; continue; } if (IsTagName(ref sourceText, "<STYLE=", i)) { int srcOffset; if (ReplaceOpeningStyleTag(ref sourceText, i, out srcOffset, ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings)) { i = srcOffset; continue; } } else if (IsTagName(ref sourceText, "</STYLE>", i)) { ReplaceClosingStyleTag(ref charBuffer, ref writeIndex, ref styleStack, ref generationSettings); // Strip </style> even if style is invalid. i += 7; continue; } } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = sourceText[i]; writeIndex += 1; } if (writeIndex == charBuffer.Length) { ResizeInternalArray(ref charBuffer); } charBuffer[writeIndex] = (char)0; }