From 3fe056899ac8ad4882f59e196aaa56cd31c2e547 Mon Sep 17 00:00:00 2001 From: Neil Date: Mon, 14 May 2018 14:39:55 +1000 Subject: Modernize Platform.h (4) - update Surface to use string_view for text arguments. --- win32/PlatWin.cxx | 122 +++++++++++++++++++++++++++--------------------------- 1 file changed, 62 insertions(+), 60 deletions(-) (limited to 'win32') diff --git a/win32/PlatWin.cxx b/win32/PlatWin.cxx index 5bff083e3..388002c70 100644 --- a/win32/PlatWin.cxx +++ b/win32/PlatWin.cxx @@ -500,13 +500,14 @@ const int stackBufferLength = 1000; class TextWide : public VarBuffer { public: int tlen; // Using int instead of size_t as most Win32 APIs take int. - TextWide(const char *s, int len, bool unicodeMode, int codePage=0) : - VarBuffer(len) { + TextWide(std::string_view text, bool unicodeMode, int codePage=0) : + VarBuffer(text.length()) { if (unicodeMode) { - tlen = static_cast(UTF16FromUTF8(s, len, buffer, len)); + tlen = static_cast(UTF16FromUTF8(text.data(), text.length(), buffer, text.length())); } else { // Support Asian string display in 9x English - tlen = ::MultiByteToWideChar(codePage, 0, s, len, buffer, len); + tlen = ::MultiByteToWideChar(codePage, 0, text.data(), static_cast(text.length()), + buffer, static_cast(text.length())); } } }; @@ -565,12 +566,12 @@ public: void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override; void Copy(PRectangle rc, Point from, Surface &surfaceSource) override; - void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions); - void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override; - void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override; - void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore) override; - void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) override; - XYPOSITION WidthText(Font &font_, const char *s, int len) override; + void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, UINT fuOptions); + void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) override; + void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) override; + void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) override; + void MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) override; + XYPOSITION WidthText(Font &font_, std::string_view text) override; XYPOSITION Ascent(Font &font_) override; XYPOSITION Descent(Font &font_) override; XYPOSITION InternalLeading(Font &font_) override; @@ -913,69 +914,70 @@ void SurfaceGDI::Copy(PRectangle rc, Point from, Surface &surfaceSource) { typedef VarBuffer TextPositionsI; -void SurfaceGDI::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions) { +void SurfaceGDI::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, UINT fuOptions) { SetFont(font_); const RECT rcw = RectFromPRectangle(rc); const int x = static_cast(rc.left); const int yBaseInt = static_cast(ybase); if (unicodeMode) { - const TextWide tbuf(s, len, unicodeMode, codePage); - ::ExtTextOutW(hdc, x, yBaseInt, fuOptions, &rcw, tbuf.buffer, tbuf.tlen, NULL); + const TextWide tbuf(text, unicodeMode, codePage); + ::ExtTextOutW(hdc, x, yBaseInt, fuOptions, &rcw, tbuf.buffer, tbuf.tlen, nullptr); } else { - ::ExtTextOutA(hdc, x, yBaseInt, fuOptions, &rcw, s, len, NULL); + ::ExtTextOutA(hdc, x, yBaseInt, fuOptions, &rcw, text.data(), static_cast(text.length()), nullptr); } } -void SurfaceGDI::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceGDI::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { ::SetTextColor(hdc, fore.AsInteger()); ::SetBkColor(hdc, back.AsInteger()); - DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE); + DrawTextCommon(rc, font_, ybase, text, ETO_OPAQUE); } -void SurfaceGDI::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceGDI::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { ::SetTextColor(hdc, fore.AsInteger()); ::SetBkColor(hdc, back.AsInteger()); - DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE | ETO_CLIPPED); + DrawTextCommon(rc, font_, ybase, text, ETO_OPAQUE | ETO_CLIPPED); } -void SurfaceGDI::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceGDI::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) { // Avoid drawing spaces in transparent mode - for (int i=0; i(text.length()), maxLenText), &sz); } else { - const TextWide tbuf(s, len, unicodeMode, codePage); + const TextWide tbuf(text, unicodeMode, codePage); ::GetTextExtentPoint32W(hdc, tbuf.buffer, tbuf.tlen, &sz); } return static_cast(sz.cx); } -void SurfaceGDI::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) { +void SurfaceGDI::MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) { // Zero positions to avoid random behaviour on failure. - std::fill(positions, positions + len, 0.0f); + std::fill(positions, positions + text.length(), 0.0f); SetFont(font_); SIZE sz={0,0}; int fit = 0; int i = 0; + const int len = static_cast(text.length()); if (unicodeMode) { - const TextWide tbuf(s, len, unicodeMode, codePage); + const TextWide tbuf(text, unicodeMode, codePage); TextPositionsI poses(tbuf.tlen); if (!::GetTextExtentExPointW(hdc, tbuf.buffer, tbuf.tlen, maxWidthMeasure, &fit, poses.buffer, &sz)) { // Failure @@ -983,7 +985,7 @@ void SurfaceGDI::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * } // Map the widths given for UTF-16 characters back onto the UTF-8 input string for (int ui = 0; ui < fit; ui++) { - const unsigned char uch = s[i]; + const unsigned char uch = text[i]; const unsigned int byteCount = UTF8BytesOfLead[uch]; if (byteCount == 4) { // Non-BMP ui++; @@ -994,7 +996,7 @@ void SurfaceGDI::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * } } else { TextPositionsI poses(len); - if (!::GetTextExtentExPointA(hdc, s, len, maxWidthMeasure, &fit, poses.buffer, &sz)) { + if (!::GetTextExtentExPointA(hdc, text.data(), len, maxWidthMeasure, &fit, poses.buffer, &sz)) { // Eeek - a NULL DC or other foolishness could cause this. return; } @@ -1005,7 +1007,7 @@ void SurfaceGDI::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * } // If any positions not filled in then use the last position for them const XYPOSITION lastPos = (fit > 0) ? positions[fit - 1] : 0.0f; - std::fill(positions+i, positions + len, lastPos); + std::fill(positions+i, positions + text.length(), lastPos); } XYPOSITION SurfaceGDI::Ascent(Font &font_) { @@ -1126,12 +1128,12 @@ public: void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override; void Copy(PRectangle rc, Point from, Surface &surfaceSource) override; - void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions); - void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override; - void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back) override; - void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore) override; - void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) override; - XYPOSITION WidthText(Font &font_, const char *s, int len) override; + void DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, UINT fuOptions); + void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) override; + void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) override; + void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) override; + void MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) override; + XYPOSITION WidthText(Font &font_, std::string_view text) override; XYPOSITION Ascent(Font &font_) override; XYPOSITION Descent(Font &font_) override; XYPOSITION InternalLeading(Font &font_) override; @@ -1531,11 +1533,11 @@ void SurfaceD2D::Copy(PRectangle rc, Point from, Surface &surfaceSource) { } } -void SurfaceD2D::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, UINT fuOptions) { +void SurfaceD2D::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, UINT fuOptions) { SetFont(font_); // Use Unicode calls - const TextWide tbuf(s, len, unicodeMode, codePageText); + const TextWide tbuf(text, unicodeMode, codePageText); if (pRenderTarget && pTextFormat && pBrush) { if (fuOptions & ETO_CLIPPED) { D2D1_RECT_F rcClip = {rc.left, rc.top, rc.right, rc.bottom}; @@ -1558,42 +1560,42 @@ void SurfaceD2D::DrawTextCommon(PRectangle rc, Font &font_, XYPOSITION ybase, co } } -void SurfaceD2D::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceD2D::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { if (pRenderTarget) { FillRectangle(rc, back); D2DPenColour(fore); - DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE); + DrawTextCommon(rc, font_, ybase, text, ETO_OPAQUE); } } -void SurfaceD2D::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceD2D::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { if (pRenderTarget) { FillRectangle(rc, back); D2DPenColour(fore); - DrawTextCommon(rc, font_, ybase, s, len, ETO_OPAQUE | ETO_CLIPPED); + DrawTextCommon(rc, font_, ybase, text, ETO_OPAQUE | ETO_CLIPPED); } } -void SurfaceD2D::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceD2D::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) { // Avoid drawing spaces in transparent mode - for (int i=0; i 0) lastPos = positions[i-1]; - while (i(strlen(widestItem)); if (unicodeMode) { - const TextWide tbuf(widestItem, len, unicodeMode); + const TextWide tbuf(widestItem, unicodeMode); ::GetTextExtentPoint32W(hdc, tbuf.buffer, tbuf.tlen, &textSize); } else { ::GetTextExtentPoint32A(hdc, widestItem, len, &textSize); @@ -2286,7 +2288,7 @@ void ListBoxX::Draw(DRAWITEMSTRUCT *pDrawItem) { ::InsetRect(&rcText, static_cast(TextInset.x), static_cast(TextInset.y)); if (unicodeMode) { - const TextWide tbuf(text, len, unicodeMode); + const TextWide tbuf(text, unicodeMode); ::DrawTextW(pDrawItem->hDC, tbuf.buffer, tbuf.tlen, &rcText, DT_NOPREFIX|DT_END_ELLIPSIS|DT_SINGLELINE|DT_NOCLIP); } else { ::DrawTextA(pDrawItem->hDC, text, len, &rcText, DT_NOPREFIX|DT_END_ELLIPSIS|DT_SINGLELINE|DT_NOCLIP); -- cgit v1.2.3