以编程方式安装字体

时间:2010-03-16 19:22:23

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

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

1 个答案:

答案 0 :(得分:3)

字体属于~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;
}