diff options
author | crupest <crupest@outlook.com> | 2021-09-06 22:19:25 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-09-06 22:19:25 +0800 |
commit | bd44f9bc59c7fc2a8d2715d23368926aef5c5c32 (patch) | |
tree | 844c3a1faf62d133bfced5dc7f39f13f13d87b46 /src/osx/graphics/quartz | |
parent | 16007714fc0ad365c6da52dc58201ba4bc6aa964 (diff) | |
download | cru-bd44f9bc59c7fc2a8d2715d23368926aef5c5c32.tar.gz cru-bd44f9bc59c7fc2a8d2715d23368926aef5c5c32.tar.bz2 cru-bd44f9bc59c7fc2a8d2715d23368926aef5c5c32.zip |
...
Diffstat (limited to 'src/osx/graphics/quartz')
-rw-r--r-- | src/osx/graphics/quartz/TextLayout.cpp | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/src/osx/graphics/quartz/TextLayout.cpp b/src/osx/graphics/quartz/TextLayout.cpp index bd0562cb..6b183fa7 100644 --- a/src/osx/graphics/quartz/TextLayout.cpp +++ b/src/osx/graphics/quartz/TextLayout.cpp @@ -1,5 +1,45 @@ #include "cru/osx/graphics/quartz/TextLayout.hpp" +#include "cru/osx/graphics/quartz/Convert.hpp" +#include "cru/osx/graphics/quartz/Resource.hpp" 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::SetText(String new_text) { + text_ = std::move(new_text); + CFRelease(cf_text_); + cf_text_ = Convert(text_); + RecreateFrame(); +} + +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); } +} // namespace cru::platform::graphics::osx::quartz |