Skip to content

Commit

Permalink
src:improve decoder
Browse files Browse the repository at this point in the history
Signed-off-by: xiaoming <[email protected]>
  • Loading branch information
QQxiaoming committed Jan 3, 2023
1 parent 25e1bc9 commit 4fa2c5b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/TTFdecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ QList<ImageDecoder::SvgInfo> ImageDecoder::ttf_verbose(const QString &ttffilenam

std::string ImageDecoder::ttf_glyph_out(void *glyph, bool verbose) {
font2svg::glyph *g = static_cast<font2svg::glyph *>(glyph);
if(verbose)
if(verbose)
return g->svgheader() +
g->svgborder() +
g->svgtransform() +
Expand All @@ -43,7 +43,7 @@ std::string ImageDecoder::ttf_glyph_out(void *glyph, bool verbose) {
g->outline() +
g->labelpts() +
g->svgfooter();
else
else
return g->svgheader() +
g->svgtransform() +
g->outline() +
Expand All @@ -56,7 +56,7 @@ QList<ImageDecoder::SvgInfo> ImageDecoder::ttf_decode(QString ttffilename,int W,
if(codepoint == -1) {
font2svg::ttf_file tfile(ttffilename.toStdString());
for(int index = 0;index<=0x10FFFF;index++) {
font2svg::glyph g(tfile, index);
font2svg::glyph g(tfile, index, false);
if(g.glyph_index != 0) {
SvgInfo info;
info.src = new QString(QString::fromStdString(ttf_glyph_out(&g,verbose)));
Expand All @@ -68,7 +68,7 @@ QList<ImageDecoder::SvgInfo> ImageDecoder::ttf_decode(QString ttffilename,int W,
}
tfile.free();
} else {
font2svg::glyph g(ttffilename.toStdString(), codepoint );
font2svg::glyph g(ttffilename.toStdString(), codepoint, true );
SvgInfo info;
info.src = new QString(QString::fromStdString(ttf_glyph_out(&g,verbose)));
info.H = H;
Expand Down
38 changes: 21 additions & 17 deletions src/font_to_svg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ endorsement.

namespace font2svg {

std::stringstream debug;

#if 0
#define DEBUG_OUT std::cout
#else
Expand Down Expand Up @@ -84,6 +82,7 @@ class ttf_file

ttf_file( std::string fname )
{
std::stringstream debug;
filename = fname;
error = FT_Init_FreeType( &library );
debug << "Init error code: " << error;
Expand All @@ -93,23 +92,27 @@ class ttf_file
debug << "\nFace load error code: " << error;
debug << "\nfont filename: " << filename;
if (error) {
DEBUG_OUT << debug.str();
std::cerr << "problem loading file " << filename << "\n";
exit(1);
}
debug << "\nFamily Name: " << face->family_name;
debug << "\nStyle Name: " << face->style_name;
debug << "\nNumber of faces: " << face->num_faces;
debug << "\nNumber of glyphs: " << face->num_glyphs;
DEBUG_OUT << debug.str();
}

void free()
{
std::stringstream debug;
debug << "\n<!--";
error = FT_Done_Face( face );
debug << "\nFree face. error code: " << error;
error = FT_Done_FreeType( library );
debug << "\nFree library. error code: " << error;
debug << "\n-->\n";
DEBUG_OUT << debug.str();
}
};

Expand Down Expand Up @@ -240,60 +243,61 @@ class glyph
std::stringstream debug, tmp;
int bbwidth, bbheight;

glyph( ttf_file &f, std::string unicode_str )
glyph( ttf_file &f, std::string unicode_str, bool force )
{
file = f;
init( unicode_str );
init( unicode_str, force );
}

glyph( const char * filename, std::string unicode_str )
glyph( const char * filename, std::string unicode_str, bool force )
{
this->file = ttf_file( std::string(filename) );
init( unicode_str );
init( unicode_str, force );
}

glyph( const char * filename, const char * unicode_c_str )
glyph( const char * filename, const char * unicode_c_str, bool force )
{
this->file = ttf_file( std::string(filename) );
init( std::string(unicode_c_str) );
init( std::string(unicode_c_str), force );
}

glyph( ttf_file &f, int unicode )
glyph( ttf_file &f, int unicode, bool force )
{
file = f;
init( unicode );
init( unicode, force );
}

glyph( const char * filename, int unicode )
glyph( const char * filename, int unicode, bool force )
{
this->file = ttf_file( std::string(filename) );
init( unicode );
init( unicode, force );
}

glyph( std::string fname, int unicode )
glyph( std::string fname, int unicode, bool force )
{
this->file = ttf_file( fname );
init( unicode );
init( unicode, force );
}

void free()
{
file.free();
}

void init( std::string unicode_s )
void init( std::string unicode_s, bool force )
{
int unicode = strtol( unicode_s.c_str() , NULL, 0 );
debug << "<!--\nUnicode requested: " << unicode_s;
init(unicode);
init(unicode,force);
}

void init( int unicode )
void init( int unicode, bool force )
{
face = file.face;
codepoint = unicode;
// Load the Glyph into the face's Glyph Slot + print details
glyph_index = FT_Get_Char_Index( face, codepoint );
if(glyph_index == 0 && unicode != 0 && (!force)) return;
debug << " (decimal: " << codepoint << " hex: 0x"
<< std::hex << codepoint << std::dec << ")";
debug << "\nGlyph index for unicode: " << glyph_index;
Expand Down

0 comments on commit 4fa2c5b

Please sign in to comment.