diff options
author | Neil <nyamatongwe@gmail.com> | 2018-05-14 14:39:55 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2018-05-14 14:39:55 +1000 |
commit | 3fe056899ac8ad4882f59e196aaa56cd31c2e547 (patch) | |
tree | dcbd61fefbf0dad1472713d890b7c9d6fc8d4814 | |
parent | c4aa7826f3d2178e39e5bff2f6886d7d3d3f46d7 (diff) | |
download | scintilla-mirror-3fe056899ac8ad4882f59e196aaa56cd31c2e547.tar.gz |
Modernize Platform.h (4) - update Surface to use string_view for text arguments.
-rw-r--r-- | cocoa/PlatCocoa.h | 10 | ||||
-rw-r--r-- | cocoa/PlatCocoa.mm | 41 | ||||
-rw-r--r-- | cocoa/QuartzTextLayout.h | 6 | ||||
-rw-r--r-- | gtk/PlatGTK.cxx | 92 | ||||
-rw-r--r-- | gtk/ScintillaGTK.cxx | 4 | ||||
-rw-r--r-- | include/Platform.h | 10 | ||||
-rw-r--r-- | qt/ScintillaEditBase/PlatQt.cpp | 46 | ||||
-rw-r--r-- | qt/ScintillaEditBase/PlatQt.h | 10 | ||||
-rw-r--r-- | src/CallTip.cxx | 6 | ||||
-rw-r--r-- | src/EditView.cxx | 59 | ||||
-rw-r--r-- | src/EditView.h | 2 | ||||
-rw-r--r-- | src/Editor.cxx | 2 | ||||
-rw-r--r-- | src/LineMarker.cxx | 8 | ||||
-rw-r--r-- | src/MarginView.cxx | 4 | ||||
-rw-r--r-- | src/PositionCache.cxx | 4 | ||||
-rw-r--r-- | src/ViewStyle.cxx | 4 | ||||
-rw-r--r-- | win32/PlatWin.cxx | 122 |
17 files changed, 214 insertions, 216 deletions
diff --git a/cocoa/PlatCocoa.h b/cocoa/PlatCocoa.h index aafcf67bb..6a9bada96 100644 --- a/cocoa/PlatCocoa.h +++ b/cocoa/PlatCocoa.h @@ -97,13 +97,13 @@ public: void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) override; void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override; void Copy(PRectangle rc, Scintilla::Point from, Surface &surfaceSource) override; - void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, + 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, const char *s, int len, ColourDesired fore, + 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, 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 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; diff --git a/cocoa/PlatCocoa.mm b/cocoa/PlatCocoa.mm index 9f04870c2..75c386f76 100644 --- a/cocoa/PlatCocoa.mm +++ b/cocoa/PlatCocoa.mm @@ -778,19 +778,19 @@ void SurfaceImpl::Copy(PRectangle rc, Scintilla::Point from, Surface &surfaceSou //-------------------------------------------------------------------------------------------------- -void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { FillRectangle(rc, back); - DrawTextTransparent(rc, font_, ybase, s, len, fore); + DrawTextTransparent(rc, font_, ybase, text, fore); } //-------------------------------------------------------------------------------------------------- -void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { CGContextSaveGState(gc); CGContextClipToRect(gc, PRectangleToCGRect(rc)); - DrawTextNoClip(rc, font_, ybase, s, len, fore, back); + DrawTextNoClip(rc, font_, ybase, text, fore, back); CGContextRestoreGState(gc); } @@ -851,7 +851,7 @@ CFStringEncoding EncodingFromCharacterSet(bool unicode, int characterSet) { } } -void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); ColourDesired colour(fore.AsInteger()); @@ -862,15 +862,15 @@ void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION yba CGColorRelease(color); - textLayout->setText(s, len, encoding, *style); + textLayout->setText(text, encoding, *style); textLayout->draw(rc.left, ybase); } //-------------------------------------------------------------------------------------------------- -void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) { +void SurfaceImpl::MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); - textLayout->setText(s, len, encoding, *TextStyleFromFont(font_)); + textLayout->setText(text, encoding, *TextStyleFromFont(font_)); CTLineRef mLine = textLayout->getCTLine(); assert(mLine != NULL); @@ -881,11 +881,11 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION int ui=0; int i=0; while (ui<fit) { - const unsigned char uch = s[i]; + const unsigned char uch = text[i]; const unsigned int byteCount = UTF8BytesOfLead[uch]; const int codeUnits = UTF16LengthFromUTF8ByteCount(byteCount); CGFloat xPosition = CTLineGetOffsetForStringIndex(mLine, ui+codeUnits, NULL); - for (unsigned int bytePos=0; (bytePos<byteCount) && (i<len); bytePos++) { + for (unsigned int bytePos=0; (bytePos<byteCount) && (i<text.length()); bytePos++) { positions[i++] = static_cast<XYPOSITION>(xPosition); } ui += codeUnits; @@ -893,21 +893,21 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION XYPOSITION lastPos = 0.0f; if (i > 0) lastPos = positions[i-1]; - while (i<len) { + while (i<text.length()) { positions[i++] = lastPos; } } else if (codePage) { int ui = 0; - for (int i=0; i<len;) { - size_t lenChar = DBCSIsLeadByte(codePage, s[i]) ? 2 : 1; + for (int i=0; i<text.length();) { + size_t lenChar = DBCSIsLeadByte(codePage, text[i]) ? 2 : 1; CGFloat xPosition = CTLineGetOffsetForStringIndex(mLine, ui+1, NULL); - for (unsigned int bytePos=0; (bytePos<lenChar) && (i<len); bytePos++) { + for (unsigned int bytePos=0; (bytePos<lenChar) && (i<text.length()); bytePos++) { positions[i++] = static_cast<XYPOSITION>(xPosition); } ui++; } } else { // Single byte encoding - for (int i=0; i<len; i++) { + for (int i=0; i<text.length(); i++) { CGFloat xPosition = CTLineGetOffsetForStringIndex(mLine, i+1, NULL); positions[i] = static_cast<XYPOSITION>(xPosition); } @@ -915,10 +915,10 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION } -XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) { +XYPOSITION SurfaceImpl::WidthText(Font &font_, std::string_view text) { if (font_.GetID()) { CFStringEncoding encoding = EncodingFromCharacterSet(unicodeMode, FontCharacterSet(font_)); - textLayout->setText(s, len, encoding, *TextStyleFromFont(font_)); + textLayout->setText(text, encoding, *TextStyleFromFont(font_)); return static_cast<XYPOSITION>(textLayout->MeasureStringWidth()); } @@ -961,10 +961,9 @@ XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) { if (!font_.GetID()) return 1; - const int sizeStringLength = ELEMENTS(sizeString); - XYPOSITION width = WidthText(font_, sizeString, sizeStringLength); + XYPOSITION width = WidthText(font_, sizeString); - return round(width / sizeStringLength); + return round(width / strlen(sizeString)); } void SurfaceImpl::SetClip(PRectangle rc) { @@ -1551,7 +1550,7 @@ void ListBoxImpl::Append(char *s, int type) { ld.Add(count, type, s); Scintilla::SurfaceImpl surface; - XYPOSITION width = surface.WidthText(font, s, static_cast<int>(strlen(s))); + XYPOSITION width = surface.WidthText(font, s); if (width > maxItemWidth) { maxItemWidth = width; colText.width = maxItemWidth; diff --git a/cocoa/QuartzTextLayout.h b/cocoa/QuartzTextLayout.h index 2f4de363b..c33231176 100644 --- a/cocoa/QuartzTextLayout.h +++ b/cocoa/QuartzTextLayout.h @@ -37,9 +37,9 @@ public: } } - 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); + void setText(std::string_view sv, CFStringEncoding encoding, const QuartzTextStyle &r) { + const UInt8 *puiBuffer = reinterpret_cast<const UInt8 *>(sv.data()); + CFStringRef str = CFStringCreateWithBytes(NULL, puiBuffer, sv.length(), encoding, false); if (!str) return; diff --git a/gtk/PlatGTK.cxx b/gtk/PlatGTK.cxx index 554128477..d938693d0 100644 --- a/gtk/PlatGTK.cxx +++ b/gtk/PlatGTK.cxx @@ -165,12 +165,12 @@ public: void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) override; void Copy(PRectangle rc, Point from, Surface &surfaceSource) override; - void DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore); - 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 DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore); + 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; @@ -598,11 +598,11 @@ void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) { } } -std::string UTF8FromLatin1(const char *s, int len) { - std::string utfForm(len*2 + 1, '\0'); +std::string UTF8FromLatin1(std::string_view text) { + std::string utfForm(text.length()*2 + 1, '\0'); size_t lenU = 0; - for (int i=0; i<len; i++) { - unsigned int uch = static_cast<unsigned char>(s[i]); + for (size_t i=0; i<text.length(); i++) { + unsigned int uch = static_cast<unsigned char>(text[i]); if (uch < 0x80) { utfForm[lenU++] = uch; } else { @@ -614,14 +614,14 @@ std::string UTF8FromLatin1(const char *s, int len) { return utfForm; } -static std::string UTF8FromIconv(const Converter &conv, const char *s, int len) { +static std::string UTF8FromIconv(const Converter &conv, std::string_view text) { if (conv) { - std::string utfForm(len*3+1, '\0'); - char *pin = const_cast<char *>(s); - gsize inLeft = len; + std::string utfForm(text.length()*3+1, '\0'); + char *pin = const_cast<char *>(text.data()); + gsize inLeft = text.length(); char *putf = &utfForm[0]; char *pout = putf; - gsize outLeft = len*3+1; + gsize outLeft = text.length()*3+1; gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft); if (conversions != sizeFailure) { *pout = '\0'; @@ -649,7 +649,7 @@ static size_t MultiByteLenFromIconv(const Converter &conv, const char *s, size_t return 1; } -void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) { PenColour(fore); if (context) { @@ -657,12 +657,12 @@ void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, con if (PFont(font_)->pfd) { std::string utfForm; if (et == UTF8) { - pango_layout_set_text(layout, s, len); + pango_layout_set_text(layout, text.data(), text.length()); } else { SetConverter(PFont(font_)->characterSet); - utfForm = UTF8FromIconv(conv, s, len); + utfForm = UTF8FromIconv(conv, text); if (utfForm.empty()) { // iconv failed so treat as Latin1 - utfForm = UTF8FromLatin1(s, len); + utfForm = UTF8FromLatin1(text); } pango_layout_set_text(layout, utfForm.c_str(), utfForm.length()); } @@ -675,25 +675,25 @@ void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, con } } -void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { FillRectangle(rc, back); - DrawTextBase(rc, font_, ybase, s, len, fore); + DrawTextBase(rc, font_, ybase, text, fore); } // On GTK+, exactly same as DrawTextNoClip -void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) { FillRectangle(rc, back); - DrawTextBase(rc, font_, ybase, s, len, fore); + DrawTextBase(rc, font_, ybase, text, fore); } -void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, +void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) { // Avoid drawing spaces in transparent mode - for (int i=0; i<len; i++) { - if (s[i] != ' ') { - DrawTextBase(rc, font_, ybase, s, len, fore); + for (size_t i=0; i<text.length(); i++) { + if (text[i] != ' ') { + DrawTextBase(rc, font_, ybase, text, fore); return; } } @@ -702,14 +702,14 @@ void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION yba class ClusterIterator { PangoLayoutIter *iter; PangoRectangle pos; - int lenPositions; + size_t lenPositions; public: bool finished; XYPOSITION positionStart; XYPOSITION position; XYPOSITION distance; int curIndex; - ClusterIterator(PangoLayout *layout, int len) : lenPositions(len), finished(false), + ClusterIterator(PangoLayout *layout, size_t len) : lenPositions(len), finished(false), positionStart(0), position(0), distance(0), curIndex(0) { iter = pango_layout_get_iter(layout); pango_layout_iter_get_cluster_extents(iter, NULL, &pos); @@ -733,16 +733,15 @@ public: } }; -void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) { +void SurfaceImpl::MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) { if (font_.GetID()) { - const int lenPositions = len; if (PFont(font_)->pfd) { pango_layout_set_font_description(layout, PFont(font_)->pfd); if (et == UTF8) { // Simple and direct as UTF-8 is native Pango encoding int i = 0; - pango_layout_set_text(layout, s, len); - ClusterIterator iti(layout, lenPositions); + pango_layout_set_text(layout, text.data(), text.length()); + ClusterIterator iti(layout, text.length()); while (!iti.finished) { iti.Next(); int places = iti.curIndex - i; @@ -755,12 +754,12 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION i++; } } - PLATFORM_ASSERT(i == lenPositions); + PLATFORM_ASSERT(i == text.length()); } else { int positionsCalculated = 0; if (et == dbcs) { SetConverter(PFont(font_)->characterSet); - std::string utfForm = UTF8FromIconv(conv, s, len); + std::string utfForm = UTF8FromIconv(conv, text); if (!utfForm.empty()) { // Convert to UTF-8 so can ask Pango for widths, then // Loop through UTF-8 and DBCS forms, taking account of different @@ -776,7 +775,7 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart); int place = 1; while (clusterStart < clusterEnd) { - size_t lenChar = MultiByteLenFromIconv(convMeasure, s+i, len-i); + size_t lenChar = MultiByteLenFromIconv(convMeasure, text.data()+i, text.length()-i); while (lenChar--) { positions[i++] = iti.position - (places - place) * iti.distance / places; positionsCalculated++; @@ -785,17 +784,18 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION place++; } } - PLATFORM_ASSERT(i == lenPositions); + PLATFORM_ASSERT(i == text.length()); } } if (positionsCalculated < 1 ) { + const int lenPositions = static_cast<int>(text.length()); // Either 8-bit or DBCS conversion failed so treat as 8-bit. SetConverter(PFont(font_)->characterSet); const bool rtlCheck = PFont(font_)->characterSet == SC_CHARSET_HEBREW || PFont(font_)->characterSet == SC_CHARSET_ARABIC; - std::string utfForm = UTF8FromIconv(conv, s, len); + std::string utfForm = UTF8FromIconv(conv, text); if (utfForm.empty()) { - utfForm = UTF8FromLatin1(s, len); + utfForm = UTF8FromLatin1(text); } pango_layout_set_text(layout, utfForm.c_str(), utfForm.length()); int i = 0; @@ -827,31 +827,31 @@ void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION // If something failed, fill in rest of the positions positions[i++] = clusterStart; } - PLATFORM_ASSERT(i == lenPositions); + PLATFORM_ASSERT(i == text.length()); } } } } else { // No font so return an ascending range of values - for (int i = 0; i < len; i++) { + for (size_t i = 0; i < text.length(); i++) { positions[i] = i + 1; } } } -XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) { +XYPOSITION SurfaceImpl::WidthText(Font &font_, std::string_view text) { if (font_.GetID()) { if (PFont(font_)->pfd) { std::string utfForm; pango_layout_set_font_description(layout, PFont(font_)->pfd); PangoRectangle pos; if (et == UTF8) { - pango_layout_set_text(layout, s, len); + pango_layout_set_text(layout, text.data(), text.length()); } else { SetConverter(PFont(font_)->characterSet); - utfForm = UTF8FromIconv(conv, s, len); + utfForm = UTF8FromIconv(conv, text); if (utfForm.empty()) { // iconv failed so treat as Latin1 - utfForm = UTF8FromLatin1(s, len); + utfForm = UTF8FromLatin1(text); } pango_layout_set_text(layout, utfForm.c_str(), utfForm.length()); } @@ -906,7 +906,7 @@ XYPOSITION SurfaceImpl::Height(Font &font_) { } XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) { - return WidthText(font_, "n", 1); + return WidthText(font_, "n"); } void SurfaceImpl::SetClip(PRectangle rc) { diff --git a/gtk/ScintillaGTK.cxx b/gtk/ScintillaGTK.cxx index b75a96c5d..6bcb1ae29 100644 --- a/gtk/ScintillaGTK.cxx +++ b/gtk/ScintillaGTK.cxx @@ -111,7 +111,7 @@ static GdkWindow *PWindow(const Window &w) { return gtk_widget_get_window(widget); } -extern std::string UTF8FromLatin1(const char *s, int len); +extern std::string UTF8FromLatin1(std::string_view text); enum { COMMAND_SIGNAL, @@ -1391,7 +1391,7 @@ void ScintillaGTK::GetGtkSelectionText(GtkSelectionData *selectionData, Selectio if (selectionTypeData == GDK_TARGET_STRING) { if (IsUnicodeMode()) { // Unknown encoding so assume in Latin1 - dest = UTF8FromLatin1(dest.c_str(), dest.length()); + dest = UTF8FromLatin1(dest); selText.Copy(dest, SC_CP_UTF8, 0, isRectangular, false); } else { // Assume buffer is in same encoding as selection diff --git a/include/Platform.h b/include/Platform.h index f50aca431..1b2b5f4b7 100644 --- a/include/Platform.h +++ b/include/Platform.h @@ -288,11 +288,11 @@ public: virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0; virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0; - virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0; - virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0; - virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0; - virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0; - virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0; + virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) = 0; + virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore, ColourDesired back) = 0; + virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, std::string_view text, ColourDesired fore) = 0; + virtual void MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) = 0; + virtual XYPOSITION WidthText(Font &font_, std::string_view text) = 0; virtual XYPOSITION Ascent(Font &font_)=0; virtual XYPOSITION Descent(Font &font_)=0; virtual XYPOSITION InternalLeading(Font &font_)=0; diff --git a/qt/ScintillaEditBase/PlatQt.cpp b/qt/ScintillaEditBase/PlatQt.cpp index 26890e4b7..0a4609acc 100644 --- a/qt/ScintillaEditBase/PlatQt.cpp +++ b/qt/ScintillaEditBase/PlatQt.cpp @@ -92,6 +92,10 @@ const char *CharacterSetID(int characterSet) } } +QString UnicodeFromText(QTextCodec *codec, std::string_view text) { + return codec->toUnicode(text.data(), static_cast<int>(text.length())); +} + class FontAndCharacterSet { public: int characterSet; @@ -387,8 +391,7 @@ void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font, XYPOSITION ybase, - const char *s, - int len, + std::string_view text, ColourDesired fore, ColourDesired back) { @@ -397,35 +400,33 @@ void SurfaceImpl::DrawTextNoClip(PRectangle rc, GetPainter()->setBackground(QColorFromCA(back)); GetPainter()->setBackgroundMode(Qt::OpaqueMode); - QString su = codec->toUnicode(s, len); + QString su = UnicodeFromText(codec, text); GetPainter()->drawText(QPointF(rc.left, ybase), su); } void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font, XYPOSITION ybase, - const char *s, - int len, + std::string_view text, ColourDesired fore, ColourDesired back) { SetClip(rc); - DrawTextNoClip(rc, font, ybase, s, len, fore, back); + DrawTextNoClip(rc, font, ybase, text, fore, back); GetPainter()->setClipping(false); } void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font, XYPOSITION ybase, - const char *s, - int len, + std::string_view text, ColourDesired fore) { SetFont(font); PenColour(fore); GetPainter()->setBackgroundMode(Qt::TransparentMode); - QString su = codec->toUnicode(s, len); + QString su = UnicodeFromText(codec, text); GetPainter()->drawText(QPointF(rc.left, ybase), su); } @@ -435,14 +436,13 @@ void SurfaceImpl::SetClip(PRectangle rc) } void SurfaceImpl::MeasureWidths(Font &font, - const char *s, - int len, + std::string_view text, XYPOSITION *positions) { if (!font.GetID()) return; SetCodec(font); - QString su = codec->toUnicode(s, len); + QString su = UnicodeFromText(codec, text); QTextLayout tlay(su, *FontPointer(font), GetPaintDevice()); tlay.beginLayout(); QTextLine tl = tlay.createLine(); @@ -450,13 +450,13 @@ void SurfaceImpl::MeasureWidths(Font &font, if (unicodeMode) { int fit = su.size(); int ui=0; - int i=0; + size_t i=0; while (ui<fit) { - const unsigned char uch = s[i]; + const unsigned char uch = text[i]; const unsigned int byteCount = UTF8BytesOfLead[uch]; const int codeUnits = UTF16LengthFromUTF8ByteCount(byteCount); qreal xPosition = tl.cursorToX(ui+codeUnits); - for (unsigned int bytePos=0; (bytePos<byteCount) && (i<len); bytePos++) { + for (size_t bytePos=0; (bytePos<byteCount) && (i<text.length()); bytePos++) { positions[i++] = xPosition; } ui += codeUnits; @@ -464,34 +464,34 @@ void SurfaceImpl::MeasureWidths(Font &font, XYPOSITION lastPos = 0; if (i > 0) lastPos = positions[i-1]; - while (i<len) { + while (i<text.length()) { positions[i++] = lastPos; } } else if (codePage) { // DBCS int ui = 0; - for (int i=0; i<len;) { - size_t lenChar = DBCSIsLeadByte(codePage, s[i]) ? 2 : 1; + for (size_t i=0; i<text.length();) { + size_t lenChar = DBCSIsLeadByte(codePage, text[i]) ? 2 : 1; qreal xPosition = tl.cursorToX(ui+1); - for (unsigned int bytePos=0; (bytePos<lenChar) && (i<len); bytePos++) { + for (unsigned int bytePos=0; (bytePos<lenChar) && (i<text.length()); bytePos++) { positions[i++] = xPosition; } ui++; } } else { // Single byte encoding - for (int i=0; i<len; i++) { + for (int i=0; i<static_cast<int>(text.length()); i++) { positions[i] = tl.cursorToX(i+1); } } } -XYPOSITION SurfaceImpl::WidthText(Font &font, const char *s, int len) +XYPOSITION SurfaceImpl::WidthText(Font &font, std::string_view text) { QFontMetricsF metrics(*FontPointer(font), device); SetCodec(font); - QString string = codec->toUnicode(s, len); - return metrics.width(string); + QString su = UnicodeFromText(codec, text); + return metrics.width(su); } XYPOSITION SurfaceImpl::Ascent(Font &font) diff --git a/qt/ScintillaEditBase/PlatQt.h b/qt/ScintillaEditBase/PlatQt.h index 07da76682..a03271653 100644 --- a/qt/ScintillaEditBase/PlatQt.h +++ b/qt/ScintillaEditBase/PlatQt.h @@ -97,14 +97,14 @@ public: void Copy(PRectangle rc, Point from, Surface &surfaceSource) override; void DrawTextNoClip(PRectangle rc, Font &font, XYPOSITION ybase, - const char *s, int len, ColourDesired fore, ColourDesired back) override; + std::string_view text, ColourDesired fore, ColourDesired back) override; void DrawTextClipped(PRectangle rc, Font &font, XYPOSITION ybase, - const char *s, int len, ColourDesired fore, ColourDesired back) override; + std::string_view text, 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, + std::string_view text, ColourDesired fore) override; + void MeasureWidths(Font &font, std::string_view text, XYPOSITION *positions) override; - XYPOSITION WidthText(Font &font, const char *s, int len) 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; diff --git a/src/CallTip.cxx b/src/CallTip.cxx index 2eab1146f..437933af9 100644 --- a/src/CallTip.cxx +++ b/src/CallTip.cxx @@ -154,13 +154,13 @@ void CallTip::DrawChunk(Surface *surface, int &x, const char *s, } else if (IsTabCharacter(s[startSeg])) { xEnd = NextTabPos(x); } else { - xEnd = x + static_cast<int>(lround(surface->WidthText(font, s + startSeg, endSeg - startSeg))); + std::string_view segText(s + startSeg, endSeg - startSeg); + xEnd = x + static_cast<int>(lround(surface->WidthText(font, segText))); if (draw) { rcClient.left = static_cast<XYPOSITION>(x); rcClient.right = static_cast<XYPOSITION>(xEnd); surface->DrawTextTransparent(rcClient, font, static_cast<XYPOSITION>(ytext), - s+startSeg, endSeg - startSeg, - highlight ? colourSel : colourUnSel); + segText, highlight ? colourSel : colourUnSel); } } x = xEnd; diff --git a/src/EditView.cxx b/src/EditView.cxx index a63b666e5..b406747fb 100644 --- a/src/EditView.cxx +++ b/src/EditView.cxx @@ -96,8 +96,8 @@ static int WidthStyledText(Surface *surface, const ViewStyle &vs, int styleOffse while ((endSegment + 1 < len) && (styles[endSegment + 1] == style)) endSegment++; FontAlias fontText = vs.styles[style + styleOffset].font; - width += static_cast<int>(surface->WidthText(fontText, text + start, - static_cast<int>(endSegment - start + 1))); + std::string_view sv(text + start, endSegment - start + 1); + width += static_cast<int>(surface->WidthText(fontText, sv)); start = endSegment + 1; } return width; @@ -113,8 +113,8 @@ int WidestLineWidth(Surface *surface, const ViewStyle &vs, int styleOffset, cons widthSubLine = WidthStyledText(surface, vs, styleOffset, st.text + start, st.styles + start, lenLine); } else { FontAlias fontText = vs.styles[styleOffset + st.style].font; - widthSubLine = static_cast<int>(surface->WidthText(fontText, - st.text + start, static_cast<int>(lenLine))); + std::string_view text(st.text + start, lenLine); + widthSubLine = static_cast<int>(surface->WidthText(fontText, text)); } if (widthSubLine > widthMax) widthMax = widthSubLine; @@ -124,18 +124,18 @@ int WidestLineWidth(Surface *surface, const ViewStyle &vs, int styleOffset, cons } void DrawTextNoClipPhase(Surface *surface, PRectangle rc, const Style &style, XYPOSITION ybase, - const char *s, int len, DrawPhase phase) { + std::string_view text, DrawPhase phase) { FontAlias fontText = style.font; if (phase & drawBack) { if (phase & drawText) { // Drawing both - surface->DrawTextNoClip(rc, fontText, ybase, s, len, + surface->DrawTextNoClip(rc, fontText, ybase, text, style.fore, style.back); } else { surface->FillRectangle(rc, style.back); } } else if (phase & drawText) { - surface->DrawTextTransparent(rc, fontText, ybase, s, len, style.fore); + surface->DrawTextTransparent(rc, fontText, ybase, text, style.fore); } } @@ -152,22 +152,21 @@ void DrawStyledText(Surface *surface, const ViewStyle &vs, int styleOffset, PRec end++; style += styleOffset; FontAlias fontText = vs.styles[style].font; - const int width = static_cast<int>(surface->WidthText(fontText, - st.text + start + i, static_cast<int>(end - i + 1))); + std::string_view text(st.text + start + i, end - i + 1); + const int width = static_cast<int>(surface->WidthText(fontText, text)); PRectangle rcSegment = rcText; rcSegment.left = static_cast<XYPOSITION>(x); rcSegment.right = static_cast<XYPOSITION>(x + width + 1); DrawTextNoClipPhase(surface, rcSegment, vs.styles[style], - rcText.top + vs.maxAscent, st.text + start + i, - static_cast<int>(end - i + 1), phase); + rcText.top + vs.maxAscent, text, phase); x += width; i = end + 1; } } else { const size_t style = st.style + styleOffset; DrawTextNoClipPhase(surface, rcText, vs.styles[style], - rcText.top + vs.maxAscent, st.text + start, - static_cast<int>(length), phase); + rcText.top + vs.maxAscent, + std::string_view(st.text + start, length), phase); } } @@ -803,7 +802,7 @@ static void SimpleAlphaRectangle(Surface *surface, PRectangle rc, ColourDesired } static void DrawTextBlob(Surface *surface, const ViewStyle &vsDraw, PRectangle rcSegment, - const char *s, ColourDesired textBack, ColourDesired textFore, bool fillBackground) { + std::string_view text, ColourDesired textBack, ColourDesired textFore, bool fillBackground) { if (rcSegment.Empty()) return; if (fillBackground) { @@ -823,7 +822,7 @@ static void DrawTextBlob(Surface *surface, const ViewStyle &vsDraw, PRectangle r rcChar.left++; rcChar.right--; surface->DrawTextClipped(rcChar, ctrlCharsFont, - rcSegment.top + vsDraw.maxAscent, s, static_cast<int>(s ? strlen(s) : 0), + rcSegment.top + vsDraw.maxAscent, text, textBack, textFore); } @@ -1103,10 +1102,9 @@ void EditView::DrawFoldDisplayText(Surface *surface, const EditModel &model, con return; PRectangle rcSegment = rcLine; - const char *foldDisplayText = model.pcs->GetFoldDisplayText(line); - const int lengthFoldDisplayText = static_cast<int>(strlen(foldDisplayText)); + const std::string_view foldDisplayText = model.pcs->GetFoldDisplayText(line); FontAlias fontText = vsDraw.styles[STYLE_FOLDDISPLAYTEXT].font; - const int widthFoldDisplayText = static_cast<int>(surface->WidthText(fontText, foldDisplayText, lengthFoldDisplayText)); + const int widthFoldDisplayText = static_cast<int>(surface->WidthText(fontText, foldDisplayText)); int eolInSelection = 0; int alpha = SC_ALPHA_NOALPHA; @@ -1154,11 +1152,11 @@ void EditView::DrawFoldDisplayText(Surface *surface, const EditModel &model, con if (phasesDraw != phasesOne) { surface->DrawTextTransparent(rcSegment, textFont, rcSegment.top + vsDraw.maxAscent, foldDisplayText, - lengthFoldDisplayText, textFore); + textFore); } else { surface->DrawTextNoClip(rcSegment, textFont, rcSegment.top + vsDraw.maxAscent, foldDisplayText, - lengthFoldDisplayText, textFore, textBack); + textFore, textBack); } } @@ -1309,9 +1307,9 @@ static void DrawBlockCaret(Surface *surface, const EditModel &model, const ViewS // (inversed) for drawing the caret here. const int styleMain = ll->styles[offsetFirstChar]; FontAlias fontText = vsDraw.styles[styleMain].font; + std::string_view text(&ll->chars[offsetFirstChar], numCharsToDraw); surface->DrawTextClipped(rcCaret, fontText, - rcCaret.top + vsDraw.maxAscent, &ll->chars[offsetFirstChar], - static_cast<int>(numCharsToDraw), vsDraw.styles[styleMain].back, + rcCaret.top + vsDraw.maxAscent, text, vsDraw.styles[styleMain].back, caretColour); } @@ -1735,23 +1733,22 @@ void EditView::DrawForeground(Surface *surface, const EditModel &model, const Vi const char cc[2] = { static_cast<char>(vsDraw.controlCharSymbol), '\0' }; surface->DrawTextNoClip(rcSegment, ctrlCharsFont, rcSegment.top + vsDraw.maxAscent, - cc, 1, textBack, textFore); + cc, textBack, textFore); } else { - DrawTextBlob(surface, vsDraw, rcSegment, ts.representation->stringRep.c_str(), + DrawTextBlob(surface, vsDraw, rcSegment, ts.representation->stringRep, textBack, textFore, phasesDraw == phasesOne); } } } else { // Normal text display if (vsDraw.styles[styleMain].visible) { + std::string_view text(&ll->chars[ts.start], i - ts.start + 1); if (phasesDraw != phasesOne) { surface->DrawTextTransparent(rcSegment, textFont, - rcSegment.top + vsDraw.maxAscent, &ll->chars[ts.start], - static_cast<int>(i - ts.start + 1), textFore); + rcSegment.top + vsDraw.maxAscent, text, textFore); } else { surface->DrawTextNoClip(rcSegment, textFont, - rcSegment.top + vsDraw.maxAscent, &ll->chars[ts.start], - static_cast<int>(i - ts.start + 1), textFore, textBack); + rcSegment.top + vsDraw.maxAscent, text, textFore, textBack); } } if (vsDraw.viewWhitespace != wsInvisible || @@ -2271,7 +2268,7 @@ Sci::Position EditView::FormatRange(bool draw, const Sci_RangeToFormat *pfr, Sur int lineNumberWidth = 0; if (lineNumberIndex >= 0) { lineNumberWidth = static_cast<int>(surfaceMeasure->WidthText(vsPrint.styles[STYLE_LINENUMBER].font, - "99999" lineNumberPrintSpace, 5 + static_cast<int>(strlen(lineNumberPrintSpace)))); + "99999" lineNumberPrintSpace)); vsPrint.ms[lineNumberIndex].width = lineNumberWidth; vsPrint.Refresh(*surfaceMeasure, model.pdoc->tabInChars); // Recalculate fixedColumnWidth } @@ -2352,10 +2349,10 @@ Sci::Position EditView::FormatRange(bool draw, const Sci_RangeToFormat *pfr, Sur rcNumber.right = rcNumber.left + lineNumberWidth; // Right justify rcNumber.left = rcNumber.right - surfaceMeasure->WidthText( - vsPrint.styles[STYLE_LINENUMBER].font, number.c_str(), static_cast<int>(number.length())); + vsPrint.styles[STYLE_LINENUMBER].font, number); surface->FlushCachedState(); surface->DrawTextNoClip(rcNumber, vsPrint.styles[STYLE_LINENUMBER].font, - static_cast<XYPOSITION>(ypos + vsPrint.maxAscent), number.c_str(), static_cast<int>(number.length()), + static_cast<XYPOSITION>(ypos + vsPrint.maxAscent), number, vsPrint.styles[STYLE_LINENUMBER].fore, vsPrint.styles[STYLE_LINENUMBER].back); } diff --git a/src/EditView.h b/src/EditView.h index f649f7c87..51e2a1e53 100644 --- a/src/EditView.h +++ b/src/EditView.h @@ -36,7 +36,7 @@ enum DrawPhase { bool ValidStyledText(const ViewStyle &vs, size_t styleOffset, const StyledText &st); int WidestLineWidth(Surface *surface, const ViewStyle &vs, int styleOffset, const StyledText &st); void DrawTextNoClipPhase(Surface *surface, PRectangle rc, const Style &style, XYPOSITION ybase, - const char *s, int len, DrawPhase phase); + std::string_view text, DrawPhase phase); void DrawStyledText(Surface *surface, const ViewStyle &vs, int styleOffset, PRectangle rcText, const StyledText &st, size_t start, size_t length, DrawPhase phase); diff --git a/src/Editor.cxx b/src/Editor.cxx index 80cdbc97a..36ebc5cea 100644 --- a/src/Editor.cxx +++ b/src/Editor.cxx @@ -1792,7 +1792,7 @@ int Editor::TextWidth(int style, const char *text) { RefreshStyleData(); AutoSurface surface(this); if (surface) { - return static_cast<int>(surface->WidthText(vs.styles[style].font, text, static_cast<int>(strlen(text)))); + return static_cast<int>(surface->WidthText(vs.styles[style].font, text)); } else { return 1; } diff --git a/src/LineMarker.cxx b/src/LineMarker.cxx index a2f7e299e..53231d778 100644 --- a/src/LineMarker.cxx +++ b/src/LineMarker.cxx @@ -9,6 +9,7 @@ #include <cmath> #include <stdexcept> +#include <string> #include <string_view> #include <vector> #include <map> @@ -377,14 +378,13 @@ void LineMarker::Draw(Surface *surface, PRectangle &rcWhole, Font &fontForCharac DrawMinus(surface, centreX, centreY, blobSize, colourTail); } else if (markType >= SC_MARK_CHARACTER) { - char character[1]; - character[0] = static_cast<char>(markType - SC_MARK_CHARACTER); - const XYPOSITION width = surface->WidthText(fontForCharacter, character, 1); + std::string character(1, static_cast<char>(markType - SC_MARK_CHARACTER)); + const XYPOSITION width = surface->WidthText(fontForCharacter, character); PRectangle rcText = rc; rcText.left += (rc.Width() - width) / 2; rcText.right = rc.left + width; surface->DrawTextClipped(rcText, fontForCharacter, rcText.bottom - 2, - character, 1, fore, back); + character, fore, back); } else if (markType == SC_MARK_DOTDOTDOT) { XYPOSITION right = static_cast<XYPOSITION>(centreX - 6); diff --git a/src/MarginView.cxx b/src/MarginView.cxx index f9d566b44..6604657f6 100644 --- a/src/MarginView.cxx +++ b/src/MarginView.cxx @@ -393,11 +393,11 @@ void MarginView::PaintMargin(Surface *surface, Sci::Line topLine, PRectangle rc, } PRectangle rcNumber = rcMarker; // Right justify - const XYPOSITION width = surface->WidthText(fontLineNumber, sNumber.c_str(), static_cast<int>(sNumber.length())); + const XYPOSITION width = surface->WidthText(fontLineNumber, sNumber); const XYPOSITION xpos = rcNumber.right - width - vs.marginNumberPadding; rcNumber.left = xpos; DrawTextNoClipPhase(surface, rcNumber, vs.styles[STYLE_LINENUMBER], - rcNumber.top + vs.maxAscent, sNumber.c_str(), static_cast<int>(sNumber.length()), drawAll); + rcNumber.top + vs.maxAscent, sNumber, drawAll); } else if (vs.wrapVisualFlags & SC_WRAPVISUALFLAG_MARGIN) { PRectangle rcWrapMarker = rcMarker; rcWrapMarker.right -= wrapMarkerPaddingRight; diff --git a/src/PositionCache.cxx b/src/PositionCache.cxx index 9769202c6..bf5560678 100644 --- a/src/PositionCache.cxx +++ b/src/PositionCache.cxx @@ -691,7 +691,7 @@ void PositionCache::MeasureWidths(Surface *surface, const ViewStyle &vstyle, uns while (startSegment < len) { const unsigned int lenSegment = pdoc->SafeSegment(s + startSegment, len - startSegment, BreakFinder::lengthEachSubdivision); FontAlias fontStyle = vstyle.styles[styleNumber].font; - surface->MeasureWidths(fontStyle, s + startSegment, lenSegment, positions + startSegment); + surface->MeasureWidths(fontStyle, std::string_view(s + startSegment, lenSegment), positions + startSegment); for (unsigned int inSeg = 0; inSeg < lenSegment; inSeg++) { positions[startSegment + inSeg] += xStartSegment; } @@ -700,7 +700,7 @@ void PositionCache::MeasureWidths(Surface *surface, const ViewStyle &vstyle, uns } } else { FontAlias fontStyle = vstyle.styles[styleNumber].font; - surface->MeasureWidths(fontStyle, s, len, positions); + surface->MeasureWidths(fontStyle, std::string_view(s, len), positions); } if (probe < pces.size()) { // Store into cache diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 1a95450ce..2700fb016 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -80,7 +80,7 @@ void FontRealised::Realise(Surface &surface, int zoomLevel, int technology, cons descent = static_cast<unsigned int>(surface.Descent(font)); capitalHeight = surface.Ascent(font) - surface.InternalLeading(font); aveCharWidth = surface.AverageCharWidth(font); - spaceWidth = surface.WidthText(font, " ", 1); + spaceWidth = surface.WidthText(font, " "); } ViewStyle::ViewStyle() : markers(MARKER_MAX + 1), indicators(INDIC_MAX + 1) { @@ -367,7 +367,7 @@ void ViewStyle::Refresh(Surface &surface, int tabInChars) { controlCharWidth = 0.0; if (controlCharSymbol >= 32) { const char cc[2] = { static_cast<char>(controlCharSymbol), '\0' }; - controlCharWidth = surface.WidthText(styles[STYLE_CONTROLCHAR].font, cc, 1); + controlCharWidth = surface.WidthText(styles[STYLE_CONTROLCHAR].font, cc); } CalculateMarginWidthAndMask(); 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<wchar_t, stackBufferLength> { 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<wchar_t, stackBufferLength>(len) { + TextWide(std::string_view text, bool unicodeMode, int codePage=0) : + VarBuffer<wchar_t, stackBufferLength>(text.length()) { if (unicodeMode) { - tlen = static_cast<int>(UTF16FromUTF8(s, len, buffer, len)); + tlen = static_cast<int>(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<int>(text.length()), + buffer, static_cast<int>(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<int, stackBufferLength> 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<int>(rc.left); const int yBaseInt = static_cast<int>(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<UINT>(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<len; i++) { - if (s[i] != ' ') { + for (size_t i=0; i<text.length(); i++) { + if (text[i] != ' ') { ::SetTextColor(hdc, fore.AsInteger()); ::SetBkMode(hdc, TRANSPARENT); - DrawTextCommon(rc, font_, ybase, s, len, 0); + DrawTextCommon(rc, font_, ybase, text, 0); ::SetBkMode(hdc, OPAQUE); return; } } } -XYPOSITION SurfaceGDI::WidthText(Font &font_, const char *s, int len) { +XYPOSITION SurfaceGDI::WidthText(Font &font_, std::string_view text) { SetFont(font_); SIZE sz={0,0}; if (!unicodeMode) { - ::GetTextExtentPoint32A(hdc, s, std::min(len, maxLenText), &sz); + ::GetTextExtentPoint32A(hdc, text.data(), std::min(static_cast<int>(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<XYPOSITION>(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<int>(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<len; i++) { - if (s[i] != ' ') { + for (size_t i=0; i<text.length(); i++) { + if (text[i] != ' ') { if (pRenderTarget) { D2DPenColour(fore); - DrawTextCommon(rc, font_, ybase, s, len, 0); + DrawTextCommon(rc, font_, ybase, text, 0); } return; } } } -XYPOSITION SurfaceD2D::WidthText(Font &font_, const char *s, int len) { +XYPOSITION SurfaceD2D::WidthText(Font &font_, std::string_view text) { FLOAT width = 1.0; SetFont(font_); - const TextWide tbuf(s, len, unicodeMode, codePageText); + const TextWide tbuf(text, unicodeMode, codePageText); if (pIDWriteFactory && pTextFormat) { // Create a layout IDWriteTextLayout *pTextLayout = 0; @@ -1608,13 +1610,13 @@ XYPOSITION SurfaceD2D::WidthText(Font &font_, const char *s, int len) { return width; } -void SurfaceD2D::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) { +void SurfaceD2D::MeasureWidths(Font &font_, std::string_view text, XYPOSITION *positions) { SetFont(font_); if (!pIDWriteFactory || !pTextFormat) { // SetFont failed or no access to DirectWrite so give up. return; } - const TextWide tbuf(s, len, unicodeMode, codePageText); + const TextWide tbuf(text, unicodeMode, codePageText); TextPositions poses(tbuf.tlen); // Initialize poses for safety. std::fill(poses.buffer, poses.buffer + tbuf.tlen, 0.0f); @@ -1645,14 +1647,14 @@ void SurfaceD2D::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * if (unicodeMode) { // Map the widths given for UTF-16 characters back onto the UTF-8 input string int ui=0; - int i=0; + size_t i=0; while (ui<tbuf.tlen) { - const unsigned char uch = s[i]; + const unsigned char uch = text[i]; const unsigned int byteCount = UTF8BytesOfLead[uch]; if (byteCount == 4) { // Non-BMP ui++; } - for (unsigned int bytePos=0; (bytePos<byteCount) && (i<len); bytePos++) { + for (unsigned int bytePos=0; (bytePos<byteCount) && (i<text.length()); bytePos++) { positions[i++] = poses.buffer[ui]; } ui++; @@ -1660,13 +1662,13 @@ void SurfaceD2D::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * XYPOSITION lastPos = 0.0f; if (i > 0) lastPos = positions[i-1]; - while (i<len) { + while (i<text.length()) { positions[i++] = lastPos; } } else if (codePageText == 0) { // One char per position - PLATFORM_ASSERT(len == tbuf.tlen); + PLATFORM_ASSERT(text.length() == tbuf.tlen); for (int kk=0; kk<tbuf.tlen; kk++) { positions[kk] = poses.buffer[kk]; } @@ -1675,9 +1677,9 @@ void SurfaceD2D::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION * // May be one or two bytes per position int ui = 0; - for (int i=0; i<len && ui<tbuf.tlen;) { + for (size_t i=0; i<text.length() && ui<tbuf.tlen;) { positions[i] = poses.buffer[ui]; - if (DBCSIsLeadByte(codePageText, s[i])) { + if (DBCSIsLeadByte(codePageText, text[i])) { positions[i+1] = poses.buffer[ui]; i += 2; } else { @@ -2169,7 +2171,7 @@ PRectangle ListBoxX::GetDesiredRect() { if (widestItem) { len = static_cast<int>(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<int>(TextInset.x), static_cast<int>(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); |