diff --git a/src/osgText/CMakeLists.txt b/src/osgText/CMakeLists.txt index a0f309733..b974179ae 100644 --- a/src/osgText/CMakeLists.txt +++ b/src/osgText/CMakeLists.txt @@ -1,3 +1,4 @@ +find_package(Fontconfig MODULE) IF(DYNAMIC_OPENSCENEGRAPH) ADD_DEFINITIONS(-DOSGTEXT_LIBRARY) @@ -47,5 +48,10 @@ SET(TARGET_LIBRARIES OpenThreads ) +if(Fontconfig_FOUND) + list(APPEND TARGET_LIBRARIES Fontconfig::Fontconfig) + ADD_DEFINITIONS(-DWITH_FONTCONFIG) +endif() + SETUP_LIBRARY(${LIB_NAME}) diff --git a/src/osgText/Font.cpp b/src/osgText/Font.cpp index 98d6fdaa3..7422e62d4 100644 --- a/src/osgText/Font.cpp +++ b/src/osgText/Font.cpp @@ -27,6 +27,10 @@ #include +#ifdef WITH_FONTCONFIG +#include +#endif + #include "DefaultFont.h" using namespace osgText; @@ -47,6 +51,34 @@ static OpenThreads::ReentrantMutex& getFontFileMutex() return s_FontFileMutex; } +#ifdef WITH_FONTCONFIG +static FcConfig* getFontConfig() +{ + static FcConfig* s_fontConfig = FcInitLoadConfigAndFonts(); + return s_fontConfig; +} + +static bool getFilePathFromFontconfig(const std::string &fontName, std::string &fontPath) +{ + FcPattern* pat = FcNameParse((FcChar8*)fontName.c_str()); + FcConfigSubstitute(getFontConfig(), pat, FcMatchPattern); + FcDefaultSubstitute(pat); + FcResult result = FcResultNoMatch; + FcPattern* font = FcFontMatch(getFontConfig(), pat, &result); + if (font) + { + FcChar8* file = NULL; + if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch) + { + fontPath = (char *)file; + } + FcPatternDestroy(font); + } + FcPatternDestroy(pat); + return result == FcResultMatch; +} +#endif + std::string osgText::findFontFile(const std::string& str) { // try looking in OSGFILEPATH etc first for fonts. @@ -78,11 +110,17 @@ std::string osgText::findFontFile(const std::string& str) s_FontFilePath); #else osgDB::convertStringPathIntoFilePathList( - ".:/usr/share/fonts/ttf:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives", + ".:/usr/share/fonts/ttf:/usr/share/fonts/truetype:/usr/share/fonts/ttf/western:/usr/share/fonts/ttf/decoratives", s_FontFilePath); #endif } +#ifdef WITH_FONTCONFIG + std::string fontPath; + if (getFilePathFromFontconfig(str, fontPath)) + return fontPath; +#endif + filename = osgDB::findFileInPath(str,s_FontFilePath); if (!filename.empty()) return filename;