• 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);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    调试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("<
            convertStringPathIntoFilePathList(ptr, filepath);
        }
        else if( (ptr = getenv( "OSGFILEPATH" )) )
        {
            //OSG_NOTIFY(DEBUG_INFO) << "OSGFILEPATH("<
            convertStringPathIntoFilePathList(ptr, filepath);
        }
    
        osgDB::appendPlatformSpecificResourceFilePaths(filepath);
        setDataFilePathList(filepath);
        
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    实际需求

    安装包指定目录下加载osg字体

    解决方案

    • 当前工作目录的说明
      若没有显示调用SetCurrentDirectory或C语言中的chdir,则默认的工作目录会取决于用户启动进程的方式:快捷图标启动,当前工作目录就为其属性中的起始位置;直接双击EXE启动,当前工作目录则为EXE所在目录。

    • 方案1
      显示设置好当前工作目录,将字体放于此目录下

    • 方案2
      显示设置好当前工作目录,并在当前工作目录中创建fonts文件夹(windows下大小写不区分),将字体放于此目录下

    • 方案3
      在加载字体前,在进程环境变量OSG_FILE_PATH中追加自定义的目录,如d:\myAppData,然后将字体放于此目录下

    • 方案4
      在加载字体前,在进程环境变量OSG_FILE_PATH中追加自定义的目录,如d:\myAppData,然后在此目录下新建文件夹fonts(windows下大小写不区分),并将字体放于此目录下

  • 相关阅读:
    uniapp项目实践总结(二十七)苹果应用商店上架教程
    基于web的招标投标系统的设计与实现-计算机毕业设计源码+LW文档
    BI业务用户商业分析新时代,如何把数据用透?
    MySQL的安装教程(嗷嗷详细,包教包会~)
    【大家的项目】通用规则引擎——Rush(一)可以自定义的规则引擎,告别发版,快速配置...
    matlab 使用 audioread 、 sound 读取和播放 wav 文件
    超大模型工程化实践打磨,百度智能云发布云原生AI 2.0方案
    ImageDecoder解码GIF、ImageView显示GIF
    Python编程基础:实验3——字典及集合的使用
    【RTOS学习】同步与互斥 | 队列
  • 原文地址:https://blog.csdn.net/s634772208/article/details/126706283