aboutsummaryrefslogtreecommitdiff
path: root/src/osx/graphics/quartz/TextLayout.cpp
blob: 73bfb1cea9d87d68a9ad9a831e095a603eeb1054 (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
#include "cru/osx/graphics/quartz/TextLayout.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 {
OsxCTTextLayout::OsxCTTextLayout(IGraphFactory* graphics_factory,
                                 std::shared_ptr<OsxCTFont> font,
                                 const String& str)
    : OsxQuartzResource(graphics_factory),
      max_width_(0.f),
      max_height_(0.f),
      font_(std::move(font)),
      text_(str) {
  cf_text_ = Convert(str);

  RecreateFrame();
}

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

void OsxCTTextLayout::SetText(String new_text) {
  text_ = std::move(new_text);
  CFRelease(cf_text_);
  cf_text_ = Convert(text_);
  CFRelease(ct_framesetter_);
  CFRelease(ct_frame_);
  RecreateFrame();
}

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

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

Rect OsxCTTextLayout::GetTextBounds(bool includingTrailingSpace) {
  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 = static_cast<CTLineRef>(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::TextRangeRect(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;
}

void OsxCTTextLayout::RecreateFrame() {
  auto attributed_string = CFAttributedStringCreateMutable(nullptr, 0);
  CFAttributedStringReplaceString(attributed_string, CFRangeMake(0, 0),
                                  cf_text_);
  CFAttributedStringSetAttribute(attributed_string,
                                 CFRangeMake(0, CFStringGetLength(cf_text_)),
                                 kCTFontAttributeName, font_->GetCTFont());
  ct_framesetter_ = CTFramesetterCreateWithAttributedString(attributed_string);

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

  ct_frame_ = CTFramesetterCreateFrame(
      ct_framesetter_, CFRangeMake(0, CFStringGetLength(cf_text_)), path,
      nullptr);

  CFRelease(attributed_string);
  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));
  }
}
}  // namespace cru::platform::graphics::osx::quartz