Browse Source

* Some fixes to FPPDF:
- fppdf: Fixes support for FPC 2.6.4
- fppdf: Fixes bug where PDFName values were not escaped.
Spaces for example are not allowed in PDFName elements.
- fppdf: Removes usage of TFriendTTFileInfo class. Not needed any more.

git-svn-id: trunk@33495 -

michael 9 years ago
parent
commit
e662e49cf9
2 changed files with 21 additions and 17 deletions
  1. 19 11
      packages/fcl-pdf/src/fppdf.pp
  2. 2 6
      packages/fcl-pdf/src/fpttf.pp

+ 19 - 11
packages/fcl-pdf/src/fppdf.pp

@@ -164,12 +164,14 @@ type
   TPDFName = class(TPDFObject)
   private
     FName : string;
+    FMustEscape: boolean;
     function  ConvertCharsToHex: string;
   protected
     procedure Write(const AStream: TStream); override;
   public
-    constructor Create(Const ADocument : TPDFDocument; const AValue: string); overload;
+    constructor Create(Const ADocument : TPDFDocument; const AValue: string; const AMustEscape: boolean = True); overload;
     property Name : String read FName;
+    property MustScape: boolean read FMustEscape;
   end;
 
 
@@ -431,7 +433,7 @@ type
     function GetV(AIndex : Integer): TPDFObject;
   protected
     procedure AddElement(const AKey: string; const AValue: TPDFObject);
-    procedure AddName(const AKey,AName : String);
+    procedure AddName(const AKey,AName : String; const AMustEscape: boolean = True);
     procedure AddInteger(const AKey : String; AInteger : Integer);
     procedure AddReference(const AKey : String; AReference : Integer);
     procedure AddString(const AKey, AString : String);
@@ -840,7 +842,7 @@ type
     Function CreateInteger(AValue : Integer) : TPDFInteger;
     Function CreateReference(AValue : Integer) : TPDFReference;
     Function CreateLineStyle(APenStyle: TPDFPenStyle) : TPDFLineStyle;
-    Function CreateName(AValue : String) : TPDFName;
+    Function CreateName(AValue : String; const AMustEscape: boolean = True) : TPDFName;
     Function CreateStream(OwnsObjects : Boolean = True) : TPDFStream;
     Function CreateDictionary : TPDFDictionary;
     Function CreateXRef : TPDFXRef;
@@ -2150,7 +2152,7 @@ function TPDFImages.AddFromFile(const AFileName: String; KeepImage: Boolean): In
         while (r >= 0) do
           begin
           Result := ImageReader[TypeNames[r]];
-          if (pos(s,Extensions[TypeNames[r]]+';') <> 0) then
+          if (pos(s,{$if (FPC_FULLVERSION = 20604)}Extentions{$else}Extensions{$endif}[TypeNames[r]]+';') <> 0) then
             Exit;
           dec (r);
           end;
@@ -2307,13 +2309,19 @@ begin
     if Pos('Length1', FName) > 0 then
       WriteString('/Length1', AStream)
     else
-      WriteString('/'+FName {ConvertCharsToHex}, AStream);
+    begin
+      if FMustEscape then
+        WriteString('/'+ConvertCharsToHex, AStream)
+      else
+        WriteString('/'+FName, AStream);
+    end;
 end;
 
-constructor TPDFName.Create(const ADocument: TPDFDocument; const AValue: string);
+constructor TPDFName.Create(const ADocument: TPDFDocument; const AValue: string; const AMustEscape: boolean = True);
 begin
   inherited Create(ADocument);
   FName:=AValue;
+  FMustEscape := AMustEscape;
 end;
 
 function TPDFName.ConvertCharsToHex: string;
@@ -2782,9 +2790,9 @@ begin
   FElements.Add(DicElement);
 end;
 
-procedure TPDFDictionary.AddName(const AKey, AName: String);
+procedure TPDFDictionary.AddName(const AKey, AName: String; const AMustEscape: boolean = True);
 begin
-  AddElement(AKey,Document.CreateName(AName));
+  AddElement(AKey,Document.CreateName(AName, AMustEscape));
 end;
 
 procedure TPDFDictionary.AddInteger(const AKey: String; AInteger: Integer);
@@ -3720,7 +3728,7 @@ begin
         ADict:=GlobalXRefByName('Catalog').Dict;
         Arr:=ADict.ValueByName('OpenAction') as TPDFArray;
         Arr.AddItem(CreateReference(GLobalXRefCount-1));
-        Arr.AddItem(CreateName('XYZ null null '+TPDFObject.FloatStr(StrToInt(FZoomValue) / 100)));
+        Arr.AddItem(CreateName('XYZ null null '+TPDFObject.FloatStr(StrToInt(FZoomValue) / 100), False));
         end;
       PageNum:=CreateContentsEntry; // pagenum = object number in the pdf file
       CreatePageStream(S.Pages[k],PageNum);
@@ -3873,9 +3881,9 @@ begin
   Result:=TPDFLineStyle.Create(Self,APenStyle,0)
 end;
 
-function TPDFDocument.CreateName(AValue: String): TPDFName;
+function TPDFDocument.CreateName(AValue: String; const AMustEscape: boolean = True): TPDFName;
 begin
-  Result:=TPDFName.Create(Self,AValue);
+  Result:=TPDFName.Create(Self,AValue,AMustEscape);
 end;
 
 function TPDFDocument.CreateStream(OwnsObjects : Boolean = True): TPDFStream;

+ 2 - 6
packages/fcl-pdf/src/fpttf.pp

@@ -113,10 +113,6 @@ resourcestring
   rsNoFontFileName = 'The FileName property is empty, so we can''t load font data.';
   rsCharAboveWord = 'TextWidth doesn''t support characters higher then High(Word) - %d.';
 
-type
-  { so we can get access to protected methods }
-  TFriendTTFFileInfo = class(TTFFileInfo);
-
 var
   uFontCacheList: TFPFontCacheList;
 
@@ -252,7 +248,7 @@ function TFPFontCacheItem.TextWidth(AStr: utf8string; APointSize: single): singl
     550 * 18 * 72 / ( 72 * 2048 ) = 4.83
 }
 var
-  lFntInfo: TFriendTTFFileInfo;
+  lFntInfo: TTFFileInfo;
   i: integer;
   lWidth: integer;
   lGIndex: integer;
@@ -266,7 +262,7 @@ begin
   if Length(AStr) = 0 then
     Exit;
 
-  lFntInfo := TFriendTTFFileInfo(GetFontData);
+  lFntInfo := GetFontData;
   if not Assigned(lFntInfo) then
     Exit;