iOSFont.mm 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "platformiOS/platformiOS.h"
  24. #import "platformiOS/iOSFont.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 iOSFont;
  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. iOSFont::iOSFont()
  42. {
  43. // Reset the rendering color-space.
  44. mColorSpace = NULL;
  45. }
  46. //------------------------------------------------------------------------------
  47. iOSFont::~iOSFont()
  48. {
  49. // Destroy the rendering color-space.
  50. CGColorSpaceRelease( mColorSpace );
  51. }
  52. //------------------------------------------------------------------------------
  53. bool iOSFont::create( const char* name, U32 size, U32 charset )
  54. {
  55. // Sanity!
  56. AssertFatal( name != NULL, "Cannot create a NULL font name." );
  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. // Return status.
  86. return true;
  87. }
  88. //------------------------------------------------------------------------------
  89. bool iOSFont::isValidChar( const UTF8* str ) const
  90. {
  91. // since only low order characters are invalid, and since those characters
  92. // are single codeunits in UTF8, we can safely cast here.
  93. return isValidChar((UTF16)*str);
  94. }
  95. //------------------------------------------------------------------------------
  96. bool iOSFont::isValidChar( const UTF16 character) const
  97. {
  98. // We cut out the ASCII control chars here. Only printable characters are valid.
  99. // 0x20 == 32 == space
  100. if( character < 0x20 )
  101. return false;
  102. return true;
  103. }
  104. //------------------------------------------------------------------------------
  105. PlatformFont::CharInfo& iOSFont::getCharInfo(const UTF8 *str) const
  106. {
  107. return getCharInfo( oneUTF32toUTF16(oneUTF8toUTF32(str,NULL)) );
  108. }
  109. //------------------------------------------------------------------------------
  110. PlatformFont::CharInfo& iOSFont::getCharInfo(const UTF16 character) const
  111. {
  112. // Declare and clear out the CharInfo that will be returned.
  113. static PlatformFont::CharInfo characterInfo;
  114. dMemset(&characterInfo, 0, sizeof(characterInfo));
  115. // prep values for GFont::addBitmap()
  116. characterInfo.bitmapIndex = 0;
  117. characterInfo.xOffset = 0;
  118. characterInfo.yOffset = 0;
  119. CGGlyph characterGlyph;
  120. CGRect characterBounds;
  121. CGSize characterAdvances;
  122. UniChar unicodeCharacter = character;
  123. // Fetch font glyphs.
  124. if ( !CTFontGetGlyphsForCharacters( mFontRef, &unicodeCharacter, &characterGlyph, (CFIndex)1) )
  125. {
  126. // Sanity!
  127. AssertFatal( false, "Cannot create font glyph." );
  128. }
  129. // Fetch glyph bounding box.
  130. CTFontGetBoundingRectsForGlyphs( mFontRef, kCTFontHorizontalOrientation, &characterGlyph, &characterBounds, (CFIndex)1 );
  131. // Fetch glyph advances.
  132. CTFontGetAdvancesForGlyphs( mFontRef, kCTFontHorizontalOrientation, &characterGlyph, &characterAdvances, (CFIndex)1 );
  133. // Set character metrics,
  134. characterInfo.xOrigin = (S32)mRound( characterBounds.origin.x );
  135. characterInfo.yOrigin = (S32)mRound( characterBounds.origin.y );
  136. characterInfo.width = (U32)mCeil( characterBounds.size.width ) + 2;
  137. characterInfo.height = (U32)mCeil( characterBounds.size.height ) + 2;
  138. characterInfo.xIncrement = (S32)mRound( characterAdvances.width );
  139. // Finish if character is undrawable.
  140. if ( characterInfo.width == 0 && characterInfo.height == 0 )
  141. return characterInfo;
  142. // Clamp character minimum width.
  143. if ( characterInfo.width == 0 )
  144. characterInfo.width = 2;
  145. if ( characterInfo.height == 0 )
  146. characterInfo.height = 1;
  147. // Allocate a bitmap surface.
  148. const U32 bitmapSize = characterInfo.width * characterInfo.height;
  149. characterInfo.bitmapData = new U8[bitmapSize];
  150. dMemset(characterInfo.bitmapData, 0x00, bitmapSize);
  151. // Create a bitmap context.
  152. CGContextRef bitmapContext = CGBitmapContextCreate( characterInfo.bitmapData, characterInfo.width, characterInfo.height, 8, characterInfo.width, mColorSpace, kCGImageAlphaNone );
  153. // Sanity!
  154. AssertFatal( bitmapContext != NULL, "Cannot create font context." );
  155. // Render font anti-aliased if font is arbitrarily small.
  156. CGContextSetShouldAntialias( bitmapContext, true);
  157. CGContextSetShouldSmoothFonts( bitmapContext, true);
  158. CGContextSetRenderingIntent( bitmapContext, kCGRenderingIntentAbsoluteColorimetric);
  159. CGContextSetInterpolationQuality( bitmapContext, kCGInterpolationNone);
  160. CGContextSetGrayFillColor( bitmapContext, 1.0, 1.0);
  161. CGContextSetTextDrawingMode( bitmapContext, kCGTextFill);
  162. // Draw glyph.
  163. CGPoint renderOrigin;
  164. renderOrigin.x = -characterInfo.xOrigin;
  165. renderOrigin.y = -characterInfo.yOrigin;
  166. CTFontDrawGlyphs( mFontRef, &characterGlyph, &renderOrigin, 1, bitmapContext );
  167. #if 0
  168. Con::printf("Width:%f, Height:%f, OriginX:%f, OriginY:%f",
  169. characterBounds.size.width,
  170. characterBounds.size.height,
  171. characterBounds.origin.x,
  172. characterBounds.origin.y );
  173. #endif
  174. // Adjust the y origin for the glyph size.
  175. characterInfo.yOrigin += characterInfo.height;// + mHeight;
  176. // Release the bitmap context.
  177. CGContextRelease( bitmapContext );
  178. // Return character information.
  179. return characterInfo;
  180. }