Free cookie consent management tool by TermsFeed Policy Generator

source: branches/EnhancedProgress/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Utils/DocumentPrinter.cs @ 15683

Last change on this file since 15683 was 11700, checked in by jkarder, 10 years ago

#2077: created branch and added first version

File size: 4.6 KB
Line 
1// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4// software and associated documentation files (the "Software"), to deal in the Software
5// without restriction, including without limitation the rights to use, copy, modify, merge,
6// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7// to whom the Software is furnished to do so, subject to the following conditions:
8//
9// The above copyright notice and this permission notice shall be included in all copies or
10// substantial portions of the Software.
11//
12// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17// DEALINGS IN THE SOFTWARE.
18
19using System;
20using System.Collections.Generic;
21using System.Windows;
22using System.Windows.Documents;
23
24using ICSharpCode.NRefactory.Editor;
25using ICSharpCode.AvalonEdit.Document;
26using ICSharpCode.AvalonEdit.Highlighting;
27
28namespace ICSharpCode.AvalonEdit.Utils
29{
30  /// <summary>
31  /// Helps printing documents.
32  /// </summary>
33  public static class DocumentPrinter
34  {
35    #if NREFACTORY
36    /// <summary>
37    /// Converts a readonly TextDocument to a Block and applies the provided highlighting definition.
38    /// </summary>
39    public static Block ConvertTextDocumentToBlock(ReadOnlyDocument document, IHighlightingDefinition highlightingDefinition)
40    {
41      IHighlighter highlighter;
42      if (highlightingDefinition != null)
43        highlighter = new DocumentHighlighter(document, highlightingDefinition);
44      else
45        highlighter = null;
46      return ConvertTextDocumentToBlock(document, highlighter);
47    }
48    #endif
49   
50    /// <summary>
51    /// Converts an IDocument to a Block and applies the provided highlighter.
52    /// </summary>
53    public static Block ConvertTextDocumentToBlock(IDocument document, IHighlighter highlighter)
54    {
55      if (document == null)
56        throw new ArgumentNullException("document");
57      Paragraph p = new Paragraph();
58      p.TextAlignment = TextAlignment.Left;
59      for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
60        if (lineNumber > 1)
61          p.Inlines.Add(new LineBreak());
62        var line = document.GetLineByNumber(lineNumber);
63        if (highlighter != null) {
64          HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
65          p.Inlines.AddRange(highlightedLine.ToRichText().CreateRuns());
66        } else {
67          p.Inlines.Add(document.GetText(line));
68        }
69      }
70      return p;
71    }
72   
73    #if NREFACTORY
74    /// <summary>
75    /// Converts a readonly TextDocument to a RichText and applies the provided highlighting definition.
76    /// </summary>
77    public static RichText ConvertTextDocumentToRichText(ReadOnlyDocument document, IHighlightingDefinition highlightingDefinition)
78    {
79      IHighlighter highlighter;
80      if (highlightingDefinition != null)
81        highlighter = new DocumentHighlighter(document, highlightingDefinition);
82      else
83        highlighter = null;
84      return ConvertTextDocumentToRichText(document, highlighter);
85    }
86    #endif
87   
88    /// <summary>
89    /// Converts an IDocument to a RichText and applies the provided highlighter.
90    /// </summary>
91    public static RichText ConvertTextDocumentToRichText(IDocument document, IHighlighter highlighter)
92    {
93      if (document == null)
94        throw new ArgumentNullException("document");
95      var texts = new List<RichText>();
96      for (int lineNumber = 1; lineNumber <= document.LineCount; lineNumber++) {
97        var line = document.GetLineByNumber(lineNumber);
98        if (lineNumber > 1)
99          texts.Add(line.PreviousLine.DelimiterLength == 2 ? "\r\n" : "\n");
100        if (highlighter != null) {
101          HighlightedLine highlightedLine = highlighter.HighlightLine(lineNumber);
102          texts.Add(highlightedLine.ToRichText());
103        } else {
104          texts.Add(document.GetText(line));
105        }
106      }
107      return RichText.Concat(texts.ToArray());
108    }
109   
110    /// <summary>
111    /// Creates a flow document from the editor's contents.
112    /// </summary>
113    public static FlowDocument CreateFlowDocumentForEditor(TextEditor editor)
114    {
115      IHighlighter highlighter = editor.TextArea.GetService(typeof(IHighlighter)) as IHighlighter;
116      FlowDocument doc = new FlowDocument(ConvertTextDocumentToBlock(editor.Document, highlighter));
117      doc.FontFamily = editor.FontFamily;
118      doc.FontSize = editor.FontSize;
119      return doc;
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.