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<float> positions;
75 std::vector<int> glyph_to_char;
77 int total_advance = 0;
84 const GlyphID *GetGlyphs()
const override {
return &this->glyphs[0]; }
85 const float *GetPositions()
const override {
return &this->positions[0]; }
86 const int *GetGlyphToCharMap()
const override {
return &this->glyph_to_char[0]; }
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.begin();
106 while (map < fontMapping.end() - 1 && map->first <= chars.location) map++;
108 this->emplace_back(run, map->second, buff);
114 int CountRuns()
const override {
return this->size(); }
115 const VisualRun &GetVisualRun(
int run)
const override {
return this->at(run); }
117 int GetInternalCharLength(
WChar c)
const override
120 return c >= 0x010000U ? 2 : 1;
129 void Reflow()
override
131 this->cur_offset = 0;
134 std::unique_ptr<const Line> NextLine(
int max_width)
override;
142 WChar c = (
WChar)((
size_t)ref_con & 0xFFFFFF);
148 kCTRunDelegateCurrentVersion,
nullptr,
nullptr,
nullptr,
157 ptrdiff_t length = buff_end - buff;
158 if (length == 0)
return nullptr;
161 for (
const auto &i : fontMapping) {
162 if (i.second->fc->IsBuiltInFont())
return nullptr;
167 CFAttributedStringBeginEditing(str.get());
170 CFAttributedStringReplaceString(str.get(), CFRangeMake(0, 0), base.get());
175 for (
const auto &i : fontMapping) {
176 if (i.first - last == 0)
continue;
178 CTFontRef font = (CTFontRef)i.second->fc->GetOSHandle();
179 if (font ==
nullptr) {
182 CFAutoRelease<CFStringRef> font_name(CFStringCreateWithCString(kCFAllocatorDefault, i.second->fc->GetFontName(), kCFStringEncodingUTF8));
183 _font_cache[i.second->fc->GetSize()].reset(CTFontCreateWithName(font_name.get(), i.second->fc->GetFontSize(),
nullptr));
187 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTFontAttributeName, font);
189 CGColorRef color = CGColorCreateGenericGray((uint8)i.second->colour / 255.0f, 1.0f);
190 CFAttributedStringSetAttribute(str.get(), CFRangeMake(last, i.first - last), kCTForegroundColorAttributeName, color);
191 CGColorRelease(color);
194 for (ssize_t c = last; c < i.first; c++) {
195 if (buff[c] >= SCC_SPRITE_START && buff[c] <= SCC_SPRITE_END) {
197 CFAttributedStringSetAttribute(str.get(), CFRangeMake(c, 1), kCTRunDelegateAttributeName, del.get());
203 CFAttributedStringEndEditing(str.get());
211 std::unique_ptr<const ParagraphLayouter::Line> CoreTextParagraphLayout::NextLine(
int max_width)
213 if (this->
cur_offset >= this->length)
return nullptr;
216 CFIndex len = CTTypesetterSuggestLineBreak(this->typesetter.get(), this->cur_offset, max_width);
217 if (len <= 0) len = CTTypesetterSuggestClusterBreak(this->typesetter.get(), this->cur_offset, max_width);
223 return std::unique_ptr<const Line>(line ?
new CoreTextLine(std::move(line), this->font_map, this->text_buffer) :
nullptr);
228 this->glyphs.resize(CTRunGetGlyphCount(run));
231 CFIndex map[this->glyphs.size()];
232 CTRunGetStringIndices(run, CFRangeMake(0, 0), map);
234 this->glyph_to_char.resize(this->glyphs.size());
235 for (
size_t i = 0; i < this->glyph_to_char.size(); i++) this->glyph_to_char[i] = (
int)map[i];
237 CGPoint pts[this->glyphs.size()];
238 CTRunGetPositions(run, CFRangeMake(0, 0), pts);
239 this->positions.resize(this->glyphs.size() * 2 + 2);
243 CGGlyph gl[this->glyphs.size()];
244 CTRunGetGlyphs(run, CFRangeMake(0, 0), gl);
245 for (
size_t i = 0; i < this->glyphs.size(); i++) {
246 if (buff[this->glyph_to_char[i]] >= SCC_SPRITE_START && buff[this->glyph_to_char[i]] <= SCC_SPRITE_END) {
247 this->glyphs[i] = font->fc->MapCharToGlyph(buff[this->glyph_to_char[i]]);
248 this->positions[i * 2 + 0] = pts[i].x;
249 this->positions[i * 2 + 1] = (font->fc->GetHeight() -
ScaleSpriteTrad(FontCache::GetDefaultFontHeight(font->fc->GetSize()))) / 2;
251 this->glyphs[i] = gl[i];
252 this->positions[i * 2 + 0] = pts[i].x;
253 this->positions[i * 2 + 1] = pts[i].y;
256 this->total_advance = (int)CTRunGetTypographicBounds(run, CFRangeMake(0, 0),
nullptr,
nullptr,
nullptr);
257 this->positions[this->glyphs.size() * 2] = this->positions[0] + this->total_advance;
267 for (
const auto &run : *
this) {
268 leading = std::max(leading, run.GetLeading());
280 if (this->size() == 0)
return 0;
283 for (
const auto &run : *
this) {
284 total_width += run.GetAdvance();
303 CFAutoRelease<CFURLRef> url(CFURLCreateWithFileSystemPath(kCFAllocatorDefault, path.get(), kCFURLPOSIXPathStyle,
false));
305 CTFontManagerRegisterFontsForURL(url.get(), kCTFontManagerScopeProcess,
nullptr);
314 _osx_locale.reset(CFLocaleCreate(kCFAllocatorDefault, iso.get()));
327 if (!supported)
return 0;
329 CFStringCompareFlags flags = kCFCompareCaseInsensitive | kCFCompareNumerically | kCFCompareLocalized | kCFCompareWidthInsensitive | kCFCompareForcedOrdering;
335 if (cf1 ==
nullptr || cf2 ==
nullptr)
return 0;
337 return (
int)CFStringCompareWithOptionsAndLocale(cf1.get(), cf2.get(), CFRangeMake(0, CFStringGetLength(cf1.get())), flags,
_osx_locale.get()) + 2;
343 const char *string_base = s;
345 this->utf16_to_utf8.clear();
346 this->str_info.clear();
351 std::vector<UniChar> utf16_str;
353 size_t idx = s - string_base;
355 WChar c = Utf8Consume(&s);
357 utf16_str.push_back((UniChar)c);
360 utf16_str.push_back((UniChar)(0xD800 + ((c - 0x10000) >> 10)));
361 utf16_str.push_back((UniChar)(0xDC00 + ((c - 0x10000) & 0x3FF)));
362 this->utf16_to_utf8.push_back(idx);
364 this->utf16_to_utf8.push_back(idx);
366 this->utf16_to_utf8.push_back(s - string_base);
369 this->str_info.resize(utf16_to_utf8.size());
371 if (utf16_str.size() > 0) {
372 CFAutoRelease<CFStringRef> str(CFStringCreateWithCharactersNoCopy(kCFAllocatorDefault, &utf16_str[0], utf16_str.size(), kCFAllocatorNull));
375 for (CFIndex i = 0; i < CFStringGetLength(str.get()); ) {
376 CFRange r = CFStringGetRangeOfComposedCharactersAtIndex(str.get(), i);
377 this->str_info[r.location].char_stop =
true;
385 CFStringTokenizerTokenType tokenType = kCFStringTokenizerTokenNone;
386 while ((tokenType = CFStringTokenizerAdvanceToNextToken(tokenizer.get())) != kCFStringTokenizerTokenNone) {
388 if ((tokenType & kCFStringTokenizerTokenHasNonLettersMask) != kCFStringTokenizerTokenHasNonLettersMask) {
389 CFRange r = CFStringTokenizerGetCurrentTokenRange(tokenizer.get());
390 this->str_info[r.location].word_stop =
true;
396 this->str_info.back().char_stop =
true;
397 this->str_info.back().word_stop =
true;
403 size_t utf16_pos = 0;
404 for (
size_t i = 0; i < this->utf16_to_utf8.size(); i++) {
405 if (this->utf16_to_utf8[i] == pos) {
412 while (utf16_pos > 0 && !this->str_info[utf16_pos].char_stop) utf16_pos--;
413 this->cur_pos = utf16_pos;
415 return this->utf16_to_utf8[this->cur_pos];
420 assert(this->cur_pos <= this->utf16_to_utf8.size());
423 if (this->cur_pos == this->utf16_to_utf8.size())
return END;
427 }
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));
429 return this->cur_pos == this->utf16_to_utf8.size() ? END : this->utf16_to_utf8[this->cur_pos];
434 assert(this->cur_pos <= this->utf16_to_utf8.size());
437 if (this->cur_pos == 0)
return END;
441 }
while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
443 return this->utf16_to_utf8[this->cur_pos];