BM_Font.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 Padding
  37. {
  38. int Up,
  39. Right,
  40. Down,
  41. Left;
  42. };
  43. struct Spacing
  44. {
  45. int Horizontal,
  46. Vertical;
  47. };
  48. struct FontInfo
  49. {
  50. String FamilyName;
  51. String Source;
  52. String Directory;
  53. int Size;
  54. Font::Style Style;
  55. Font::Weight Weight;
  56. String CharsetName;
  57. bool IsUnicode;
  58. int StretchHeight;
  59. bool IsSmoothed;
  60. int SuperSamplingLevel;
  61. Padding FontPadding;
  62. Spacing FontSpacing;
  63. int Outline;
  64. };
  65. struct CharacterCommonInfo
  66. {
  67. int LineHeight;
  68. int BaseLine;
  69. int ScaleWidth;
  70. int ScaleHeight;
  71. int PageCount;
  72. bool IsPacked;
  73. int AlphaChanelUsage;
  74. int RedChanelUsage;
  75. int GreenChanelUsage;
  76. int BlueChanelUsage;
  77. int CharacterCount;
  78. int KerningCount;
  79. };
  80. struct PageInfo
  81. {
  82. int Id;
  83. String FileName;
  84. };
  85. struct CharacterInfo
  86. {
  87. int Id;
  88. int X;
  89. int Y;
  90. int Width;
  91. int Height;
  92. int XOffset;
  93. int YOffset;
  94. int Advance;
  95. int PageId;
  96. int ChannelUsed;
  97. };
  98. struct KerningInfo
  99. {
  100. int FirstCharacterId;
  101. int SecondCharacterId;
  102. int KerningAmount;
  103. };
  104. class BM_Font
  105. {
  106. public:
  107. FontInfo Face;
  108. CharacterCommonInfo CommonCharactersInfo;
  109. PageInfo *PagesInfo;
  110. CharacterInfo *CharactersInfo;
  111. KerningInfo *KerningsInfo;
  112. int BM_Helper_GetCharacterTableIndex( int unicode_code )
  113. {
  114. return BinarySearch( unicode_code, 0, CommonCharactersInfo.CharacterCount );
  115. }
  116. int BM_Helper_GetXKerning( int left_uni_id, int right_uni_id )
  117. {
  118. for ( int i = 0; i < this->CommonCharactersInfo.KerningCount; i++ )
  119. {
  120. if ( this->KerningsInfo[i].FirstCharacterId == left_uni_id && this->KerningsInfo[i].SecondCharacterId == right_uni_id )
  121. {
  122. return this->KerningsInfo[i].KerningAmount;
  123. }
  124. }
  125. return 0;
  126. }
  127. private:
  128. int BinarySearch( int unicode_code, int min_index, int max_index )
  129. {
  130. if ( abs( max_index - min_index ) <= 1 )
  131. {
  132. if ( this->CharactersInfo[ min_index ].Id == unicode_code )
  133. {
  134. return min_index;
  135. }
  136. else if ( this->CharactersInfo[ max_index ].Id == unicode_code )
  137. {
  138. return max_index;
  139. }
  140. else
  141. {
  142. return -1;
  143. }
  144. }
  145. else
  146. {
  147. int mid_index = ( min_index + max_index ) / 2;
  148. if ( this->CharactersInfo[ mid_index ].Id == unicode_code )
  149. {
  150. return mid_index;
  151. }
  152. else if ( this->CharactersInfo[ mid_index ].Id > unicode_code )
  153. {
  154. return BinarySearch( unicode_code, min_index, mid_index );
  155. }
  156. else
  157. {
  158. return BinarySearch( unicode_code, mid_index, max_index );
  159. }
  160. }
  161. }
  162. };
  163. }
  164. }
  165. }
  166. #endif