start integrating FreeType for font rendering

This commit is contained in:
carlo morgantini 2013-07-11 15:43:45 -07:00
parent 002ae77698
commit 1caa6dfbd7
7 changed files with 452 additions and 2 deletions

View File

@ -1 +1 @@
677964274bf46041ba92250007e73f13c206b537 bb1fa37ce8c92571a186d384fac845fcd218b10e

View File

@ -0,0 +1,279 @@
#include <stdio.h>
#include "cocos2d.h"
#include "CCFontFreeType.h"
#include "CCTextImage.h"
NS_CC_BEGIN
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()];
// actually create iOS font (s)
_fontRef = CTFontCreateWithName(theRefString, fontSize, NULL);
_fontUI = [UIFont fontWithName:fntName size:fontSize];
return ( (_fontRef != NULL) && (_fontUI != NULL) );
*/
return false;
}
FontFreeType::~FontFreeType()
{
// release the font
// TO DO
}
GlyphDef * FontFreeType::getGlyphsForText(const char *pText, int &outNumGlyphs)
{
/*
float CHAR_PADDING = 10.0f;
UniChar * characters;
CGGlyph * glyphs;
CFIndex count;
CFStringRef lettersString;
lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
if (NULL == lettersString)
return false;
count = CFStringGetLength(lettersString);
// Allocate our buffers for characters and glyphs.
characters = new UniChar[count];
assert(characters != NULL);
glyphs = new CGGlyph[count];
assert(glyphs != NULL);
// Get the characters from the string.
CFStringGetCharacters(lettersString, CFRangeMake(0, count), characters);
// Get the glyphs for the characters.
CTFontGetGlyphsForCharacters(_fontRef, characters, glyphs, count);
CGGlyph *theFirstGlyph = &glyphs[0];
// get letters bounding boxes
CGRect *BBOx = new CGRect[count];
assert(BBOx != NULL);
CTFontGetBoundingRectsForGlyphs(_fontRef, kCTFontHorizontalOrientation, theFirstGlyph, BBOx, count);
GlyphDef *pGlyphs = new GlyphDef[count];
assert(pGlyphs != NULL);
// sore result as CCRect
for (int c=0; c<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);
}
// release memory
delete [] characters;
delete [] glyphs;
delete [] BBOx;
outNumGlyphs = count;
return pGlyphs;
*/
return 0;
}
Size * FontFreeType::getAdvancesForTextUTF8(unsigned short *pText, int &outNumLetters)
{
/*
if (!pText)
return 0;
outNumLetters = cc_wcslen(pText);
if (!outNumLetters)
return 0;
// create the reference to the string
CFStringRef lettersString = CFStringCreateWithCharacters(kCFAllocatorDefault, pText, outNumLetters);
if (NULL == lettersString)
{
return 0;
}
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;
}
delete [] characters;
delete [] glyphs;
delete [] pSize;
outNumLetters = count;
return pCCSizes;
*/
return 0;
}
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;
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 = tmp.width;
retSize.height = tmp.height;
return retSize;
*/
Size retSize;
return retSize;
}
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;
*/
return 0;
}
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])
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];
*/
return 0;
}
int FontFreeType::getUTF8TextLenght(const char *pText)
{
/*
CFStringRef lettersString = CFStringCreateWithCString(kCFAllocatorDefault, pText, kCFStringEncodingUTF8);
if (NULL == lettersString)
{
return 0;
}
return CFStringGetLength(lettersString);
*/
return 0;
}
NS_CC_END

View File

@ -0,0 +1,41 @@
//
// CCFontIOS.h
// TestNewStringStuff
//
// Created by Carlo Morgantini on 5/20/13.
//
//
#ifndef _FontIOS_h
#define _FontIOS_h
#include <CoreText/CTFont.h>
#include "CCFont.h"
NS_CC_BEGIN
class FontFreeType : public Font
{
public:
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);
virtual Size * getAdvancesForText(const char *pText, int &outNumLetters);
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);
private:
//CTFontRef _fontRef;
//void * _fontUI;
};
NS_CC_END
#endif

View File

@ -0,0 +1,102 @@
//
// CCFontRenderIOS.mm
// TestNewStringStuff
//
// Created by Carlo Morgantini on 5/28/13.
//
//
#include "cocos2d.h"
#include "CCTextImage.h"
#include "CCFontRenderFreeType.h"
NS_CC_BEGIN
unsigned char * FontRenderFreeType::preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize)
{
return 0;
/*
// constants
float LINE_PADDING = 1.9;
if (!thePage)
return NULL;
if (thePage->getNumLines() == 0)
return NULL;
int pageWidth = thePage->getWidth();
int pageHeight = thePage->getHeight();
// prepare memory ans set to 0
int sizeInBytes = (pageWidth * pageHeight * 4);
unsigned char* data = new unsigned char[sizeInBytes];
memset(data, 0, sizeInBytes);
// 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;
*/
}
NS_CC_END

View File

@ -0,0 +1,27 @@
//
// CCFontRenderIOS.h
// TestNewStringStuff
//
// Created by Carlo Morgantini on 5/28/13.
//
//
#ifndef _FontRenderFreeType_h
#define _FontRenderFreeType_h
#include "CCFontRender.h"
NS_CC_BEGIN
class FontRenderFreeType : public FontRender
{
public:
virtual ~FontRenderFreeType() {}
virtual unsigned char * preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize);
};
NS_CC_END
#endif

View File

@ -53,6 +53,7 @@ struct LineBreakLine {
}; };
NS_CC_BEGIN NS_CC_BEGIN
class BitmapDC class BitmapDC
{ {
public: public: