Browse Source

* Fixes from Graeme Geldenhuys:
TTF: Fixes failing tests due to newly added font to the test suite.
Font count went from 4 to 5.
TTF: fix bug where Postscript.FontAngle had the wrong data type.
Updated the unit tests, but extending them with Liberation Sans Italic font.

git-svn-id: trunk@34060 -

michael 9 năm trước cách đây
mục cha
commit
a7ddadcb37

+ 7 - 4
packages/fcl-pdf/src/fpparsettf.pp

@@ -34,6 +34,9 @@ type
   TSmallintArray = Packed Array of Int16;
   TWordArray = Packed Array of UInt16;
 
+  { Signed Fixed 16.16 Float }
+  TF16Dot16 = type Int32;
+
   TFixedVersionRec = packed record
     case Integer of
       0:  (Minor, Major: Word);
@@ -65,7 +68,7 @@ type
 Type
   TPostScript = Packed Record
     Format : TFixedVersionRec;
-    ItalicAngle : LongWord;
+    ItalicAngle : TF16Dot16;
     UnderlinePosition : SmallInt;
     underlineThickness : SmallInt;
     isFixedPitch : Cardinal;
@@ -288,7 +291,7 @@ Type
     Function CapHeight: SmallInt;
     { Returns the glyph advance width, based on the AIndex (glyph index) value. The result is in font units. }
     function GetAdvanceWidth(AIndex: word): word;
-    function ItalicAngle: LongWord;
+    function ItalicAngle: single;
     { max glyph bounding box values - as space separated values }
     function BBox: string;
     property MissingWidth: Integer read GetMissingWidth;
@@ -898,9 +901,9 @@ begin
   Result := Widths[AIndex].AdvanceWidth;
 end;
 
-function TTFFileInfo.ItalicAngle: LongWord;
+function TTFFileInfo.ItalicAngle: single;
 begin
-  Result := FPostScript.ItalicAngle;
+  Result := FPostScript.ItalicAngle / 65536.0;
 end;
 
 function TTFFileInfo.BBox: string;

+ 7 - 7
packages/fcl-pdf/src/fppdf.pp

@@ -2188,7 +2188,6 @@ begin
 end;
 
 procedure TPDFImageItem.CreateStreamedData(AUseCompression: Boolean);
-
 Var
   X,Y : Integer;
   C : TFPColor;
@@ -2237,10 +2236,8 @@ begin
 end;
 
 function TPDFImageItem.WriteImageStream(AStream: TStream): int64;
-
 var
   Img : TBytes;
-
 begin
   TPDFObject.WriteString(CRLF+'stream'+CRLF,AStream);
   Img:=StreamedData;
@@ -2259,7 +2256,12 @@ begin
     Result := False;
     exit;
   end;
-  Result := True;
+
+  { if dimensions don't match, we know we can exit early }
+  Result := (Image.Width = AImage.Width) and (Image.Height = AImage.Height);
+  if not Result then
+    Exit;
+
   for x := 0 to Image.Width-1 do
     for y := 0 to Image.Height-1 do
       if Image.Colors[x, y] <> AImage.Colors[x, y] then
@@ -2269,8 +2271,6 @@ begin
       end;
 end;
 
-
-
 { TPDFImages }
 
 function TPDFImages.GetI(AIndex : Integer): TPDFImageItem;
@@ -3639,7 +3639,7 @@ begin
   Arr:=CreateArray;
   FDict.AddElement('FontBBox',Arr);
   Arr.AddIntArray(Fonts[EmbeddedFontNum].FTrueTypeFile.BBox);
-  FDict.AddInteger('ItalicAngle',Fonts[EmbeddedFontNum].FTrueTypeFile.ItalicAngle);
+  FDict.AddInteger('ItalicAngle', trunc(Fonts[EmbeddedFontNum].FTrueTypeFile.ItalicAngle));
   FDict.AddInteger('StemV', Fonts[EmbeddedFontNum].FTrueTypeFile.StemV);
   FDict.AddInteger('MissingWidth', Fonts[EmbeddedFontNum].FTrueTypeFile.MissingWidth);
   CreateFontFileEntry(EmbeddedFontNum);

+ 28 - 0
packages/fcl-pdf/tests/fpparsettf_test.pas

@@ -196,6 +196,15 @@ type
   end;
 
 
+  TTestLiberationItalicFont = class(TBaseTestParseTTF)
+  protected
+    procedure SetUp; override;
+  published
+    { PostScript data structure }
+    procedure TestPostScript_ItalicAngle;
+  end;
+
+
   TTestFreeSansFont = class(TBaseTestParseTTF)
   protected
     procedure SetUp; override;
@@ -361,6 +370,7 @@ uses
 const
   cFont1 = 'fonts' + PathDelim + 'LiberationSans-Regular.ttf';
   cFont2 = 'fonts' + PathDelim + 'FreeSans.ttf';
+  cFont3 = 'fonts' + PathDelim + 'LiberationSans-Italic.ttf';
 
 { TTestEmptyParseTTF }
 
@@ -1150,6 +1160,23 @@ begin
   AssertEquals('Failed on 12', 1139, FI.GetAdvanceWidth(20));  // '1'
 end;
 
+{ TTestLiberationItalicFont }
+
+procedure TTestLiberationItalicFont.SetUp;
+begin
+  inherited SetUp;
+  AssertTrue('Failed to find TTF font file <' + cFont3 + '>' + LineEnding +
+    'You can download it from [https://fedorahosted.org/releases/l/i/liberation-fonts/liberation-fonts-ttf-2.00.1.tar.gz]',
+    FileExists(cFont3) = True);
+  LoadFont(cFont3);
+end;
+
+procedure TTestLiberationItalicFont.TestPostScript_ItalicAngle;
+begin
+  AssertEquals('Failed on 1', -12.0, FI.PostScript.ItalicAngle / 65536.0);
+  AssertEquals('Failed on 2', -12.0, FI.ItalicAngle);
+end;
+
 { TTestFreeSansFont }
 
 procedure TTestFreeSansFont.SetUp;
@@ -1900,6 +1927,7 @@ initialization
   RegisterTest({$ifdef fptest}'fpParseTTF',{$endif}TTestEmptyParseTTF{$ifdef fptest}.Suite{$endif});
   RegisterTest({$ifdef fptest}'fpParseTTF',{$endif}TTestLiberationFont{$ifdef fptest}.Suite{$endif});
   RegisterTest({$ifdef fptest}'fpParseTTF',{$endif}TTestFreeSansFont{$ifdef fptest}.Suite{$endif});
+  RegisterTest({$ifdef fptest}'fpParseTTF',{$endif}TTestLiberationItalicFont{$ifdef fptest}.Suite{$endif});
 
 end.
 

+ 11 - 8
packages/fcl-pdf/tests/fpttf_test.pas

@@ -59,8 +59,11 @@ implementation
 uses
   fpparsettf;
 
+const
+  cFontCount = 5;
+
 resourcestring
-  cErrFontCountWrong =   ' - make sure you only have the 4 test fonts in the "fonts" directory.';
+  cErrFontCountWrong =   ' - make sure you only have the 5 test fonts in the "fonts" directory.';
 
 { TFPFontCacheItemTest }
 
@@ -192,7 +195,7 @@ begin
   FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
   AssertEquals('Failed on 2', 0, FC.Count);
   FC.BuildFontCache;
-  AssertEquals('Failed on 3' + cErrFontCountWrong, 4, FC.Count);
+  AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
 end;
 
 procedure TFPFontCacheListTest.TestBuildFontCache;
@@ -211,7 +214,7 @@ begin
   FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
   AssertEquals('Failed on 4', 0, FC.Count);
   FC.BuildFontCache;
-  AssertEquals('Failed on 5' + cErrFontCountWrong, 4, FC.Count);
+  AssertEquals('Failed on 5' + cErrFontCountWrong, cFontCount, FC.Count);
 end;
 
 procedure TFPFontCacheListTest.TestBuildFontCache_tests_for_bug;
@@ -227,7 +230,7 @@ begin
   AssertEquals('Failed on 1', 0, FC.Count);
   FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
   FC.BuildFontCache;
-  AssertEquals('Failed on 2', 4, FC.Count);
+  AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
   FC.Clear;
   AssertEquals('Failed on 3', 0, FC.Count);
 end;
@@ -242,7 +245,7 @@ begin
   AssertTrue('Failed on 2', lCI = nil);
   FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
   FC.BuildFontCache;
-  AssertEquals('Failed on 3' + cErrFontCountWrong, 4, FC.Count);
+  AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
   lCI := FC.Find('Ubuntu');
   AssertTrue('Failed on 4', Assigned(lCI));
 
@@ -272,7 +275,7 @@ begin
   AssertTrue('Failed on 2', lCI = nil);
   FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
   FC.BuildFontCache;
-  AssertEquals('Failed on 3' + cErrFontCountWrong, 4, FC.Count);
+  AssertEquals('Failed on 3' + cErrFontCountWrong, cFontCount, FC.Count);
   lCI := FC.Find('Ubuntu');
   AssertTrue('Failed on 4', Assigned(lCI));
 
@@ -301,9 +304,9 @@ begin
     AssertEquals('Failed on 1', 0, FC.Count);
     FC.SearchPath.Add(ExtractFilePath(ParamStr(0)) + 'fonts');
     FC.BuildFontCache;
-    AssertEquals('Failed on 2', 4, FC.Count);
+    AssertEquals('Failed on 2' + cErrFontCountWrong, cFontCount, FC.Count);
     FC.AssignFontList(sl);
-    AssertEquals('Failed on 3', 4, sl.Count);
+    AssertEquals('Failed on 3', cFontCount, sl.Count);
   finally
     sl.Free;
   end;