AndroidFont.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "platformAndroid/platformAndroid.h"
  24. #include "platformAndroid/AndroidFont.h"
  25. #include "string/unicode.h"
  26. #include <sstream>
  27. //------------------------------------------------------------------------------
  28. // New Unicode capable font class.
  29. PlatformFont *createPlatformFont(const char *name, U32 size, U32 charset /* = TGE_ANSI_CHARSET */)
  30. {
  31. PlatformFont *retFont = new AndroidFont;
  32. if(retFont->create(name, size, charset))
  33. return retFont;
  34. delete retFont;
  35. return NULL;
  36. }
  37. //------------------------------------------------------------------------------
  38. void PlatformFont::enumeratePlatformFonts( Vector<StringTableEntry>& fonts )
  39. {
  40. }
  41. //------------------------------------------------------------------------------
  42. AndroidFont::AndroidFont()
  43. {
  44. fontFileBuffer = NULL;
  45. fontFileBufferSize = 0;
  46. fontFaceCreated = false;
  47. int error = FT_Init_FreeType( &library );
  48. if ( error )
  49. {
  50. Con::errorf("Failed to initialize the freetype2 library");
  51. }
  52. }
  53. //------------------------------------------------------------------------------
  54. AndroidFont::~AndroidFont()
  55. {
  56. FT_Done_Face( face );
  57. if (fontFileBuffer != NULL)
  58. {
  59. delete[] fontFileBuffer;
  60. fontFileBufferSize = 0;
  61. }
  62. }
  63. //------------------------------------------------------------------------------
  64. bool AndroidFont::create( const char* name, U32 size, U32 charset )
  65. {
  66. // Sanity!
  67. AssertFatal( name != NULL, "Cannot create a NULL font name." );
  68. // Sanity!
  69. if ( !name )
  70. {
  71. Con::errorf("Could not handle font name of '%s'.", name );
  72. return false;
  73. }
  74. char fontPath[255];
  75. activity.getFontPath(name, fontPath);
  76. int error = FT_New_Face( library, fontPath, 0, &face );
  77. if ( error == FT_Err_Unknown_File_Format )
  78. {
  79. fontFaceCreated = false;
  80. Con::errorf("freetype2: Font was found but format is unsupported");
  81. }
  82. else if ( error )
  83. {
  84. fontFaceCreated = false;
  85. Con::errorf("freetype2: Font file was not found.");
  86. }
  87. else
  88. {
  89. fontFaceCreated = true;
  90. }
  91. if (fontFaceCreated == true)
  92. {
  93. error = FT_Set_Pixel_Sizes(face, 0, size);
  94. mBaseline = (face->size->metrics.ascender + 32) >> 6;
  95. mHeight = ((face->size->metrics.ascender + -face->size->metrics.descender) + 32) >> 6;
  96. }
  97. else
  98. {
  99. mHeight = 0;
  100. mBaseline = 0;
  101. }
  102. return true;
  103. }
  104. //------------------------------------------------------------------------------
  105. bool AndroidFont::isValidChar( const UTF8* str ) const
  106. {
  107. // since only low order characters are invalid, and since those characters
  108. // are single codeunits in UTF8, we can safely cast here.
  109. return isValidChar((UTF16)*str);
  110. }
  111. //------------------------------------------------------------------------------
  112. bool AndroidFont::isValidChar( const UTF16 character) const
  113. {
  114. // We cut out the ASCII control chars here. Only printable characters are valid.
  115. // 0x20 == 32 == space
  116. if( character < 0x20 )
  117. return false;
  118. return true;
  119. }
  120. //------------------------------------------------------------------------------
  121. PlatformFont::CharInfo& AndroidFont::getCharInfo(const UTF8 *str) const
  122. {
  123. return getCharInfo( oneUTF32toUTF16(oneUTF8toUTF32(str,NULL)) );
  124. }
  125. //------------------------------------------------------------------------------
  126. PlatformFont::CharInfo& AndroidFont::getCharInfo(const UTF16 character) const
  127. {
  128. // Declare and clear out the CharInfo that will be returned.
  129. static PlatformFont::CharInfo characterInfo;
  130. dMemset(&characterInfo, 0, sizeof(characterInfo));
  131. // prep values for GFont::addBitmap()
  132. characterInfo.bitmapIndex = 0;
  133. characterInfo.xOffset = 0;
  134. characterInfo.yOffset = 0;
  135. FT_GlyphSlot slot = face->glyph;
  136. int error = FT_Load_Char( face, character, FT_LOAD_RENDER );
  137. if ( error ) {
  138. return characterInfo;
  139. }
  140. FT_Glyph_Metrics metrics = face->glyph->metrics;
  141. // Set character metrics,
  142. characterInfo.xOrigin = slot->bitmap_left;
  143. characterInfo.yOrigin = slot->bitmap_top;
  144. characterInfo.width = (metrics.width / 64);
  145. characterInfo.height = (metrics.height / 64);
  146. characterInfo.xIncrement = slot->advance.x / 64;
  147. // characterInfo.yOrigin = characterInfo.height - characterInfo.yOrigin;
  148. // Finish if character is undrawable.
  149. if ( characterInfo.width == 0 && characterInfo.height == 0 )
  150. return characterInfo;
  151. // Clamp character minimum width.
  152. if ( characterInfo.width == 0 )
  153. return characterInfo;
  154. if ( characterInfo.height == 0 )
  155. return characterInfo;
  156. // Allocate a bitmap surface.
  157. const U32 bitmapSize = characterInfo.width * characterInfo.height;
  158. characterInfo.bitmapData = new U8[bitmapSize];
  159. dMemset(characterInfo.bitmapData, 0x00, bitmapSize);
  160. //copy glyph
  161. if (slot->bitmap.buffer != NULL)
  162. {
  163. for (int i = 0; i < slot->bitmap.width; i++)
  164. {
  165. for (int j = 0; j < slot->bitmap.rows; j++)
  166. {
  167. characterInfo.bitmapData[i + (j*slot->bitmap.width)] = slot->bitmap.buffer[i + (j*slot->bitmap.width)];
  168. }
  169. }
  170. }
  171. // Return character information.
  172. return characterInfo;
  173. }