2 * Copyright (C) 2013 FooProject
3 * * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
6 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
9 You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
12 using System.Collections.Generic;
15 using System.Windows.Media;
16 using System.Windows.Media.TextFormatting;
18 namespace FooEditEngine.WPF
20 // CustomTextSource is our implementation of TextSource. This is required to use the WPF
21 // text engine. This implementation is very simplistic as is DOES NOT monitor spans of text
22 // for different properties. The entire text content is considered a single span and all
23 // changes to the size, alignment, font, etc. are applied across the entire text.
24 sealed class CustomTextSource : TextSource
26 // Used by the TextFormatter object to retrieve a run of text from the text source.
27 public override TextRun GetTextRun(int textSourceCharacterIndex)
29 // Make sure text source index is in bounds.
30 if (textSourceCharacterIndex < 0)
31 throw new ArgumentOutOfRangeException("textSourceCharacterIndex", "Value must be greater than 0.");
32 if (textSourceCharacterIndex >= _text.Length)
34 return new TextEndOfParagraph(1);
37 if (this.DecorationCollection != null)
39 foreach (TextDecorationInfo info in this.DecorationCollection)
41 if(textSourceCharacterIndex < info.Start)
42 return new TextCharacters(
44 textSourceCharacterIndex,
45 info.Start - textSourceCharacterIndex,
46 new GenericTextRunProperties(_currentRendering, this.EffectCollection));
47 else if (textSourceCharacterIndex >= info.Start && textSourceCharacterIndex < info.Start + info.Count)
48 return new TextCharacters(
50 textSourceCharacterIndex,
51 info.Start + info.Count - textSourceCharacterIndex,
52 new GenericTextRunProperties(_currentRendering, this.EffectCollection, info.DecorationCollection));
55 return new TextCharacters(
57 textSourceCharacterIndex,
58 _text.Length - textSourceCharacterIndex,
59 new GenericTextRunProperties(_currentRendering, this._effectCollection));
62 public override TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText(int textSourceCharacterIndexLimit)
64 CharacterBufferRange cbr = new CharacterBufferRange(_text, 0, textSourceCharacterIndexLimit);
65 return new TextSpan<CultureSpecificCharacterBufferRange>(
66 textSourceCharacterIndexLimit,
67 new CultureSpecificCharacterBufferRange(System.Globalization.CultureInfo.CurrentUICulture, cbr)
71 public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex(int textSourceCharacterIndex)
73 return textSourceCharacterIndex;
80 set { _text = value; }
83 public FontRendering FontRendering
85 get { return _currentRendering; }
86 set { _currentRendering = value; }
89 public TextEffectCollection EffectCollection
91 get { return _effectCollection; }
92 set { _effectCollection = value; }
95 public List<TextDecorationInfo> DecorationCollection
102 #region Private Fields
104 private string _text; //text store
105 private FontRendering _currentRendering;
106 private TextEffectCollection _effectCollection;
111 struct TextDecorationInfo
115 public TextDecorationCollection DecorationCollection;
116 public TextDecorationInfo(int start,int count,TextDecorationCollection collection)
120 this.DecorationCollection = collection;