Skip to content
Browse files

Improved text output on Windows XP by adjusting MIP levels to 1 (usin…

…g manually created FontDescription in DirectX.Direct3D.Font constructor instead of direct System.Drawing.Font)

Improved and fixed caching of font/text textures on Vista/Win7
Measuring and text position is good enough/same as before (on master), but it could/should be improved
  • Loading branch information...
1 parent cc6fa29 commit 4a8ca7c22e83da14e04a444b2834edba86e14a5e @SilentException SilentException committed Nov 29, 2011
View
3 mediaportal/Core/guilib/FontTexture.cs
@@ -20,6 +20,7 @@
using System;
using System.Collections.Generic;
+using System.Drawing;
using System.Linq;
using System.Text;
using Microsoft.DirectX.Direct3D;
@@ -35,7 +36,9 @@ internal class FontTexture : IDisposable
#region Properties
+ public string name { get; set; }
public int size { get; set; }
+ public FontStyle style { get; set; }
public string text { get; set; }
public Texture texture { get; set; }
View
2 mediaportal/Core/guilib/GUIFont.cs
@@ -1260,7 +1260,7 @@ public void InitializeDeviceObjects()
_textureFont.Disposing -= new EventHandler(_textureFont_Disposing);
_textureFont.Disposing += new EventHandler(_textureFont_Disposing);
SetFontEgine();
- _d3dxFont = new Microsoft.DirectX.Direct3D.Font(GUIGraphicsContext.DX9Device, _systemFont);
+ _d3dxFont = new Microsoft.DirectX.Direct3D.Font(GUIGraphicsContext.DX9Device, GUIFontManager.GetFontDescription(_systemFont));
}
finally
{
View
49 mediaportal/Core/guilib/GUIFontManager.cs
@@ -49,6 +49,16 @@ public class GUIFontManager
[DllImport("fontEngine.dll", ExactSpelling = true, CharSet = CharSet.Auto, SetLastError = true)]
private static extern unsafe void FontEngineSetDevice(void* device);
+ [DllImport("user32.dll")]
+ static extern IntPtr GetDC(IntPtr hWnd);
+
+ [DllImport("User32.dll")]
+ public static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
+
+ [DllImport("gdi32.dll")]
+ static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
+
+
#region Constructors
// singleton. Dont allow any instance of this class
@@ -273,6 +283,27 @@ public static GUIFont GetFont(string strFontName)
}
}
+ public static FontDescription GetFontDescription(System.Drawing.Font fnt)
+ {
+ FontDescription desc = new FontDescription();
+
+ IntPtr dC = GetDC(IntPtr.Zero);
+ int deviceCaps = GetDeviceCaps(dC, 90); //LOGPIXELSY
+ ReleaseDC(IntPtr.Zero, dC);
+
+ desc.FaceName = fnt.Name;
+ desc.OutputPrecision = Precision.TtOnly;
+ desc.MipLevels = 1;
+ desc.Height = (int)((double)((-1 * fnt.Size * (float)deviceCaps) / 72));
+ desc.Width = 0;
+ desc.CharSet = CharacterSet.Default;
+ desc.Quality = FontQuality.ClearType;
+ desc.PitchAndFamily = PitchAndFamily.FamilyDoNotCare;
+ desc.Weight = fnt.Bold ? FontWeight.Bold : FontWeight.Normal;
+ desc.IsItalic = fnt.Italic;
+ return desc;
+ }
+
public static void MeasureText(Font fnt, string text, ref float textwidth, ref float textheight, string fontName, int fontSize, FontStyle fontStyle)
{
if (text[0] == ' ') // anti-trim
@@ -394,22 +425,22 @@ private static void DrawTextUsingTexture(FontManagerDrawText draw)
string fontName = draw.fontName;
FontStyle fontStyle = draw.fontStyle;
- bool textureCached = false;
- int cacheSlot = 0;
- FontTexture drawingTexture = new FontTexture();
- foreach (FontTexture cachedTexture in _listFontTextures)
+ int cacheSlot = -1;
+ FontTexture drawingTexture = null;
+ //foreach (FontTexture cachedTexture in _listFontTextures)
+ for (int i = _listFontTextures.Count - 1; i >=0; i--)
{
- if (cachedTexture.text == draw.text && cachedTexture.size == fontSize)
+ FontTexture cachedTexture = _listFontTextures[i];
+ if (cachedTexture.text == draw.text && cachedTexture.name == fontName && cachedTexture.size == fontSize && cachedTexture.style == fontStyle)
{
- textureCached = true;
drawingTexture = cachedTexture;
+ cacheSlot = i;
break;
}
- cacheSlot++;
}
Size size = new Size(0, 0);
- if (textureCached)
+ if (drawingTexture != null)
{
//keep commonly used textures at the top of the pile
_listFontTextures.RemoveAt(cacheSlot);
@@ -487,7 +518,9 @@ private static void DrawTextUsingTexture(FontManagerDrawText draw)
FontTexture newTexture = new FontTexture();
newTexture.text = draw.text;
newTexture.texture = texture;
+ newTexture.name = fontName;
newTexture.size = fontSize;
+ newTexture.style = fontStyle;
if (_listFontTextures.Count >= _maxCachedTextures)
{

0 comments on commit 4a8ca7c

Please sign in to comment.
Something went wrong with that request. Please try again.