c++ - 以编程方式安装字体

标签 c++ macos osx-snow-leopard wxwidgets

如何以编程方式在 Mac 平台 (Snow Leopard) 上安装字体?我需要遵循哪些步骤?我希望用户输入一个字体文件,然后我的软件会安装它。

最佳答案

字体属于 ~user/Library/Fonts/供单个用户使用或/Library/Fonts/可供所有用户访问。您需要获得许可才能写入/Library/Fonts/,尽管有一个 API 使它相对容易。 (我在某处有代码,如果没有其他人知道,可以立即查找。)


应要求,这里有一些 API 文档:

http://developer.apple.com/mac/library/documentation/Security/Reference/authorization_ref/Reference/reference.html

这是我在 Carbon 下进行更新的旧代码(因此是 Pascal 字符串)。它基于可能位于上述 URL 中某处的示例代码。我没有考虑在 Cocoa 下这样做,这是代码的编辑版本(仍然有点乱),所以 YMMV。

int main()
{
    OSStatus myStatus = -1;
    char path[1024];
    char myToolPath[2048];
    getUpdateAppPath(myToolPath);
    getFilePath(path);

    if (path[0] != 0)
    {
        char temp[2048];
        FILE *f;
        printf("Attempting to open \'%s\'\n", path);
        f = fopen(path, "w+");
        if (f != 0) // we seem to have write permission
        {
            fclose(f);
            SInt16 res;
            sprintf(temp, "\'%s\' \'%s\'", myToolPath, path);
            system(temp);
            StandardAlert(kAlertNoteAlert, "\pUpdate Complete", "\pSuccessfully updated.", 0, &res);
            return 0;
        }

        AuthorizationFlags myFlags = kAuthorizationFlagDefaults;
        AuthorizationRef myAuthorizationRef;
        myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
                                       myFlags, &myAuthorizationRef);
        if (myStatus != errAuthorizationSuccess)
        {
            SInt16 res;
            StandardAlert(kAlertNoteAlert, "\pAuthorization Error", "\pCould not authorize application to update.", 0, &res);
            return myStatus;
        }

        AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0};

        AuthorizationRights myRights = {1, &myItems};
        myFlags = kAuthorizationFlagDefaults |
        kAuthorizationFlagInteractionAllowed |
        kAuthorizationFlagPreAuthorize |
        kAuthorizationFlagExtendRights;

        myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights, NULL, myFlags, NULL );

        if (myStatus != errAuthorizationSuccess)
            break;

        char *myArguments[] = { path, NULL };
        FILE *myCommunicationsPipe = NULL;
        char myReadBuffer[128];


        myFlags = kAuthorizationFlagDefaults;
        myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, myToolPath, myFlags, myArguments,
                                                      &myCommunicationsPipe);

        if (myStatus == errAuthorizationSuccess)
            for(;;)
            {
                int bytesRead = read (fileno (myCommunicationsPipe),
                                      myReadBuffer, sizeof (myReadBuffer));
                if (bytesRead < 1) break;
                write (fileno (stdout), myReadBuffer, bytesRead);
            }

        AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults); // 17
    }
    if (myStatus)
    {
        printf("Status: %ld\n", myStatus);
        SInt16 res;
        StandardAlert(kAlertNoteAlert, "\pUpdater Error", "\pMay not have updated properly.", 0, &res);
    }
    else {
        SInt16 res;
        StandardAlert(kAlertNoteAlert, "\pUpdate Complete", "\pSuccessfully updated.", 0, &res);
    }
    return myStatus;
}

关于c++ - 以编程方式安装字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2457409/

相关文章:

python - 在雪豹上安装mysqldb

c++ - std::shared_ptr 和继承

macos - 在 Mac 上获取文件的完整网络路径 - 终端

macos - 为什么 OS X 不报告支持 AVX2 而 Ubuntu 报告支持?

php - Mac和Windows上传文件名编码不同的问题

macos - Mac Pro 64 位寻址内核

cocoa - 获取桌面图标的位置?

C++:使用指向 vector 开头的指针访问 vector 元素

c++ - 引用放置的 map 值需要类的定义

c++ - c++11(工作草案)标准中的布局兼容性是否太弱?