示例#1
0
        public string Rebuild(Phrase phrase, int defaultSize, int delta)
        {
            string data = string.Empty;

            var wrap = phrase.GetSanitizedWrapper();

            foreach (var node in wrap.Nodes())
            {
                if (node.NodeType == XmlNodeType.Text)
                {
                    data += "<span style=font-size:" + defaultSize + "pt>" + ((XText)node).Value + "</span>";
                }
                else
                {
                    var info = new CssInfo();
                    CollectFromSpan((XElement)node, info);
                    if (info.FontSize == null)
                    {
                        info.FontSize = defaultSize.ToString("#.0") + "pt";
                    }
                    else
                    {
                        info.FontSize = (ParseSize(info.FontSize) + delta).ToString("#.0") + "pt";
                    }

                    data += "<span style=" + info.ToCss() + ">" + ((XElement)node).Value + "</span>";
                }
            }

            return(data);
        }
示例#2
0
        private void CollectFromObjectElement(XElement element, CssInfo info, List <IStyleInfo> templates)
        {
            CollectFromSpan(element, info);

            var a = element.Attribute("quickStyleIndex");

            if (a != null)
            {
                var template = templates.Where(e => a.Value.Equals(e.Index)).FirstOrDefault();
                if (template != null)
                {
                    info.Collect(template);
                }
            }

            a = element.Attribute("spaceBefore");
            if (a != null)
            {
                info.SpaceBefore = a.Value;
            }

            a = element.Attribute("spaceAfter");
            if (a != null)
            {
                info.SpaceAfter = a.Value;
            }
        }
示例#3
0
        private void CollectFromSpan(XElement element, CssInfo info)
        {
            var spanstyle = element.Attributes("style").Select(a => a.Value).FirstOrDefault();

            if (spanstyle != null)
            {
                info.Collect(spanstyle);
            }
        }
示例#4
0
        private void Evaluate(bool increase)
        {
            var templates = GetQuickStyleDefs();

            // Find all TextRanges (one:T) - each and every one!

            var ranges = page.Elements(ns + "Outline")?.Descendants(ns + "T");

            if (ranges?.Count() > 0)
            {
                foreach (var range in ranges)
                {
                    var phrase = new Phrase(range);
                    if (!phrase.IsEmpty)
                    {
                        var info = new CssInfo();

                        // collect from one:T
                        CollectFromSpan(range, info);

                        // collect from one:OE
                        CollectFromObjectElement(range.Parent, info, templates);

                        if (info.FontSize == null)
                        {
                            info.FontSize = defaultInfo.FontSize;
                        }

                        var delta       = increase ? 1 : -1;
                        var defaultSize = ParseSize(info.FontSize) + delta;

                        var data = Rebuild(phrase, defaultSize, delta);

                        range.FirstNode.ReplaceWith(new XCData(data));
                    }
                }
            }
        }
示例#5
0
文件: Phrase.cs 项目: dweedul/OneMore
        public CssInfo GetStyleInfo()
        {
            if (IsEmpty)
            {
                return(null);
            }

            var wrap = GetSanitizedWrapper();
            var ns   = wrap.GetDefaultNamespace();

            CssInfo info = null;

            var spans = wrap.Elements(ns + "span").Select(e => e.Attribute("style").Value);

            if (spans?.Count() > 0)
            {
                foreach (var css in spans)
                {
                    var next = new CssInfo(css);
                    if (info == null)
                    {
                        info = next;
                    }
                    else if (!next.Matches(info))
                    {
                        // conflicts across multiple span elements
                        // so a single overall style cannot be determined
                        return(null);
                    }
                }
            }
            else
            {
                return(new CssInfo());
            }

            return(info);
        }
示例#6
0
        private void Evaluate(ApplicationManager manager)
        {
            //System.Diagnostics.Debugger.Launch();

            var headings  = new List <Heading>();
            var templates = GetHeadingTemplates();

            // get page.ObjectId for hyperlinks
            var pid = page.Attribute("ID")?.Value;

            // Find all OEs that might be headings: must contain one T block; if there are more
            // than one T block then only allow the OE if those Ts represent current selection

            var candidates =
                from e in page.Element(ns + "Outline").Element(ns + "OEChildren").Elements(ns + "OE")
                let n = e.Elements(ns + "T").Count()
                        where n == 1 || (n <= 3 && e.Elements(ns + "T").Attributes("selected").Any(a => a.Value.Equals("all")))
                        select e;

            if (candidates?.Count() > 0)
            {
                foreach (var candidate in candidates)
                {
                    CssInfo info = null;

                    var phrases = candidate.Elements(ns + "T").Select(e => new Phrase(e));
                    foreach (var phrase in phrases)
                    {
                        if (!phrase.IsEmpty)
                        {
                            info = phrase.GetStyleInfo();
                            if (info != null)
                            {
                                // collect from one:T
                                CollectFromSpan(phrase.CData.Parent, info);
                            }
                        }
                    }

                    if (info != null)
                    {
                        // collect from one:OE
                        CollectFromObjectElement(candidate, info, templates);

                        var template = templates.Where(t => t.Matches(info)).FirstOrDefault();
                        if (template != null)
                        {
                            string link = null;
                            if (pid != null)
                            {
                                var oid = candidate.Attribute("objectID")?.Value;
                                if (oid != null)
                                {
                                    manager.Application.GetHyperlinkToObject(pid, oid, out link);
                                }
                            }

                            // capture only first-level one:T text ranges, ignoring OEChildren
                            // which would represent something like a bullet list or indented child
                            var text = new XElement("x", candidate.Elements(ns + "T")).Value;

                            var heading = new Heading()
                            {
                                Text  = ClearFormatting(text),
                                Link  = link,
                                Level = template.Level
                            };

                            logger.WriteLine($"Heading => {heading.Text} ({heading.Level})");
                            headings.Add(heading);
                        }
                    }
                }
            }

            InsertToc(headings);
        }