mirror of
https://github.com/enduro2d/enduro2d.git
synced 2025-12-15 00:11:55 +07:00
clean code
This commit is contained in:
@@ -8,21 +8,12 @@
|
||||
|
||||
#include "_high.hpp"
|
||||
|
||||
//#include "assets/texture_asset.hpp"
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
class bmfont;
|
||||
|
||||
using bmfont_ptr = std::shared_ptr<bmfont>;
|
||||
|
||||
class bad_bmfont_access final : public exception {
|
||||
public:
|
||||
const char* what() const noexcept final {
|
||||
return "bad bmfont access";
|
||||
}
|
||||
};
|
||||
|
||||
class bmfont final {
|
||||
public:
|
||||
struct char_data {
|
||||
@@ -36,11 +27,6 @@ namespace e2d
|
||||
};
|
||||
|
||||
struct info_data {
|
||||
struct spacing_data {
|
||||
i32 horizontal;
|
||||
i32 vertical;
|
||||
};
|
||||
|
||||
struct padding_data {
|
||||
i16 up;
|
||||
i16 right;
|
||||
@@ -48,49 +34,31 @@ namespace e2d
|
||||
i16 left;
|
||||
};
|
||||
|
||||
// str face; //This is the name of the true type font.
|
||||
u32 size; //The size of the true type font.
|
||||
// bool bold; //The font is bold.
|
||||
// bool italic; //The font is italic.
|
||||
// str charset; //The name of the OEM charset used (when not unicode).
|
||||
// bool unicode; //Set to 1 if it is the unicode charset.
|
||||
// u32 stretchH; //The font height stretch in percentage. 100% means no stretch.
|
||||
// bool smooth; //Set to 1 if smoothing was turned on.
|
||||
// u32 aa; //The supersampling level used. 1 means no supersampling was used.
|
||||
padding_data padding; //The padding for each character (up, right, down, left). TODOPA
|
||||
// spacing_data spacing; //The spacing for each character (horizontal, vertical).
|
||||
// u32 outline; //The outline thickness for the characters.
|
||||
u32 size;
|
||||
padding_data padding;
|
||||
};
|
||||
|
||||
struct kerning_data {
|
||||
u32 first; // The first character id.
|
||||
u32 second; // The second character id.
|
||||
i32 amount; // How much the x position should be adjusted when drawing the second character immediately following the first.
|
||||
u32 first;
|
||||
u32 second;
|
||||
i32 amount;
|
||||
};
|
||||
|
||||
struct common_data {
|
||||
u32 lineHeight; //This is the distance in pixels between each line of text.
|
||||
// u32 base; //The number of pixels from the absolute top of the line to the base of the characters.
|
||||
// u32 scaleW; //The width of the texture, normally used to scale the x pos of the character image.
|
||||
// u32 scaleH; //The height of the texture, normally used to scale the y pos of the character image.
|
||||
u32 pages; //The number of texture pages included in the font.
|
||||
// u32 packed; //Set to 1 if the monochrome characters have been packed into each of the texture channels. In this case alphaChnl describes what is stored in each channel.
|
||||
// u32 alphaChnl; //Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one.
|
||||
// u32 redChnl; //Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one.
|
||||
// u32 greenChnl; //Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one.
|
||||
// u32 blueChnl; //Set to 0 if the channel holds the glyph data, 1 if it holds the outline, 2 if it holds the glyph and the outline, 3 if its set to zero, and 4 if its set to one.
|
||||
u32 line_height;
|
||||
u32 pages;
|
||||
};
|
||||
|
||||
bmfont() = default;
|
||||
~bmfont() noexcept = default;
|
||||
|
||||
static bmfont_ptr create (str_view content);
|
||||
static bmfont_ptr create(str_view content);
|
||||
|
||||
protected:
|
||||
private:
|
||||
info_data info_;
|
||||
common_data common_;
|
||||
std::vector<std::string> pages_;
|
||||
std::vector<char_data> chars_;
|
||||
std::vector<kerning_data> kerning_;
|
||||
vector<str> pages_;
|
||||
vector<char_data> chars_;
|
||||
vector<kerning_data> kerning_;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -5,22 +5,21 @@
|
||||
******************************************************************************/
|
||||
|
||||
#include <enduro2d/high/bmfont.hpp>
|
||||
//#include <regex>
|
||||
#include <sstream>
|
||||
|
||||
namespace e2d
|
||||
{
|
||||
bmfont_ptr bmfont::create (str_view content) {
|
||||
bmfont_ptr b = std::make_shared<bmfont>();
|
||||
std::string s{content.data()};
|
||||
str s{content.data()};
|
||||
std::replace(s.begin(), s.end(), '=', ' ');
|
||||
std::replace(s.begin(), s.end(), ',', ' ');
|
||||
|
||||
std::string tag;
|
||||
std::string line;
|
||||
str tag;
|
||||
str line;
|
||||
std::stringstream data_stream(s);
|
||||
std::vector<char_data> chars(120);
|
||||
std::vector<kerning_data> kerning(120);
|
||||
vector<char_data> chars(120);
|
||||
vector<kerning_data> kerning(120);
|
||||
int chars_counter = 0;
|
||||
int kerning_counter = 0;
|
||||
while (getline(data_stream, line, '\n'))
|
||||
@@ -39,34 +38,6 @@ namespace e2d
|
||||
line_stream >> b->info_.padding.down;
|
||||
line_stream >> b->info_.padding.left;
|
||||
}
|
||||
|
||||
// if (tag == "face") {
|
||||
// line_stream >> b->info_.face;
|
||||
// } else if (tag == "size") {
|
||||
// line_stream >> b->info_.size;
|
||||
// } else if (tag == "bold") {
|
||||
// line_stream >> b->info_.bold;
|
||||
// } else if (tag == "italic") {
|
||||
// line_stream >> b->info_.italic;
|
||||
// } else if (tag == "charset") {
|
||||
// line_stream >> b->info_.charset;
|
||||
// } else if (tag == "unicode") {
|
||||
// line_stream >> b->info_.unicode;
|
||||
// } else if (tag == "stretchH") {
|
||||
// line_stream >> b->info_.stretchH;
|
||||
// } else if (tag == "smooth") {
|
||||
// line_stream >> b->info_.smooth;
|
||||
// } else if (tag == "aa") {
|
||||
// line_stream >> b->info_.aa;
|
||||
// } else if (tag == "padding") {
|
||||
// line_stream >> b->info_.padding.up
|
||||
// >> b->info_.padding.right
|
||||
// >> b->info_.padding.down
|
||||
// >> b->info_.padding.left;
|
||||
// } else if (tag == "spacing") {
|
||||
// line_stream >> b->info_.spacing.horizontal
|
||||
// >> b->info_.spacing.vertical;
|
||||
// }
|
||||
line_stream >> tag;
|
||||
}
|
||||
} else if (tag == "common") {
|
||||
@@ -74,7 +45,7 @@ namespace e2d
|
||||
line_stream >> tag;
|
||||
while (!line_stream.eof()) {
|
||||
if (tag == "lineHeight") {
|
||||
line_stream >> b->common_.lineHeight;
|
||||
line_stream >> b->common_.line_height;
|
||||
} else if (tag == "pages") {
|
||||
line_stream >> b->common_.pages;
|
||||
b->pages_.resize( b->common_.pages);
|
||||
@@ -89,7 +60,7 @@ namespace e2d
|
||||
if (tag == "id") {
|
||||
line_stream >> id;
|
||||
} else if (tag == "file") {
|
||||
std::string file;
|
||||
str file;
|
||||
line_stream >> file;
|
||||
//remove ""
|
||||
file.erase(file.begin());
|
||||
@@ -114,11 +85,10 @@ namespace e2d
|
||||
} else if (tag == "char") {
|
||||
//char id=123 x=2 y=2 width=38 height=54 xoffset=0 yoffset=-3 xadvance=12 page=0 chnl=0 letter="{"
|
||||
line_stream >> tag;
|
||||
i32 id{-1};
|
||||
char_data c;
|
||||
char_data& c = chars[chars_counter++];
|
||||
while (!line_stream.eof()) {
|
||||
if (tag == "id") {
|
||||
line_stream >> id;
|
||||
line_stream >> c.id;
|
||||
} else if (tag == "x") {
|
||||
line_stream >> c.rect.position.x;
|
||||
} else if (tag == "y") {
|
||||
@@ -127,6 +97,7 @@ namespace e2d
|
||||
line_stream >> c.rect.size.x;
|
||||
} else if (tag == "height") {
|
||||
line_stream >> c.rect.size.y;
|
||||
c.rect.position.y -= c.rect.size.y;
|
||||
} else if (tag == "xoffset") {
|
||||
line_stream >> c.xoffset;
|
||||
} else if (tag == "yoffset") {
|
||||
@@ -138,7 +109,6 @@ namespace e2d
|
||||
}
|
||||
line_stream >> tag;
|
||||
}
|
||||
chars[chars_counter++] = c;
|
||||
} else if (tag == "kernings") {
|
||||
//kernings count=243
|
||||
//this field may not exist!
|
||||
@@ -155,7 +125,7 @@ namespace e2d
|
||||
} else if (tag == "kerning") {
|
||||
//kerning first=86 second=101 amount=-2
|
||||
line_stream >> tag;
|
||||
kerning_data k;
|
||||
kerning_data& k = kerning[kerning_counter++];
|
||||
while (!line_stream.eof()) {
|
||||
if (tag == "first") {
|
||||
line_stream >> k.first;
|
||||
@@ -166,7 +136,6 @@ namespace e2d
|
||||
}
|
||||
line_stream >> tag;
|
||||
}
|
||||
kerning[kerning_counter++] = k;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user