OSDN Git Service

初コミット
[fooeditengine/FooEditEngine.git] / WPF / FooEditEngine / WPF / CustomTextSource.cs
1 /*
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.
5
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.
8
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/>.
10  */
11 using System;
12 using System.Collections.Generic;
13 using System.Text;
14 using System.Windows;
15 using System.Windows.Media;
16 using System.Windows.Media.TextFormatting;
17
18 namespace FooEditEngine.WPF
19 {
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
25     {
26         // Used by the TextFormatter object to retrieve a run of text from the text source.
27         public override TextRun GetTextRun(int textSourceCharacterIndex)
28         {
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)
33             {
34                 return new TextEndOfParagraph(1);
35             }
36
37             if (this.DecorationCollection != null)
38             {
39                 foreach (TextDecorationInfo info in this.DecorationCollection)
40                 {
41                     if(textSourceCharacterIndex < info.Start)
42                         return new TextCharacters(
43                             _text,
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(
49                             _text,
50                             textSourceCharacterIndex,
51                             info.Start + info.Count - textSourceCharacterIndex,
52                             new GenericTextRunProperties(_currentRendering, this.EffectCollection, info.DecorationCollection));
53                 }
54             }
55             return new TextCharacters(
56                _text,
57                textSourceCharacterIndex,
58                _text.Length - textSourceCharacterIndex,
59                new GenericTextRunProperties(_currentRendering, this._effectCollection));
60         }
61
62         public override TextSpan<CultureSpecificCharacterBufferRange> GetPrecedingText(int textSourceCharacterIndexLimit)
63         {
64             CharacterBufferRange cbr = new CharacterBufferRange(_text, 0, textSourceCharacterIndexLimit);
65             return new TextSpan<CultureSpecificCharacterBufferRange>(
66              textSourceCharacterIndexLimit,
67              new CultureSpecificCharacterBufferRange(System.Globalization.CultureInfo.CurrentUICulture, cbr)
68              );
69         }
70
71         public override int GetTextEffectCharacterIndexFromTextSourceCharacterIndex(int textSourceCharacterIndex)
72         {
73             return textSourceCharacterIndex;
74         }
75
76         #region Properties
77         public string Text
78         {
79             get { return _text; }
80             set { _text = value; }
81         }
82
83         public FontRendering FontRendering
84         {
85             get { return _currentRendering; }
86             set { _currentRendering = value; }
87         }
88
89         public TextEffectCollection EffectCollection
90         {
91             get { return _effectCollection; }
92             set { _effectCollection = value; }
93         }
94
95         public List<TextDecorationInfo> DecorationCollection
96         {
97             get;
98             set;
99         }
100         #endregion
101
102         #region Private Fields
103
104         private string _text;      //text store
105         private FontRendering _currentRendering;
106         private TextEffectCollection _effectCollection;
107
108         #endregion
109     }
110
111     struct TextDecorationInfo
112     {
113         public int Start;
114         public int Count;
115         public TextDecorationCollection DecorationCollection;
116         public TextDecorationInfo(int start,int count,TextDecorationCollection collection)
117         {
118             this.Start = start;
119             this.Count = count;
120             this.DecorationCollection = collection;
121         }
122     }
123 }