【问题标题】:Programmatically Installing Fonts以编程方式安装字体
【发布时间】:2011-01-28 06:46:34
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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


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

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

    这是我在 Carbon 下进行更新的旧代码(因此是帕斯卡字符串)。它基于可能在上述 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;
    }
    

    【讨论】:

    • 添加了更多细节。希望这可以帮助;我使用它已经有几年了,所以情况可能已经改变,但它应该能让你朝着正确的方向前进。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-13
    • 2014-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多