Browse Source

Fppdf fix encoding for standard fonts. Issue 39554

Ondrej Pokorny 3 years ago
parent
commit
f51c0daeb4
1 changed files with 29 additions and 16 deletions
  1. 29 16
      packages/fcl-pdf/src/fppdf.pp

+ 29 - 16
packages/fcl-pdf/src/fppdf.pp

@@ -1276,7 +1276,6 @@ type
   TTTFFriendClass = class(TTFFileInfo)
   TTTFFriendClass = class(TTFFileInfo)
   end;
   end;
 
 
-
 const
 const
   cInchToMM = 25.4;
   cInchToMM = 25.4;
   cInchToCM = 2.54;
   cInchToCM = 2.54;
@@ -1519,9 +1518,13 @@ procedure TXMPStream.Write(const AStream: TStream);
 
 
   procedure Add(const Tag, Value: string);
   procedure Add(const Tag, Value: string);
   begin
   begin
-    WriteString('<'+Tag+'>', AStream);
+    WriteString('<', AStream);
+    WriteString(Tag, AStream);
+    WriteString('>', AStream);
     WriteString(Value, AStream);
     WriteString(Value, AStream);
-    WriteString('</'+Tag+'>'+CRLF, AStream);
+    WriteString('</', AStream);
+    WriteString(Tag, AStream);
+    WriteString('>'+CRLF, AStream);
   end;
   end;
 
 
   function DateToISO8601Date(t: TDateTime): string;
   function DateToISO8601Date(t: TDateTime): string;
@@ -2930,14 +2933,13 @@ begin
 end;
 end;
 
 
 class procedure TPDFObject.WriteString(const AValue: RawByteString; AStream: TStream);
 class procedure TPDFObject.WriteString(const AValue: RawByteString; AStream: TStream);
-
-Var
-  L : Integer;
+var
+  L: SizeInt;
 
 
 begin
 begin
   L:=Length(AValue);
   L:=Length(AValue);
   if L>0 then
   if L>0 then
-    AStream.Write(AValue[1],L);
+    AStream.WriteBuffer(AValue[1],L);
 end;
 end;
 
 
 // Font=Name-Size:x:y
 // Font=Name-Size:x:y
@@ -3416,10 +3418,11 @@ begin
       WriteString('/Length1', AStream)
       WriteString('/Length1', AStream)
     else
     else
     begin
     begin
+      WriteString('/', AStream);
       if FMustEscape then
       if FMustEscape then
-        WriteString('/'+ConvertCharsToHex, AStream)
+        WriteString(ConvertCharsToHex, AStream)
       else
       else
-        WriteString('/'+FName, AStream);
+        WriteString(FName, AStream);
     end;
     end;
 end;
 end;
 
 
@@ -3469,10 +3472,14 @@ end;
 
 
 procedure TPDFString.Write(const AStream: TStream);
 procedure TPDFString.Write(const AStream: TStream);
 var
 var
-  s: AnsiString;
+  s: RawByteString;
 begin
 begin
-  s := Utf8ToAnsi(FValue);
-  WriteString('('+s+')', AStream);
+  // TPDFText uses hardcoded WinAnsiEncoding (=win-1252), we have to convert to 1252 as well and not to ansi (that is not always 1252)
+  s := FValue;
+  SetCodePage(s, 1252);
+  WriteString('(', AStream);
+  WriteString(s, AStream);
+  WriteString(')', AStream);
 end;
 end;
 
 
 constructor TPDFString.Create(Const ADocument : TPDFDocument; const AValue: string);
 constructor TPDFString.Create(Const ADocument : TPDFDocument; const AValue: string);
@@ -3526,7 +3533,10 @@ begin
     else
     else
       s:=fValue;
       s:=fValue;
   end;
   end;
-  WriteString('('+s+')', AStream);
+
+  WriteString('(', AStream);
+  WriteString(s, AStream);
+  WriteString(')', AStream);
 end;
 end;
 
 
 
 
@@ -3543,7 +3553,9 @@ end;
 
 
 procedure TPDFUTF8String.Write(const AStream: TStream);
 procedure TPDFUTF8String.Write(const AStream: TStream);
 begin
 begin
-  WriteString('<'+RemapedText+'>', AStream);
+  WriteString('<', AStream);
+  WriteString(RemapedText, AStream);
+  WriteString('>', AStream);
 end;
 end;
 
 
 constructor TPDFUTF8String.Create(const ADocument: TPDFDocument; const AValue: UTF8String; const AFontIndex: integer);
 constructor TPDFUTF8String.Create(const ADocument: TPDFDocument; const AValue: UTF8String; const AFontIndex: integer);
@@ -3557,9 +3569,10 @@ end;
 
 
 procedure TPDFFreeFormString.Write(const AStream: TStream);
 procedure TPDFFreeFormString.Write(const AStream: TStream);
 var
 var
-  s: AnsiString;
+  s: RawByteString;
 begin
 begin
-  s := Utf8ToAnsi(FValue);
+  s := FValue;
+  SetCodePage(s, 1252);
   WriteString(s, AStream);
   WriteString(s, AStream);
 end;
 end;