</pre></h4><p><pre name="code" class="cpp">
近日,一直被3.0的字体描边所困扰,在安卓手机下字体没问题,挺好看,但是在iphone手机上显示特别不清晰,一直很怪异的感觉,尤其是白色的label,后来我的同事兼师父将底层改了下(3.2之后的版本貌似就不存在这个问题了)。
方法:找到项目/cocos2d/cocos/2d/platform目录下的 CCDevice.h文件 的静态gettexturedatafortext方法改成这个(即加一个参数)
static Data getTextureDataForText(const char * text,const FontDefinition& textDefinition,TextAlign align,int &width,int &height,bool& hasPremultipliedAlpha);,然后把相应平台的这个函数分别增一个参数(因为只有ios好像有这个问题,所以其他的平台暂时不用改,但是需要在功能函数添加一个实参):
ios/CCDevice.mm
Data Device::getTextureDataForText(const char * text,bool& hasPremultipliedAlpha) { Data ret; do { tImageInfo info = {0}; info.width = textDefinition._dimensions.width; info.height = textDefinition._dimensions.height; info.hasShadow = textDefinition._shadow._shadowEnabled; info.shadowOffset.width = textDefinition._shadow._shadowOffset.width; info.shadowOffset.height = textDefinition._shadow._shadowOffset.height; info.shadowBlur = textDefinition._shadow._shadowBlur; info.shadowOpacity = textDefinition._shadow._shadowOpacity; info.hasStroke = textDefinition._stroke._strokeEnabled; info.strokeColorR = textDefinition._stroke._strokeColor.r / 255.0f; info.strokeColorG = textDefinition._stroke._strokeColor.g / 255.0f; info.strokeColorB = textDefinition._stroke._strokeColor.b / 255.0f; info.strokeSize = textDefinition._stroke._strokeSize; info.tintColorR = textDefinition._fontFillColor.r / 255.0f; info.tintColorG = textDefinition._fontFillColor.g / 255.0f; info.tintColorB = textDefinition._fontFillColor.b / 255.0f; if (! _initWithString(text,align,textDefinition._fontName.c_str(),textDefinition._fontSize,&info)) { break; } height = (short)info.height; width = (short)info.width; ret.fastSet(info.data,width * height * 4); hasPremultipliedAlpha = true; } while (0); return ret; }
win32/CCDevice.cpp
Data Device::getTextureDataForText(const char * text,bool& hasPremultipliedAlpha) { Data ret; do { BitmapDC& dc = sharedBitmapDC(); if (! dc.setFont(textDefinition._fontName.c_str(),textDefinition._fontSize)) { log("Can't found font(%s),use system default",textDefinition._fontName.c_str()); } // draw text SIZE size = {textDefinition._dimensions.width,textDefinition._dimensions.height}; CC_BREAK_IF(! dc.drawText(text,size,align)); int dataLen = size.cx * size.cy * 4; unsigned char* dataBuf = (unsigned char*)malloc(sizeof(unsigned char) * dataLen); CC_BREAK_IF(! dataBuf); struct { BITMAPINFOHEADER bmiHeader; int mask[4]; } bi = {0}; bi.bmiHeader.biSize = sizeof(bi.bmiHeader); CC_BREAK_IF(! GetDIBits(dc.getDC(),dc.getBitmap(),NULL,(LPBITMAPINFO)&bi,DIB_RGB_COLORS)); width = (short)size.cx; height = (short)size.cy; // copy pixed data bi.bmiHeader.biHeight = (bi.bmiHeader.biHeight > 0) ? - bi.bmiHeader.biHeight : bi.bmiHeader.biHeight; GetDIBits(dc.getDC(),height,dataBuf,DIB_RGB_COLORS); // change pixel's alpha value to 255,when it's RGB != 0 COLORREF * pPixel = NULL; for (int y = 0; y < height; ++y) { pPixel = (COLORREF *)dataBuf + y * width; for (int x = 0; x < width; ++x) { COLORREF& clr = *pPixel; clr = (0xffffff | (GetRValue(clr) << 24)); ++pPixel; } } ret.fastSet(dataBuf,dataLen); } while (0); return ret; }
最后,将CCTexture2D.cpp的initWithString(const char *text,const FontDefinition& textDefinition)方法改成:
bool Texture2D::initWithString(const char *text,const FontDefinition& textDefinition) { if(!text || 0 == strlen(text)) { return false; } #if CC_ENABLE_CACHE_TEXTURE_DATA // cache the texture data VolatileTextureMgr::addStringTexture(this,text,textDefinition); #endif bool ret = false; Device::TextAlign align; if (TextVAlignment::TOP == textDefinition._vertAlignment) { align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::TOP : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::TOP_LEFT : Device::TextAlign::TOP_RIGHT; } else if (TextVAlignment::CENTER == textDefinition._vertAlignment) { align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::CENTER : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::LEFT : Device::TextAlign::RIGHT; } else if (TextVAlignment::BOTTOM == textDefinition._vertAlignment) { align = (TextHAlignment::CENTER == textDefinition._alignment) ? Device::TextAlign::BOTTOM : (TextHAlignment::LEFT == textDefinition._alignment) ? Device::TextAlign::BOTTOM_LEFT : Device::TextAlign::BOTTOM_RIGHT; } else { CCASSERT(false,"Not supported alignment format!"); return false; } #if (CC_TARGET_PLATFORM != CC_PLATFORM_ANDROID) && (CC_TARGET_PLATFORM != CC_PLATFORM_IOS) CCASSERT(textDefinition._stroke._strokeEnabled == false,"Currently stroke only supported on iOS and Android!"); #endif PixelFormat pixelFormat = g_defaultAlphaPixelFormat; unsigned char* outTempData = nullptr; ssize_t outTempDataLen = 0; int imageWidth; int imageHeight; auto textDef = textDefinition; auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR(); textDef._fontSize *= contentScaleFactor; textDef._dimensions.width *= contentScaleFactor; textDef._dimensions.height *= contentScaleFactor; textDef._stroke._strokeSize *= contentScaleFactor; textDef._shadow._shadowEnabled = false; bool hasPremultipliedAlpha; Data outData = Device::getTextureDataForText(text,textDef,imageWidth,imageHeight,hasPremultipliedAlpha); if(outData.isNull()) { return false; } Size imageSize = Size((float)imageWidth,(float)imageHeight); pixelFormat = convertDataToFormat(outData.getBytes(),imageWidth*imageHeight*4,PixelFormat::RGBA8888,pixelFormat,&outTempData,&outTempDataLen); ret = initWithData(outTempData,outTempDataLen,imageSize); if (outTempData != nullptr && outTempData != outData.getBytes()) { free(outTempData); } _hasPremultipliedAlpha = hasPremultipliedAlpha; return ret; }