// // CCFontIOS.mm // TestNewStringStuff // // Created by Carlo Morgantini on 5/20/13. // // #include #include #include #include #include #include #include "support/ccUTF8.h" #include "cocos2d.h" #include "CCFontIOS.h" #include "CCTextImage.h" NS_CC_BEGIN bool FontIOS::createFontObject(const std::string &fontName, int fontSize) { CFStringRef theRefString = NULL; theRefString = CFStringCreateWithCString(kCFAllocatorDefault, fontName.c_str(), CFStringGetSystemEncoding()); NSString * fntName = [NSString stringWithUTF8String:fontName.c_str()]; // actually create iOS font (s) _fontRef = CTFontCreateWithName(theRefString, fontSize, NULL); _fontUI = [UIFont fontWithName:fntName size:fontSize]; return ( (_fontRef != NULL) && (_fontUI != NULL) ); } FontIOS::~FontIOS() { // release the font // TO DO } GlyphDef * FontIOS::getGlyphsForText(const char *pText, int &outNumGlyphs) { float CHAR_PADDING = 10.0f; UniChar * characters; CGGlyph * glyphs; CFIndex count; CFStringRef lettersString; lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8); if (NULL == lettersString) return false; count = CFStringGetLength(lettersString); // Allocate our buffers for characters and glyphs. characters = new UniChar[count]; assert(characters != NULL); glyphs = new CGGlyph[count]; assert(glyphs != NULL); // Get the characters from the string. CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters); // Get the glyphs for the characters. CTFontGetGlyphsForCharacters(_fontRef, characters, glyphs, count); CGGlyph *theFirstGlyph = &glyphs[0]; // get letters bounding boxes CGRect *BBOx = new CGRect[count]; assert(BBOx != NULL); CTFontGetBoundingRectsForGlyphs(_fontRef, kCTFontHorizontalOrientation, theFirstGlyph, BBOx, count); GlyphDef *pGlyphs = new GlyphDef[count]; assert(pGlyphs != NULL); // sore result as CCRect for (int c=0; c=newEnd ) return 0; NSString * str = [NSString stringWithUTF8String:pText]; if ( newEnd >= [str length]) return 0; NSRange theRange; theRange.location = newBegin; theRange.length = (newEnd - newBegin) +1; // trim the string NSString *trimmedString = [str substringWithRange:theRange]; // ret the string return [trimmedString UTF8String]; } int FontIOS::getUTF8TextLenght(const char *pText) { CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8); if (NULL == lettersString) { return 0; } return CFStringGetLength(lettersString); } NS_CC_END