From f3719a5128ef911ec61c43a6eed34203f3b9bbb4 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 31 Dec 2019 23:32:55 +0800 Subject: ... --- src/win/graph/direct/text_layout.cpp | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) (limited to 'src/win/graph/direct/text_layout.cpp') diff --git a/src/win/graph/direct/text_layout.cpp b/src/win/graph/direct/text_layout.cpp index a1750799..a070d288 100644 --- a/src/win/graph/direct/text_layout.cpp +++ b/src/win/graph/direct/text_layout.cpp @@ -10,6 +10,9 @@ #include namespace cru::platform::graph::win::direct { +using cru::platform::win::IndexUtf16ToUtf8; +using cru::platform::win::IndexUtf8ToUtf16; + DWriteTextLayout::DWriteTextLayout(DirectGraphFactory* factory, std::shared_ptr font, std::string text) @@ -64,6 +67,12 @@ Rect DWriteTextLayout::GetTextBounds() { } std::vector DWriteTextLayout::TextRangeRect(const TextRange& text_range) { + // TODO: This can be faster with one iteration. + const int start_index = + IndexUtf8ToUtf16(text_, static_cast(text_range.position), w_text_); + const int end_index = IndexUtf8ToUtf16( + text_, static_cast(text_range.position + text_range.count), w_text_); + DWRITE_TEXT_METRICS text_metrics; ThrowIfFailed(text_layout_->GetMetrics(&text_metrics)); const auto metrics_count = @@ -71,8 +80,9 @@ std::vector DWriteTextLayout::TextRangeRect(const TextRange& text_range) { std::vector hit_test_metrics(metrics_count); UINT32 actual_count; - text_layout_->HitTestTextRange(text_range.position, text_range.count, 0, 0, - hit_test_metrics.data(), metrics_count, + text_layout_->HitTestTextRange(static_cast(start_index), + static_cast(end_index - start_index), + 0, 0, hit_test_metrics.data(), metrics_count, &actual_count); hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count, -- cgit v1.2.3