diff options
author | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2008-05-23 12:31:07 +0000 |
---|---|---|
committer | graemeg <graemeg@ae50a9b5-8222-0410-bf8d-8a13f76226bf> | 2008-05-23 12:31:07 +0000 |
commit | 12b6e7ecb1d97f60a72841979e7eb77a2b401f1b (patch) | |
tree | 7782a8ef143afe46d823a334014beae5d110d3d5 /src | |
parent | 7ba2139f6fc402dfc26f1358649771e83b4059f7 (diff) | |
download | fpGUI-12b6e7ecb1d97f60a72841979e7eb77a2b401f1b.tar.xz |
* Added some sanity checks in TfpgFontBase.TextWidth and Utf8Copy
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/gfx_utf8utils.pas | 37 | ||||
-rw-r--r-- | 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; |