OSG笔记:osgText::Font搜索路径及开发中的应用

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)代码,发现顺序是这样的

  1. 搜索OSG_FILE_PATH或OSGFILEPATH(看代码,文章后面贴了代码截图,是优先读取OSG_FILE_PATH,并且同OSGFILEPATH是互斥关系)
  2. 当前目录、C:/winnt/fonts、C:/windows/fonts
  3. 在字体名称前加上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下大小写不区分),并将字体放于此目录下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值