BitmapFontDefinitions.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #ifndef RMLUI_NO_FONT_INTERFACE_DEFAULT
  31. #include <RmlUi/Core/Header.h>
  32. #include <RmlUi/Core/Types.h>
  33. #include <RmlUi/Core/Dictionary.h>
  34. #include <set>
  35. namespace Rml {
  36. namespace Core {
  37. namespace BitmapFont {
  38. struct FontInfo
  39. {
  40. String FamilyName;
  41. String Source;
  42. String BitmapSource;
  43. int Size;
  44. Style::FontStyle Style;
  45. Style::FontWeight Weight;
  46. };
  47. struct CharacterCommonInfo
  48. {
  49. int LineHeight;
  50. int BaseLine;
  51. int ScaleWidth;
  52. int ScaleHeight;
  53. int CharacterCount;
  54. int KerningCount;
  55. };
  56. struct CharacterInfo
  57. {
  58. int Id;
  59. int X;
  60. int Y;
  61. int Width;
  62. int Height;
  63. int XOffset;
  64. int YOffset;
  65. int Advance;
  66. };
  67. struct KerningInfo
  68. {
  69. int FirstCharacterId;
  70. int SecondCharacterId;
  71. int KerningAmount;
  72. };
  73. class BitmapFontDefinitions
  74. {
  75. public:
  76. FontInfo Face;
  77. CharacterCommonInfo CommonCharactersInfo;
  78. CharacterInfo *CharactersInfo;
  79. KerningInfo *KerningsInfo;
  80. int BM_Helper_GetCharacterTableIndex( int unicode_code )
  81. {
  82. return BinarySearch( unicode_code, 0, CommonCharactersInfo.CharacterCount );
  83. }
  84. int BM_Helper_GetXKerning( int left_uni_id, int right_uni_id )
  85. {
  86. for ( int i = 0; i < this->CommonCharactersInfo.KerningCount; i++ )
  87. {
  88. if ( this->KerningsInfo[i].FirstCharacterId == left_uni_id && this->KerningsInfo[i].SecondCharacterId == right_uni_id )
  89. {
  90. return this->KerningsInfo[i].KerningAmount;
  91. }
  92. }
  93. return 0;
  94. }
  95. private:
  96. int BinarySearch( int unicode_code, int min_index, int max_index )
  97. {
  98. if ( abs( max_index - min_index ) <= 1 )
  99. {
  100. if ( this->CharactersInfo[ min_index ].Id == unicode_code )
  101. {
  102. return min_index;
  103. }
  104. else if ( this->CharactersInfo[ max_index ].Id == unicode_code )
  105. {
  106. return max_index;
  107. }
  108. else
  109. {
  110. return -1;
  111. }
  112. }
  113. else
  114. {
  115. int mid_index = ( min_index + max_index ) / 2;
  116. if ( this->CharactersInfo[ mid_index ].Id == unicode_code )
  117. {
  118. return mid_index;
  119. }
  120. else if ( this->CharactersInfo[ mid_index ].Id > unicode_code )
  121. {
  122. return BinarySearch( unicode_code, min_index, mid_index );
  123. }
  124. else
  125. {
  126. return BinarySearch( unicode_code, mid_index, max_index );
  127. }
  128. }
  129. }
  130. };
  131. }
  132. }
  133. }
  134. #endif
  135. #endif