mirror of https://github.com/axmolengine/axmol.git
render and font IOS are no more
This commit is contained in:
parent
9c53da22fa
commit
31b40263a7
|
@ -1,46 +0,0 @@
|
|||
//
|
||||
// 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 FontIOS : public Font
|
||||
{
|
||||
public:
|
||||
|
||||
virtual ~FontIOS();
|
||||
virtual bool createFontObject(const std::string &fontName, int fontSize);
|
||||
|
||||
virtual GlyphDef * getGlyphsForText(const char *pText, int &outNumGlyphs, bool UTF16text = false);
|
||||
virtual Size * getAdvancesForText(const char *pText, int &outNumLetters, bool UTF16text = false);
|
||||
virtual Size getTextWidthAndHeight(const char *pText, bool UTF16text = false);
|
||||
virtual Size * getAdvancesForTextUTF16(unsigned short *pText, int &outNumLetters);
|
||||
virtual unsigned short int * getUTF16Text(const char *pText, int &outNumLetters);
|
||||
|
||||
virtual int getLetterPadding() { return 0; }
|
||||
virtual unsigned char * getGlyphBitmap(unsigned short theChar, int &outWidth, int &outHeight) { return 0; }
|
||||
virtual unsigned short int * trimUTF16Text(unsigned short int *pText, int newBegin, int newEnd) { return 0; }
|
||||
virtual int getUTF16TextLenght(unsigned short int *pText) { return 0; }
|
||||
|
||||
|
||||
private:
|
||||
|
||||
CTFontRef _fontRef;
|
||||
void * _fontUI;
|
||||
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif
|
|
@ -1,227 +0,0 @@
|
|||
//
|
||||
// CCFontIOS.mm
|
||||
// TestNewStringStuff
|
||||
//
|
||||
// Created by Carlo Morgantini on 5/20/13.
|
||||
//
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <CoreText/CTFont.h>
|
||||
#include <CoreText/CTTypesetter.h>
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <UIKit/UIFont.h>
|
||||
#include <UIKit/UIStringDrawing.h>
|
||||
|
||||
#include "support/ccUTF8.h"
|
||||
|
||||
#include "cocos2d.h"
|
||||
#include "CCFontIOS.h"
|
||||
#include "CCTextImage.h"
|
||||
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
bool FontIOS::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) );
|
||||
}
|
||||
|
||||
FontIOS::~FontIOS()
|
||||
{
|
||||
// release the font
|
||||
// TO DO
|
||||
}
|
||||
|
||||
GlyphDef * FontIOS::getGlyphsForText(const char *pText, int &outNumGlyphs, bool UTF16text)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
Size * FontIOS::getAdvancesForTextUTF16(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;
|
||||
}
|
||||
|
||||
Size * FontIOS::getAdvancesForText(const char *pText, int &outNumLetters, bool UTF16text)
|
||||
{
|
||||
unsigned short int *utf8Text = FontIOS::getUTF16Text(pText, outNumLetters);
|
||||
if (utf8Text)
|
||||
{
|
||||
Size *ret = getAdvancesForTextUTF16(utf8Text, outNumLetters);
|
||||
delete [] utf8Text;
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Size FontIOS::getTextWidthAndHeight(const char *pText, bool UTF16text)
|
||||
{
|
||||
Size retSize;
|
||||
NSString * str = [NSString stringWithUTF8String:pText];
|
||||
CGSize tmp = [str sizeWithFont:(UIFont *)_fontUI];
|
||||
|
||||
retSize.width = tmp.width;
|
||||
retSize.height = tmp.height;
|
||||
|
||||
return retSize;
|
||||
}
|
||||
|
||||
unsigned short int * FontIOS::getUTF16Text(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;
|
||||
}
|
||||
|
||||
NS_CC_END
|
|
@ -1,31 +0,0 @@
|
|||
//
|
||||
// CCFontRenderIOS.h
|
||||
// TestNewStringStuff
|
||||
//
|
||||
// Created by Carlo Morgantini on 5/28/13.
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef _FontRenderIOS_h
|
||||
#define _FontRenderIOS_h
|
||||
|
||||
#include "CCFontRender.h"
|
||||
|
||||
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);
|
||||
|
||||
};
|
||||
|
||||
NS_CC_END
|
||||
|
||||
#endif
|
|
@ -1,99 +0,0 @@
|
|||
//
|
||||
// CCFontRenderIOS.mm
|
||||
// TestNewStringStuff
|
||||
//
|
||||
// Created by Carlo Morgantini on 5/28/13.
|
||||
//
|
||||
//
|
||||
#include <CoreGraphics/CoreGraphics.h>
|
||||
#include <UIKit/UIKit.h>
|
||||
#include "cocos2d.h"
|
||||
#include "CCTextImage.h"
|
||||
#include "CCFontRenderIOS.h"
|
||||
|
||||
NS_CC_BEGIN
|
||||
|
||||
unsigned char * FontRenderIOS::preparePageGlyphData(TextPageDef *thePage, char *fontName, int fontSize)
|
||||
{
|
||||
// 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
|
Loading…
Reference in New Issue