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); foreach (var element in elements) { var style = analyzer.CollectStyleFrom(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); }
// 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}"; } }
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); } } } } }