10 #include "../../stdafx.h"
12 #include "../../string_func.h"
13 #include "../../strings_func.h"
14 #include "../../table/control_codes.h"
15 #include "../../fontcache.h"
16 #include "../../zoom_func.h"
19 #include <CoreFoundation/CoreFoundation.h>
23 #ifndef HAVE_OSX_109_SDK
25 typedef const struct __CTRunDelegate * CTRunDelegateRef;
27 typedef void (*CTRunDelegateDeallocateCallback) (
void *refCon);
28 typedef CGFloat (*CTRunDelegateGetAscentCallback) (
void *refCon);
29 typedef CGFloat (*CTRunDelegateGetDescentCallback) (
void *refCon);
30 typedef CGFloat (*CTRunDelegateGetWidthCallback) (
void *refCon);
33 CTRunDelegateDeallocateCallback dealloc;
34 CTRunDelegateGetAscentCallback getAscent;
35 CTRunDelegateGetDescentCallback getDescent;
36 CTRunDelegateGetWidthCallback getWidth;
40 kCTRunDelegateVersion1 = 1,
41 kCTRunDelegateCurrentVersion = kCTRunDelegateVersion1
44 extern const CFStringRef kCTRunDelegateAttributeName AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
46 CTRunDelegateRef CTRunDelegateCreate(
const CTRunDelegateCallbacks *callbacks,
void *refCon) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
73 std::vector<GlyphID> glyphs;
74 std::vector<Position> positions;
75 std::vector<int> glyph_to_char;
77 int total_advance = 0;
84 std::span<const GlyphID> GetGlyphs()
const override {
return this->glyphs; }
85 std::span<const Position> GetPositions()
const override {
return this->positions; }
86 std::span<const int> GetGlyphToCharMap()
const override {
return this->glyph_to_char; }
88 const Font *GetFont()
const override {
return this->font; }
89 int GetLeading()
const override {
return this->font->
fc->
GetHeight(); }
90 int GetGlyphCount()
const override {
return (
int)this->glyphs.size(); }
91 int GetAdvance()
const {
return this->total_advance; }
99 CFArrayRef runs = CTLineGetGlyphRuns(line.get());
100 for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
101 CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
104 CFRange chars = CTRunGetStringRange(run);
105 auto map = fontMapping.upper_bound(chars.location);
107 this->emplace_back(run, map->second, buff);
113 int CountRuns()
const override {
return this->size(); }
114 const VisualRun &GetVisualRun(
int run)
const override {
return this->at(run); }
116 int GetInternalCharLength(char32_t c)
const override
119 return c >= 0x010000U ? 2 : 1;
128 void Reflow()
override
130 this->cur_offset = 0;
133 std::unique_ptr<const Line> NextLine(
int max_width)
override;
141 char32_t c = (char32_t)((
size_t)ref_con & 0xFFFFFF);
147 kCTRunDelegateCurrentVersion,
nullptr,
nullptr,
nullptr,
156 ptrdiff_t length = buff_end - buff;
157 if (length == 0)
return nullptr;
160 for (
const auto &i : fontMapping) {
161 if (i.second->fc->IsBuiltInFont())
return nullptr;
166 CFAttributedStringBeginEditing(str.get());
169 CFAttributedStringReplaceString(str.get(), CFRangeMake(0, 0), base.get());
171 const UniChar replacment_char = 0xFFFC;
177 for (
const auto &i : fontMapping) {
178 if (i.first - last == 0)
continue;
180 CTFontRef font = (CTFontRef)i.second->fc->GetOSHandle();
181 if (font ==
nullptr) {
184 CFAutoRelease<CFStringRef> font_name(CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName().c_str(), kCFStringEncodingUTF8));
185 _font_cache[i.second->fc->GetSize()].reset(CTFontCreateWithName(font_name.get(), i.second->fc->GetFontSize(),
nullptr));
189 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTFontAttributeName, font);
191 CGColorRef color = CGColorCreateGenericGray((uint8_t)i.second->colour / 255.0f, 1.0f);
192 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
193 CGColorRelease(color);
196 for (ssize_t c = last; c < i.first; c++) {
197 if (buff[c] >= SCC_SPRITE_START && buff[c] <= SCC_SPRITE_END && i.second->fc->MapCharToGlyph(buff[c],
false) == 0) {
200 CFAttributedStringReplaceString(str.get(), CFRangeMake(c, 1), replacment_str.get());
201 CFAttributedStringSetAttribute(str.get(), CFRangeMake(c, 1), kCTRunDelegateAttributeName, del.get());
207 CFAttributedStringEndEditing(str.get());
215 std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(
int max_width)
217 if (this->
cur_offset >= this->length)
return nullptr;
220 CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter.get(), this->cur_offset, max_width);
221 if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter.get(), this->cur_offset, max_width);
227 if (!line)
return nullptr;
228 return std::make_unique<CoreTextLine>(std::move(line), this->font_map, this->text_buffer);
233 this->glyphs.resize(CTRunGetGlyphCount(run));
236 CFIndex map[this->glyphs.size()];
237 CTRunGetStringIndices(run, CFRangeMake(0, 0), map);
239 this->glyph_to_char.resize(this->glyphs.size());
240 for (
size_t i = 0; i < this->glyph_to_char.size(); i++) this->glyph_to_char[i] = (
int)map[i];
242 CGPoint pts[this->glyphs.size()];
243 CTRunGetPositions(run, CFRangeMake(0, 0), pts);
244 CGSize advs[this->glyphs.size()];
245 CTRunGetAdvances(run, CFRangeMake(0, 0), advs);
246 this->positions.reserve(this->glyphs.size());
250 CGGlyph gl[this->glyphs.size()];
251 CTRunGetGlyphs(run, CFRangeMake(0, 0), gl);
252 for (
size_t i = 0; i < this->glyphs.size(); i++) {
253 if (buff[this->glyph_to_char[i]] >= SCC_SPRITE_START && buff[this->glyph_to_char[i]] <= SCC_SPRITE_END && (gl[i] == 0 || gl[i] == 3)) {
258 this->glyphs[i] = gl[i];
259 this->positions.emplace_back(pts[i].x, pts[i].x + advs[i].width - 1, pts[i].y);
262 this->total_advance = (int)std::ceil(CTRunGetTypographicBounds(run, CFRangeMake(0, 0),
nullptr,
nullptr,
nullptr));
272 for (
const auto &run : *
this) {
273 leading = std::max(leading, run.GetLeading());
285 if (this->empty())
return 0;
288 for (
const auto &run : *
this) {
289 total_width += run.GetAdvance();
308 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
310 CTFontManagerRegisterFontsForURL(url.get(), kCTFontManagerScopeProcess,
nullptr);
319 _osx_locale.reset(CFLocaleCreate(kCFAllocatorDefault, iso.get()));
332 if (!supported)
return 0;
334 CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
336 CFAutoRelease<CFStringRef> cf1(CFStringCreateWithBytes(kCFAllocatorDefault, (
const UInt8 *)s1.data(), s1.size(), kCFStringEncodingUTF8,
false));
337 CFAutoRelease<CFStringRef> cf2(CFStringCreateWithBytes(kCFAllocatorDefault, (
const UInt8 *)s2.data(), s2.size(), kCFStringEncodingUTF8,
false));
340 if (cf1 ==
nullptr || cf2 ==
nullptr)
return 0;
342 return (
int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags,
_osx_locale.get()) + 2;
356 if (!supported)
return -1;
358 CFStringCompareFlags flags = kCFCompareLocalized | kCFCompareWidthInsensitive;
359 if (case_insensitive) flags |= kCFCompareCaseInsensitive;
361 CFAutoRelease<CFStringRef> cf_str(CFStringCreateWithBytes(kCFAllocatorDefault, (
const UInt8 *)str.data(), str.size(), kCFStringEncodingUTF8,
false));
362 CFAutoRelease<CFStringRef> cf_value(CFStringCreateWithBytes(kCFAllocatorDefault, (
const UInt8 *)value.data(), value.size(), kCFStringEncodingUTF8,
false));
365 if (cf_str ==
nullptr || cf_value ==
nullptr)
return -1;
367 return CFStringFindWithOptionsAndLocale(cf_str.get(), cf_value.get(), CFRangeMake(0, CFStringGetLength(cf_str.get())), flags,
_osx_locale.get(),
nullptr) ? 1 : 0;
373 const char *string_base = s;
375 this->utf16_to_utf8.clear();
376 this->str_info.clear();
381 std::vector<UniChar> utf16_str;
383 size_t idx = s - string_base;
385 char32_t c = Utf8Consume(&s);
387 utf16_str.push_back((UniChar)c);
390 utf16_str.push_back((UniChar)(0xD800 + ((c - 0x10000) >> 10)));
391 utf16_str.push_back((UniChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
392 this->utf16_to_utf8.push_back(idx);
394 this->utf16_to_utf8.push_back(idx);
396 this->utf16_to_utf8.push_back(s - string_base);
399 this->str_info.resize(utf16_to_utf8.size());
401 if (!utf16_str.empty()) {
402 CFAutoRelease<CFStringRef> str(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull));
405 for (CFIndex i = 0; i < CFStringGetLength(str.get()); ) {
406 CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str.get(), i);
407 this->str_info[r.location].char_stop =
true;
415 CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
416 while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer.get())) != kCFStringTokenizerTokenNone) {
418 if ((tokenType & kCFStringTokenizerTokenHasNonLettersMask) != kCFStringTokenizerTokenHasNonLettersMask) {
419 CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer.get());
420 this->str_info[r.location].word_stop =
true;
426 this->str_info.back().char_stop =
true;
427 this->str_info.back().word_stop =
true;
433 size_t utf16_pos = 0;
434 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
435 if (this->utf16_to_utf8[i] == pos) {
442 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
443 this->cur_pos = utf16_pos;
445 return this->utf16_to_utf8[this->cur_pos];
450 assert(this->cur_pos <= this->utf16_to_utf8.size());
453 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
457 }
while (this->cur_pos < this->utf16_to_utf8.size() && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
459 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
464 assert(this->cur_pos <= this->utf16_to_utf8.size());
467 if (this->cur_pos == 0)
return END;
471 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
473 return this->utf16_to_utf8[this->cur_pos];
476 std::unique_ptr<StringIterator> OSXStringIterator::Create()
480 return std::make_unique<OSXStringIterator>();