diff options
-rw-r--r-- | include/cru/osx/graphics/quartz/TextLayout.hpp | 17 | ||||
-rw-r--r-- | include/cru/platform/graphics/TextLayout.hpp | 5 | ||||
-rw-r--r-- | src/osx/graphics/quartz/TextLayout.cpp | 42 |
3 files changed, 56 insertions, 8 deletions
diff --git a/include/cru/osx/graphics/quartz/TextLayout.hpp b/include/cru/osx/graphics/quartz/TextLayout.hpp index bfa2aee3..4a3830db 100644 --- a/include/cru/osx/graphics/quartz/TextLayout.hpp +++ b/include/cru/osx/graphics/quartz/TextLayout.hpp @@ -1,11 +1,12 @@ #pragma once -#include <memory> #include "Resource.hpp" #include "Font.hpp" #include "cru/common/Base.hpp" #include "cru/platform/graphics/TextLayout.hpp" +#include <memory> + namespace cru::platform::graphics::osx::quartz { class OsxCTTextLayout : public OsxQuartzResource, public virtual ITextLayout { public: @@ -18,9 +19,8 @@ class OsxCTTextLayout : public OsxQuartzResource, public virtual ITextLayout { ~OsxCTTextLayout() override; public: - std::u16string GetText() override; - std::u16string_view GetTextView() override; - void SetText(std::u16string new_text) override; + String GetText() override { return text_; } + void SetText(String new_text) override; std::shared_ptr<IFont> GetFont() override; void SetFont(std::shared_ptr<IFont> font) override; @@ -33,9 +33,18 @@ class OsxCTTextLayout : public OsxQuartzResource, public virtual ITextLayout { Point TextSinglePoint(Index position, bool trailing) override; TextHitTestResult HitTest(const Point& point) override; +private: + void RecreateFrame(); + private: + float max_width_; + float max_height_; + std::shared_ptr<OsxCTFont> font_; + String text_; + CFStringRef cf_text_; + CTFramesetterRef ct_framesetter_; CTFrameRef ct_frame_; }; diff --git a/include/cru/platform/graphics/TextLayout.hpp b/include/cru/platform/graphics/TextLayout.hpp index b363fb77..716c8047 100644 --- a/include/cru/platform/graphics/TextLayout.hpp +++ b/include/cru/platform/graphics/TextLayout.hpp @@ -6,9 +6,8 @@ namespace cru::platform::graphics { struct ITextLayout : virtual IGraphResource { - virtual std::u16string GetText() = 0; - virtual std::u16string_view GetTextView() = 0; - virtual void SetText(std::u16string new_text) = 0; + virtual String GetText() = 0; + virtual void SetText(String new_text) = 0; virtual std::shared_ptr<IFont> GetFont() = 0; virtual void SetFont(std::shared_ptr<IFont> font) = 0; 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 |