BitmapFontDefinitions.h 3.5 KB

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