gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:11:10 26 4
gpt4 key购买 nike

如何以编程方式在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com