2012-08-18 05:45:21 +08:00
|
|
|
/****************************************************************************
|
2012-10-17 01:47:04 +08:00
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
2012-10-17 10:57:21 +08:00
|
|
|
#define __CC_PLATFORM_IMAGE_CPP__
|
|
|
|
#include "platform/CCImageCommon_cpp.h"
|
2012-10-17 01:47:04 +08:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
|
|
|
#include <string>
|
|
|
|
#include <sstream>
|
2013-02-01 11:20:46 +08:00
|
|
|
#include <algorithm>
|
2012-10-17 10:57:21 +08:00
|
|
|
#include "platform/CCImage.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "platform/CCCommon.h"
|
|
|
|
#include "CCStdC.h"
|
2012-10-17 01:47:04 +08:00
|
|
|
#include "ft2build.h"
|
|
|
|
#include FT_FREETYPE_H
|
|
|
|
|
|
|
|
#define szFont_kenning 2
|
|
|
|
|
|
|
|
#define SHIFT6(num) (num>>6)
|
|
|
|
|
2012-08-18 05:45:21 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
struct TextLine {
|
|
|
|
int iLineWidth;
|
2012-10-19 01:43:31 +08:00
|
|
|
wchar_t* text;
|
2012-10-17 01:47:04 +08:00
|
|
|
};
|
|
|
|
|
2012-10-17 10:57:21 +08:00
|
|
|
NS_CC_BEGIN
|
2012-08-18 05:45:21 +08:00
|
|
|
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
2013-01-03 07:53:31 +08:00
|
|
|
BitmapDC()
|
2012-08-18 05:45:21 +08:00
|
|
|
{
|
2012-10-17 01:47:04 +08:00
|
|
|
libError = FT_Init_FreeType( &library );
|
|
|
|
iInterval = szFont_kenning;
|
2013-06-15 14:03:30 +08:00
|
|
|
_data = NULL;
|
2012-10-17 01:47:04 +08:00
|
|
|
reset();
|
2012-08-18 05:45:21 +08:00
|
|
|
}
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2012-08-18 05:45:21 +08:00
|
|
|
~BitmapDC(void)
|
|
|
|
{
|
2012-10-17 01:47:04 +08:00
|
|
|
FT_Done_FreeType(library);
|
2012-08-18 05:45:21 +08:00
|
|
|
}
|
2012-10-17 01:47:04 +08:00
|
|
|
|
|
|
|
void reset() {
|
|
|
|
iMaxLineWidth = 0;
|
|
|
|
iMaxLineHeight = 0;
|
2012-10-19 01:43:31 +08:00
|
|
|
size_t size = vLines.size();
|
|
|
|
for (int i=0; i<size; ++i) {
|
|
|
|
TextLine line = vLines[i];
|
|
|
|
free(line.text);
|
|
|
|
}
|
2012-10-17 01:47:04 +08:00
|
|
|
vLines.clear();
|
|
|
|
}
|
|
|
|
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
int utf8(char **p)
|
|
|
|
{
|
|
|
|
if ((**p & 0x80) == 0x00)
|
|
|
|
{
|
|
|
|
int a = *((*p)++);
|
|
|
|
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
if ((**p & 0xE0) == 0xC0)
|
|
|
|
{
|
|
|
|
int a = *((*p)++) & 0x1F;
|
|
|
|
int b = *((*p)++) & 0x3F;
|
|
|
|
|
|
|
|
return (a << 6) | b;
|
|
|
|
}
|
|
|
|
if ((**p & 0xF0) == 0xE0)
|
|
|
|
{
|
|
|
|
int a = *((*p)++) & 0x0F;
|
|
|
|
int b = *((*p)++) & 0x3F;
|
|
|
|
int c = *((*p)++) & 0x3F;
|
|
|
|
|
|
|
|
return (a << 12) | (b << 6) | c;
|
|
|
|
}
|
|
|
|
if ((**p & 0xF8) == 0xF0)
|
|
|
|
{
|
|
|
|
int a = *((*p)++) & 0x07;
|
|
|
|
int b = *((*p)++) & 0x3F;
|
|
|
|
int c = *((*p)++) & 0x3F;
|
|
|
|
int d = *((*p)++) & 0x3F;
|
|
|
|
|
|
|
|
return (a << 18) | (b << 12) | (c << 8) | d;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void buildLine(wchar_t* buf, size_t buf_len, FT_Face face, int iCurXCursor, FT_UInt cLastChar) {
|
2012-10-17 01:47:04 +08:00
|
|
|
TextLine oTempLine;
|
2012-10-19 01:43:31 +08:00
|
|
|
wchar_t* text = (wchar_t*)malloc(sizeof(wchar_t) * (buf_len+1));
|
|
|
|
memcpy(text, buf, sizeof(wchar_t) * buf_len);
|
|
|
|
text[buf_len] = '\0';
|
|
|
|
oTempLine.text = text;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
//get last glyph
|
2012-10-19 01:43:31 +08:00
|
|
|
int iError = FT_Load_Char(face, cLastChar, FT_LOAD_DEFAULT);
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
oTempLine.iLineWidth = iCurXCursor - SHIFT6((face->glyph->metrics.horiAdvance - face->glyph->metrics.horiBearingX - face->glyph->metrics.width))/*-iInterval*/;//TODO interval
|
|
|
|
iMaxLineWidth = MAX(iMaxLineWidth, oTempLine.iLineWidth);
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
vLines.push_back(oTempLine);
|
2012-10-17 01:47:04 +08:00
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
bool divideString(FT_Face face, const char* sText, int iMaxWidth, int iMaxHeight) {
|
|
|
|
int iError = 0;
|
2012-10-19 01:43:31 +08:00
|
|
|
int iCurXCursor, iCurYCursor;
|
|
|
|
const char* pText = sText;
|
|
|
|
|
|
|
|
FT_UInt unicode = utf8((char**)&pText);
|
|
|
|
iError = FT_Load_Char(face, unicode, FT_LOAD_DEFAULT);
|
2012-10-17 01:47:04 +08:00
|
|
|
if (iError) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
FT_UInt cLastCh = 0;
|
|
|
|
|
|
|
|
pText = sText;
|
|
|
|
size_t text_len = 0;
|
|
|
|
wchar_t* text_buf = (wchar_t*) malloc(sizeof(wchar_t) * strlen(sText));
|
|
|
|
while (unicode=utf8((char**)&pText)) {
|
|
|
|
if (unicode == '\n') {
|
|
|
|
buildLine(text_buf, text_len, face, iCurXCursor, cLastCh);
|
|
|
|
text_len = 0;
|
|
|
|
|
|
|
|
iError = FT_Load_Char(face, unicode, FT_LOAD_DEFAULT);
|
2012-10-17 01:47:04 +08:00
|
|
|
if (iError) {
|
2012-10-19 01:43:31 +08:00
|
|
|
free(text_buf);
|
2012-10-17 01:47:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
|
|
|
|
continue;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
iError = FT_Load_Char(face, unicode, FT_LOAD_DEFAULT);
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
if (iError) {
|
2012-10-19 01:43:31 +08:00
|
|
|
free(text_buf);
|
2012-10-17 01:47:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
//check its width
|
|
|
|
//divide it when exceeding
|
|
|
|
if ((iMaxWidth > 0
|
2012-10-19 01:43:31 +08:00
|
|
|
&& iCurXCursor + SHIFT6(face->glyph->metrics.width)
|
|
|
|
> iMaxWidth)) {
|
|
|
|
buildLine(text_buf, text_len, face , iCurXCursor, cLastCh);
|
|
|
|
text_len = 0;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
iCurXCursor = -SHIFT6(face->glyph->metrics.horiBearingX);
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
cLastCh = unicode;
|
|
|
|
text_buf[text_len] = unicode;
|
|
|
|
++text_len;
|
2012-10-17 01:47:04 +08:00
|
|
|
iCurXCursor += SHIFT6(face->glyph->metrics.horiAdvance) + iInterval;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
if (iError) {
|
2012-10-19 01:43:31 +08:00
|
|
|
free(text_buf);
|
2012-10-17 01:47:04 +08:00
|
|
|
return false;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
|
|
|
buildLine(text_buf, text_len, face, iCurXCursor, cLastCh);
|
|
|
|
free(text_buf);
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
/**
|
|
|
|
* compute the start pos of every line
|
|
|
|
*
|
|
|
|
* return >0 represent the start x pos of the line
|
|
|
|
* while -1 means fail
|
|
|
|
*
|
|
|
|
*/
|
2013-07-19 15:37:54 +08:00
|
|
|
int computeLineStart(FT_Face face, Image::TextAlign eAlignMask, char cText,
|
2012-10-17 01:47:04 +08:00
|
|
|
int iLineIndex) {
|
|
|
|
int iRet;
|
|
|
|
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, cText),
|
|
|
|
FT_LOAD_DEFAULT);
|
|
|
|
if (iError) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
if (eAlignMask == Image::kAlignCenter) {
|
2012-10-17 01:47:04 +08:00
|
|
|
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth) / 2
|
|
|
|
- SHIFT6(face->glyph->metrics.horiBearingX );
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
} else if (eAlignMask == Image::kAlignRight) {
|
2012-10-17 01:47:04 +08:00
|
|
|
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth)
|
|
|
|
- SHIFT6(face->glyph->metrics.horiBearingX );
|
|
|
|
} else {
|
|
|
|
// left or other situation
|
|
|
|
iRet = -SHIFT6(face->glyph->metrics.horiBearingX );
|
|
|
|
}
|
|
|
|
return iRet;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2013-07-19 15:37:54 +08:00
|
|
|
int computeLineStartY( FT_Face face, Image::TextAlign eAlignMask, int txtHeight, int borderHeight ){
|
2012-10-19 01:43:31 +08:00
|
|
|
int iRet;
|
2013-06-20 14:13:12 +08:00
|
|
|
if (eAlignMask == Image::kAlignCenter || eAlignMask == Image::kAlignLeft ||
|
|
|
|
eAlignMask == Image::kAlignRight ) {
|
2012-10-19 01:43:31 +08:00
|
|
|
//vertical center
|
|
|
|
iRet = (borderHeight - txtHeight)/2 + SHIFT6(face->size->metrics.ascender);
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
} else if (eAlignMask == Image::kAlignBottomRight ||
|
|
|
|
eAlignMask == Image::kAlignBottom ||
|
|
|
|
eAlignMask == Image::kAlignBottomLeft ) {
|
2012-10-19 01:43:31 +08:00
|
|
|
//vertical bottom
|
|
|
|
iRet = borderHeight - txtHeight + SHIFT6(face->size->metrics.ascender);
|
|
|
|
} else {
|
|
|
|
// left or other situation
|
|
|
|
iRet = SHIFT6(face->size->metrics.ascender);
|
|
|
|
}
|
|
|
|
return iRet;
|
|
|
|
}
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2013-07-19 15:37:54 +08:00
|
|
|
bool getBitmap(const char *text, int nWidth, int nHeight, Image::TextAlign eAlignMask, const char * pFontName, float fontSize) {
|
2012-10-17 01:47:04 +08:00
|
|
|
FT_Face face;
|
|
|
|
FT_Error iError;
|
|
|
|
|
|
|
|
const char* pText = text;
|
2013-06-20 14:13:12 +08:00
|
|
|
//data will be deleted by Image
|
2013-06-15 14:03:30 +08:00
|
|
|
// if (_data) {
|
|
|
|
// delete _data;
|
2012-10-17 01:47:04 +08:00
|
|
|
// }
|
|
|
|
|
|
|
|
int iCurXCursor, iCurYCursor;
|
|
|
|
bool bRet = false;
|
|
|
|
if (libError) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
do {
|
2013-03-16 23:56:03 +08:00
|
|
|
//CCLog(" ---- FT_New_Face with pFontName = %s", pFontName);
|
2012-10-17 01:47:04 +08:00
|
|
|
iError = FT_New_Face( library, pFontName, 0, &face );
|
|
|
|
|
|
|
|
if (iError) {
|
|
|
|
//no valid font found use default
|
2013-03-16 23:56:03 +08:00
|
|
|
//CCLog(" ---- no valid font, use default %s", pFontName);
|
2012-10-17 01:47:04 +08:00
|
|
|
iError = FT_New_Face( library, "/usr/fonts/font_repository/monotype/arial.ttf", 0, &face );
|
|
|
|
}
|
|
|
|
CC_BREAK_IF(iError);
|
|
|
|
|
|
|
|
//select utf8 charmap
|
|
|
|
iError = FT_Select_Charmap(face,FT_ENCODING_UNICODE);
|
|
|
|
CC_BREAK_IF(iError);
|
|
|
|
|
|
|
|
iError = FT_Set_Pixel_Sizes(face, fontSize,fontSize);
|
|
|
|
CC_BREAK_IF(iError);
|
|
|
|
|
|
|
|
iError = divideString(face, text, nWidth, nHeight)?0:1;
|
|
|
|
|
|
|
|
//compute the final line width
|
|
|
|
iMaxLineWidth = MAX(iMaxLineWidth, nWidth);
|
|
|
|
|
|
|
|
iMaxLineHeight = (face->size->metrics.ascender >> 6)
|
|
|
|
- (face->size->metrics.descender >> 6);
|
|
|
|
iMaxLineHeight *= vLines.size();
|
|
|
|
|
2012-10-19 01:43:31 +08:00
|
|
|
int txtHeight = iMaxLineHeight;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
//compute the final line height
|
|
|
|
iMaxLineHeight = MAX(iMaxLineHeight, nHeight);
|
2013-06-15 14:03:30 +08:00
|
|
|
_data = new unsigned char[iMaxLineWidth * iMaxLineHeight*4];
|
2012-10-19 01:43:31 +08:00
|
|
|
// iCurYCursor = SHIFT6(face->size->metrics.ascender);
|
|
|
|
iCurYCursor = computeLineStartY( face, eAlignMask, txtHeight, iMaxLineHeight );
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
memset(_data,0, iMaxLineWidth * iMaxLineHeight*4);
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2012-10-19 01:43:31 +08:00
|
|
|
size_t lines = vLines.size();
|
|
|
|
for (size_t i = 0; i < lines; i++) {
|
|
|
|
const wchar_t* text_ptr = vLines[i].text;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
//initialize the origin cursor
|
2012-10-19 01:43:31 +08:00
|
|
|
iCurXCursor = computeLineStart(face, eAlignMask, text_ptr[0], i);
|
|
|
|
|
|
|
|
size_t text_len = wcslen(text_ptr);
|
|
|
|
for (size_t i=0; i<text_len; ++i) {
|
|
|
|
int iError = FT_Load_Char(face, text_ptr[i], FT_LOAD_RENDER);
|
2012-10-17 01:47:04 +08:00
|
|
|
if (iError) {
|
|
|
|
break;
|
|
|
|
}
|
2012-10-19 01:43:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
// convert glyph to bitmap with 256 gray
|
|
|
|
// and get the bitmap
|
2012-10-19 01:43:31 +08:00
|
|
|
FT_Bitmap& bitmap = face->glyph->bitmap;
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2012-10-19 01:43:31 +08:00
|
|
|
int yoffset = iCurYCursor - (face->glyph->metrics.horiBearingY >> 6);
|
|
|
|
int xoffset = iCurXCursor + (face->glyph->metrics.horiBearingX >> 6);
|
2012-10-17 01:47:04 +08:00
|
|
|
for (int i = 0; i < bitmap.rows; ++i) {
|
|
|
|
for (int j = 0; j < bitmap.width; ++j) {
|
2012-10-19 01:43:31 +08:00
|
|
|
unsigned char cTemp = bitmap.buffer[i * bitmap.width + j];
|
|
|
|
if (cTemp == 0) continue;
|
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
// if it has gray>0 we set show it as 1, o otherwise
|
2012-10-19 01:43:31 +08:00
|
|
|
int iY = yoffset + i;
|
|
|
|
int iX = xoffset + j;
|
2012-10-17 01:47:04 +08:00
|
|
|
|
|
|
|
if (iY>=iMaxLineHeight) {
|
|
|
|
//exceed the height truncate
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// _data[(iY * iMaxLineWidth + iX) * 4 + 3] =
|
2012-10-19 01:43:31 +08:00
|
|
|
// bitmap.buffer[i * bitmap.width + j] ?
|
|
|
|
// 0xff : 0;//alpha
|
2013-06-15 14:03:30 +08:00
|
|
|
// _data[(iY * iMaxLineWidth + iX) * 4 + 1] =
|
2012-10-19 01:43:31 +08:00
|
|
|
// bitmap.buffer[i * bitmap.width + j];//R
|
2013-06-15 14:03:30 +08:00
|
|
|
// _data[(iY * iMaxLineWidth + iX) * 4 + 2] =
|
2012-10-19 01:43:31 +08:00
|
|
|
// bitmap.buffer[i * bitmap.width + j];//G
|
2013-06-15 14:03:30 +08:00
|
|
|
// _data[(iY * iMaxLineWidth + iX) * 4 + 0] =
|
2012-10-19 01:43:31 +08:00
|
|
|
// bitmap.buffer[i * bitmap.width + j];//B
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2012-10-19 01:43:31 +08:00
|
|
|
int iTemp = cTemp << 24 | cTemp << 16 | cTemp << 8 | cTemp;
|
2013-06-15 14:03:30 +08:00
|
|
|
*(int*) &_data[(iY * iMaxLineWidth + iX) * 4 + 0] = iTemp;
|
2012-10-17 01:47:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//step to next glyph
|
|
|
|
iCurXCursor += (face->glyph->metrics.horiAdvance >> 6)
|
|
|
|
+ iInterval;
|
|
|
|
|
|
|
|
pText++;
|
|
|
|
}
|
|
|
|
iCurYCursor += (face->size->metrics.ascender >> 6)
|
|
|
|
- (face->size->metrics.descender >> 6);
|
|
|
|
}
|
|
|
|
//print all image bitmap
|
|
|
|
// for (int i = 0; i < iMaxLineHeight; i++) {
|
|
|
|
// for (int j = 0; j < iMaxLineWidth; j++) {
|
|
|
|
// printf("%d",
|
2013-06-15 14:03:30 +08:00
|
|
|
// _data[(i * iMaxLineWidth + j) * 4] ? 1 : 0);
|
2012-10-17 01:47:04 +08:00
|
|
|
// }
|
|
|
|
// printf("\n");
|
|
|
|
// }
|
|
|
|
|
|
|
|
// free face
|
|
|
|
FT_Done_Face(face);
|
|
|
|
face = NULL;
|
|
|
|
|
|
|
|
//clear all lines
|
|
|
|
vLines.clear();
|
|
|
|
|
|
|
|
//success;
|
|
|
|
if (iError) {
|
|
|
|
bRet = false;
|
|
|
|
} else
|
|
|
|
bRet = true;
|
|
|
|
}while(0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
2013-01-03 07:53:31 +08:00
|
|
|
|
2012-10-17 01:47:04 +08:00
|
|
|
public:
|
|
|
|
FT_Library library;
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned char *_data;
|
2012-10-17 01:47:04 +08:00
|
|
|
int libError;
|
|
|
|
vector<TextLine> vLines;
|
|
|
|
int iInterval;
|
|
|
|
int iMaxLineWidth;
|
|
|
|
int iMaxLineHeight;
|
2012-08-18 05:45:21 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Image::initWithString(
|
2012-10-17 01:47:04 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
2013-07-19 15:37:54 +08:00
|
|
|
TextAlign eAlignMask/* = kAlignCenter*/,
|
2012-10-17 01:47:04 +08:00
|
|
|
const char * pFontName/* = nil*/,
|
|
|
|
int nSize/* = 0*/)
|
2012-08-18 05:45:21 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
2012-10-17 01:47:04 +08:00
|
|
|
do
|
2012-08-18 05:45:21 +08:00
|
|
|
{
|
|
|
|
CC_BREAK_IF(! pText);
|
|
|
|
BitmapDC &dc = sharedBitmapDC();
|
2013-01-03 07:53:31 +08:00
|
|
|
|
2013-02-01 11:20:46 +08:00
|
|
|
std::string fullFontName = pFontName;
|
|
|
|
std::string lowerCasePath = fullFontName;
|
|
|
|
std::transform(lowerCasePath.begin(), lowerCasePath.end(), lowerCasePath.begin(), ::tolower);
|
|
|
|
|
|
|
|
if ( lowerCasePath.find(".ttf") != std::string::npos ) {
|
2013-07-12 12:03:39 +08:00
|
|
|
fullFontName = FileUtils::getInstance()->fullPathForFilename(pFontName);
|
2013-02-01 11:20:46 +08:00
|
|
|
}
|
2013-01-29 10:40:44 +08:00
|
|
|
//CCLog("-----pText=%s and Font File is %s nWidth= %d,nHeight=%d",pText,fullFontName.c_str(),nWidth,nHeight);
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2013-01-29 10:40:44 +08:00
|
|
|
CC_BREAK_IF(! dc.getBitmap(pText, nWidth, nHeight, eAlignMask, fullFontName.c_str(), nSize));
|
2013-03-16 23:56:03 +08:00
|
|
|
//CCLog("---- dc.getBitmap is Succesfull...");
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// assign the dc._data to _data in order to save time
|
|
|
|
_data = dc._data;
|
|
|
|
CC_BREAK_IF(! _data);
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_width = (short)dc.iMaxLineWidth;
|
|
|
|
_height = (short)dc.iMaxLineHeight;
|
|
|
|
_preMulti = true;
|
2013-07-27 22:06:30 +08:00
|
|
|
_renderFormat = Texture2D::PixelFormat::RGBA8888;
|
2012-10-17 01:47:04 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
|
|
|
|
dc.reset();
|
|
|
|
|
2012-08-18 05:45:21 +08:00
|
|
|
} while (0);
|
2012-10-17 01:47:04 +08:00
|
|
|
|
2012-08-18 05:45:21 +08:00
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-10-17 10:57:21 +08:00
|
|
|
NS_CC_END
|
2012-08-18 05:45:21 +08:00
|
|
|
|