AndroidFont.cpp 8.2 KB

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