forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalEmojiFiles.cs
More file actions
374 lines (330 loc) · 11.1 KB
/
Copy pathLocalEmojiFiles.cs
File metadata and controls
374 lines (330 loc) · 11.1 KB
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Waher.Events;
using Waher.Runtime.Settings;
namespace Waher.Content.Emoji.Emoji1
{
/// <summary>
/// What source files to use when displaying emoji.
/// </summary>
public enum Emoji1SourceFileType
{
/// <summary>
/// 64x64 PNG files stored in the Graphics/Emoji1/png/64x64 folder.
/// </summary>
Png64,
/// <summary>
/// 128x128 PNG files stored in the Graphics/Emoji1/png/128x128 folder.
/// </summary>
Png128,
/// <summary>
/// 512x512 PNG files stored in the Graphics/Emoji1/png/512x512 folder.
/// </summary>
Png512,
/// <summary>
/// SVG files stored in the Graphics/Emoji1/svg folder.
/// </summary>
Svg
}
/// <summary>
/// Delegate to a FileExists method.
/// </summary>
/// <param name="path">Path to check.</param>
/// <returns>If the file exists.</returns>
public delegate bool FileExistsHandler(string path);
/// <summary>
/// Delegate to a ReadAllBytes method.
/// </summary>
/// <param name="path">Path of file to load.</param>
/// <returns>Contents of file.</returns>
public delegate byte[] ReadAllBytesHandler(string path);
/// <summary>
/// Provides emojis from Emoji One (http://emojione.com/) stored as local files.
/// </summary>
public class Emoji1LocalFiles : IEmojiSource, IDisposable
{
private readonly Emoji1SourceFileType sourceFileType;
private ManualResetEvent initialized = new ManualResetEvent(false);
private readonly string zipFileName;
private readonly string programDataFolder;
private readonly string imageUrl;
private readonly int width;
private readonly int height;
/// <summary>
/// Provides emojis from Emoji One (http://emojione.com/) stored as local files.
/// </summary>
/// <param name="SourceFileType">Type of files to use.</param>
/// <param name="Width">Desired width of emojis.</param>
/// <param name="Height">Desired height of emojis.</param>
/// <param name="ZipFileName">Full path of the emoji1 zip file. It will be deleted after unpacking to <paramref name="ProgramDataFolder"/>.</param>
/// <param name="ProgramDataFolder">Folder to unzip emojis to.</param>
public Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height,
string ZipFileName, string ProgramDataFolder)
: this(SourceFileType, Width, Height, string.Empty, ZipFileName, ProgramDataFolder)
{
}
/// <summary>
/// Provides emojis from Emoji One (http://emojione.com/) stored as local files.
/// </summary>
/// <param name="SourceFileType">Type of files to use.</param>
/// <param name="Width">Desired width of emojis.</param>
/// <param name="Height">Desired height of emojis.</param>
/// <param name="ImageURL">URL for remote clients to fetch the image. If not provided, images are embedded into generated pages.
/// Include the string %FILENAME% where the name of the emoji image file is to be inserted.</param>
/// <param name="ZipFileName">Full path of the emoji1 zip file. It will be deleted after unpacking to <paramref name="ProgramDataFolder"/>.</param>
/// <param name="ProgramDataFolder">Folder to unzip emojis to.</param>
public Emoji1LocalFiles(Emoji1SourceFileType SourceFileType, int Width, int Height, string ImageURL,
string ZipFileName, string ProgramDataFolder)
{
this.sourceFileType = SourceFileType;
this.width = Width;
this.height = Height;
this.imageUrl = ImageURL;
this.zipFileName = ZipFileName;
this.programDataFolder = ProgramDataFolder;
try
{
DateTime TP = File.GetLastWriteTime(this.zipFileName);
if (File.Exists(this.zipFileName) && RuntimeSettings.Get(this.zipFileName, DateTime.MinValue) != TP)
{
if (!Directory.Exists(ProgramDataFolder))
Directory.CreateDirectory(ProgramDataFolder);
else
{
string Folder = Path.Combine(ProgramDataFolder, "Emoji1");
if (Directory.Exists(Folder))
Directory.Delete(Folder, true);
}
this.initialized = new ManualResetEvent(false);
Task.Run(this.Unpack);
}
else
this.initialized = new ManualResetEvent(true);
}
catch (Exception ex)
{
Log.Critical(ex);
}
}
private async Task Unpack()
{
try
{
Log.Informational("Starting unpacking file.",
new KeyValuePair<string, object>("FileName", this.zipFileName),
new KeyValuePair<string, object>("Destination", this.programDataFolder));
ZipFile.ExtractToDirectory(this.zipFileName, this.programDataFolder);
DateTime TP = File.GetLastWriteTime(this.zipFileName);
await RuntimeSettings.SetAsync(this.zipFileName, TP);
try
{
File.Delete(this.zipFileName);
Log.Informational("File unpacked and deleted.", new KeyValuePair<string, object>("FileName", this.zipFileName));
}
catch (Exception)
{
Log.Informational("File unpacked.", new KeyValuePair<string, object>("FileName", this.zipFileName));
}
}
catch (Exception ex)
{
Log.Critical(ex);
}
finally
{
this.initialized.Set();
}
}
/// <summary>
/// Waits until initialization is completed.
/// </summary>
/// <param name="TimeoutMilliseconds">Timeout, in milliseconds.</param>
/// <returns>If initialization completed successfully.</returns>
public bool WaitUntilInitialized(int TimeoutMilliseconds)
{
return this.initialized.WaitOne(TimeoutMilliseconds);
}
/// <summary>
/// Type of files to use.
/// </summary>
public Emoji1SourceFileType SourceFileType
{
get { return this.sourceFileType; }
}
/// <summary>
/// Desired width of emojis.
/// </summary>
public int Width
{
get { return this.width; }
}
/// <summary>
/// Desired height of emojis.
/// </summary>
public int Height
{
get { return this.height; }
}
/// <summary>
/// If the emoji is supported by the emoji source.
/// </summary>
/// <param name="Emoji">Emoji</param>
/// <returns>If emoji is supported.</returns>
public bool EmojiSupported(EmojiInfo Emoji)
{
string FileName = this.GetFileName(Emoji);
return File.Exists(FileName);
}
/// <summary>
/// Gets the local file name for a given emoji.
/// </summary>
/// <param name="Emoji">Emoji</param>
/// <returns>Local file name.</returns>
public string GetFileName(EmojiInfo Emoji)
{
switch (this.sourceFileType)
{
case Emoji1SourceFileType.Png64: return Path.Combine(this.programDataFolder, "Emoji1", "png", "64x64", Emoji.FileName);
case Emoji1SourceFileType.Png128: return Path.Combine(this.programDataFolder, "Emoji1", "png", "128x128", Emoji.FileName);
case Emoji1SourceFileType.Png512: return Path.Combine(this.programDataFolder, "Emoji1", "png", "512x512", Emoji.FileName);
case Emoji1SourceFileType.Svg:
default:
string s = Emoji.FileName;
if (s.EndsWith(".png"))
s = s.Substring(0, s.Length - 3) + "svg";
return Path.Combine(this.programDataFolder, "Emoji1", "svg", s);
}
}
/// <summary>
/// Generates HTML for a given Emoji.
/// </summary>
/// <param name="Output">Output</param>
/// <param name="Emoji">Emoji</param>
/// <param name="EmbedImage">If image should be embedded into the generated HTML, using the data URI scheme.</param>
public void GenerateHTML(StringBuilder Output, EmojiInfo Emoji, bool EmbedImage)
{
}
/// <summary>
/// Generates HTML for a given Emoji.
/// </summary>
/// <param name="Output">Output</param>
/// <param name="Emoji">Emoji</param>
/// <param name="Level">Level (number of colons used to define the emoji)</param>
/// <param name="EmbedImage">If image should be embedded into the generated HTML, using the data URI scheme.</param>
public void GenerateHTML(StringBuilder Output, EmojiInfo Emoji, int Level, bool EmbedImage)
{
Output.Append("<img alt=\":");
Output.Append(Encode(Emoji.ShortName));
Output.Append(":\" title=\"");
Output.Append(Encode(Emoji.Description));
Output.Append("\" width=\"");
Output.Append(this.CalcSize(this.width, Level).ToString());
Output.Append("\" height=\"");
Output.Append(this.CalcSize(this.height, Level).ToString());
Output.Append("\" src=\"");
Output.Append(Encode(this.GetUrl(Emoji, EmbedImage)));
Output.Append("\"/>");
}
/// <summary>
/// Calculates the size of an emoji.
/// </summary>
/// <param name="OrgSize">Original size.</param>
/// <param name="Level">Level</param>
/// <returns>Resulting size.</returns>
public int CalcSize(int OrgSize, int Level)
{
while (Level > 1)
{
OrgSize = (OrgSize * 4) / 3;
if (--Level == 1)
return OrgSize;
OrgSize = (OrgSize * 3) / 2;
Level--;
}
return OrgSize;
}
private static string Encode(string s)
{
if (s is null || s.IndexOfAny(specialCharacters) < 0)
return s;
return s.
Replace("&", "&").
Replace("<", "<").
Replace(">", ">").
Replace("\"", """).
Replace("'", "'");
}
private static readonly char[] specialCharacters = new char[] { '<', '>', '&', '"', '\'' };
/// <summary>
/// Gets an URL for the emoji.
/// </summary>
/// <param name="Emoji">Emoji</param>
/// <param name="Embed">If emoji should be embedded.</param>
/// <returns>URL</returns>
public string GetUrl(EmojiInfo Emoji, bool Embed)
{
if (Embed || string.IsNullOrEmpty(this.imageUrl))
{
StringBuilder Output = new StringBuilder();
Output.Append("data:image/");
if (this.sourceFileType == Emoji1SourceFileType.Svg)
Output.Append("svg+xml");
else
Output.Append("png");
Output.Append(";base64,");
string FileName = this.GetFileName(Emoji);
byte[] Data = File.ReadAllBytes(FileName);
Output.Append(Convert.ToBase64String(Data));
return Output.ToString();
}
else
{
string s = Emoji.FileName;
if (this.sourceFileType == Emoji1SourceFileType.Svg && s.EndsWith(".png"))
s = s.Substring(0, s.Length - 3) + "svg";
return this.imageUrl.Replace("%FILENAME%", s);
}
}
/// <summary>
/// Gets the image source of an emoji.
/// </summary>
/// <param name="Emoji">Emoji</param>
/// <param name="Url">URL to emoji.</param>
/// <param name="Width">Width of emoji.</param>
/// <param name="Height">Height of emoji.</param>
public void GetImageSource(EmojiInfo Emoji, out string Url, out int Width, out int Height)
{
this.GetImageSource(Emoji, 1, out Url, out Width, out Height);
}
/// <summary>
/// Gets the image source of an emoji.
/// </summary>
/// <param name="Emoji">Emoji</param>
/// <param name="Level">Level (number of colons used to define the emoji)</param>
/// <param name="Url">URL to emoji.</param>
/// <param name="Width">Width of emoji.</param>
/// <param name="Height">Height of emoji.</param>
public void GetImageSource(EmojiInfo Emoji, int Level, out string Url, out int Width, out int Height)
{
Url = this.GetUrl(Emoji, false);
Width = this.CalcSize(this.width, Level);
Height = this.CalcSize(this.height, Level);
}
/// <summary>
/// <see cref="IDisposable"/>
/// </summary>
public void Dispose()
{
if (!(this.initialized is null))
{
this.initialized.Dispose();
this.initialized = null;
}
}
}
}