osxFont.mm 8.5 KB

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