11 #include "IGraphicsCoreText.h" 14 using namespace iplug;
15 using namespace igraphics;
17 IFontDataPtr CoreTextFont::GetFontData()
19 CFLocal<CFDataRef> rawData(CGDataProviderCopyData(mProvider));
20 const UInt8* bytes = CFDataGetBytePtr(rawData.Get());
23 if (mStyleString.GetLength())
24 faceIdx = GetFaceIdx(bytes, static_cast<int>(CFDataGetLength(rawData.Get())), mStyleString.Get());
26 IFontDataPtr fontData(
new IFontData(bytes, static_cast<int>(CFDataGetLength(rawData.Get())), faceIdx));
31 CoreTextFont::~CoreTextFont()
33 CGDataProviderRelease(mProvider);
35 CFRelease(mDescriptor);
38 PlatformFontPtr CoreTextHelpers::LoadPlatformFont(
const char* fontID,
const char* fileNameOrResID,
const char* bundleID,
const char* sharedResourceSubPath)
41 const EResourceLocation fontLocation =
LocateResource(fileNameOrResID,
"ttf", fullPath, bundleID,
nullptr, sharedResourceSubPath);
43 if (fontLocation == kNotFound)
46 CFLocal<CFStringRef> path(CFStringCreateWithCString(NULL, fullPath.Get(), kCFStringEncodingUTF8));
47 CFLocal<CFURLRef> url(CFURLCreateWithFileSystemPath(NULL, path.Get(), kCFURLPOSIXPathStyle,
false));
48 CFLocal<CGDataProviderRef> provider(url.Get() ? CGDataProviderCreateWithURL(url.Get()) :
nullptr);
53 CFLocal<CGFontRef> cgFont(CGFontCreateWithDataProvider(provider.Get()));
54 CFLocal<CTFontRef> ctFont(CTFontCreateWithGraphicsFont(cgFont.Get(), 0.f, NULL, NULL));
55 CFLocal<CTFontDescriptorRef> descriptor(CTFontCopyFontDescriptor(ctFont.Get()));
57 if (!descriptor.Get())
60 return PlatformFontPtr(
new CoreTextFont(descriptor.Release(), provider.Release(),
"",
false));
63 PlatformFontPtr CoreTextHelpers::LoadPlatformFont(
const char* fontID,
const char* fontName, ETextStyle style)
65 CFLocal<CFStringRef> fontStr(CFStringCreateWithCString(NULL, fontName, kCFStringEncodingUTF8));
66 CFLocal<CFStringRef> styleStr(CFStringCreateWithCString(NULL,
TextStyleString(style), kCFStringEncodingUTF8));
68 CFStringRef keys[] = { kCTFontFamilyNameAttribute, kCTFontStyleNameAttribute };
69 CFTypeRef values[] = { fontStr.Get(), styleStr.Get() };
71 CFLocal<CFDictionaryRef> dictionary(CFDictionaryCreate(NULL, (
const void**)&keys, (
const void**)&values, 2, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
72 CFLocal<CTFontDescriptorRef> descriptor(CTFontDescriptorCreateWithAttributes(dictionary.Get()));
73 CFLocal<CFURLRef> url((CFURLRef) CTFontDescriptorCopyAttribute(descriptor.Get(), kCTFontURLAttribute));
74 CFLocal<CGDataProviderRef> provider(url.Get() ? CGDataProviderCreateWithURL(url.Get()) :
nullptr);
79 return PlatformFontPtr(
new CoreTextFont(descriptor.Release(), provider.Release(),
TextStyleString(style),
true));
82 void releaseFontData(
void* info,
const void* data,
size_t size)
84 uint8_t* pData = (uint8_t*)data;
88 PlatformFontPtr CoreTextHelpers::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
90 uint8_t* dataCopy =
new uint8_t[dataSize];
91 memcpy((
void*)dataCopy, pData, dataSize);
93 CFLocal<CGDataProviderRef> provider(CGDataProviderCreateWithData(
nullptr, dataCopy, (
size_t)dataSize, &releaseFontData));
98 CFLocal<CGFontRef> cgFont(CGFontCreateWithDataProvider(provider.Get()));
99 CFLocal<CTFontRef> ctFont(CTFontCreateWithGraphicsFont(cgFont.Get(), 0.f, NULL, NULL));
100 CFLocal<CTFontDescriptorRef> descriptor(CTFontCopyFontDescriptor(ctFont.Get()));
102 if (!descriptor.Get())
105 return PlatformFontPtr(
new CoreTextFont(descriptor.Release(), provider.Release(),
"",
false));
108 void CoreTextHelpers::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font, StaticStorage<CoreTextFontDescriptor>& cache)
110 StaticStorage<CoreTextFontDescriptor>::Accessor storage(cache);
112 CTFontDescriptorRef descriptor = font->GetDescriptor();
113 IFontDataPtr data = font->GetFontData();
115 if (!storage.Find(fontID))
116 storage.Add(
new CoreTextFontDescriptor(descriptor, data->GetHeightEMRatio()), fontID);
119 CoreTextFontDescriptor* CoreTextHelpers::GetCTFontDescriptor(
const IText& text, StaticStorage<CoreTextFontDescriptor>& cache)
121 StaticStorage<CoreTextFontDescriptor>::Accessor storage(cache);
123 CoreTextFontDescriptor* cachedFont = storage.Find(text.mFont);
125 assert(cachedFont &&
"font not found - did you forget to load it?");
Common paths useful for plug-ins.
IText is used to manage font and text/text entry style for a piece of text on the UI...
EResourceLocation LocateResource(const char *fileNameOrResID, const char *type, WDL_String &result, const char *bundleID, void *pHInstance, const char *sharedResourcesSubPath)
Find the absolute path of a resource based on it's file name (e.g.
static const char * TextStyleString(ETextStyle style)
Helper to get a CString based on ETextStyle.