mirror of https://github.com/OpenTTD/OpenTTD.git
Codechange: [OSX] Use std::unique_ptr with a custom deleter to simply memory management of Core Foundation types.
This commit is contained in:
parent
58122983fb
commit
d5a9bd404a
|
@ -394,7 +394,7 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
|
|||
/* Get font reference from name. */
|
||||
UInt8 file_path[PATH_MAX];
|
||||
OSStatus os_err = -1;
|
||||
CFStringRef name = CFStringCreateWithCString(kCFAllocatorDefault, font_name, kCFStringEncodingUTF8);
|
||||
CFAutoRelease<CFStringRef> name(CFStringCreateWithCString(kCFAllocatorDefault, font_name, kCFStringEncodingUTF8));
|
||||
|
||||
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6)
|
||||
if (MacOSVersionIsAtLeast(10, 6, 0)) {
|
||||
|
@ -402,28 +402,21 @@ FT_Error GetFontByFaceName(const char *font_name, FT_Face *face)
|
|||
* something, no matter the name. As such, we can't use it to check for existence.
|
||||
* We instead query the list of all font descriptors that match the given name which
|
||||
* does not do this stupid name fallback. */
|
||||
CTFontDescriptorRef name_desc = CTFontDescriptorCreateWithNameAndSize(name, 0.0);
|
||||
CFSetRef mandatory_attribs = CFSetCreate(kCFAllocatorDefault, (const void **)&kCTFontNameAttribute, 1, &kCFTypeSetCallBacks);
|
||||
CFArrayRef descs = CTFontDescriptorCreateMatchingFontDescriptors(name_desc, mandatory_attribs);
|
||||
CFRelease(mandatory_attribs);
|
||||
CFRelease(name_desc);
|
||||
CFRelease(name);
|
||||
CFAutoRelease<CTFontDescriptorRef> name_desc(CTFontDescriptorCreateWithNameAndSize(name.get(), 0.0));
|
||||
CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault, (const void **)&kCTFontNameAttribute, 1, &kCFTypeSetCallBacks));
|
||||
CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(name_desc.get(), mandatory_attribs.get()));
|
||||
|
||||
/* Loop over all matches until we can get a path for one of them. */
|
||||
for (CFIndex i = 0; descs != nullptr && i < CFArrayGetCount(descs) && os_err != noErr; i++) {
|
||||
CTFontRef font = CTFontCreateWithFontDescriptor((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs, i), 0.0, nullptr);
|
||||
CFURLRef fontURL = (CFURLRef)CTFontCopyAttribute(font, kCTFontURLAttribute);
|
||||
if (CFURLGetFileSystemRepresentation(fontURL, true, file_path, lengthof(file_path))) os_err = noErr;
|
||||
CFRelease(font);
|
||||
CFRelease(fontURL);
|
||||
for (CFIndex i = 0; descs.get() != nullptr && i < CFArrayGetCount(descs.get()) && os_err != noErr; i++) {
|
||||
CFAutoRelease<CTFontRef> font(CTFontCreateWithFontDescriptor((CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i), 0.0, nullptr));
|
||||
CFAutoRelease<CFURLRef> fontURL((CFURLRef)CTFontCopyAttribute(font.get(), kCTFontURLAttribute));
|
||||
if (CFURLGetFileSystemRepresentation(fontURL.get(), true, file_path, lengthof(file_path))) os_err = noErr;
|
||||
}
|
||||
if (descs != nullptr) CFRelease(descs);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_6)
|
||||
ATSFontRef font = ATSFontFindFromName(name, kATSOptionFlagsDefault);
|
||||
CFRelease(name);
|
||||
ATSFontRef font = ATSFontFindFromName(name.get(), kATSOptionFlagsDefault);
|
||||
if (font == kInvalidFont) return err;
|
||||
|
||||
/* Get a file system reference for the font. */
|
||||
|
@ -480,31 +473,28 @@ bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, i
|
|||
if (sep != nullptr) *sep = '\0';
|
||||
}
|
||||
|
||||
/* Create a font descriptor matching the wanted language and latin (english) glyphs. */
|
||||
/* Create a font descriptor matching the wanted language and latin (english) glyphs.
|
||||
* Can't use CFAutoRelease here for everything due to the way the dictionary has to be created. */
|
||||
CFStringRef lang_codes[2];
|
||||
lang_codes[0] = CFStringCreateWithCString(kCFAllocatorDefault, lang, kCFStringEncodingUTF8);
|
||||
lang_codes[1] = CFSTR("en");
|
||||
CFArrayRef lang_arr = CFArrayCreate(kCFAllocatorDefault, (const void **)lang_codes, lengthof(lang_codes), &kCFTypeArrayCallBacks);
|
||||
CFDictionaryRef lang_attribs = CFDictionaryCreate(kCFAllocatorDefault, (const void**)&kCTFontLanguagesAttribute, (const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
|
||||
CTFontDescriptorRef lang_desc = CTFontDescriptorCreateWithAttributes(lang_attribs);
|
||||
CFAutoRelease<CFDictionaryRef> lang_attribs(CFDictionaryCreate(kCFAllocatorDefault, (const void**)&kCTFontLanguagesAttribute, (const void **)&lang_arr, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks));
|
||||
CFAutoRelease<CTFontDescriptorRef> lang_desc(CTFontDescriptorCreateWithAttributes(lang_attribs.get()));
|
||||
CFRelease(lang_arr);
|
||||
CFRelease(lang_attribs);
|
||||
CFRelease(lang_codes[0]);
|
||||
|
||||
/* Get array of all font descriptors for the wanted language. */
|
||||
CFSetRef mandatory_attribs = CFSetCreate(kCFAllocatorDefault, (const void **)&kCTFontLanguagesAttribute, 1, &kCFTypeSetCallBacks);
|
||||
CFArrayRef descs = CTFontDescriptorCreateMatchingFontDescriptors(lang_desc, mandatory_attribs);
|
||||
CFRelease(mandatory_attribs);
|
||||
CFRelease(lang_desc);
|
||||
CFAutoRelease<CFSetRef> mandatory_attribs(CFSetCreate(kCFAllocatorDefault, (const void **)&kCTFontLanguagesAttribute, 1, &kCFTypeSetCallBacks));
|
||||
CFAutoRelease<CFArrayRef> descs(CTFontDescriptorCreateMatchingFontDescriptors(lang_desc.get(), mandatory_attribs.get()));
|
||||
|
||||
for (CFIndex i = 0; descs != nullptr && i < CFArrayGetCount(descs); i++) {
|
||||
CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs, i);
|
||||
for (CFIndex i = 0; descs.get() != nullptr && i < CFArrayGetCount(descs.get()); i++) {
|
||||
CTFontDescriptorRef font = (CTFontDescriptorRef)CFArrayGetValueAtIndex(descs.get(), i);
|
||||
|
||||
/* Get font traits. */
|
||||
CFDictionaryRef traits = (CFDictionaryRef)CTFontDescriptorCopyAttribute(font, kCTFontTraitsAttribute);
|
||||
CFAutoRelease<CFDictionaryRef> traits((CFDictionaryRef)CTFontDescriptorCopyAttribute(font, kCTFontTraitsAttribute));
|
||||
CTFontSymbolicTraits symbolic_traits;
|
||||
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(traits, kCTFontSymbolicTrait), kCFNumberIntType, &symbolic_traits);
|
||||
CFRelease(traits);
|
||||
CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(traits.get(), kCTFontSymbolicTrait), kCFNumberIntType, &symbolic_traits);
|
||||
|
||||
/* Skip symbol fonts and vertical fonts. */
|
||||
if ((symbolic_traits & kCTFontClassMaskTrait) == (CTFontStylisticClass)kCTFontSymbolicClass || (symbolic_traits & kCTFontVerticalTrait)) continue;
|
||||
|
@ -515,9 +505,8 @@ bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, i
|
|||
|
||||
/* Get font name. */
|
||||
char name[128];
|
||||
CFStringRef font_name = (CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontDisplayNameAttribute);
|
||||
CFStringGetCString(font_name, name, lengthof(name), kCFStringEncodingUTF8);
|
||||
CFRelease(font_name);
|
||||
CFAutoRelease<CFStringRef> font_name((CFStringRef)CTFontDescriptorCopyAttribute(font, kCTFontDisplayNameAttribute));
|
||||
CFStringGetCString(font_name.get(), name, lengthof(name), kCFStringEncodingUTF8);
|
||||
|
||||
/* There are some special fonts starting with an '.' and the last
|
||||
* resort font that aren't usable. Skip them. */
|
||||
|
@ -531,7 +520,6 @@ bool SetFallbackFont(FreeTypeSettings *settings, const char *language_isocode, i
|
|||
break;
|
||||
}
|
||||
}
|
||||
if (descs != nullptr) CFRelease(descs);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
|
|
|
@ -161,13 +161,12 @@ void MusicDriver_Cocoa::PlaySong(const MusicSongInfo &song)
|
|||
}
|
||||
|
||||
const char *os_file = OTTD2FS(filename.c_str());
|
||||
CFURLRef url = CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)os_file, strlen(os_file), false);
|
||||
CFAutoRelease<CFURLRef> url(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8*)os_file, strlen(os_file), false));
|
||||
|
||||
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
|
||||
if (MacOSVersionIsAtLeast(10, 5, 0)) {
|
||||
if (MusicSequenceFileLoad(_sequence, url, kMusicSequenceFile_AnyType, 0) != noErr) {
|
||||
if (MusicSequenceFileLoad(_sequence, url.get(), kMusicSequenceFile_AnyType, 0) != noErr) {
|
||||
DEBUG(driver, 0, "cocoa_m: Failed to load MIDI file");
|
||||
CFRelease(url);
|
||||
return;
|
||||
}
|
||||
} else
|
||||
|
@ -175,19 +174,16 @@ void MusicDriver_Cocoa::PlaySong(const MusicSongInfo &song)
|
|||
{
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5)
|
||||
FSRef ref_file;
|
||||
if (!CFURLGetFSRef(url, &ref_file)) {
|
||||
if (!CFURLGetFSRef(url.get(), &ref_file)) {
|
||||
DEBUG(driver, 0, "cocoa_m: Failed to make FSRef");
|
||||
CFRelease(url);
|
||||
return;
|
||||
}
|
||||
if (MusicSequenceLoadSMFWithFlags(_sequence, &ref_file, 0) != noErr) {
|
||||
DEBUG(driver, 0, "cocoa_m: Failed to load MIDI file old style");
|
||||
CFRelease(url);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
CFRelease(url);
|
||||
|
||||
/* Construct audio graph */
|
||||
AUGraph graph = nullptr;
|
||||
|
|
|
@ -40,4 +40,17 @@ bool IsMonospaceFont(CFStringRef name);
|
|||
|
||||
void MacOSSetThreadName(const char *name);
|
||||
|
||||
|
||||
/** Deleter that calls CFRelease rather than deleting the pointer. */
|
||||
template <typename T> struct CFDeleter {
|
||||
void operator()(T *p)
|
||||
{
|
||||
if (p) ::CFRelease(p);
|
||||
}
|
||||
};
|
||||
|
||||
/** Specialisation of std::unique_ptr for CoreFoundation objects. */
|
||||
template <typename T>
|
||||
using CFAutoRelease = std::unique_ptr<typename std::remove_pointer<T>::type, CFDeleter<typename std::remove_pointer<T>::type>>;
|
||||
|
||||
#endif /* MACOS_H */
|
||||
|
|
|
@ -22,9 +22,9 @@
|
|||
|
||||
#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
|
||||
/** Cached current locale. */
|
||||
static CFLocaleRef _osx_locale = nullptr;
|
||||
static CFAutoRelease<CFLocaleRef> _osx_locale;
|
||||
/** CoreText cache for font information, cleared when OTTD changes fonts. */
|
||||
static CTFontRef _font_cache[FS_END];
|
||||
static CFAutoRelease<CTFontRef> _font_cache[FS_END];
|
||||
|
||||
|
||||
/**
|
||||
|
@ -36,7 +36,7 @@ private:
|
|||
ptrdiff_t length;
|
||||
const FontMap& font_map;
|
||||
|
||||
CTTypesetterRef typesetter;
|
||||
CFAutoRelease<CTTypesetterRef> typesetter;
|
||||
|
||||
CFIndex cur_offset = 0; ///< Offset from the start of the current run from where to output.
|
||||
|
||||
|
@ -68,9 +68,9 @@ public:
|
|||
/** A single line worth of VisualRuns. */
|
||||
class CoreTextLine : public std::vector<CoreTextVisualRun>, public ParagraphLayouter::Line {
|
||||
public:
|
||||
CoreTextLine(CTLineRef line, const FontMap &fontMapping, const CoreTextParagraphLayoutFactory::CharType *buff)
|
||||
CoreTextLine(CFAutoRelease<CTLineRef> line, const FontMap &fontMapping, const CoreTextParagraphLayoutFactory::CharType *buff)
|
||||
{
|
||||
CFArrayRef runs = CTLineGetGlyphRuns(line);
|
||||
CFArrayRef runs = CTLineGetGlyphRuns(line.get());
|
||||
for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
|
||||
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
|
||||
|
||||
|
@ -81,7 +81,6 @@ public:
|
|||
|
||||
this->emplace_back(run, map->second, buff);
|
||||
}
|
||||
CFRelease(line);
|
||||
}
|
||||
|
||||
int GetLeading() const override;
|
||||
|
@ -96,16 +95,11 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
CoreTextParagraphLayout(CTTypesetterRef typesetter, const CoreTextParagraphLayoutFactory::CharType *buffer, ptrdiff_t len, const FontMap &fontMapping) : text_buffer(buffer), length(len), font_map(fontMapping), typesetter(typesetter)
|
||||
CoreTextParagraphLayout(CFAutoRelease<CTTypesetterRef> typesetter, const CoreTextParagraphLayoutFactory::CharType *buffer, ptrdiff_t len, const FontMap &fontMapping) : text_buffer(buffer), length(len), font_map(fontMapping), typesetter(std::move(typesetter))
|
||||
{
|
||||
this->Reflow();
|
||||
}
|
||||
|
||||
~CoreTextParagraphLayout() override
|
||||
{
|
||||
CFRelease(this->typesetter);
|
||||
}
|
||||
|
||||
void Reflow() override
|
||||
{
|
||||
this->cur_offset = 0;
|
||||
|
@ -143,12 +137,11 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
|
|||
}
|
||||
|
||||
/* Make attributed string with embedded font information. */
|
||||
CFMutableAttributedStringRef str = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
|
||||
CFAttributedStringBeginEditing(str);
|
||||
CFAutoRelease<CFMutableAttributedStringRef> str(CFAttributedStringCreateMutable(kCFAllocatorDefault, 0));
|
||||
CFAttributedStringBeginEditing(str.get());
|
||||
|
||||
CFStringRef base = CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buff, length, kCFAllocatorNull);
|
||||
CFAttributedStringReplaceString(str, CFRangeMake(0, 0), base);
|
||||
CFRelease(base);
|
||||
CFAutoRelease<CFStringRef> base(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, buff, length, kCFAllocatorNull));
|
||||
CFAttributedStringReplaceString(str.get(), CFRangeMake(0, 0), base.get());
|
||||
|
||||
/* Apply font and colour ranges to our string. This is important to make sure
|
||||
* that we get proper glyph boundaries on style changes. */
|
||||
|
@ -156,36 +149,33 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
|
|||
for (const auto &i : fontMapping) {
|
||||
if (i.first - last == 0) continue;
|
||||
|
||||
if (_font_cache[i.second->fc->GetSize()] == nullptr) {
|
||||
if (!_font_cache[i.second->fc->GetSize()]) {
|
||||
/* Cache font information. */
|
||||
CFStringRef font_name = CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName(), kCFStringEncodingUTF8);
|
||||
_font_cache[i.second->fc->GetSize()] = CTFontCreateWithName(font_name, i.second->fc->GetFontSize(), nullptr);
|
||||
CFRelease(font_name);
|
||||
CFAutoRelease<CFStringRef> font_name(CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName(), kCFStringEncodingUTF8));
|
||||
_font_cache[i.second->fc->GetSize()].reset(CTFontCreateWithName(font_name.get(), i.second->fc->GetFontSize(), nullptr));
|
||||
}
|
||||
CFAttributedStringSetAttribute(str, CFRangeMake(last, i.first - last), kCTFontAttributeName, _font_cache[i.second->fc->GetSize()]);
|
||||
CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTFontAttributeName, _font_cache[i.second->fc->GetSize()].get());
|
||||
|
||||
CGColorRef color = CGColorCreateGenericGray((uint8)i.second->colour / 255.0f, 1.0f); // We don't care about the real colours, just that they are different.
|
||||
CFAttributedStringSetAttribute(str, CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
|
||||
CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
|
||||
CGColorRelease(color);
|
||||
|
||||
/* Install a size callback for our special sprite glyphs. */
|
||||
for (ssize_t c = last; c < i.first; c++) {
|
||||
if (buff[c] >= SCC_SPRITE_START && buff[c] <= SCC_SPRITE_END) {
|
||||
CTRunDelegateRef del = CTRunDelegateCreate(&_sprite_font_callback, (void *)(size_t)(buff[c] | (i.second->fc->GetSize() << 24)));
|
||||
CFAttributedStringSetAttribute(str, CFRangeMake(c, 1), kCTRunDelegateAttributeName, del);
|
||||
CFRelease(del);
|
||||
CFAutoRelease<CTRunDelegateRef> del(CTRunDelegateCreate(&_sprite_font_callback, (void *)(size_t)(buff[c] | (i.second->fc->GetSize() << 24))));
|
||||
CFAttributedStringSetAttribute(str.get(), CFRangeMake(c, 1), kCTRunDelegateAttributeName, del.get());
|
||||
}
|
||||
}
|
||||
|
||||
last = i.first;
|
||||
}
|
||||
CFAttributedStringEndEditing(str);
|
||||
CFAttributedStringEndEditing(str.get());
|
||||
|
||||
/* Create and return typesetter for the string. */
|
||||
CTTypesetterRef typesetter = CTTypesetterCreateWithAttributedString(str);
|
||||
CFRelease(str);
|
||||
CFAutoRelease<CTTypesetterRef> typesetter(CTTypesetterCreateWithAttributedString(str.get()));
|
||||
|
||||
return typesetter != nullptr ? new CoreTextParagraphLayout(typesetter, buff, length, fontMapping) : nullptr;
|
||||
return typesetter ? new CoreTextParagraphLayout(std::move(typesetter), buff, length, fontMapping) : nullptr;
|
||||
}
|
||||
|
||||
/* virtual */ std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(int max_width)
|
||||
|
@ -193,14 +183,14 @@ static CTRunDelegateCallbacks _sprite_font_callback = {
|
|||
if (this->cur_offset >= this->length) return nullptr;
|
||||
|
||||
/* Get line break position, trying word breaking first and breaking somewhere if that doesn't work. */
|
||||
CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter, this->cur_offset, max_width);
|
||||
if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter, this->cur_offset, max_width);
|
||||
CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter.get(), this->cur_offset, max_width);
|
||||
if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter.get(), this->cur_offset, max_width);
|
||||
|
||||
/* Create line. */
|
||||
CTLineRef line = CTTypesetterCreateLine(this->typesetter, CFRangeMake(this->cur_offset, len));
|
||||
CFAutoRelease<CTLineRef> line(CTTypesetterCreateLine(this->typesetter.get(), CFRangeMake(this->cur_offset, len)));
|
||||
this->cur_offset += len;
|
||||
|
||||
return std::unique_ptr<const Line>(line != nullptr ? new CoreTextLine(line, this->font_map, this->text_buffer) : nullptr);
|
||||
return std::unique_ptr<const Line>(line ? new CoreTextLine(std::move(line), this->font_map, this->text_buffer) : nullptr);
|
||||
}
|
||||
|
||||
CoreTextParagraphLayout::CoreTextVisualRun::CoreTextVisualRun(CTRunRef run, Font *font, const CoreTextParagraphLayoutFactory::CharType *buff) : font(font)
|
||||
|
@ -271,10 +261,7 @@ int CoreTextParagraphLayout::CoreTextLine::GetWidth() const
|
|||
/** Delete CoreText font reference for a specific font size. */
|
||||
void MacOSResetScriptCache(FontSize size)
|
||||
{
|
||||
if (_font_cache[size] != nullptr) {
|
||||
CFRelease(_font_cache[size]);
|
||||
_font_cache[size] = nullptr;
|
||||
}
|
||||
_font_cache[size].reset();
|
||||
}
|
||||
|
||||
/** Store current language locale as a CoreFounation locale. */
|
||||
|
@ -282,11 +269,8 @@ void MacOSSetCurrentLocaleName(const char *iso_code)
|
|||
{
|
||||
if (!MacOSVersionIsAtLeast(10, 5, 0)) return;
|
||||
|
||||
if (_osx_locale != nullptr) CFRelease(_osx_locale);
|
||||
|
||||
CFStringRef iso = CFStringCreateWithCString(kCFAllocatorDefault, iso_code, kCFStringEncodingUTF8);
|
||||
_osx_locale = CFLocaleCreate(kCFAllocatorDefault, iso);
|
||||
CFRelease(iso);
|
||||
CFAutoRelease<CFStringRef> iso(CFStringCreateWithCString(kCFAllocatorDefault, iso_code, kCFStringEncodingUTF8));
|
||||
_osx_locale.reset(CFLocaleCreate(kCFAllocatorDefault, iso.get()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -303,22 +287,13 @@ int MacOSStringCompare(const char *s1, const char *s2)
|
|||
|
||||
CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
|
||||
|
||||
CFStringRef cf1 = CFStringCreateWithCString(kCFAllocatorDefault, s1, kCFStringEncodingUTF8);
|
||||
CFStringRef cf2 = CFStringCreateWithCString(kCFAllocatorDefault, s2, kCFStringEncodingUTF8);
|
||||
CFAutoRelease<CFStringRef> cf1(CFStringCreateWithCString(kCFAllocatorDefault, s1, kCFStringEncodingUTF8));
|
||||
CFAutoRelease<CFStringRef> cf2(CFStringCreateWithCString(kCFAllocatorDefault, s2, kCFStringEncodingUTF8));
|
||||
|
||||
/* If any CFString could not be created (e.g., due to UTF8 invalid chars), return OS unsupported functionality */
|
||||
if (cf1 == nullptr || cf2 == nullptr) {
|
||||
if (cf1 != nullptr) CFRelease(cf1);
|
||||
if (cf2 != nullptr) CFRelease(cf2);
|
||||
return 0;
|
||||
}
|
||||
if (cf1 == nullptr || cf2 == nullptr) return 0;
|
||||
|
||||
CFComparisonResult res = CFStringCompareWithOptionsAndLocale(cf1, cf2, CFRangeMake(0, CFStringGetLength(cf1)), flags, _osx_locale);
|
||||
|
||||
CFRelease(cf1);
|
||||
CFRelease(cf2);
|
||||
|
||||
return (int)res + 2;
|
||||
return (int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags, _osx_locale.get()) + 2;
|
||||
}
|
||||
|
||||
|
||||
|
@ -353,30 +328,27 @@ int MacOSStringCompare(const char *s1, const char *s2)
|
|||
this->str_info.resize(utf16_to_utf8.size());
|
||||
|
||||
if (utf16_str.size() > 0) {
|
||||
CFStringRef str = CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull);
|
||||
CFAutoRelease<CFStringRef> str(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull));
|
||||
|
||||
/* Get cluster breaks. */
|
||||
for (CFIndex i = 0; i < CFStringGetLength(str); ) {
|
||||
CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str, i);
|
||||
for (CFIndex i = 0; i < CFStringGetLength(str.get()); ) {
|
||||
CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str.get(), i);
|
||||
this->str_info[r.location].char_stop = true;
|
||||
|
||||
i += r.length;
|
||||
}
|
||||
|
||||
/* Get word breaks. */
|
||||
CFStringTokenizerRef tokenizer = CFStringTokenizerCreate(kCFAllocatorDefault, str, CFRangeMake(0, CFStringGetLength(str)), kCFStringTokenizerUnitWordBoundary, _osx_locale);
|
||||
CFAutoRelease<CFStringTokenizerRef> tokenizer(CFStringTokenizerCreate(kCFAllocatorDefault, str.get(), CFRangeMake(0, CFStringGetLength(str.get())), kCFStringTokenizerUnitWordBoundary, _osx_locale.get()));
|
||||
|
||||
CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
|
||||
while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer)) != kCFStringTokenizerTokenNone) {
|
||||
while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer.get())) != kCFStringTokenizerTokenNone) {
|
||||
/* Skip tokens that are white-space or punctuation tokens. */
|
||||
if ((tokenType & kCFStringTokenizerTokenHasNonLettersMask) != kCFStringTokenizerTokenHasNonLettersMask) {
|
||||
CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer);
|
||||
CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer.get());
|
||||
this->str_info[r.location].word_stop = true;
|
||||
}
|
||||
}
|
||||
|
||||
CFRelease(tokenizer);
|
||||
CFRelease(str);
|
||||
}
|
||||
|
||||
/* End-of-string is always a valid stopping point. */
|
||||
|
|
|
@ -258,11 +258,10 @@ static void QZ_GetDisplayModeInfo(CFArrayRef modes, CFIndex i, int &bpp, uint16
|
|||
|
||||
#if (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_11)
|
||||
/* Extract bit depth from mode string. */
|
||||
CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode);
|
||||
if (CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 32;
|
||||
if (CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 16;
|
||||
if (CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 8;
|
||||
CFRelease(pixEnc);
|
||||
CFAutoRelease<CFStringRef> pixEnc(CGDisplayModeCopyPixelEncoding(mode));
|
||||
if (CFStringCompare(pixEnc.get(), CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 32;
|
||||
if (CFStringCompare(pixEnc.get(), CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 16;
|
||||
if (CFStringCompare(pixEnc.get(), CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo) bpp = 8;
|
||||
#else
|
||||
/* CGDisplayModeCopyPixelEncoding is deprecated on OSX 10.11+, but there are no 8 bpp modes anyway... */
|
||||
bpp = 32;
|
||||
|
@ -687,15 +686,13 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
|
|||
void cocoaSetApplicationBundleDir()
|
||||
{
|
||||
char tmp[MAXPATHLEN];
|
||||
CFURLRef url = CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle());
|
||||
if (CFURLGetFileSystemRepresentation(url, true, (unsigned char*)tmp, MAXPATHLEN)) {
|
||||
CFAutoRelease<CFURLRef> url(CFBundleCopyResourcesDirectoryURL(CFBundleGetMainBundle()));
|
||||
if (CFURLGetFileSystemRepresentation(url.get(), true, (unsigned char*)tmp, MAXPATHLEN)) {
|
||||
AppendPathSeparator(tmp, lastof(tmp));
|
||||
_searchpaths[SP_APPLICATION_BUNDLE_DIR] = stredup(tmp);
|
||||
} else {
|
||||
_searchpaths[SP_APPLICATION_BUNDLE_DIR] = NULL;
|
||||
}
|
||||
|
||||
CFRelease(url);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue