вторник, 7 сентября 2010 г.

NSString to wstring

NSString to std::wstring & std::wstring to NSString

  1. std::wstring NSStringToStringW ( NSString* Str )   
  2. {   
  3.     NSStringEncoding pEncode    =   CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE );   
  4.     NSData* pSData              =   [ Str dataUsingEncoding : pEncode ];    
  5.    
  6.     return std::wstring ( (wchar_t*) [ pSData bytes ], [ pSData length] / sizeof ( wchar_t ) );   
  7. }   
  8.    
  9. NSString* StringWToNSString ( const std::wstring& Str )   
  10. {   
  11.     NSString* pString = [ [ NSString alloc ]    
  12.                         initWithBytes : (char*)Str.data()   
  13.                                length : Str.size() * sizeof(wchar_t)   
  14.                              encoding : CFStringConvertEncodingToNSStringEncoding ( kCFStringEncodingUTF32LE ) ];   
  15.     return pString;   
  16. }  
.

понедельник, 6 сентября 2010 г.

Create a context for the drawing of the buffer.

CGContext Reference
CGColorSpace Reference

  1. int ImageWidth          =   512;   
  2. int ImageHeight         =   512;   
  3.    
  4. unsigned char* pBuffer  =   NULL;   
  5.    
  6. CGContextRef CreateBitmapContext ()   
  7. {   
  8.     CGContextRef    pContext        =   NULL;   
  9.     CGColorSpaceRef ColorSpace  =   CGColorSpaceCreateDeviceRGB ();   
  10.    
  11.     int bitmapByteCount     =   ( ImageWidth * 4 );   
  12.     int bitmapBytesPerRow   =   ( bitmapBytesPerRow * ImageWidth );   
  13.    
  14.     pBuffer = new unsigned char [ bitmapByteCount ];   
  15.    
  16.     if ( NULL == pBuffer )   
  17.     {   
  18.         delete [] pBuffer;   
  19.    
  20.         CGColorSpaceRelease ( ColorSpace );   
  21.    
  22.         return NULL;   
  23.     }   
  24.    
  25.     pContext    =   CGBitmapContextCreate (   
  26.         pBuffer,   
  27.         ImageWidth,   
  28.         ImageHeight,   
  29.         8,   
  30.         bitmapBytesPerRow,   
  31.         ColorSpace,   
  32.         kCGImageAlphaPremultipliedLast );   
  33.    
  34.     if ( NULL == pContext )   
  35.     {   
  36.         delete [] m_pBuffer;   
  37.    
  38.         CGColorSpaceRelease( colorSpace );   
  39.    
  40.         return NULL;   
  41.     }   
  42.    
  43.     CGColorSpaceRelease ( ColorSpace );   
  44.    
  45.     return pContext;   
  46. }  
.

среда, 1 сентября 2010 г.

Create custom font from bundle.

Use Quartz 2D objects to create custom font.

CGFont Reference
CGDataProviderRef

  1. CGFontRef FontFromBundle ( NSString* FontName )   
  2. {   
  3.     CGFontRef font  =   NULL;   
  4.    
  5.     NSString* fontPath  =   [ [ NSBundle mainBundle ] pathForResource : FontName ofType : @"ttf" ];    
  6.     CGDataProviderRef fontProvider  =   CGDataProviderCreateWithFilename([fontPath UTF8String]);   
  7.    
  8.     font    =   CGFontCreateWithDataProvider ( fontProvider );   
  9.    
  10.     CGDataProviderRelease ( fontProvider );    
  11.    
  12.     return font;   
  13. }