aboutsummaryrefslogtreecommitdiff
path: root/src/osx/graphics/quartz/TextLayout.cpp
blob: d30c386f81d4dbe3bb547f95cada8f545f736d02 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include "cru/osx/graphics/quartz/TextLayout.hpp"
#include "cru/osx/Convert.hpp"
#include "cru/osx/graphics/quartz/Convert.hpp"
#include "cru/osx/graphics/quartz/Resource.hpp"
#include "cru/platform/Check.hpp"

#include <algorithm>
#include <limits>

namespace cru::platform::graphics::osx::quartz {
using cru::platform::osx::Convert;

OsxCTTextLayout::OsxCTTextLayout(IGraphicsFactory* graphics_factory,
                                 std::shared_ptr<OsxCTFont> font,
                                 const String& str)
    : OsxQuartzResource(graphics_factory),
      max_width_(std::numeric_limits<float>::max()),
      max_height_(std::numeric_limits<float>::max()),
      font_(std::move(font)),
      text_(str) {
  Expects(font_);

  CFStringRef s = Convert(text_);
  cf_attributed_text_ = CFAttributedStringCreateMutable(nullptr, 0);
  CFAttributedStringReplaceString(cf_attributed_text_, CFRangeMake(0, 0), s);
  Ensures(cf_attributed_text_);
  CFAttributedStringSetAttribute(
      cf_attributed_text_,
      CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)),
      kCTFontAttributeName, font_->GetCTFont());
  CFRelease(s);

  RecreateFrame();
}

OsxCTTextLayout::~OsxCTTextLayout() {
  ReleaseResource();
  CFRelease(cf_attributed_text_);
}

void OsxCTTextLayout::SetFont(std::shared_ptr<IFont> font) {
  font_ = CheckPlatform<OsxCTFont>(font, GetPlatformId());
  RecreateFrame();
}

void OsxCTTextLayout::SetText(String new_text) {
  text_ = std::move(new_text);

  CFRelease(cf_attributed_text_);

  CFStringRef s = Convert(text_);
  cf_attributed_text_ = CFAttributedStringCreateMutable(nullptr, 0);
  CFAttributedStringReplaceString(cf_attributed_text_, CFRangeMake(0, 0), s);
  Ensures(cf_attributed_text_);
  CFAttributedStringSetAttribute(
      cf_attributed_text_,
      CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)),
      kCTFontAttributeName, font_->GetCTFont());
  CFRelease(s);

  RecreateFrame();
}

void OsxCTTextLayout::SetMaxWidth(float max_width) {
  max_width_ = max_width;
  RecreateFrame();
}

void OsxCTTextLayout::SetMaxHeight(float max_height) {
  max_height_ = max_height;
  RecreateFrame();
}

Rect OsxCTTextLayout::GetTextBounds(bool includingTrailingSpace) {
  return transform_.TransformRect(DoGetTextBounds(includingTrailingSpace));
}

std::vector<Rect> OsxCTTextLayout::TextRangeRect(const TextRange& text_range) {
  std::vector<Rect> results = DoTextRangeRect(text_range);

  for (auto& rect : results) {
    rect = transform_.TransformRect(rect);
  }

  return results;
}

Rect OsxCTTextLayout::TextSinglePoint(Index position, bool trailing) {
  return transform_.TransformRect(DoTextSinglePoint(position, trailing));
}

TextHitTestResult OsxCTTextLayout::HitTest(const Point& point) {
  for (int i = 0; i < line_count_; i++) {
    auto line = lines_[i];
    const auto& line_origin = line_origins_[i];

    auto bounds = Convert(CTLineGetImageBounds(line, nullptr));
    bounds.left += line_origin.x;
    bounds.top += line_origin.y;
    if (bounds.IsPointInside(point)) {
      int position = CTLineGetStringIndexForPosition(
          line, CGPointMake(point.x - line_origin.x, point.y - line_origin.y));
      return TextHitTestResult{position, false, true};
    }
  }

  return TextHitTestResult{0, false, false};
}

void OsxCTTextLayout::ReleaseResource() {
  line_count_ = 0;
  line_origins_.clear();
  lines_.clear();
  if (ct_framesetter_) CFRelease(ct_framesetter_);
  if (ct_frame_) CFRelease(ct_frame_);
}

void OsxCTTextLayout::RecreateFrame() {
  ReleaseResource();

  ct_framesetter_ =
      CTFramesetterCreateWithAttributedString(cf_attributed_text_);
  Ensures(ct_framesetter_);

  CFRange fit_range;

  suggest_height_ =
      CTFramesetterSuggestFrameSizeWithConstraints(
          ct_framesetter_,
          CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)),
          nullptr, CGSizeMake(max_width_, max_height_), &fit_range)
          .height;

  auto path = CGPathCreateMutable();
  Ensures(path);
  CGPathAddRect(path, nullptr, CGRectMake(0, 0, max_width_, suggest_height_));

  ct_frame_ = CTFramesetterCreateFrame(
      ct_framesetter_,
      CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), path,
      nullptr);
  Ensures(ct_frame_);

  CGPathRelease(path);

  const auto lines = CTFrameGetLines(ct_frame_);
  line_count_ = CFArrayGetCount(lines);
  lines_.resize(line_count_);
  line_origins_.resize(line_count_);
  CTFrameGetLineOrigins(ct_frame_, CFRangeMake(0, 0), line_origins_.data());
  for (int i = 0; i < line_count_; i++) {
    lines_[i] = static_cast<CTLineRef>(CFArrayGetValueAtIndex(lines, i));
  }

  auto bounds = DoGetTextBounds(false);

  transform_ =
      Matrix::Translation(-bounds.GetRight() / 2, -bounds.GetBottom() / 2) *
      Matrix::Scale(1, -1) *
      Matrix::Translation(bounds.GetRight() / 2, bounds.GetBottom() / 2);
}

CTFrameRef OsxCTTextLayout::CreateFrameWithColor(const Color& color) {
  auto path = CGPathCreateMutable();
  CGPathAddRect(path, nullptr, CGRectMake(0, 0, max_width_, suggest_height_));

  CGColorRef cg_color =
      CGColorCreateGenericRGB(color.GetFloatRed(), color.GetFloatGreen(),
                              color.GetFloatBlue(), color.GetFloatAlpha());
  CFAttributedStringSetAttribute(
      cf_attributed_text_,
      CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)),
      kCTForegroundColorAttributeName, cg_color);

  auto frame = CTFramesetterCreateFrame(
      ct_framesetter_,
      CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), path,
      nullptr);
  Ensures(frame);

  CGPathRelease(path);

  return frame;
}

String OsxCTTextLayout::GetDebugString() {
  return Format(u"OsxCTTextLayout(text: {}, size: ({}, {}))", text_, max_width_,
                max_height_);
}

Rect OsxCTTextLayout::DoGetTextBounds(bool includingTrailingSpace) {
  if (text_.empty()) return Rect{};

  float left = std::numeric_limits<float>::max();
  float top = std::numeric_limits<float>::max();
  float right = 0;
  float bottom = 0;

  for (int i = 0; i < line_count_; i++) {
    auto line = lines_[i];
    const auto& line_origin = line_origins_[i];

    CGRect line_rect = CTLineGetImageBounds(line, nullptr);
    if (includingTrailingSpace) {
      float trailingWidth = CTLineGetTrailingWhitespaceWidth(line);
      line_rect.size.width += trailingWidth;
    }

    line_rect.origin.x += line_origin.x;
    line_rect.origin.y += line_origin.y;

    left = std::min<float>(line_rect.origin.x, left);
    top = std::min<float>(line_rect.origin.y, top);
    right = std::max<float>(line_rect.origin.x + line_rect.size.width, right);
    bottom =
        std::max<float>(line_rect.origin.y + line_rect.size.height, bottom);
  }

  return Rect::FromVertices(left, top, right, bottom);
}

std::vector<Rect> OsxCTTextLayout::DoTextRangeRect(
    const TextRange& text_range) {
  const auto r = text_.RangeFromCodeUnitToCodePoint(text_range).Normalize();

  std::vector<Rect> results;

  for (int i = 0; i < line_count_; i++) {
    auto line = lines_[i];
    const auto& line_origin = line_origins_[i];

    Range range = Convert(CTLineGetStringRange(line));
    range = range.CoerceInto(r.GetStart(), r.GetEnd());

    if (range.count) {
      auto line_rect = CTLineGetImageBounds(line, nullptr);
      line_rect.origin.x += line_origin.x;
      line_rect.origin.y += line_origin.y;
      float start_offset =
          CTLineGetOffsetForStringIndex(line, range.GetStart(), nullptr);
      float end_offset =
          CTLineGetOffsetForStringIndex(line, range.GetEnd(), nullptr);
      line_rect.origin.x += start_offset;
      line_rect.size.width = end_offset - start_offset;
      results.push_back(Convert(line_rect));
    }
  }

  return results;
}

Rect OsxCTTextLayout::DoTextSinglePoint(Index position, bool trailing) {
  if (lines_.empty()) return Rect{0, 0, 0, font_->GetFontSize()};

  position = text_.IndexFromCodeUnitToCodePoint(position);
  for (int i = 0; i < line_count_; i++) {
    auto line = lines_[i];
    const auto& line_origin = line_origins_[i];

    auto rect = Convert(CTLineGetImageBounds(line, nullptr));
    rect.left += line_origin.x;
    rect.top += line_origin.y;

    Range range = Convert(CTLineGetStringRange(line));
    if (range.GetStart() <= position && position < range.GetEnd()) {
      auto offset = CTLineGetOffsetForStringIndex(line, position, nullptr);
      return Rect(rect.left + offset, rect.top, 0, rect.height);
    } else if (position == range.GetEnd()) {
      return Rect(rect.GetRight(), rect.top, 0, rect.height);
    }
  }

  // TODO: Complete logic here.

  auto rect = Convert(CTLineGetImageBounds(lines_.back(), nullptr));
  rect.left = rect.GetRight();
  rect.width = 0;
  return rect;
}

}  // namespace cru::platform::graphics::osx::quartz