Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
StaticSpriteFont.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 using System.Collections.Generic;
4 
5 using SiliconStudio.Core;
6 using SiliconStudio.Core.Mathematics;
7 using SiliconStudio.Core.Serialization.Contents;
8 using SiliconStudio.Core.Serialization.Converters;
9 
10 namespace SiliconStudio.Paradox.Graphics.Font
11 {
12  [ContentSerializer(typeof(DataContentConverterSerializer<StaticSpriteFont>))]
13  internal class StaticSpriteFont : SpriteFont
14  {
15  private readonly Dictionary<char, Glyph> characterToGlyph;
16 
17  internal Texture2D[] StaticTextures;
18 
19  public StaticSpriteFont(FontSystem fontSystem, StaticSpriteFontData spriteFontData)
20  : base(fontSystem, spriteFontData, false)
21  {
22  characterToGlyph = new Dictionary<char, Glyph>(spriteFontData.Glyphs.Length);
23 
24  // build the character map
25  foreach (var glyph in spriteFontData.Glyphs)
26  {
27  var character = (char)glyph.Character;
28  characterToGlyph[character] = glyph;
29  }
30 
31  // Prepare kernings if they are available.
32  var kernings = spriteFontData.Kernings;
33  if (kernings != null)
34  {
35  for (int i = 0; i < kernings.Length; i++)
36  {
37  int key = (kernings[i].First << 16) | kernings[i].Second;
38  KerningMap.Add(key, kernings[i].Offset);
39  }
40  }
41 
42  // Read the texture data.
43  StaticTextures = new Texture2D[spriteFontData.Bitmaps.Length];
44  for (int i = 0; i < StaticTextures.Length; i++)
45  {
46  if (spriteFontData.Bitmaps[i].Value != null)
47  StaticTextures[i] = Texture2D.New(fontSystem.GraphicsDevice, spriteFontData.Bitmaps[i].Value).DisposeBy(this);
48  }
49  Textures = StaticTextures;
50 
51  BaseOffsetY = spriteFontData.BaseOffset;
52  DefaultLineSpacing = spriteFontData.FontDefaultLineSpacing;
53  }
54 
55  public override float GetExtraSpacing(float fontSize)
56  {
57  return ExtraSpacing;
58  }
59 
60  public override float GetExtraLineSpacing(float fontSize)
61  {
62  return ExtraLineSpacing;
63  }
64 
65  public override float GetFontDefaultLineSpacing(float fontSize)
66  {
67  return DefaultLineSpacing;
68  }
69 
70  protected override float GetBaseOffsetY(float fontSize)
71  {
72  return BaseOffsetY;
73  }
74 
75  public override bool IsCharPresent(char c)
76  {
77  return characterToGlyph.ContainsKey(c);
78  }
79 
80  protected override Glyph GetGlyph(char character, ref Vector2 fontSize, bool dumb)
81  {
82  Glyph glyph = null;
83 
84  if (!characterToGlyph.ContainsKey(character))
85  Logger.Warning("Character '{0}' is not available in the static font character map", character);
86  else
87  glyph = characterToGlyph[character];
88 
89  return glyph;
90  }
91  }
92 }
Represents a two dimensional mathematical vector.
Definition: Vector2.cs:42
SharpDX.DirectWrite.Font Font
document false
The texture dimension is 2D.