aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--demos/input_method/main.cpp2
-rw-r--r--include/cru/common/StringUtil.hpp2
-rw-r--r--src/common/String.cpp4
-rw-r--r--src/osx/Convert.cpp20
-rw-r--r--src/osx/graphics/quartz/Font.cpp7
-rw-r--r--src/osx/graphics/quartz/TextLayout.cpp40
-rw-r--r--src/osx/gui/Window.mm18
-rw-r--r--test/common/StringTest.cpp11
8 files changed, 53 insertions, 51 deletions
diff --git a/demos/input_method/main.cpp b/demos/input_method/main.cpp
index cc86ce4d..67743239 100644
--- a/demos/input_method/main.cpp
+++ b/demos/input_method/main.cpp
@@ -31,7 +31,7 @@ int main() {
auto target_clause_brush = graphics_factory->CreateSolidColorBrush();
target_clause_brush->SetColor(colors::blue);
- std::shared_ptr<IFont> font = graphics_factory->CreateFont(u"Serif", 30);
+ std::shared_ptr<IFont> font = graphics_factory->CreateFont(String{}, 30);
float window_width = 10000;
diff --git a/include/cru/common/StringUtil.hpp b/include/cru/common/StringUtil.hpp
index 5c230898..4291a0da 100644
--- a/include/cru/common/StringUtil.hpp
+++ b/include/cru/common/StringUtil.hpp
@@ -110,7 +110,7 @@ class CodePointIterator {
private:
StringType string_;
Index position_;
- mutable Index next_position_cache_;
+ mutable Index next_position_cache_ = 0;
};
using Utf8CodePointIterator =
diff --git a/src/common/String.cpp b/src/common/String.cpp
index 30f83174..65d303fb 100644
--- a/src/common/String.cpp
+++ b/src/common/String.cpp
@@ -214,11 +214,11 @@ std::string String::ToUtf8() const {
Index String::IndexFromCodeUnitToCodePoint(Index code_unit_index) const {
auto iter = CodePointIterator();
Index result = 0;
- while (iter.GetPosition() <= code_unit_index && !iter.IsPastEnd()) {
+ while (iter.GetPosition() < code_unit_index && !iter.IsPastEnd()) {
++iter;
++result;
}
- return result - 1;
+ return result;
}
Index String::IndexFromCodePointToCodeUnit(Index code_point_index) const {
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();
}
diff --git a/test/common/StringTest.cpp b/test/common/StringTest.cpp
index a0895af9..53ed359f 100644
--- a/test/common/StringTest.cpp
+++ b/test/common/StringTest.cpp
@@ -11,6 +11,17 @@ TEST(String, Append) {
ASSERT_EQ(s, String(u"haha"));
}
+TEST(String, IndexConvert) {
+ using cru::String;
+
+ String s(u"123");
+ ASSERT_EQ(s.IndexFromCodePointToCodeUnit(1), 1);
+ ASSERT_EQ(s.IndexFromCodeUnitToCodePoint(1), 1);
+ ASSERT_EQ(s.IndexFromCodeUnitToCodePoint(3), 3);
+ ASSERT_EQ(s.IndexFromCodeUnitToCodePoint(3), 3);
+
+}
+
TEST(String, Format) {
using cru::Format;
using cru::String;