aboutsummaryrefslogtreecommitdiff
path: root/src/platform/graphics/direct2d/TextLayout.cpp
blob: 906d64ec563462f32006bd8fffdc1b4b316d9780 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#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"
#include "cru/platform/graphics/direct2d/Factory.h"
#include "cru/platform/graphics/direct2d/Font.h"

#include <utility>

namespace cru::platform::graphics::direct2d {
DWriteTextLayout::DWriteTextLayout(DirectGraphicsFactory* factory,
                                   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(
      utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()),
      font_->GetComInterface(), max_width_, max_height_, &text_layout_));
}

DWriteTextLayout::~DWriteTextLayout() = default;

std::string DWriteTextLayout::GetText() { return text_; }

void DWriteTextLayout::SetText(std::string new_text) {
  text_ = std::move(new_text);
  utf16_text_ = string::ToUtf16(text_);
  ThrowIfFailed(GetDirectFactory()->GetDWriteFactory()->CreateTextLayout(
      utf16_text_.c_str(), static_cast<UINT32>(utf16_text_.size()),
      font_->GetComInterface(), max_width_, max_height_, &text_layout_));
}

std::shared_ptr<IFont> DWriteTextLayout::GetFont() {
  return std::dynamic_pointer_cast<IFont>(font_);
}

void DWriteTextLayout::SetFont(std::shared_ptr<IFont> font) {
  font_ = CheckPlatform<DWriteFont>(font, GetPlatformId());
  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_));
}

void DWriteTextLayout::SetMaxWidth(float max_width) {
  max_width_ = max_width;
  ThrowIfFailed(text_layout_->SetMaxWidth(max_width_));
}

void DWriteTextLayout::SetMaxHeight(float max_height) {
  max_height_ = max_height;
  ThrowIfFailed(text_layout_->SetMaxHeight(max_height_));
}

bool DWriteTextLayout::IsEditMode() { return edit_mode_; }

void DWriteTextLayout::SetEditMode(bool enable) {
  edit_mode_ = enable;
  // TODO: Implement this.
}

Index DWriteTextLayout::GetLineIndexFromCharIndex(Index char_index) {
  if (char_index < 0 || char_index >= text_.size()) {
    return -1;
  }

  auto line_index = 0;
  for (Index i = 0; i < char_index; ++i) {
    if (text_[i] == '\n') {
      line_index++;
    }
  }

  return line_index;
}

float DWriteTextLayout::GetLineHeight(Index line_index) {
  Index count = GetLineCount();
  std::vector<DWRITE_LINE_METRICS> line_metrics(count);

  UINT32 actual_line_count = 0;
  text_layout_->GetLineMetrics(line_metrics.data(), static_cast<UINT32>(count),
                               &actual_line_count);
  return line_metrics[line_index].height;
}

Index DWriteTextLayout::GetLineCount() {
  UINT32 line_count = 0;
  text_layout_->GetLineMetrics(nullptr, 0, &line_count);
  return line_count;
}

Rect DWriteTextLayout::GetTextBounds(bool includingTrailingSpace) {
  DWRITE_TEXT_METRICS metrics;
  ThrowIfFailed(text_layout_->GetMetrics(&metrics));
  return Rect{metrics.left, metrics.top,
              includingTrailingSpace ? metrics.widthIncludingTrailingWhitespace
                                     : metrics.width,
              metrics.height};
}

std::vector<Rect> DWriteTextLayout::TextRangeRect(
    const TextRange& text_range_arg) {
  if (text_range_arg.count == 0) {
    return {};
  }
  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));
  const auto metrics_count =
      text_metrics.lineCount * text_metrics.maxBidiReorderingDepth;

  std::vector<DWRITE_HIT_TEST_METRICS> hit_test_metrics(metrics_count);
  UINT32 actual_count;
  ThrowIfFailed(text_layout_->HitTestTextRange(
      static_cast<UINT32>(text_range.position),
      static_cast<UINT32>(text_range.count), 0, 0, hit_test_metrics.data(),
      metrics_count, &actual_count));

  hit_test_metrics.erase(hit_test_metrics.cbegin() + actual_count,
                         hit_test_metrics.cend());

  std::vector<Rect> result;
  result.reserve(actual_count);

  for (const auto& metrics : hit_test_metrics) {
    result.push_back(
        Rect{metrics.left, metrics.top, metrics.width, metrics.height});
  }

  return result;
}

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;
  ThrowIfFailed(text_layout_->HitTestTextPosition(static_cast<UINT32>(position),
                                                  static_cast<BOOL>(trailing),
                                                  &left, &top, &metrics));
  return Rect{left, top, 0, GetFont()->GetFontSize()};
}

TextHitTestResult DWriteTextLayout::HitTest(const Point& point) {
  BOOL trailing;
  BOOL inside;
  DWRITE_HIT_TEST_METRICS metrics;

  ThrowIfFailed(text_layout_->HitTestPoint(point.x, point.y, &trailing, &inside,
                                           &metrics));

  TextHitTestResult result;
  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;
}
}  // namespace cru::platform::graphics::direct2d