cheshirekow  v0.1.0
C++ Bindings for fontconfig

cpp_fontconfig is a very thin c++ wrapper around libfontconfig. It wraps only the public API and takes advantage of the fact that most of the library exposes opaque pointers.

Most of the classes in cpp_fontconfig are wrappers around these opaque pointers. As such, you should treat objects of these classes as if they were, in fact, pointers themselves. It is probably better to pass around copies and/or references to these objects rather than pointers to these objects.

As a simple demonstration, here is a program which will output a system font file which matches a requested family name.

/*
* Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
*
* This file is part of cppfontconfig.
*
* cppfontconfig 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.
*
* cppfontconfig 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 cppfontconfig. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
int main( int argc, char** argv )
{
namespace fc=fontconfig;
using namespace fontconfig;
// we'll use the second argument as the font name
if( argc < 2 )
{
std::cerr << "usage: tutorial [Font Name]" << std::endl;
return 1;
}
// initialize font config
init();
// we put this in a separate block because we want the pattern objects
// to destruct before we call fini... otherwise (and I'm not sure but)
// it might be possible that the patterns will try to free memory that
// fontconfig free's on fini
{
// create a pattern to search for
// type safe version, but will not give compiler error if the
// argument is the wrong type for the key
//pat.add(FAMILY, (Char8_t*)argv[1]);
// type safe but inextensible version that works only on built in
// types, will give a compiler error if the parameter is not the
// right type for the key
pat->addBuiltIn<key::FAMILY>( (const Char8_t*)argv[1] );
// get a pointer to the default configuration
// perform substitutions
pat->substitute(match::Pattern);
pat->defaultSubstitute();
// get the match
Result_t result;
RefPtr<Pattern> match = config->fontMatch(pat, result);
// get the closest matching font file
Char8_t* file;
int index;
// we should have a better get/add interface... this isn't very
// c++-y
match->get( fc::FILE, 0, file);
match->get( fc::INDEX, 0, index);
// at this point, we probably want to use freetype to get a face
// that we can use in our application, but since this is just a
// demo, we'll print the font file and quit
std::cout << "Font found for query [" << argv[1] << "] at "
<< file << "\n" << std::endl;
// demo the interface for ObjectTypeList
("ObjectA", type::Integer )
("ObjectB", type::String )
("ObjectC", type::Double )
();
std::cout << "number of items in the list: "
<< oList.get_nItems() << std::endl;
// demo the interface for ConstantList
((const Char8_t*)"NameA", "ObjectA", 1001 )
((const Char8_t*)"NameB", "ObjectB", 1002 )
((const Char8_t*)"NameC", "ObjectC", 1003 )
();
std::cout << "number of items in the list: "
<< cList.get_nItems() << std::endl;
// no need to do cleanup, pattern and match will free their memory
// when their destructors are called
}
// unload fontconfig
fini();
return 0;
}

Running the tutorial application yields the following:

user@machine:~/Codes/cpp/builds/fontconfig/test/tutorial$ ./tutorial Ubuntu
Font found for query [Ubuntu] at /usr/share/fonts/truetype/ubuntu-font-family/Ubuntu-R.ttf

user@machine:~/Codes/cpp/builds/fontconfig/test/tutorial$ ./tutorial UbuntuMono
Font found for query [UbuntuMono] at /usr/share/fonts/truetype/ubuntu-font-family/UbuntuMono-R.ttf

user@machine:~/Codes/cpp/builds/fontconfig/test/tutorial$ ./tutorial Times
Font found for query [Times] at /usr/share/fonts/X11/Type1/n021003l.pfb

user@machine:~/Codes/cpp/builds/fontconfig/test/tutorial$ ./tutorial Arial
Font found for query [Arial] at /usr/share/fonts/truetype/msttcorefonts/Arial.ttf

user@machine:~/Codes/cpp/builds/fontconfig/test/tutorial$ ./tutorial Sans
Font found for query [Sans] at /usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf