diff --git a/cocos/2d/CCFontAtlasCache.cpp b/cocos/2d/CCFontAtlasCache.cpp index 7881414770..d27a58bf2e 100644 --- a/cocos/2d/CCFontAtlasCache.cpp +++ b/cocos/2d/CCFontAtlasCache.cpp @@ -34,6 +34,7 @@ NS_CC_BEGIN std::unordered_map FontAtlasCache::_atlasMap; +#define ATLAS_MAP_KEY_BUFFER 255 void FontAtlasCache::purgeCachedData() { @@ -53,11 +54,13 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) useDistanceField = false; } - char tmp[300]; + char tmp[ATLAS_MAP_KEY_BUFFER]; if (useDistanceField) { - sprintf(tmp, "%s distance field %f %d",config->fontFilePath.c_str(), config->fontSize, config->outlineSize); + snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "df %.2f %d %s", config->fontSize, config->outlineSize, + config->fontFilePath.c_str()); } else { - sprintf(tmp, "%s %f %d",config->fontFilePath.c_str(), config->fontSize, config->outlineSize); + snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %d %s", config->fontSize, config->outlineSize, + config->fontFilePath.c_str()); } std::string atlasName = tmp; @@ -88,8 +91,8 @@ FontAtlas* FontAtlasCache::getFontAtlasTTF(const _ttfConfig* config) FontAtlas* FontAtlasCache::getFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset /* = Vec2::ZERO */) { - char tmp[255]; - sprintf(tmp, "%s %f %f", fontFileName.c_str(), imageOffset.x, imageOffset.y); + char tmp[ATLAS_MAP_KEY_BUFFER]; + snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); std::string atlasName = tmp; auto it = _atlasMap.find(atlasName); @@ -176,8 +179,8 @@ FontAtlas* FontAtlasCache::getFontAtlasCharMap(Texture2D* texture, int itemWidth FontAtlas* FontAtlasCache::getFontAtlasCharMap(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap) { - char tmp[255]; - snprintf(tmp,250,"name:%s_%d_%d_%d",charMapFile.c_str(),itemWidth,itemHeight,startCharMap); + char tmp[ATLAS_MAP_KEY_BUFFER]; + snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%d %d %d %s", itemWidth, itemHeight, startCharMap, charMapFile.c_str()); std::string atlasName = tmp; auto it = _atlasMap.find(atlasName); @@ -229,8 +232,8 @@ bool FontAtlasCache::releaseFontAtlas(FontAtlas *atlas) void FontAtlasCache::reloadFontAtlasFNT(const std::string& fontFileName, const Vec2& imageOffset/* = Vec2::ZERO*/) { - char tmp[255]; - sprintf(tmp, "%s %f %f", fontFileName.c_str(), imageOffset.x, imageOffset.y); + char tmp[ATLAS_MAP_KEY_BUFFER]; + snprintf(tmp, ATLAS_MAP_KEY_BUFFER, "%.2f %.2f %s", imageOffset.x, imageOffset.y, fontFileName.c_str()); std::string atlasName = tmp; auto it = _atlasMap.find(atlasName);