Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How to get glyph coordinates from text  (Read 1222 times)

0 Members and 1 Guest are viewing this topic.

gokaysatir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
How to get glyph coordinates from text
« on: December 20, 2014, 01:10:31 pm »
Hi :),

      I have a question=)

 First, i want to get text as polygons, then i am going to tessellate those polygons. I need this type of approach because i am working on a CAD editor as a hobby, which needs text objects as polygons.

 With below code, i can get the contours of letters. Then i use contour coordinates for creating tessellated polygons.

My question is, is there a SFML way to get these tessellated polygons, or just contours?

                       

std::string::iterator end_it = utf8::find_invalid(mytextobj->text.begin(), mytextobj->text.end());

if(end_it != mytextobj->text.end()){
     std::cout << mytextobj->text<< "not a utf8 text\n";
     mytextobj->text = "???";
}

std::vector<unsigned int> utf32line;

utf8::utf8to32(mytextobj->text.begin(), end_it, std::back_inserter(utf32line));

filename = "c:/fonts/verdana.ttf";
unsigned int h = 12;

FT_Library library;
if(FT_Init_FreeType(&library))
      throw std::runtime_error("Freetype font error.");
FT_Face face;
if(FT_New_Face(library, filename.c_str(), 0, &face))
      throw std::runtime_error("file load error.");

FT_Select_Charmap(face, ft_encoding_unicode);

FT_Set_Char_Size(face, h * 64, h * 64, 1920, 1080);

double x_counter = 0;

for(unsigned int i = 0; i < utf32line.size(); i++){

     if(FT_Load_Glyph(face, FT_Get_Char_Index(face, utf32line[i]), FT_LOAD_DEFAULT )){
          throw std::runtime_error("load glyph not working./n");}

     FT_Glyph glyph;
     
     if(FT_Get_Glyph(face->glyph, &glyph))
          throw std::runtime_error("Glyph error.");

     std::vector<Point> pv;

     FT_GlyphSlot g = face->glyph;

     unsigned int c1 = 0, c2 = 0;

      if (utf32line.at(i) != ' ' && utf32line.at(i) != '\t'){
           for(c1 = 0; c1 <= g->outline.contours[g->outline.n_contours - 1]; c1++){
               pv.push_back(Point(x_counter + ((double)(g->outline.points[c1].x)), mytextobj->koordinat.y + ((double)(g->outline.points[c1].y)), mytextobj->koordinat.z));
         
               if(c1 == g->outline.contours[c2]){
                    c2++;
                    mytextobj->pvs.push_back(pv);
                    pv.clear();
               }
          }
     }
     else{
          x_counter += g->advance.x;
     }
     FT_BBox abbox;
     FT_Glyph_Get_CBox(glyph, FT_GLYPH_BBOX_UNSCALED, &abbox);
     x_counter += abbox.xMax;

     FT_Done_Glyph(glyph);

}
FT_Done_Face(face);

FT_Done_FreeType(library);
« Last Edit: December 20, 2014, 04:36:11 pm by gokaysatir »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: How to get glyph coordinates from text
« Reply #1 on: December 20, 2014, 02:25:12 pm »
No, SFML only draws text. Using lower-level libraries like you do is the way to go.
Laurent Gomila - SFML developer

gokaysatir

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: How to get glyph coordinates from text
« Reply #2 on: December 20, 2014, 02:32:55 pm »
OK, thanks for reply :)
« Last Edit: December 20, 2014, 03:19:15 pm by gokaysatir »

 

anything