axmol/cocos2dx/platform/emscripten/CCImage.cpp

324 lines
9.4 KiB
C++
Raw Normal View History

2013-04-09 09:21:53 +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.
****************************************************************************/
#define __CC_PLATFORM_IMAGE_CPP__
#include "platform/CCImageCommon_cpp.h"
#include <string.h>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include "platform/CCImage.h"
#include "platform/CCFileUtils.h"
#include "platform/CCCommon.h"
#include "CCStdC.h"
#include <map>
#include <SDL/SDL.h>
#include <SDL/SDL_ttf.h>
#include <net/arpa/inet.h>
2013-04-09 09:21:53 +08:00
#define szFont_kenning 2
#define RSHIFT6(num) ((num)>>6)
using namespace std;
struct TextLine {
std::string sLineStr;
int iLineWidth;
};
NS_CC_BEGIN
class BitmapDC
{
public:
BitmapDC()
{
iInterval = szFont_kenning;
_data = NULL;
2013-04-09 09:21:53 +08:00
reset();
}
~BitmapDC(void)
{
}
void reset() {
iMaxLineWidth = 0;
iMaxLineHeight = 0;
vLines.clear();
}
void buildLine(std::stringstream& ss, TTF_Font *face)
2013-04-09 09:21:53 +08:00
{
TextLine oTempLine;
ss << '\0';
oTempLine.sLineStr = ss.str();
int w, h;
TTF_SizeText(face, oTempLine.sLineStr.c_str(), &w, &h);
oTempLine.iLineWidth = w;
2013-04-09 09:21:53 +08:00
iMaxLineWidth = MAX(iMaxLineWidth, oTempLine.iLineWidth);
ss.clear();
ss.str("");
vLines.push_back(oTempLine);
}
bool divideString(TTF_Font *face, const char* sText, int iMaxWidth, int iMaxHeight) {
2013-04-09 09:21:53 +08:00
const char* pText = sText;
2013-04-09 09:21:53 +08:00
//init stringstream
std::stringstream ss;
int w, h;
2013-04-09 09:21:53 +08:00
while (*pText != '\0') {
if (*pText == '\n') {
buildLine(ss, face);
2013-04-09 09:21:53 +08:00
pText++;
continue;
}
2013-04-09 09:21:53 +08:00
//check its width
//divide it when exceeding
std::string s = ss.str();
s.push_back(*pText);
TTF_SizeText(face, s.c_str(), &w, &h);
2013-04-09 09:21:53 +08:00
if (iMaxWidth > 0 && (w > iMaxWidth)) {
buildLine(ss, face);
2013-04-09 09:21:53 +08:00
}
ss << *pText;
pText++;
}
buildLine(ss, face);
2013-04-09 09:21:53 +08:00
return true;
}
/**
* compute the start pos of every line
*
* return >0 represent the start x pos of the line
* while -1 means fail
*
*/
int computeLineStart(TTF_Font *face, Image::TextAlign eAlignMask, char cText,
2013-04-09 09:21:53 +08:00
int iLineIndex) {
return 0;
/*
2013-04-09 09:21:53 +08:00
int iRet;
int iError = FT_Load_Glyph(face, FT_Get_Char_Index(face, cText),
FT_LOAD_DEFAULT);
if (iError) {
return -1;
}
if (eAlignMask == Image::kAlignCenter) {
2013-04-09 09:21:53 +08:00
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth) / 2
- RSHIFT6(face->glyph->metrics.horiBearingX );
} else if (eAlignMask == Image::kAlignRight) {
2013-04-09 09:21:53 +08:00
iRet = (iMaxLineWidth - vLines[iLineIndex].iLineWidth)
- RSHIFT6(face->glyph->metrics.horiBearingX );
} else {
// left or other situation
iRet = -RSHIFT6(face->glyph->metrics.horiBearingX );
}
return iRet;
*/
2013-04-09 09:21:53 +08:00
}
int computeLineStartY( TTF_Font *face, Image::TextAlign eAlignMask, int txtHeight, int borderHeight ){
return 0;
/*
2013-04-09 09:21:53 +08:00
int iRet;
if (eAlignMask == Image::kAlignCenter || eAlignMask == Image::kAlignLeft ||
eAlignMask == Image::kAlignRight ) {
2013-04-09 09:21:53 +08:00
//vertical center
iRet = (borderHeight - txtHeight)/2 + RSHIFT6(face->size->metrics.ascender);
} else if (eAlignMask == Image::kAlignBottomRight ||
eAlignMask == Image::kAlignBottom ||
eAlignMask == Image::kAlignBottomLeft ) {
2013-04-09 09:21:53 +08:00
//vertical bottom
iRet = borderHeight - txtHeight + RSHIFT6(face->size->metrics.ascender);
} else {
// left or other situation
iRet = RSHIFT6(face->size->metrics.ascender);
}
return iRet;
*/
2013-04-09 09:21:53 +08:00
}
bool getBitmap(const char *text, int nWidth, int nHeight, Image::TextAlign eAlignMask, const char * pFontName, float fontSize) {
2013-04-09 09:21:53 +08:00
const char* pText = text;
int pxSize = (int)fontSize;
2013-04-09 09:21:53 +08:00
TTF_Font *face = TTF_OpenFont(pFontName, pxSize);
if(!face)
{
return false;
}
2013-04-09 09:21:53 +08:00
divideString(face, text, nWidth, nHeight);
2013-04-09 09:21:53 +08:00
//compute the final line width
iMaxLineWidth = MAX(iMaxLineWidth, nWidth);
2013-04-09 09:21:53 +08:00
iMaxLineHeight = pxSize;
iMaxLineHeight *= vLines.size();
2013-04-09 09:21:53 +08:00
//compute the final line height
iMaxLineHeight = MAX(iMaxLineHeight, nHeight);
2013-04-09 09:21:53 +08:00
uint bitmapSize = iMaxLineWidth * iMaxLineHeight * 4;
_data = new unsigned char[bitmapSize];
memset(_data, 0, bitmapSize);
2013-04-09 09:21:53 +08:00
if(!strlen(text))
{
return true;
}
// XXX: Can this be optimized by inserting newlines into the string and
// making a single TTF_RenderText_Solid call? Could conceivably just
// pass back SDL's buffer then, though would need additional logic to
// call SDL_FreeSurface appropriately.
// FIXME: handle alignment, etc.
for (size_t l = 0; l < vLines.size(); l++) {
pText = vLines[l].sLineStr.c_str();
if(!strlen(pText))
{
continue;
}
2013-04-09 09:21:53 +08:00
SDL_Color color = { 0xff, 0xff, 0xff, 0xff };
SDL_Surface *tSurf = TTF_RenderText_Solid(face, pText, color);
if(!tSurf)
{
TTF_CloseFont(face);
return false;
}
SDL_LockSurface(tSurf);
SDL_UnlockSurface(tSurf);
2013-04-09 09:21:53 +08:00
// We treat pixels as 32-bit words, since both source and target
// are rendered as such.
int *pixels = (int*)tSurf->pixels;
int *out = (int*)_data;
2013-04-09 09:21:53 +08:00
// (i, j) should be treated as (x, y) coordinates in the source
// bitmap. This loop maps those locations to the target bitmap.
// Need to ensure that those values do not exceed the allocated
// memory.
int minWidth = MIN(tSurf->w, iMaxLineWidth);
for(int i = 0; i < tSurf->h && (i + l * pxSize) < iMaxLineHeight; ++i)
{
for(int j = 0; j < minWidth; ++j)
{
int sourceOffset = i * tSurf->w + j;
int targetOffset = (l * pxSize + i) * iMaxLineWidth + j;
// HTML5 canvas is non-pre-alpha-multiplied, so alpha-multiply here.
unsigned char *p = (unsigned char*) &pixels[sourceOffset];
out[targetOffset] = CC_RGB_PREMULTIPLY_ALPHA( p[0], p[1], p[2], p[3] );
2013-04-09 09:21:53 +08:00
}
}
SDL_FreeSurface(tSurf);
}
2013-04-09 09:21:53 +08:00
//clear all lines
vLines.clear();
2013-04-09 09:21:53 +08:00
TTF_CloseFont(face);
2013-04-09 09:21:53 +08:00
return true;
2013-04-09 09:21:53 +08:00
}
public:
unsigned char *_data;
2013-04-09 09:21:53 +08:00
int libError;
vector<TextLine> vLines;
int iInterval;
int iMaxLineWidth;
int iMaxLineHeight;
};
static BitmapDC& sharedBitmapDC()
{
static BitmapDC s_BmpDC;
return s_BmpDC;
}
bool Image::initWithString(
2013-04-09 09:21:53 +08:00
const char * pText,
int nWidth/* = 0*/,
int nHeight/* = 0*/,
TextAlign eAlignMask/* = kAlignCenter*/,
2013-04-09 09:21:53 +08:00
const char * pFontName/* = nil*/,
int nSize/* = 0*/)
{
bool bRet = false;
do
{
CC_BREAK_IF(! pText);
BitmapDC &dc = sharedBitmapDC();
std::string fullFontName = pFontName;
std::string lowerCasePath = fullFontName;
std::transform(lowerCasePath.begin(), lowerCasePath.end(), lowerCasePath.begin(), ::tolower);
CC_BREAK_IF(! dc.getBitmap(pText, nWidth, nHeight, eAlignMask, fullFontName.c_str(), nSize));
2013-04-09 09:21:53 +08:00
// assign the dc._data to _data in order to save time
_data = dc._data;
CC_BREAK_IF(! _data);
2013-04-09 09:21:53 +08:00
_width = (short)dc.iMaxLineWidth;
_height = (short)dc.iMaxLineHeight;
_hasAlpha = true;
_preMulti = true;
_bitsPerComponent = 8;
2013-04-09 09:21:53 +08:00
bRet = true;
dc.reset();
} while (0);
return bRet;
}
NS_CC_END