2014-01-16 16:37:29 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-11-11 17:20:21 +08:00
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-27 13:39:50 +08:00
|
|
|
#include "2d/CCFontCharMap.h"
|
2014-05-01 10:09:13 +08:00
|
|
|
#include "2d/CCFontAtlas.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
FontCharMap* FontCharMap::create(std::string_view plistFile)
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string pathStr = FileUtils::getInstance()->fullPathForFilename(plistFile);
|
|
|
|
std::string relPathStr = pathStr.substr(0, pathStr.find_last_of('/')) + "/";
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2016-02-03 23:12:37 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(pathStr);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(dict["version"].asInt() == 1, "Unsupported version. Upgrade cocos2d version");
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
std::string textureFilename = relPathStr + dict["textureFilename"].asString();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
unsigned int width = dict["itemWidth"].asInt();
|
|
|
|
unsigned int height = dict["itemHeight"].asInt();
|
2020-08-18 11:33:18 +08:00
|
|
|
unsigned int startChar = dict["firstChar"].asInt();
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* tempTexture = Director::getInstance()->getTextureCache()->addImage(textureFilename);
|
2014-01-16 16:37:29 +08:00
|
|
|
if (!tempTexture)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
FontCharMap* tempFont = new FontCharMap(tempTexture, width, height, startChar);
|
|
|
|
|
2014-01-16 16:37:29 +08:00
|
|
|
if (!tempFont)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tempFont->autorelease();
|
|
|
|
return tempFont;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
FontCharMap* FontCharMap::create(std::string_view charMapFile, int itemWidth, int itemHeight, int startCharMap)
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* tempTexture = Director::getInstance()->getTextureCache()->addImage(charMapFile);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!tempTexture)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FontCharMap* tempFont = new FontCharMap(tempTexture, itemWidth, itemHeight, startCharMap);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!tempFont)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tempFont->autorelease();
|
|
|
|
return tempFont;
|
|
|
|
}
|
|
|
|
|
|
|
|
FontCharMap* FontCharMap::create(Texture2D* texture, int itemWidth, int itemHeight, int startCharMap)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
FontCharMap* tempFont = new FontCharMap(texture, itemWidth, itemHeight, startCharMap);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
if (!tempFont)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tempFont->autorelease();
|
|
|
|
return tempFont;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
FontCharMap::~FontCharMap() {}
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int* FontCharMap::getHorizontalKerningForTextUTF32(const std::u32string& /*text*/, int& /*outNumLetters*/) const
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2015-07-24 18:38:45 +08:00
|
|
|
return nullptr;
|
2014-01-16 16:37:29 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 17:20:21 +08:00
|
|
|
FontAtlas* FontCharMap::newFontAtlas()
|
2014-01-16 16:37:29 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
FontAtlas* tempAtlas = new FontAtlas(this);
|
2021-12-08 00:11:53 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 s = _texture->getContentSizeInPixels();
|
2014-01-16 16:37:29 +08:00
|
|
|
int itemsPerColumn = (int)(s.height / _itemHeight);
|
2021-12-25 10:04:45 +08:00
|
|
|
int itemsPerRow = (int)(s.width / _itemWidth);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2019-10-09 17:50:32 +08:00
|
|
|
tempAtlas->setLineHeight((float)_itemHeight);
|
2015-06-11 17:16:44 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
auto contentScaleFactor = AX_CONTENT_SCALE_FACTOR();
|
2015-06-11 17:16:44 +08:00
|
|
|
|
2014-01-16 16:37:29 +08:00
|
|
|
FontLetterDefinition tempDefinition;
|
2021-12-25 10:04:45 +08:00
|
|
|
tempDefinition.textureID = 0;
|
|
|
|
tempDefinition.offsetX = 0.0f;
|
|
|
|
tempDefinition.offsetY = 0.0f;
|
2014-01-16 16:37:29 +08:00
|
|
|
tempDefinition.validDefinition = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
tempDefinition.width = _itemWidth / contentScaleFactor;
|
|
|
|
tempDefinition.height = _itemHeight / contentScaleFactor;
|
|
|
|
tempDefinition.xAdvance = _itemWidth;
|
|
|
|
tempDefinition.rotated = false;
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
int charId = _mapStartChar;
|
|
|
|
for (int row = 0; row < itemsPerColumn; ++row)
|
|
|
|
{
|
|
|
|
for (int col = 0; col < itemsPerRow; ++col)
|
|
|
|
{
|
2015-06-11 17:16:44 +08:00
|
|
|
tempDefinition.U = _itemWidth * col / contentScaleFactor;
|
|
|
|
tempDefinition.V = _itemHeight * row / contentScaleFactor;
|
2014-01-16 16:37:29 +08:00
|
|
|
|
2015-07-24 18:38:45 +08:00
|
|
|
tempAtlas->addLetterDefinition(charId, tempDefinition);
|
2014-01-16 16:37:29 +08:00
|
|
|
charId++;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-06-13 01:44:31 +08:00
|
|
|
tempAtlas->setTexture(0, _texture);
|
2014-01-16 16:37:29 +08:00
|
|
|
|
|
|
|
return tempAtlas;
|
|
|
|
}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|