Skip to content

Commit

Permalink
Tried to use a dictionary instead of an array which was too small.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Kassebaum committed Jul 11, 2016
1 parent b979d5f commit ac64391
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 94 deletions.
44 changes: 30 additions & 14 deletions PdfFonts.pas
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
interface

uses
SysUtils, Classes, PdfDoc, PdfTypes;
SysUtils, Classes, Generics.Collections, PdfDoc, PdfTypes;

const
TYPE1_FONT_STR_TABLE: array[0..2] of TPDF_STR_TBL =(
Expand Down Expand Up @@ -491,8 +491,10 @@ TPdfType1Font = class(TPdfFont)
private
FFirstChar: Byte;
FLastChar: Byte;
FArray: array[0..255] of Word;
FArray: TDictionary<Integer, Integer>;
public
constructor Create(AXref: TPdfXref; AName: string); override;
destructor Destroy; override;
procedure SetData(Value: TPdfDictionary); override;
function GetCharWidth(AText: string; APos: integer): integer; override;
end;
Expand Down Expand Up @@ -565,35 +567,49 @@ TPdfScript = class(TPdfType1Font)
implementation

{ TPdfType1Font }

constructor TPdfType1Font.Create(AXref: TPdfXref; AName: string);
begin
inherited Create(AXref, AName);
FArray := TDictionary<Integer, Integer>.Create;
end;

destructor TPdfType1Font.Destroy;
begin
FArray.Free;
inherited Destroy;
end;

function TPdfType1Font.GetCharWidth(AText: string; APos: integer): integer;
var
lDefaultWidth: Word;
begin
result := FArray[ord(AText[APos])];
if not FArray.TryGetValue(APos, Result) then
begin
// initialize char widths array by default value (if missing width parameter
// is defined, use it as default value.)
if Data.PdfNumberByName('MissingWidth') <> nil then
lDefaultWidth := Data.PdfNumberByName('MissingWidth').Value
else
lDefaultWidth := 0;
Result := lDefaultWidth;
end;
end;

procedure TPdfType1Font.SetData(Value: TPdfDictionary);
var
i: integer;
DefaultWidth: Word;
Widths: TPdfArray;
begin
inherited SetData(Value);

// initialize char widths array by default value (if missing width parameter
// is defined, use it as default value.)
if Data.PdfNumberByName('MissingWidth') <> nil then
DefaultWidth := Data.PdfNumberByName('MissingWidth').Value
else
DefaultWidth := 0;
for i := 0 to 255 do
FArray[i] := DefaultWidth;

FFirstChar := Data.PdfNumberByName('FirstChar').Value;
FLastChar := Data.PdfNumberByName('LastChar').Value;

// fill width array with "Widths" table values.
Widths := Data.PdfArrayByName('Widths');
for i := 0 to Widths.ItemCount - 1 do
FArray[i + FFirstChar] := TPdfNumber(Widths.Items[i]).Value;
FArray.Add(i + FFirstChar, TPdfNumber(Widths.Items[i]).Value);
end;

{ FixedWidth }
Expand Down
38 changes: 18 additions & 20 deletions hpp/Win32/Debug/PdfFonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <SysInit.hpp>
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <System.Generics.Collections.hpp>
#include <PdfDoc.hpp>
#include <PdfTypes.hpp>
#include <System.Generics.Defaults.hpp>

//-- user supplied -----------------------------------------------------------

Expand Down Expand Up @@ -93,17 +95,13 @@ class PASCALIMPLEMENTATION TPdfType1Font : public Pdfdoc::TPdfFont
private:
System::Byte FFirstChar;
System::Byte FLastChar;
System::StaticArray<System::Word, 256> FArray;
System::Generics::Collections::TDictionary__2<int,int>* FArray;

public:
__fastcall virtual TPdfType1Font(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
__fastcall virtual ~TPdfType1Font(void);
virtual void __fastcall SetData(Pdftypes::TPdfDictionary* Value);
virtual int __fastcall GetCharWidth(System::UnicodeString AText, int APos);
public:
/* TPdfFont.Create */ inline __fastcall virtual TPdfType1Font(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName) : Pdfdoc::TPdfFont(AXref, AName) { }

public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfType1Font(void) { }

};

#pragma pack(pop)
Expand All @@ -116,7 +114,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidth : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidth(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidth(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidth(void) { }

};

Expand All @@ -130,7 +128,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthBold : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBold(void) { }

};

Expand All @@ -144,7 +142,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthItalic : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthItalic(void) { }

};

Expand All @@ -158,7 +156,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBoldItalic(void) { }

};

Expand All @@ -172,7 +170,7 @@ class PASCALIMPLEMENTATION TPdfArial : public TPdfType1Font
public:
__fastcall virtual TPdfArial(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArial(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArial(void) { }

};

Expand All @@ -186,7 +184,7 @@ class PASCALIMPLEMENTATION TPdfArialBold : public TPdfType1Font
public:
__fastcall virtual TPdfArialBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialBold(void) { }

};

Expand All @@ -200,7 +198,7 @@ class PASCALIMPLEMENTATION TPdfArialItalic : public TPdfType1Font
public:
__fastcall virtual TPdfArialItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialItalic(void) { }

};

Expand All @@ -214,7 +212,7 @@ class PASCALIMPLEMENTATION TPdfArialBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfArialBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialBoldItalic(void) { }

};

Expand All @@ -228,7 +226,7 @@ class PASCALIMPLEMENTATION TPdfTimesRoman : public TPdfType1Font
public:
__fastcall virtual TPdfTimesRoman(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesRoman(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesRoman(void) { }

};

Expand All @@ -242,7 +240,7 @@ class PASCALIMPLEMENTATION TPdfTimesBold : public TPdfType1Font
public:
__fastcall virtual TPdfTimesBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesBold(void) { }

};

Expand All @@ -256,7 +254,7 @@ class PASCALIMPLEMENTATION TPdfTimesItalic : public TPdfType1Font
public:
__fastcall virtual TPdfTimesItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesItalic(void) { }

};

Expand All @@ -270,7 +268,7 @@ class PASCALIMPLEMENTATION TPdfTimesBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfTimesBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesBoldItalic(void) { }

};

Expand All @@ -284,7 +282,7 @@ class PASCALIMPLEMENTATION TPdfScript : public TPdfType1Font
public:
__fastcall virtual TPdfScript(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfScript(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfScript(void) { }

};

Expand Down
38 changes: 18 additions & 20 deletions hpp/Win32/Release/PdfFonts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
#include <SysInit.hpp>
#include <System.SysUtils.hpp>
#include <System.Classes.hpp>
#include <System.Generics.Collections.hpp>
#include <PdfDoc.hpp>
#include <PdfTypes.hpp>
#include <System.Generics.Defaults.hpp>

//-- user supplied -----------------------------------------------------------

Expand Down Expand Up @@ -93,17 +95,13 @@ class PASCALIMPLEMENTATION TPdfType1Font : public Pdfdoc::TPdfFont
private:
System::Byte FFirstChar;
System::Byte FLastChar;
System::StaticArray<System::Word, 256> FArray;
System::Generics::Collections::TDictionary__2<int,int>* FArray;

public:
__fastcall virtual TPdfType1Font(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
__fastcall virtual ~TPdfType1Font(void);
virtual void __fastcall SetData(Pdftypes::TPdfDictionary* Value);
virtual int __fastcall GetCharWidth(System::UnicodeString AText, int APos);
public:
/* TPdfFont.Create */ inline __fastcall virtual TPdfType1Font(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName) : Pdfdoc::TPdfFont(AXref, AName) { }

public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfType1Font(void) { }

};

#pragma pack(pop)
Expand All @@ -116,7 +114,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidth : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidth(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidth(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidth(void) { }

};

Expand All @@ -130,7 +128,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthBold : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBold(void) { }

};

Expand All @@ -144,7 +142,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthItalic : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthItalic(void) { }

};

Expand All @@ -158,7 +156,7 @@ class PASCALIMPLEMENTATION TPdfFixedWidthBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfFixedWidthBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfFixedWidthBoldItalic(void) { }

};

Expand All @@ -172,7 +170,7 @@ class PASCALIMPLEMENTATION TPdfArial : public TPdfType1Font
public:
__fastcall virtual TPdfArial(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArial(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArial(void) { }

};

Expand All @@ -186,7 +184,7 @@ class PASCALIMPLEMENTATION TPdfArialBold : public TPdfType1Font
public:
__fastcall virtual TPdfArialBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialBold(void) { }

};

Expand All @@ -200,7 +198,7 @@ class PASCALIMPLEMENTATION TPdfArialItalic : public TPdfType1Font
public:
__fastcall virtual TPdfArialItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialItalic(void) { }

};

Expand All @@ -214,7 +212,7 @@ class PASCALIMPLEMENTATION TPdfArialBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfArialBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfArialBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfArialBoldItalic(void) { }

};

Expand All @@ -228,7 +226,7 @@ class PASCALIMPLEMENTATION TPdfTimesRoman : public TPdfType1Font
public:
__fastcall virtual TPdfTimesRoman(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesRoman(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesRoman(void) { }

};

Expand All @@ -242,7 +240,7 @@ class PASCALIMPLEMENTATION TPdfTimesBold : public TPdfType1Font
public:
__fastcall virtual TPdfTimesBold(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesBold(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesBold(void) { }

};

Expand All @@ -256,7 +254,7 @@ class PASCALIMPLEMENTATION TPdfTimesItalic : public TPdfType1Font
public:
__fastcall virtual TPdfTimesItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesItalic(void) { }

};

Expand All @@ -270,7 +268,7 @@ class PASCALIMPLEMENTATION TPdfTimesBoldItalic : public TPdfType1Font
public:
__fastcall virtual TPdfTimesBoldItalic(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfTimesBoldItalic(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfTimesBoldItalic(void) { }

};

Expand All @@ -284,7 +282,7 @@ class PASCALIMPLEMENTATION TPdfScript : public TPdfType1Font
public:
__fastcall virtual TPdfScript(Pdfdoc::TPdfXref* AXref, System::UnicodeString AName);
public:
/* TPersistent.Destroy */ inline __fastcall virtual ~TPdfScript(void) { }
/* TPdfType1Font.Destroy */ inline __fastcall virtual ~TPdfScript(void) { }

};

Expand Down
Loading

0 comments on commit ac64391

Please sign in to comment.