1 | using System;
|
---|
2 | using System.Linq;
|
---|
3 | using System.Text;
|
---|
4 | using System.Collections.Generic;
|
---|
5 |
|
---|
6 | namespace SharpVectors.Renderers.Texts
|
---|
7 | {
|
---|
8 | public sealed class WpfTextRun
|
---|
9 | {
|
---|
10 | #region Private Fields
|
---|
11 |
|
---|
12 | private int _vertOrientation;
|
---|
13 | private int _horzOrientation;
|
---|
14 | private bool _isLatinGlyph;
|
---|
15 | private string _text;
|
---|
16 |
|
---|
17 | #endregion
|
---|
18 |
|
---|
19 | #region Constructors and Destructor
|
---|
20 |
|
---|
21 | public WpfTextRun()
|
---|
22 | {
|
---|
23 | _vertOrientation = -1;
|
---|
24 | _horzOrientation = -1;
|
---|
25 | _text = String.Empty;
|
---|
26 | _isLatinGlyph = true;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public WpfTextRun(string text, bool isLatin, int vertOrientation,
|
---|
30 | int horzOrientation)
|
---|
31 | {
|
---|
32 | _text = text;
|
---|
33 | _isLatinGlyph = isLatin;
|
---|
34 | _vertOrientation = vertOrientation;
|
---|
35 | _horzOrientation = horzOrientation;
|
---|
36 | }
|
---|
37 |
|
---|
38 | #endregion
|
---|
39 |
|
---|
40 | #region Public Properties
|
---|
41 |
|
---|
42 | public bool IsEmpty
|
---|
43 | {
|
---|
44 | get
|
---|
45 | {
|
---|
46 | return String.IsNullOrEmpty(_text);
|
---|
47 | }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public bool IsLatin
|
---|
51 | {
|
---|
52 | get
|
---|
53 | {
|
---|
54 | return _isLatinGlyph;
|
---|
55 | }
|
---|
56 | set
|
---|
57 | {
|
---|
58 | _isLatinGlyph = value;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public int VerticalOrientation
|
---|
63 | {
|
---|
64 | get
|
---|
65 | {
|
---|
66 | return _vertOrientation;
|
---|
67 | }
|
---|
68 | set
|
---|
69 | {
|
---|
70 | _vertOrientation = value;
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | public int HorizontalOrientation
|
---|
75 | {
|
---|
76 | get
|
---|
77 | {
|
---|
78 | return _horzOrientation;
|
---|
79 | }
|
---|
80 | set
|
---|
81 | {
|
---|
82 | _horzOrientation = value;
|
---|
83 | }
|
---|
84 | }
|
---|
85 |
|
---|
86 | public string Text
|
---|
87 | {
|
---|
88 | get
|
---|
89 | {
|
---|
90 | return _text;
|
---|
91 | }
|
---|
92 | set
|
---|
93 | {
|
---|
94 | _text = value;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | #endregion
|
---|
99 |
|
---|
100 | #region Public Methods
|
---|
101 |
|
---|
102 | public static bool IsLatinGlyph(char ch)
|
---|
103 | {
|
---|
104 | if ((int)ch < 256)
|
---|
105 | {
|
---|
106 | return true;
|
---|
107 | }
|
---|
108 |
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static IList<WpfTextRun> BreakWords(string text)
|
---|
113 | {
|
---|
114 | return BreakWords(text, -1, -1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | public static IList<WpfTextRun> BreakWords(string text, int vertOrientation,
|
---|
118 | int horzOrientation)
|
---|
119 | {
|
---|
120 | if (String.IsNullOrEmpty(text))
|
---|
121 | {
|
---|
122 | return null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | List<WpfTextRun> textRunList = new List<WpfTextRun>();
|
---|
126 |
|
---|
127 | StringBuilder builder = new StringBuilder();
|
---|
128 |
|
---|
129 | int textLength = text.Length;
|
---|
130 | bool isLatinStart = IsLatinGlyph(text[0]);
|
---|
131 | for (int i = 0; i < textLength; i++)
|
---|
132 | {
|
---|
133 | char nextChar = text[i];
|
---|
134 | if (IsLatinGlyph(nextChar) == isLatinStart)
|
---|
135 | {
|
---|
136 | builder.Append(nextChar);
|
---|
137 | }
|
---|
138 | else
|
---|
139 | {
|
---|
140 | textRunList.Add(new WpfTextRun(builder.ToString(), isLatinStart,
|
---|
141 | vertOrientation, horzOrientation));
|
---|
142 |
|
---|
143 | builder.Length = 0;
|
---|
144 | isLatinStart = IsLatinGlyph(nextChar);
|
---|
145 |
|
---|
146 | builder.Append(nextChar);
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | if (builder.Length != 0)
|
---|
151 | {
|
---|
152 | textRunList.Add(new WpfTextRun(builder.ToString(), isLatinStart,
|
---|
153 | vertOrientation, horzOrientation));
|
---|
154 | }
|
---|
155 |
|
---|
156 | return textRunList;
|
---|
157 | }
|
---|
158 |
|
---|
159 | #endregion
|
---|
160 | }
|
---|
161 | }
|
---|