• 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下大小写不区分),并将字体放于此目录下

  • 相关阅读:
    python 词云(Word Cloud)设计
    linux系统中日志简介
    微信小程序疑难杂症---修改数组里的某个属性的值
    企业级通用低代码开发平台——一二三应用开发平台发布4.2开源版本,回顾与展望
    Mac os通过dmg安装docker以后在终端中使用 docker -v docker --version等找不到docker命令解决
    「SpringBoot」06 单元测试
    14 结构性模式-适配器模式
    力扣刷题(代码回忆录)——动态规划
    ThingsBoard的数据分析-自定义节点来订阅kafka stream的消息
    德大黄鱼开捕 年产量20万吨 京东超市多举措保障黄鱼品质
  • 原文地址:https://blog.csdn.net/s634772208/article/details/126706283