aboutsummaryrefslogtreecommitdiff
path: root/src/platform/graphics/direct2d/Font.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/platform/graphics/direct2d/Font.cpp')
-rw-r--r--src/platform/graphics/direct2d/Font.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/platform/graphics/direct2d/Font.cpp b/src/platform/graphics/direct2d/Font.cpp
new file mode 100644
index 00000000..afbc9049
--- /dev/null
+++ b/src/platform/graphics/direct2d/Font.cpp
@@ -0,0 +1,34 @@
+#include "cru/platform/graphics/direct2d/Font.h"
+
+#include "cru/common/Format.h"
+#include "cru/platform/graphics/direct2d/Exception.h"
+#include "cru/platform/graphics/direct2d/Factory.h"
+
+#include <array>
+#include <utility>
+
+namespace cru::platform::graphics::direct2d {
+DWriteFont::DWriteFont(DirectGraphicsFactory* factory, String font_family,
+ float font_size)
+ : DirectGraphicsResource(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(), u"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));
+}
+
+String DWriteFont::GetFontName() { return font_family_; }
+
+float DWriteFont::GetFontSize() { return text_format_->GetFontSize(); }
+} // namespace cru::platform::graphics::direct2d