aboutsummaryrefslogtreecommitdiff
path: root/src/platform/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/graphics')
-rw-r--r--src/platform/graphics/direct2d/Factory.cpp6
-rw-r--r--src/platform/graphics/direct2d/Font.cpp13
-rw-r--r--src/platform/graphics/direct2d/Resource.cpp2
-rw-r--r--src/platform/graphics/direct2d/TextLayout.cpp46
4 files changed, 45 insertions, 22 deletions
diff --git a/src/platform/graphics/direct2d/Factory.cpp b/src/platform/graphics/direct2d/Factory.cpp
index cf65c2a5..af3f174d 100644
--- a/src/platform/graphics/direct2d/Factory.cpp
+++ b/src/platform/graphics/direct2d/Factory.cpp
@@ -82,13 +82,13 @@ DirectGraphicsFactory::CreateGeometryBuilder() {
return std::make_unique<D2DGeometryBuilder>(this);
}
-std::unique_ptr<IFont> DirectGraphicsFactory::CreateFont(String font_family,
- float font_size) {
+std::unique_ptr<IFont> DirectGraphicsFactory::CreateFont(
+ std::string font_family, float font_size) {
return std::make_unique<DWriteFont>(this, std::move(font_family), font_size);
}
std::unique_ptr<ITextLayout> DirectGraphicsFactory::CreateTextLayout(
- std::shared_ptr<IFont> font, String text) {
+ std::shared_ptr<IFont> font, std::string text) {
return std::make_unique<DWriteTextLayout>(this, std::move(font),
std::move(text));
}
diff --git a/src/platform/graphics/direct2d/Font.cpp b/src/platform/graphics/direct2d/Font.cpp
index 34bb2ea5..f8dfbf38 100644
--- a/src/platform/graphics/direct2d/Font.cpp
+++ b/src/platform/graphics/direct2d/Font.cpp
@@ -1,5 +1,6 @@
#include "cru/platform/graphics/direct2d/Font.h"
+#include "cru/base/StringUtil.h"
#include "cru/platform/graphics/direct2d/Exception.h"
#include "cru/platform/graphics/direct2d/Factory.h"
@@ -7,7 +8,7 @@
#include <utility>
namespace cru::platform::graphics::direct2d {
-DWriteFont::DWriteFont(DirectGraphicsFactory* factory, String font_family,
+DWriteFont::DWriteFont(DirectGraphicsFactory* factory, std::string font_family,
float font_size)
: DirectGraphicsResource(factory), font_family_(std::move(font_family)) {
// Get locale
@@ -15,19 +16,19 @@ DWriteFont::DWriteFont(DirectGraphicsFactory* factory, String font_family,
if (!::GetUserDefaultLocaleName(buffer.data(),
static_cast<int>(buffer.size())))
throw platform::win::Win32Error(
- ::GetLastError(), u"Failed to get locale when create dwrite font.");
+ ::GetLastError(), "Failed to get locale when create dwrite font.");
ThrowIfFailed(factory->GetDWriteFactory()->CreateTextFormat(
- 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_));
+ string::ToUtf16(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));
ThrowIfFailed(
text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR));
}
-String DWriteFont::GetFontName() { return font_family_; }
+std::string DWriteFont::GetFontName() { return font_family_; }
float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); }
} // namespace cru::platform::graphics::direct2d
diff --git a/src/platform/graphics/direct2d/Resource.cpp b/src/platform/graphics/direct2d/Resource.cpp
index b81aa69e..2f1b7654 100644
--- a/src/platform/graphics/direct2d/Resource.cpp
+++ b/src/platform/graphics/direct2d/Resource.cpp
@@ -3,7 +3,7 @@
#include "cru/platform/graphics/direct2d/Factory.h"
namespace cru::platform::graphics::direct2d {
-String DirectResource::kPlatformId = u"Windows Direct";
+std::string DirectResource::kPlatformId = "Windows Direct";
DirectGraphicsResource::DirectGraphicsResource(DirectGraphicsFactory* factory)
: factory_(factory) {
diff --git a/src/platform/graphics/direct2d/TextLayout.cpp b/src/platform/graphics/direct2d/TextLayout.cpp
index 06bbcaa6..906d64ec 100644
--- a/src/platform/graphics/direct2d/TextLayout.cpp
+++ b/src/platform/graphics/direct2d/TextLayout.cpp
@@ -1,6 +1,7 @@
#include "cru/platform/graphics/direct2d/TextLayout.h"
#include <dwrite.h>
+#include "cru/base/StringUtil.h"
#include "cru/base/log/Logger.h"
#include "cru/platform/Check.h"
#include "cru/platform/graphics/direct2d/Exception.h"
@@ -11,27 +12,28 @@
namespace cru::platform::graphics::direct2d {
DWriteTextLayout::DWriteTextLayout(DirectGraphicsFactory* factory,
- std::shared_ptr<IFont> font, String text)
+ std::shared_ptr<IFont> font,
+ std::string text)
: DirectGraphicsResource(factory), text_(std::move(text)) {
Expects(font);
font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
+ utf16_text_ = string::ToUtf16(text_);
ThrowIfFailed(factory->GetDWriteFactory()->CreateTextLayout(
- reinterpret_cast<const wchar_t*>(text_.c_str()),
- static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
- max_height_, &text_layout_));
+ utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()),
+ font_->GetComInterface(), max_width_, max_height_, &text_layout_));
}
DWriteTextLayout::~DWriteTextLayout() = default;
-String DWriteTextLayout::GetText() { return text_; }
+std::string DWriteTextLayout::GetText() { return text_; }
-void DWriteTextLayout::SetText(String new_text) {
+void DWriteTextLayout::SetText(std::string new_text) {
text_ = std::move(new_text);
+ utf16_text_ = string::ToUtf16(text_);
ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
- reinterpret_cast<const wchar_t*>(text_.c_str()),
- static_cast<UINT32>(text_.size()), font_->GetComInterface(), max_width_,
- max_height_, &text_layout_));
+ utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()),
+ font_->GetComInterface(), max_width_, max_height_, &text_layout_));
}
std::shared_ptr<IFont> DWriteTextLayout::GetFont() {
@@ -70,7 +72,7 @@ Index DWriteTextLayout::GetLineIndexFromCharIndex(Index char_index) {
auto line_index = 0;
for (Index i = 0; i < char_index; ++i) {
- if (text_[i] == u'\n') {
+ if (text_[i] == '\n') {
line_index++;
}
}
@@ -108,7 +110,19 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect(
if (text_range_arg.count == 0) {
return {};
}
- const auto text_range = text_range_arg.Normalize();
+ const auto text_range =
+ Range::FromTwoSides(
+ string::Utf16IndexCodePointToCodeUnit(
+ reinterpret_cast<const char16_t*>(utf16_text_.data()),
+ utf16_text_.size(),
+ string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(),
+ text_range_arg.GetStart())),
+ string::Utf16IndexCodePointToCodeUnit(
+ reinterpret_cast<const char16_t*>(utf16_text_.data()),
+ utf16_text_.size(),
+ string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(),
+ text_range_arg.GetEnd())))
+ .Normalize();
DWRITE_TEXT_METRICS text_metrics;
ThrowIfFailed(text_layout_->GetMetrics(&text_metrics));
@@ -137,6 +151,10 @@ std::vector<Rect> DWriteTextLayout::TextRangeRect(
}
Rect DWriteTextLayout::TextSinglePoint(Index position, bool trailing) {
+ position = string::Utf16IndexCodePointToCodeUnit(
+ reinterpret_cast<const char16_t*>(utf16_text_.data()), utf16_text_.size(),
+ string::Utf8IndexCodeUnitToCodePoint(text_.data(), text_.size(),
+ position));
DWRITE_HIT_TEST_METRICS metrics;
FLOAT left;
FLOAT top;
@@ -155,7 +173,11 @@ TextHitTestResult DWriteTextLayout::HitTest(const Point& point) {
&metrics));
TextHitTestResult result;
- result.position = metrics.textPosition;
+ result.position = string::Utf8IndexCodePointToCodeUnit(
+ text_.data(), text_.size(),
+ string::Utf16IndexCodeUnitToCodePoint(
+ reinterpret_cast<const char16_t*>(utf16_text_.data()),
+ utf16_text_.size(), metrics.textPosition));
result.trailing = trailing != 0;
return result;
}