diff options
author | crupest <crupest@outlook.com> | 2021-10-22 21:42:45 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-10-22 21:42:45 +0800 |
commit | 3e84cf013b31c52405a76b8e8778a5991d096290 (patch) | |
tree | 4d3b2d2ef5a9625c55f798d88281073e806ee83f /src/osx | |
parent | ed640ca9e3c7482856a0248ea38adafc86a7d4c8 (diff) | |
download | cru-3e84cf013b31c52405a76b8e8778a5991d096290.tar.gz cru-3e84cf013b31c52405a76b8e8778a5991d096290.tar.bz2 cru-3e84cf013b31c52405a76b8e8778a5991d096290.zip |
...
Diffstat (limited to 'src/osx')
-rw-r--r-- | src/osx/Convert.cpp | 20 | ||||
-rw-r--r-- | src/osx/graphics/quartz/Font.cpp | 7 | ||||
-rw-r--r-- | src/osx/graphics/quartz/TextLayout.cpp | 40 | ||||
-rw-r--r-- | src/osx/gui/Window.mm | 18 |
4 files changed, 38 insertions, 47 deletions
diff --git a/src/osx/Convert.cpp b/src/osx/Convert.cpp index a237a46f..e8c023e9 100644 --- a/src/osx/Convert.cpp +++ b/src/osx/Convert.cpp @@ -1,4 +1,7 @@ #include "cru/osx/Convert.hpp" +#include <string> + +#include "cru/common/StringUtil.hpp" namespace cru::platform::osx { CFStringRef Convert(const String& string) { @@ -8,16 +11,17 @@ CFStringRef Convert(const String& string) { } String Convert(CFStringRef string) { - auto d = CFStringCreateExternalRepresentation(nullptr, string, - kCFStringEncodingUTF16, 0); - auto l = CFDataGetLength(d); + auto length = CFStringGetLength(string); - auto s = String(reinterpret_cast<const char16_t*>(CFDataGetBytePtr(d)), - CFDataGetLength(d) / 2); + String result; - CFRelease(d); + for (int i = 0; i < length; i++) { + std::u16string s; + Utf16EncodeCodePointAppend(CFStringGetCharacterAtIndex(string, i), s); + result.append(s.data(), s.size()); + } - return s; + return result; } CFRange Convert(const Range& range) { @@ -27,4 +31,4 @@ CFRange Convert(const Range& range) { Range Convert(const CFRange& range) { return Range(range.location, range.length); } -} // namespace cru::cru::platform::osx +} // namespace cru::platform::osx diff --git a/src/osx/graphics/quartz/Font.cpp b/src/osx/graphics/quartz/Font.cpp index 72d6d56d..1499bda6 100644 --- a/src/osx/graphics/quartz/Font.cpp +++ b/src/osx/graphics/quartz/Font.cpp @@ -12,7 +12,12 @@ OsxCTFont::OsxCTFont(IGraphicsFactory* graphics_factory, const String& name, : OsxQuartzResource(graphics_factory) { CFStringRef n = Convert(name); - ct_font_ = CTFontCreateWithName(n, size, nullptr); + if (name.empty()) { + ct_font_ = + CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, size, nullptr); + } else { + ct_font_ = CTFontCreateWithName(n, size, nullptr); + } Ensures(ct_font_); CFRelease(n); diff --git a/src/osx/graphics/quartz/TextLayout.cpp b/src/osx/graphics/quartz/TextLayout.cpp index dd6b908d..29735606 100644 --- a/src/osx/graphics/quartz/TextLayout.cpp +++ b/src/osx/graphics/quartz/TextLayout.cpp @@ -21,17 +21,13 @@ OsxCTTextLayout::OsxCTTextLayout(IGraphicsFactory* graphics_factory, Expects(font_); CFStringRef s = Convert(text_); - CFDictionaryRef attributes = - CFDictionaryCreateMutable(nullptr, 0, nullptr, nullptr); - - Ensures(s); - Ensures(attributes); - cf_attributed_text_ = CFAttributedStringCreateMutable(nullptr, 0); CFAttributedStringReplaceString(cf_attributed_text_, CFRangeMake(0, 0), s); Ensures(cf_attributed_text_); - - CFRelease(attributes); + CFAttributedStringSetAttribute( + cf_attributed_text_, + CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), + kCTFontAttributeName, font_->GetCTFont()); CFRelease(s); RecreateFrame(); @@ -53,29 +49,13 @@ void OsxCTTextLayout::SetText(String new_text) { CFRelease(cf_attributed_text_); CFStringRef s = Convert(text_); - CFDictionaryRef attributes = - CFDictionaryCreateMutable(nullptr, 0, nullptr, nullptr); - - Ensures(s); - Ensures(attributes); - cf_attributed_text_ = CFAttributedStringCreateMutable(nullptr, 0); CFAttributedStringReplaceString(cf_attributed_text_, CFRangeMake(0, 0), s); Ensures(cf_attributed_text_); - CFAttributedStringSetAttribute( cf_attributed_text_, CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), kCTFontAttributeName, font_->GetCTFont()); - - CGColorRef cg_color = CGColorCreateGenericRGB(0, 0, 0, 1); - CFAttributedStringSetAttribute( - cf_attributed_text_, - CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), - kCTForegroundColorAttributeName, cg_color); - - CGColorRelease(cg_color); - CFRelease(attributes); CFRelease(s); RecreateFrame(); @@ -159,6 +139,10 @@ Point OsxCTTextLayout::TextSinglePoint(Index position, bool trailing) { if (range.GetStart() <= position && position < range.GetEnd()) { auto offset = CTLineGetOffsetForStringIndex(line, position, nullptr); return Point(line_origin.x + offset, line_origin.y); + } else if (position == range.GetEnd()) { + return Point( + line_origin.x + CTLineGetImageBounds(line, nullptr).size.width, + line_origin.y); } } @@ -211,19 +195,13 @@ void OsxCTTextLayout::RecreateFrame() { Ensures(path); CGPathAddRect(path, nullptr, CGRectMake(0, 0, max_width_, suggest_height_)); - CFMutableDictionaryRef dictionary = - CFDictionaryCreateMutable(nullptr, 0, &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - CFDictionaryAddValue(dictionary, kCTFontAttributeName, font_->GetCTFont()); - ct_frame_ = CTFramesetterCreateFrame( ct_framesetter_, CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), path, - dictionary); + nullptr); Ensures(ct_frame_); CGPathRelease(path); - CFRelease(dictionary); const auto lines = CTFrameGetLines(ct_frame_); line_count_ = CFArrayGetCount(lines); diff --git a/src/osx/gui/Window.mm b/src/osx/gui/Window.mm index 1eb97b07..84cc1145 100644 --- a/src/osx/gui/Window.mm +++ b/src/osx/gui/Window.mm @@ -487,11 +487,13 @@ IInputMethodContext* OsxWindow::GetInputMethodContext() { return p_->input_metho s = CFAttributedStringGetString(as); } - cru::log::TagDebug( - u"CruView", - u"Received setMarkedText string: {}, selected range: ({}, {}), replacement range: ({}, {}).", - Convert(s), selectedRange.location, selectedRange.length, replacementRange.location, - replacementRange.length); + auto ss = Convert(s); + + cru::log::TagDebug(u"CruView", + u"Received setMarkedText string: {}, selected range: ({}, {}), " + u"replacement range: ({}, {}).", + ss, selectedRange.location, selectedRange.length, replacementRange.location, + replacementRange.length); if (_input_context_text == nil) { _input_context_text = [[NSMutableAttributedString alloc] init]; @@ -506,8 +508,10 @@ IInputMethodContext* OsxWindow::GetInputMethodContext() { return p_->input_metho cru::platform::gui::CompositionText composition_text; composition_text.text = Convert((CFStringRef)[_input_context_text string]); - composition_text.selection.position = replacementRange.location + selectedRange.location; - composition_text.selection.count = selectedRange.length; + composition_text.selection.position = ss.IndexFromCodePointToCodeUnit(selectedRange.location); + composition_text.selection.count = + ss.IndexFromCodePointToCodeUnit(selectedRange.location + selectedRange.length) - + composition_text.selection.position; _input_context_p->SetCompositionText(composition_text); _input_context_p->RaiseCompositionEvent(); } |