aboutsummaryrefslogtreecommitdiff
path: root/src/platform_win/win_font.cpp
blob: bca70b9f7d48891c9a415da674e2d312dedb9770 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "cru/platform/win/win_font.hpp"

#include "cru/platform/win/exception.hpp"
#include "cru/platform/win/graph_manager.hpp"

#include <array>
#include <utility>

namespace cru::platform::win {
WinFontDescriptor::WinFontDescriptor(GraphManager* graph_manager,
                                     const std::wstring_view& font_family,
                                     float font_size) {
  std::array<wchar_t, LOCALE_NAME_MAX_LENGTH> buffer;
  if (!::GetUserDefaultLocaleName(buffer.data(), buffer.size()))
    throw Win32Error(::GetLastError(), "Failed to get locale.");

  ThrowIfFailed(graph_manager->GetDWriteFactory()->CreateTextFormat(
      font_family.data(), 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_CENTER));
  ThrowIfFailed(text_format_->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER));
}
}  // namespace cru::platform::win