private Style FindCustomStyle(XElement block) { var child = block.Descendants(Namespace + "T").FirstOrDefault(); if (child != null) { var analyzer = new StyleAnalyzer(Root); var style = new Style(analyzer.CollectStyleProperties(child)); // normalize style background to page background if (style.Highlight != pageColor && pageColor == "automatic") { if (style.Highlight == "#FFFFFF") { style.Highlight = "automatic"; } } // compare and find var find = customStyles.Find(s => s.Equals(style)); return(find); } return(null); }
public override async Task Execute(params object[] args) { using (var one = new OneNote(out var page, out ns)) { analyzer = new StyleAnalyzer(page.Root); var style = analyzer.CollectFromSelection(); var ok = (style != null) && NormalizeTextCursor(page, analyzer); if (!ok) { UIHelper.ShowInfo(one.Window, Resx.Error_BodyContext); return; } var runs = page.Root.Descendants(ns + "T").ToList(); foreach (var run in runs) { AnalyzeRun(run, style); } //logger.WriteLine(page.Root); //logger.WriteLine($"Catalog depth:{analyzer.Depth}, hits:{analyzer.Hits}"); await one.Update(page); } await Task.Yield(); }
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); }
public override async Task Execute(params object[] args) { Page page; Color pageColor; using (var one = new OneNote(out page, out _)) { pageColor = page.GetPageColor(out _, out _); } var analyzer = new StyleAnalyzer(page.Root); var style = analyzer.CollectFromSelection(); if (style == null) { return; } using (var dialog = new StyleDialog(style, pageColor)) { if (dialog.ShowDialog(owner) == DialogResult.OK) { if (dialog.Style != null) { ThemeProvider.Save(dialog.Style); ribbon.Invalidate(); } } } await Task.Yield(); }
public override async Task Execute(params object[] args) { using (one = new OneNote(out page, out ns)) { darkPage = page.GetPageColor(out _, out _).GetBrightness() < 0.5; analyzer = new StyleAnalyzer(page.Root); var updated = ClearTextBackground(page.GetSelectedElements(all: true)); updated |= ClearCellBackground(); if (updated) { await one.Update(page); } } }
// merge text cursor so we don't have to treat it as a special case private bool NormalizeTextCursor(Page page, StyleAnalyzer analyzer) { var cursor = page.GetTextCursor(); if (cursor == null || page.SelectionScope != SelectionScope.Empty) { return(false); } if (cursor.PreviousNode is XElement prev && cursor.NextNode is XElement next) { var prevData = prev.GetCData(); var nextData = next.GetCData(); var prevWrap = prevData.GetWrapper(); var nextWrap = nextData.GetWrapper(); if (prevWrap.Nodes().Last() is XElement prevSpan && nextWrap.Nodes().First() is XElement nextSpan) { // examine the last node from Prev and the first node from Next // to compare their styles and if they match then combine them var prevStyle = analyzer.CollectStyleFrom(prevSpan); var nextStyle = analyzer.CollectStyleFrom(nextSpan); if (prevStyle.Equals(nextStyle)) { // pull only the first node from Next and append it to Prev // then remove that first node from Next and append the remaining nodes // to retain their own stylings; finally remove Next altogether below... prevSpan.Value = $"{prevSpan.Value}{nextSpan.Value}"; nextSpan.Remove(); prevWrap.Add(nextWrap.Nodes()); prevData.Value = prevWrap.GetInnerXml(singleQuote: true); } else { prevData.Value = $"{prevData.Value}{nextData.Value}"; } }
/* * <one:OE > * <one:T><![CDATA[<span * style='font-family:Calibri'>This is the third </span><span style='font-weight: * bold;font-style:italic;text-decoration:underline line-through;font-family:Consolas; * color:#70AD47'>li</span>]]></one:T> * <one:T selected="all" style="font-family:Consolas;font-size:12.0pt;color:#70AD47"><![CDATA[]]></one:T> * <one:T style="font-family:Consolas;font-size:12.0pt;color:#70AD47"><![CDATA[<span style='font-weight:bold;font-style:italic; * text-decoration:underline line-through'>ne </span>]]></one:T> * </one:OE> */ /// <summary> /// Infer styles from the context at the position of the text cursor on the current page. /// </summary> private static Style CollectStyleFromContext() { // infer contextual style Page page = null; using (var one = new OneNote()) { page = one.GetPage(); } if (page == null) { return(null); } var ns = page.Namespace; var selection = (from e in page.Root.Descendants(ns + "T") where e.Attributes("selected").Any(a => a.Value.Equals("all")) select e).FirstOrDefault(); if (selection != null) { var analyzer = new StyleAnalyzer(page.Root, inward: false); var cdata = selection.GetCData(); if (cdata.IsEmpty()) { // inside a word, adjacent to a word, or somewhere in whitespace? if ((selection.PreviousNode is XElement prev) && !prev.GetCData().EndsWithWhitespace()) { selection = prev; if ((selection.DescendantNodes()? .OfType <XCData>() .LastOrDefault() is XCData data) && !string.IsNullOrEmpty(data?.Value)) { var wrapper = data.GetWrapper(); // if last node is text then skip the cdata and examine the parent T // otherwise if last node is a span then start with its style var last = wrapper.Nodes().Reverse().First(n => n.NodeType == XmlNodeType.Text || ((n is XElement ne) && ne.Name.LocalName == "span")); if (last?.NodeType == XmlNodeType.Element) { var wspan = last as XElement; if (wspan.Attribute("style") != null) { analyzer.CollectStyleProperties(wspan); } } } } else { if ((selection.NextNode is XElement next) && !next.GetCData().StartsWithWhitespace()) { selection = next; if ((selection.DescendantNodes()? .OfType <XCData>() .FirstOrDefault() is XCData data) && !string.IsNullOrEmpty(data?.Value)) { var wrapper = data.GetWrapper(); // if first node is text then skip the cdata and examine the parent T // otherwise if first node is a span then start with its style var last = wrapper.Nodes().First(n => n.NodeType == XmlNodeType.Text || ((n is XElement ne) && ne.Name.LocalName == "span")); if (last?.NodeType == XmlNodeType.Element) { var wspan = last as XElement; if (wspan.Attribute("style") != null) { analyzer.CollectStyleProperties(wspan); } } } } } } var properties = analyzer.CollectStyleProperties(selection); var style = new Style(properties) { Name = "Style-" + new Random().Next(1000, 9999).ToString() }; return(style); }
public override async Task Execute(params object[] args) { using (var one = new OneNote(out var page, out var ns, OneNote.PageDetail.Basic)) { var rule = page.Root .Elements(ns + "PageSettings") .Elements(ns + "RuleLines") .Where(e => e.Attribute("visible")?.Value == "true") .Select(e => e.Element(ns + "Horizontal")) .FirstOrDefault(); if (rule == null) { UIHelper.ShowMessage(Resx.FitGridToTextCommand_noGrid); return; } var quickStyles = page.GetQuickStyles().Where(s => s.Name == "p"); if (!quickStyles.Any()) { UIHelper.ShowMessage(Resx.FitGridToTextCommand_noText); return; } var pindexes = quickStyles.Select(s => s.Index.ToString()); var common = page.Root.Descendants(ns + "OE") // find all "p" paragraphs .Where(e => pindexes.Contains(e.Attribute("quickStyleIndex").Value)) .Select(e => new { Element = e, Index = e.Attribute("quickStyleIndex").Value, Css = e.Attribute("style")?.Value }) // count instances of distinct combinations .GroupBy(o => new { o.Index, o.Css }) .Select(group => new { group.Key.Index, group.First().Element, Count = group.Count() }) // grab the most commonly used; if there are two equally // used styles then this is arbitrary but OK .OrderByDescending(g => g.Count) .FirstOrDefault(); if (common != null) { //var quickStyle = quickStyles.FirstOrDefault(s => s.Index.ToString() == common.Index); var analyzer = new StyleAnalyzer(page.Root); var style = analyzer.CollectStyleFrom(common.Element); var height = CalculateLineHeight(style); using (var dialog = new FitGridToTextDialog(style.FontSize, height)) { if (dialog.ShowDialog(owner) == System.Windows.Forms.DialogResult.OK) { rule.Attribute("spacing").Value = dialog.Spacing.ToString(); var vertical = rule.Parent.Element(ns + "Vertical"); if (vertical != null) { vertical.Attribute("spacing").Value = dialog.Spacing.ToString(); } await one.Update(page); } } } } }