-
Notifications
You must be signed in to change notification settings - Fork 18
/
ImlaBoya.cs
111 lines (99 loc) · 2.68 KB
/
ImlaBoya.cs
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
/*
* Created by SharpDevelop.
* User: Gheyret Kenji
* Date: 2020/12/07
* Time: 10:20
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using UyghurEditPP.Rendering;
using UyghurEditPP.Document;
using System.Windows.Media;
using System.Windows;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace UyghurEditPP
{
/// <summary>
/// Description of SinaqReng.
/// </summary>
public class ImlaBoya:DocumentColorizingTransformer
{
readonly TextDecorationCollection gCollection;
public ImlaBoya(){
SpellCheker = null;
gCollection = new TextDecorationCollection();
TextDecoration dec = new TextDecoration();
dec.Pen = new Pen { Thickness = 2, DashStyle = DashStyles.Dash, Brush = new SolidColorBrush(Colors.Red) };
dec.PenThicknessUnit = TextDecorationUnit.FontRecommended;
gCollection.Add(dec);
gCollection.Freeze();
FindReplace=false;
}
public IEnumerable<Match> FindWords(string text)
{
if(WordFinder==null) yield return null;
foreach (Match m in WordFinder.Matches(text))
{
yield return m;
}
}
public string Selection{
get;
set;
}
public int SelectionOffset{
get;
set;
}
public Regex WordFinder{
get;
set;
}
public UyghurSpell SpellCheker{
get;
set;
}
public bool FindReplace{
get;
set;
}
protected override void ColorizeLine(DocumentLine line)
{
if (line.Length == 0)
return;
int lineStartOffset = line.Offset;
string text = CurrentContext.Document.GetText(line).ToLower();
if(WordFinder!=null && SpellCheker!=null)
{
foreach(Match soz in FindWords(text))
{
if(soz!=null && SpellCheker.IsListed(soz.Value.Replace(Uyghur.Sozghuch,""))==false)
{
ChangeLinePart(lineStartOffset + soz.Index,lineStartOffset + soz.Index +soz.Value.Length ,ApplyChanges);
}
}
}
if(!FindReplace && !string.IsNullOrEmpty(Selection) && Selection.Length>0){
int start = 0;
int index;
while ((index = text.IndexOf(Selection, start, StringComparison.Ordinal)) >= 0)
{
if((lineStartOffset + index)!=SelectionOffset){
ChangeLinePart(lineStartOffset + index, lineStartOffset + index + Selection.Length, HilightSelection);
}
start = index + 1; // search for next occurrence
}
}
}
void ApplyChanges(VisualLineElement element)
{
element.TextRunProperties.SetTextDecorations(gCollection);
}
void HilightSelection(VisualLineElement element)
{
element.TextRunProperties.SetBackgroundBrush(Brushes.Yellow);
}
}
}