private static void Count(InlineCollection inlines, int startIndex, InlineStylesCount stylesCount) { for (int y = startIndex + 1; y < inlines.Count; ++y) { Inline inlineY = inlines.ElementAt(y); if (inlineY.FontStyle == System.Windows.FontStyles.Italic) { stylesCount.ItalicCount++; } else { stylesCount.ItalicCountingFinished = true; } if (inlineY.FontWeight == System.Windows.FontWeights.Bold) { stylesCount.BoldCount++; } else { stylesCount.BoldCountingFinished = true; } if (inlineY.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) { stylesCount.UnderlinedCount++; } else { stylesCount.UnderlinedCountingFinished = true; } // continue with children if (inlineY is Span) { if (!stylesCount.BoldCountingFinished || !stylesCount.UnderlinedCountingFinished || !stylesCount.ItalicCountingFinished) { Count((inlineY as Span).Inlines, 0, stylesCount); } } if (stylesCount.BoldCountingFinished && stylesCount.UnderlinedCountingFinished && stylesCount.ItalicCountingFinished) { break; } } }
private void ProcessInlines(RichTextBox textBox, InlineCollection inlines) { for (int inlineIndex = 0; inlineIndex < inlines.Count; inlineIndex++) { Inline i = inlines.ElementAt(inlineIndex); if (i is Run) { Run r = i as Run; string text = r.Text; string emoticonFound = string.Empty; int index = FindFirstEmoticon(text, 0, out emoticonFound); if (index >= 0) { TextPointer tp = i.ContentStart; bool reposition = false; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound)) tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); TextPointer end = tp; for (int j = 0; j < emoticonFound.Length; j++) end = end.GetNextInsertionPosition(LogicalDirection.Forward); TextRange tr = new TextRange(tp, end); if (textBox != null) reposition = textBox.CaretPosition.CompareTo(tr.End) == 0; tr.Text = string.Empty; var image = GetImageForEmoticon(emoticonFound); InlineUIContainer iui = new InlineUIContainer(image, tp); iui.BaselineAlignment = BaselineAlignment.TextBottom; if (textBox != null && reposition) textBox.CaretPosition = tp.GetNextInsertionPosition(LogicalDirection.Forward); } } } }
private static void ProcessInlines(InlineCollection inlines, Size size, out int numFound) { numFound = 0; try { for (int inlineIndex = 0; inlineIndex < inlines.Count; inlineIndex++) { Inline i = inlines.ElementAt(inlineIndex); if (i is Run) { Run r = i as Run; string text = r.Text; foreach (Match m in Regex.Matches(text, HyperlinkUtility.UrlRegex2)) { TextPointer tp = i.ContentStart; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(m.Value)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < m.Value.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); tr.Text = string.Empty; UIElement element = CreateHyperlink(m.Value); InlineUIContainer iContainer = new InlineUIContainer(element, tp); iContainer.BaselineAlignment = BaselineAlignment.Bottom; continue; } foreach (Match m in Regex.Matches(text, HyperlinkUtility.UncRegex)) { TextPointer tp = i.ContentStart; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(m.Value)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < m.Value.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); tr.Text = string.Empty; UIElement element = CreateHyperlink(m.Value); InlineUIContainer iContainer = new InlineUIContainer(element, tp); iContainer.BaselineAlignment = BaselineAlignment.Bottom; continue; } text = r.Text; foreach (Match m in Regex.Matches(text, HyperlinkUtility.UrlRegex)) { TextPointer tp = i.ContentStart; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(m.Value)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < m.Value.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); tr.Text = string.Empty; UIElement element = CreateHyperlink(m.Value); InlineUIContainer iContainer = new InlineUIContainer(element, tp); iContainer.BaselineAlignment = BaselineAlignment.Bottom; continue; } string emoticonFound = string.Empty; int index = GetNextEmoticon(text, 0, out emoticonFound); if (index >= 0) { TextPointer tp = i.ContentStart; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < emoticonFound.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); tr.Text = string.Empty; //string uri = ApplicationViewModel.Current.SmiliesView.OfType<EmoticonMapping>().Where( d => d.KeyList.Contains( emoticonFound ) ).FirstOrDefault().Path; ImageSource imageSource = EmoticonLibrary.Current.Where(e => e.Keys.Contains(emoticonFound)).Select(e => e.ImageSource).FirstOrDefault(); System.Windows.Controls.Image emoticon = new System.Windows.Controls.Image(); emoticon.Style = Application.Current.Resources["AnimatedImageStyle"] as Style; if (size == Size.Empty) { emoticon.Stretch = Stretch.Fill; Binding heightBinding = new Binding("Source.PixelHeight") { RelativeSource = RelativeSource.Self }; Binding widthBinding = new Binding("Source.PixelWidth") { RelativeSource = RelativeSource.Self }; emoticon.SetBinding(Image.HeightProperty, heightBinding); emoticon.SetBinding(Image.WidthProperty, widthBinding); } else { BitmapSource bitmapSource = imageSource as BitmapSource; if (bitmapSource != null) { if (bitmapSource.PixelHeight < size.Height) { emoticon.Stretch = Stretch.Fill; Binding heightBinding = new Binding("Source.PixelHeight") { RelativeSource = RelativeSource.Self }; Binding widthBinding = new Binding("Source.PixelWidth") { RelativeSource = RelativeSource.Self }; emoticon.SetBinding(Image.HeightProperty, heightBinding); emoticon.SetBinding(Image.WidthProperty, widthBinding); } else { RenderOptions.SetBitmapScalingMode(emoticon, BitmapScalingMode.HighQuality); emoticon.MaxHeight = size.Height; //emoticon.MaxWidth = size.Width; emoticon.Stretch = Stretch.Uniform; } } emoticon.MaxHeight = size.Height; //emoticon.MaxWidth = size.Width; emoticon.Stretch = Stretch.Uniform; } //ImageSource imageSource = new ImageSourceConverter().ConvertFromString( uri ) as ImageSource; WpfAnimatedGif.ImageBehavior.SetAnimatedSource(emoticon, imageSource); numFound++; Grid grid = new Grid(); grid.ToolTip = emoticonFound; grid.Children.Add(emoticon); InlineUIContainer iui = new InlineUIContainer(grid, tp); iui.BaselineAlignment = BaselineAlignment.TextBottom; } emoticonFound = string.Empty; ChatUser user; index = GetNextUser(text, 0, out emoticonFound, out user); if (index >= 0) { TextPointer tp = i.ContentStart; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < emoticonFound.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); tr.Text = string.Empty; ChatUserImage userImage = new ChatUserImage { User = user }; if (size != Size.Empty) { userImage.Height = size.Height; userImage.Width = size.Width; } Grid grid = new Grid(); grid.ToolTip = emoticonFound; grid.Children.Add(userImage); InlineUIContainer iui = new InlineUIContainer(grid, tp); iui.BaselineAlignment = BaselineAlignment.TextBottom; } } } } catch (Exception) { // Do nothing, error processing emoticons } }
private static void Count(InlineCollection inlines, int startIndex, InlineStylesCount stylesCount) { for (int y = startIndex + 1; y < inlines.Count; ++y) { Inline inlineY = inlines.ElementAt(y); if (inlineY.FontStyle == System.Windows.FontStyles.Italic) stylesCount.ItalicCount++; else stylesCount.ItalicCountingFinished = true; if (inlineY.FontWeight == System.Windows.FontWeights.Bold) stylesCount.BoldCount++; else stylesCount.BoldCountingFinished = true; if (inlineY.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) stylesCount.UnderlinedCount++; else stylesCount.UnderlinedCountingFinished = true; // continue with children if (inlineY is Span) { if (!stylesCount.BoldCountingFinished || !stylesCount.UnderlinedCountingFinished || !stylesCount.ItalicCountingFinished) { Count((inlineY as Span).Inlines, 0, stylesCount); } } if (stylesCount.BoldCountingFinished && stylesCount.UnderlinedCountingFinished && stylesCount.ItalicCountingFinished) break; } }
/// <summary> /// Convert an flow document inlines into their (combined!) html representation. /// </summary> /// <param name="inlines">Flow document inlines collection.</param> /// <param name="htmlWriter">XmlTextWriter producing resulting html.</param> /// <param name="conversionResult">Conversion result to store error and warning messages. Can be null.</param> /// <param name="inheritedStyle">Inherited inline styles.</param> /// <remarks> /// Supported: Hyperlink, Bold, Underline, Italic as well as Run and InlineUIContainer for Image. /// </remarks> private static void AddInlines(InlineCollection inlines, XmlTextWriter htmlWriter, ValidationResult conversionResult, List<InlineStyles> inheritedStyle) { for (int i = 0; i < inlines.Count; ++i) { Inline inline = inlines.ElementAt(i); #region InheritedStyle + B,I,U processing bool bAddBold = false; bool bAddItalic = false; bool bAddUnderlined = false; // Bold if (inline.FontWeight != System.Windows.FontWeights.Bold && inheritedStyle.Contains(InlineStyles.Bold)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Bold); } else if (!inheritedStyle.Contains(InlineStyles.Bold) && inline.FontWeight == System.Windows.FontWeights.Bold) bAddBold = true; // Italic if (inline.FontStyle != System.Windows.FontStyles.Italic && inheritedStyle.Contains(InlineStyles.Italic)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Italic); } else if (!inheritedStyle.Contains(InlineStyles.Italic) && inline.FontStyle == System.Windows.FontStyles.Italic) bAddItalic = true; // Underline bool bHasUnderline = false; if (inline is Run && inline.Parent is Span) { if ((inline.Parent as Span).TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) bHasUnderline = true; } if (!inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0]) && inheritedStyle.Contains(InlineStyles.Underlined)) { if (!bHasUnderline) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Underlined); } } else if (!inheritedStyle.Contains(InlineStyles.Underlined) && inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) if (!bHasUnderline) bAddUnderlined = true; if (inline is HtmlHyperlink) bAddUnderlined = false; // see what needs to be added.. if multiple elements need to be added, than we have to iterate through // siblings to see what tag to set first (depending on what to close first). if (bAddUnderlined || bAddBold || bAddItalic) { if (bAddBold && bAddItalic || bAddBold && bAddUnderlined || bAddItalic && bAddUnderlined) { InlineStylesCount stylesCount = InlineStylesCounter.Count(inlines, i + 1); // see what has to be added first // add tags for (int z = 0; z < 3; ++z) { if (stylesCount.Order[z] == InlineStyles.Bold && bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (stylesCount.Order[z] == InlineStyles.Italic && bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (stylesCount.Order[z] == InlineStyles.Underlined && bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } else { // just add the tag of the needed style if (bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } #endregion // continue with text or inlines if (inline is InlineUIContainer) { InlineUIContainer container = inline as InlineUIContainer; if (container.Child is HtmlImage) { // Add image AddImage(container.Child as HtmlImage, htmlWriter, conversionResult); } else if (container.Child is System.Windows.Controls.TextBlock) { // Comment, just add text as is string text = ExtendedHtmlUtility.HtmlEntityEncode((container.Child as System.Windows.Controls.TextBlock).Text); htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { if (container.Child != null) conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline ui container child: " + container.Child.ToString())); //else // conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, // "AddInline: Inline ui container child is null: " + container.ToString())); } } } else if (inline is HtmlHyperlink) { // Add hyperlink AddHyperlink(inline as HtmlHyperlink, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Span) { Span span = inline as Span; AddInlines(span.Inlines, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Run) { string text = ExtendedHtmlUtility.HtmlEntityEncode((inline as Run).Text); //string text = HtmlEncode((inline as Run).Text); ; htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline element: " + inline.ToString())); } } } }
public static void ProcessInlines(RichTextBox textBox, InlineCollection inlines, bool findLinks) { for (int inlineIndex = 0; inlineIndex < inlines.Count; inlineIndex++) { Inline i = inlines.ElementAt(inlineIndex); if (i is Run) { Run r = i as Run; string text = r.Text; if (findLinks) { ProcessParagraphLink(textBox, text, i); } string emoticonFound = string.Empty; int index = FindFirstEmoticon(text, 0, out emoticonFound); if (index >= 0) { TextPointer tp = i.ContentStart; bool reposition = false; while (!tp.GetTextInRun(LogicalDirection.Forward).StartsWith(emoticonFound)) { tp = tp.GetNextInsertionPosition(LogicalDirection.Forward); } TextPointer end = tp; for (int j = 0; j < emoticonFound.Length; j++) { end = end.GetNextInsertionPosition(LogicalDirection.Forward); } TextRange tr = new TextRange(tp, end); if (textBox != null) { reposition = textBox.CaretPosition.CompareTo(tr.End) == 0; } tr.Text = string.Empty; Image image = new Image(); image.SnapsToDevicePixels = true; image.Source = LoadResource.GetEmoticon(emoticonFound.ToLower()); image.Width = 19; image.Height = 19; image.Stretch = Stretch.Fill; image.Tag = emoticonFound.ToLower(); RenderOptions.SetBitmapScalingMode(image, BitmapScalingMode.NearestNeighbor); RenderOptions.SetEdgeMode(image, EdgeMode.Aliased); InlineUIContainer iui = new InlineUIContainer(image, tp); iui.BaselineAlignment = BaselineAlignment.TextBottom; if (textBox != null && reposition) { textBox.CaretPosition = tp.GetNextInsertionPosition(LogicalDirection.Forward); } } } } }
/// <summary> /// Convert an flow document inlines into their (combined!) html representation. /// </summary> /// <param name="inlines">Flow document inlines collection.</param> /// <param name="htmlWriter">XmlTextWriter producing resulting html.</param> /// <param name="conversionResult">Conversion result to store error and warning messages. Can be null.</param> /// <param name="inheritedStyle">Inherited inline styles.</param> /// <remarks> /// Supported: Hyperlink, Bold, Underline, Italic as well as Run and InlineUIContainer for Image. /// </remarks> private static void AddInlines(InlineCollection inlines, XmlTextWriter htmlWriter, ValidationResult conversionResult, List <InlineStyles> inheritedStyle) { for (int i = 0; i < inlines.Count; ++i) { Inline inline = inlines.ElementAt(i); #region InheritedStyle + B,I,U processing bool bAddBold = false; bool bAddItalic = false; bool bAddUnderlined = false; // Bold if (inline.FontWeight != System.Windows.FontWeights.Bold && inheritedStyle.Contains(InlineStyles.Bold)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Bold); } else if (!inheritedStyle.Contains(InlineStyles.Bold) && inline.FontWeight == System.Windows.FontWeights.Bold) { bAddBold = true; } // Italic if (inline.FontStyle != System.Windows.FontStyles.Italic && inheritedStyle.Contains(InlineStyles.Italic)) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Italic); } else if (!inheritedStyle.Contains(InlineStyles.Italic) && inline.FontStyle == System.Windows.FontStyles.Italic) { bAddItalic = true; } // Underline bool bHasUnderline = false; if (inline is Run && inline.Parent is Span) { if ((inline.Parent as Span).TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) { bHasUnderline = true; } } if (!inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0]) && inheritedStyle.Contains(InlineStyles.Underlined)) { if (!bHasUnderline) { // close last element tag htmlWriter.WriteEndElement(); inheritedStyle.Remove(InlineStyles.Underlined); } } else if (!inheritedStyle.Contains(InlineStyles.Underlined) && inline.TextDecorations.Contains(System.Windows.TextDecorations.Underline[0])) { if (!bHasUnderline) { bAddUnderlined = true; } } if (inline is HtmlHyperlink) { bAddUnderlined = false; } // see what needs to be added.. if multiple elements need to be added, than we have to iterate through // siblings to see what tag to set first (depending on what to close first). if (bAddUnderlined || bAddBold || bAddItalic) { if (bAddBold && bAddItalic || bAddBold && bAddUnderlined || bAddItalic && bAddUnderlined) { InlineStylesCount stylesCount = InlineStylesCounter.Count(inlines, i + 1); // see what has to be added first // add tags for (int z = 0; z < 3; ++z) { if (stylesCount.Order[z] == InlineStyles.Bold && bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (stylesCount.Order[z] == InlineStyles.Italic && bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (stylesCount.Order[z] == InlineStyles.Underlined && bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } else { // just add the tag of the needed style if (bAddBold) { htmlWriter.WriteStartElement("b"); inheritedStyle.Add(InlineStyles.Bold); } else if (bAddItalic) { htmlWriter.WriteStartElement("i"); inheritedStyle.Add(InlineStyles.Italic); } else if (bAddUnderlined) { htmlWriter.WriteStartElement("u"); inheritedStyle.Add(InlineStyles.Underlined); } } } #endregion // continue with text or inlines if (inline is InlineUIContainer) { InlineUIContainer container = inline as InlineUIContainer; if (container.Child is HtmlImage) { // Add image AddImage(container.Child as HtmlImage, htmlWriter, conversionResult); } else if (container.Child is System.Windows.Controls.TextBlock) { // Comment, just add text as is string text = ExtendedHtmlUtility.HtmlEntityEncode((container.Child as System.Windows.Controls.TextBlock).Text); htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { if (container.Child != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline ui container child: " + container.Child.ToString())); } //else // conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, // "AddInline: Inline ui container child is null: " + container.ToString())); } } } else if (inline is HtmlHyperlink) { // Add hyperlink AddHyperlink(inline as HtmlHyperlink, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Span) { Span span = inline as Span; AddInlines(span.Inlines, htmlWriter, conversionResult, inheritedStyle); } else if (inline is Run) { string text = ExtendedHtmlUtility.HtmlEntityEncode((inline as Run).Text); //string text = HtmlEncode((inline as Run).Text); ; htmlWriter.WriteRaw(text); } else { // not supported if (conversionResult != null) { conversionResult.AddMessage(new ConversionMessage(ModelValidationViolationType.Warning, "AddInline: Unknown inline element: " + inline.ToString())); } } } }