CCFontCharMap.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. Copyright (c) 2013 Zynga Inc.
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "2d/CCFontCharMap.h"
  23. #include "2d/CCFontAtlas.h"
  24. #include "platform/CCFileUtils.h"
  25. #include "base/CCDirector.h"
  26. #include "renderer/CCTextureCache.h"
  27. NS_CC_BEGIN
  28. FontCharMap * FontCharMap::create(const std::string& plistFile)
  29. {
  30. std::string pathStr = FileUtils::getInstance()->fullPathForFilename(plistFile);
  31. std::string relPathStr = pathStr.substr(0, pathStr.find_last_of("/"))+"/";
  32. ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
  33. CCASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");
  34. std::string textureFilename = relPathStr + dict["textureFilename"].asString();
  35. unsigned int width = dict["itemWidth"].asInt();
  36. unsigned int height = dict["itemHeight"].asInt();
  37. unsigned int startChar = dict["firstChar"].asInt();
  38. Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(textureFilename);
  39. if (!tempTexture)
  40. {
  41. return nullptr;
  42. }
  43. FontCharMap *tempFont = new FontCharMap(tempTexture,width,height,startChar);
  44. if (!tempFont)
  45. {
  46. return nullptr;
  47. }
  48. tempFont->autorelease();
  49. return tempFont;
  50. }
  51. FontCharMap* FontCharMap::create(const std::string& charMapFile, int itemWidth, int itemHeight, int startCharMap)
  52. {
  53. Texture2D *tempTexture = Director::getInstance()->getTextureCache()->addImage(charMapFile);
  54. if (!tempTexture)
  55. {
  56. return nullptr;
  57. }
  58. FontCharMap *tempFont = new FontCharMap(tempTexture,itemWidth,itemHeight,startCharMap);
  59. if (!tempFont)
  60. {
  61. return nullptr;
  62. }
  63. tempFont->autorelease();
  64. return tempFont;
  65. }
  66. FontCharMap* FontCharMap::create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
  67. {
  68. FontCharMap *tempFont = new FontCharMap(texture,itemWidth,itemHeight,startCharMap);
  69. if (!tempFont)
  70. {
  71. return nullptr;
  72. }
  73. tempFont->autorelease();
  74. return tempFont;
  75. }
  76. FontCharMap::~FontCharMap()
  77. {
  78. }
  79. int* FontCharMap::getHorizontalKerningForTextUTF32(const std::u32string& /*text*/, int & /*outNumLetters*/) const
  80. {
  81. return nullptr;
  82. }
  83. FontAtlas * FontCharMap::createFontAtlas()
  84. {
  85. FontAtlas *tempAtlas = new (std::nothrow) FontAtlas(*this);
  86. if (!tempAtlas)
  87. return nullptr;
  88. Size s = _texture->getContentSizeInPixels();
  89. int itemsPerColumn = (int)(s.height / _itemHeight);
  90. int itemsPerRow = (int)(s.width / _itemWidth);
  91. tempAtlas->setLineHeight(_itemHeight);
  92. auto contentScaleFactor = CC_CONTENT_SCALE_FACTOR();
  93. FontLetterDefinition tempDefinition;
  94. tempDefinition.textureID = 0;
  95. tempDefinition.offsetX = 0.0f;
  96. tempDefinition.offsetY = 0.0f;
  97. tempDefinition.validDefinition = true;
  98. tempDefinition.width = _itemWidth / contentScaleFactor;
  99. tempDefinition.height = _itemHeight / contentScaleFactor;
  100. tempDefinition.xAdvance = _itemWidth;
  101. int charId = _mapStartChar;
  102. for (int row = 0; row < itemsPerColumn; ++row)
  103. {
  104. for (int col = 0; col < itemsPerRow; ++col)
  105. {
  106. tempDefinition.U = _itemWidth * col / contentScaleFactor;
  107. tempDefinition.V = _itemHeight * row / contentScaleFactor;
  108. tempAtlas->addLetterDefinition(charId, tempDefinition);
  109. charId++;
  110. }
  111. }
  112. tempAtlas->addTexture(_texture,0);
  113. return tempAtlas;
  114. }
  115. NS_CC_END