aboutsummaryrefslogtreecommitdiff
path: root/src/win/graph/direct/Font.cpp
blob: edbbc59d97f4685ba90db4426c3738bbe43aa024 (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
26
27
28
29
30
31
32
33
#include "cru/win/graph/direct/Font.hpp"

#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) {
  // 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.");

  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,
      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::graph::win::direct