Exemplo n.º 1
0
            public static InlineStylesCount Count(InlineCollection collection, int startIndex)
            {
                InlineStylesCount stylesCount = new InlineStylesCount();

                stylesCount.BoldCount                  = 0;
                stylesCount.ItalicCount                = 0;
                stylesCount.UnderlinedCount            = 0;
                stylesCount.BoldCountingFinished       = false;
                stylesCount.UnderlinedCountingFinished = false;
                stylesCount.ItalicCountingFinished     = false;
                stylesCount.Order = new List <InlineStyles>();

                Count(collection, startIndex, stylesCount);

                // build order
                stylesCount.Order.Add(InlineStyles.Bold);
                if (stylesCount.ItalicCount > stylesCount.BoldCount)
                {
                    stylesCount.Order.Insert(0, InlineStyles.Italic);
                }
                else
                {
                    stylesCount.Order.Add(InlineStyles.Italic);
                }

                for (int z = 0; z < 2; ++z)
                {
                    if (stylesCount.Order[z] == InlineStyles.Bold && stylesCount.UnderlinedCount > stylesCount.BoldCount)
                    {
                        stylesCount.Order.Insert(z, InlineStyles.Underlined);
                        break;
                    }
                    else if (stylesCount.Order[z] == InlineStyles.Italic && stylesCount.UnderlinedCount > stylesCount.ItalicCount)
                    {
                        stylesCount.Order.Insert(z, InlineStyles.Underlined);
                        break;
                    }
                    else if (z == 1)
                    {
                        stylesCount.Order.Add(InlineStyles.Underlined);
                    }
                }

                // remove unneeded elements

                /*
                 * if (stylesCount.BoldCount == 0)
                 *  stylesCount.Order.Remove(InlineStyles.Bold);
                 * if (stylesCount.ItalicCount == 0)
                 *  stylesCount.Order.Remove(InlineStyles.Italic);
                 * if (stylesCount.UnderlinedCount == 0)
                 *  stylesCount.Order.Remove(InlineStyles.Underlined);
                 */

                return(stylesCount);
            }
Exemplo n.º 2
0
            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 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;
                }
            }
            public static InlineStylesCount Count(InlineCollection collection, int startIndex)
            {
                InlineStylesCount stylesCount = new InlineStylesCount();
                stylesCount.BoldCount = 0;
                stylesCount.ItalicCount = 0;
                stylesCount.UnderlinedCount = 0;
                stylesCount.BoldCountingFinished = false;
                stylesCount.UnderlinedCountingFinished = false;
                stylesCount.ItalicCountingFinished = false;
                stylesCount.Order = new List<InlineStyles>();

                Count(collection, startIndex, stylesCount);

                // build order
                stylesCount.Order.Add(InlineStyles.Bold);
                if (stylesCount.ItalicCount > stylesCount.BoldCount)
                    stylesCount.Order.Insert(0, InlineStyles.Italic);
                else
                    stylesCount.Order.Add(InlineStyles.Italic);

                for (int z = 0; z < 2; ++z)
                {
                    if (stylesCount.Order[z] == InlineStyles.Bold && stylesCount.UnderlinedCount > stylesCount.BoldCount)
                    {
                        stylesCount.Order.Insert(z, InlineStyles.Underlined);
                        break;
                    }
                    else if (stylesCount.Order[z] == InlineStyles.Italic && stylesCount.UnderlinedCount > stylesCount.ItalicCount)
                    {
                        stylesCount.Order.Insert(z, InlineStyles.Underlined);
                        break;
                    }
                    else if (z == 1)
                        stylesCount.Order.Add(InlineStyles.Underlined);
                }

                // remove unneeded elements
                /*
                if (stylesCount.BoldCount == 0)
                    stylesCount.Order.Remove(InlineStyles.Bold);
                if (stylesCount.ItalicCount == 0)
                    stylesCount.Order.Remove(InlineStyles.Italic);
                if (stylesCount.UnderlinedCount == 0)
                    stylesCount.Order.Remove(InlineStyles.Underlined);
                */

                return stylesCount;
            }
Exemplo n.º 5
0
        /// <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()));
                    }
                }
            }
        }