2011-09-19 18:05:40 +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.
|
|
|
|
****************************************************************************/
|
2011-11-24 15:15:23 +08:00
|
|
|
#include <FBase.h>
|
|
|
|
#include <FGraphics.h>
|
|
|
|
|
|
|
|
using namespace Osp::Base;
|
|
|
|
using namespace Osp::Base::Collection;
|
|
|
|
using namespace Osp::Graphics;
|
|
|
|
|
2011-09-19 18:05:40 +08:00
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_BEGIN
|
2011-09-19 18:05:40 +08:00
|
|
|
|
2011-11-24 15:15:23 +08:00
|
|
|
/**
|
|
|
|
@brief A memory DC which uses to draw text on bitmap.
|
|
|
|
*/
|
|
|
|
class BitmapDC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
BitmapDC()
|
|
|
|
{
|
2011-09-26 15:40:56 +08:00
|
|
|
m_pCanvas = NULL;
|
2011-11-24 15:15:23 +08:00
|
|
|
m_pEnrichedText = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
~BitmapDC()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(m_pCanvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool drawText(const char * pszText, Dimension& dimensions, CCImage::ETextAlign alignment, const char * fontName = NULL, int fontSize = 0)
|
|
|
|
{
|
|
|
|
bool nRet = false;
|
2011-09-20 12:39:44 +08:00
|
|
|
do
|
|
|
|
{
|
2011-11-23 18:40:36 +08:00
|
|
|
CC_BREAK_IF(pszText == NULL || strlen(pszText) <= 0);
|
2011-09-20 12:39:44 +08:00
|
|
|
// text
|
2011-09-29 17:29:38 +08:00
|
|
|
Osp::Base::String strText(pszText);
|
2011-09-20 12:39:44 +08:00
|
|
|
// Set a font to the TextElement
|
|
|
|
Font font;
|
2011-11-24 15:15:23 +08:00
|
|
|
bool bUseDefaultFont = true;
|
|
|
|
if (fontName != NULL && strlen(fontName) > 0)
|
|
|
|
{
|
|
|
|
String strFonName(fontName);
|
|
|
|
if (strFonName.EndsWith(".ttf") || strFonName.EndsWith(".TTF"))
|
|
|
|
{
|
|
|
|
bUseDefaultFont = false;
|
|
|
|
const char* pFullFontPath = CCFileUtils::fullPathFromRelativePath(fontName);
|
|
|
|
font.Construct(pFullFontPath, FONT_STYLE_PLAIN, fontSize);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
IList* pSystemFontList = Font::GetSystemFontListN();
|
|
|
|
if (pSystemFontList != NULL)
|
|
|
|
{
|
|
|
|
IEnumerator* pEnum = pSystemFontList->GetEnumeratorN();
|
|
|
|
Object* pObj = null;
|
|
|
|
while (pEnum->MoveNext() == E_SUCCESS)
|
|
|
|
{
|
|
|
|
pObj = pEnum->GetCurrent();
|
|
|
|
String* pStrName = static_cast<String*>(pObj);
|
|
|
|
if (pStrName->Equals(strFonName, false))
|
|
|
|
{
|
|
|
|
bUseDefaultFont = false;
|
|
|
|
font.Construct(*pStrName, FONT_STYLE_PLAIN, fontSize);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete pEnum;
|
|
|
|
|
|
|
|
pSystemFontList->RemoveAll(true);
|
|
|
|
delete pSystemFontList;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bUseDefaultFont)
|
|
|
|
{
|
|
|
|
font.Construct(FONT_STYLE_PLAIN, fontSize);
|
|
|
|
}
|
2011-09-20 12:39:44 +08:00
|
|
|
|
|
|
|
// calculate text size
|
2011-11-23 18:40:36 +08:00
|
|
|
if (dimensions.width <= 0)
|
2011-09-20 12:39:44 +08:00
|
|
|
{
|
|
|
|
Dimension dim;
|
2011-09-29 17:29:38 +08:00
|
|
|
font.GetTextExtent(strText, strText.GetLength(), dim);
|
2011-09-26 15:40:56 +08:00
|
|
|
dimensions.width = dim.width;
|
|
|
|
dimensions.height = dim.height;
|
2011-09-20 12:39:44 +08:00
|
|
|
}
|
|
|
|
|
2011-11-23 18:40:36 +08:00
|
|
|
CC_BREAK_IF(dimensions.width <= 0);
|
2011-09-20 12:39:44 +08:00
|
|
|
|
|
|
|
// Create an EnrichedText
|
|
|
|
m_pEnrichedText = new EnrichedText();
|
2011-11-23 18:40:36 +08:00
|
|
|
m_pEnrichedText->Construct(Dimension(dimensions.width, 10));
|
2011-09-20 12:39:44 +08:00
|
|
|
|
|
|
|
switch (alignment)
|
|
|
|
{
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignCenter:
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
|
|
|
|
break;
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignTop:
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
|
|
|
|
break;
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignTopRight:
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
|
|
|
|
break;
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignRight:
|
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
|
|
|
|
break;
|
|
|
|
case CCImage::kAlignBottomRight:
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_RIGHT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
|
|
|
|
break;
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignBottom:
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
|
|
|
|
break;
|
2011-09-26 15:40:56 +08:00
|
|
|
case CCImage::kAlignBottomLeft:
|
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_BOTTOM);
|
|
|
|
break;
|
|
|
|
case CCImage::kAlignLeft:
|
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
|
|
|
|
break;
|
|
|
|
case CCImage::kAlignTopLeft:
|
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_LEFT);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_TOP);
|
|
|
|
break;
|
2011-09-20 12:39:44 +08:00
|
|
|
default:
|
|
|
|
m_pEnrichedText->SetHorizontalAlignment(TEXT_ALIGNMENT_CENTER);
|
|
|
|
m_pEnrichedText->SetVerticalAlignment(TEXT_ALIGNMENT_MIDDLE);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// Set attributes of the EnrichedText
|
|
|
|
m_pEnrichedText->SetTextWrapStyle(TEXT_WRAP_CHARACTER_WRAP);
|
|
|
|
m_pEnrichedText->SetTextAbbreviationEnabled(true);
|
|
|
|
|
|
|
|
// Create a TextElement
|
2011-09-26 15:40:56 +08:00
|
|
|
TextElement* pTextElement = new TextElement();
|
2011-09-29 17:29:38 +08:00
|
|
|
pTextElement->Construct(strText);
|
2011-09-20 12:39:44 +08:00
|
|
|
// After Adding, set attributes of the TextElement
|
2011-09-26 15:40:56 +08:00
|
|
|
pTextElement->SetTextColor(Color::COLOR_WHITE);
|
|
|
|
pTextElement->SetFont(font);
|
2011-09-20 12:39:44 +08:00
|
|
|
// Add the TextElement to the EnrichedText
|
2011-09-26 15:40:56 +08:00
|
|
|
m_pEnrichedText->Add(*pTextElement);
|
2011-09-20 12:39:44 +08:00
|
|
|
|
2011-11-23 18:40:36 +08:00
|
|
|
m_pEnrichedText->Refresh();
|
|
|
|
dimensions.height = m_pEnrichedText->GetTotalLineHeight();
|
|
|
|
m_pEnrichedText->SetSize(dimensions.width, dimensions.height);
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(m_pCanvas);
|
|
|
|
m_pCanvas = new Canvas();
|
|
|
|
m_pCanvas->Construct(Rectangle(0, 0, dimensions.width, dimensions.height));
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pCanvas->DrawText(Point(0, 0), *m_pEnrichedText);
|
2011-09-26 15:40:56 +08:00
|
|
|
|
2011-09-20 12:39:44 +08:00
|
|
|
m_pEnrichedText->RemoveAllTextElements(true);
|
2011-09-26 15:40:56 +08:00
|
|
|
CC_SAFE_DELETE(m_pEnrichedText);
|
|
|
|
|
|
|
|
nRet = true;
|
2011-11-24 15:15:23 +08:00
|
|
|
} while (0);
|
|
|
|
return nRet;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
friend class CCImage;
|
|
|
|
Canvas* m_pCanvas;
|
|
|
|
EnrichedText* m_pEnrichedText;
|
|
|
|
std::string m_curFontPath;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BitmapDC& sharedBitmapDC()
|
|
|
|
{
|
|
|
|
static BitmapDC s_BmpDC;
|
|
|
|
return s_BmpDC;
|
2011-09-20 12:39:44 +08:00
|
|
|
}
|
|
|
|
|
2011-09-19 18:05:40 +08:00
|
|
|
bool CCImage::initWithString(
|
|
|
|
const char * pText,
|
|
|
|
int nWidth/* = 0*/,
|
|
|
|
int nHeight/* = 0*/,
|
|
|
|
ETextAlign eAlignMask/* = kAlignCenter*/,
|
|
|
|
const char * pFontName/* = nil*/,
|
|
|
|
int nSize/* = 0*/)
|
|
|
|
{
|
|
|
|
bool bRet = false;
|
|
|
|
result r = E_FAILURE;
|
|
|
|
do
|
|
|
|
{
|
2011-09-28 09:49:18 +08:00
|
|
|
int nLen = 0;
|
2011-11-24 15:15:23 +08:00
|
|
|
CC_BREAK_IF(! pText);
|
|
|
|
|
|
|
|
BitmapDC& dc = sharedBitmapDC();
|
2011-09-20 12:39:44 +08:00
|
|
|
|
2011-11-23 18:40:36 +08:00
|
|
|
Dimension size(nWidth, nHeight);
|
2011-09-19 18:05:40 +08:00
|
|
|
|
2011-09-26 15:40:56 +08:00
|
|
|
bRet = dc.drawText(pText, size, eAlignMask, pFontName, nSize);
|
|
|
|
|
|
|
|
CC_BREAK_IF(!bRet);
|
2011-09-19 18:05:40 +08:00
|
|
|
|
|
|
|
BufferInfo bufferInfo;
|
2011-09-26 15:40:56 +08:00
|
|
|
r = dc.m_pCanvas->Lock(bufferInfo);
|
2011-09-19 18:05:40 +08:00
|
|
|
CC_BREAK_IF(IsFailed(r));
|
|
|
|
|
2011-09-28 09:49:18 +08:00
|
|
|
nLen = bufferInfo.pitch * bufferInfo.height;
|
2011-09-26 15:40:56 +08:00
|
|
|
CC_SAFE_DELETE(m_pData);
|
2011-09-19 18:05:40 +08:00
|
|
|
m_pData = new unsigned char [nLen];
|
2011-09-28 09:49:18 +08:00
|
|
|
memset(m_pData, 0, nLen);
|
2011-09-19 18:05:40 +08:00
|
|
|
CC_BREAK_IF(!m_pData);
|
|
|
|
memcpy(m_pData, bufferInfo.pPixels, nLen);
|
|
|
|
|
2011-11-23 18:40:36 +08:00
|
|
|
int iBytesPerPixel = bufferInfo.bitsPerPixel/8;
|
2011-09-26 15:40:56 +08:00
|
|
|
|
2011-11-23 18:40:36 +08:00
|
|
|
m_nWidth = bufferInfo.pitch / iBytesPerPixel;
|
|
|
|
m_nHeight = bufferInfo.height;
|
2011-09-19 18:05:40 +08:00
|
|
|
m_bHasAlpha = true;
|
2011-09-26 15:40:56 +08:00
|
|
|
m_bPreMulti = false;
|
2011-11-23 18:40:36 +08:00
|
|
|
m_nBitsPerComponent = 8;
|
2011-09-26 15:40:56 +08:00
|
|
|
dc.m_pCanvas->Unlock();
|
2011-09-19 18:05:40 +08:00
|
|
|
|
|
|
|
bRet = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
return bRet;
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:56:22 +08:00
|
|
|
NS_CC_END
|