cheshirekow  v0.1.0
C++ Bindings for freetype

cpp_freetype is a C++ wrapper around the FreeType2 library. It is a simple, thin wrapper that doesn't do much on it's own. I wouldn't consider it very useful except to vain C++ programs who want the font config objects to be nicely namespaced and to eliminate the FT_ macros from their code.

cpp_freetype mostly just provides objects which wrap freetype pointers with the methods of the underlying object.

Note-to-self: You probably want to link against this library using Link Time Optimization (LTO) (i.e. the -flto switch with gcc). It can inline functions that are declared in this library in the code that uses it.

As an example, here is a simple program which loads a font file and displays some information from it, and then laods the outline for the character 'A' and prints the list of path segments:

/*
* Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
*
* This file is part of cppfreetype.
*
* cppfreetype is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cppfreetype is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Fontconfigmm. If not, see <http://www.gnu.org/licenses/>.
*/
#include <sigc++/sigc++.h>
#include <iostream>
int main( int argc, char** argv )
{
using namespace freetype;
if(argc < 2 )
{
std::cerr << "Provide font file path as parameter please" << std::endl;
return 1;
}
Error result=1;
RefPtr<Library> freetype;
(freetype,result) = init_e();
if(result)
{
std::cerr << "Failed to initialize freetype: "
<< result
<< std::endl;
return 1;
}
// artificial scope... we dont want a dangling Face pointer hanging
// around trying to free itself after we call ft::done
{
(face,result) = freetype->new_face_e( argv[1], 0 );
if(result)
{
std::cerr << "Freetype failed to read " << argv[1]
<< " as a font file " << std::endl;
return 1;
}
std::cout << "Some info about the font: "
<< "\n filepath: " << argv[1]
<< "\n family: " << face->family_name()
<< "\n style: " << face->style_name()
<< "\n n fixed size: " << face->num_fixed_sizes()
<< "\n n charmaps: " << face->num_charmaps()
<< "\n scalable: " << (face->is_scalable() ? "yes" : "no")
<< "\n n glyphs: " << face->num_glyphs()
<< "\n units per EM: " << face->units_per_EM()
<< "\n charmaps: ";
for(int i=0; i < face->num_charmaps(); i++)
{
Untag untag = (*face)->charmaps[i]->encoding;
std::cout
<< "\n " << untag;
}
std::cout
<< "\n"
<< std::endl;
result = face->select_charmap( encoding::UNICODE );
std::cout << "Set charmap to index: "
<< FT_Get_Charmap_Index( (*face)->charmap )
<< std::endl;
result = face->set_char_size(
0, // char_width in 1/64th of points
16*64, // char height in 1/64th of points
300, // horizontal device resolution
300 ); // vertical device resolution
char theChar = 'A';
UInt charIndex = face->get_char_index(theChar);
result = face->load_char(theChar, load::NO_BITMAP | load::NO_SCALE);
ULong glyphFormat = face->glyph()->format();
std::cout << "for char " << theChar << " :"
<< "\n ascii: " << (int)theChar
<< "\n index: " << charIndex
<< "\n format: " << (char)( (glyphFormat >> 24 ) & 0xff )
<< (char)( (glyphFormat >> 16 ) & 0xff )
<< (char)( (glyphFormat >> 8 ) & 0xff )
<< (char)( (glyphFormat >> 0 ) & 0xff )
<< "\n contours: " << face->glyph()->outline()->n_contours()
<< "\n points: " << face->glyph()->outline()->n_points()
<< std::endl;
ContourIterator ic = face->glyph()->outline()->begin();
int i=0;
for(; !ic.done(); ++ic)
{
std::cout << "\n\nContour: " << i++ << "\n";
for( ip = ic->begin(); ip != ic->end(); ++ip )
{
std::cout << "\n ("
<< ip->x() << ","
<< ip->y() << ") "
<< ( ip->on() ? "on" : "off") << " ";
if( ip->off() )
std::cout << (ip->cubic() ? "cubic" : "quadradic");
}
}
}
result = done(freetype);
return 0;
}

The output of this program looks like the following

user@machine:~/path/to$ ./test /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf
Some info about the font:
      filepath: /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf
        family: Ubuntu
         style: Regular
  n fixed size: 0
    n charmaps: 3
      scalable: yes
      n glyphs: 1264
  units per EM: 1000
      charmaps:
                unic
                armn
                unic

Set charmap to index: 2
for char A :
    ascii: 65
    index: 36
   format: outl
 contours: 2
   points: 25


Contour: 0

   (549,0)  on
   (532,45)  off  quadradic
   (502,132)  off  quadradic
   (486,177)  on
   (172,177)  on
   (109,0)  on
   (8,0)  on
   (48,110)  off  quadradic
   (118,297)  off  quadradic
   (185,465)  off  quadradic
   (251,618)  off  quadradic
   (287,693)  on
   (376,693)  on
   (412,618)  off  quadradic
   (478,465)  off  quadradic
   (545,297)  off  quadradic
   (615,110)  off  quadradic

Contour: 1

   (458,257)  on
   (426,344)  off  quadradic
   (363,507)  off  quadradic
   (329,582)  on
   (294,507)  off  quadradic


josh@Nadie:~/Codes/cpp/builds/cppfreetype/test$