Ejemplo n.º 1
0
        private int AlterSelections()
        {
            var elements = page.Root.Element(ns + "Outline").Descendants(ns + "T")
                           .Where(e => e.Attributes("selected").Any(a => a.Value == "all"));

            if (elements.IsNullOrEmpty())
            {
                return(0);
            }

            var count    = 0;
            var analyzer = new StyleAnalyzer(page.Root, true);

            foreach (var element in elements)
            {
                analyzer.Clear();
                var style = new Style(analyzer.CollectStyleProperties(element));

                // add .05 to compensate for unpredictable behavior; there are cases where going
                // from 11pt to 12pt actually causes OneNote to calculate 9pt :-(
                style.FontSize = (ParseFontSize(style.FontSize) + delta).ToString("#0") + ".05pt";

                var stylizer = new Stylizer(style);
                stylizer.ApplyStyle(element);
                count++;
            }

            return(count);
        }
Ejemplo n.º 2
0
    public void UpdateKey(bool giveWarning = true)
    {
        if (_textField == default)
        {
            // _textField has been deleted, but garbage collection
            //  has not called OnDestroy() yet.
            OnDestroy();
            return;
        }

        Stylizer.ApplyStyle(_textField, Style, giveWarning);

        switch (Key)
        {
        case ".languageString":
            _textField.text = Localizer.LanguageString;
            break;

        case ".languageName":
            _textField.text = Localizer.LanguageName.ToString();
            break;

        case ".languageType":
            _textField.text = Localizer.LanguageType.ToString();
            break;

        case ".languageCode":
            _textField.text = Localizer.LanguageCode.ToString();
            break;

        default:
            _textField.text = Localizer.Value(Key, Variables, giveWarning);
            break;
        }
    }
Ejemplo n.º 3
0
        private bool ClearTextBackground(IEnumerable <XElement> runs, bool deep = false)
        {
            var updated = false;

            foreach (var run in runs)
            {
                var style = new Style(analyzer.CollectFrom(run, true));

                if (!string.IsNullOrEmpty(style.Highlight))
                {
                    style.Highlight = Style.Automatic;
                }

                var darkText = !style.Color.Equals(Style.Automatic) &&
                               ColorTranslator.FromHtml(style.Color).GetBrightness() < 0.5;

                // if dark-on-dark or light-on-light
                if (darkText == darkPage)
                {
                    style.Color = darkText ? White : Black;
                }

                var stylizer = new Stylizer(style);
                stylizer.ApplyStyle(run);

                ClearHyperlinkBackground(run);
                updated = true;

                // deep prevents runs from being processed multiple times

                // NOTE sometimes OneNote will incorrectly set the collapsed attribute,
                // thinking it is set to 1 but is not visually collapsed!

                if (!deep && run.Parent.Attribute("collapsed")?.Value == "1")
                {
                    updated |= ClearTextBackground(
                        run.Parent.Descendants(ns + "T").Where(e => e != run),
                        true);
                }
            }

            return(updated);
        }
Ejemplo n.º 4
0
        private bool StylizeWords()
        {
            // find all selected T element; may be multiple if text is selected across
            // multiple paragraphs - OEs - but partial paragraphs may be selected too...

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

            if (selections == null)
            {
                // shouldn't happen, but...
                return(false);
            }

            foreach (var selection in selections)
            {
                bool empty = true;

                if (selection.Parent.Nodes().Count() == 1)
                {
                    // OE parent must have only this T

                    stylizer.ApplyStyle(selection);
                }
                else
                {
                    // OE parent has multiple Ts so test if we need to merge them

                    //logger.WriteLine("selection.parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));

                    var cdata = selection.GetCData();

                    // text cursor is positioned but selection length is 0
                    if (cdata.IsEmpty())
                    {
                        // inside a word, adjacent to a word, or somewhere in whitespace?

                        var prev = selection.PreviousNode as XElement;
                        if ((prev != null) && prev.GetCData().EndsWithWhitespace())
                        {
                            prev = null;
                        }

                        var next = selection.NextNode as XElement;
                        if ((next != null) && next.GetCData().StartsWithWhitespace())
                        {
                            next = null;
                        }

                        if ((prev != null) && (next != null))
                        {
                            empty = false;

                            // navigate to closest word

                            var word = new StringBuilder();

                            if (prev != null)
                            {
                                logger.WriteLine("prev:" + prev.ToString(SaveOptions.None));

                                if (!prev.GetCData().EndsWithWhitespace())
                                {
                                    // grab previous part of word
                                    word.Append(prev.ExtractLastWord());
                                    //logger.WriteLine("word with prev:" + word.ToString());
                                    //logger.WriteLine("prev updated:" + prev.ToString(SaveOptions.None));
                                    //logger.WriteLine("parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));

                                    if (prev.GetCData().Value.Length == 0)
                                    {
                                        prev.Remove();
                                    }
                                }
                            }

                            if (next != null)
                            {
                                logger.WriteLine("next:" + next.ToString(SaveOptions.None));

                                if (!next.GetCData().StartsWithWhitespace())
                                {
                                    // grab following part of word
                                    word.Append(next.ExtractFirstWord());
                                    //logger.WriteLine("word with next:" + word.ToString());
                                    //logger.WriteLine("next updated:" + next.ToString(SaveOptions.None));
                                    //logger.WriteLine("parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));

                                    if (next.GetCData().Value.Length == 0)
                                    {
                                        next.Remove();
                                    }
                                }
                            }

                            if (word.Length > 0)
                            {
                                selection.DescendantNodes().OfType <XCData>()
                                .First()
                                .ReplaceWith(new XCData(word.ToString()));

                                //logger.WriteLine("parent udpated:" + (selection.Parent as XElement).ToString(SaveOptions.None));
                            }
                            else
                            {
                                empty = true;
                            }

                            //logger.WriteLine("parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));
                        }
                    }

                    if (empty)
                    {
                        stylizer.ApplyStyle(selection.GetCData());
                        //logger.WriteLine("final empty parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));
                    }
                    else
                    {
                        stylizer.ApplyStyle(selection);
                        //logger.WriteLine("final parent:" + (selection.Parent as XElement).ToString(SaveOptions.None));
                    }
                }
            }

            return(true);
        }