Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
DynamicSpriteFont.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System;
5 using System.Collections.Generic;
6 
7 using SiliconStudio.Core.Mathematics;
8 using SiliconStudio.Core.Serialization.Contents;
9 using SiliconStudio.Core.Serialization.Converters;
10 
11 namespace SiliconStudio.Paradox.Graphics.Font
12 {
13  /// <summary>
14  /// A dynamic font. That is a font that generate its character bitmaps at execution.
15  /// </summary>
16  [ContentSerializer(typeof(DataContentConverterSerializer<DynamicSpriteFont>))]
17  internal class DynamicSpriteFont : SpriteFont
18  {
19  /// <summary>
20  /// Input the family name of the (TrueType) font.
21  /// </summary>
22  private readonly string fontName;
23 
24  /// <summary>
25  /// Style for the font. 'regular', 'bold' or 'italic'. Default is 'regular
26  /// </summary>
27  private readonly FontStyle style;
28 
29  /// <summary>
30  /// Specifies whether to use kerning information when rendering the font. Default value is false (NOT SUPPORTED YET).
31  /// </summary>
32  private readonly bool useKerning;
33 
34  /// <summary>
35  /// The alias mode of the font
36  /// </summary>
37  private readonly FontAntiAliasMode antiAlias;
38 
39  /// <summary>
40  /// The character specifications cached to avoid re-allocations
41  /// </summary>
42  private readonly Dictionary<CharacterKey, CharacterSpecification> sizedCharacterToCharacterData = new Dictionary<CharacterKey, CharacterSpecification>();
43 
44  internal FontManager FontManager
45  {
46  get { return FontSystem.FontManager; }
47  }
48 
49  internal FontCacheManager FontCacheManager
50  {
51  get { return FontSystem.FontCacheManager; }
52  }
53 
54  internal int FrameCount
55  {
56  get { return FontSystem.FrameCount; }
57  }
58 
59  public DynamicSpriteFont(FontSystem fontSystem, DynamicSpriteFontData fontData)
60  : base(fontSystem, fontData, true)
61  {
62  // import font properties from font data
63  style = fontData.Style;
64  fontName = fontData.FontName;
65  useKerning = fontData.UseKerning;
66  antiAlias = fontData.AntiAlias;
67 
68  // retrieve needed info from the font
69  float relativeLineSpacing;
70  float relativeBaseOffsetY;
71  float relativeMaxWidth;
72  float relativeMaxHeight;
73  FontManager.GetFontInfo(fontData.FontName, fontData.Style, out relativeLineSpacing, out relativeBaseOffsetY, out relativeMaxWidth, out relativeMaxHeight);
74 
75  // set required base properties
76  DefaultLineSpacing = relativeLineSpacing * Size;
77  BaseOffsetY = relativeBaseOffsetY * Size;
78  Textures = FontCacheManager.Textures;
79  Swizzle = SwizzleMode.RRRR;
80  }
81 
82  public override bool IsCharPresent(char c)
83  {
84  return FontManager.DoesFontContains(fontName, style, c);
85  }
86 
87  protected override Glyph GetGlyph(char character, ref Vector2 fontSize, bool uploadGpuResources)
88  {
89  // get the character data associated to the provided character and size
90  var characterData = GetOrCreateCharacterData(fontSize, character);
91 
92  // generate the bitmap if it does not exist
93  if(characterData.Bitmap == null)
94  FontManager.GenerateBitmap(characterData, false);
95 
96  // upload the character to the GPU font texture and create the glyph if does not exists
97  if (uploadGpuResources && characterData.Bitmap != null && !characterData.IsBitmapUploaded)
98  FontCacheManager.UploadCharacterBitmap(characterData);
99 
100  // update the character usage info
101  FontCacheManager.NotifyCharacterUtilization(characterData);
102 
103  return characterData.Glyph;
104  }
105 
106  internal override void PreGenerateGlyphs(ref StringProxy text, ref Vector2 size)
107  {
108  for (int i = 0; i < text.Length; i++)
109  {
110  // get the character data associated to the provided character and size
111  var characterData = GetOrCreateCharacterData(size, text[i]);
112 
113  // force asynchronous generation of the bitmap if it does not exist
114  if (characterData.Bitmap == null)
115  FontManager.GenerateBitmap(characterData, true);
116  }
117  }
118 
119  private CharacterSpecification GetOrCreateCharacterData(Vector2 size, char character)
120  {
121  // build the dictionary look up key
122  var lookUpKey = new CharacterKey(character, size);
123 
124  // get the entry (creates it if it does not exist)
125  CharacterSpecification characterData;
126  if (!sizedCharacterToCharacterData.TryGetValue(lookUpKey, out characterData))
127  {
128  characterData = new CharacterSpecification(character, fontName, size, style, antiAlias);
129  sizedCharacterToCharacterData[lookUpKey] = characterData;
130  }
131  return characterData;
132  }
133 
134  private struct CharacterKey : IEquatable<CharacterKey>
135  {
136  private readonly char character;
137 
138  private readonly Vector2 size;
139 
140  public CharacterKey(char character, Vector2 size)
141  {
142  this.character = character;
143  this.size = size;
144  }
145 
146  public bool Equals(CharacterKey other)
147  {
148  return character == other.character && size == other.size;
149  }
150 
151  public override bool Equals(object obj)
152  {
153  if (ReferenceEquals(null, obj)) return false;
154  return obj is CharacterKey && Equals((CharacterKey)obj);
155  }
156 
157  public override int GetHashCode()
158  {
159  return character.GetHashCode();
160  }
161  }
162  }
163 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
SharpDX.DirectWrite.Font Font
FontAntiAliasMode
Available antialias mode.
SiliconStudio.Paradox.Graphics.Font.FontStyle FontStyle
_In_ size_t _In_ size_t size
Definition: DirectXTexP.h:175