Beispiel #1
0
        /// <summary>
        /// Build an XML Style element suitable for storing int the user preferences file.
        /// </summary>
        /// <returns></returns>

        private XElement MakeElement(CustomStyle custom)
        {
            var builder = new StringBuilder();

            if ((custom.Font.Style & FontStyle.Bold) > 0)
            {
                builder.Append("Bold");
            }
            if ((custom.Font.Style & FontStyle.Italic) > 0)
            {
                if (builder.Length > 0)
                {
                    builder.Append("+");
                }
                builder.Append("Italic");
            }
            if ((custom.Font.Style & FontStyle.Underline) > 0)
            {
                if (builder.Length > 0)
                {
                    builder.Append("+");
                }
                builder.Append("Underline");
            }

            var style = builder.Length == 0 ? "Regular" : builder.ToString();

            var colorHex = custom.Color.ToArgb().ToString("X6");

            if (colorHex.Length > 6)
            {
                colorHex = colorHex.Substring(colorHex.Length - 6);
            }

            var element = new XElement("Style",
                                       new XAttribute("name", custom.Name),
                                       new XAttribute("fontFamily", custom.Font.FontFamily.Name),
                                       new XAttribute("fontSize", custom.Font.Size.ToString("#.0")),
                                       new XAttribute("fontStyle", style),
                                       new XAttribute("color", "#ff" + colorHex),
                                       new XAttribute("spaceBefore", custom.SpaceBefore),
                                       new XAttribute("spaceAfter", custom.SpaceAfter),
                                       new XAttribute("isHeading", custom.IsHeading.ToString())
                                       );

            if (!custom.Background.IsEmpty &&
                !custom.Background.Equals(Color.Transparent) &&
                !custom.Background.Equals(custom.Color))
            {
                var backHex = custom.Background.ToArgb().ToString("X6");
                if (backHex.Length > 6)
                {
                    backHex = backHex.Substring(backHex.Length - 6);
                }
                element.Add(new XAttribute("background", "#ff" + backHex));
            }

            return(element);
        }
Beispiel #2
0
        public CustomStyle GetStyle(int index, bool scaling = true)
        {
            CustomStyle style = null;

            var template = root.Elements(ns + "Styles").Elements(ns + "Style")?.Skip(index).FirstOrDefault();

            if (template != null)
            {
                style = ReadStyle(template, scaling);
            }

            return(style);
        }
Beispiel #3
0
        /// <summary>
        /// Create a dialog to edit a single style; this is for creating new styles
        /// </summary>
        /// <param name="style"></param>

        public StyleDialog(CustomStyle style)
        {
            Initialize();

            Logger.DesignMode = DesignMode;

            updatable = true;

            Text = "New Custom Style";
            loadButton.Enabled    = false;
            reorderButton.Enabled = false;
            deleteButton.Enabled  = false;

            this.selection = style;
            ShowSelection();
        }
Beispiel #4
0
        public void SaveStyle(CustomStyle style)
        {
            var candidate =
                (from e in root.Elements(ns + "Styles").Elements(ns + "Style")
                 where e.Attribute("name").Value.Equals(style.Name)
                 select e).FirstOrDefault();

            if (candidate != null)
            {
                candidate.ReplaceWith(MakeElement(style));
            }
            else
            {
                root.Element(ns + "Styles")?.Add(MakeElement(style));
            }

            string path = PathFactory.GetAppDataPath();

            PathFactory.EnsurePathExists(path);

            Save(root, Path.Combine(path, Resx.CustomStylesFilename));
        }
Beispiel #5
0
        //<one:T><![CDATA[One two thre]]></one:T>
        //<one:T selected="all"><![CDATA[]]></one:T>
        //<one:T><![CDATA[e]]></one:T>

        private void EvaluatePage(XElement page, CustomStyle style, ApplicationManager manager)
        {
            var ns = page.GetNamespaceOfPrefix("one");

            // find all selections; may be multiple if text is selected across multiple paragraphs

            var selections = page.Descendants(ns + "T")
                             .Where(e => e.Attributes("selected").Any(a => a.Value.Equals("all")));

            if (selections != null)
            {
                foreach (var selection in selections)
                {
                    var phrase = new Phrase(selection);

                    if (phrase.IsEmpty)
                    {
                        // infer selected word by combining adjacent non-whitespace characters to
                        // the left of the cursor and to the right of the cursor into a single word

                        string word = string.Empty;

                        if ((selection.PreviousNode != null) && (selection.PreviousNode is XElement))
                        {
                            var prev = new Phrase(selection.PreviousNode as XElement);
                            if (!prev.EndsWithSpace)
                            {
                                word += prev.ExtractLastWord();
                            }
                        }

                        if ((selection.NextNode != null) && (selection.NextNode is XElement))
                        {
                            var next = new Phrase(selection.NextNode as XElement);
                            if (!next.StartsWithSpace)
                            {
                                word += next.ExtractFirstWord();
                            }
                        }

                        if (word.Length > 0)
                        {
                            phrase.CData.Value =
                                new XElement("span", new XAttribute("style", style.ToCss(true)), word)
                                .ToString(SaveOptions.DisableFormatting);
                        }
                        else
                        {
                            // cannot apply style to an empty CDATA because OneNote will
                            // strip the styling off, so instead need to apply to parent one:T instead

                            var span = selection.Attribute("style");
                            if (span == null)
                            {
                                selection.Add(new XAttribute("style", style.ToCss(true)));
                            }
                            else
                            {
                                span.Value = style.ToCss(true);
                            }
                        }
                    }
                    else
                    {
                        phrase.ClearFormatting();

                        phrase.CData.Value =
                            new XElement("span", new XAttribute("style", style.ToCss(true)), phrase.CData.Value)
                            .ToString(SaveOptions.DisableFormatting);
                    }

                    // apply spacing to parent OE; we may have selected text across multiple OEs
                    // but we'll just reapply if all Ts are within the same OE, no biggie

                    var oe = selection.Parent;
                    ApplySpacing(oe, "spaceBefore", style.SpaceBefore);
                    ApplySpacing(oe, "spaceAfter", style.SpaceAfter);
                }

                manager.UpdatePageContent(page);
            }
        }
Beispiel #6
0
 private void namesBox_SelectedIndexChanged(object sender, EventArgs e)
 {
     selection = (CustomStyle)namesBox.SelectedItem;
     ShowSelection();
 }
Beispiel #7
0
        private CustomStyle MakeCustomFromAttributes()
        {
            var family = attributes.ContainsKey("font-family") ? attributes["font-family"] : "Calibri";

            var size = 11;

            if (attributes.ContainsKey("font-size"))
            {
                if (float.TryParse(attributes["font-size"].Replace("pt", string.Empty), out var fs))
                {
                    size = (int)fs;
                }
            }

            FontStyle style = FontStyle.Regular;

            if (attributes.ContainsKey("font-weight") && attributes["font-weight"].Equals("bold"))
            {
                style |= FontStyle.Bold;
            }

            if (attributes.ContainsKey("font-style") && attributes["font-style"].Equals("italic"))
            {
                style |= FontStyle.Italic;
            }

            if (attributes.ContainsKey("text-decoration") && attributes["text-decoration"].Equals("underline"))
            {
                style |= FontStyle.Underline;
            }

            Color color = Color.Black;

            if (attributes.ContainsKey("color"))
            {
                color = ColorTranslator.FromHtml(attributes["color"]);
            }

            Color background = Color.Transparent;

            if (attributes.ContainsKey("background"))
            {
                background = ColorTranslator.FromHtml(attributes["background"]);
            }

            int spaceBefore = 0;

            if (attributes.ContainsKey("spaceBefore"))
            {
                int.TryParse(attributes["spaceBefore"], out spaceBefore);
            }

            int spaceAfter = 0;

            if (attributes.ContainsKey("spaceAfter"))
            {
                int.TryParse(attributes["spaceAfter"], out spaceAfter);
            }

            var custom = new CustomStyle(
                "Style-" + new Random().Next(1000, 9999).ToString(),
                new Font(family, size, style), color, background, spaceBefore, spaceAfter);

            return(custom);
        }
Beispiel #8
0
        private CustomStyle ReadStyle(XElement template, bool scaling = false)
        {
            float scaleFactor = 1f;

            if (scaling)
            {
                using (var bitmap = new Bitmap(1, 1))
                {
                    using (var graphics = Graphics.FromImage(bitmap))
                    {
                        scaleFactor = 96 / graphics.DpiY;
                    }
                }
            }

            var aName = template.Attribute("name")?.Value
                        ?? "Style-" + new Random().Next(1000, 9999).ToString();

            var aFamily      = template.Attribute("fontFamily")?.Value ?? "Calibri";
            var aSize        = template.Attribute("fontSize")?.Value ?? "10";
            var aStyle       = template.Attribute("fontStyle")?.Value ?? "Regular";
            var aTextColor   = template.Attribute("color")?.Value ?? "Black";
            var aHighColor   = template.Attribute("background")?.Value ?? String.Empty;
            var aSpaceAfter  = template.Attribute("spaceAfter")?.Value ?? "0";
            var aSpaceBefore = template.Attribute("spaceBefore")?.Value ?? "0";
            var aHeading     = template.Attribute("isHeading")?.Value ?? "False";

            if (!float.TryParse(aSize, out var size))
            {
                size = 11f;
            }

            FontStyle fontStyle = FontStyle.Regular;
            var       bits      = aStyle.Split('+');

            foreach (var bit in bits)
            {
                if (Enum.TryParse <FontStyle>(bit, out var styleBit))
                {
                    fontStyle |= styleBit;
                }
            }

            Font font = null;

            try
            {
                font = new Font(aFamily, size * scaleFactor, fontStyle);
            }
            catch (Exception exc)
            {
                Logger.Current.WriteLine("Error translating color" + aTextColor);
                Logger.Current.WriteLine(exc);
                font = new Font("Calibri", 11 * scaleFactor, fontStyle);
            }

            var textColor = Color.Black;

            try
            {
                textColor = ColorTranslator.FromHtml(aTextColor);
            }
            catch (Exception exc)
            {
                Logger.Current.WriteLine("Error translating color" + aTextColor);
                Logger.Current.WriteLine(exc);
            }

            var highColor = Color.Empty;

            if (String.IsNullOrEmpty(aHighColor))
            {
                highColor = Color.Transparent;
            }
            else
            {
                try
                {
                    highColor = ColorTranslator.FromHtml(aHighColor);
                }
                catch (Exception exc)
                {
                    Logger.Current.WriteLine("Error translating background" + aHighColor);
                    Logger.Current.WriteLine(exc);
                }
            }

            int.TryParse(aSpaceAfter, out var spaceAfter);
            int.TryParse(aSpaceBefore, out var spaceBefore);

            bool.TryParse(aHeading, out var isHeading);

            var style = new CustomStyle(aName,
                                        font, textColor, highColor, spaceBefore, spaceAfter, isHeading);

            return(style);
        }