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"
20 #include <CoreFoundation/CoreFoundation.h>
24 #ifndef HAVE_OSX_109_SDK
26 typedef const struct __CTRunDelegate * CTRunDelegateRef;
28 typedef void (*CTRunDelegateDeallocateCallback) (
void* refCon);
29 typedef CGFloat (*CTRunDelegateGetAscentCallback) (
void* refCon);
30 typedef CGFloat (*CTRunDelegateGetDescentCallback) (
void* refCon);
31 typedef CGFloat (*CTRunDelegateGetWidthCallback) (
void* refCon);
34 CTRunDelegateDeallocateCallback dealloc;
35 CTRunDelegateGetAscentCallback getAscent;
36 CTRunDelegateGetDescentCallback getDescent;
37 CTRunDelegateGetWidthCallback getWidth;
41 kCTRunDelegateVersion1 = 1,
42 kCTRunDelegateCurrentVersion = kCTRunDelegateVersion1
45 extern const CFStringRef kCTRunDelegateAttributeName AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
47 CTRunDelegateRef CTRunDelegateCreate(
const CTRunDelegateCallbacks* callbacks,
void* refCon) AVAILABLE_MAC_OS_X_VERSION_10_5_AND_LATER;
74 std::vector<GlyphID> glyphs;
75 std::vector<float> positions;
76 std::vector<int> glyph_to_char;
78 int total_advance = 0;
85 const GlyphID *GetGlyphs()
const override {
return &this->glyphs[0]; }
86 const float *GetPositions()
const override {
return &this->positions[0]; }
87 const int *GetGlyphToCharMap()
const override {
return &this->glyph_to_char[0]; }
89 const Font *GetFont()
const override {
return this->font; }
90 int GetLeading()
const override {
return this->font->fc->GetHeight(); }
91 int GetGlyphCount()
const override {
return (
int)this->glyphs.size(); }
92 int GetAdvance()
const {
return this->total_advance; }
100 CFArrayRef runs = CTLineGetGlyphRuns(line.get());
101 for (CFIndex i = 0; i < CFArrayGetCount(runs); i++) {
102 CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, i);
105 CFRange chars = CTRunGetStringRange(run);
106 auto map = fontMapping.begin();
107 while (map < fontMapping.end() - 1 && map->first <= chars.location) map++;
109 this->emplace_back(run, map->second, buff);
115 int CountRuns()
const override {
return this->size(); }
116 const VisualRun &GetVisualRun(
int run)
const override {
return this->at(run); }
118 int GetInternalCharLength(
WChar c)
const override
121 return c >= 0x010000U ? 2 : 1;
130 void Reflow()
override
132 this->cur_offset = 0;
135 std::unique_ptr<const Line> NextLine(
int max_width)
override;
143 WChar c = (
WChar)((
size_t)ref_con & 0xFFFFFF);
149 kCTRunDelegateCurrentVersion,
nullptr,
nullptr,
nullptr,
158 ptrdiff_t length = buff_end - buff;
159 if (length == 0)
return nullptr;
162 for (
const auto &i : fontMapping) {
163 if (i.second->fc->IsBuiltInFont())
return nullptr;
168 CFAttributedStringBeginEditing(str.get());
171 CFAttributedStringReplaceString(str.get(), CFRangeMake(0, 0), base.get());
176 for (
const auto &i : fontMapping) {
177 if (i.first - last == 0)
continue;
179 CTFontRef font = (CTFontRef)i.second->fc->GetOSHandle();
180 if (font ==
nullptr) {
183 CFAutoRelease<CFStringRef> font_name(CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName().c_str(), kCFStringEncodingUTF8));
184 _font_cache[i.second->fc->GetSize()].reset(CTFontCreateWithName(font_name.get(), i.second->fc->GetFontSize(),
nullptr));
188 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTFontAttributeName, font);
190 CGColorRef color = CGColorCreateGenericGray((uint8)i.second->colour / 255.0f, 1.0f);
191 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
192 CGColorRelease(color);
195 for (ssize_t c = last; c < i.first; c++) {
196 if (buff[c] >= SCC_SPRITE_START && buff[c] <= SCC_SPRITE_END) {
198 CFAttributedStringSetAttribute(str.get(), CFRangeMake(c, 1), kCTRunDelegateAttributeName, del.get());
204 CFAttributedStringEndEditing(str.get());
212 std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(
int max_width)
214 if (this->
cur_offset >= this->length)
return nullptr;
217 CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter.get(), this->cur_offset, max_width);
218 if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter.get(), this->cur_offset, max_width);
224 return std::unique_ptr<const Line>(line ?
new CoreTextLine(std::move(line), this->font_map, this->text_buffer) :
nullptr);
229 this->glyphs.resize(CTRunGetGlyphCount(run));
232 CFIndex map[this->glyphs.size()];
233 CTRunGetStringIndices(run, CFRangeMake(0, 0), map);
235 this->glyph_to_char.resize(this->glyphs.size());
236 for (
size_t i = 0; i < this->glyph_to_char.size(); i++) this->glyph_to_char[i] = (
int)map[i];
238 CGPoint pts[this->glyphs.size()];
239 CTRunGetPositions(run, CFRangeMake(0, 0), pts);
240 this->positions.resize(this->glyphs.size() * 2 + 2);
244 CGGlyph gl[this->glyphs.size()];
245 CTRunGetGlyphs(run, CFRangeMake(0, 0), gl);
246 for (
size_t i = 0; i < this->glyphs.size(); i++) {
247 if (buff[this->glyph_to_char[i]] >= SCC_SPRITE_START && buff[this->glyph_to_char[i]] <= SCC_SPRITE_END) {
248 this->glyphs[i] = font->fc->MapCharToGlyph(buff[this->glyph_to_char[i]]);
249 this->positions[i * 2 + 0] = pts[i].x;
250 this->positions[i * 2 + 1] = (font->fc->GetHeight() -
ScaleSpriteTrad(FontCache::GetDefaultFontHeight(font->fc->GetSize()))) / 2;
252 this->glyphs[i] = gl[i];
253 this->positions[i * 2 + 0] = pts[i].x;
254 this->positions[i * 2 + 1] = pts[i].y;
257 this->total_advance = (int)std::ceil(CTRunGetTypographicBounds(run, CFRangeMake(0, 0),
nullptr,
nullptr,
nullptr));
258 this->positions[this->glyphs.size() * 2] = this->positions[0] + this->total_advance;
268 for (
const auto &run : *
this) {
269 leading = std::max(leading, run.GetLeading());
281 if (this->size() == 0)
return 0;
284 for (
const auto &run : *
this) {
285 total_width += run.GetAdvance();
304 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
306 CTFontManagerRegisterFontsForURL(url.get(), kCTFontManagerScopeProcess,
nullptr);
315 _osx_locale.reset(CFLocaleCreate(kCFAllocatorDefault, iso.get()));
328 if (!supported)
return 0;
330 CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
336 if (cf1 ==
nullptr || cf2 ==
nullptr)
return 0;
338 return (
int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags,
_osx_locale.get()) + 2;
344 const char *string_base = s;
346 this->utf16_to_utf8.clear();
347 this->str_info.clear();
352 std::vector<UniChar> utf16_str;
354 size_t idx = s - string_base;
356 WChar c = Utf8Consume(&s);
358 utf16_str.push_back((UniChar)c);
361 utf16_str.push_back((UniChar)(0xD800 + ((c - 0x10000) >> 10)));
362 utf16_str.push_back((UniChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
363 this->utf16_to_utf8.push_back(idx);
365 this->utf16_to_utf8.push_back(idx);
367 this->utf16_to_utf8.push_back(s - string_base);
370 this->str_info.resize(utf16_to_utf8.size());
372 if (utf16_str.size() > 0) {
373 CFAutoRelease<CFStringRef> str(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull));
376 for (CFIndex i = 0; i < CFStringGetLength(str.get()); ) {
377 CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str.get(), i);
378 this->str_info[r.location].char_stop =
true;
386 CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
387 while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer.get())) != kCFStringTokenizerTokenNone) {
389 if ((tokenType & kCFStringTokenizerTokenHasNonLettersMask) != kCFStringTokenizerTokenHasNonLettersMask) {
390 CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer.get());
391 this->str_info[r.location].word_stop =
true;
397 this->str_info.back().char_stop =
true;
398 this->str_info.back().word_stop =
true;
404 size_t utf16_pos = 0;
405 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
406 if (this->utf16_to_utf8[i] == pos) {
413 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
414 this->cur_pos = utf16_pos;
416 return this->utf16_to_utf8[this->cur_pos];
421 assert(this->cur_pos <= this->utf16_to_utf8.size());
424 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
428 }
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));
430 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
435 assert(this->cur_pos <= this->utf16_to_utf8.size());
438 if (this->cur_pos == 0)
return END;
442 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
444 return this->utf16_to_utf8[this->cur_pos];