BMFontReader.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //----------------------------------------------
  2. // NGUI: Next-Gen UI kit
  3. // Copyright © 2011-2016 Tasharen Entertainment
  4. //----------------------------------------------
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Text;
  8. /// <summary>
  9. /// Helper class that takes care of loading BMFont's glyph information from the specified byte array.
  10. /// This functionality is not a part of BMFont anymore because Flash export option can't handle System.IO functions.
  11. /// </summary>
  12. public static class BMFontReader
  13. {
  14. /// <summary>
  15. /// Helper function that retrieves the string value of the key=value pair.
  16. /// </summary>
  17. static string GetString (string s)
  18. {
  19. int idx = s.IndexOf('=');
  20. return (idx == -1) ? "" : s.Substring(idx + 1);
  21. }
  22. /// <summary>
  23. /// Helper function that retrieves the integer value of the key=value pair.
  24. /// </summary>
  25. static int GetInt (string s)
  26. {
  27. int val = 0;
  28. string text = GetString(s);
  29. #if UNITY_FLASH
  30. try { val = int.Parse(text); } catch (System.Exception) { }
  31. #else
  32. int.TryParse(text, out val);
  33. #endif
  34. return val;
  35. }
  36. /// <summary>
  37. /// Reload the font data.
  38. /// </summary>
  39. static public void Load (BMFont font, string name, byte[] bytes)
  40. {
  41. font.Clear();
  42. if (bytes != null)
  43. {
  44. ByteReader reader = new ByteReader(bytes);
  45. char[] separator = new char[] { ' ' };
  46. while (reader.canRead)
  47. {
  48. string line = reader.ReadLine();
  49. if (string.IsNullOrEmpty(line)) break;
  50. string[] split = line.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
  51. int len = split.Length;
  52. if (split[0] == "char")
  53. {
  54. // Expected data style:
  55. // char id=13 x=506 y=62 width=3 height=3 xoffset=-1 yoffset=50 xadvance=0 page=0 chnl=15
  56. int channel = (len > 10) ? GetInt(split[10]) : 15;
  57. if (len > 9 && GetInt(split[9]) > 0)
  58. {
  59. Debug.LogError("Your font was exported with more than one texture. Only one texture is supported by NGUI.\n" +
  60. "You need to re-export your font, enlarging the texture's dimensions until everything fits into just one texture.");
  61. break;
  62. }
  63. if (len > 8)
  64. {
  65. int id = GetInt(split[1]);
  66. BMGlyph glyph = font.GetGlyph(id, true);
  67. if (glyph != null)
  68. {
  69. glyph.x = GetInt(split[2]);
  70. glyph.y = GetInt(split[3]);
  71. glyph.width = GetInt(split[4]);
  72. glyph.height = GetInt(split[5]);
  73. glyph.offsetX = GetInt(split[6]);
  74. glyph.offsetY = GetInt(split[7]);
  75. glyph.advance = GetInt(split[8]);
  76. glyph.channel = channel;
  77. }
  78. else Debug.Log("Char: " + split[1] + " (" + id + ") is NULL");
  79. }
  80. else
  81. {
  82. Debug.LogError("Unexpected number of entries for the 'char' field (" + name + ", " + split.Length + "):\n" + line);
  83. break;
  84. }
  85. }
  86. else if (split[0] == "kerning")
  87. {
  88. // Expected data style:
  89. // kerning first=84 second=244 amount=-5
  90. if (len > 3)
  91. {
  92. int first = GetInt(split[1]);
  93. int second = GetInt(split[2]);
  94. int amount = GetInt(split[3]);
  95. BMGlyph glyph = font.GetGlyph(second, true);
  96. if (glyph != null) glyph.SetKerning(first, amount);
  97. }
  98. else
  99. {
  100. Debug.LogError("Unexpected number of entries for the 'kerning' field (" +
  101. name + ", " + split.Length + "):\n" + line);
  102. break;
  103. }
  104. }
  105. else if (split[0] == "common")
  106. {
  107. // Expected data style:
  108. // common lineHeight=64 base=51 scaleW=512 scaleH=512 pages=1 packed=0 alphaChnl=1 redChnl=4 greenChnl=4 blueChnl=4
  109. if (len > 5)
  110. {
  111. font.charSize = GetInt(split[1]);
  112. font.baseOffset = GetInt(split[2]);
  113. font.texWidth = GetInt(split[3]);
  114. font.texHeight = GetInt(split[4]);
  115. int pages = GetInt(split[5]);
  116. if (pages != 1)
  117. {
  118. Debug.LogError("Font '" + name + "' must be created with only 1 texture, not " + pages);
  119. break;
  120. }
  121. }
  122. else
  123. {
  124. Debug.LogError("Unexpected number of entries for the 'common' field (" +
  125. name + ", " + split.Length + "):\n" + line);
  126. break;
  127. }
  128. }
  129. else if (split[0] == "page")
  130. {
  131. // Expected data style:
  132. // page id=0 file="textureName.png"
  133. if (len > 2)
  134. {
  135. font.spriteName = GetString(split[2]).Replace("\"", "");
  136. font.spriteName = font.spriteName.Replace(".png", "");
  137. font.spriteName = font.spriteName.Replace(".tga", "");
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }