axmol/cocos2dx/platform/emscripten/CCImage.cpp

360 lines
11 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
std::stringstream ss;
int w, h;
2013-04-09 09:21:53 +08:00
// if there is no maximum width specified, just slam the whole input
// string into a single line, thereby avoiding many expensive calls to
// compute font metrics.
if(iMaxWidth == 0)
{
std::string text = sText;
ss << text;
buildLine(ss, face);
return true;
}
std::vector<std::string> words;
std::string text = sText;
std::istringstream iss(text);
copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter< vector<string> >(words));
2013-04-09 09:21:53 +08:00
for(std::vector<string>::iterator i = words.begin(); i != words.end(); ++i)
{
// Specially handle the case where a single word exceeds the
// available width.
TTF_SizeText(face, i->c_str(), &w, &h);
if(w > iMaxWidth)
{
buildLine(ss, face);
for(std::string::iterator c = i->begin(); c != i->end(); ++c)
{
ss << *c;
TTF_SizeText(face, ss.str().c_str(), &w, &h);
if(w > iMaxWidth)
{
buildLine(ss, face);
}
}
2013-04-09 09:21:53 +08:00
continue;
}
std::string tmp = ss.str() + std::string(" ") + *i;
TTF_SizeText(face, tmp.c_str(), &w, &h);
if(w > iMaxWidth)
{
buildLine(ss, face);
ss << *i;
}
else
{
ss << " " << *i;
2013-04-09 09:21:53 +08:00
}
}
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(Image::TextAlign eAlignMask, int lineWidth, int maxLineWidth)
{
int result = 0;
if( eAlignMask == Image::TextAlign::CENTER ||
eAlignMask == Image::TextAlign::TOP ||
eAlignMask == Image::TextAlign::BOTTOM)
{
result = (maxLineWidth / 2) - (lineWidth / 2);
}
else if(eAlignMask == Image::TextAlign::RIGHT ||
eAlignMask == Image::TextAlign::TOP_RIGHT ||
eAlignMask == Image::TextAlign::BOTTOM_RIGHT)
{
result = maxLineWidth - lineWidth;
}
// In all other cases (left alignment, most likely), return 0.
// Attempt to ensure that we don't produce any completely invalid reslts.
if(result < 0)
{
result = 0;
}
return result;
}
int computeLineStartY(Image::TextAlign eAlignMask, int lineHeight, int maxLineHeight)
{
int result = 0;
if( eAlignMask == Image::TextAlign::CENTER ||
eAlignMask == Image::TextAlign::RIGHT ||
eAlignMask == Image::TextAlign::LEFT)
{
result = (maxLineHeight / 2) - (lineHeight / 2);
}
else if(eAlignMask == Image::TextAlign::BOTTOM ||
eAlignMask == Image::TextAlign::BOTTOM_RIGHT ||
eAlignMask == Image::TextAlign::BOTTOM_LEFT)
{
result = maxLineHeight - lineHeight;
}
// In all other cases (top alignment, most likely), return 0;
if(result < 0)
{
result = 0;
}
return result;
}
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) {
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.
// Y offset for vertical alignment remains constant throughout, as it
// is the entire block of text that must be vertically aligned.
int yOffset = computeLineStartY(eAlignMask, vLines.size() * pxSize, iMaxLineHeight);
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;
}
// The lock/unlock pair is required since Emscripten's SDL
// implementation copies pixel data from its off-screen canvas to
// the pixels array in the unlock operation. Without this, we would
// be reading uninitialized memory.
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
// Compute offset to produce horizontal alignment.
int xOffset = computeLineStart(eAlignMask, tSurf->w, iMaxLineWidth);
// (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 j = 0; j < tSurf->h && (j + l * pxSize) < iMaxLineHeight; ++j)
{
for(int i = 0; i < minWidth; ++i)
{
int sourceOffset = j * tSurf->w + i;
int targetOffset = (l * pxSize + j + yOffset) * iMaxLineWidth + i + xOffset;
// 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
2013-08-20 04:45:32 +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;
_preMulti = true;
_renderFormat = Texture2D::PixelFormat::RGBA8888;
_dataLen = _width * _height * 4;
2013-04-09 09:21:53 +08:00
bRet = true;
dc.reset();
} while (0);
return bRet;
}
NS_CC_END