Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cocos/2d/CCFontAtlas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
tempDef.height = tempDef.height / scaleFactor;
tempDef.U = tempDef.U / scaleFactor;
tempDef.V = tempDef.V / scaleFactor;
tempDef.rotated = false;
}
else{
delete[] bitmap;
Expand All @@ -451,6 +452,7 @@ bool FontAtlas::prepareLetterDefinitions(const std::u32string& utf32Text)
tempDef.offsetX = 0;
tempDef.offsetY = 0;
tempDef.textureID = 0;
tempDef.rotated = false;
_currentPageOrigX += 1;
}

Expand Down
1 change: 1 addition & 0 deletions cocos/2d/CCFontAtlas.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct FontLetterDefinition
int textureID;
bool validDefinition;
int xAdvance;
bool rotated;
};

class CC_DLL FontAtlas : public Ref
Expand Down
1 change: 1 addition & 0 deletions cocos/2d/CCFontCharMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ FontAtlas * FontCharMap::createFontAtlas()
tempDefinition.width = _itemWidth / contentScaleFactor;
tempDefinition.height = _itemHeight / contentScaleFactor;
tempDefinition.xAdvance = _itemWidth;
tempDefinition.rotated = false;

int charId = _mapStartChar;
for (int row = 0; row < itemsPerColumn; ++row)
Expand Down
97 changes: 2 additions & 95 deletions cocos/2d/CCFontFNT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
#include "renderer/CCTextureCache.h"

#include <cmath>
#include <set>
#include <unordered_map>

NS_CC_BEGIN

Expand All @@ -52,99 +50,6 @@ enum {

struct _FontDefHashElement;

/**
@struct BMFontDef
BMFont definition
*/
typedef struct _BMFontDef {
//! ID of the character
unsigned int charID;
//! origin and size of the font
Rect rect;
//! The X amount the image should be offset when drawing the image (in pixels)
short xOffset;
//! The Y amount the image should be offset when drawing the image (in pixels)
short yOffset;
//! The amount to move the current position after drawing the character (in pixels)
short xAdvance;
} BMFontDef;

/** @struct BMFontPadding
BMFont padding
@since v0.8.2
*/
typedef struct _BMFontPadding {
/// padding left
int left;
/// padding top
int top;
/// padding right
int right;
/// padding bottom
int bottom;
} BMFontPadding;

/** @brief BMFontConfiguration has parsed configuration of the .fnt file
@since v0.8
*/
class CC_DLL BMFontConfiguration : public Ref
{
// FIXME: Creating a public interface so that the bitmapFontArray[] is accessible
public://@public
// BMFont definitions
std::unordered_map<int /* key */, BMFontDef /* fontDef */> _fontDefDictionary;

//! FNTConfig: Common Height Should be signed (issue #1343)
int _commonHeight;
//! Padding
BMFontPadding _padding;
//! atlas name
std::string _atlasName;
//! values for kerning
std::unordered_map<uint64_t /* key */, int /* amount */> _kerningDictionary;

// Character Set defines the letters that actually exist in the font
std::set<unsigned int> *_characterSet;
//! Font Size
int _fontSize;
public:
/**
* @js ctor
*/
BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
virtual ~BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
std::string description() const;

/** allocates a BMFontConfiguration with a FNT file */
static BMFontConfiguration * create(const std::string& FNTfile);

/** initializes a BitmapFontConfiguration with a FNT file */
bool initWithFNTfile(const std::string& FNTfile);

const std::string& getAtlasName() { return _atlasName; }
void setAtlasName(const std::string& atlasName) { _atlasName = atlasName; }

std::set<unsigned int>* getCharacterSet() const;
private:
std::set<unsigned int>* parseConfigFile(const std::string& controlFile);
std::set<unsigned int>* parseBinaryConfigFile(unsigned char* pData, unsigned long size, const std::string& controlFile);
unsigned int parseCharacterDefinition(const char* line);
void parseInfoArguments(const char* line);
void parseCommonArguments(const char* line);
void parseImageFileName(const char* line, const std::string& fntFile);
void parseKerningEntry(const char* line);
void purgeKerningDictionary();
void purgeFontDefDictionary();
};

//
//FNTConfig Cache - free functions
//
Expand Down Expand Up @@ -748,6 +653,8 @@ FontAtlas * FontFNT::createFontAtlas()

tempDefinition.validDefinition = true;
tempDefinition.xAdvance = fontDef.xAdvance;
tempDefinition.rotated = false;

// add the new definition
if (65535 < fontDef.charID) {
CCLOGWARN("Warning: 65535 < fontDef.charID (%u), ignored", fontDef.charID);
Expand Down
107 changes: 102 additions & 5 deletions cocos/2d/CCFontFNT.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,106 @@
/// @cond DO_NOT_SHOW

#include "2d/CCFont.h"
#include <set>
#include <unordered_map>

NS_CC_BEGIN

class BMFontConfiguration;
/**
@struct BMFontDef
BMFont definition
*/
typedef struct _BMFontDef {
//! ID of the character
unsigned int charID;
//! origin and size of the font
Rect rect;
//! The X amount the image should be offset when drawing the image (in pixels)
short xOffset;
//! The Y amount the image should be offset when drawing the image (in pixels)
short yOffset;
//! The amount to move the current position after drawing the character (in pixels)
short xAdvance;
} BMFontDef;

/** @struct BMFontPadding
BMFont padding
@since v0.8.2
*/
typedef struct _BMFontPadding {
/// padding left
int left;
/// padding top
int top;
/// padding right
int right;
/// padding bottom
int bottom;
} BMFontPadding;

/** @brief BMFontConfiguration has parsed configuration of the .fnt file
@since v0.8
*/
class CC_DLL BMFontConfiguration : public Ref
{
// FIXME: Creating a public interface so that the bitmapFontArray[] is accessible
public://@public
// BMFont definitions
std::unordered_map<int /* key */, BMFontDef /* fontDef */> _fontDefDictionary;

//! FNTConfig: Common Height Should be signed (issue #1343)
int _commonHeight;
//! Padding
BMFontPadding _padding;
//! atlas name
std::string _atlasName;
//! values for kerning
std::unordered_map<uint64_t /* key */, int /* amount */> _kerningDictionary;

// Character Set defines the letters that actually exist in the font
std::set<unsigned int>* _characterSet;
//! Font Size
int _fontSize;
public:
/**
* @js ctor
*/
BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
virtual ~BMFontConfiguration();
/**
* @js NA
* @lua NA
*/
std::string description() const;

/** allocates a BMFontConfiguration with a FNT file */
static BMFontConfiguration* create(const std::string& FNTfile);

/** initializes a BitmapFontConfiguration with a FNT file */
bool initWithFNTfile(const std::string& FNTfile);

const std::string& getAtlasName() { return _atlasName; }
void setAtlasName(const std::string& atlasName) { _atlasName = atlasName; }

std::set<unsigned int>* getCharacterSet() const;

protected:
virtual std::set<unsigned int>* parseConfigFile(const std::string& controlFile);
virtual std::set<unsigned int>* parseBinaryConfigFile(unsigned char* pData, unsigned long size, const std::string& controlFile);

private:
unsigned int parseCharacterDefinition(const char* line);
void parseInfoArguments(const char* line);
void parseCommonArguments(const char* line);
void parseImageFileName(const char* line, const std::string& fntFile);
void parseKerningEntry(const char* line);
void purgeKerningDictionary();
void purgeFontDefDictionary();
};

class CC_DLL FontFNT : public Font
{
Expand All @@ -47,7 +143,10 @@ class CC_DLL FontFNT : public Font
static void purgeCachedData();
virtual int* getHorizontalKerningForTextUTF32(const std::u32string& text, int &outNumLetters) const override;
virtual FontAtlas *createFontAtlas() override;

void setFontSize(float fontSize);
float getFontSize() const { return _fontSize; }

int getOriginalFontSize()const;

static void reloadBMFontResource(const std::string& fntFilePath);
Expand All @@ -60,12 +159,10 @@ class CC_DLL FontFNT : public Font
* @lua NA
*/
virtual ~FontFNT();

private:


BMFontConfiguration* _configuration;
int getHorizontalKerningForChars(char32_t firstChar, char32_t secondChar) const;

BMFontConfiguration * _configuration;
Vec2 _imageOffset;
//User defined font size
float _fontSize;
Expand Down
6 changes: 3 additions & 3 deletions cocos/2d/CCLabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ void Label::updateLabelLetters()
}
else
{
letterSprite->setTextureRect(uvRect, false, uvRect.size);
letterSprite->setTextureRect(uvRect, letterDef.rotated, uvRect.size);
letterSprite->setTextureAtlas(_batchNodes.at(letterDef.textureID)->getTextureAtlas());
letterSprite->setAtlasIndex(_lettersInfo[letterIndex].atlasIndex);
}
Expand Down Expand Up @@ -971,7 +971,7 @@ bool Label::updateQuads()

if (_reusedRect.size.height > 0.f && _reusedRect.size.width > 0.f)
{
_reusedLetter->setTextureRect(_reusedRect, false, _reusedRect.size);
_reusedLetter->setTextureRect(_reusedRect, letterDef.rotated, _reusedRect.size);
float letterPositionX = _lettersInfo[ctr].positionX + _linesOffsetX[_lettersInfo[ctr].lineIndex];
_reusedLetter->setPosition(letterPositionX, py);
auto index = static_cast<int>(_batchNodes.at(letterDef.textureID)->getTextureAtlas()->getTotalQuads());
Expand Down Expand Up @@ -1799,7 +1799,7 @@ Sprite* Label::getLetter(int letterIndex)
else
{
this->updateBMFontScale();
letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect);
letter = LabelLetter::createWithTexture(_fontAtlas->getTexture(textureID), uvRect, letterDef.rotated);
letter->setTextureAtlas(_batchNodes.at(textureID)->getTextureAtlas());
letter->setAtlasIndex(letterInfo.atlasIndex);
auto px = letterInfo.positionX + _bmfontScale * uvRect.size.width / 2 + _linesOffsetX[letterInfo.lineIndex];
Expand Down