AndroidFont.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. //------------------------------------------------------------------------------
  27. // New Unicode capable font class.
  28. PlatformFont *createPlatformFont(const char *name, U32 size, U32 charset /* = TGE_ANSI_CHARSET */)
  29. {
  30. PlatformFont *retFont = new AndroidFont;
  31. if(retFont->create(name, size, charset))
  32. return retFont;
  33. delete retFont;
  34. return NULL;
  35. }
  36. //------------------------------------------------------------------------------
  37. void PlatformFont::enumeratePlatformFonts( Vector<StringTableEntry>& fonts )
  38. {
  39. }
  40. //------------------------------------------------------------------------------
  41. AndroidFont::AndroidFont()
  42. {
  43. // Reset the rendering color-space.
  44. //TODO: android
  45. //mColorSpace = NULL;
  46. }
  47. //------------------------------------------------------------------------------
  48. AndroidFont::~AndroidFont()
  49. {
  50. }
  51. //------------------------------------------------------------------------------
  52. bool AndroidFont::create( const char* name, U32 size, U32 charset )
  53. {
  54. // Sanity!
  55. AssertFatal( name != NULL, "Cannot create a NULL font name." );
  56. //TODO: font create
  57. /*
  58. // Generate compatible font name.
  59. CFStringRef fontName = CFStringCreateWithCString( kCFAllocatorDefault, name, kCFStringEncodingUTF8 );
  60. // Sanity!
  61. if ( !fontName )
  62. {
  63. Con::errorf("Could not handle font name of '%s'.", name );
  64. return false;
  65. }
  66. // Use Windows as a baseline (96 DPI) and adjust accordingly.
  67. F32 scaledSize = size * (72.0f/96.0f);
  68. scaledSize = mRound(scaledSize);
  69. // Create the font reference.
  70. mFontRef = CTFontCreateWithName( fontName, scaledSize, NULL );
  71. // Sanity!
  72. if ( !mFontRef )
  73. {
  74. Con::errorf( "Could not generate a font reference to font name '%s' of size '%d'", name, size );
  75. return false;
  76. }
  77. // Fetch font metrics.
  78. CGFloat ascent = CTFontGetAscent( mFontRef );
  79. CGFloat descent = CTFontGetDescent( mFontRef );
  80. // Set baseline.
  81. mBaseline = (U32)mRound(ascent);
  82. // Set height.
  83. mHeight = (U32)mRound( ascent + descent );
  84. // Create a gray-scale color-space.
  85. mColorSpace = CGColorSpaceCreateDeviceGray();
  86. */
  87. // Return status.
  88. return true;
  89. }
  90. //------------------------------------------------------------------------------
  91. bool AndroidFont::isValidChar( const UTF8* str ) const
  92. {
  93. // since only low order characters are invalid, and since those characters
  94. // are single codeunits in UTF8, we can safely cast here.
  95. return isValidChar((UTF16)*str);
  96. }
  97. //------------------------------------------------------------------------------
  98. bool AndroidFont::isValidChar( const UTF16 character) const
  99. {
  100. // We cut out the ASCII control chars here. Only printable characters are valid.
  101. // 0x20 == 32 == space
  102. if( character < 0x20 )
  103. return false;
  104. return true;
  105. }
  106. //------------------------------------------------------------------------------
  107. PlatformFont::CharInfo& AndroidFont::getCharInfo(const UTF8 *str) const
  108. {
  109. return getCharInfo( oneUTF32toUTF16(oneUTF8toUTF32(str,NULL)) );
  110. }
  111. //------------------------------------------------------------------------------
  112. PlatformFont::CharInfo& AndroidFont::getCharInfo(const UTF16 character) const
  113. {
  114. // Declare and clear out the CharInfo that will be returned.
  115. static PlatformFont::CharInfo characterInfo;
  116. dMemset(&characterInfo, 0, sizeof(characterInfo));
  117. // prep values for GFont::addBitmap()
  118. characterInfo.bitmapIndex = 0;
  119. characterInfo.xOffset = 0;
  120. characterInfo.yOffset = 0;
  121. //TODO: getcharinfo
  122. /*
  123. CGGlyph characterGlyph;
  124. CGRect characterBounds;
  125. CGSize characterAdvances;
  126. UniChar unicodeCharacter = character;
  127. // Fetch font glyphs.
  128. if ( !CTFontGetGlyphsForCharacters( mFontRef, &unicodeCharacter, &characterGlyph, (CFIndex)1) )
  129. {
  130. // Sanity!
  131. AssertFatal( false, "Cannot create font glyph." );
  132. }
  133. // Fetch glyph bounding box.
  134. CTFontGetBoundingRectsForGlyphs( mFontRef, kCTFontHorizontalOrientation, &characterGlyph, &characterBounds, (CFIndex)1 );
  135. // Fetch glyph advances.
  136. CTFontGetAdvancesForGlyphs( mFontRef, kCTFontHorizontalOrientation, &characterGlyph, &characterAdvances, (CFIndex)1 );
  137. // Set character metrics,
  138. characterInfo.xOrigin = (S32)mRound( characterBounds.origin.x );
  139. characterInfo.yOrigin = (S32)mRound( characterBounds.origin.y );
  140. characterInfo.width = (U32)mCeil( characterBounds.size.width ) + 2;
  141. characterInfo.height = (U32)mCeil( characterBounds.size.height ) + 2;
  142. characterInfo.xIncrement = (S32)mRound( characterAdvances.width );
  143. // Finish if character is undrawable.
  144. if ( characterInfo.width == 0 && characterInfo.height == 0 )
  145. return characterInfo;
  146. // Clamp character minimum width.
  147. if ( characterInfo.width == 0 )
  148. characterInfo.width = 2;
  149. if ( characterInfo.height == 0 )
  150. characterInfo.height = 1;
  151. // Allocate a bitmap surface.
  152. const U32 bitmapSize = characterInfo.width * characterInfo.height;
  153. characterInfo.bitmapData = new U8[bitmapSize];
  154. dMemset(characterInfo.bitmapData, 0x00, bitmapSize);
  155. // Create a bitmap context.
  156. CGContextRef bitmapContext = CGBitmapContextCreate( characterInfo.bitmapData, characterInfo.width, characterInfo.height, 8, characterInfo.width, mColorSpace, kCGImageAlphaNone );
  157. // Sanity!
  158. AssertFatal( bitmapContext != NULL, "Cannot create font context." );
  159. // Render font anti-aliased if font is arbitrarily small.
  160. CGContextSetShouldAntialias( bitmapContext, true);
  161. CGContextSetShouldSmoothFonts( bitmapContext, true);
  162. CGContextSetRenderingIntent( bitmapContext, kCGRenderingIntentAbsoluteColorimetric);
  163. CGContextSetInterpolationQuality( bitmapContext, kCGInterpolationNone);
  164. CGContextSetGrayFillColor( bitmapContext, 1.0, 1.0);
  165. CGContextSetTextDrawingMode( bitmapContext, kCGTextFill);
  166. // Draw glyph.
  167. CGPoint renderOrigin;
  168. renderOrigin.x = -characterInfo.xOrigin;
  169. renderOrigin.y = -characterInfo.yOrigin;
  170. CTFontDrawGlyphs( mFontRef, &characterGlyph, &renderOrigin, 1, bitmapContext );
  171. #if 0
  172. Con::printf("Width:%f, Height:%f, OriginX:%f, OriginY:%f",
  173. characterBounds.size.width,
  174. characterBounds.size.height,
  175. characterBounds.origin.x,
  176. characterBounds.origin.y );
  177. #endif
  178. // Adjust the y origin for the glyph size.
  179. characterInfo.yOrigin += characterInfo.height;// + mHeight;
  180. // Release the bitmap context.
  181. CGContextRelease( bitmapContext );
  182. */
  183. // Return character information.
  184. return characterInfo;
  185. }