private void GenerateRuns() { // 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 if (!PatternsHaveMatches()) { run = new Run(); run.Append(new Text(md) { Space = SpaceProcessingModeValues.Preserve }); para.Append(run); } else { int pos = 0; string buffer = ""; // This needs optimizing so it builds a string buffer before adding the run itself while (pos < md.Length) { if (!Tokens.ContainsValue(pos)) { buffer += md.Substring(pos, 1); } else if (buffer.Length > 0) { run = new Run(); RunProperties rPr = new RunProperties(); Bold.SetFlagFor(pos - 1); Italic.SetFlagFor(pos - 1); Underline.SetFlagFor(pos - 1); BulletList.SetFlagFor(pos - 1); if (Bold.Flag) { rPr.Append(new Bold() { Val = new OnOffValue(true) }); } if (Italic.Flag) { rPr.Append(new Italic()); } if (Underline.Flag) { rPr.Append(new Underline() { Val = DocumentFormat.OpenXml.Wordprocessing.UnderlineValues.Single }); } if (BulletList.Flag) { rPr.Append(new NumberingFormat() { Val = NumberFormatValues.Bullet }); } run.Append(rPr); run.Append(new Text(buffer) { Space = SpaceProcessingModeValues.Preserve }); if (Tab.ContainsValue(pos)) { run.Append(new TabChar()); } para.Append(run); buffer = ""; } pos++; } ; } }
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 ContainsValue(int num) { return(matches.ContainsValue(num)); }