2010-11-12 17:16:15 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
#include <UIKit/UIKit.h>
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
#include "CCXBitmapDC.h"
|
|
|
|
#include "CCXApplication_iphone.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "platform/platform.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2011-01-21 18:15:12 +08:00
|
|
|
static bool isValidFontName(const char *fontName)
|
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
NSString *fontNameNS = [NSString stringWithUTF8String:fontName];
|
|
|
|
|
|
|
|
for (NSString *familiName in [UIFont familyNames]) {
|
|
|
|
if ([familiName isEqualToString:fontNameNS]) {
|
|
|
|
ret = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for(NSString *font in [UIFont fontNamesForFamilyName: familiName]){
|
|
|
|
if ([font isEqualToString: fontNameNS]){
|
|
|
|
ret = true;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
static void initWithString(const char *content, const char *fontName, float size, /*input params*/
|
|
|
|
int *pWidth, int *pHeight, unsigned char** ppData) /*output params*/
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
2011-01-21 18:15:12 +08:00
|
|
|
NSUInteger width, height;
|
2010-11-12 17:16:15 +08:00
|
|
|
unsigned char* data;
|
2010-12-14 11:21:44 +08:00
|
|
|
CGContextRef context;
|
2011-01-21 18:15:12 +08:00
|
|
|
CGColorSpaceRef colorSpace;
|
|
|
|
id uiFont;
|
2010-12-14 11:21:44 +08:00
|
|
|
NSString *string;
|
|
|
|
CGSize dimensions;
|
2011-01-21 18:15:12 +08:00
|
|
|
UITextAlignment alignment;
|
2010-12-14 11:21:44 +08:00
|
|
|
NSString *name;
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
if (!pWidth || !pHeight || !ppData)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
alignment = UITextAlignmentCenter;
|
2011-01-19 16:26:55 +08:00
|
|
|
string = [[NSString alloc] initWithUTF8String:content];
|
2011-01-21 18:15:12 +08:00
|
|
|
//name = isValidFontName(fontName) ? [[NSString alloc] initWithUTF8String:fontName] : @"Thonburi";
|
|
|
|
name = isValidFontName(fontName) ? [[NSString alloc] initWithUTF8String:fontName] : @"MarkerFelt-Wide";
|
2010-12-14 11:21:44 +08:00
|
|
|
dimensions = [string sizeWithFont:[UIFont fontWithName:name size:size]];
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
width = dimensions.width;
|
2010-12-14 11:21:44 +08:00
|
|
|
height = dimensions.height;
|
|
|
|
|
2010-11-12 17:16:15 +08:00
|
|
|
colorSpace = CGColorSpaceCreateDeviceRGB();
|
2010-12-14 11:21:44 +08:00
|
|
|
data = new unsigned char[height * width * 4];
|
|
|
|
|
|
|
|
memset(data, 0, height * width * 4);
|
2010-11-12 17:16:15 +08:00
|
|
|
context = CGBitmapContextCreate(data, width, height, 8, width * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
|
|
|
CGColorSpaceRelease(colorSpace);
|
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
|
2010-11-12 17:16:15 +08:00
|
|
|
CGContextTranslateCTM(context, 0.0f, height);
|
|
|
|
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
|
|
|
|
UIGraphicsPushContext(context);
|
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
uiFont = [UIFont fontWithName:name size:size];
|
|
|
|
CGRect rect;
|
|
|
|
rect.origin.x = 0;
|
|
|
|
rect.origin.y = 0;
|
|
|
|
rect.size.width = dimensions.width;
|
|
|
|
rect.size.height = dimensions.height;
|
|
|
|
[string drawInRect:rect withFont:uiFont lineBreakMode:UILineBreakModeWordWrap alignment:alignment];
|
2010-11-12 17:16:15 +08:00
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
UIGraphicsPopContext();
|
2010-11-12 17:16:15 +08:00
|
|
|
|
|
|
|
CGContextRelease(context);
|
|
|
|
|
2010-12-14 11:21:44 +08:00
|
|
|
[string release];
|
|
|
|
[name release];
|
|
|
|
|
|
|
|
// output params
|
|
|
|
*pWidth = width;
|
|
|
|
*pHeight = height;
|
|
|
|
*ppData = data;
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace cocos2d {
|
|
|
|
|
|
|
|
CCXBitmapDC::CCXBitmapDC()
|
2010-12-14 11:21:44 +08:00
|
|
|
:m_nWidth(0)
|
|
|
|
,m_nHeight(0)
|
|
|
|
,m_pData(NULL)
|
2010-11-12 17:16:15 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCXBitmapDC::CCXBitmapDC(const char *text, CGSize dimensions, UITextAlignment alignment, const char *fontName, float fontSize)
|
2010-12-14 11:21:44 +08:00
|
|
|
{
|
|
|
|
initWithString(text, fontName, fontSize, &m_nWidth, &m_nHeight, &m_pData);
|
2010-11-12 17:16:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CCXBitmapDC::~CCXBitmapDC()
|
|
|
|
{
|
|
|
|
if (m_pData)
|
|
|
|
{
|
|
|
|
delete[] m_pData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCXBitmapDC::getWidth()
|
|
|
|
{
|
|
|
|
return m_nWidth;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CCXBitmapDC::getHeight()
|
|
|
|
{
|
|
|
|
return m_nHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char* CCXBitmapDC::getData()
|
|
|
|
{
|
|
|
|
return m_pData;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|