OSG(3.0.1)注释描述了字体搜索路径
/** Read a font from specified file. The filename may contain a path.
* It will search for the font file in the following places in this order:
* - In the current directory
* - All paths defined in OSG_FILE_PATH or OSGFILEPATH environment variable
* - Filename with path stripped: In the current directory
* - Filename with path stripped: All paths defined in OSG_FILE_PATH or OSGFILEPATH
*
* Then the file will be searched in OS specific directories in the following order:
* - Again in the current directory
* - Windows: In C:/winnt/fonts
* - Windows: In C:/windows/fonts
* - Windows: In the fonts directory of the windows install directory
* - Other OS: In /usr/share/fonts/ttf
* - Other OS: In /usr/share/fonts/ttf/western
* - Other OS: In /usr/share/fonts/ttf/decoratives
*
* If the given file could not be found, the path part will be stripped and
* the file will be searched again in the OS specific directories.
*/
extern OSGTEXT_EXPORT Font* readFontFile(const std::string& filename, const osgDB::Options* userOptions = 0);
调试OSG(3.0.1)代码,发现顺序是这样的
- 搜索OSG_FILE_PATH或OSGFILEPATH(看代码,文章后面贴了代码截图,是优先读取OSG_FILE_PATH,并且同OSGFILEPATH是互斥关系)
- 当前目录、C:/winnt/fonts、C:/windows/fonts
- 在字体名称前加上fonts文件夹,继续步骤1-2搜索,如fonts/xxx.ttf
环境变量OSG_FILE_PATH或OSGFILEPATH
void Registry::initDataFilePathList()
{
FilePathList filepath;
//
// set up data file paths
//
char *ptr;
if( (ptr = getenv( "OSG_FILE_PATH" )) )
{
//OSG_NOTIFY(DEBUG_INFO) << "OSG_FILE_PATH("<<ptr<<")"<<std::endl;
convertStringPathIntoFilePathList(ptr, filepath);
}
else if( (ptr = getenv( "OSGFILEPATH" )) )
{
//OSG_NOTIFY(DEBUG_INFO) << "OSGFILEPATH("<<ptr<<")"<<std::endl;
convertStringPathIntoFilePathList(ptr, filepath);
}
osgDB::appendPlatformSpecificResourceFilePaths(filepath);
setDataFilePathList(filepath);
}
实际需求
安装包指定目录下加载osg字体
解决方案
-
当前工作目录的说明
若没有显示调用SetCurrentDirectory或C语言中的chdir,则默认的工作目录会取决于用户启动进程的方式:快捷图标启动,当前工作目录就为其属性中的起始位置;直接双击EXE启动,当前工作目录则为EXE所在目录。 -
方案1
显示设置好当前工作目录,将字体放于此目录下 -
方案2
显示设置好当前工作目录,并在当前工作目录中创建fonts文件夹(windows下大小写不区分),将字体放于此目录下 -
方案3
在加载字体前,在进程环境变量OSG_FILE_PATH中追加自定义的目录,如d:\myAppData,然后将字体放于此目录下 -
方案4
在加载字体前,在进程环境变量OSG_FILE_PATH中追加自定义的目录,如d:\myAppData,然后在此目录下新建文件夹fonts(windows下大小写不区分),并将字体放于此目录下