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 Size getTextWidthAndHeight(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
|
||||
|
|
|
@ -4,24 +4,81 @@
|
|||
#include "cocos2d.h"
|
||||
#include "CCFontFreeType.h"
|
||||
#include "CCTextImage.h"
|
||||
#include "ccUTF8.h"
|
||||
|
||||
|
||||
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)
|
||||
{
|
||||
/*
|
||||
CFStringRef theRefString = NULL;
|
||||
theRefString = CFStringCreateWithCString(kCFAllocatorDefault, fontName.c_str(), CFStringGetSystemEncoding());
|
||||
NSString * fntName = [NSString stringWithUTF8String:fontName.c_str()];
|
||||
unsigned char* data = NULL;
|
||||
int dpi = 72;
|
||||
|
||||
// actually create iOS font (s)
|
||||
_fontRef = CTFontCreateWithName(theRefString, fontSize, NULL);
|
||||
_fontUI = [UIFont fontWithName:fntName size:fontSize];
|
||||
|
||||
return ( (_fontRef != NULL) && (_fontUI != NULL) );
|
||||
*/
|
||||
int len = 0;
|
||||
data = FileUtils::sharedFileUtils()->getFileData(fontName.c_str(), "rb", (unsigned long *)(&len) );
|
||||
|
||||
if (!data)
|
||||
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()
|
||||
|
@ -30,77 +87,73 @@ FontFreeType::~FontFreeType()
|
|||
// TO DO
|
||||
}
|
||||
|
||||
GlyphDef * FontFreeType::getGlyphsForText(const char *pText, int &outNumGlyphs)
|
||||
bool FontFreeType::getBBOXFotChar(unsigned short theChar, Rect &outRect)
|
||||
{
|
||||
/*
|
||||
float CHAR_PADDING = 10.0f;
|
||||
|
||||
UniChar * characters;
|
||||
CGGlyph * glyphs;
|
||||
CFIndex count;
|
||||
|
||||
CFStringRef lettersString;
|
||||
lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
||||
|
||||
if (NULL == lettersString)
|
||||
if (!_fontRef)
|
||||
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.
|
||||
characters = new UniChar[count];
|
||||
assert(characters != NULL);
|
||||
if (!glyph_index)
|
||||
return false;
|
||||
|
||||
glyphs = new CGGlyph[count];
|
||||
assert(glyphs != NULL);
|
||||
// load glyph infos
|
||||
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
|
||||
return false;
|
||||
|
||||
// Get the characters from the string.
|
||||
CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters);
|
||||
// store result in the passed rectangle
|
||||
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.
|
||||
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<count; ++c)
|
||||
{
|
||||
Rect tempRect;
|
||||
tempRect.origin.x = BBOx[c].origin.x;
|
||||
tempRect.origin.y = BBOx[c].origin.y;
|
||||
tempRect.size.width = BBOx[c].size.width;
|
||||
tempRect.size.height = BBOx[c].size.height;
|
||||
|
||||
pGlyphs[c].setRect(tempRect);
|
||||
pGlyphs[c].setUTF8Letter(characters[c]);
|
||||
pGlyphs[c].setPadding(CHAR_PADDING);
|
||||
return true;
|
||||
}
|
||||
|
||||
// release memory
|
||||
delete [] characters;
|
||||
delete [] glyphs;
|
||||
delete [] BBOx;
|
||||
|
||||
outNumGlyphs = count;
|
||||
return pGlyphs;
|
||||
*/
|
||||
GlyphDef * FontFreeType::getGlyphsForText(const char *pText, int &outNumGlyphs)
|
||||
{
|
||||
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||
|
||||
//
|
||||
if (!utf16String)
|
||||
return 0;
|
||||
|
||||
int numChar = cc_wcslen(utf16String);
|
||||
if (!numChar)
|
||||
return 0;
|
||||
|
||||
// allocate the needed Glyphs
|
||||
GlyphDef *pGlyphs = new GlyphDef[numChar];
|
||||
assert( pGlyphs != NULL );
|
||||
return 0;
|
||||
|
||||
// sore result as CCRect
|
||||
for (int c=0; c<numChar; ++c)
|
||||
{
|
||||
Rect tempRect;
|
||||
if( !getBBOXFotChar(utf16String[c], tempRect) )
|
||||
{
|
||||
delete [] pGlyphs;
|
||||
return 0;
|
||||
}
|
||||
|
||||
pGlyphs[c].setRect(tempRect);
|
||||
pGlyphs[c].setUTF8Letter(utf16String[c]);
|
||||
pGlyphs[c].setPadding(_letterPadding);
|
||||
}
|
||||
|
||||
outNumGlyphs = numChar;
|
||||
|
||||
// free memory
|
||||
delete [] utf16String;
|
||||
|
||||
// done
|
||||
return pGlyphs;
|
||||
}
|
||||
|
||||
Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters)
|
||||
{
|
||||
/*
|
||||
if (!pText)
|
||||
return 0;
|
||||
|
||||
|
@ -109,171 +162,174 @@ Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLe
|
|||
if (!outNumLetters)
|
||||
return 0;
|
||||
|
||||
// create the reference to the string
|
||||
CFStringRef lettersString = CFStringCreateWithCharacters(kCFAllocatorDefault, pText, outNumLetters);
|
||||
|
||||
if (NULL == lettersString)
|
||||
{
|
||||
Size *pSizes = new Size[outNumLetters];
|
||||
if (!pSizes)
|
||||
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;
|
||||
CGGlyph *glyphs;
|
||||
CFIndex count;
|
||||
|
||||
// num char
|
||||
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];
|
||||
|
||||
|
||||
CGSize *pSize = new CGSize[count];
|
||||
if(!pSize)
|
||||
return 0;
|
||||
|
||||
Size *pCCSizes = new Size[count];
|
||||
if (!pCCSizes)
|
||||
return 0;
|
||||
|
||||
// actually get the advances
|
||||
CTFontGetAdvancesForGlyphs(_fontRef, kCTFontHorizontalOrientation, theFirstGlyph, pSize, count);
|
||||
|
||||
for (int c = 0; c<count; ++c)
|
||||
{
|
||||
pCCSizes[c].width = pSize[c].width;
|
||||
pCCSizes[c].height = pSize[c].height;
|
||||
return pSizes;
|
||||
}
|
||||
|
||||
delete [] characters;
|
||||
delete [] glyphs;
|
||||
delete [] pSize;
|
||||
int FontFreeType::getAdvanceFotChar(unsigned short theChar)
|
||||
{
|
||||
if (!_fontRef)
|
||||
return false;
|
||||
|
||||
outNumLetters = count;
|
||||
return pCCSizes;
|
||||
*/
|
||||
// get the ID to the char we need
|
||||
int glyph_index = FT_Get_Char_Index(_fontRef, theChar);
|
||||
|
||||
return 0;
|
||||
if (!glyph_index)
|
||||
return false;
|
||||
|
||||
// load glyph infos
|
||||
if (FT_Load_Glyph(_fontRef, glyph_index, FT_LOAD_DEFAULT))
|
||||
return false;
|
||||
|
||||
// get to the advance for this glyph
|
||||
return (_fontRef->glyph->advance.x >> 6);
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
if (!glyph_index1)
|
||||
return -1;
|
||||
|
||||
// get the ID to the char we need
|
||||
int glyph_index2 = FT_Get_Char_Index(_fontRef, secondChar);
|
||||
|
||||
if (!glyph_index2)
|
||||
return -1;
|
||||
|
||||
FT_Vector kerning;
|
||||
|
||||
if (FT_Get_Kerning( _fontRef, glyph_index1, glyph_index2, FT_KERNING_DEFAULT, &kerning ))
|
||||
return -1;
|
||||
|
||||
return ( kerning.x >> 6 );
|
||||
}
|
||||
|
||||
Size * FontFreeType::getAdvancesForText(const char *pText, int &outNumLetters)
|
||||
{
|
||||
/*
|
||||
unsigned short int *utf8Text = FontIOS::getUTF8Text(pText, outNumLetters);
|
||||
if (utf8Text)
|
||||
{
|
||||
Size *ret = getAdvancesForTextUTF8(utf8Text, outNumLetters);
|
||||
delete [] utf8Text;
|
||||
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||
Size *ret = getAdvancesForTextUTF8(utf16String, outNumLetters);
|
||||
delete [] utf16String;
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Size FontFreeType::getTextWidthAndHeight(const char *pText)
|
||||
{
|
||||
/*
|
||||
Size retSize;
|
||||
NSString * str = [NSString stringWithUTF8String:pText];
|
||||
CGSize tmp = [str sizeWithFont:(UIFont *)_fontUI];
|
||||
retSize.width = 0;
|
||||
retSize.height = 0;
|
||||
|
||||
retSize.width = tmp.width;
|
||||
retSize.height = tmp.height;
|
||||
int numLetters;
|
||||
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;
|
||||
*/
|
||||
}
|
||||
|
||||
Size retSize;
|
||||
return retSize;
|
||||
unsigned char * FontFreeType::getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight)
|
||||
{
|
||||
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)
|
||||
{
|
||||
/*
|
||||
CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
||||
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;
|
||||
*/
|
||||
|
||||
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||
if(!utf16String)
|
||||
return 0;
|
||||
outNumLetters = cc_wcslen(utf16String);
|
||||
return utf16String;
|
||||
}
|
||||
|
||||
// carloX this could be broken
|
||||
const char * FontFreeType::trimUTF8Text(const char *pText, int newBegin, int newEnd)
|
||||
{
|
||||
/*
|
||||
if ( newBegin<0 || newEnd<=0 )
|
||||
return 0;
|
||||
|
||||
if ( newBegin>=newEnd )
|
||||
return 0;
|
||||
|
||||
NSString * str = [NSString stringWithUTF8String:pText];
|
||||
if ( newEnd >= [str length])
|
||||
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||
if (!utf16String)
|
||||
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];
|
||||
*/
|
||||
|
||||
if (newEnd >= cc_wcslen(utf16String))
|
||||
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)
|
||||
{
|
||||
/*
|
||||
CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
|
||||
if (NULL == lettersString)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return CFStringGetLength(lettersString);
|
||||
*/
|
||||
return 0;
|
||||
unsigned short* utf16String = cc_utf8_to_utf16(pText);
|
||||
if (!utf16String)
|
||||
return -1;
|
||||
int outNumLetters = cc_wcslen(utf16String);
|
||||
delete [] utf16String;
|
||||
return outNumLetters;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -22,8 +22,10 @@ class FontFreeType : public Font
|
|||
{
|
||||
public:
|
||||
|
||||
FontFreeType();
|
||||
virtual ~FontFreeType();
|
||||
virtual bool createFontObject(const std::string &fontName, int fontSize);
|
||||
|
||||
virtual int getUTF8TextLenght(const char *pText);
|
||||
virtual Size getTextWidthAndHeight(const char *pText);
|
||||
virtual GlyphDef * getGlyphsForText(const char *pText, int &outNumGlyphs);
|
||||
|
@ -31,11 +33,24 @@ public:
|
|||
virtual Size * getAdvancesForTextUTF8(unsigned short *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 int getLetterPadding() { return _letterPadding;}
|
||||
unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight);
|
||||
|
||||
private:
|
||||
|
||||
//CTFontRef _fontRef;
|
||||
//void * _fontUI;
|
||||
bool initFreeType();
|
||||
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
|
||||
|
||||
// FWD
|
||||
class Font;
|
||||
|
||||
class TextPageDef;
|
||||
|
||||
class FontRender
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
FontRender(Font *pFont) { _font = pFont; }
|
||||
virtual ~FontRender() {}
|
||||
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize) = 0;
|
||||
|
||||
protected:
|
||||
|
||||
Font * _font;
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
|
|
@ -10,28 +10,29 @@
|
|||
#include "CCTextImage.h"
|
||||
#include "CCFontRenderFreeType.h"
|
||||
|
||||
|
||||
#include "CCImage.h"
|
||||
|
||||
|
||||
|
||||
#include "ft2build.h"
|
||||
#include "CCStdC.h"
|
||||
#include FT_FREETYPE_H
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
FT_Library library;
|
||||
FT_Face currentFace;
|
||||
|
||||
|
||||
// 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
|
||||
if (FT_Render_Glyph( theGlyph, FT_RENDER_MODE_NORMAL ))
|
||||
return false;
|
||||
|
||||
// carloX posY -= theGlyph->bitmap_top;
|
||||
// carloX int testbearing = theGlyph->bitmap_top;
|
||||
|
||||
// copy the gliph into the bitmap
|
||||
FT_Bitmap& bitmap = theGlyph->bitmap;
|
||||
|
||||
|
@ -61,40 +62,105 @@ bool renderCharAt(FT_GlyphSlot theGlyph, unsigned char *destMemory, int posX, in
|
|||
return true;
|
||||
}
|
||||
|
||||
|
||||
void FontRenderFreeType::testRendering()
|
||||
bool FontRenderFreeType::renderCharAt(unsigned short int charToRender, int posX, int posY, unsigned char *destMemory, int destSize)
|
||||
{
|
||||
if (!_font)
|
||||
return false;
|
||||
|
||||
const char *stringToRender = "abcdefghilmnopqrstuvz";
|
||||
unsigned char *sourceBitmap = 0;
|
||||
int sourceWidth = 0;
|
||||
int sourceHeight = 0;
|
||||
|
||||
// font info
|
||||
int dpi = 72;
|
||||
int fontSize = 30;
|
||||
// get the glyph's bitmap
|
||||
sourceBitmap = _font->getGlyphBitmap(charToRender, sourceWidth, sourceHeight);
|
||||
|
||||
// error
|
||||
int libError;
|
||||
if(!sourceBitmap)
|
||||
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
|
||||
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;
|
||||
|
||||
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)
|
||||
return;
|
||||
return false;
|
||||
|
||||
// create the new face
|
||||
FT_Face face;
|
||||
|
||||
// create the face from the data
|
||||
if (FT_New_Memory_Face(library, data, len, 0, &face) )
|
||||
return;
|
||||
return false;
|
||||
|
||||
// set the requested font size
|
||||
int fontSizePoints = (int)(64.f * fontSize);
|
||||
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
|
||||
int bitmapSize = 512;
|
||||
|
@ -102,21 +168,20 @@ void FontRenderFreeType::testRendering()
|
|||
if(!pBitmap) return;
|
||||
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
|
||||
int paddingX = 5;
|
||||
int paddingY = 10;
|
||||
int paddingY = 0;
|
||||
|
||||
// where we need to render first
|
||||
int renderPosX = paddingX;
|
||||
int renderPosY = paddingY;
|
||||
|
||||
// this must be dynamic
|
||||
int letterWidth = 30;
|
||||
int letterHeight = 30;
|
||||
// carloX int renderPosY = lineHeight + paddingY;
|
||||
int renderPosY = 0;
|
||||
|
||||
int stringLength = strlen(stringToRender);
|
||||
|
||||
|
@ -129,113 +194,51 @@ void FontRenderFreeType::testRendering()
|
|||
if (FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT))
|
||||
return;
|
||||
|
||||
// get witdh and height for the next character
|
||||
|
||||
// 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;
|
||||
renderPosY += (letterHeight + paddingY);
|
||||
renderPosY += (lineHeight + paddingY);
|
||||
}
|
||||
|
||||
renderCharAt(face->glyph, pBitmap, renderPosX, renderPosY, bitmapSize);
|
||||
// render in bitmap
|
||||
renderCharAtStatic(face->glyph, pBitmap, renderPosX, renderPosY, bitmapSize);
|
||||
|
||||
// move
|
||||
renderPosX += letterWidth + paddingX;
|
||||
// move pen
|
||||
// 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
|
||||
Image *pImage = new Image;
|
||||
pImage->initWithRawData(pBitmap, (bitmapSize * bitmapSize * 4), 512, 512, 8, false);
|
||||
pImage->saveToFile("carlottone");
|
||||
|
||||
|
||||
// release the bitmap
|
||||
delete [] pBitmap;
|
||||
|
||||
// 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)
|
||||
{
|
||||
|
||||
// constants
|
||||
float LINE_PADDING = 1.9;
|
||||
|
||||
if (!thePage)
|
||||
return NULL;
|
||||
return 0;
|
||||
|
||||
if (_font)
|
||||
return 0;
|
||||
|
||||
if (thePage->getNumLines() == 0)
|
||||
return NULL;
|
||||
|
@ -246,16 +249,19 @@ unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, c
|
|||
// prepare memory and clean to 0
|
||||
int sizeInBytes = (pageWidth * pageHeight * 4);
|
||||
unsigned char* data = new unsigned char[sizeInBytes];
|
||||
|
||||
if (!data)
|
||||
return 0;
|
||||
|
||||
memset(data, 0, sizeInBytes);
|
||||
|
||||
|
||||
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 origX = _font->getLetterPadding();
|
||||
float origY = pCurrentLine->getY();
|
||||
|
||||
int numGlyphToRender = pCurrentLine->getNumGlyph();
|
||||
|
@ -263,91 +269,31 @@ unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, c
|
|||
for (int cglyph = 0; cglyph < numGlyphToRender; ++cglyph)
|
||||
{
|
||||
GlyphDef currGlyph = pCurrentLine->getGlyphAt(cglyph);
|
||||
|
||||
//NSString *lineString = [NSString stringWithFormat: @"%C", currGlyph.getUTF8Letter()];
|
||||
//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());
|
||||
renderCharAt(currGlyph.getUTF8Letter(), origX, origY, data, pageWidth);
|
||||
origX += (currGlyph.getRect().size.width + _font->getLetterPadding());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
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"
|
||||
|
||||
// this should be here #include "ft2build.h"
|
||||
// this should bg here #include FT_FREETYPE_H
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
@ -19,12 +21,23 @@ class FontRenderFreeType : public FontRender
|
|||
{
|
||||
public:
|
||||
|
||||
FontRenderFreeType(Font *pFont): FontRender(pFont) {}
|
||||
virtual ~FontRenderFreeType() {}
|
||||
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
|
||||
|
|
|
@ -13,10 +13,14 @@
|
|||
|
||||
NS_CC_BEGIN
|
||||
|
||||
class Font;
|
||||
|
||||
class FontRenderIOS : public FontRender
|
||||
{
|
||||
|
||||
public:
|
||||
|
||||
FontRenderIOS(Font *pFont): FontRender(pFont) {}
|
||||
virtual ~FontRenderIOS() {}
|
||||
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)
|
||||
{
|
||||
//carloX new stuff
|
||||
//_font
|
||||
|
||||
|
||||
// end carloX new stuff
|
||||
|
||||
|
||||
|
||||
if (_font)
|
||||
{
|
||||
delete _font;
|
||||
|
@ -308,7 +316,7 @@ bool TextImage::createFontRender()
|
|||
}
|
||||
|
||||
// carloX
|
||||
_fontRender = new FontRenderIOS();
|
||||
_fontRender = new FontRenderIOS(_font);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -184,6 +184,12 @@ private:
|
|||
Font * _font;
|
||||
FontRender * _fontRender;
|
||||
|
||||
|
||||
// carloX new stuff
|
||||
Font * _fontNew;
|
||||
FontRender * _fontRenderNew;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -1 +1 @@
|
|||
2836b978437c1bddf24c7c2cb39ac27d17eb6775
|
||||
a6b1fd0e7c62ab8e5372202bdb349c4ff7ffbe94
|
Loading…
Reference in New Issue