From 3168b19e9ad1798d5b45c34b3f37ba496f5da66a Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 20 Oct 2021 17:39:59 +0800 Subject: ... --- include/cru/osx/graphics/quartz/TextLayout.hpp | 2 +- src/osx/graphics/quartz/TextLayout.cpp | 34 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/include/cru/osx/graphics/quartz/TextLayout.hpp b/include/cru/osx/graphics/quartz/TextLayout.hpp index 92e0c861..87932423 100644 --- a/include/cru/osx/graphics/quartz/TextLayout.hpp +++ b/include/cru/osx/graphics/quartz/TextLayout.hpp @@ -48,7 +48,7 @@ class OsxCTTextLayout : public OsxQuartzResource, public virtual ITextLayout { std::shared_ptr font_; String text_; - CFAttributedStringRef cf_attributed_text_; + CFMutableAttributedStringRef cf_attributed_text_; CTFramesetterRef ct_framesetter_ = nullptr; CTFrameRef ct_frame_ = nullptr; diff --git a/src/osx/graphics/quartz/TextLayout.cpp b/src/osx/graphics/quartz/TextLayout.cpp index d96cf025..7ce03a5e 100644 --- a/src/osx/graphics/quartz/TextLayout.cpp +++ b/src/osx/graphics/quartz/TextLayout.cpp @@ -14,8 +14,8 @@ OsxCTTextLayout::OsxCTTextLayout(IGraphicsFactory* graphics_factory, std::shared_ptr font, const String& str) : OsxQuartzResource(graphics_factory), - max_width_(0.f), - max_height_(0.f), + max_width_(std::numeric_limits::max()), + max_height_(std::numeric_limits::max()), font_(std::move(font)), text_(str) { Expects(font_); @@ -27,7 +27,8 @@ OsxCTTextLayout::OsxCTTextLayout(IGraphicsFactory* graphics_factory, Ensures(s); Ensures(attributes); - cf_attributed_text_ = CFAttributedStringCreate(nullptr, s, attributes); + cf_attributed_text_ = CFAttributedStringCreateMutable(nullptr, 0); + CFAttributedStringReplaceString(cf_attributed_text_, CFRangeMake(0, 0), s); Ensures(cf_attributed_text_); CFRelease(attributes); @@ -58,9 +59,22 @@ void OsxCTTextLayout::SetText(String new_text) { Ensures(s); Ensures(attributes); - cf_attributed_text_ = CFAttributedStringCreate(nullptr, s, attributes); + 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()); + + CGColorRef cg_color = CGColorCreateGenericRGB(0, 0, 0, 1); + CFAttributedStringSetAttribute( + cf_attributed_text_, + CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), + kCTForegroundColorAttributeName, cg_color); + + CGColorRelease(cg_color); CFRelease(attributes); CFRelease(s); @@ -84,7 +98,7 @@ Rect OsxCTTextLayout::GetTextBounds(bool includingTrailingSpace) { float bottom = 0; for (int i = 0; i < line_count_; i++) { - auto line = static_cast(lines_[i]); + auto line = lines_[i]; const auto& line_origin = line_origins_[i]; CGRect line_rect = CTLineGetImageBounds(line, nullptr); @@ -216,15 +230,13 @@ CTFrameRef OsxCTTextLayout::CreateFrameWithColor(const Color& color) { auto path = CGPathCreateMutable(); CGPathAddRect(path, nullptr, CGRectMake(0, 0, max_width_, max_height_)); - CFMutableDictionaryRef dictionary = - CFDictionaryCreateMutable(nullptr, 0, &kCFTypeDictionaryKeyCallBacks, - &kCFTypeDictionaryValueCallBacks); - CFDictionaryAddValue(dictionary, kCTFontAttributeName, font_->GetCTFont()); - CGColorRef cg_color = CGColorCreateGenericRGB(color.GetFloatRed(), color.GetFloatGreen(), color.GetFloatBlue(), color.GetFloatAlpha()); - CFDictionaryAddValue(dictionary, kCTForegroundColorAttributeName, cg_color); + CFAttributedStringSetAttribute( + cf_attributed_text_, + CFRangeMake(0, CFAttributedStringGetLength(cf_attributed_text_)), + kCTForegroundColorAttributeName, cg_color); auto frame = CTFramesetterCreateFrame( ct_framesetter_, -- cgit v1.2.3