From 19fa33a509d3c8d4dceaaa04baab4b8240a8959e Mon Sep 17 00:00:00 2001 From: jesusr Date: Fri, 16 Apr 2010 14:16:38 +0000 Subject: [PATCH] PowerPDF, convert text to winansiencoding before getting textwidth git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@1199 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/powerpdf/PdfDoc.pas | 12 ++++++++-- components/powerpdf/PdfTypes.pas | 33 +++++++++++++++------------ components/powerpdf/pack_powerpdf.lpk | 2 +- 3 files changed, 30 insertions(+), 17 deletions(-) diff --git a/components/powerpdf/PdfDoc.pas b/components/powerpdf/PdfDoc.pas index 8532cf47f..e9b81db74 100644 --- a/components/powerpdf/PdfDoc.pas +++ b/components/powerpdf/PdfDoc.pas @@ -1290,7 +1290,11 @@ var tmpWidth: Single; begin Result := 0; - + {$IFDEF LAZ_POWERPDF} + // for invalid char, any value less than 32 will make FFont.GetCharWidth + // to return a default missing width for that font. + Text := _UTF8ToWinAnsi(Text, #31); + {$ENDIF} // calculate width of specified text from current attributes for i := 1 to Length(Text) do begin @@ -1321,6 +1325,10 @@ begin Result := 0; tmpTotalWidth := 0; + {$IFDEF LAZ_POWERPDF} + Text := _UTF8ToWinAnsi(Text, #31); + {$ENDIF} + // calculate number of charactor contain in the specified width. for i := 1 to Length(Text) do begin @@ -2159,7 +2167,7 @@ end; procedure TPdfCanvas.RoundRect(x, y, width, height, rx, ry: Single); var - hm,wm,h1,w1:single; + h1,w1:single; begin h1 := ry*11/20; w1 := rx*11/20; diff --git a/components/powerpdf/PdfTypes.pas b/components/powerpdf/PdfTypes.pas index f78211199..d3b923c50 100644 --- a/components/powerpdf/PdfTypes.pas +++ b/components/powerpdf/PdfTypes.pas @@ -300,6 +300,7 @@ type {$IFDEF LAZ_POWERPDF} function _UTF8StrToUnicodeHex(const Value:string): string; + function _UTF8ToWinAnsi(const value:string; InvalidChar:char='?'): string; procedure PdfLazRegisterClassAlias(aClass: TPersistentClass; Alias: string); function PdfLazFindClass(aClassName: string):TPersistentClass; {$ENDIF} @@ -1052,7 +1053,7 @@ end; //type // TConvFunc=function(const W:Word): char; -function CP1252(const W: Word): Char; +function CP1252(const W: Word; const InvalidChar: Char): Char; begin case W of $00..$7F,$A0..$FF: result := char(W); @@ -1084,19 +1085,10 @@ begin $017E: result := #$9E; $0178: result := #$9F; else - result:='?'; + result:=InvalidChar; end; end; -function UnicodeToWinAnsi(W: widestring): string; -var - i: Integer; -begin - result := ''; - for i:=1 to length(w) do - result := result + CP1252(word(w[i])); -end; - // _EscapeText function _EscapeText(const Value: string): string; const @@ -1106,15 +1098,17 @@ var i, j: integer; flg: boolean; S: string; - W: widestring; begin // If text contains chars to need escape, replace text using "\". // // TODO: implement UNICODE support in powerpdf. Currently we can't do // any better than converting utf-8 strings to unicode. - W := UTF8Decode(Value); - S := UnicodeToWinAnsi(W); result := ''; + {$IFDEF LAZ_POWERPDF} + S := _UTF8ToWinAnsi(Value); + {$ELSE} + S := Value; + {$ENDIF} for i := 1 to Length(S) do begin flg := false; @@ -1207,6 +1201,17 @@ begin end; end; +function _UTF8ToWinAnsi(const value: string; InvalidChar:char='?'): string; +var + W: widestring; + i: Integer; +begin + W := UTF8Decode(Value); + result := ''; + for i:=1 to length(w) do + result := result + CP1252(word(w[i]), InvalidChar); +end; + procedure PdfLazRegisterClassAlias(aClass: TPersistentClass; Alias: string); begin Classes.RegisterClass(aClass); diff --git a/components/powerpdf/pack_powerpdf.lpk b/components/powerpdf/pack_powerpdf.lpk index 2b0834192..d8a39cb41 100644 --- a/components/powerpdf/pack_powerpdf.lpk +++ b/components/powerpdf/pack_powerpdf.lpk @@ -20,7 +20,7 @@ - +