Exemple #1
0
 internal static void ApplyColorToElement(VisualLineElement element, HighlightingColor color, ITextRunConstructionContext context)
 {
     if (color.Foreground != null)
     {
         var b = color.Foreground.GetBrush(context);
         if (b != null)
         {
             element.TextRunProperties.ForegroundBrush = b;
         }
     }
     if (color.Background != null)
     {
         var b = color.Background.GetBrush(context);
         if (b != null)
         {
             element.BackgroundBrush = b;
         }
     }
     if (color.FontStyle != null || color.FontWeight != null)
     {
         var tf = element.TextRunProperties.Typeface;
         element.TextRunProperties.Typeface = tf;
     }
     //if(color.Underline ?? false)
     //	element.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
 }
 public HtmlElement(int offset, int nesting, bool isEnd, HighlightingColor color)
 {
     Offset  = offset;
     Nesting = nesting;
     IsEnd   = isEnd;
     Color   = color;
 }
 public static void ApplyColorToElement(VisualLineElement element, HighlightingColor color, ITextRunConstructionContext context)
 {
     if (color.Foreground != null)
     {
         var b = color.Foreground.GetBrush(context);
         if (b != null)
         {
             element.TextRunProperties.ForegroundBrush = b;
         }
     }
     if (color.Background != null)
     {
         var b = color.Background.GetBrush(context);
         if (b != null)
         {
             element.BackgroundBrush = b;
         }
     }
     if (color.FontStyle != null || color.FontWeight != null || color.FontFamily != null)
     {
         var tf = element.TextRunProperties.Typeface;
         element.TextRunProperties.Typeface = new Avalonia.Media.Typeface(
             color.FontFamily ?? tf.FontFamily,
             color.FontStyle ?? tf.Style,
             color.FontWeight ?? tf.Weight
             );
     }
     if (color.FontSize.HasValue)
     {
         element.TextRunProperties.FontSize = color.FontSize.Value;
     }
     //if(color.Underline ?? false)
     //	element.TextRunProperties.SetTextDecorations(TextDecorations.Underline);
 }
Exemple #4
0
 /// <summary>
 /// Gets whether the color needs to be written out to HTML.
 /// </summary>
 public virtual bool ColorNeedsSpanForStyling(HighlightingColor color)
 {
     if (color == null)
     {
         throw new ArgumentNullException(nameof(color));
     }
     return(!string.IsNullOrEmpty(color.ToCss()));
 }
Exemple #5
0
 /// <summary>
 /// Gets whether the color is empty (has no effect on a VisualLineTextElement).
 /// For example, the C# "Punctuation" is an empty color.
 /// </summary>
 internal static bool IsEmptyColor(HighlightingColor color)
 {
     if (color == null)
     {
         return(true);
     }
     return(color.Background == null && color.Foreground == null &&
            color.FontStyle == null && color.FontWeight == null &&
            color.Underline == null);
 }
Exemple #6
0
 /// <summary>
 /// Writes the HTML attribute for the style to the text writer.
 /// </summary>
 public virtual void WriteStyleAttributeForColor(TextWriter writer, HighlightingColor color)
 {
     if (writer == null)
     {
         throw new ArgumentNullException(nameof(writer));
     }
     if (color == null)
     {
         throw new ArgumentNullException(nameof(color));
     }
     writer.Write(" style=\"");
     writer.Write(WebUtility.HtmlEncode(color.ToCss()));
     writer.Write('"');
 }
        /// <summary>
        /// Sets the HighlightingColor for the specified range of text,
        /// completely replacing the existing highlighting in that area.
        /// </summary>
        public void SetHighlighting(int offset, int length, HighlightingColor color)
        {
            if (length <= 0)
            {
                return;
            }
            var startIndex = GetIndexForOffset(offset);
            var endIndex   = GetIndexForOffset(offset + length);

            _stateChanges[startIndex] = color != null?color.Clone() : new HighlightingColor();

            _stateChanges.RemoveRange(startIndex + 1, endIndex - (startIndex + 1));
            _stateChangeOffsets.RemoveRange(startIndex + 1, endIndex - (startIndex + 1));
        }
Exemple #8
0
 /// <inheritdoc/>
 public override void BeginSpan(HighlightingColor highlightingColor)
 {
     WriteIndentationAndSpace();
     if (options.ColorNeedsSpanForStyling(highlightingColor))
     {
         htmlWriter.Write("<span");
         options.WriteStyleAttributeForColor(htmlWriter, highlightingColor);
         htmlWriter.Write('>');
         endTagStack.Push("</span>");
     }
     else
     {
         endTagStack.Push(null);
     }
 }
        /// <summary>
        /// Applies the HighlightingColor to the specified range of text.
        /// If the color specifies <c>null</c> for some properties, existing highlighting is preserved.
        /// </summary>
        public void ApplyHighlighting(int offset, int length, HighlightingColor color)
        {
            if (color == null || color.IsEmptyForMerge)
            {
                // Optimization: don't split the HighlightingState when we're not changing
                // any property. For example, the "Punctuation" color in C# is
                // empty by default.
                return;
            }
            var startIndex = GetIndexForOffset(offset);
            var endIndex   = GetIndexForOffset(offset + length);

            for (var i = startIndex; i < endIndex; i++)
            {
                _stateChanges[i].MergeWith(color);
            }
        }
        private void Insert(ref int pos, ref int newSectionStart, int insertionEndPos, HighlightingColor color, Stack <int> insertionStack)
        {
            if (newSectionStart >= insertionEndPos)
            {
                // nothing to insert here
                return;
            }

            while (insertionStack.Peek() <= newSectionStart)
            {
                insertionStack.Pop();
            }
            while (insertionStack.Peek() < insertionEndPos)
            {
                var end = insertionStack.Pop();
                // insert the portion from newSectionStart to end
                if (end > newSectionStart)
                {
                    Sections.Insert(pos++, new HighlightedSection
                    {
                        Offset = newSectionStart,
                        Length = end - newSectionStart,
                        Color  = color
                    });
                    newSectionStart = end;
                }
            }
            if (insertionEndPos > newSectionStart)
            {
                Sections.Insert(pos++, new HighlightedSection
                {
                    Offset = newSectionStart,
                    Length = insertionEndPos - newSectionStart,
                    Color  = color
                });
                newSectionStart = insertionEndPos;
            }
        }
Exemple #11
0
 /// <summary>
 /// Applies a highlighting color to a visual line element.
 /// </summary>
 protected virtual void ApplyColorToElement(VisualLineElement element, HighlightingColor color)
 {
     ApplyColorToElement(element, color, CurrentContext);
 }