aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph
diff options
context:
space:
mode:
Diffstat (limited to 'src/win/graph')
-rw-r--r--src/win/graph/direct/Factory.cpp10
-rw-r--r--src/win/graph/direct/Font.cpp12
-rw-r--r--src/win/graph/direct/TextLayout.cpp51
3 files changed, 28 insertions, 45 deletions
diff --git a/src/win/graph/direct/Factory.cpp b/src/win/graph/direct/Factory.cpp
index d9213994..03e64e13 100644
--- a/src/win/graph/direct/Factory.cpp
+++ b/src/win/graph/direct/Factory.cpp
@@ -19,8 +19,8 @@ void InitializeCom() {
}
if (hresult == S_FALSE) {
log::Debug(
- "Try to call CoInitializeEx, but it seems COM is already "
- "initialized.");
+ u"Try to call CoInitializeEx, but it seems COM is already "
+ u"initialized.");
}
}
@@ -95,12 +95,12 @@ std::unique_ptr<IGeometryBuilder> DirectGraphFactory::CreateGeometryBuilder() {
}
std::unique_ptr<IFont> DirectGraphFactory::CreateFont(
- const std::string_view& font_family, float font_size) {
- return std::make_unique<DWriteFont>(this, font_family, font_size);
+ std::u16string font_family, float font_size) {
+ return std::make_unique<DWriteFont>(this, std::move(font_family), font_size);
}
std::unique_ptr<ITextLayout> DirectGraphFactory::CreateTextLayout(
- std::shared_ptr<IFont> font, std::string text) {
+ std::shared_ptr<IFont> font, std::u16string text) {
return std::make_unique<DWriteTextLayout>(this, std::move(font),
std::move(text));
}
diff --git a/src/win/graph/direct/Font.cpp b/src/win/graph/direct/Font.cpp
index edbbc59d..34de5b71 100644
--- a/src/win/graph/direct/Font.cpp
+++ b/src/win/graph/direct/Font.cpp
@@ -2,15 +2,14 @@
#include "cru/win/graph/direct/Exception.hpp"
#include "cru/win/graph/direct/Factory.hpp"
-#include "cru/win/String.hpp"
#include <array>
#include <utility>
namespace cru::platform::graph::win::direct {
-DWriteFont::DWriteFont(DirectGraphFactory* factory,
- const std::string_view& font_family, float font_size)
- : DirectGraphResource(factory) {
+DWriteFont::DWriteFont(DirectGraphFactory* factory, std::u16string font_family,
+ float font_size)
+ : DirectGraphResource(factory), font_family_(std::move(font_family)) {
// Get locale
std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
if (!::GetUserDefaultLocaleName(buffer.data(),
@@ -18,10 +17,9 @@ DWriteFont::DWriteFont(DirectGraphFactory* factory,
throw platform::win::Win32Error(
::GetLastError(), "Failed to get locale when create dwrite font.");
- const std::wstring&& wff = cru::platform::win::ToUtf16String(font_family);
-
ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat(
- wff.data(), nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
+ reinterpret_cast<const wchar_t*>(font_family_.c_str()), nullptr,
+ DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, font_size, buffer.data(), &text_format_));
ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING));
diff --git a/src/win/graph/direct/TextLayout.cpp b/src/win/graph/direct/TextLayout.cpp
index 16db77f0..7b5b230f 100644
--- a/src/win/graph/direct/TextLayout.cpp
+++ b/src/win/graph/direct/TextLayout.cpp
@@ -5,38 +5,33 @@
#include "cru/win/graph/direct/Exception.hpp"
#include "cru/win/graph/direct/Factory.hpp"
#include "cru/win/graph/direct/Font.hpp"
-#include "cru/win/String.hpp"
#include <utility>
namespace cru::platform::graph::win::direct {
-using cru::platform::win::IndexUtf16ToUtf8;
-using cru::platform::win::IndexUtf8ToUtf16;
-
DWriteTextLayout::DWriteTextLayout(DirectGraphFactory* factory,
std::shared_ptr<IFont> font,
- std::string text)
+ std::u16string text)
: DirectGraphResource(factory), text_(std::move(text)) {
Expects(font);
font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
- w_text_ = cru::platform::win::ToUtf16String(text_);
-
ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout(
- w_text_.c_str(), static_cast<UINT32>(w_text_.size()),
- font_->GetComInterface(), max_width_, max_height_, &text_layout_));
+ reinterpret_cast<const wchar_t*>(text_.c_str()),
+ static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
+ max_height_, &text_layout_));
}
DWriteTextLayout::~DWriteTextLayout() = default;
-std::string DWriteTextLayout::GetText() { return text_; }
+std::u16string DWriteTextLayout::GetText() { return text_; }
-void DWriteTextLayout::SetText(std::string new_text) {
+void DWriteTextLayout::SetText(std::u16string new_text) {
text_.swap(new_text);
- w_text_ = cru::platform::win::ToUtf16String(text_);
ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
- w_text_.c_str(), static_cast<UINT32>(w_text_.size()),
- font_->GetComInterface(), max_width_, max_height_, &text_layout_));
+ reinterpret_cast<const wchar_t*>(text_.c_str()),
+ static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
+ max_height_, &text_layout_));
}
std::shared_ptr<IFont> DWriteTextLayout::GetFont() {
@@ -46,8 +41,9 @@ std::shared_ptr<IFont> DWriteTextLayout::GetFont() {
void DWriteTextLayout::SetFont(std::shared_ptr<IFont> font) {
font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
- w_text_.c_str(), static_cast<UINT32>(w_text_.size()),
- font_->GetComInterface(), max_width_, max_height_, &text_layout_));
+ reinterpret_cast<const wchar_t*>(text_.c_str()),
+ static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
+ max_height_, &text_layout_));
}
void DWriteTextLayout::SetMaxWidth(float max_width) {
@@ -73,12 +69,6 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect(
}
const auto text_range = text_range_arg.Normalize();
- // TODO: This can be faster with one iteration.
- const int start_index =
- IndexUtf8ToUtf16(text_, static_cast<int>(text_range.position), w_text_);
- const int end_index = IndexUtf8ToUtf16(
- text_, static_cast<int>(text_range.position + text_range.count), w_text_);
-
DWRITE_TEXT_METRICS text_metrics;
ThrowIfFailed(text_layout_->GetMetrics(&text_metrics));
const auto metrics_count =
@@ -87,9 +77,9 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect(
std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count);
UINT32 actual_count;
ThrowIfFailed(text_layout_->HitTestTextRange(
- static_cast<UINT32>(start_index),
- static_cast<UINT32>(end_index - start_index), 0, 0,
- hit_test_metrics.data(), metrics_count, &actual_count));
+ static_cast<UINT32>(text_range.position),
+ static_cast<UINT32>(text_range.count), 0, 0, hit_test_metrics.data(),
+ metrics_count, &actual_count));
hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count,
hit_test_metrics.cend());
@@ -105,14 +95,11 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect(
return result;
}
-Point DWriteTextLayout::TextSinglePoint(gsl::index position, bool trailing) {
- const auto index =
- IndexUtf8ToUtf16(text_, static_cast<int>(position), w_text_);
-
+Point DWriteTextLayout::TextSinglePoint(Index position, bool trailing) {
DWRITE_HIT_TEST_METRICS metrics;
FLOAT left;
FLOAT top;
- ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast<UINT32>(index),
+ ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast<UINT32>(position),
static_cast<BOOL>(trailing),
&left, &top, &metrics));
return Point{left, top};
@@ -126,10 +113,8 @@ TextHitTestResult DWriteTextLayout::HitTest(const Point& point) {
ThrowIfFailed(text_layout_->HitTestPoint(point.x, point.y, &trailing, &inside,
&metrics));
- const auto index =
- IndexUtf16ToUtf8(w_text_, static_cast<int>(metrics.textPosition), text_);
TextHitTestResult result;
- result.position = index;
+ result.position = metrics.textPosition;
result.trailing = trailing != 0;
result.insideText = inside != 0;
return result;