Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 676 lines (534 sloc) 18.779 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
/*
* cocos2d for iPhone: http://www.cocos2d-iphone.org
*
* Copyright (c) 2008-2010 Ricardo Quesada
* Copyright (c) 2011 Zynga Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* Portions of this code are based and inspired on:
* http://www.71squared.co.uk/2009/04/iphone-game-programming-tutorial-4-bitmap-font-class
* by Michael Daley
*
*
* Use any of these editors to generate BMFonts:
* http://glyphdesigner.71squared.com/ (Commercial, Mac OS X)
* http://www.n4te.com/hiero/hiero.jnlp (Free, Java)
* http://slick.cokeandcode.com/demos/hiero.jnlp (Free, Java)
* http://www.angelcode.com/products/bmfont/ (Free, Windows only)
*/

#import "ccConfig.h"
#import "CCLabelBMFont.h"
#import "CCSprite.h"
#import "CCDrawingPrimitives.h"
#import "CCConfiguration.h"
#import "Support/CCFileUtils.h"
#import "Support/CGPointExtension.h"
#import "Support/uthash.h"

#pragma mark -
#pragma mark FNTConfig Cache - free functions

NSMutableDictionary *configurations = nil;
CCBMFontConfiguration* FNTConfigLoadFile( NSString *fntFile)
{
CCBMFontConfiguration *ret = nil;

if( configurations == nil )
configurations = [[NSMutableDictionary dictionaryWithCapacity:3] retain];

ret = [configurations objectForKey:fntFile];
if( ret == nil ) {
ret = [CCBMFontConfiguration configurationWithFNTFile:fntFile];
[configurations setObject:ret forKey:fntFile];
}

return ret;
}

void FNTConfigRemoveCache( void )
{
[configurations removeAllObjects];
}

#pragma mark - Hash Element

// Equal function for targetSet.
typedef struct _KerningHashElement
{
int key; // key for the hash. 16-bit for 1st element, 16-bit for 2nd element
int amount;
UT_hash_handle hh;
} tKerningHashElement;

#pragma mark -
#pragma mark BitmapFontConfiguration


@interface CCBMFontConfiguration (Private)
-(void) parseConfigFile:(NSString*)controlFile;
-(void) parseCharacterDefinition:(NSString*)line charDef:(ccBMFontDef*)characterDefinition;
-(void) parseInfoArguments:(NSString*)line;
-(void) parseCommonArguments:(NSString*)line;
-(void) parseImageFileName:(NSString*)line fntFile:(NSString*)fntFile;
-(void) parseKerningCapacity:(NSString*)line;
-(void) parseKerningEntry:(NSString*)line;
-(void) purgeKerningDictionary;
@end

@implementation CCBMFontConfiguration

+(id) configurationWithFNTFile:(NSString*)FNTfile
{
return [[[self alloc] initWithFNTfile:FNTfile] autorelease];
}

-(id) initWithFNTfile:(NSString*)fntFile
{
if((self=[super init])) {

kerningDictionary_ = NULL;

[self parseConfigFile:fntFile];
}
return self;
}

- (void) dealloc
{
CCLOGINFO( @"cocos2d: deallocing %@", self);
[self purgeKerningDictionary];
[atlasName_ release];
[super dealloc];
}

- (NSString*) description
{
return [NSString stringWithFormat:@"<%@ = %08X | Kernings:%d | Image = %@>", [self class], self,
HASH_COUNT(kerningDictionary_),
atlasName_];
}


-(void) purgeKerningDictionary
{
tKerningHashElement *current;

while(kerningDictionary_) {
current = kerningDictionary_;
HASH_DEL(kerningDictionary_,current);
free(current);
}
}

- (void)parseConfigFile:(NSString*)fntFile
{
NSString *fullpath = [CCFileUtils fullPathFromRelativePath:fntFile];
NSError *error;
NSString *contents = [NSString stringWithContentsOfFile:fullpath encoding:NSUTF8StringEncoding error:&error];

NSAssert1( contents, @"cocos2d: Error parsing FNTfile: %@", error);


// Move all lines in the string, which are denoted by \n, into an array
NSArray *lines = [[NSArray alloc] initWithArray:[contents componentsSeparatedByString:@"\n"]];

// Create an enumerator which we can use to move through the lines read from the control file
NSEnumerator *nse = [lines objectEnumerator];

// Create a holder for each line we are going to work with
NSString *line;

// Loop through all the lines in the lines array processing each one
while( (line = [nse nextObject]) ) {
// parse spacing / padding
if([line hasPrefix:@"info face"]) {
// XXX: info parsing is incomplete
// Not needed for the Hiero editors, but needed for the AngelCode editor
// [self parseInfoArguments:line];
}
// Check to see if the start of the line is something we are interested in
else if([line hasPrefix:@"common lineHeight"]) {
[self parseCommonArguments:line];
}
else if([line hasPrefix:@"page id"]) {
[self parseImageFileName:line fntFile:fntFile];
}
else if([line hasPrefix:@"chars c"]) {
// Ignore this line
}
else if([line hasPrefix:@"char"]) {
// Parse the current line and create a new CharDef
ccBMFontDef characterDefinition;
[self parseCharacterDefinition:line charDef:&characterDefinition];

// Add the CharDef returned to the charArray
BMFontArray_[ characterDefinition.charID ] = characterDefinition;
}
else if([line hasPrefix:@"kernings count"]) {
[self parseKerningCapacity:line];
}
else if([line hasPrefix:@"kerning first"]) {
[self parseKerningEntry:line];
}
}
// Finished with lines so release it
[lines release];
}

-(void) parseImageFileName:(NSString*)line fntFile:(NSString*)fntFile
{
NSString *propertyValue = nil;

// Break the values for this line up using =
NSArray *values = [line componentsSeparatedByString:@"="];

// Get the enumerator for the array of components which has been created
NSEnumerator *nse = [values objectEnumerator];

// We need to move past the first entry in the array before we start assigning values
[nse nextObject];

// page ID. Sanity check
propertyValue = [nse nextObject];
NSAssert( [propertyValue intValue] == 0, @"XXX: LabelBMFont only supports 1 page");

// file
propertyValue = [nse nextObject];
NSArray *array = [propertyValue componentsSeparatedByString:@"\""];
propertyValue = [array objectAtIndex:1];
NSAssert(propertyValue,@"LabelBMFont file could not be found");

// Supports subdirectories
NSString *dir = [fntFile stringByDeletingLastPathComponent];
atlasName_ = [dir stringByAppendingPathComponent:propertyValue];

[atlasName_ retain];
}

-(void) parseInfoArguments:(NSString*)line
{
//
// possible lines to parse:
// info face="Script" size=32 bold=0 italic=0 charset="" unicode=1 stretchH=100 smooth=1 aa=1 padding=1,4,3,2 spacing=0,0 outline=0
// info face="Cracked" size=36 bold=0 italic=0 charset="" unicode=0 stretchH=100 smooth=1 aa=1 padding=0,0,0,0 spacing=1,1
//
NSArray *values = [line componentsSeparatedByString:@"="];
NSEnumerator *nse = [values objectEnumerator];
NSString *propertyValue = nil;

// We need to move past the first entry in the array before we start assigning values
[nse nextObject];

// face (ignore)
[nse nextObject];

// size (ignore)
[nse nextObject];

// bold (ignore)
[nse nextObject];

// italic (ignore)
[nse nextObject];

// charset (ignore)
[nse nextObject];

// unicode (ignore)
[nse nextObject];

// strechH (ignore)
[nse nextObject];

// smooth (ignore)
[nse nextObject];

// aa (ignore)
[nse nextObject];

// padding (ignore)
propertyValue = [nse nextObject];
{

NSArray *paddingValues = [propertyValue componentsSeparatedByString:@","];
NSEnumerator *paddingEnum = [paddingValues objectEnumerator];
// padding top
propertyValue = [paddingEnum nextObject];
padding_.top = [propertyValue intValue];

// padding right
propertyValue = [paddingEnum nextObject];
padding_.right = [propertyValue intValue];

// padding bottom
propertyValue = [paddingEnum nextObject];
padding_.bottom = [propertyValue intValue];

// padding left
propertyValue = [paddingEnum nextObject];
padding_.left = [propertyValue intValue];

CCLOG(@"cocos2d: padding: %d,%d,%d,%d", padding_.left, padding_.top, padding_.right, padding_.bottom);
}

// spacing (ignore)
[nse nextObject];
}

-(void) parseCommonArguments:(NSString*)line
{
//
// line to parse:
// common lineHeight=104 base=26 scaleW=1024 scaleH=512 pages=1 packed=0
//
NSArray *values = [line componentsSeparatedByString:@"="];
NSEnumerator *nse = [values objectEnumerator];
NSString *propertyValue = nil;

// We need to move past the first entry in the array before we start assigning values
[nse nextObject];

// Character ID
propertyValue = [nse nextObject];
commonHeight_ = [propertyValue intValue];

// base (ignore)
[nse nextObject];


// scaleW. sanity check
propertyValue = [nse nextObject];
NSAssert( [propertyValue intValue] <= [[CCConfiguration sharedConfiguration] maxTextureSize], @"CCLabelBMFont: page can't be larger than supported");

// scaleH. sanity check
propertyValue = [nse nextObject];
NSAssert( [propertyValue intValue] <= [[CCConfiguration sharedConfiguration] maxTextureSize], @"CCLabelBMFont: page can't be larger than supported");

// pages. sanity check
propertyValue = [nse nextObject];
NSAssert( [propertyValue intValue] == 1, @"CCBitfontAtlas: only supports 1 page");

// packed (ignore) What does this mean ??
}
- (void)parseCharacterDefinition:(NSString*)line charDef:(ccBMFontDef*)characterDefinition
{
// Break the values for this line up using =
NSArray *values = [line componentsSeparatedByString:@"="];
NSEnumerator *nse = [values objectEnumerator];
NSString *propertyValue;

// We need to move past the first entry in the array before we start assigning values
[nse nextObject];

// Character ID
propertyValue = [nse nextObject];
propertyValue = [propertyValue substringToIndex: [propertyValue rangeOfString: @" "].location];
characterDefinition->charID = [propertyValue intValue];
NSAssert(characterDefinition->charID < kCCBMFontMaxChars, @"BitmpaFontAtlas: CharID bigger than supported");

// Character x
propertyValue = [nse nextObject];
characterDefinition->rect.origin.x = [propertyValue intValue];
// Character y
propertyValue = [nse nextObject];
characterDefinition->rect.origin.y = [propertyValue intValue];
// Character width
propertyValue = [nse nextObject];
characterDefinition->rect.size.width = [propertyValue intValue];
// Character height
propertyValue = [nse nextObject];
characterDefinition->rect.size.height = [propertyValue intValue];
// Character xoffset
propertyValue = [nse nextObject];
characterDefinition->xOffset = [propertyValue intValue];
// Character yoffset
propertyValue = [nse nextObject];
characterDefinition->yOffset = [propertyValue intValue];
// Character xadvance
propertyValue = [nse nextObject];
characterDefinition->xAdvance = [propertyValue intValue];
}

-(void) parseKerningCapacity:(NSString*) line
{
// When using uthash there is not need to parse the capacity.

// NSAssert(!kerningDictionary, @"dictionary already initialized");
//
// // Break the values for this line up using =
// NSArray *values = [line componentsSeparatedByString:@"="];
// NSEnumerator *nse = [values objectEnumerator];
// NSString *propertyValue;
//
// // We need to move past the first entry in the array before we start assigning values
// [nse nextObject];
//
// // count
// propertyValue = [nse nextObject];
// int capacity = [propertyValue intValue];
//
// if( capacity != -1 )
// kerningDictionary = ccHashSetNew(capacity, targetSetEql);
}

-(void) parseKerningEntry:(NSString*) line
{
NSArray *values = [line componentsSeparatedByString:@"="];
NSEnumerator *nse = [values objectEnumerator];
NSString *propertyValue;

// We need to move past the first entry in the array before we start assigning values
[nse nextObject];

// first
propertyValue = [nse nextObject];
int first = [propertyValue intValue];

// second
propertyValue = [nse nextObject];
int second = [propertyValue intValue];

// second
propertyValue = [nse nextObject];
int amount = [propertyValue intValue];

tKerningHashElement *element = calloc( sizeof( *element ), 1 );
element->amount = amount;
element->key = (first<<16) | (second&0xffff);
HASH_ADD_INT(kerningDictionary_,key, element);
}

@end

#pragma mark -
#pragma mark CCLabelBMFont

@interface CCLabelBMFont (Private)
-(NSString*) atlasNameFromFntFile:(NSString*)fntFile;

-(int) kerningAmountForFirst:(unichar)first second:(unichar)second;

@end

@implementation CCLabelBMFont

@synthesize opacity = opacity_, color = color_;

#pragma mark LabelBMFont - Purge Cache
+(void) purgeCachedData
{
FNTConfigRemoveCache();
}

#pragma mark LabelBMFont - Creation & Init

+(id) labelWithString:(NSString *)string fntFile:(NSString *)fntFile
{
return [[[self alloc] initWithString:string fntFile:fntFile] autorelease];
}

// XXX - deprecated - Will be removed in 1.0.1
+(id) bitmapFontAtlasWithString:(NSString*)string fntFile:(NSString*)fntFile
{
return [self labelWithString:string fntFile:fntFile];
}

-(id) initWithString:(NSString*)theString fntFile:(NSString*)fntFile
{

[configuration_ release]; // allow re-init

configuration_ = FNTConfigLoadFile(fntFile);
[configuration_ retain];

NSAssert( configuration_, @"Error creating config for LabelBMFont");


if ((self=[super initWithFile:configuration_->atlasName_ capacity:[theString length]])) {

opacity_ = 255;
color_ = ccWHITE;

contentSize_ = CGSizeZero;

opacityModifyRGB_ = [[textureAtlas_ texture] hasPremultipliedAlpha];

anchorPoint_ = ccp(0.5f, 0.5f);

[self setString:theString];
}

return self;
}

-(void) dealloc
{
[string_ release];
[configuration_ release];
[super dealloc];
}

#pragma mark LabelBMFont - Atlas generation

-(int) kerningAmountForFirst:(unichar)first second:(unichar)second
{
int ret = 0;
unsigned int key = (first<<16) | (second & 0xffff);

if( configuration_->kerningDictionary_ ) {
tKerningHashElement *element = NULL;
HASH_FIND_INT(configuration_->kerningDictionary_, &key, element);
if(element)
ret = element->amount;
}

return ret;
}

-(void) createFontChars
{
NSInteger nextFontPositionX = 0;
NSInteger nextFontPositionY = 0;
unichar prev = -1;
NSInteger kerningAmount = 0;

CGSize tmpSize = CGSizeZero;

NSInteger longestLine = 0;
NSUInteger totalHeight = 0;

NSUInteger quantityOfLines = 1;

NSUInteger stringLen = [string_ length];
if( ! stringLen )
return;

// quantity of lines NEEDS to be calculated before parsing the lines,
// since the Y position needs to be calcualted before hand
for(NSUInteger i=0; i < stringLen-1;i++) {
unichar c = [string_ characterAtIndex:i];
if( c=='\n')
quantityOfLines++;
}

totalHeight = configuration_->commonHeight_ * quantityOfLines;
nextFontPositionY = -(configuration_->commonHeight_ - configuration_->commonHeight_*quantityOfLines);

for(NSUInteger i=0; i<stringLen; i++) {
unichar c = [string_ characterAtIndex:i];
NSAssert( c < kCCBMFontMaxChars, @"LabelBMFont: character outside bounds");

if (c == '\n') {
nextFontPositionX = 0;
nextFontPositionY -= configuration_->commonHeight_;
continue;
}

kerningAmount = [self kerningAmountForFirst:prev second:c];

ccBMFontDef fontDef = configuration_->BMFontArray_[c];

CGRect rect = fontDef.rect;

CCSprite *fontChar;

fontChar = (CCSprite*) [self getChildByTag:i];
if( ! fontChar ) {
fontChar = [[CCSprite alloc] initWithBatchNode:self rectInPixels:rect];
[self addChild:fontChar z:0 tag:i];
[fontChar release];
}
else {
// reusing fonts
[fontChar setTextureRectInPixels:rect rotated:NO untrimmedSize:rect.size];

// restore to default in case they were modified
fontChar.visible = YES;
fontChar.opacity = 255;
}

float yOffset = configuration_->commonHeight_ - fontDef.yOffset;
fontChar.positionInPixels = ccp( (float)nextFontPositionX + fontDef.xOffset + fontDef.rect.size.width*0.5f + kerningAmount,
(float)nextFontPositionY + yOffset - rect.size.height*0.5f );

// update kerning
nextFontPositionX += configuration_->BMFontArray_[c].xAdvance + kerningAmount;
prev = c;

// Apply label properties
[fontChar setOpacityModifyRGB:opacityModifyRGB_];
// Color MUST be set before opacity, since opacity might change color if OpacityModifyRGB is on
[fontChar setColor:color_];

// only apply opacity if it is different than 255 )
// to prevent modifying the color too (issue #610)
if( opacity_ != 255 )
[fontChar setOpacity: opacity_];

if (longestLine < nextFontPositionX)
longestLine = nextFontPositionX;
}

tmpSize.width = longestLine;
tmpSize.height = totalHeight;

[self setContentSizeInPixels:tmpSize];
}

#pragma mark LabelBMFont - CCLabelProtocol protocol
- (void) setString:(NSString*) newString
{
[string_ release];
string_ = [newString copy];

CCNode *child;
CCARRAY_FOREACH(children_, child)
child.visible = NO;

[self createFontChars];
}

-(NSString*) string
{
return string_;
}

-(void) setCString:(char*)label
{
[self setString:[NSString stringWithUTF8String:label]];
}

#pragma mark LabelBMFont - CCRGBAProtocol protocol

-(void) setColor:(ccColor3B)color
{
color_ = color;

CCSprite *child;
CCARRAY_FOREACH(children_, child)
[child setColor:color_];
}

-(void) setOpacity:(GLubyte)opacity
{
opacity_ = opacity;

id<CCRGBAProtocol> child;
CCARRAY_FOREACH(children_, child)
[child setOpacity:opacity_];
}
-(void) setOpacityModifyRGB:(BOOL)modify
{
opacityModifyRGB_ = modify;

id<CCRGBAProtocol> child;
CCARRAY_FOREACH(children_, child)
[child setOpacityModifyRGB:modify];
}

-(BOOL) doesOpacityModifyRGB
{
return opacityModifyRGB_;
}

#pragma mark LabelBMFont - AnchorPoint
-(void) setAnchorPoint:(CGPoint)point
{
if( ! CGPointEqualToPoint(point, anchorPoint_) ) {
[super setAnchorPoint:point];
[self createFontChars];
}
}

#pragma mark LabelBMFont - Debug draw
#if CC_LABELBMFONT_DEBUG_DRAW
-(void) draw
{
[super draw];

CGSize s = [self contentSize];
CGPoint vertices[4]={
ccp(0,0),ccp(s.width,0),
ccp(s.width,s.height),ccp(0,s.height),
};
ccDrawPoly(vertices, 4, YES);
}
#endif // CC_LABELBMFONT_DEBUG_DRAW
@end
Something went wrong with that request. Please try again.