BMFont.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2016 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using System.Collections.Generic;
  7. /// <summary>
  8. /// BMFont reader. C# implementation of http://www.angelcode.com/products/bmfont/
  9. /// </summary>
  10. [System.Serializable]
  11. public class BMFont
  12. {
  13. [HideInInspector][SerializeField] int mSize = 16; // How much to move the cursor when moving to the next line
  14. [HideInInspector][SerializeField] int mBase = 0; // Offset from the top of the line to the base of each character
  15. [HideInInspector][SerializeField] int mWidth = 0; // Original width of the texture
  16. [HideInInspector][SerializeField] int mHeight = 0; // Original height of the texture
  17. [HideInInspector][SerializeField] string mSpriteName;
  18. // List of serialized glyphs
  19. [HideInInspector][SerializeField] List<BMGlyph> mSaved = new List<BMGlyph>();
  20. // Actual glyphs that we'll be working with are stored in a dictionary, making the lookup faster
  21. Dictionary<int, BMGlyph> mDict = new Dictionary<int, BMGlyph>();
  22. /// <summary>
  23. /// Whether the font can be used.
  24. /// </summary>
  25. public bool isValid { get { return (mSaved.Count > 0); } }
  26. /// <summary>
  27. /// Size of this font (for example 32 means 32 pixels).
  28. /// </summary>
  29. public int charSize { get { return mSize; } set { mSize = value; } }
  30. /// <summary>
  31. /// Base offset applied to characters.
  32. /// </summary>
  33. public int baseOffset { get { return mBase; } set { mBase = value; } }
  34. /// <summary>
  35. /// Original width of the texture.
  36. /// </summary>
  37. public int texWidth { get { return mWidth; } set { mWidth = value; } }
  38. /// <summary>
  39. /// Original height of the texture.
  40. /// </summary>
  41. public int texHeight { get { return mHeight; } set { mHeight = value; } }
  42. /// <summary>
  43. /// Number of valid glyphs.
  44. /// </summary>
  45. public int glyphCount { get { return isValid ? mSaved.Count : 0; } }
  46. /// <summary>
  47. /// Original name of the sprite that the font is expecting to find (usually the name of the texture).
  48. /// </summary>
  49. public string spriteName { get { return mSpriteName; } set { mSpriteName = value; } }
  50. /// <summary>
  51. /// Access to BMFont's entire set of glyphs.
  52. /// </summary>
  53. public List<BMGlyph> glyphs { get { return mSaved; } }
  54. /// <summary>
  55. /// Helper function that retrieves the specified glyph, creating it if necessary.
  56. /// </summary>
  57. public BMGlyph GetGlyph (int index, bool createIfMissing)
  58. {
  59. // Get the requested glyph
  60. BMGlyph glyph = null;
  61. if (mDict.Count == 0)
  62. {
  63. // Populate the dictionary for faster access
  64. for (int i = 0, imax = mSaved.Count; i < imax; ++i)
  65. {
  66. BMGlyph bmg = mSaved[i];
  67. mDict.Add(bmg.index, bmg);
  68. }
  69. }
  70. // Saved check is here so that the function call is not needed if it's true
  71. if (!mDict.TryGetValue(index, out glyph) && createIfMissing)
  72. {
  73. glyph = new BMGlyph();
  74. glyph.index = index;
  75. mSaved.Add(glyph);
  76. mDict.Add(index, glyph);
  77. }
  78. return glyph;
  79. }
  80. /// <summary>
  81. /// Retrieve the specified glyph, if it's present.
  82. /// </summary>
  83. public BMGlyph GetGlyph (int index) { return GetGlyph(index, false); }
  84. /// <summary>
  85. /// Clear the glyphs.
  86. /// </summary>
  87. public void Clear ()
  88. {
  89. mDict.Clear();
  90. mSaved.Clear();
  91. }
  92. /// <summary>
  93. /// Trim the glyphs, ensuring that they will never go past the specified bounds.
  94. /// </summary>
  95. public void Trim (int xMin, int yMin, int xMax, int yMax)
  96. {
  97. if (isValid)
  98. {
  99. for (int i = 0, imax = mSaved.Count; i < imax; ++i)
  100. {
  101. BMGlyph glyph = mSaved[i];
  102. if (glyph != null) glyph.Trim(xMin, yMin, xMax, yMax);
  103. }
  104. }
  105. }
  106. }