BitmapFontDefinitions.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef BITMAPFONTDEFINITIONS_H
  28. #define BITMAPFONTDEFINITIONS_H
  29. #include <Rocket/Core/Header.h>
  30. #include <Rocket/Core/Types.h>
  31. #include <Rocket/Core/Dictionary.h>
  32. #include <set>
  33. namespace Rocket {
  34. namespace Core {
  35. namespace BitmapFont {
  36. struct FontInfo
  37. {
  38. String FamilyName;
  39. String Source;
  40. String BitmapSource;
  41. int Size;
  42. Font::Style Style;
  43. Font::Weight Weight;
  44. };
  45. struct CharacterCommonInfo
  46. {
  47. int LineHeight;
  48. int BaseLine;
  49. int ScaleWidth;
  50. int ScaleHeight;
  51. int CharacterCount;
  52. int KerningCount;
  53. };
  54. struct CharacterInfo
  55. {
  56. int Id;
  57. int X;
  58. int Y;
  59. int Width;
  60. int Height;
  61. int XOffset;
  62. int YOffset;
  63. int Advance;
  64. };
  65. struct KerningInfo
  66. {
  67. int FirstCharacterId;
  68. int SecondCharacterId;
  69. int KerningAmount;
  70. };
  71. class BitmapFontDefinitions
  72. {
  73. public:
  74. FontInfo Face;
  75. CharacterCommonInfo CommonCharactersInfo;
  76. CharacterInfo *CharactersInfo;
  77. KerningInfo *KerningsInfo;
  78. int BM_Helper_GetCharacterTableIndex( int unicode_code )
  79. {
  80. return BinarySearch( unicode_code, 0, CommonCharactersInfo.CharacterCount );
  81. }
  82. int BM_Helper_GetXKerning( int left_uni_id, int right_uni_id )
  83. {
  84. for ( int i = 0; i < this->CommonCharactersInfo.KerningCount; i++ )
  85. {
  86. if ( this->KerningsInfo[i].FirstCharacterId == left_uni_id && this->KerningsInfo[i].SecondCharacterId == right_uni_id )
  87. {
  88. return this->KerningsInfo[i].KerningAmount;
  89. }
  90. }
  91. return 0;
  92. }
  93. private:
  94. int BinarySearch( int unicode_code, int min_index, int max_index )
  95. {
  96. if ( abs( max_index - min_index ) <= 1 )
  97. {
  98. if ( this->CharactersInfo[ min_index ].Id == unicode_code )
  99. {
  100. return min_index;
  101. }
  102. else if ( this->CharactersInfo[ max_index ].Id == unicode_code )
  103. {
  104. return max_index;
  105. }
  106. else
  107. {
  108. return -1;
  109. }
  110. }
  111. else
  112. {
  113. int mid_index = ( min_index + max_index ) / 2;
  114. if ( this->CharactersInfo[ mid_index ].Id == unicode_code )
  115. {
  116. return mid_index;
  117. }
  118. else if ( this->CharactersInfo[ mid_index ].Id > unicode_code )
  119. {
  120. return BinarySearch( unicode_code, min_index, mid_index );
  121. }
  122. else
  123. {
  124. return BinarySearch( unicode_code, mid_index, max_index );
  125. }
  126. }
  127. }
  128. };
  129. }
  130. }
  131. }
  132. #endif