Пример #1
0
        private static Color?ParseColorWithDefs(string raw, Dictionary <string, Color> colorByName)
        {
            Color color;

            if (colorByName.TryGetValue(raw, out color))
            {
                return(color);
            }
            return(HighlighterUtil.ParseColor(raw));
        }
Пример #2
0
        public void ParseXml(IEnumerable <XmlDocument> xmls)
        {
            Reset();

            Dictionary <string, Color> defColors = new Dictionary <string, Color>();
            Dictionary <string, Color> colors    = new Dictionary <string, Color>();
            Dictionary <string, int>   widths    = new Dictionary <string, int>();

            foreach (XmlDocument xml in xmls)
            {
                foreach (XmlNode node in xml.ChildNodes)
                {
                    XmlElement root = node as XmlElement;
                    if (root == null || root.Name != "scheme")
                    {
                        continue;
                    }
                    foreach (XmlNode nodeI in root.ChildNodes)
                    {
                        XmlElement elementI = nodeI as XmlElement;
                        if (elementI == null)
                        {
                            continue;
                        }
                        if (elementI.Name == "defColor")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
                            {
                                Color?color = HighlighterUtil.ParseColor(value);
                                if (color != null)
                                {
                                    defColors[name] = color.Value;
                                }
                            }
                        }
                        else if (elementI.Name == "style")
                        {
                            Ds        ds    = Ds.GetByName(elementI.GetAttribute("name"));
                            TextStyle style = new TextStyle();
                            Color?    color = ParseColorWithDefs(elementI.GetAttribute("color"), defColors);
                            if (color != null)
                            {
                                style.brush = new SolidBrush(color.Value);
                            }
                            style.Italic    = elementI.GetAttribute("italic") == "true";
                            style.Bold      = elementI.GetAttribute("bold") == "true";
                            style.Underline = elementI.GetAttribute("underline") == "true";
                            style.Strikeout = elementI.GetAttribute("strikeout") == "true";
                            this[ds]        = style;
                        }
                        else if (elementI.Name == "color")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            string width = elementI.GetAttribute("width");
                            if (!string.IsNullOrEmpty(name))
                            {
                                if (!string.IsNullOrEmpty(value))
                                {
                                    Color?color = ParseColorWithDefs(value, defColors);
                                    if (color != null)
                                    {
                                        colors[name] = color.Value;
                                    }
                                }
                                if (!string.IsNullOrEmpty(width))
                                {
                                    int intValue;
                                    if (int.TryParse(width, out intValue))
                                    {
                                        widths[name] = intValue;
                                    }
                                }
                            }
                        }
                        else if (elementI.Name == "width")
                        {
                            string name  = elementI.GetAttribute("name");
                            string value = elementI.GetAttribute("value");
                            if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(value))
                            {
                                int intValue;
                                if (int.TryParse(value, out intValue))
                                {
                                    widths[name] = intValue;
                                }
                            }
                        }
                    }
                    break;
                }
            }

            SetColor(ref bgColor, "bg", colors);
            SetColor(ref fgColor, "fg", colors);
            SetColor(ref lineBgColor, "lineBg", colors);
            SetColor(ref lineNumberBgColor, "lineNumberBg", colors);
            SetColor(ref lineNumberFgColor, "lineNumberFg", colors);
            SetColor(ref selectionBrushColor, "selectionBrush", colors);
            SetColor(ref selectionPenColor, "selectionPen", colors);
            SetColor(ref matchBrushColor, "matchBrush", colors);
            SetColor(ref markPenColor, "markPen", colors);
            SetColor(ref mainCaretColor, "mainCaret", colors);
            SetColor(ref caretColor, "caret", colors);
            SetColor(ref printMarginColor, "printMargin", colors);
            SetWidth(ref mainCaretWidth, "mainCaret", widths);
            SetWidth(ref caretWidth, "caret", widths);

            SetColor(ref separatorColor, "separator", colors);
            SetColor(ref selectedSeparatorColor, "selectedSeparator", colors);

            SetColor(ref splitterBgColor, "splitterBg", colors);
            SetColor(ref splitterLineColor, "splitterLine", colors);
            SetColor(ref scrollBgColor, "scrollBg", colors);
            SetColor(ref scrollThumbColor, "scrollThumb", colors);
            SetColor(ref scrollThumbHoverColor, "scrollThumbHover", colors);
            SetColor(ref scrollArrowColor, "scrollArrow", colors);
            SetColor(ref scrollArrowHoverColor, "scrollArrowHover", colors);

            Tabs_ParseXml(colors);

            Update();
        }
Пример #3
0
        public Highlighter(Raw raw)
        {
            styleDataOf      = new Dictionary <string, StyleData>();
            textListOf       = new Dictionary <string, string[]>();
            keywordDataOf    = new Dictionary <string, Rules.KeywordData>();
            customStyleDatas = new List <StyleData>();
            foreach (Raw.RawList listI in raw.lists)
            {
                textListOf[listI.name.ToLowerInvariant()] = listI.items.ToArray();
            }
            {
                bool first = true;
                foreach (Raw.ItemData itemDataI in raw.itemDatas)
                {
                    StyleData data = new StyleData();
                    data.ds   = Ds.GetByName(itemDataI.defStyleNum);
                    data.name = itemDataI.name;

                    data.color     = HighlighterUtil.ParseColor(itemDataI.color);
                    data.italic    = GetVariantBool(itemDataI.italic);
                    data.bold      = GetVariantBool(itemDataI.bold);
                    data.underline = GetVariantBool(itemDataI.underline);
                    data.strikeout = GetVariantBool(itemDataI.strikeout);
                    if (data.color == null && data.italic == null && data.bold == null && data.underline == null && data.strikeout == null)
                    {
                        data.index = data.ds.index;
                    }
                    else
                    {
                        data.index = (short)(Ds.all.Count + customStyleDatas.Count);
                        customStyleDatas.Add(data);
                    }

                    styleDataOf[data.name.ToLowerInvariant()] = data;
                    if (first)
                    {
                        first            = false;
                        defaultStyleData = data;
                    }
                }
            }

            List <Rules.Context> contextList = new List <Rules.Context>();

            contextOf = new Dictionary <string, Rules.Context>();
            foreach (Raw.Context contextI in raw.contexts)
            {
                string        name    = contextI.name;
                Rules.Context context = new Rules.Context();
                context.name = name;
                contextOf[name.ToLowerInvariant()] = context;
                contextList.Add(context);
            }
            List <Rules.RegExpr> regExprRules = new List <Rules.RegExpr>();

            foreach (Raw.Context contextI in raw.contexts)
            {
                Rules.Context context = contextOf[contextI.name.ToLowerInvariant()];
                {
                    StyleData styleData = null;
                    string    attribute = contextI.attribute;
                    if (attribute != null)
                    {
                        styleDataOf.TryGetValue(attribute.ToLowerInvariant(), out styleData);
                    }
                    if (styleData == null)
                    {
                        styleData = defaultStyleData;
                    }
                    context.attribute = styleData;
                }
                StyleData currentAttribute = context.attribute;
                context.lineEndContext     = GetSwitchInfo(contextI.lineEndContext);
                context.fallthrough        = GetBool(contextI.fallthrough);
                context.fallthroughContext = GetSwitchInfo(contextI.fallthroughContext);

                List <Rules.Rule> contextRules = new List <Rules.Rule>();
                foreach (Raw.Rule ruleI in contextI.rules)
                {
                    Rules.Rule rule = ParseRule(ruleI, regExprRules, context.attribute);
                    if (rule != null)
                    {
                        contextRules.Add(rule);
                    }
                }
                context.childs = contextRules.ToArray();
            }
            awakePositions = new int[regExprRules.Count];
            for (int i = 0; i < regExprRules.Count; i++)
            {
                Rules.RegExpr rule = regExprRules[i];
                rule.awakePositions = awakePositions;
                rule.awakeIndex     = i;
            }
            contexts    = contextList.ToArray();
            styleDataOf = null;
            contextOf   = null;
            textListOf  = null;
        }