diff options
author | Neil Hodgson <nyamatongwe@gmail.com> | 2018-04-22 08:22:53 +1000 |
---|---|---|
committer | Neil Hodgson <nyamatongwe@gmail.com> | 2018-04-22 08:22:53 +1000 |
commit | 23ef8619d029b9edbe0f19212a6b3954b9f79874 (patch) | |
tree | 125f89702ee442456ba78c8e8f55ddb605ea3730 | |
parent | e5afba6e119511b90bb8bc533a0d1414735bc6bd (diff) | |
download | scintilla-mirror-23ef8619d029b9edbe0f19212a6b3954b9f79874.tar.gz |
Reduce casts by moving casting from char* to UInt8* into QuartzTextLayout::SetText
and adding helper TextStyleFromFont to cast from Font to QuartzTextStyle.
-rw-r--r-- | cocoa/PlatCocoa.mm | 26 | ||||
-rw-r--r-- | cocoa/QuartzTextLayout.h | 5 |
2 files changed, 20 insertions, 11 deletions
diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm index 9c4d6a340..606927995 100644 --- a/cocoa/PlatCocoa.mm +++ b/cocoa/PlatCocoa.mm @@ -97,10 +97,18 @@ Font::~Font() { //-------------------------------------------------------------------------------------------------- +static QuartzTextStyle *TextStyleFromFont(Font &f) { + return static_cast<QuartzTextStyle *>(f.GetID()); +} + +//-------------------------------------------------------------------------------------------------- + static int FontCharacterSet(Font &f) { - return static_cast<QuartzTextStyle *>(f.GetID())->getCharacterSet(); + return TextStyleFromFont(f)->getCharacterSet(); } +//-------------------------------------------------------------------------------------------------- + /** * Creates a CTFontRef with the given properties. */ @@ -862,12 +870,12 @@ void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION yba ColourDesired colour(fore.AsLong()); CGColorRef color = CGColorCreateGenericRGB(colour.GetRed()/255.0, colour.GetGreen()/255.0, colour.GetBlue()/255.0, 1.0); - QuartzTextStyle *style = static_cast<QuartzTextStyle *>(font_.GetID()); + QuartzTextStyle *style = TextStyleFromFont(font_); style->setCTStyleColor(color); CGColorRelease(color); - textLayout->setText(reinterpret_cast<const UInt8 *>(s), len, encoding, *static_cast<QuartzTextStyle *>(font_.GetID())); + textLayout->setText(s, len, encoding, *style); textLayout->draw(rc.left, ybase); } @@ -875,7 +883,7 @@ void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION yba void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); - textLayout->setText(reinterpret_cast<const UInt8 *>(s), len, encoding, *static_cast<QuartzTextStyle *>(font_.GetID())); + textLayout->setText(s, len, encoding, *TextStyleFromFont(font_)); CTLineRef mLine = textLayout->getCTLine(); assert(mLine != NULL); @@ -923,7 +931,7 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) { if (font_.GetID()) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); - textLayout->setText(reinterpret_cast<const UInt8 *>(s), len, encoding, *static_cast<QuartzTextStyle *>(font_.GetID())); + textLayout->setText(s, len, encoding, *TextStyleFromFont(font_)); return static_cast<XYPOSITION>(textLayout->MeasureStringWidth()); } @@ -934,7 +942,7 @@ XYPOSITION SurfaceImpl::WidthChar(Font &font_, char ch) { char str[2] = { ch, '\0' }; if (font_.GetID()) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); - textLayout->setText(reinterpret_cast<const UInt8 *>(str), 1, encoding, *static_cast<QuartzTextStyle *>(font_.GetID())); + textLayout->setText(str, 1, encoding, *TextStyleFromFont(font_)); return textLayout->MeasureStringWidth(); } else @@ -949,7 +957,7 @@ XYPOSITION SurfaceImpl::Ascent(Font &font_) { if (!font_.GetID()) return 1; - float ascent = static_cast<QuartzTextStyle *>(font_.GetID())->getAscent(); + float ascent = TextStyleFromFont(font_)->getAscent(); return ascent + 0.5f; } @@ -958,7 +966,7 @@ XYPOSITION SurfaceImpl::Descent(Font &font_) { if (!font_.GetID()) return 1; - float descent = static_cast<QuartzTextStyle *>(font_.GetID())->getDescent(); + float descent = TextStyleFromFont(font_)->getDescent(); return descent + 0.5f; } @@ -1490,7 +1498,7 @@ void ListBoxImpl::Create(Window & /*parent*/, int /*ctrlID*/, Scintilla::Point p void ListBoxImpl::SetFont(Font &font_) { // NSCell setFont takes an NSFont* rather than a CTFontRef but they // are the same thing toll-free bridged. - QuartzTextStyle *style = static_cast<QuartzTextStyle *>(font_.GetID()); + QuartzTextStyle *style = TextStyleFromFont(font_); font.Release(); font.SetID(new QuartzTextStyle(*style)); NSFont *pfont = (__bridge NSFont *)style->getFontRef(); diff --git a/cocoa/QuartzTextLayout.h b/cocoa/QuartzTextLayout.h index 033859529..2f4de363b 100644 --- a/cocoa/QuartzTextLayout.h +++ b/cocoa/QuartzTextLayout.h @@ -37,8 +37,9 @@ public: } } - inline void setText(const UInt8 *buffer, size_t byteLength, CFStringEncoding encoding, const QuartzTextStyle &r) { - CFStringRef str = CFStringCreateWithBytes(NULL, buffer, byteLength, encoding, false); + void setText(const char *buffer, size_t byteLength, CFStringEncoding encoding, const QuartzTextStyle &r) { + const UInt8 *puiBuffer = reinterpret_cast<const UInt8 *>(buffer); + CFStringRef str = CFStringCreateWithBytes(NULL, puiBuffer, byteLength, encoding, false); if (!str) return; |