/
GUIFontManager.cs
636 lines (577 loc) · 20.7 KB
/
GUIFontManager.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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
#region Copyright (C) 2005-2011 Team MediaPortal
// Copyright (C) 2005-2011 Team MediaPortal
// http://www.team-mediaportal.com
//
// MediaPortal is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// MediaPortal is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with MediaPortal. If not, see <http://www.gnu.org/licenses/>.
#endregion
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
using DShowNET.Helper;
using MediaPortal.Util;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Filter = Microsoft.DirectX.Direct3D.Filter;
using Font = Microsoft.DirectX.Direct3D.Font;
using Matrix = Microsoft.DirectX.Matrix;
using MediaPortal.ExtensionMethods;
namespace MediaPortal.GUI.Library
{
/// <summary>
/// The class responsible for keeping track of the used fonts.
/// </summary>
public class GUIFontManager
{
#region Constructors
// singleton. Dont allow any instance of this class
private GUIFontManager() {}
static GUIFontManager() {}
#endregion
#region Private structs
private struct FontManagerDrawText
{
public Font fnt;
public float xpos;
public float ypos;
public uint color;
public string text;
public float[,] matrix;
public Viewport viewport;
public int fontHeight;
} ;
#endregion
private static object _renderlock = new object();
protected static List<GUIFont> _listFonts = new List<GUIFont>();
protected static Dictionary<string, string> _dictFontAlias = new Dictionary<string, string>();
private static Sprite _d3dxSprite;
private static bool _d3dxSpriteUsed;
private static int _maxCachedTextures = 250;
private static List<FontManagerDrawText> _listDrawText = new List<FontManagerDrawText>();
private static List<FontTexture> _listFontTextures = new List<FontTexture>();
private static List<FontObject> _listFontObjects = new List<FontObject>();
public static int Count
{
get { return _listFonts.Count; }
}
public static object Renderlock
{
get { return _renderlock; }
}
public static void ReleaseUnmanagedResources()
{
unsafe
{
DXNative.FontEngineSetDevice(null);
}
}
/// <summary>
/// Loads the fonts from a file.
/// </summary>
/// <param name="strFilename">The filename from where the fonts are loaded.</param>
/// <returns>true if loaded else false</returns>
public static bool LoadFonts(string strFilename)
{
// Clear current set of fonts
Dispose();
lock (Renderlock)
{
int counter = 0;
Log.Info(" Load fonts from {0}", strFilename);
_listFonts.DisposeAndClear();
// Load the debug font
GUIFont fontDebug = new GUIFont("debug", "Arial", 12);
fontDebug.ID = counter++;
fontDebug.Load();
_listFonts.Add(fontDebug);
try
{
// Load the XML document
XmlDocument doc = new XmlDocument();
doc.Load(strFilename);
// Check the root element
if (doc.DocumentElement == null)
{
return false;
}
string strRoot = doc.DocumentElement.Name;
if (strRoot != "fonts")
{
return false;
}
// Select the list of fonts
XmlNodeList list = doc.DocumentElement.SelectNodes("/fonts/font");
foreach (XmlNode node in list)
{
XmlNode nodeStart = node.SelectSingleNodeFast("startchar");
XmlNode nodeEnd = node.SelectSingleNodeFast("endchar");
XmlNode nodeName = node.SelectSingleNodeFast("name");
XmlNode nodeFileName = node.SelectSingleNodeFast("filename");
XmlNode nodeHeight = node.SelectSingleNodeFast("height");
XmlNode nodeBold = node.SelectSingleNodeFast("bold");
XmlNode nodeItalics = node.SelectSingleNodeFast("italic");
if (nodeHeight != null && nodeName != null && nodeFileName != null)
{
bool bold = false;
bool italic = false;
if (nodeBold != null && nodeBold.InnerText != null && nodeBold.InnerText.Equals("yes"))
{
bold = true;
}
if (nodeItalics != null && nodeItalics.InnerText != null && nodeItalics.InnerText.Equals("yes"))
{
italic = true;
}
string strName = nodeName.InnerText;
string strFileName = nodeFileName.InnerText;
int iHeight = Int32.Parse(nodeHeight.InnerText);
// height is based on 720x576
float fPercent = (((float)GUIGraphicsContext.Height) * GUIGraphicsContext.ZoomVertical) / 576.0f;
fPercent *= iHeight;
iHeight = (int)fPercent;
FontStyle style = new FontStyle();
style = FontStyle.Regular;
if (bold)
{
style |= FontStyle.Bold;
}
if (italic)
{
style |= FontStyle.Italic;
}
GUIFont font = new GUIFont(strName, strFileName, iHeight, style);
font.ID = counter++;
// .NET's LocalisationProvider should give the correct amount of chars.
if (nodeStart != null && nodeStart.InnerText != "" && nodeEnd != null && nodeEnd.InnerText != "")
{
int start = Int32.Parse(nodeStart.InnerText);
int end = Int32.Parse(nodeEnd.InnerText);
font.SetRange(start, end);
}
else
{
font.SetRange(0, GUIGraphicsContext.CharsInCharacterSet);
}
font.Load();
_listFonts.Add(font);
}
}
// Select the list of aliases
XmlNodeList listAlias = doc.DocumentElement.SelectNodes("/fonts/alias");
foreach (XmlNode node in listAlias)
{
XmlNode nodeName = node.SelectSingleNodeFast("name");
XmlNode nodeFontName = node.SelectSingleNodeFast("fontname");
_dictFontAlias.Add(nodeName.InnerText, nodeFontName.InnerText);
}
return true;
}
catch (Exception ex)
{
Log.Info("GUIFontManager: Exception loading fonts {0} err:{1} stack:{2}", strFilename, ex.Message,
ex.StackTrace);
}
return false;
}
}
/// <summary>
/// Gets a GUIFont.
/// </summary>
/// <param name="iFont">The font number</param>
/// <returns>A GUIFont instance representing the fontnumber or a default GUIFont if the number does not exists.</returns>
public static GUIFont GetFont(int iFont)
{
lock (Renderlock)
{
if (iFont >= 0 && iFont < _listFonts.Count)
{
return _listFonts[iFont];
}
return GetFont("debug");
}
}
/// <summary>
/// Gets a GUIFont.
/// </summary>
/// <param name="strFontName">The name of the font</param>
/// <returns>A GUIFont instance representing the strFontName or a default GUIFont if the strFontName does not exists.</returns>
public static GUIFont GetFont(string strFontName)
{
lock (Renderlock)
{
if (strFontName != null)
{
// Try to interpret the font name as an alias before searching for the font.
string fn = null;
if (!_dictFontAlias.TryGetValue(strFontName, out fn))
{
fn = strFontName;
}
for (int i = 0; i < _listFonts.Count; ++i)
{
GUIFont font = _listFonts[i];
if (font.FontName == fn)
{
return font;
}
}
}
// just return a font
return GetFont("debug");
}
}
public static void MeasureText(Font fnt, string text, ref float textwidth, ref float textheight, int fontSize)
{
if (text[0] == ' ') // anti-trim
{
text = "_" + text.Substring(1);
}
if (text[text.Length - 1] == ' ')
{
text = text.Substring(0, text.Length - 1) + '_';
}
// Text drawing doesnt work with DX9Ex & sprite
if (GUIGraphicsContext.IsDirectX9ExUsed())
{
MeasureText(text, ref textwidth, ref textheight, fontSize);
}
else
{
if (_d3dxSprite == null)
{
_d3dxSprite = new Sprite(GUIGraphicsContext.DX9Device);
}
Rectangle rect = fnt.MeasureString(_d3dxSprite, text, DrawTextFormat.NoClip, Color.Black);
textwidth = rect.Width;
textheight = rect.Height;
}
return;
}
/// <summary>
/// Uses GDI+ Graphics to render a given text and measure the it's size
/// </summary>
/// <param name="text"></param>
/// <param name="textwidth"></param>
/// <param name="textheight"></param>
/// <param name="fontSize"></param>
public static void MeasureText(string text, ref float textwidth, ref float textheight, int fontSize)
{
try
{
using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bmp))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.TextContrast = 0;
Size size = g.MeasureString(text, CachedSystemFont(fontSize)).ToSize();
textwidth = size.Width;
textheight = size.Height;
}
}
}
catch (Exception ex)
{
Log.Error("GUIFontManager: Error in MeasureText - {0}", ex.Message);
}
}
private static System.Drawing.Font CachedSystemFont(int fontSize)
{
bool fontCached = false;
int cacheSlot = 0;
foreach (FontObject cachedFont in _listFontObjects)
{
if (cachedFont.size == fontSize)
{
fontCached = true;
break;
}
cacheSlot++;
}
if (!fontCached)
{
System.Drawing.Font systemFont = new System.Drawing.Font("Arial", fontSize);
FontObject newFont = new FontObject();
newFont.size = fontSize;
newFont.font = systemFont;
_listFontObjects.Add(newFont);
}
return _listFontObjects[cacheSlot].font;
}
public static void DrawText(Font fnt, float xpos, float ypos, Color color, string text, int maxWidth, int fontHeight)
{
FontManagerDrawText draw;
draw.fontHeight = fontHeight;
draw.fnt = fnt;
draw.xpos = xpos;
draw.ypos = ypos;
draw.color = (uint)color.ToArgb();
draw.text = text;
draw.matrix = (float[,])GUIGraphicsContext.GetFinalMatrix().Clone();
draw.viewport = GUIGraphicsContext.DX9Device.Viewport;
if (maxWidth >= 0)
{
draw.viewport.Width = ((int)GUIGraphicsContext.GetFinalTransform().TransformXCoord(xpos + maxWidth, 0, 0)) -
draw.viewport.X;
}
_listDrawText.Add(draw);
_d3dxSpriteUsed = true;
}
private static void DrawTextUsingTexture(FontManagerDrawText draw, int fontSize)
{
bool textureCached = false;
int cacheSlot = 0;
FontTexture drawingTexture = new FontTexture();
foreach (FontTexture cachedTexture in _listFontTextures)
{
if (cachedTexture.text == draw.text && cachedTexture.size == fontSize)
{
textureCached = true;
drawingTexture = cachedTexture;
break;
}
cacheSlot++;
}
Size size = new Size(0, 0);
if (textureCached)
{
//keep commonly used textures at the top of the pile
_listFontTextures.RemoveAt(cacheSlot);
_listFontTextures.Add(drawingTexture);
}
else // texture needs to be cached
{
Texture texture = null;
float textwidth = 0, textheight = 0;
MeasureText(draw.text, ref textwidth, ref textheight, fontSize);
size.Width = (int)textwidth;
size.Height = (int)textheight;
try
{
// The MemoryStream must be kept open for the lifetime of the bitmap
using (MemoryStream imageStream = new MemoryStream())
{
using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.SmoothingMode = SmoothingMode.AntiAlias;
g.TextRenderingHint = TextRenderingHint.AntiAlias;
g.TextContrast = 0;
g.DrawString(draw.text, CachedSystemFont(fontSize), Brushes.White, new Point((int)0, (int)0),
StringFormat.GenericTypographic);
bitmap.Save(imageStream, ImageFormat.Bmp);
imageStream.Position = 0;
Format format = Format.Dxt3;
if (GUIGraphicsContext.GetTexturePoolType() == Pool.Default)
{
format = Format.Unknown;
}
ImageInformation info = new ImageInformation();
try
{
texture = TextureLoader.FromStream(GUIGraphicsContext.DX9Device,
imageStream, (int)imageStream.Length,
0, 0,
1,
0,
format,
GUIGraphicsContext.GetTexturePoolType(),
Filter.None,
Filter.None,
0,
ref info);
}
catch (OutOfVideoMemoryException oovme)
{
Log.Error("GUIFontManager: OutOfVideoMemory in DrawTextUsingTexture - {0}", oovme.Message);
}
catch (OutOfMemoryException oome)
{
Log.Error("GUIFontManager: OutOfMemory in DrawTextUsingTexture - {0}", oome.Message);
}
}
}
}
}
catch (Exception ex)
{
Log.Error("GUIFontManager: Error in DrawTextUsingTexture - {0}", ex.Message);
}
MeasureText(draw.text, ref textwidth, ref textheight, fontSize);
size.Width = (int)textwidth;
size.Height = (int)textheight;
FontTexture newTexture = new FontTexture();
newTexture.text = draw.text;
newTexture.texture = texture;
newTexture.size = fontSize;
if (_listFontTextures.Count >= _maxCachedTextures)
{
//need to clear this and not rely on the finalizer
FontTexture disposableFont = _listFontTextures[0];
_listFontTextures.RemoveAt(0);
disposableFont.Dispose();
}
_listFontTextures.Add(newTexture);
drawingTexture = newTexture;
}
_d3dxSprite.Draw(drawingTexture.texture, new Rectangle(0, 0, size.Width, size.Height),
Vector3.Empty,
new Vector3((int)draw.xpos, (int)draw.ypos, 0), (int)draw.color);
}
public static void Present()
{
lock (Renderlock)
{
DXNative.FontEnginePresentTextures();
for (int i = 0; i < _listFonts.Count; ++i)
{
GUIFont font = _listFonts[i];
font.Present();
}
if (_d3dxSpriteUsed)
{
if (_d3dxSprite == null)
{
_d3dxSprite = new Sprite(GUIGraphicsContext.DX9Device);
}
_d3dxSprite.Begin(SpriteFlags.AlphaBlend | SpriteFlags.SortTexture);
Viewport orgView = GUIGraphicsContext.DX9Device.Viewport;
Matrix orgProj = GUIGraphicsContext.DX9Device.Transform.View;
Matrix projm = orgProj;
Matrix finalm;
foreach (FontManagerDrawText draw in _listDrawText)
{
finalm.M11 = draw.matrix[0, 0];
finalm.M21 = draw.matrix[0, 1];
finalm.M31 = draw.matrix[0, 2];
finalm.M41 = draw.matrix[0, 3];
finalm.M12 = draw.matrix[1, 0];
finalm.M22 = draw.matrix[1, 1];
finalm.M32 = draw.matrix[1, 2];
finalm.M42 = draw.matrix[1, 3];
finalm.M13 = draw.matrix[2, 0];
finalm.M23 = draw.matrix[2, 1];
finalm.M33 = draw.matrix[2, 2];
finalm.M43 = draw.matrix[2, 3];
finalm.M14 = 0;
finalm.M24 = 0;
finalm.M34 = 0;
finalm.M44 = 1.0f;
_d3dxSprite.Transform = finalm;
GUIGraphicsContext.DX9Device.Viewport = draw.viewport;
float wfactor = ((float)orgView.Width) / (float)draw.viewport.Width;
float hfactor = ((float)orgView.Height) / (float)draw.viewport.Height;
float xoffset = (float)(orgView.X - draw.viewport.X);
float yoffset = (float)(orgView.Y - draw.viewport.Y);
projm.M11 = (orgProj.M11 + orgProj.M14 * xoffset) * wfactor;
projm.M21 = (orgProj.M21 + orgProj.M24 * xoffset) * wfactor;
projm.M31 = (orgProj.M31 + orgProj.M34 * xoffset) * wfactor;
projm.M41 = (orgProj.M41 + orgProj.M44 * xoffset) * wfactor;
projm.M12 = (orgProj.M12 + orgProj.M14 * yoffset) * hfactor;
projm.M22 = (orgProj.M22 + orgProj.M24 * yoffset) * hfactor;
projm.M32 = (orgProj.M32 + orgProj.M34 * yoffset) * hfactor;
projm.M42 = (orgProj.M42 + orgProj.M44 * yoffset) * hfactor;
GUIGraphicsContext.DX9Device.Transform.View = projm;
if (GUIGraphicsContext.IsDirectX9ExUsed())
{
DrawTextUsingTexture(draw, draw.fontHeight);
}
else
{
draw.fnt.DrawText(_d3dxSprite, draw.text, new Rectangle((int)draw.xpos,
(int)draw.ypos, 0, 0), DrawTextFormat.NoClip,
(int)draw.color);
}
_d3dxSprite.Flush();
}
GUIGraphicsContext.DX9Device.Viewport = orgView;
GUIGraphicsContext.DX9Device.Transform.View = orgProj;
_d3dxSprite.End();
_listDrawText = new List<FontManagerDrawText>();
_d3dxSpriteUsed = false;
}
}
}
/// <summary>
/// Disposes all GUIFonts.
/// </summary>
public static void Dispose()
{
lock (Renderlock)
{
Log.Info(" fonts.SafeDispose()");
//_listFonts.DisposeAndClear();
foreach (GUIFont font in _listFonts)
{
font.Dispose(null, null);
}
if (_d3dxSprite != null)
{
_d3dxSprite.SafeDispose();
_d3dxSprite = null;
_d3dxSpriteUsed = false;
}
_listFontTextures.DisposeAndClear();
_listFontObjects.DisposeAndClear();
}
}
public static void ClearFontCache()
{
try
{
string fontCache = String.Format(@"{0}\fonts", GUIGraphicsContext.SkinCacheFolder);
MediaPortal.Util.Utils.DirectoryDelete(fontCache, true);
}
catch (Exception) {}
}
/// <summary>
/// Sets the device and the FVF.
/// </summary>
public static void SetDevice()
{
Log.Info(" fonts.SetDevice()");
IntPtr upDevice = DirectShowUtil.GetUnmanagedDevice(GUIGraphicsContext.DX9Device);
unsafe
{
DXNative.FontEngineSetDevice(upDevice.ToPointer());
}
}
/// <summary>
/// Initializes the device objects of the GUIFonts.
/// </summary>
public static void InitializeDeviceObjects()
{
lock (Renderlock)
{
Log.Debug(" fonts.InitializeDeviceObjects()");
IntPtr upDevice = DirectShowUtil.GetUnmanagedDevice(GUIGraphicsContext.DX9Device);
unsafe
{
DXNative.FontEngineSetDevice(upDevice.ToPointer());
}
foreach (GUIFont font in _listFonts)
{
font.InitializeDeviceObjects();
}
}
}
}
}