mirror of https://github.com/axmolengine/axmol.git
new stuff for FreeType
This commit is contained in:
parent
45f9edcdc3
commit
9cfe1b7823
|
@ -1 +1 @@
|
||||||
ec5e1afbe876b72e97277ce13b4b1f8b1835eaea
|
487c7958470e51a30b8e0ad7be7c9b07a74a5ff8
|
|
@ -27,6 +27,8 @@ public:
|
||||||
virtual const char * trimUTF8Text(const char *pText, int newBegin, int newEnd) = 0;
|
virtual const char * trimUTF8Text(const char *pText, int newBegin, int newEnd) = 0;
|
||||||
virtual Size getTextWidthAndHeight(const char *pText) = 0;
|
virtual Size getTextWidthAndHeight(const char *pText) = 0;
|
||||||
virtual int getUTF8TextLenght(const char *pText) = 0;
|
virtual int getUTF8TextLenght(const char *pText) = 0;
|
||||||
|
virtual int getLetterPadding() { return 0;}
|
||||||
|
virtual unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) { return 0; }
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -4,24 +4,81 @@
|
||||||
#include "cocos2d.h"
|
#include "cocos2d.h"
|
||||||
#include "CCFontFreeType.h"
|
#include "CCFontFreeType.h"
|
||||||
#include "CCTextImage.h"
|
#include "CCTextImage.h"
|
||||||
|
#include "ccUTF8.h"
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
|
||||||
|
FT_Library FontFreeType::_FTlibrary;
|
||||||
|
bool FontFreeType::_FTInitialized = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool FontFreeType::initFreeType()
|
||||||
|
{
|
||||||
|
if (_FTInitialized == false)
|
||||||
|
{
|
||||||
|
// begin freetype
|
||||||
|
if (FT_Init_FreeType( &_FTlibrary ))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
_FTInitialized = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _FTInitialized;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontFreeType::shutdownFreeType()
|
||||||
|
{
|
||||||
|
if (_FTInitialized == true)
|
||||||
|
{
|
||||||
|
FT_Done_FreeType(_FTlibrary);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FT_Library FontFreeType::getFTLibrary()
|
||||||
|
{
|
||||||
|
initFreeType();
|
||||||
|
return _FTlibrary;
|
||||||
|
}
|
||||||
|
|
||||||
|
FontFreeType::FontFreeType() : _letterPadding(5)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
|
bool FontFreeType::createFontObject(const std::string &fontName, int fontSize)
|
||||||
{
|
{
|
||||||
/*
|
unsigned char* data = NULL;
|
||||||
CFStringRef theRefString = NULL;
|
int dpi = 72;
|
||||||
theRefString = CFStringCreateWithCString(kCFAllocatorDefault, fontName.c_str(), CFStringGetSystemEncoding());
|
|
||||||
NSString * fntName = [NSString stringWithUTF8String:fontName.c_str()];
|
|
||||||
|
|
||||||
// actually create iOS font (s)
|
int len = 0;
|
||||||
_fontRef = CTFontCreateWithName(theRefString, fontSize, NULL);
|
data = FileUtils::sharedFileUtils()->getFileData(fontName.c_str(), "rb", (unsigned long *)(&len) );
|
||||||
_fontUI = [UIFont fontWithName:fntName size:fontSize];
|
|
||||||
|
|
||||||
return ( (_fontRef != NULL) && (_fontUI != NULL) );
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
if (!data)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// create the new face
|
||||||
|
FT_Face face;
|
||||||
|
|
||||||
|
// create the face from the data
|
||||||
|
if ( FT_New_Memory_Face(getFTLibrary(), data, len, 0, &face ) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
//we want to use unicode
|
||||||
|
if( FT_Select_Charmap(face, FT_ENCODING_UNICODE) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// set the requested font size
|
||||||
|
int fontSizePoints = (int)(64.f * fontSize);
|
||||||
|
if( FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// store the face globally
|
||||||
|
_fontRef = face;
|
||||||
|
|
||||||
|
// done and good
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
FontFreeType::~FontFreeType()
|
FontFreeType::~FontFreeType()
|
||||||
|
@ -30,77 +87,73 @@ FontFreeType::~FontFreeType()
|
||||||
// TO DO
|
// TO DO
|
||||||
}
|
}
|
||||||
|
|
||||||
GlyphDef * FontFreeType::getGlyphsForText(const char *pText, int &outNumGlyphs)
|
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect)
|
||||||
{
|
{
|
||||||
/*
|
if (!_fontRef)
|
||||||
float CHAR_PADDING = 10.0f;
|
|
||||||
|
|
||||||
UniChar * characters;
|
|
||||||
CGGlyph * glyphs;
|
|
||||||
CFIndex count;
|
|
||||||
|
|
||||||
CFStringRef lettersString;
|
|
||||||
lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
|
||||||
|
|
||||||
if (NULL == lettersString)
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
count = CFStringGetLength(lettersString);
|
// get the ID to the char we need
|
||||||
|
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
|
||||||
|
|
||||||
// Allocate our buffers for characters and glyphs.
|
if (!glyph_index)
|
||||||
characters = new UniChar[count];
|
return false;
|
||||||
assert(characters != NULL);
|
|
||||||
|
|
||||||
glyphs = new CGGlyph[count];
|
// load glyph infos
|
||||||
assert(glyphs != NULL);
|
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
|
||||||
|
return false;
|
||||||
|
|
||||||
// Get the characters from the string.
|
// store result in the passed rectangle
|
||||||
CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters);
|
outRect.origin.x = 0;
|
||||||
|
outRect.origin.y = - (_fontRef->glyph->metrics.horiBearingY >> 6);
|
||||||
|
outRect.size.width = (_fontRef->glyph->metrics.width >> 6);
|
||||||
|
outRect.size.height = (_fontRef->glyph->metrics.height >> 6);
|
||||||
|
|
||||||
// Get the glyphs for the characters.
|
return true;
|
||||||
CTFontGetGlyphsForCharacters(_fontRef, characters, glyphs, count);
|
}
|
||||||
CGGlyph *theFirstGlyph = &glyphs[0];
|
|
||||||
|
|
||||||
// get letters bounding boxes
|
GlyphDef * FontFreeType::getGlyphsForText(const char *pText, int &outNumGlyphs)
|
||||||
CGRect *BBOx = new CGRect[count];
|
{
|
||||||
assert(BBOx != NULL);
|
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||||
|
|
||||||
|
//
|
||||||
|
if (!utf16String)
|
||||||
|
return 0;
|
||||||
|
|
||||||
CTFontGetBoundingRectsForGlyphs(_fontRef, kCTFontHorizontalOrientation, theFirstGlyph, BBOx, count);
|
int numChar = cc_wcslen(utf16String);
|
||||||
|
if (!numChar)
|
||||||
GlyphDef *pGlyphs = new GlyphDef[count];
|
return 0;
|
||||||
assert(pGlyphs != NULL);
|
|
||||||
|
|
||||||
|
// allocate the needed Glyphs
|
||||||
|
GlyphDef *pGlyphs = new GlyphDef[numChar];
|
||||||
|
assert( pGlyphs != NULL );
|
||||||
|
return 0;
|
||||||
|
|
||||||
// sore result as CCRect
|
// sore result as CCRect
|
||||||
for (int c=0; c<count; ++c)
|
for (int c=0; c<numChar; ++c)
|
||||||
{
|
{
|
||||||
Rect tempRect;
|
Rect tempRect;
|
||||||
tempRect.origin.x = BBOx[c].origin.x;
|
if( !getBBOXFotChar(utf16String[c], tempRect) )
|
||||||
tempRect.origin.y = BBOx[c].origin.y;
|
{
|
||||||
tempRect.size.width = BBOx[c].size.width;
|
delete [] pGlyphs;
|
||||||
tempRect.size.height = BBOx[c].size.height;
|
return 0;
|
||||||
|
|
||||||
pGlyphs[c].setRect(tempRect);
|
|
||||||
pGlyphs[c].setUTF8Letter(characters[c]);
|
|
||||||
pGlyphs[c].setPadding(CHAR_PADDING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// release memory
|
pGlyphs[c].setRect(tempRect);
|
||||||
delete [] characters;
|
pGlyphs[c].setUTF8Letter(utf16String[c]);
|
||||||
delete [] glyphs;
|
pGlyphs[c].setPadding(_letterPadding);
|
||||||
delete [] BBOx;
|
}
|
||||||
|
|
||||||
outNumGlyphs = count;
|
outNumGlyphs = numChar;
|
||||||
|
|
||||||
|
// free memory
|
||||||
|
delete [] utf16String;
|
||||||
|
|
||||||
|
// done
|
||||||
return pGlyphs;
|
return pGlyphs;
|
||||||
*/
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters)
|
Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
if (!pText)
|
if (!pText)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
@ -109,171 +162,174 @@ Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLe
|
||||||
if (!outNumLetters)
|
if (!outNumLetters)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// create the reference to the string
|
Size *pSizes = new Size[outNumLetters];
|
||||||
CFStringRef lettersString = CFStringCreateWithCharacters(kCFAllocatorDefault, pText, outNumLetters);
|
if (!pSizes)
|
||||||
|
|
||||||
if (NULL == lettersString)
|
|
||||||
{
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
for (int c = 0; c<outNumLetters; ++c)
|
||||||
|
{
|
||||||
|
int advance = 0;
|
||||||
|
int kerning = 0;
|
||||||
|
|
||||||
|
advance = getAdvanceFotChar(pText[c]);
|
||||||
|
|
||||||
|
if ( c < (outNumLetters-1) )
|
||||||
|
kerning = getHorizontalKerningForChars(pText[c], pText[c+1]);
|
||||||
|
|
||||||
|
pSizes[c].width = (advance + kerning);
|
||||||
}
|
}
|
||||||
|
|
||||||
UniChar *characters;
|
return pSizes;
|
||||||
CGGlyph *glyphs;
|
}
|
||||||
CFIndex count;
|
|
||||||
|
|
||||||
// num char
|
int FontFreeType::getAdvanceFotChar(unsigned short theChar)
|
||||||
count = CFStringGetLength(lettersString);
|
{
|
||||||
|
if (!_fontRef)
|
||||||
|
return false;
|
||||||
|
|
||||||
// Allocate our buffers for characters and glyphs.
|
// get the ID to the char we need
|
||||||
characters = new UniChar[count];
|
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
|
||||||
assert(characters != NULL);
|
|
||||||
|
|
||||||
glyphs = new CGGlyph[count];
|
if (!glyph_index)
|
||||||
assert(glyphs != NULL);
|
return false;
|
||||||
|
|
||||||
// Get the characters from the string.
|
// load glyph infos
|
||||||
CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters);
|
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
|
||||||
|
return false;
|
||||||
|
|
||||||
// Get the glyphs for the characters.
|
// get to the advance for this glyph
|
||||||
CTFontGetGlyphsForCharacters(_fontRef, characters, glyphs, count);
|
return (_fontRef->glyph->advance.x >> 6);
|
||||||
|
}
|
||||||
|
|
||||||
CGGlyph *theFirstGlyph = &glyphs[0];
|
int FontFreeType::getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar)
|
||||||
|
{
|
||||||
|
if (!_fontRef)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// get the ID to the char we need
|
||||||
|
int glyph_index1 = FT_Get_Char_Index(_fontRef, firstChar);
|
||||||
|
|
||||||
CGSize *pSize = new CGSize[count];
|
if (!glyph_index1)
|
||||||
if(!pSize)
|
return -1;
|
||||||
return 0;
|
|
||||||
|
|
||||||
Size *pCCSizes = new Size[count];
|
// get the ID to the char we need
|
||||||
if (!pCCSizes)
|
int glyph_index2 = FT_Get_Char_Index(_fontRef, secondChar);
|
||||||
return 0;
|
|
||||||
|
|
||||||
// actually get the advances
|
if (!glyph_index2)
|
||||||
CTFontGetAdvancesForGlyphs(_fontRef, kCTFontHorizontalOrientation, theFirstGlyph, pSize, count);
|
return -1;
|
||||||
|
|
||||||
for (int c = 0; c<count; ++c)
|
FT_Vector kerning;
|
||||||
{
|
|
||||||
pCCSizes[c].width = pSize[c].width;
|
|
||||||
pCCSizes[c].height = pSize[c].height;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete [] characters;
|
if (FT_Get_Kerning( _fontRef, glyph_index1, glyph_index2, FT_KERNING_DEFAULT, &kerning ))
|
||||||
delete [] glyphs;
|
return -1;
|
||||||
delete [] pSize;
|
|
||||||
|
|
||||||
outNumLetters = count;
|
return ( kerning.x >> 6 );
|
||||||
return pCCSizes;
|
|
||||||
*/
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Size * FontFreeType::getAdvancesForText(const char *pText, int &outNumLetters)
|
Size * FontFreeType::getAdvancesForText(const char *pText, int &outNumLetters)
|
||||||
{
|
{
|
||||||
/*
|
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||||
unsigned short int *utf8Text = FontIOS::getUTF8Text(pText, outNumLetters);
|
Size *ret = getAdvancesForTextUTF8(utf16String, outNumLetters);
|
||||||
if (utf8Text)
|
delete [] utf16String;
|
||||||
{
|
|
||||||
Size *ret = getAdvancesForTextUTF8(utf8Text, outNumLetters);
|
|
||||||
delete [] utf8Text;
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Size FontFreeType::getTextWidthAndHeight(const char *pText)
|
Size FontFreeType::getTextWidthAndHeight(const char *pText)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
Size retSize;
|
Size retSize;
|
||||||
NSString * str = [NSString stringWithUTF8String:pText];
|
retSize.width = 0;
|
||||||
CGSize tmp = [str sizeWithFont:(UIFont *)_fontUI];
|
retSize.height = 0;
|
||||||
|
|
||||||
retSize.width = tmp.width;
|
int numLetters;
|
||||||
retSize.height = tmp.height;
|
Size *tempSizes = getAdvancesForText(pText, numLetters);
|
||||||
|
|
||||||
|
for (int c = 0; c<numLetters; ++c)
|
||||||
|
{
|
||||||
|
retSize.width += tempSizes[c].width;
|
||||||
|
}
|
||||||
|
|
||||||
|
retSize.height = (_fontRef->size->metrics.height >> 6);
|
||||||
|
|
||||||
|
delete [] tempSizes;
|
||||||
|
|
||||||
return retSize;
|
return retSize;
|
||||||
*/
|
}
|
||||||
|
|
||||||
Size retSize;
|
unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight)
|
||||||
return retSize;
|
{
|
||||||
|
if (!_fontRef)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// get the ID to the char we need
|
||||||
|
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
|
||||||
|
|
||||||
|
if (!glyph_index)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// load glyph infos
|
||||||
|
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
if (FT_Render_Glyph( _fontRef->glyph, FT_RENDER_MODE_NORMAL ))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
outWidth = _fontRef->glyph->bitmap.width;
|
||||||
|
outHeight = _fontRef->glyph->bitmap.rows;
|
||||||
|
|
||||||
|
// return the pointer to the bitmap
|
||||||
|
return _fontRef->glyph->bitmap.buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned short int * FontFreeType::getUTF8Text(const char *pText, int &outNumLetters)
|
unsigned short int * FontFreeType::getUTF8Text(const char *pText, int &outNumLetters)
|
||||||
{
|
{
|
||||||
/*
|
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||||
CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
if(!utf16String)
|
||||||
if (NULL == lettersString)
|
|
||||||
{
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// num char
|
|
||||||
int count = CFStringGetLength(lettersString);
|
|
||||||
|
|
||||||
// Allocate our buffers for characters and glyphs.
|
|
||||||
UniChar *characters = new UniChar[count + 1];
|
|
||||||
if (!characters)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Get the characters from the string.
|
|
||||||
CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters);
|
|
||||||
|
|
||||||
// terminate the string
|
|
||||||
outNumLetters = count;
|
|
||||||
characters[count] = 0;
|
|
||||||
|
|
||||||
return (unsigned short int *) characters;
|
|
||||||
*/
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
outNumLetters = cc_wcslen(utf16String);
|
||||||
|
return utf16String;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// carloX this could be broken
|
||||||
const char * FontFreeType::trimUTF8Text(const char *pText, int newBegin, int newEnd)
|
const char * FontFreeType::trimUTF8Text(const char *pText, int newBegin, int newEnd)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
if ( newBegin<0 || newEnd<=0 )
|
if ( newBegin<0 || newEnd<=0 )
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
if ( newBegin>=newEnd )
|
if ( newBegin>=newEnd )
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
NSString * str = [NSString stringWithUTF8String:pText];
|
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||||
if ( newEnd >= [str length])
|
if (!utf16String)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
NSRange theRange;
|
if (newEnd >= cc_wcslen(utf16String))
|
||||||
|
|
||||||
theRange.location = newBegin;
|
|
||||||
theRange.length = (newEnd - newBegin) +1;
|
|
||||||
|
|
||||||
// trim the string
|
|
||||||
NSString *trimmedString = [str substringWithRange:theRange];
|
|
||||||
|
|
||||||
// ret the string
|
|
||||||
return [trimmedString UTF8String];
|
|
||||||
*/
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
int newLenght = newEnd - newBegin + 2;
|
||||||
|
unsigned short* trimmedString = new unsigned short[newLenght];
|
||||||
|
|
||||||
|
for(int c = 0; c < (newLenght-1); ++c)
|
||||||
|
{
|
||||||
|
trimmedString[c] = utf16String[newBegin + c];
|
||||||
|
}
|
||||||
|
|
||||||
|
// last char
|
||||||
|
trimmedString[newLenght-1] = 0x0000;
|
||||||
|
|
||||||
|
// release temp
|
||||||
|
delete [] utf16String;
|
||||||
|
|
||||||
|
return (const char *)trimmedString;
|
||||||
}
|
}
|
||||||
|
|
||||||
int FontFreeType::getUTF8TextLenght(const char *pText)
|
int FontFreeType::getUTF8TextLenght(const char *pText)
|
||||||
{
|
{
|
||||||
/*
|
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||||
CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
if (!utf16String)
|
||||||
if (NULL == lettersString)
|
return -1;
|
||||||
{
|
int outNumLetters = cc_wcslen(utf16String);
|
||||||
return 0;
|
delete [] utf16String;
|
||||||
}
|
return outNumLetters;
|
||||||
|
|
||||||
return CFStringGetLength(lettersString);
|
|
||||||
*/
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
|
@ -22,8 +22,10 @@ class FontFreeType : public Font
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
FontFreeType();
|
||||||
virtual ~FontFreeType();
|
virtual ~FontFreeType();
|
||||||
virtual bool createFontObject(const std::string &fontName, int fontSize);
|
virtual bool createFontObject(const std::string &fontName, int fontSize);
|
||||||
|
|
||||||
virtual int getUTF8TextLenght(const char *pText);
|
virtual int getUTF8TextLenght(const char *pText);
|
||||||
virtual Size getTextWidthAndHeight(const char *pText);
|
virtual Size getTextWidthAndHeight(const char *pText);
|
||||||
virtual GlyphDef * getGlyphsForText(const char *pText, int &outNumGlyphs);
|
virtual GlyphDef * getGlyphsForText(const char *pText, int &outNumGlyphs);
|
||||||
|
@ -31,11 +33,24 @@ public:
|
||||||
virtual Size * getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters);
|
virtual Size * getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters);
|
||||||
virtual unsigned short int * getUTF8Text(const char *pText, int &outNumLetters);
|
virtual unsigned short int * getUTF8Text(const char *pText, int &outNumLetters);
|
||||||
virtual const char * trimUTF8Text(const char *pText, int newBegin, int newEnd);
|
virtual const char * trimUTF8Text(const char *pText, int newBegin, int newEnd);
|
||||||
|
virtual int getLetterPadding() { return _letterPadding;}
|
||||||
|
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
//CTFontRef _fontRef;
|
bool initFreeType();
|
||||||
//void * _fontUI;
|
void shutdownFreeType();
|
||||||
|
FT_Library getFTLibrary();
|
||||||
|
bool getBBOXFotChar(unsigned short theChar, Rect &outRect);
|
||||||
|
int getAdvanceFotChar(unsigned short theChar);
|
||||||
|
int getHorizontalKerningForChars(unsigned short firstChar, unsigned short secondChar);
|
||||||
|
|
||||||
|
static FT_Library _FTlibrary;
|
||||||
|
static bool _FTInitialized;
|
||||||
|
FT_Face _fontRef;
|
||||||
|
const int _letterPadding;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -12,14 +12,23 @@
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
// FWD
|
||||||
|
class Font;
|
||||||
|
|
||||||
class TextPageDef;
|
class TextPageDef;
|
||||||
|
|
||||||
class FontRender
|
class FontRender
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
FontRender(Font *pFont) { _font = pFont; }
|
||||||
virtual ~FontRender() {}
|
virtual ~FontRender() {}
|
||||||
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize) = 0;
|
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize) = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
Font * _font;
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -10,28 +10,29 @@
|
||||||
#include "CCTextImage.h"
|
#include "CCTextImage.h"
|
||||||
#include "CCFontRenderFreeType.h"
|
#include "CCFontRenderFreeType.h"
|
||||||
|
|
||||||
|
|
||||||
#include "CCImage.h"
|
#include "CCImage.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#include "ft2build.h"
|
#include "ft2build.h"
|
||||||
#include "CCStdC.h"
|
|
||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
FT_Library library;
|
FT_Library library;
|
||||||
|
FT_Face currentFace;
|
||||||
|
|
||||||
|
|
||||||
// globals
|
// globals
|
||||||
|
|
||||||
bool renderCharAt(FT_GlyphSlot theGlyph, unsigned char *destMemory, int posX, int posY, int destSize)
|
bool renderCharAtStatic(FT_GlyphSlot theGlyph, unsigned char *destMemory, int posX, int posY, int destSize)
|
||||||
{
|
{
|
||||||
// render the glyph
|
// render the glyph
|
||||||
if (FT_Render_Glyph( theGlyph, FT_RENDER_MODE_NORMAL ))
|
if (FT_Render_Glyph( theGlyph, FT_RENDER_MODE_NORMAL ))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
// carloX posY -= theGlyph->bitmap_top;
|
||||||
|
// carloX int testbearing = theGlyph->bitmap_top;
|
||||||
|
|
||||||
// copy the gliph into the bitmap
|
// copy the gliph into the bitmap
|
||||||
FT_Bitmap& bitmap = theGlyph->bitmap;
|
FT_Bitmap& bitmap = theGlyph->bitmap;
|
||||||
|
|
||||||
|
@ -61,40 +62,105 @@ bool renderCharAt(FT_GlyphSlot theGlyph, unsigned char *destMemory, int posX, in
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool FontRenderFreeType::renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize)
|
||||||
void FontRenderFreeType::testRendering()
|
|
||||||
{
|
{
|
||||||
|
if (!_font)
|
||||||
|
return false;
|
||||||
|
|
||||||
const char *stringToRender = "abcdefghilmnopqrstuvz";
|
unsigned char *sourceBitmap = 0;
|
||||||
|
int sourceWidth = 0;
|
||||||
|
int sourceHeight = 0;
|
||||||
|
|
||||||
// font info
|
// get the glyph's bitmap
|
||||||
int dpi = 72;
|
sourceBitmap = _font->getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
|
||||||
int fontSize = 30;
|
|
||||||
|
|
||||||
// error
|
if(!sourceBitmap)
|
||||||
int libError;
|
return false;
|
||||||
|
|
||||||
|
int iX = posX;
|
||||||
|
int iY = posY;
|
||||||
|
|
||||||
|
for (int y = 0; y < sourceHeight; ++y)
|
||||||
|
{
|
||||||
|
int bitmap_y = y * sourceWidth;
|
||||||
|
|
||||||
|
for (int x = 0; x < sourceWidth; ++x)
|
||||||
|
{
|
||||||
|
unsigned char cTemp = sourceBitmap[bitmap_y + x];
|
||||||
|
|
||||||
|
// the final pixel
|
||||||
|
int iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
|
||||||
|
*(int*) &destMemory[(iX + ( iY * destSize ) ) * 4] = iTemp;
|
||||||
|
|
||||||
|
iX += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
iX = posX;
|
||||||
|
iY += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
//everything good
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontRenderFreeType::InitFreeType()
|
||||||
|
{
|
||||||
// begin freetype
|
// begin freetype
|
||||||
libError = FT_Init_FreeType( &library );
|
if (FT_Init_FreeType( &library ))
|
||||||
|
return false;
|
||||||
|
else
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool FontRenderFreeType::CreateFreeTypeFont(const char *fontName, int fontSize, int dpi)
|
||||||
|
{
|
||||||
unsigned char* data = NULL;
|
unsigned char* data = NULL;
|
||||||
|
|
||||||
int len = 0;
|
int len = 0;
|
||||||
data = FileUtils::sharedFileUtils()->getFileData("fonts/Thonburi.ttf", "rb", (unsigned long *)(&len) );
|
data = FileUtils::sharedFileUtils()->getFileData(fontName, "rb", (unsigned long *)(&len) );
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
// create the new face
|
// create the new face
|
||||||
FT_Face face;
|
FT_Face face;
|
||||||
|
|
||||||
// create the face from the data
|
// create the face from the data
|
||||||
if (FT_New_Memory_Face(library, data, len, 0, &face) )
|
if (FT_New_Memory_Face(library, data, len, 0, &face) )
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
// set the requested font size
|
// set the requested font size
|
||||||
int fontSizePoints = (int)(64.f * fontSize);
|
int fontSizePoints = (int)(64.f * fontSize);
|
||||||
if( FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi) )
|
if( FT_Set_Char_Size(face, fontSizePoints, fontSizePoints, dpi, dpi) )
|
||||||
return;
|
return false;
|
||||||
|
|
||||||
|
// store the face globally
|
||||||
|
currentFace = face;
|
||||||
|
|
||||||
|
// done and good
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FontRenderFreeType::ReleaseFreeType()
|
||||||
|
{
|
||||||
|
// end freetype
|
||||||
|
FT_Done_FreeType(library);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void FontRenderFreeType::testRendering()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
const char *stringToRender = "abcdefghilmnopqrstuvz";
|
||||||
|
|
||||||
|
// init freetype
|
||||||
|
InitFreeType();
|
||||||
|
|
||||||
|
|
||||||
|
// init and create a font
|
||||||
|
CreateFreeTypeFont("fonts/Thonburi.ttf", 100, 72);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// allocate memory for the bitmap
|
// allocate memory for the bitmap
|
||||||
int bitmapSize = 512;
|
int bitmapSize = 512;
|
||||||
|
@ -102,21 +168,20 @@ void FontRenderFreeType::testRendering()
|
||||||
if(!pBitmap) return;
|
if(!pBitmap) return;
|
||||||
memset(pBitmap, 0, bitmapSize * bitmapSize * 4);
|
memset(pBitmap, 0, bitmapSize * bitmapSize * 4);
|
||||||
|
|
||||||
|
// carloX: has to be changed
|
||||||
|
FT_Face face = currentFace;
|
||||||
|
|
||||||
|
// line height
|
||||||
|
int lineHeight = face->size->metrics.height>>6;
|
||||||
|
|
||||||
|
|
||||||
// padding between letters
|
// padding between letters
|
||||||
int paddingX = 5;
|
int paddingX = 5;
|
||||||
int paddingY = 10;
|
int paddingY = 0;
|
||||||
|
|
||||||
|
// where we need to render first
|
||||||
int renderPosX = paddingX;
|
int renderPosX = paddingX;
|
||||||
int renderPosY = paddingY;
|
// carloX int renderPosY = lineHeight + paddingY;
|
||||||
|
int renderPosY = 0;
|
||||||
// this must be dynamic
|
|
||||||
int letterWidth = 30;
|
|
||||||
int letterHeight = 30;
|
|
||||||
|
|
||||||
int stringLength = strlen(stringToRender);
|
int stringLength = strlen(stringToRender);
|
||||||
|
|
||||||
|
@ -129,113 +194,51 @@ void FontRenderFreeType::testRendering()
|
||||||
if (FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT))
|
if (FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// get witdh and height for the next character
|
|
||||||
|
|
||||||
// check for bounds
|
// check for bounds
|
||||||
if ( (renderPosX +letterWidth + paddingX) >= bitmapSize )
|
|
||||||
|
// test carloX
|
||||||
|
int advancePixel = face->glyph->advance.x >> 6;
|
||||||
|
int widthInPixel = face->glyph->metrics.width >> 6;
|
||||||
|
int heightInPixel = face->glyph->metrics.height >> 6;
|
||||||
|
int ascent = face->glyph->metrics.horiBearingY>>6;
|
||||||
|
// end test carloX
|
||||||
|
|
||||||
|
if ( (renderPosX + (face->glyph->advance.x >> 6) + paddingX) >= bitmapSize )
|
||||||
{
|
{
|
||||||
renderPosX = paddingX;
|
renderPosX = paddingX;
|
||||||
renderPosY += (letterHeight + paddingY);
|
renderPosY += (lineHeight + paddingY);
|
||||||
}
|
}
|
||||||
|
|
||||||
renderCharAt(face->glyph, pBitmap, renderPosX, renderPosY, bitmapSize);
|
// render in bitmap
|
||||||
|
renderCharAtStatic(face->glyph, pBitmap, renderPosX, renderPosY, bitmapSize);
|
||||||
|
|
||||||
// move
|
// move pen
|
||||||
renderPosX += letterWidth + paddingX;
|
// carloX renderPosX += ((face->glyph->advance.x >> 6) + paddingX);
|
||||||
|
renderPosX += (widthInPixel + paddingX);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
|
|
||||||
// allocate the bitmap
|
|
||||||
int bitmapSize = 512;
|
|
||||||
unsigned char *pBitmap = new unsigned char[bitmapSize * bitmapSize * 4];
|
|
||||||
memset(pBitmap, 0, bitmapSize * bitmapSize * 4);
|
|
||||||
|
|
||||||
renderCharAt(face->glyph, pBitmap, 10, 10, 512);
|
|
||||||
renderCharAt(face->glyph, pBitmap, 200, 200, 512);
|
|
||||||
renderCharAt(face->glyph, pBitmap, 300, 300, 512);
|
|
||||||
*/
|
|
||||||
/*
|
|
||||||
|
|
||||||
// render the glyph
|
|
||||||
if (FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL ))
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// copy the gliph into the bitmap
|
|
||||||
FT_Bitmap& bitmap = face->glyph->bitmap;
|
|
||||||
|
|
||||||
// allocate the bitmap
|
|
||||||
int bitmapSize = 512;
|
|
||||||
unsigned char *pBitmap = new unsigned char[bitmapSize * bitmapSize * 4];
|
|
||||||
memset(pBitmap, 0, bitmapSize * bitmapSize * 4);
|
|
||||||
|
|
||||||
|
|
||||||
if (!pBitmap)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int destX = 10;
|
|
||||||
int destY = 10;
|
|
||||||
|
|
||||||
int iX = destX;
|
|
||||||
int iY = destY;
|
|
||||||
|
|
||||||
|
|
||||||
for (int y = 0; y < bitmap.rows; ++y)
|
|
||||||
{
|
|
||||||
int bitmap_y = y * bitmap.width;
|
|
||||||
|
|
||||||
for (int x = 0; x < bitmap.width; ++x)
|
|
||||||
{
|
|
||||||
unsigned char cTemp = bitmap.buffer[bitmap_y + x];
|
|
||||||
|
|
||||||
//if (cTemp == 0)
|
|
||||||
//{
|
|
||||||
// continue;
|
|
||||||
//}
|
|
||||||
|
|
||||||
// the final pixel
|
|
||||||
int iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
|
|
||||||
*(int*) &pBitmap[(iX + ( iY * bitmapSize ) ) * 4] = iTemp;
|
|
||||||
|
|
||||||
iX += 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
iX = destX;
|
|
||||||
iY += 1;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// save the bitmap into a file
|
// save the bitmap into a file
|
||||||
Image *pImage = new Image;
|
Image *pImage = new Image;
|
||||||
pImage->initWithRawData(pBitmap, (bitmapSize * bitmapSize * 4), 512, 512, 8, false);
|
pImage->initWithRawData(pBitmap, (bitmapSize * bitmapSize * 4), 512, 512, 8, false);
|
||||||
pImage->saveToFile("carlottone");
|
pImage->saveToFile("carlottone");
|
||||||
|
|
||||||
|
|
||||||
// release the bitmap
|
// release the bitmap
|
||||||
delete [] pBitmap;
|
delete [] pBitmap;
|
||||||
|
|
||||||
// end freetype
|
// end freetype
|
||||||
FT_Done_FreeType(library);
|
ReleaseFreeType();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void FontRenderFreeType::renderCharToBitmap(char *pDestBitmap, char charToRender, int posX, int posY)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize)
|
unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize)
|
||||||
{
|
{
|
||||||
|
|
||||||
// constants
|
|
||||||
float LINE_PADDING = 1.9;
|
|
||||||
|
|
||||||
if (!thePage)
|
if (!thePage)
|
||||||
return NULL;
|
return 0;
|
||||||
|
|
||||||
|
if (_font)
|
||||||
|
return 0;
|
||||||
|
|
||||||
if (thePage->getNumLines() == 0)
|
if (thePage->getNumLines() == 0)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -246,16 +249,19 @@ unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, c
|
||||||
// prepare memory and clean to 0
|
// prepare memory and clean to 0
|
||||||
int sizeInBytes = (pageWidth * pageHeight * 4);
|
int sizeInBytes = (pageWidth * pageHeight * 4);
|
||||||
unsigned char* data = new unsigned char[sizeInBytes];
|
unsigned char* data = new unsigned char[sizeInBytes];
|
||||||
|
|
||||||
|
if (!data)
|
||||||
|
return 0;
|
||||||
|
|
||||||
memset(data, 0, sizeInBytes);
|
memset(data, 0, sizeInBytes);
|
||||||
|
|
||||||
|
|
||||||
int numLines = thePage->getNumLines();
|
int numLines = thePage->getNumLines();
|
||||||
|
|
||||||
for (int c = 0; c<numLines; ++c)
|
for (int c = 0; c<numLines; ++c)
|
||||||
{
|
{
|
||||||
TextLineDef *pCurrentLine = thePage->getLineAt(c);
|
TextLineDef *pCurrentLine = thePage->getLineAt(c);
|
||||||
float lineHeight = pCurrentLine->getHeight();
|
|
||||||
|
|
||||||
float origX = LINE_PADDING;
|
float origX = _font->getLetterPadding();
|
||||||
float origY = pCurrentLine->getY();
|
float origY = pCurrentLine->getY();
|
||||||
|
|
||||||
int numGlyphToRender = pCurrentLine->getNumGlyph();
|
int numGlyphToRender = pCurrentLine->getNumGlyph();
|
||||||
|
@ -263,91 +269,31 @@ unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, c
|
||||||
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
|
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
|
||||||
{
|
{
|
||||||
GlyphDef currGlyph = pCurrentLine->getGlyphAt(cglyph);
|
GlyphDef currGlyph = pCurrentLine->getGlyphAt(cglyph);
|
||||||
|
renderCharAt(currGlyph.getUTF8Letter(), origX, origY, data, pageWidth);
|
||||||
//NSString *lineString = [NSString stringWithFormat: @"%C", currGlyph.getUTF8Letter()];
|
origX += (currGlyph.getRect().size.width + _font->getLetterPadding());
|
||||||
//CGRect tempRect;
|
|
||||||
Rect tempRect;
|
|
||||||
|
|
||||||
tempRect.origin.x = (origX - currGlyph.getRect().origin.x);
|
|
||||||
tempRect.origin.y = origY;
|
|
||||||
tempRect.size.width = currGlyph.getRect().size.width;
|
|
||||||
tempRect.size.height = lineHeight;
|
|
||||||
|
|
||||||
// actually draw one character
|
|
||||||
//[lineString drawInRect: tempRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:align];
|
|
||||||
|
|
||||||
// move to next character
|
|
||||||
origX += (tempRect.size.width + currGlyph.getPadding());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool debug = true;
|
||||||
|
if (debug)
|
||||||
|
{
|
||||||
|
static int counter = 0;
|
||||||
|
|
||||||
|
char outFilename[512];
|
||||||
|
sprintf(outFilename,"carlottone%d", counter);
|
||||||
|
++counter;
|
||||||
|
|
||||||
|
// save the bitmap into a file
|
||||||
|
Image *pImage = new Image;
|
||||||
|
pImage->initWithRawData(data, (pageWidth * pageWidth * 4), 512, 512, 8, false);
|
||||||
|
pImage->saveToFile(outFilename);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// we are done here
|
// we are done here
|
||||||
return data;
|
return data;
|
||||||
|
|
||||||
/*
|
|
||||||
// prepare the context
|
|
||||||
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
||||||
CGContextRef context = CGBitmapContextCreate(data, pageWidth, pageHeight, 8, pageWidth * 4, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
|
|
||||||
CGColorSpaceRelease(colorSpace);
|
|
||||||
|
|
||||||
if (!context)
|
|
||||||
{
|
|
||||||
delete[] data;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// prepare the context
|
|
||||||
CGContextSetRGBFillColor(context, 1, 1, 1, 1);
|
|
||||||
CGContextTranslateCTM(context, 0.0f, pageHeight);
|
|
||||||
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
|
|
||||||
|
|
||||||
|
|
||||||
UIGraphicsPushContext(context);
|
|
||||||
UITextAlignment align = NSTextAlignmentLeft;
|
|
||||||
|
|
||||||
// create the font
|
|
||||||
NSString *nsFontName = [NSString stringWithUTF8String:fontName];
|
|
||||||
id font = [UIFont fontWithName:nsFontName size:fontSize];
|
|
||||||
|
|
||||||
int numLines = thePage->getNumLines();
|
|
||||||
for (int c = 0; c<numLines; ++c)
|
|
||||||
{
|
|
||||||
TextLineDef *pCurrentLine = thePage->getLineAt(c);
|
|
||||||
float lineHeight = pCurrentLine->getHeight();
|
|
||||||
|
|
||||||
float origX = LINE_PADDING;
|
|
||||||
float origY = pCurrentLine->getY();
|
|
||||||
|
|
||||||
int numGlyphToRender = pCurrentLine->getNumGlyph();
|
|
||||||
|
|
||||||
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
|
|
||||||
{
|
|
||||||
GlyphDef currGlyph = pCurrentLine->getGlyphAt(cglyph);
|
|
||||||
|
|
||||||
NSString *lineString = [NSString stringWithFormat: @"%C", currGlyph.getUTF8Letter()];
|
|
||||||
CGRect tempRect;
|
|
||||||
|
|
||||||
tempRect.origin.x = (origX - currGlyph.getRect().origin.x);
|
|
||||||
tempRect.origin.y = origY;
|
|
||||||
tempRect.size.width = currGlyph.getRect().size.width;
|
|
||||||
tempRect.size.height = lineHeight;
|
|
||||||
|
|
||||||
// actually draw one character
|
|
||||||
[lineString drawInRect: tempRect withFont:font lineBreakMode:NSLineBreakByWordWrapping alignment:align];
|
|
||||||
|
|
||||||
// move to next character
|
|
||||||
origX += (tempRect.size.width + currGlyph.getPadding());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// clean everything
|
|
||||||
UIGraphicsPopContext();
|
|
||||||
CGContextRelease(context);
|
|
||||||
|
|
||||||
// everything looks good
|
|
||||||
return data;
|
|
||||||
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@
|
||||||
|
|
||||||
#include "CCFontRender.h"
|
#include "CCFontRender.h"
|
||||||
|
|
||||||
|
// this should be here #include "ft2build.h"
|
||||||
|
// this should bg here #include FT_FREETYPE_H
|
||||||
|
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
@ -19,12 +21,23 @@ class FontRenderFreeType : public FontRender
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
FontRenderFreeType(Font *pFont): FontRender(pFont) {}
|
||||||
virtual ~FontRenderFreeType() {}
|
virtual ~FontRenderFreeType() {}
|
||||||
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize);
|
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize);
|
||||||
static void testRendering();
|
|
||||||
static void renderCharToBitmap(char *pDestBitmap, char charToRender, int posX, int posY);
|
|
||||||
|
|
||||||
|
|
||||||
|
// only a test, needs to go
|
||||||
|
static void testRendering();
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
bool renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize);
|
||||||
|
|
||||||
|
static bool InitFreeType();
|
||||||
|
static bool CreateFreeTypeFont(const char *fontName, int fontSize, int dpi);
|
||||||
|
static void ReleaseFreeType();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
NS_CC_END
|
NS_CC_END
|
||||||
|
|
|
@ -13,10 +13,14 @@
|
||||||
|
|
||||||
NS_CC_BEGIN
|
NS_CC_BEGIN
|
||||||
|
|
||||||
|
class Font;
|
||||||
|
|
||||||
class FontRenderIOS : public FontRender
|
class FontRenderIOS : public FontRender
|
||||||
{
|
{
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
FontRenderIOS(Font *pFont): FontRender(pFont) {}
|
||||||
virtual ~FontRenderIOS() {}
|
virtual ~FontRenderIOS() {}
|
||||||
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize);
|
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize);
|
||||||
|
|
||||||
|
|
|
@ -284,6 +284,14 @@ bool TextImage::initWithString(const char * pText, int nWidth, int nHeight, cons
|
||||||
|
|
||||||
bool TextImage::createFontRef(const char *fontName, int fontSize)
|
bool TextImage::createFontRef(const char *fontName, int fontSize)
|
||||||
{
|
{
|
||||||
|
//carloX new stuff
|
||||||
|
//_font
|
||||||
|
|
||||||
|
|
||||||
|
// end carloX new stuff
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (_font)
|
if (_font)
|
||||||
{
|
{
|
||||||
delete _font;
|
delete _font;
|
||||||
|
@ -308,7 +316,7 @@ bool TextImage::createFontRender()
|
||||||
}
|
}
|
||||||
|
|
||||||
// carloX
|
// carloX
|
||||||
_fontRender = new FontRenderIOS();
|
_fontRender = new FontRenderIOS(_font);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -184,6 +184,12 @@ private:
|
||||||
Font * _font;
|
Font * _font;
|
||||||
FontRender * _fontRender;
|
FontRender * _fontRender;
|
||||||
|
|
||||||
|
|
||||||
|
// carloX new stuff
|
||||||
|
Font * _fontNew;
|
||||||
|
FontRender * _fontRenderNew;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
2836b978437c1bddf24c7c2cb39ac27d17eb6775
|
a6b1fd0e7c62ab8e5372202bdb349c4ff7ffbe94
|
Loading…
Reference in New Issue