macFont.mm 8.6 KB

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