From 12b6e7ecb1d97f60a72841979e7eb77a2b401f1b Mon Sep 17 00:00:00 2001 From: graemeg Date: Fri, 23 May 2008 12:31:07 +0000 Subject: * Added some sanity checks in TfpgFontBase.TextWidth and Utf8Copy --- src/corelib/gfx_utf8utils.pas | 37 ++++++++++++++++++++++--------------- src/corelib/gfxbase.pas | 5 ++++- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/corelib/gfx_utf8utils.pas b/src/corelib/gfx_utf8utils.pas index 137e39de..5c7185a3 100644 --- a/src/corelib/gfx_utf8utils.pas +++ b/src/corelib/gfx_utf8utils.pas @@ -41,22 +41,29 @@ end; // returns substring function UTF8Copy(const s: string; StartCharIndex, CharCount: integer): string; var - StartBytePos: PChar; - EndBytePos: PChar; - MaxBytes: PtrInt; + StartBytePos: PChar; + EndBytePos: PChar; + MaxBytes: PtrInt; begin - StartBytePos := UTF8CharStart(PChar(s),length(s),StartCharIndex-1); - if StartBytePos = nil then - Result := '' - else - begin - MaxBytes := PtrInt(PChar(s)+length(s)-StartBytePos); - EndBytePos := UTF8CharStart(StartBytePos,MaxBytes,CharCount); - if EndBytePos = nil then - Result := copy(s,StartBytePos-PChar(s)+1,MaxBytes) - else - Result := copy(s,StartBytePos-PChar(s)+1,EndBytePos-StartBytePos); - end; + Result := ''; + // Some sanity checks + if (Length(s) = 0) then + Exit; //==> + if CharCount = 0 then + Exit; //==> + + StartBytePos := UTF8CharStart(PChar(s),length(s),StartCharIndex-1); + if StartBytePos = nil then + Result := '' + else + begin + MaxBytes := PtrInt(PChar(s)+length(s)-StartBytePos); + EndBytePos := UTF8CharStart(StartBytePos,MaxBytes,CharCount); + if EndBytePos = nil then + Result := copy(s,StartBytePos-PChar(s)+1,MaxBytes) + else + Result := copy(s,StartBytePos-PChar(s)+1,EndBytePos-StartBytePos); + end; end; function UTF8CStringToUTF8String(SourceStart: PChar; SourceLen: SizeInt): string; diff --git a/src/corelib/gfxbase.pas b/src/corelib/gfxbase.pas index 3acadb64..03aae37d 100644 --- a/src/corelib/gfxbase.pas +++ b/src/corelib/gfxbase.pas @@ -1415,7 +1415,10 @@ end; function TfpgFontBase.TextWidth(const txt: string): integer; begin - Result := FFontRes.GetTextWidth(txt); + if Length(txt) = 0 then + Result := 0 + else + Result := FFontRes.GetTextWidth(txt); end; function TfpgFontBase.Ascent: integer; -- cgit v1.2.3-70-g09d2