diff options
-rw-r--r-- | cocoa/PlatCocoa.h | 3 | ||||
-rw-r--r-- | cocoa/PlatCocoa.mm | 39 | ||||
-rw-r--r-- | cocoa/QuartzTextLayout.h | 58 |
3 files changed, 42 insertions, 58 deletions
diff --git a/cocoa/PlatCocoa.h b/cocoa/PlatCocoa.h index 708bdcd2b..d63ad63c6 100644 --- a/cocoa/PlatCocoa.h +++ b/cocoa/PlatCocoa.h @@ -45,9 +45,6 @@ private: CGContextRef gc; - /** The text layout instance */ - std::unique_ptr<QuartzTextLayout> textLayout; - /** If the surface is a bitmap context, contains a reference to the bitmap data. */ std::unique_ptr<uint8_t[]> bitmapData; /** If the surface is a bitmap context, stores the dimensions of the bitmap. */ diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm index c3a5e1d48..239884341 100644 --- a/cocoa/PlatCocoa.mm +++ b/cocoa/PlatCocoa.mm @@ -357,8 +357,6 @@ const Supports SupportsCocoa[] = { SurfaceImpl::SurfaceImpl() { gc = NULL; - textLayout.reset(new QuartzTextLayout()); - bitmapData.reset(); // Release will try and delete bitmapData if != nullptr bitmapWidth = 0; bitmapHeight = 0; @@ -368,8 +366,6 @@ SurfaceImpl::SurfaceImpl() { SurfaceImpl::SurfaceImpl(const SurfaceImpl *surface, int width, int height) { - textLayout.reset(new QuartzTextLayout()); - // Create a new bitmap context, along with the RAM for the bitmap itself bitmapWidth = width; bitmapHeight = height; @@ -1269,8 +1265,8 @@ void SurfaceImpl::DrawTextTransparent(PRectangle rc, const Font *font_, XYPOSITI CGColorRelease(color); - textLayout->setText(text, encoding, style); - textLayout->draw(gc, rc.left, ybase); + QuartzTextLayout layoutDraw(text, encoding, style); + layoutDraw.draw(gc, rc.left, ybase); } //-------------------------------------------------------------------------------------------------- @@ -1281,10 +1277,10 @@ void SurfaceImpl::MeasureWidths(const Font *font_, std::string_view text, XYPOSI return; } CFStringEncoding encoding = EncodingFromCharacterSet(UnicodeMode(), style->getCharacterSet()); - const CFStringEncoding encodingUsed = - textLayout->setText(text, encoding, style); + QuartzTextLayout layoutMeasure(text, encoding, style); + const CFStringEncoding encodingUsed = layoutMeasure.getEncoding(); - CTLineRef mLine = textLayout->getCTLine(); + CTLineRef mLine = layoutMeasure.getCTLine(); assert(mLine); if (encodingUsed != encoding) { @@ -1298,7 +1294,7 @@ void SurfaceImpl::MeasureWidths(const Font *font_, std::string_view text, XYPOSI if (UnicodeMode()) { // Map the widths given for UTF-16 characters back onto the UTF-8 input string - CFIndex fit = textLayout->getStringLength(); + CFIndex fit = layoutMeasure.getStringLength(); int ui=0; int i=0; std::vector<CGFloat> linePositions(fit); @@ -1344,9 +1340,9 @@ XYPOSITION SurfaceImpl::WidthText(const Font *font_, std::string_view text) { return 1; } CFStringEncoding encoding = EncodingFromCharacterSet(UnicodeMode(), style->getCharacterSet()); - textLayout->setText(text, encoding, style); + QuartzTextLayout layoutMeasure(text, encoding, style); - return static_cast<XYPOSITION>(textLayout->MeasureStringWidth()); + return static_cast<XYPOSITION>(layoutMeasure.MeasureStringWidth()); } //-------------------------------------------------------------------------------------------------- @@ -1386,8 +1382,8 @@ void SurfaceImpl::DrawTextTransparentUTF8(PRectangle rc, const Font *font_, XYPO CGColorRelease(color); - textLayout->setText(text, encoding, style); - textLayout->draw(gc, rc.left, ybase); + QuartzTextLayout layoutDraw(text, encoding, style); + layoutDraw.draw(gc, rc.left, ybase); } //-------------------------------------------------------------------------------------------------- @@ -1397,11 +1393,11 @@ void SurfaceImpl::MeasureWidthsUTF8(const Font *font_, std::string_view text, XY if (!style) { return; } - const CFStringEncoding encoding = kCFStringEncodingUTF8; - const CFStringEncoding encodingUsed = - textLayout->setText(text, encoding, style); + constexpr CFStringEncoding encoding = kCFStringEncodingUTF8; + QuartzTextLayout layoutMeasure(text, encoding, style); + const CFStringEncoding encodingUsed = layoutMeasure.getEncoding(); - CTLineRef mLine = textLayout->getCTLine(); + CTLineRef mLine = layoutMeasure.getCTLine(); assert(mLine); if (encodingUsed != encoding) { @@ -1414,7 +1410,7 @@ void SurfaceImpl::MeasureWidthsUTF8(const Font *font_, std::string_view text, XY } // Map the widths given for UTF-16 characters back onto the UTF-8 input string - CFIndex fit = textLayout->getStringLength(); + CFIndex fit = layoutMeasure.getStringLength(); int ui=0; int i=0; std::vector<CGFloat> linePositions(fit); @@ -1442,9 +1438,8 @@ XYPOSITION SurfaceImpl::WidthTextUTF8(const Font *font_, std::string_view text) if (!style) { return 1; } - textLayout->setText(text, kCFStringEncodingUTF8, style); - - return static_cast<XYPOSITION>(textLayout->MeasureStringWidth()); + QuartzTextLayout layoutMeasure(text, kCFStringEncodingUTF8, style); + return static_cast<XYPOSITION>(layoutMeasure.MeasureStringWidth()); } //-------------------------------------------------------------------------------------------------- diff --git a/cocoa/QuartzTextLayout.h b/cocoa/QuartzTextLayout.h index 1f7adc241..f1eaebdc9 100644 --- a/cocoa/QuartzTextLayout.h +++ b/cocoa/QuartzTextLayout.h @@ -19,41 +19,18 @@ class QuartzTextLayout { public: /** Create a text layout for drawing. */ - QuartzTextLayout() : mString(NULL), mLine(NULL), stringLength(0) { - } - - ~QuartzTextLayout() { - if (mString) { - CFRelease(mString); - mString = NULL; - } - if (mLine) { - CFRelease(mLine); - mLine = NULL; - } - } - - CFStringEncoding setText(std::string_view sv, CFStringEncoding encoding, const QuartzTextStyle *r) { - // First clear current values in case of failure. - if (mString) { - CFRelease(mString); - mString = NULL; - } - if (mLine) { - CFRelease(mLine); - mLine = NULL; - } - + QuartzTextLayout(std::string_view sv, CFStringEncoding encoding, const QuartzTextStyle *r) { + encodingUsed = encoding; const UInt8 *puiBuffer = reinterpret_cast<const UInt8 *>(sv.data()); - CFStringRef str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encoding, false); + CFStringRef str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encodingUsed, false); if (!str) { // Failed to decode bytes into string with given encoding so try // MacRoman which should accept any byte. - encoding = kCFStringEncodingMacRoman; - str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encoding, false); + encodingUsed = kCFStringEncodingMacRoman; + str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encodingUsed, false); } if (!str) { - return encoding; + return; } stringLength = CFStringGetLength(str); @@ -65,7 +42,17 @@ public: mLine = ::CTLineCreateWithAttributedString(mString); CFRelease(str); - return encoding; + } + + ~QuartzTextLayout() { + if (mString) { + CFRelease(mString); + mString = NULL; + } + if (mLine) { + CFRelease(mLine); + mLine = NULL; + } } /** Draw the text layout into a CGContext at the specified position. @@ -100,10 +87,15 @@ public: return stringLength; } + CFStringEncoding getEncoding() { + return encodingUsed; + } + private: - CFAttributedStringRef mString; - CTLineRef mLine; - CFIndex stringLength; + CFAttributedStringRef mString = NULL; + CTLineRef mLine = NULL; + CFIndex stringLength = 0; + CFStringEncoding encodingUsed = kCFStringEncodingMacRoman; }; #endif |