AndroidFont.cpp 8.1 KB

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