diff options
author | crupest <crupest@outlook.com> | 2021-10-24 18:59:07 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-10-24 18:59:07 +0800 |
commit | a4dafa3e2918f2cc28657145e6b0afe7bcf1ca7b (patch) | |
tree | 9ba8b9c960fdc830c272042e6b595e0a20e3bfb2 /src | |
parent | 3e84cf013b31c52405a76b8e8778a5991d096290 (diff) | |
download | cru-a4dafa3e2918f2cc28657145e6b0afe7bcf1ca7b.tar.gz cru-a4dafa3e2918f2cc28657145e6b0afe7bcf1ca7b.tar.bz2 cru-a4dafa3e2918f2cc28657145e6b0afe7bcf1ca7b.zip |
...
Diffstat (limited to 'src')
-rw-r--r-- | src/osx/Convert.cpp | 3 | ||||
-rw-r--r-- | src/osx/graphics/quartz/Painter.cpp | 60 |
2 files changed, 49 insertions, 14 deletions
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 <string> #include "cru/common/StringUtil.hpp" +#include <string> + 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() { |