BMGlyph.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. /// Glyph structure used by BMFont. For more information see http://www.angelcode.com/products/bmfont/
  9. /// </summary>
  10. [System.Serializable]
  11. public class BMGlyph
  12. {
  13. public int index; // Index of this glyph (used by BMFont)
  14. public int x; // Offset from the left side of the texture to the left side of the glyph
  15. public int y; // Offset from the top of the texture to the top of the glyph
  16. public int width; // Glyph's width in pixels
  17. public int height; // Glyph's height in pixels
  18. public int offsetX; // Offset to apply to the cursor's left position before drawing this glyph
  19. public int offsetY; // Offset to apply to the cursor's top position before drawing this glyph
  20. public int advance; // How much to move the cursor after printing this character
  21. public int channel; // Channel mask (in most cases this will be 15 (RGBA, 1+2+4+8)
  22. public List<int> kerning;
  23. /// <summary>
  24. /// Retrieves the special amount by which to adjust the cursor position, given the specified previous character.
  25. /// </summary>
  26. public int GetKerning (int previousChar)
  27. {
  28. if (kerning != null && previousChar != 0)
  29. {
  30. for (int i = 0, imax = kerning.Count; i < imax; i += 2)
  31. if (kerning[i] == previousChar)
  32. return kerning[i + 1];
  33. }
  34. return 0;
  35. }
  36. /// <summary>
  37. /// Add a new kerning entry to the character (or adjust an existing one).
  38. /// </summary>
  39. public void SetKerning (int previousChar, int amount)
  40. {
  41. if (kerning == null) kerning = new List<int>();
  42. for (int i = 0; i < kerning.Count; i += 2)
  43. {
  44. if (kerning[i] == previousChar)
  45. {
  46. kerning[i + 1] = amount;
  47. return;
  48. }
  49. }
  50. kerning.Add(previousChar);
  51. kerning.Add(amount);
  52. }
  53. /// <summary>
  54. /// Trim the glyph, given the specified minimum and maximum dimensions in pixels.
  55. /// </summary>
  56. public void Trim (int xMin, int yMin, int xMax, int yMax)
  57. {
  58. int x1 = x + width;
  59. int y1 = y + height;
  60. if (x < xMin)
  61. {
  62. int offset = xMin - x;
  63. x += offset;
  64. width -= offset;
  65. offsetX += offset;
  66. }
  67. if (y < yMin)
  68. {
  69. int offset = yMin - y;
  70. y += offset;
  71. height -= offset;
  72. offsetY += offset;
  73. }
  74. if (x1 > xMax) width -= x1 - xMax;
  75. if (y1 > yMax) height -= y1 - yMax;
  76. }
  77. }