From a4dafa3e2918f2cc28657145e6b0afe7bcf1ca7b Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 24 Oct 2021 18:59:07 +0800 Subject: ... --- src/osx/Convert.cpp | 3 +- src/osx/graphics/quartz/Painter.cpp | 60 +++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/osx/Convert.cpp b/src/osx/Convert.cpp index e8c023e9..6bec5adc 100644 --- a/src/osx/Convert.cpp +++ b/src/osx/Convert.cpp @@ -1,8 +1,9 @@ #include "cru/osx/Convert.hpp" -#include #include "cru/common/StringUtil.hpp" +#include + namespace cru::platform::osx { CFStringRef Convert(const String& string) { return CFStringCreateWithBytes( diff --git a/src/osx/graphics/quartz/Painter.cpp b/src/osx/graphics/quartz/Painter.cpp index 406e3273..eee175ac 100644 --- a/src/osx/graphics/quartz/Painter.cpp +++ b/src/osx/graphics/quartz/Painter.cpp @@ -21,6 +21,13 @@ QuartzCGContextPainter::QuartzCGContextPainter( size_(size), on_end_draw_(std::move(on_end_draw)) { Expects(cg_context); + + CGContextConcatCTM(cg_context_, + CGAffineTransformInvert(CGContextGetCTM(cg_context_))); + + transform_ = Matrix::Scale(1, -1) * Matrix::Translation(0, size.height); + CGContextConcatCTM(cg_context_, Convert(transform_)); + // log::TagDebug(log_tag, // u"Created with CGContext: {}, Auto Release: {}, Size: {}.", // cg_context, auto_release, size_); @@ -34,15 +41,17 @@ QuartzCGContextPainter::~QuartzCGContextPainter() { } } -Matrix QuartzCGContextPainter::GetTransform() { - return Convert(CGContextGetCTM(cg_context_)); -} +Matrix QuartzCGContextPainter::GetTransform() { return transform_; } void QuartzCGContextPainter::SetTransform(const Matrix& matrix) { - auto old = CGContextGetCTM(cg_context_); - old = CGAffineTransformInvert(old); - CGContextConcatCTM(cg_context_, old); + CGContextConcatCTM(cg_context_, Convert(*transform_.Inverted())); + CGContextConcatCTM(cg_context_, Convert(matrix)); + transform_ = matrix; +} + +void QuartzCGContextPainter::ConcatTransform(const Matrix& matrix) { CGContextConcatCTM(cg_context_, Convert(matrix)); + transform_ = matrix * transform_; } void QuartzCGContextPainter::Clear(const Color& color) { @@ -128,13 +137,28 @@ void QuartzCGContextPainter::DrawText(const Point& offset, color = colors::black; } - util::WithTransform(this, Matrix::Translation(offset), - [this, tl, color](IPainter*) { - auto frame = tl->CreateFrameWithColor(color); - Ensures(frame); - CTFrameDraw(frame, cg_context_); - CFRelease(frame); - }); + auto bounds = tl->GetTextBounds(); + + bounds.width += bounds.left; + bounds.height += bounds.top; + bounds.left = bounds.top = 0; + + Matrix transform = + Matrix::Translation(-bounds.width / 2, -bounds.height / 2) * + Matrix::Scale(1, -1) * + Matrix::Translation(bounds.width / 2, bounds.height / 2); + + CGContextSaveGState(cg_context_); + + CGContextConcatCTM(cg_context_, Convert(transform * Matrix::Translation( + offset.x, offset.y))); + + auto frame = tl->CreateFrameWithColor(color); + Ensures(frame); + CTFrameDraw(frame, cg_context_); + CFRelease(frame); + + CGContextRestoreGState(cg_context_); } void QuartzCGContextPainter::PushLayer(const Rect& bounds) { @@ -153,6 +177,16 @@ void QuartzCGContextPainter::PopLayer() { } } +void QuartzCGContextPainter::PushState() { + Validate(); + CGContextSaveGState(cg_context_); +} + +void QuartzCGContextPainter::PopState() { + Validate(); + CGContextRestoreGState(cg_context_); +} + void QuartzCGContextPainter::EndDraw() { DoEndDraw(); } void QuartzCGContextPainter::DoEndDraw() { -- cgit v1.2.3