-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEmojiRender.cs
218 lines (203 loc) · 8.91 KB
/
EmojiRender.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ST.Library.UI.STTextBox;
using ST.Library.Drawing.SvgRender;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace ST.Library.Drawing
{
public class EmojiRender : IEmojiRender, IDisposable
{
private Dictionary<string, string> m_dic_xml = new Dictionary<string, string>();
private Dictionary<string, CacheInfo> m_dic_cache = new Dictionary<string, CacheInfo>();
private struct CacheInfo
{
public int Size;
public Image Image;
public Image SelectedImage;
}
private ImageAttributes m_img_attr_normal;
private ImageAttributes m_img_attr_selected;
public EmojiRender(string strFile) {
ColorMatrix m = new ColorMatrix(new float[][]{
new float[]{1, 0, 0, 0, 0},
new float[]{0, 1, 0, 0, 0},
new float[]{0, 0, 1, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 0, 1},
});
m_img_attr_normal = new ImageAttributes();
m_img_attr_normal.SetColorMatrix(m);
m[3, 3] = 0.5F;
m_img_attr_selected = new ImageAttributes();
m_img_attr_selected.SetColorMatrix(m);
this.InitSvgFromPackage(strFile);
}
public bool IsEmoji(string strChar) {
return this.GetEmojiString(strChar) != null;
}
private string GetEmojiString(string strText) {
if (m_dic_xml.ContainsKey(strText)) {
return strText;
}
// http://www.unicode.org/charts/PDF/UFE00.pdf
// FE00 - FE0F is the Variation Selectors
// FE0E -> text variation selector
// FE0F -> emoji variation selector
if (strText.IndexOf('\uFE0F') == -1) {
return null;
}
strText = strText.Replace("\uFE0F", "");// strText.Substring(0, strText.Length - 1);
if (m_dic_xml.ContainsKey(strText)) {
return strText;
}
return null;
}
public void DrawEmoji(ISTTextBoxRender render, string strChar, int nX, int nY, int nWidth, bool bSelected) {
strChar = this.GetEmojiString(strChar);
CacheInfo ci;
if (string.IsNullOrEmpty(strChar)) {
strChar = "none";
}
if (m_dic_cache.ContainsKey(strChar)) {
ci = m_dic_cache[strChar];
if (ci.Size != nWidth) {
ci.Image.Dispose();
ci.Image = this.GetEmojiImage(strChar, nWidth);
m_dic_cache[strChar] = ci;
}
} else {
ci.Size = nWidth;
ci.Image = this.GetEmojiImage(strChar, nWidth);
ci.SelectedImage = this.SetAlpha((Bitmap)ci.Image.Clone());
m_dic_cache.Add(strChar, ci);
}
Rectangle rect = new Rectangle(nX, nY, nWidth, nWidth);
render.DrawImage(bSelected ? ci.SelectedImage : ci.Image, rect);
//if (!bSelected) {
// render.DrawImage(ci.Image, rect);
//} else {
// using (Bitmap bmp = (Bitmap)ci.Image.Clone()) {
// var bmpData = bmp.LockBits(new Rectangle(0, 0, nWidth, nWidth), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
// byte[] byClr = new byte[bmpData.Stride * bmpData.Height];
// Marshal.Copy(bmpData.Scan0, byClr, 0, byClr.Length);
// for (int x = 0; x < bmp.Width; x++) {
// for (int y = 0; y < bmp.Height; y++) {
// int nIndex = y * bmpData.Stride + x * 4 + 3;
// byClr[nIndex] = (byte)(byClr[nIndex] >> 1);
// }
// }
// Marshal.Copy(byClr, 0, bmpData.Scan0, byClr.Length);
// bmp.UnlockBits(bmpData);
// render.DrawImage(bmp, rect);
// }
//}
}
public Image GetEmojiImage(string strChar, int nSize) {
strChar = this.GetEmojiString(strChar);
if (strChar == null) {
return null;
}
Bitmap bmp = new Bitmap(nSize, nSize);
using (Graphics g = Graphics.FromImage(bmp)) {
using (SvgDocument svg = SvgDocument.FromXml(m_dic_xml[strChar])) {
svg.Draw(g, new RectangleF(0, 0, nSize, nSize));
}
}
return bmp;
}
public Image SetAlpha(Bitmap bmp) {
var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
byte[] byClr = new byte[bmpData.Stride * bmpData.Height];
Marshal.Copy(bmpData.Scan0, byClr, 0, byClr.Length);
for (int x = 0; x < bmp.Width; x++) {
for (int y = 0; y < bmp.Height; y++) {
int nIndex = y * bmpData.Stride + x * 4 + 3;
byClr[nIndex] = (byte)(byClr[nIndex] >> 1);
}
}
Marshal.Copy(byClr, 0, bmpData.Scan0, byClr.Length);
bmp.UnlockBits(bmpData);
return bmp;
}
private int InitSvgFromPackage(string strFile) {
int nCounter = 0;
Dictionary<string, string> dic = new Dictionary<string, string>();
using (StreamReader reader = new StreamReader(strFile, Encoding.UTF8)) {
string strLine = string.Empty;
while ((strLine = reader.ReadLine()) != null) {
int nIndex = strLine.IndexOf(',');
string strXml = strLine.Substring(nIndex + 1);
dic.Add(this.GetString(strLine.Substring(0, nIndex)), strXml);
}
}
if (!dic.ContainsKey("none")) {
dic.Add("none", "<svg viewBox='0,0,20,20'><rect stroke='red' fill='yellow' x='4' y='2' width='12' height='16'/><path stroke='red' fill='none' d='M8,8 v-2 h4 v4 h-2 v2'/><line stroke='red' x1='8' y1='14' x2='12' y2='14'/></svg>");
}
m_dic_xml = dic;
return nCounter;
}
public string GetString(string strText) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strText.Length; i += 4) {
sb.Append((char)(Convert.ToInt32(strText.Substring(i, 4), 16)));
}
return sb.ToString();
}
/// <summary>
/// Package all svg files to one file
/// </summary>
/// <param name="strSvgFilesPath">The svg path</param>
/// <param name="strFileOut">The out file</param>
/// <returns>Svg count</returns>
public static int PackageSvgFiles(string strSvgFilesPath, string strFileOut) {
int nCounter = 0;
Regex reg_start = new Regex(@"^\s+|\s+$", RegexOptions.Multiline);
using (StreamWriter writer = new StreamWriter(strFileOut, false, Encoding.UTF8)) {
foreach (var v in Directory.GetFiles(strSvgFilesPath, "*.svg")) {
string strName = Path.GetFileNameWithoutExtension(v);
string strText = EmojiRender.FileNameToUnicode(strName);
string strXml = File.ReadAllText(v).Trim();
strXml = reg_start.Replace(strXml, "").Replace("\r", "").Replace("\n", "");
writer.WriteLine(strText + "," + strXml);
nCounter++;
}
}
return nCounter;
}
private static string FileNameToUnicode(string strName) {
//((strText[nIndex] & 0x03FF) << 10) + (strText[nIndex + 1] & 0x03FF) + 0x10000;
strName.ToLower();
if (strName[0] == 'u') {
strName = strName.Substring(1);
}
string strRet = string.Empty;
foreach (var v in strName.Split('-', '_')) {
int number = Convert.ToInt32(v, 16);
if (number < 0x10000) {
strRet += number.ToString("X4");
} else {
number &= 0xFFFF;
int nH = (number >> 10) | 0xD800; //D800-DBFF -> hight
int nL = (number & 0x03FF) | 0xDC00; //DC00-DEFF -> low
strRet += nH.ToString("X4");
strRet += nL.ToString("X4");
}
}
return strRet;
}
public void Dispose() {
if (m_img_attr_normal != null) {
m_img_attr_normal.Dispose();
}
if (m_img_attr_selected != null) {
m_img_attr_selected.Dispose();
}
}
}
}