File size: 7,303 Bytes
fab29d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) Dr. Dirk Lellinger. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Documents;

namespace HtmlToFlowDocument.Rendering
{
  /// <summary>
  /// Helper functions for enumeration through the tree of <see cref="TextElement"/>s.
  /// </summary>
  public static class WpfHelper
  {
    /// <summary>
    /// Gets the immediate childs (but not the grand childs) of the given Wpf <see cref="TextElement"/> or <see cref="FlowDocument"/>.
    /// </summary>
    /// <param name="wpf">The Wpf text element.</param>
    /// <returns>The immediate childs (but not the grand childs) of the given <see cref="TextElement"/> or <see cref="FlowDocument"/>.</returns>
    public static IEnumerable<TextElement> GetImmediateChildsOf(FrameworkContentElement wpf)
    {
      switch (wpf)
      {
        case Figure figure:
          return figure.Blocks;
        case Floater floater:
          return floater.Blocks;
        case FlowDocument flowDocument:
          return flowDocument.Blocks;
        case List list:
          return list.ListItems;
        case ListItem listItem:
          return listItem.Blocks;
        case Section section:
          return section.Blocks;
        case Table table:
          return table.RowGroups;
        case TableCell tableCell:
          return tableCell.Blocks;
        case TableRow tableRow:
          return tableRow.Cells;
        case TableRowGroup tableRowGroup:
          return tableRowGroup.Rows;
        // now elements that can contain inlines
        case Paragraph paragraph:
          return paragraph.Inlines;
        case Span span:
          return span.Inlines;
        default:
          return new TextElement[0];
      }
    }

    /// <summary>
    /// Gets a bookmark string from a given text element. In a document of exactly the same structure this bookmark string
    /// can be used to find the TextElement again by using <see cref="GetTextElementFromBookmark(FrameworkContentElement, string)"/>.
    /// </summary>
    /// <param name="wpf">The text element.</param>
    /// <returns>A bookmark string that can be used to find that text element again.</returns>
    public static string GetBookmarkFromTextElement(FrameworkContentElement wpf)
    {
      var result = new StringBuilder();
      var child = wpf;
      while (child.Parent is FrameworkContentElement parent)
      {

        int index = 0;
        foreach (var c in GetImmediateChildsOf(parent))
        {
          if (object.ReferenceEquals(c, child))
          {
            break;
          }
          else
          {
            ++index;
          }
        }

        result.Insert(0, string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0} ", index));

        child = parent;
      }
      return result.ToString();
    }

    /// <summary>
    /// Given the bookmark string returned by <see cref="GetBookmarkFromTextElement(FrameworkContentElement)"/>, in a FlowDocument
    /// of the same structure this function returns the same text element again.
    /// </summary>
    /// <param name="doc">The flow document.</param>
    /// <param name="bookmark">The bookmark string.</param>
    /// <returns>The text element that corresponds with the bookmark string.</returns>
    public static FrameworkContentElement GetTextElementFromBookmark(FlowDocument doc, string bookmark)
    {
      var bm = bookmark.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
      FrameworkContentElement result = doc;

      foreach (var numStr in bm)
      {
        int num = int.Parse(numStr, System.Globalization.CultureInfo.InvariantCulture);

        foreach (var c in GetImmediateChildsOf(result))
        {
          if (num == 0)
          {
            result = c;
            break;
          }
          else
          {
            --num;
          }
        }
      }
      return result;
    }

    /// <summary>
    /// Gets the text elements beginning with the text element given in the argument, and then all subsequent text elements of higher, equal, or lower levels until the end of the document.
    /// </summary>
    /// <param name="te">The <see cref="TextElement"/> to start with.</param>
    /// <returns>An enumeration beginning with the text element given in the argument, and then all subsequent text elements of higher, equal, or lower levels, until the end of the document.</returns>
    public static IEnumerable<TextElement> GetTextElementsBeginningWith(TextElement te)
    {
      foreach (var children in GetThisAndAllChildTextElements(te))
        yield return children;

      foreach (var other in GetTextElementsAfter(te))
        yield return other;
    }

    /// <summary>
    /// Returns an enumeration which contains the TextElement given in the argument and all child elements, including grandchilds and childs of higher levels.
    /// </summary>
    /// <param name="te">The <see cref="TextElement"/> to start the enumeration with.</param>
    /// <returns>An enumeration which contains the TextElement given in the argument and all child elements, including grandchilds and childs of higher levels.</returns>
    public static IEnumerable<TextElement> GetThisAndAllChildTextElements(TextElement te)
    {
      yield return te;
      foreach (var child in GetImmediateChildsOf(te))
        foreach (var e in GetThisAndAllChildTextElements(child).OfType<TextElement>())
          yield return e;
    }

    /// <summary>
    /// Returns an enumeration which contains all <see cref="TextElement"/>s which come after the <see cref="TextElement"/> given in the argument.
    /// The <see cref="TextElement"/> given in the argument is <b>not</b> included in the returned enumeration.
    /// </summary>
    /// <param name="te">The <see cref="TextElement"/> which is not included in the enumeration.</param>
    /// <returns>An enumeration which contains all <see cref="TextElement"/>s which come after the <see cref="TextElement"/> given in the argument.
    /// The <see cref="TextElement"/> given in the argument is <b>not</b> included in the returned enumeration.
    /// </returns>
    public static IEnumerable<TextElement> GetTextElementsAfter(TextElement te)
    {
      var parent = te.Parent as FrameworkContentElement;
      if (null == parent)
        yield break;

      bool stopAtNext = false;
      TextElement nextChild = null;
      foreach (var c in GetImmediateChildsOf(parent))
      {
        if (stopAtNext)
        {
          nextChild = c;
          break;
        }
        if (object.ReferenceEquals(te, c))
        {
          stopAtNext = true;
        }
      }

      if (null == nextChild)
      {
        // go one level back
        if (parent is TextElement parentAsTextElement)
        {
          foreach (var c in GetTextElementsAfter(parentAsTextElement))
            yield return c;
        }
      }
      else
      {
        foreach (var c in GetThisAndAllChildTextElements(nextChild))
          yield return c;
        foreach (var c in GetTextElementsAfter(nextChild))
          yield return c;
      }
    }
  }
}