aboutsummaryrefslogtreecommitdiff
path: root/src/win/graphics/direct/Font.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
committercrupest <crupest@outlook.com>2021-03-24 19:14:19 +0800
commit7f15a1ff9a2007e119798053083a0a87d042990a (patch)
treecb35c01a7eaee867376d959b96c9bbd15df939e5 /src/win/graphics/direct/Font.cpp
parent74956951ee663012df0c3fe4ebe29799cb2f7732 (diff)
parent7703063a5816b089483e78ccd74bb9902ccfbea8 (diff)
downloadcru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.gz
cru-7f15a1ff9a2007e119798053083a0a87d042990a.tar.bz2
cru-7f15a1ff9a2007e119798053083a0a87d042990a.zip
Merge branch 'master' of https://github.com/crupest/CruUI
Diffstat (limited to 'src/win/graphics/direct/Font.cpp')
-rw-r--r--src/win/graphics/direct/Font.cpp31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/win/graphics/direct/Font.cpp b/src/win/graphics/direct/Font.cpp
new file mode 100644
index 00000000..1d6a5c88
--- /dev/null
+++ b/src/win/graphics/direct/Font.cpp
@@ -0,0 +1,31 @@
+#include "cru/win/graphics/direct/Font.hpp"
+
+#include "cru/win/graphics/direct/Exception.hpp"
+#include "cru/win/graphics/direct/Factory.hpp"
+
+#include <array>
+#include <utility>
+
+namespace cru::platform::graphics::win::direct {
+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(),
+ static_cast<int>(buffer.size())))
+ throw platform::win::Win32Error(
+ ::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_));
+
+ ThrowIfFailed(text_format_->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING));
+ ThrowIfFailed(
+ text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_NEAR));
+}
+
+float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); }
+} // namespace cru::platform::graphics::win::direct