繁体   English   中英

Delphi - OSX - 从内存流中加载字体

[英]Delphi - OSX - Load font from memory stream

我试图用Delphi在Firemonkey(FMX)应用程序中加载OSX上的内存流中的字体。

这是我试过的代码:

procedure LoadFontFromStream(SourceStream:TStream);
var cfData:CFDataRef;
    fontBuffer:TBytes;
    fontRef: CGFontRef;
    provider: CGDataProviderRef;
    currentStrRef: CFStringRef;
    currentStr: string;
    streamSize: Int64;
    ctFontReference : CTFontRef;
    ctAscent, ctDescent, ctCapHeight: CGFloat;
begin
  streamSize := SourceStream.Size;
  SetLength(fontBuffer, streamSize);
  SourceStream.Read(fontBuffer[0], streamSize);

  cfData := CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, @fontBuffer[0],
    streamSize, kCFAllocatorNull);
  provider := CGDataProviderCreateWithCFData(cfData);
  CFRelease(cfData);

  fontRef := CGFontCreateWithDataProvider(provider);
  CGDataProviderRelease(provider);

  if fontRef <> nil then
  begin
    ctFontReference := CTFontCreateWithGraphicsFont(fontRef,0.0, nil, nil);
    if ctFontReference <> nil then
    begin
      ctCapHeight := CTFontGetCapHeight(ctFontReference);
      ctAscent := CTFontGetAscent(ctFontReference);
      ctDescent := CTFontGetDescent(ctFontReference);
    end;
    currentStrRef := CTFontCopyFamilyName(ctFontReference);
    currentStr := CFToDelphiString(currentStrRef);
    Log.d('Family name: %s',[currentStr]);
    CFRelease(currentStrRef);
    currentStrRef := CTFontCopyPostscriptName(ctFontReference);
    currentStr := CFToDelphiString(currentStrRef);
    Log.d('Postscript name: %s',[currentStr]);
    CFRelease(currentStrRef);
    currentStrRef := CTFontCopyDisplayName(ctFontReference);
    currentStr := CFToDelphiString(currentStrRef);
    Log.d('Display name: %s',[currentStr]);
    CFRelease(currentStrRef);
  end;
end;

姓氏,postscript名称和显示名称都在调试器上显示正确。 但是,我的表单上没有加载/显示字体。 我在运行代码后尝试枚举所有字体系列,似乎我的字体(Open Sans)不存在。 以下是我枚举字体系列的方法:

procedure ListFonts();
var fontFamilyArray: CFArrayRef;
    i : CFIndex;
    NumFamilies: CFIndex;
    family: CFStringRef;
    familyStr: string;
    foundOpenSans: Boolean;
begin
  foundOpenSans := False;
  fontFamilyArray := CTFontManagerCopyAvailableFontFamilyNames();
  NumFamilies  :=  CFArrayGetCount(fontFamilyArray);
  for i := 0 to NumFamilies-1 do
  begin
    family := CFArrayGetValueAtIndex(fontFamilyArray, i);
    familyStr := CFToDelphiString(family);
    if familyStr.StartsWith('Open') then
    begin
      foundOpenSans := true;
      Log.d('Found %s', [familyStr]);
    end;
  end;

  if not foundOpenSans then
    Log.d('Open Sans not found');

  CFRelease(fontFamilyArray);
end;

我知道通过在info.plist文件中设置ATSApplicationFontsPath并将文件放在包含我的文件夹的文件夹中,可以加载自定义字体。 我试过了,这很有效。 但是,我需要从内存流中加载字体。

任何想法为什么该代码不加载字体? 我正在运行代码以在我的应用程序中创建任何表单之前加载字体。

我没有看到你用字体管理器注册该字体。 看看CTFontManagerRegisterGraphicsFont 文档明确提到了这个函数,用于注册从内存构造的字体。

问题与我使用的缓冲区有关。 这是我如何解决它。

procedure LoadFontFromStream(SourceStream:TStream);
var cfData:CFDataRef;
    fontBuffer:TBytes;
    fontRef: CGFontRef;
    provider: CGDataProviderRef;
    streamSize: Int64;
    theError: CFErrorRef;
    errorDescription : CFStringRef;
    errorDescriptionString : string;
    registerFontValue: Integer;
begin
  streamSize := SourceStream.Size;
  SetLength(fontBuffer, streamSize);
  SourceStream.Read(fontBuffer[0], streamSize);
  cfData := CFDataCreate(kCFAllocatorDefault, @fontBuffer[0],streamSize);
  provider := CGDataProviderCreateWithCFData(cfData);
  CFRelease(cfData);
  fontRef := CGFontCreateWithDataProvider(provider);
  CGDataProviderRelease(provider);
  if fontRef <> nil then
  begin
    theError := nil;
    registerFontValue :=CTFontManagerRegisterGraphicsFont(fontRef, @theError);
    if theError <> nil then
    begin
      errorDescription := CFErrorCopyDescription(theError);
      errorDescriptionString := CFToDelphiString(errorDescription);
      Log.d('Error loading font: %s', [errorDescriptionString]);
      CFRelease(errorDescription);
    end;
  end;
end;

但是,这仅适用于OSX 10.8及更高版本,因为CTFontManagerRegisterGraphicsFont在OSX 10.7上不可用。 想知道OSX 10.7上有哪些方法可用?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM