10 #include "../../stdafx.h"
12 #include "../../string_func.h"
13 #include "../../strings_func.h"
14 #include "../../table/control_codes.h"
15 #include "../../fontcache.h"
18 #include <CoreFoundation/CoreFoundation.h>
22 #ifndef HAVE_OSX_109_SDK
24 typedef const struct __CTRunDelegate * CTRunDelegateRef;
26 typedef void (*CTRunDelegateDeallocateCallback) (
void* refCon);
27 typedef CGFloat (*CTRunDelegateGetAscentCallback) (
void* refCon);
28 typedef CGFloat (*CTRunDelegateGetDescentCallback) (
void* refCon);
29 typedef CGFloat (*CTRunDelegateGetWidthCallback) (
void* refCon);
32 CTRunDelegateDeallocateCallback dealloc;
33 CTRunDelegateGetAscentCallback getAscent;
34 CTRunDelegateGetDescentCallback getDescent;
35 CTRunDelegateGetWidthCallback getWidth;
39 kCTRunDelegateVersion1 = 1,
40 kCTRunDelegateCurrentVersion = kCTRunDelegateVersion1
43 extern const CFStringRef kCTRunDelegateAttributeName AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
45 CTRunDelegateRef CTRunDelegateCreate(
const CTRunDelegateCallbacks* callbacks,
void* refCon) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
72 std::vector<GlyphID> glyphs;
73 std::vector<float> positions;
74 std::vector<int> glyph_to_char;
76 int total_advance = 0;
83 const GlyphID *GetGlyphs()
const override {
return &this->glyphs[0]; }
84 const float *GetPositions()
const override {
return &this->positions[0]; }
85 const int *GetGlyphToCharMap()
const override {
return &this->glyph_to_char[0]; }
87 const Font *GetFont()
const override {
return this->font; }
88 int GetLeading()
const override {
return this->font->fc->GetHeight(); }
89 int GetGlyphCount()
const override {
return (
int)this->glyphs.size(); }
90 int GetAdvance()
const {
return this->total_advance; }
98 CFArrayRef runs = CTLineGetGlyphRuns(line.get());
99 for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
100 CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
103 CFRange chars = CTRunGetStringRange(run);
104 auto map = fontMapping.begin();
105 while (map < fontMapping.end() - 1 && map->first <= chars.location) map++;
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(
WChar 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 WChar c = (
WChar)((
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());
174 for (
const auto &i : fontMapping) {
175 if (i.first - last == 0)
continue;
177 CTFontRef font = (CTFontRef)i.second->fc->GetOSHandle();
178 if (font ==
nullptr) {
181 CFAutoRelease<CFStringRef> font_name(CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName(), kCFStringEncodingUTF8));
182 _font_cache[i.second->fc->GetSize()].reset(CTFontCreateWithName(font_name.get(), i.second->fc->GetFontSize(),
nullptr));
186 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTFontAttributeName, font);
188 CGColorRef color = CGColorCreateGenericGray((uint8)i.second->colour / 255.0f, 1.0f);
189 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
190 CGColorRelease(color);
193 for (ssize_t c = last; c < i.first; c++) {
194 if (buff[c] >= SCC_SPRITE_START && buff[c] <= SCC_SPRITE_END) {
196 CFAttributedStringSetAttribute(str.get(), CFRangeMake(c, 1), kCTRunDelegateAttributeName, del.get());
202 CFAttributedStringEndEditing(str.get());
210 std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(
int max_width)
212 if (this->
cur_offset >= this->length)
return nullptr;
215 CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter.get(), this->cur_offset, max_width);
216 if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter.get(), this->cur_offset, max_width);
222 return std::unique_ptr<const Line>(line ?
new CoreTextLine(std::move(line), this->font_map, this->text_buffer) :
nullptr);
227 this->glyphs.resize(CTRunGetGlyphCount(run));
230 CFIndex map[this->glyphs.size()];
231 CTRunGetStringIndices(run, CFRangeMake(0, 0), map);
233 this->glyph_to_char.resize(this->glyphs.size());
234 for (
size_t i = 0; i < this->glyph_to_char.size(); i++) this->glyph_to_char[i] = (
int)map[i];
236 CGPoint pts[this->glyphs.size()];
237 CTRunGetPositions(run, CFRangeMake(0, 0), pts);
238 this->positions.resize(this->glyphs.size() * 2 + 2);
242 CGGlyph gl[this->glyphs.size()];
243 CTRunGetGlyphs(run, CFRangeMake(0, 0), gl);
244 for (
size_t i = 0; i < this->glyphs.size(); i++) {
245 if (buff[this->glyph_to_char[i]] >= SCC_SPRITE_START && buff[this->glyph_to_char[i]] <= SCC_SPRITE_END) {
246 this->glyphs[i] = font->fc->MapCharToGlyph(buff[this->glyph_to_char[i]]);
247 this->positions[i * 2 + 0] = pts[i].x;
248 this->positions[i * 2 + 1] = font->fc->GetAscender() - font->fc->GetGlyph(this->glyphs[i])->height - 1;
250 this->glyphs[i] = gl[i];
251 this->positions[i * 2 + 0] = pts[i].x;
252 this->positions[i * 2 + 1] = pts[i].y;
255 this->total_advance = (int)CTRunGetTypographicBounds(run, CFRangeMake(0, 0),
nullptr,
nullptr,
nullptr);
256 this->positions[this->glyphs.size() * 2] = this->positions[0] + this->total_advance;
266 for (
const auto &run : *
this) {
267 leading = std::max(leading, run.GetLeading());
279 if (this->size() == 0)
return 0;
282 for (
const auto &run : *
this) {
283 total_width += run.GetAdvance();
302 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
304 CTFontManagerRegisterFontsForURL(url.get(), kCTFontManagerScopeProcess,
nullptr);
313 _osx_locale.reset(CFLocaleCreate(kCFAllocatorDefault, iso.get()));
326 if (!supported)
return 0;
328 CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
334 if (cf1 ==
nullptr || cf2 ==
nullptr)
return 0;
336 return (
int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags,
_osx_locale.get()) + 2;
342 const char *string_base = s;
344 this->utf16_to_utf8.clear();
345 this->str_info.clear();
350 std::vector<UniChar> utf16_str;
352 size_t idx = s - string_base;
354 WChar c = Utf8Consume(&s);
356 utf16_str.push_back((UniChar)c);
359 utf16_str.push_back((UniChar)(0xD800 + ((c - 0x10000) >> 10)));
360 utf16_str.push_back((UniChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
361 this->utf16_to_utf8.push_back(idx);
363 this->utf16_to_utf8.push_back(idx);
365 this->utf16_to_utf8.push_back(s - string_base);
368 this->str_info.resize(utf16_to_utf8.size());
370 if (utf16_str.size() > 0) {
371 CFAutoRelease<CFStringRef> str(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull));
374 for (CFIndex i = 0; i < CFStringGetLength(str.get()); ) {
375 CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str.get(), i);
376 this->str_info[r.location].char_stop =
true;
384 CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
385 while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer.get())) != kCFStringTokenizerTokenNone) {
387 if ((tokenType & kCFStringTokenizerTokenHasNonLettersMask) != kCFStringTokenizerTokenHasNonLettersMask) {
388 CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer.get());
389 this->str_info[r.location].word_stop =
true;
395 this->str_info.back().char_stop =
true;
396 this->str_info.back().word_stop =
true;
402 size_t utf16_pos = 0;
403 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
404 if (this->utf16_to_utf8[i] == pos) {
411 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
412 this->cur_pos = utf16_pos;
414 return this->utf16_to_utf8[this->cur_pos];
419 assert(this->cur_pos <= this->utf16_to_utf8.size());
422 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
426 }
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));
428 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
433 assert(this->cur_pos <= this->utf16_to_utf8.size());
436 if (this->cur_pos == 0)
return END;
440 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
442 return this->utf16_to_utf8[this->cur_pos];