11#include "IGraphicsCoreText.h"
15using namespace igraphics;
17IFontDataPtr 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));
31CoreTextFont::~CoreTextFont()
33 CGDataProviderRelease(mProvider);
35 CFRelease(mDescriptor);
38PlatformFontPtr 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));
63PlatformFontPtr 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<CFSetRef> keysAsSet(CFSetCreate(NULL, (
const void**)&keys, 2, &kCFTypeSetCallBacks));
74 CFLocal<CFArrayRef> matched(CTFontDescriptorCreateMatchingFontDescriptors(descriptor.Get(), keysAsSet.Get()));
76 for (
int i = 0; i < CFArrayGetCount(matched.Get()); i++)
80 CTFontDescriptorRef testDescriptor = (CTFontDescriptorRef) CFArrayGetValueAtIndex(matched.Get(), i);
81 CFLocal<CFURLRef> url((CFURLRef) CTFontDescriptorCopyAttribute(testDescriptor, kCTFontURLAttribute));
82 CFLocal<CGDataProviderRef> provider(url.Get() ? CGDataProviderCreateWithURL(url.Get()) :
nullptr);
86 CFRetain(testDescriptor);
87 return PlatformFontPtr(
new CoreTextFont(testDescriptor, provider.Release(),
TextStyleString(style),
true));
94void releaseFontData(
void* info,
const void* data,
size_t size)
96 uint8_t* pData = (uint8_t*)data;
100PlatformFontPtr CoreTextHelpers::LoadPlatformFont(
const char* fontID,
void* pData,
int dataSize)
102 uint8_t* dataCopy =
new uint8_t[dataSize];
103 memcpy((
void*)dataCopy, pData, dataSize);
105 CFLocal<CGDataProviderRef> provider(CGDataProviderCreateWithData(
nullptr, dataCopy, (
size_t)dataSize, &releaseFontData));
110 CFLocal<CGFontRef> cgFont(CGFontCreateWithDataProvider(provider.Get()));
111 CFLocal<CTFontRef> ctFont(CTFontCreateWithGraphicsFont(cgFont.Get(), 0.f, NULL, NULL));
112 CFLocal<CTFontDescriptorRef> descriptor(CTFontCopyFontDescriptor(ctFont.Get()));
114 if (!descriptor.Get())
117 return PlatformFontPtr(
new CoreTextFont(descriptor.Release(), provider.Release(),
"",
false));
120void CoreTextHelpers::CachePlatformFont(
const char* fontID,
const PlatformFontPtr& font, StaticStorage<CoreTextFontDescriptor>& cache)
122 StaticStorage<CoreTextFontDescriptor>::Accessor storage(cache);
124 CTFontDescriptorRef descriptor = font->GetDescriptor();
125 IFontDataPtr data = font->GetFontData();
127 if (!storage.Find(fontID))
128 storage.Add(
new CoreTextFontDescriptor(descriptor, data->GetHeightEMRatio()), fontID);
131CoreTextFontDescriptor* CoreTextHelpers::GetCTFontDescriptor(
const IText& text, StaticStorage<CoreTextFontDescriptor>& cache)
133 StaticStorage<CoreTextFontDescriptor>::Accessor storage(cache);
135 CoreTextFontDescriptor* cachedFont = storage.Find(text.mFont);
137 assert(cachedFont &&
"font not found - did you forget to load it?");
Common paths useful for plug-ins.
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.
IText is used to manage font and text/text entry style for a piece of text on the UI,...