2011-02-24 19:42:45 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
// undefine ANDROID to include skia headers
|
|
|
|
#ifdef ANDROID
|
|
|
|
#undef ANDROID
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* used for SkRefCnt::unref(), it used to decrease the count */
|
|
|
|
static int sk_atomic_dec(int *value)
|
|
|
|
{
|
|
|
|
*value = *value - 1;
|
|
|
|
return *value;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "SkPaint.h"
|
|
|
|
#include "SkScalar.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkTypeface.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN;
|
|
|
|
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BitmapDC()
|
|
|
|
: m_pBitmap(NULL),
|
|
|
|
m_pPaint(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC(void)
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_SAFE_DELETE(m_pPaint);
|
|
|
|
CC_SAFE_DELETE(m_pBitmap);
|
2011-02-24 19:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool setFont(const char * pFontName = NULL, int nSize = 0)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
if (m_pPaint)
|
|
|
|
{
|
|
|
|
delete m_pPaint;
|
|
|
|
m_pPaint = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
/* init paint */
|
|
|
|
m_pPaint = new SkPaint();
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! m_pPaint);
|
2011-02-24 19:42:45 +08:00
|
|
|
m_pPaint->setColor(SK_ColorWHITE);
|
|
|
|
m_pPaint->setTextSize(nSize);
|
|
|
|
|
|
|
|
/* create font */
|
|
|
|
SkTypeface *pTypeFace = SkTypeface::CreateFromName(pFontName, SkTypeface::kNormal);
|
|
|
|
if (! pTypeFace)
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_SAFE_DELETE(m_pPaint);
|
2011-02-24 19:42:45 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
m_pPaint->setTypeface( pTypeFace );
|
|
|
|
/* can not unref, I don't know why. It may be memory leak, but how to avoid? */
|
|
|
|
pTypeFace->unref();
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool prepareBitmap(int nWidth, int nHeight)
|
|
|
|
{
|
|
|
|
// release bitmap
|
|
|
|
if (m_pBitmap)
|
|
|
|
{
|
|
|
|
delete m_pBitmap;
|
|
|
|
m_pBitmap = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nWidth > 0 && nHeight > 0)
|
|
|
|
{
|
|
|
|
/* create and init bitmap */
|
|
|
|
m_pBitmap = new SkBitmap();
|
|
|
|
if (! m_pBitmap)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use rgba8888 and alloc memory */
|
|
|
|
m_pBitmap->setConfig(SkBitmap::kARGB_8888_Config, nWidth, nHeight);
|
|
|
|
if (! m_pBitmap->allocPixels())
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_SAFE_DELETE(m_pBitmap);
|
2011-02-24 19:42:45 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start with black/transparent pixels */
|
|
|
|
m_pBitmap->eraseColor(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:49:58 +08:00
|
|
|
bool drawText(const char *pszText, int nWidth, int nHeight, CCImage::ETextAlign eAlignMask)
|
2011-02-24 19:42:45 +08:00
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! pszText);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! prepareBitmap(nWidth, nHeight));
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
/* create canvas */
|
|
|
|
SkPaint::FontMetrics font;
|
|
|
|
m_pPaint->getFontMetrics(&font);
|
|
|
|
SkCanvas canvas(*m_pBitmap);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* draw text
|
|
|
|
* @todo: alignment
|
|
|
|
*/
|
|
|
|
canvas.drawText(pszText, strlen(pszText), 0.0, -font.fAscent, *m_pPaint);
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool getTextExtentPoint(const char * pszText, int *pWidth, int *pHeight)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(!pszText || !pWidth || !pHeight);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
// get text width and height
|
|
|
|
if (m_pPaint)
|
|
|
|
{
|
|
|
|
SkPaint::FontMetrics font;
|
|
|
|
m_pPaint->getFontMetrics(&font);
|
2011-02-25 13:35:27 +08:00
|
|
|
*pHeight = (int)ceil((font.fDescent - font.fAscent));
|
2011-03-01 19:53:39 +08:00
|
|
|
*pWidth = (int)ceil((m_pPaint->measureText(pszText, strlen(pszText))));
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
}
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkBitmap* getBitmap()
|
|
|
|
{
|
|
|
|
return m_pBitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
SkPaint *m_pPaint;
|
|
|
|
SkBitmap *m_pBitmap;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
|
|
|
}
|
|
|
|
|
2011-03-08 13:49:58 +08:00
|
|
|
bool CCImage::initWithString(
|
2011-02-24 19:42:45 +08:00
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
|
|
|
ETextAlign eAlignMask/* = kAlignCenter*/,
|
|
|
|
const char * pFontName/* = nil*/,
|
|
|
|
int nSize/* = 0*/)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! pText);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
BitmapDC &dc = sharedBitmapDC();
|
|
|
|
|
|
|
|
/* init font with font name and size */
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! dc.setFont(pFontName, nSize));
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
/* compute text width and height */
|
|
|
|
if (nWidth <= 0 || nHeight <= 0)
|
|
|
|
{
|
|
|
|
dc.getTextExtentPoint(pText, &nWidth, &nHeight);
|
|
|
|
}
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(nWidth <= 0 || nHeight <= 0);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-04-28 15:14:59 +08:00
|
|
|
CC_BREAK_IF( false == dc.drawText(pText, nWidth, nHeight, eAlignMask) );
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
/*init image information */
|
|
|
|
SkBitmap *pBitmap = dc.getBitmap();
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(! pBitmap);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
int nWidth = pBitmap->width();
|
|
|
|
int nHeight = pBitmap->height();
|
2011-03-08 13:49:58 +08:00
|
|
|
CC_BREAK_IF(nWidth <= 0 || nHeight <= 0);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
|
|
|
int nDataLen = pBitmap->rowBytes() * pBitmap->height();
|
2011-03-28 11:09:49 +08:00
|
|
|
m_pData = new unsigned char[nDataLen];
|
|
|
|
CC_BREAK_IF(! m_pData);
|
|
|
|
memcpy((void*) m_pData, pBitmap->getPixels(), nDataLen);
|
2011-02-24 19:42:45 +08:00
|
|
|
|
2011-03-28 11:09:49 +08:00
|
|
|
m_nWidth = (short)nWidth;
|
|
|
|
m_nHeight = (short)nHeight;
|
2011-02-24 19:42:45 +08:00
|
|
|
m_bHasAlpha = true;
|
|
|
|
m_bPreMulti = true;
|
|
|
|
m_nBitsPerComponent = pBitmap->bytesPerPixel();
|
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END;
|