Exemplo n.º 1
0
        private static Paragraph GenerateRuns(Paragraph p, string Buffer)
        {
            // Calculate positions of all tokens and use this to set
            // run styles when iterating through the string

            // in the same calculation note down location of tokens
            // so they can be ignored when loading strings into the buffer

            Regex pBold, pItalic, pUnderline;

            if (!CustomMode)
            {
                pBold      = Patterns[rExKey.DblAsterisk];
                pItalic    = Patterns[rExKey.Asterisk];
                pUnderline = Patterns[rExKey.Underscore];
            }
            else
            {
                pBold      = Patterns[rExKey.DblAsterisk];
                pItalic    = Patterns[rExKey.Grave];
                pUnderline = Patterns[rExKey.Underscore];
            }


            Ranges <int> Tokens    = new Ranges <int>();
            Ranges <int> Bold      = FindMatches(pBold, Buffer, ref Tokens);
            Ranges <int> Italic    = FindMatches(pItalic, Buffer, ref Tokens);
            Ranges <int> Underline = FindMatches(pUnderline, Buffer, ref Tokens);

            Ranges <int> Tabs = FindTabs(new Regex(@"(\\t|[\\ ]{4})"), Buffer, ref Tokens);

            if ((Bold.Count() + Italic.Count() + Underline.Count() + Tabs.Count()) == 0)
            {
                Run run = new Run();
                run.Append(new Text(Buffer)
                {
                    Space = SpaceProcessingModeValues.Preserve
                });
                p.Append(run);
            }
            else
            {
                int CurrentPosition = 0;
                Run run;

                // This needs optimizing so it builds a string buffer before adding the run itself

                while (CurrentPosition < Buffer.Length)
                {
                    if (!Tokens.ContainsValue(CurrentPosition) || Tabs.ContainsValue(CurrentPosition))
                    {
                        run = new Run();

                        if (Tabs.ContainsValue(CurrentPosition))
                        {
                            run.Append(new TabChar());
                        }
                        else
                        {
                            RunProperties rPr = new RunProperties();
                            if (Bold.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Bold()
                                {
                                    Val = new OnOffValue(true)
                                });
                            }
                            if (Italic.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Italic());
                            }
                            if (Underline.ContainsValue(CurrentPosition))
                            {
                                rPr.Append(new Underline()
                                {
                                    Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single
                                });
                            }
                            run.Append(rPr);

                            string TextBuffer = Buffer.Substring(CurrentPosition, 1);
                            run.Append(new Text(TextBuffer)
                            {
                                Space = SpaceProcessingModeValues.Preserve
                            });
                        }
                        p.Append(run);
                    }
                    CurrentPosition++;
                }
            }
            return(p);
        }
 public bool HasMatches()
 {
     return(matches.Count() > 0);
 }