mORMot and Open Source friends
Check-in [6822e8c67f]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:6822e8c67f135cbe17c03e52d42d4da573b03741
Date: 2013-06-19 14:01:30
User: abouchez
Comment:implemented 40 bit and 128 bit security - see TPdfEncryption.New()
Tags And Properties
Context
2013-06-20
06:17
[383bc8f6c0] added TGdiPages.ExportPDFEncryptionLevel/User/OwnerPassword/Permissions properties to optionally export report as 40 bit or 128 bit encrypted pdf (user: abouchez, tags: trunk)
2013-06-19
14:01
[6822e8c67f] implemented 40 bit and 128 bit security - see TPdfEncryption.New() (user: abouchez, tags: trunk)
13:43
[2411745168] fixed RC4 encryption (user: abouchez, tags: trunk)
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to SynPdf.pas.

182
183
184
185
186
187
188

189
190
191
192
193
194
195
...
231
232
233
234
235
236
237
238
239
240
241
242







243
244
245
246
247
248
249
...
402
403
404
405
406
407
408



409
410
411
412
413
414
415
...
562
563
564
565
566
567
568

569
570
571
572
573
574
575
576
577
578
579

580
581
582
583

584
585
586
587
588
589
590



591















592
593
594
595
596



597

598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621

622
623
624
625
626
627
628
...
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
...
699
700
701
702
703
704
705




706
707
708
709
710
711
712
713
...
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
...
859
860
861
862
863
864
865












866
867
868
869
870
871
872
....
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
....
1183
1184
1185
1186
1187
1188
1189

1190
1191
1192
1193
1194



1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207

1208
1209
1210
1211
1212
1213
1214
....
2521
2522
2523
2524
2525
2526
2527





























2528
2529
2530
2531
2532
2533
2534
....
2917
2918
2919
2920
2921
2922
2923







2924
2925
2926
2927
2928
2929
2930
....
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
....
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
....
3099
3100
3101
3102
3103
3104
3105










3106
3107
3108
3109
3110
3111
3112
....
3129
3130
3131
3132
3133
3134
3135

3136
3137
3138
3139
3140
3141
3142
....
3271
3272
3273
3274
3275
3276
3277

3278
3279
3280
3281
3282
3283
3284
....
3451
3452
3453
3454
3455
3456
3457

3458

3459
3460
3461
3462
3463
3464
3465
....
3474
3475
3476
3477
3478
3479
3480





3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492





3493
3494
3495
3496
3497
3498
3499
....
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
....
3521
3522
3523
3524
3525
3526
3527

3528
3529
3530
3531
3532
3533
3534
....
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
....
3804
3805
3806
3807
3808
3809
3810





















3811

3812
3813
3814


3815
3816
3817
3818
3819




3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
....
4022
4023
4024
4025
4026
4027
4028



4029








4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041

4042
4043
4044
4045
4046
4047
4048
....
4371
4372
4373
4374
4375
4376
4377
4378
4379

4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
....
4637
4638
4639
4640
4641
4642
4643



4644
4645
4646
4647
4648
4649
4650
....
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
....
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
....
5024
5025
5026
5027
5028
5029
5030




5031





5032
5033
5034
5035
5036
5037
5038
....
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072




5073
5074
5075
5076
5077
5078
5079
....
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
....
5154
5155
5156
5157
5158
5159
5160


5161
5162
5163
5164
5165
5166
5167
....
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
....
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171




7172
7173
7174
7175
7176
7177
7178
7179
7180
....
9591
9592
9593
9594
9595
9596
9597



9598
9599
9600
9601
9602
9603
9604
....
9605
9606
9607
9608
9609
9610
9611
9612






9613
9614
9615
9616
9617










9618

















9619
9620



















9621
















































9622
9623
9624
9625

9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
  - SynPdf unit can now link to standard ZLib.pas unit if you want to use SynPdf
    stand-alone and do not need SynZip.pas + deflate.obj + trees.obj
    (but SQLite3Commons.pas main unit of mORMot will need SynZip, so it is
    enabled by default for use within the framework)

  Version 1.18
  - major speed up of TPdfCanvas.RenderMetaFile() by caching printer resolution

  - introducing TPdfDocument.SaveToStreamDirectBegin/PageFlush/End methods,
    able to render all page content directly to the destination stream/file,
    therefore reducing the memory use to a minimal value for huge content - used
    e.g. in TPdfDocumentGDI.SaveToStream() and TGDIPages.ExportPDFStream()
  - TPdfDocumentGDI will now compress (via our SynLZ algorithm) all its page
    content (TMetaFile) for efficiency
  - therefore, TPdfDocumentGDI will use much less resource and memory with no
................................................................................
    produce bigger pdf file size, but will fulfill feature request [7d6a3a3f0f]  

}


{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64

{.$define USE_PDFSECURITY}
{ - if defined, the TPdfDocument*.Create() constructor will have an additional
  AEncryption: TPdfSecurity parameter able to create secured PDF files
  - this feature will need the SynCrypto unit for MD5 and RC4 algorithms }
  







{$define USE_UNISCRIBE}
{ - if defined, the PDF engine will use the Windows Uniscribe API to
  render Ordering and Shaping of the text (usefull for Hebrew, Arabic and
  some Asiatic languages)
  - this feature need the TPdfDocument.UseUniscribe property to be forced to true
  according to the language of the text you want to render
  - can be undefined to safe some KB if you're sure you won't need it }
................................................................................
  /// the PDF library use internaly AnsiString text encoding
  // - the corresponding charset is the current system charset, or the one
  // supplied as a parameter to TPdfDocument.Create
  PDFString = AnsiString;

  /// a PDF date, encoded as 'D:20100414113241'
  TPdfDate = PDFString;




  /// PDF exception, raised when an invalid value is given to a constructor
  EPdfInvalidValue = class(Exception);

  /// PDF exception, raised when an invalid operation is trigerred
  EPdfInvalidOperation = class(Exception);

................................................................................
  // - Authoring Comments and Form Fields: If this is disabled, adding,
  // modifying, or deleting comments and form fields is prohibited. Form field
  // filling is allowed.
  // - Form Field Fill-in or Signing: If this is enabled, users can sign and
  // fill in forms, but not create form fields.
  // - Document Assembly: If this is disabled, inserting, deleting or rotating
  // pages, or creating bookmarks and thumbnails is prohibited.

  TPdfEncryptionPermission = (epPrinting, epGeneralEditing, epContentCopy,
    epAuthoringComment, epPrintingHighResolution, epFillingForms,
    epContentExtraction, epDocumentAssembly);

  /// set of restrictions on PDF document operations
  TPdfEncryptionPermissions = set of TPdfEncryptionPermission;

  /// abstract class to handle PDF security
  TPdfEncryption = class
  protected
    fLevel: TPdfEncryptionLevel;

    fInternalKey: TByteDynArray;
    fPermissions: TPdfEncryptionPermissions;
    fUserPassword: string;
    fOwnerPassword: string;

    procedure EncodeBuffer(const BufIn; var BufOut; Count: cardinal;
      ObjectNumber,GenerationNumber: integer); virtual; abstract;
  public
    /// initialize the internal structures with the proper classes
    // - do not call this method directly, but class function TPdfEncryption.New()
    constructor Create(aLevel: TPdfEncryptionLevel; aPermissions: TPdfEncryptionPermissions;
      const aUserPassword, aOwnerPassword: string); virtual;



    /// will create the expected TPdfEncryption instance, depending on aLevel















    class function New(aLevel: TPdfEncryptionLevel;
      const aUserPassword, aOwnerPassword: string;
      aPermissions: TPdfEncryptionPermissions): TPdfEncryption;
  end;




  /// handle PDF security with RC4+MD5 scheme in 40-bit and 128-bit

  TPdfEncryptionRC4MD5 = class(TPdfEncryption)
  protected
    fLastObjectNumber: integer;
    fLastGenerationNumber: Integer;
    fLastRC4Key: TRC4InternalKey;
    procedure EncodeBuffer(const BufIn; var BufOut; Count: cardinal;
      ObjectNumber,GenerationNumber: integer); override;
  public
    /// initialize the internal structures
    // - allowed aLevel here are only elRC4_40 and elRC4_128
    // - do not call this method directly, but class function TPdfEncryption.New()
    constructor Create(aLevel: TPdfEncryptionLevel; aPermissions: TPdfEncryptionPermissions;
      const aUserPassword, aOwnerPassword: string); override;
  end;
{$endif}

  /// buffered writer class, specialized for PDF encoding
  TPdfWrite = class
  protected
    B, BEnd, BEnd4: PAnsiChar;
    fDestStream: TStream;
    fDestStreamPosition: integer;
    fCodePage: integer;
    fAddGlyphFont: (fNone, fMain, fFallBack);

    Tmp: array[0..511] of AnsiChar;
    /// internal Ansi->Unicode conversion, using the CodePage used in Create()
    // - caller must release the returned memory via FreeMem()
    function ToWideChar(const Ansi: PDFString; out DLen: Integer): PWideChar;
{$ifdef USE_UNISCRIBE}
    /// internal method using the Windows Uniscribe API
    // - return FALSE if PW was not appened to the PDF content, TRUE if OK
................................................................................
      NextLine: boolean; Canvas: TPdfCanvas);
    /// internal methods handling font fall-back
    procedure AddGlyphFromChar(Char: WideChar; Canvas: TPdfCanvas;
      TTF: TPdfFontTrueType; NextLine: PBoolean);
    procedure AddGlyphFlush(Canvas: TPdfCanvas; TTF: TPdfFontTrueType; NextLine: PBoolean);
  public
    /// create the buffered writer, for a specified destination stream
    constructor Create(DestStream: TStream; CodePage: integer);
    /// add a character to the buffer
    function Add(c: AnsiChar): TPdfWrite; overload; {$ifdef HASINLINE}inline;{$endif}
    /// add an integer numerical value to the buffer
    function Add(Value: Integer): TPdfWrite; overload;
    /// add an integer numerical value to the buffer
    // - add a trailing space
    function AddWithSpace(Value: Integer): TPdfWrite; overload;
................................................................................
    function AddUnicodeHexText(PW: PWideChar; NextLine: boolean;
      Canvas: TPdfCanvas): TPdfWrite;
    /// write some Unicode text, encoded as Glyphs indexes, corresponding
    // to the current font
    function AddGlyphs(Glyphs: PWord; GlyphsCount: integer; Canvas: TPdfCanvas): TPdfWrite;
    /// add some WinAnsi text as PDF text
    // - used by TPdfText object




    function AddEscape(Text: PAnsiChar): TPdfWrite;
    /// add some WinAnsi text as PDF text
    // - used by TPdfCanvas.ShowText method for WinAnsi text
    function AddEscapeText(Text: PAnsiChar; Font: TPdfFont): TPdfWrite;
    /// add some PDF /property value
    function AddEscapeName(Text: PAnsiChar): TPdfWrite;
    /// add a PDF color, from its TColorRef RGB value
    function AddColorStr(Color: TColorRef): TPdfWrite;
................................................................................
  /// a PDF object, storing a textual value
  // - the value is specified as a PDFString
  // - this object is stored as '(escapedValue)'
  // - in case of MBCS, conversion is made into Unicode before writing, and
  // stored as '<FEFFHexUnicodeEncodedValue>'
  TPdfText = class(TPdfObject)
  private
    FValue: PDFString;
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  public
    constructor Create(const AValue: PDFString); reintroduce;
    property Value: PDFString read FValue write FValue;
  end;

  /// a PDF object, storing a textual value
  // - the value is specified as an UTF-8 encoded string
  // - this object is stored as '(escapedValue)'
  // - in case characters with ANSI code higher than 8 Bits, conversion is made
  // into Unicode before writing, and '<FEFFHexUnicodeEncodedValue>'
................................................................................
  TPdfRawText = class(TPdfText)
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  public
    /// simple creator, replacing every % in Fmt by the corresponding Args[]
    constructor CreateFmt(Fmt: PAnsiChar; const Args: array of Integer);
  end;













  /// a PDF object, storing a PDF name
  // - this object is stored as '/Value'
  TPdfName = class(TPdfText)
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  end;
................................................................................
  /// array used to store a TPdfImage hash
  // - uses 4 hash codes, created with 4 diverse algorithms, in order to avoid
  // false positives
  TPdfImageHash = array[0..3] of cardinal;

  /// the main class of the PDF engine, processing the whole PDF document
  TPdfDocument = class(TObject)
  private
    FRoot: TPdfCatalog;
    FCurrentPages: TPdfDictionary;
    FOutputIntents: TPdfArray;
    FMetaData: TPdfStream;
    FCanvas: TPdfCanvas;
    FHeader: TPdfRawText;
    FTrailer: TPdfTrailer;
    FXref: TPdfXref;
    FInfo: TPdfInfo;
    FFontList: TList;
    FObjectList: TList;
    FOutlineRoot: TPdfOutlineRoot;
    FXObjectList: TPdfArray;
................................................................................
    fUseFontFallBack: boolean;
    fFontFallBackIndex: integer;
    /// a list of Bookmark text keys, associated to a TPdfDest object
    fBookMarks: TRawUTF8List;
    fMissingBookmarks: TRawUTF8List;
    /// internal temporary variable - used by CreateOutline
    fLastOutline: TPdfOutlineEntry;

    fPDFA1: boolean;
    fSaveToStreamWriter: TPdfWrite;
{$ifdef USE_PDFSECURITY}
    fEncryption: TPdfEncryption;
    fFileID: TMD5Digest;



{$endif}
    function GetInfo: TPdfInfo;     {$ifdef HASINLINE}inline;{$endif}
    function GetOutlineRoot: TPdfOutlineRoot; {$ifdef HASINLINE}inline;{$endif}
    procedure SetStandardFontsReplace(const Value: boolean); {$ifdef HASINLINE}inline;{$endif}
    function GetEmbeddedTTFIgnore: TRawUTF8List;
    procedure SetDefaultPaperSize(const Value: TPDFPaperSize);
    procedure SetDefaultPageHeight(const Value: cardinal);
    procedure SetDefaultPageWidth(const Value: cardinal);
    procedure SetPDFA1(const Value: boolean);
    function GetDefaultPageLandscape: boolean;
    procedure SetDefaultPageLandscape(const Value: boolean);
    procedure SetFontFallBackName(const Value: string);
    function GetFontFallBackName: string;

  protected
    /// can be useful in descendant objects in other units
    fTPdfPageClass: TPdfPageClass;
    procedure RaiseInvalidOperation;
    procedure CreateInfo;
    /// get the PostScript Name of a TrueType Font
    // - use the Naming Table ('name') of the TTF content if not 7 bit ascii
................................................................................
// is created from the specified numerical level (0=root)
procedure GDICommentOutline(MetaHandle: HDC; const aTitle: RawUTF8; aLevel: Integer);

/// append a EMR_GDICOMMENT message for creating a Link into a specified bookmark
procedure GDICommentLink(MetaHandle: HDC; const aBookmarkName: RawUTF8; const aRect: TRect);
































(*
    Windows Uniscribe APIs

    Uniscribe is a set of APIs that allow a high degree of control for fine
    typography and for processing complex scripts
    - see http://msdn.microsoft.com/en-us/library/dd374091(v=VS.85).aspx
................................................................................
constructor TPdfObject.Create;
begin
  FObjectNumber := -1;
end;

procedure TPdfObject.InternalWriteTo(var W: TPdfWrite);
begin







end;

procedure TPdfObject.SetObjectNumber(Value: integer);
begin
  FObjectNumber := Value;
  if Value > 0 then
    FObjectType := otIndirectObject else
................................................................................

{ TPdfText }

procedure TPdfText.InternalWriteTo(var W: TPdfWrite);
begin
  // if the value has multibyte character, convert the value to hex unicode.
  // otherwise, escape characters.
  if SysLocale.FarEast and _HasMultiByteString(pointer(FValue)) then
    W.Add('<FEFF').AddToUnicodeHex(FValue).Add('>') else
    W.Add('(').AddEscape(pointer(FValue)).Add(')');
end;

constructor TPdfText.Create(const AValue: PDFString);
begin
  inherited Create;
  Value := AValue;
end;


{ TPdfTextUTF8 }
................................................................................

procedure TPdfTextUTF8.InternalWriteTo(var W: TPdfWrite);
var Len: Integer;
begin
  // if the value has multibyte character, convert the value to hex unicode.
  // otherwise, escape characters
  if IsWinAnsiU8Bit(Pointer(FValue)) then
    W.Add('(').AddEscape(pointer(Utf8ToWinAnsi(FValue))).Add(')') else
    W.Add('<FEFF').AddUnicodeHex(
      Pointer(Utf8DecodeToRawUnicodeUI(FValue,@Len)),Len shr 1).Add('>');
end;


{ TPdfTextString }

................................................................................
end;

procedure TPdfRawText.InternalWriteTo(var W: TPdfWrite);
begin
  W.Add(FValue);
end;












{ TPdfName }

procedure TPdfName.InternalWriteTo(var W: TPdfWrite);
begin
  W.Add('/').AddEscapeName(pointer(FValue));
end;
................................................................................
    Result := 0 else
    Result := FArray.Count;
end;

procedure TPdfArray.InternalWriteTo(var W: TPdfWrite);
var i: integer;
begin

  W.Add('[');
  for i := 0 to FArray.Count-1 do begin
    TPdfObject(FArray[i]).WriteTo(W);
    W.Add(' ');
  end;
  W.Add(']');
end;
................................................................................
    Result := FArray.Count;
end;

procedure TPdfDictionary.InternalWriteTo(var W: TPdfWrite);
var i: integer;
    FElement: TPdfDictionaryElement;
begin

  W.Add('<<'#10);
  for i := 0 to FArray.Count-1 do begin
    FElement := FArray.List[i];
    if not FElement.IsInternal then begin
      FElement.FKey.WriteTo(W);
      W.Add(' ');
      FElement.FValue.WriteTo(W);
................................................................................

{ TPdfStream }

procedure TPdfStream.InternalWriteTo(var W: TPdfWrite);
var FLength: TPdfNumber;
    TmpStream: TMemoryStream;
    TmpSize: integer;

begin

  FLength := FAttributes.PdfNumberByName('Length');
//  assert(FFilter = TPdfArray(FAttributes.ValueByName('Filter')));
  FWriter.Save; // flush FWriter content
  TmpSize := FWriter.Position;
  if FFilter.FindName('FlateDecode')<>nil then
    if TmpSize<100 then // don't compress tiny blocks
      FFilter.RemoveName('FlateDecode') else begin
................................................................................
          Write(TMemoryStream(FWriter.fDestStream).Memory^,TmpSize);
          Free;
        end;
        TmpSize := TmpStream.Size;
        {$endif}
        FLength.Value := TmpSize;
        FAttributes.WriteTo(W);





        W.Add(#10'stream'#10).Add(TmpStream.Memory,TmpSize).Add(#10'endstream');
      finally
        TmpStream.Free;
      end;
      FWriter.fDestStream.Size := 0; // release internal steram memory
      exit;
    end;
  FLength.Value := TmpSize;
  if FFilter.FArray.Count=0 then
    FAttributes.RemoveItem('Filter'); // if no filter needed finally
  FAttributes.WriteTo(W);
  W.Add(#10'stream'#10).Add(TMemoryStream(FWriter.fDestStream).Memory,TmpSize).





    Add(#10'endstream');
  FWriter.fDestStream.Size := 0; // release internal steram memory
end;

constructor TPdfStream.Create(ADoc: TPdfDocument; DontAddToFXref: boolean=false);
var FXref: TPdfXRef;
begin
................................................................................
  end;
  FAttributes := TPdfDictionary.Create(FXref);
  FAttributes.AddItem('Length', TPdfNumber.Create(0));
  FFilter := TPdfArray.Create(FXref);
  if ADoc.CompressionMethod=cmFlateDecode then
    FFilter.AddItem(TPdfName.Create('FlateDecode'));
  FAttributes.AddItem('Filter', FFilter);
  FWriter := TPdfWrite.Create(THeapMemoryStream.Create,ADoc.CodePage);
end;

destructor TPdfStream.Destroy;
begin
  FWriter.fDestStream.Free;
  FWriter.Free;
  FAttributes.Free;
................................................................................
end;


{ TPdfBinary }

procedure TPdfBinary.InternalWriteTo(var W: TPdfWrite);
begin

  W.Add(Stream.Memory,FStream.Size);
end;

constructor TPdfBinary.Create;
begin
  inherited;
  FStream := THeapMemoryStream.Create;
................................................................................
function TPdfWrite.Add(Value, DigitCount: Integer): TPdfWrite;
var t: array[0..15] of AnsiChar;
    i64: array[0..1] of Int64 absolute t;
begin
//  assert(DigitCount<high(t));
  if B+16>Bend then
    Save;
  i64[0] := $3030303030303030;
  i64[1] := $2030303030303030; // t[15]=' '
  if Value<0 then
    Value := 0;
  StrInt32(@t[15],Value);
  inc(DigitCount);
  Move(t[16-DigitCount],B^,DigitCount);
  inc(B,DigitCount);
................................................................................
var X: array[0..3] of Byte absolute Color;
begin
  if integer(Color)<0 then
    Color := GetSysColor(Color and $ff);
  result := AddWithSpace(X[0]/255).AddWithSpace(X[1]/255).AddWithSpace(X[2]/255);
end;






















function TPdfWrite.AddEscape(Text: PAnsiChar): TPdfWrite;

begin
  if Text<>nil then
  repeat


    if B>=Bend4 then
      Save;
    case Text^ of
     #0: Break;
     '(',')','\': PWord(B)^ := ord('\')+ord(Text^)shl 8;




     #8:          PWord(B)^ := ord('\')+ord('b')shl 8;
     #9:          PWord(B)^ := ord('\')+ord('t')shl 8;
     #10:         PWord(B)^ := ord('\')+ord('n')shl 8;
     #12:         PWord(B)^ := ord('\')+ord('f')shl 8;
     #13: begin
       inc(Text);
       continue;
     end;
     else begin
       B^ := Text^;
       Inc(B);
       Inc(Text);
       continue;
     end;
    end;
    Inc(B,2);
    Inc(Text);
  until false;
  result := self;
end;

const // should be local for better code generation
  HexChars: array[0..15] of AnsiChar = '0123456789ABCDEF';

function TPdfWrite.AddEscapeName(Text: PAnsiChar): TPdfWrite;
................................................................................
    inc(Bin);
    Hex[0] := HexChars[v shr 4];   // MSB stored first (BigEndian)
    Hex[1] := HexChars[v and $F];
    inc(Hex,4);
  end;
end;
var L: Integer;



begin








  repeat
    L := WideCharCount;
    if B+L*4>=BEnd then begin
      Save;
      if L>high(Tmp) shr 2 then
        L := high(Tmp) shr 2; // max WideCharCount allowed in Tmp[]
    end;
    BinToHex4(pointer(PW),B,L);
    inc(PtrInt(PW),L*2);
    inc(B,L*4);
    dec(WideCharCount,L);
  until WideCharCount=0;

  result := self;
end;

function TPdfWrite.AddToUnicodeHexText(const Text: PDFString;
  NextLine: boolean; Canvas: TPdfCanvas): TPdfWrite;
var PBuf: pointer;
    Len: integer;
................................................................................
    P := StrInt32(@t[14],Value);
    Move(P^,B^,@t[15]-P);
    inc(B,@t[15]-P);
  end;
  result := self;
end;

constructor TPdfWrite.Create(DestStream: TStream; CodePage: integer);
begin

  fDestStream := DestStream;
  fDestStreamPosition := fDestStream.Seek(0,soFromCurrent);
  fCodePage := CodePage;
  B := @Tmp;
  Bend := B+high(Tmp);
  Bend4 := Bend-4;
end;

function TPdfWrite.Position: Integer;
begin
................................................................................
    SelectObject(FDC,fSelectedDCFontOld);
  DeleteDC(FDC);
  FEmbeddedTTFIgnore.Free;
  fRawPages.Free;
  fBookMarks.Free;
  fMissingBookmarks.Free;
  inherited;



end;

function TPdfDocument.CreateEmbeddedFont(const FontName: RawUTF8): TPdfFont;
const
  // WidthArray[30]=Ascent, WidthArray[31]=Descent,
  // WidthArray[32..255]=Width(#32..#255)
  ARIAL_W_ARRAY: array[30..255] of SmallInt = ( 905, -212,
................................................................................
    i: integer;
    NeedFileID: boolean;
    FileID: array[0..3] of cardinal;
    {$ifndef USE_PDFSECURITY}
    P: PAnsiChar;
    {$endif}
const
  PDFHEADER: array[boolean] of PDFString = (
    '%PDF-1.3'#10, '%PDF-1.4'#10'%'#228#229#230#240#10);
  ICC: array[0..139] of cardinal = (
    805437440,1161970753,4098,1920233069,541214546,542792024,134270983,318769920,989868800,
    1886610273,1280331841,0,1701736302,0,0,0,0,3606446080,256,768802816,1161970753,
    0,0,0,0,0,0,0,0,0,0,0,167772160,1953656931,4227858432,838860800,1668506980,805371904,
    1795162112,1953526903,2617311232,335544320,1953524578,2952855552,335544320,1129469042,
    3288399872,234881024,1129469031,3556835328,234881024,1129469026,3825270784,234881024,
    1515804786,4093706240,335544320,1515804775,134348800,335544320,1515804770,469893120,
................................................................................
    16777216,13058,1987212643,0,16777216,13058,542792024,0,412876800,2773417984,4228120576,
    542792024,0,2368995328,748683264,2500788224,542792024,0,824573952,789577728,2629697536);
begin
  fLastOutline := nil;
  FreeDoc;
  FXref := TPdfXref.Create;
  FTrailer := TPdfTrailer.Create(FXref);
  FHeader := TPdfRawText.Create(PDFHEADER[PDFA1]);
  FFontList := TList.Create;
  FXObjectList := TPdfArray.Create(FXref);
  FXObjectList.FSaveAtTheEnd := true;
  FObjectList := TList.Create;
  FRoot := TPdfCatalog.Create;
  CatalogDictionary := TPdfDictionary.Create(FXref);
  FXref.AddObject(CatalogDictionary);
................................................................................
    FRoot.Data.AddItem('Outlines', FOutlineRoot.Data);
  end;
  CreateInfo;
  FInfo.CreationDate := now;
  FCurrentPages := CreatePages(nil); // nil -> create root Pages XObject
  FRoot.SetPages(FCurrentPages);
  NeedFileID := false;




  if PDFA1 then begin





    fUseFontFallBack := true;
    FOutputIntents := TPdfArray.Create(FXref);
    Dico := TPdfDictionary.Create(FXRef);
    Dico.AddItem('Type','OutputIntent');
    Dico.AddItem('S','GTS_PDFA1');
    Dico.AddItemText('OutputConditionIdentifier','sRGB');
    Dico.AddItemText('RegistryName','http://www.color.org');
................................................................................
  if NeedFileID then begin
    Randomize;
    for i := 0 to high(FileID) do
      FileID[i] := cardinal(Random(MaxInt));
    inc(FileID[0],GetTickCount);
    {$ifdef USE_PDFSECURITY}
    fFileID := MD5Buf(FileID[0],16);
    IDs := '<'+MD5DigestToString(fFileID)+'>';
    {$else}
    SetLength(IDs,34);
    P := pointer(IDs);
    P[0] := '<';
    SynCommons.BinToHex(PAnsiChar(@FileID[0]),P+1,16);
    P[33] := '>';
    {$endif}
    ID := TPdfArray.Create(FXref);
    ID.AddItem(TPdfRawText.Create(IDs));
    ID.AddItem(TPdfRawText.Create(IDs));
    FTrailer.Attributes.AddItem('ID',ID);
  end;




end;

function TPdfDocument.AddXObject(const AName: PDFString; AXObject: TPdfXObject): integer;
begin
  if GetXObject(AName)<>nil then
    raise EPdfInvalidValue.CreateFmt('AddXObject: dup name %s', [AName]);
  // check whether AImage is valid PdfImage or not.
................................................................................
    for i := FFontList.Count-1 downto 0 do
      TObject(FFontList.List[i]).Free;
    FreeAndNil(FFontList);
    for i := FObjectList.Count-1 downto 0 do
      TObject(FObjectList.List[i]).Free;
    FreeAndNil(FObjectList);
    FreeAndNil(FXref);
    FreeAndNil(FHeader);
    FreeAndNil(FTrailer);
  end;
end;

procedure TPdfDocument.SaveToStream(AStream: TStream; ForceModDate: TDateTime=0);
begin
  if FCanvas.Page=nil then
................................................................................
    on E: Exception do // error on file creation (opened in reader?)
      result := false;
  end;
end;

procedure TPdfDocument.SaveToStreamDirectBegin(AStream: TStream;
  ForceModDate: TDateTime);


begin
  if fSaveToStreamWriter<>nil then
    raise EPdfInvalidOperation.Create('SaveToStreamDirectBegin');
  // write all objects to specified stream
  if ForceModDate=0 then
    FInfo.ModDate := Now else
    FInfo.ModDate := ForceModDate;
................................................................................
      '<pdf:Keywords>').Add(StringToUTF8(Info.Keywords)).Add('</pdf:Keywords>'+
      '<pdf:Producer>'+PDF_PRODUCER+'</pdf:Producer></rdf:Description>'+
      '<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'+
      '<pdfaid:part>1</pdfaid:part><pdfaid:conformance>A</pdfaid:conformance>'+
      '</rdf:Description></rdf:RDF></x:xmpmeta><?xpacket end="w"?>');
  end;
  // write beginning of the content
  fSaveToStreamWriter := TPdfWrite.Create(AStream,FCodePage);
  FHeader.WriteTo(fSaveToStreamWriter);
end;

procedure TPdfDocument.SaveToStreamDirectPageFlush;
var i: integer;
begin
  if (self=nil) or (fSaveToStreamWriter=nil) or (FCanvas.FPage=nil) then
    raise EPdfInvalidOperation.Create('SaveToStreamDirectPageFlush');
................................................................................
    pusSubsetKeepList: PWordArray; usSubsetKeepListCount: word;
    lpfnAllocate, lpfnReAllocate, lpfnFree, reserved: pointer): cardinal; cdecl; 

procedure TPdfFontTrueType.PrepareForSaving;
var c: AnsiChar;
    i, n, L, ndx, count: integer;
    Descendants: TPdfArray;
    Descendant: TPdfDictionary;
    ToUnicode: TPdfStream;
    DS: TStream;
    WR: TPdfWrite;
    old: THandle;
    ttfSize: cardinal;
    ttf: PDFString;
    SubSetData: PAnsiChar;
    SubSetMem: cardinal;
    SubSetSize: cardinal;
    Used: TSortedWordArray;
begin
  DS := THeapMemoryStream.Create;
  WR := TPdfWrite.Create(DS,CODEPAGE_US);
  try
    if Unicode then begin
      // 1. Unicode Font (see PDF 1.3 reference 5.9)
      // create descendant font
      Descendant := TPdfDictionary.Create(fDoc.FXref);
      Descendant.AddItem('Type','Font');
      Descendant.AddItem('Subtype','CIDFontType2');
      Descendant.AddItem('BaseFont',FName);




      Descendant.AddItem('CIDSystemInfo',TPdfRawText.Create(
        '<<'#10'/Ordering(Identity)/Registry(Adobe)/Supplement 0'#10'>>'));
      n := WinAnsiFont.fUsedWideChar.Count;
      if n>0 then begin
        fFirstChar := WinAnsiFont.fUsedWide[0].Glyph;
        fLastChar := WinAnsiFont.fUsedWide[n-1].Glyph;
      end;
      Descendant.AddItem('DW',WinAnsiFont.fDefaultWidth);
      if fDoc.PDFA1 or not WinAnsiFont.fFixedWidth then begin
................................................................................
constructor TPdfEncryption.Create(aLevel: TPdfEncryptionLevel;
  aPermissions: TPdfEncryptionPermissions;
  const aUserPassword, aOwnerPassword: string);
begin
  fLevel := aLevel;
  fPermissions := aPermissions;
  fUserPassword := aUserPassword;



  fOwnerPassword := aOwnerPassword;
end;

class function TPdfEncryption.New(aLevel: TPdfEncryptionLevel;
  const aUserPassword, aOwnerPassword: string;
  aPermissions: TPdfEncryptionPermissions): TPdfEncryption;
begin
................................................................................
  case aLevel of
  elRC4_40, elRC4_128:
    result := TPdfEncryptionRC4MD5.Create(aLevel,aPermissions,aUserPassword,aOwnerPassword);
  else
    result := nil;
  end;
end;







{ TPdfEncryptionRC4MD5 }

constructor TPdfEncryptionRC4MD5.Create(aLevel: TPdfEncryptionLevel;
  aPermissions: TPdfEncryptionPermissions; const aUserPassword,
  aOwnerPassword: string);










begin

















  inherited;




















end;

















































procedure TPdfEncryptionRC4MD5.EncodeBuffer(const BufIn; var BufOut; Count: cardinal;
  ObjectNumber, GenerationNumber: integer);
// see http://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt

var RC4: TRC4;
  procedure ComputeNewRC4Key;
  const HASHSIZE: array[elRC4_40..elRC4_128] of integer = (5,16);
        KEYSIZE:  array[elRC4_40..elRC4_128] of integer = (10,16);
  var MD5: TMD5;
      Digest: TMD5Digest;
  begin
    MD5.Init;
    MD5.Update(fInternalKey[0],HASHSIZE[fLevel]);
    MD5.Update(ObjectNumber,3);
    MD5.Update(GenerationNumber,2);
    MD5.Final(Digest);
    RC4.Init(Digest,KEYSIZE[fLevel]);
    RC4.SaveKey(fLastRC4Key); // a lot of string encodings have the same context
    fLastObjectNumber := ObjectNumber;
    fLastGenerationNumber := GenerationNumber;
  end;
begin
  if (ObjectNumber<>fLastObjectNumber) or
     (GenerationNumber<>fLastGenerationNumber) then
    ComputeNewRC4Key else
    RC4.RestoreKey(fLastRC4Key);
  RC4.Encrypt(BufIn,BufOut,Count); // RC4 allows in-place encryption :)
end;

{$endif USE_PDFSECURITY}








>







 







|

|

|
>
>
>
>
>
>
>







 







>
>
>







 







>
|
|
|








>




>
|
<





>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
>
>

>





|
|
|
|
|
|
|
<











>







 







|







 







>
>
>
>
|







 







|



|
|







 







>
>
>
>
>
>
>
>
>
>
>
>







 







|





<







 







>





>
>
>













>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>







 







|

|


|







 







|







 







>
>
>
>
>
>
>
>
>
>







 







>







 







>







 







>

>







 







>
>
>
>
>
|










|
>
>
>
>
>







 







|







 







>







 







|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>

<
<
>
>



<

>
>
>
>
|
|
|
|
<
<
<
|









|







 







>
>
>

>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
>







 







|

>


|







 







>
>
>







 







<
<







 







<







 







>
>
>
>

>
>
>
>
>







 







|












>
>
>
>







 







<







 







>
>







 







|
|







 







|












|








>
>
>
>
|
<







 







>
>
>







 








>
>
>
>
>
>


<
<
<
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

|
<

>


<
|




|
|
|



|
|


|
|







182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
...
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
...
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
...
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
...
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
...
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
...
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
...
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
....
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201

1202
1203
1204
1205
1206
1207
1208
....
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
....
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
....
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
....
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
....
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
....
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
....
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
....
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
....
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
....
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
....
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
....
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
....
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
....
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950


3951
3952
3953
3954
3955

3956
3957
3958
3959
3960
3961
3962
3963
3964



3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
....
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
....
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
....
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
....
5136
5137
5138
5139
5140
5141
5142


5143
5144
5145
5146
5147
5148
5149
....
5154
5155
5156
5157
5158
5159
5160

5161
5162
5163
5164
5165
5166
5167
....
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
....
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
....
5281
5282
5283
5284
5285
5286
5287

5288
5289
5290
5291
5292
5293
5294
....
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
....
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
....
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341

7342
7343
7344
7345
7346
7347
7348
....
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
....
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791



9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820

9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890

9891
9892
9893
9894

9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
  - SynPdf unit can now link to standard ZLib.pas unit if you want to use SynPdf
    stand-alone and do not need SynZip.pas + deflate.obj + trees.obj
    (but SQLite3Commons.pas main unit of mORMot will need SynZip, so it is
    enabled by default for use within the framework)

  Version 1.18
  - major speed up of TPdfCanvas.RenderMetaFile() by caching printer resolution
  - implemented 40 bit and 128 bit security - see TPdfEncryption.New()
  - introducing TPdfDocument.SaveToStreamDirectBegin/PageFlush/End methods,
    able to render all page content directly to the destination stream/file,
    therefore reducing the memory use to a minimal value for huge content - used
    e.g. in TPdfDocumentGDI.SaveToStream() and TGDIPages.ExportPDFStream()
  - TPdfDocumentGDI will now compress (via our SynLZ algorithm) all its page
    content (TMetaFile) for efficiency
  - therefore, TPdfDocumentGDI will use much less resource and memory with no
................................................................................
    produce bigger pdf file size, but will fulfill feature request [7d6a3a3f0f]  

}


{$I Synopse.inc} // define HASINLINE USETYPEINFO CPU32 CPU64

{$define USE_PDFSECURITY}
{ - if defined, the TPdfDocument*.Create() constructor will have an additional
  AEncryption: TPdfEncryption parameter able to create secured PDF files
  - this feature will need the SynCrypto unit for MD5 and RC4 algorithms }

{$ifdef NO_USE_PDFSECURITY}
  { this special conditional can be set globaly for an application which doesn't
    need the security features, therefore dependency to SynCrypto unit }
  {$undef USE_PDFSECURITY}
{$endif}


{$define USE_UNISCRIBE}
{ - if defined, the PDF engine will use the Windows Uniscribe API to
  render Ordering and Shaping of the text (usefull for Hebrew, Arabic and
  some Asiatic languages)
  - this feature need the TPdfDocument.UseUniscribe property to be forced to true
  according to the language of the text you want to render
  - can be undefined to safe some KB if you're sure you won't need it }
................................................................................
  /// the PDF library use internaly AnsiString text encoding
  // - the corresponding charset is the current system charset, or the one
  // supplied as a parameter to TPdfDocument.Create
  PDFString = AnsiString;

  /// a PDF date, encoded as 'D:20100414113241'
  TPdfDate = PDFString;

  /// the internal pdf file format 
  TPdfFileFormat = (pdf13, pdf14);

  /// PDF exception, raised when an invalid value is given to a constructor
  EPdfInvalidValue = class(Exception);

  /// PDF exception, raised when an invalid operation is trigerred
  EPdfInvalidOperation = class(Exception);

................................................................................
  // - Authoring Comments and Form Fields: If this is disabled, adding,
  // modifying, or deleting comments and form fields is prohibited. Form field
  // filling is allowed.
  // - Form Field Fill-in or Signing: If this is enabled, users can sign and
  // fill in forms, but not create form fields.
  // - Document Assembly: If this is disabled, inserting, deleting or rotating
  // pages, or creating bookmarks and thumbnails is prohibited.
  // - ordinal value follow the flag bit position, as expected by pdf 
  TPdfEncryptionPermission = (epPrinting=2, epGeneralEditing, epContentCopy,
    epAuthoringComment, epFillingForms=8, epContentExtraction,
    epDocumentAssembly, epPrintingHighResolution);

  /// set of restrictions on PDF document operations
  TPdfEncryptionPermissions = set of TPdfEncryptionPermission;

  /// abstract class to handle PDF security
  TPdfEncryption = class
  protected
    fLevel: TPdfEncryptionLevel;
    fFlags: integer;
    fInternalKey: TByteDynArray;
    fPermissions: TPdfEncryptionPermissions;
    fUserPassword: string;
    fOwnerPassword: string;
    fDoc: TPdfDocument;
    procedure EncodeBuffer(const BufIn; var BufOut; Count: cardinal); virtual; abstract;

  public
    /// initialize the internal structures with the proper classes
    // - do not call this method directly, but class function TPdfEncryption.New()
    constructor Create(aLevel: TPdfEncryptionLevel; aPermissions: TPdfEncryptionPermissions;
      const aUserPassword, aOwnerPassword: string); virtual;
    /// prepare a specific document to be encrypted
    // - internally used by TPdfDocument.NewDoc method
    procedure AttachDocument(aDoc: TPdfDocument); virtual; 
    /// will create the expected TPdfEncryption instance, depending on aLevel
    // - to be called as parameter of TPdfDocument/TPdfDocumentGDI.Create()
    // - currently, only elRC4_40 and elRC4_128 levels are implemented
    // - both passwords are expected to be ASCII-7 characters only
    // - aUserPassword will be asked at file opening: to be set to '' for not
    // blocking display, but optional permission
    // - aOwnerPassword shall not be '', and will be used internally to cypher
    // the pdf file content
    // - aPermissions can be either one of the PDF_PERMISSION_ALL /
    // PDF_PERMISSION_NOMODIF / PDF_PERSMISSION_NOPRINT / PDF_PERMISSION_NOCOPY /
    // PDF_PERMISSION_NOCOPYNORPRINT set of options
    // - typical use may be:
    // ! Doc := TPdfDocument.Create(false,0,false,
    // !   TPdfEncryption.New(elRC4_40,'','toto',PDF_PERMISSION_NOMODIF));
    // ! Doc := TPdfDocument.Create(false,0,false,
    // !   TPdfEncryption.New(elRC4_128,'','toto',PDF_PERMISSION_NOCOPYNORPRINT));
    class function New(aLevel: TPdfEncryptionLevel;
      const aUserPassword, aOwnerPassword: string;
      aPermissions: TPdfEncryptionPermissions): TPdfEncryption;
  end;

  /// internal 32 bytes buffer, used during encryption process
  TPdfBuffer32 = array[0..31] of byte;

  /// handle PDF security with RC4+MD5 scheme in 40-bit and 128-bit
  // - allowed aLevel parameters for Create() are only elRC4_40 and elRC4_128
  TPdfEncryptionRC4MD5 = class(TPdfEncryption)
  protected
    fLastObjectNumber: integer;
    fLastGenerationNumber: Integer;
    fLastRC4Key: TRC4InternalKey;
    fUserPass, fOwnerPass: array[0..31] of byte;
    fUserPassEncoded, fOwnerPassEncoded: RawUTF8;
    procedure EncodeBuffer(const BufIn; var BufOut; Count: cardinal); override;
  public
    /// prepare a specific document to be encrypted
    // - will compute the internal keys
    procedure AttachDocument(aDoc: TPdfDocument); override;

  end;
{$endif}

  /// buffered writer class, specialized for PDF encoding
  TPdfWrite = class
  protected
    B, BEnd, BEnd4: PAnsiChar;
    fDestStream: TStream;
    fDestStreamPosition: integer;
    fCodePage: integer;
    fAddGlyphFont: (fNone, fMain, fFallBack);
    fDoc: TPdfDocument;
    Tmp: array[0..511] of AnsiChar;
    /// internal Ansi->Unicode conversion, using the CodePage used in Create()
    // - caller must release the returned memory via FreeMem()
    function ToWideChar(const Ansi: PDFString; out DLen: Integer): PWideChar;
{$ifdef USE_UNISCRIBE}
    /// internal method using the Windows Uniscribe API
    // - return FALSE if PW was not appened to the PDF content, TRUE if OK
................................................................................
      NextLine: boolean; Canvas: TPdfCanvas);
    /// internal methods handling font fall-back
    procedure AddGlyphFromChar(Char: WideChar; Canvas: TPdfCanvas;
      TTF: TPdfFontTrueType; NextLine: PBoolean);
    procedure AddGlyphFlush(Canvas: TPdfCanvas; TTF: TPdfFontTrueType; NextLine: PBoolean);
  public
    /// create the buffered writer, for a specified destination stream
    constructor Create(Destination: TPdfDocument; DestStream: TStream);
    /// add a character to the buffer
    function Add(c: AnsiChar): TPdfWrite; overload; {$ifdef HASINLINE}inline;{$endif}
    /// add an integer numerical value to the buffer
    function Add(Value: Integer): TPdfWrite; overload;
    /// add an integer numerical value to the buffer
    // - add a trailing space
    function AddWithSpace(Value: Integer): TPdfWrite; overload;
................................................................................
    function AddUnicodeHexText(PW: PWideChar; NextLine: boolean;
      Canvas: TPdfCanvas): TPdfWrite;
    /// write some Unicode text, encoded as Glyphs indexes, corresponding
    // to the current font
    function AddGlyphs(Glyphs: PWord; GlyphsCount: integer; Canvas: TPdfCanvas): TPdfWrite;
    /// add some WinAnsi text as PDF text
    // - used by TPdfText object
    // - will optionally encrypt the content
    function AddEscapeContent(const Text: RawByteString): TPdfWrite;
    /// add some WinAnsi text as PDF text
    // - used by TPdfText object
    function AddEscape(Text: PAnsiChar; TextLen: integer): TPdfWrite;
    /// add some WinAnsi text as PDF text
    // - used by TPdfCanvas.ShowText method for WinAnsi text
    function AddEscapeText(Text: PAnsiChar; Font: TPdfFont): TPdfWrite;
    /// add some PDF /property value
    function AddEscapeName(Text: PAnsiChar): TPdfWrite;
    /// add a PDF color, from its TColorRef RGB value
    function AddColorStr(Color: TColorRef): TPdfWrite;
................................................................................
  /// a PDF object, storing a textual value
  // - the value is specified as a PDFString
  // - this object is stored as '(escapedValue)'
  // - in case of MBCS, conversion is made into Unicode before writing, and
  // stored as '<FEFFHexUnicodeEncodedValue>'
  TPdfText = class(TPdfObject)
  private
    FValue: RawByteString;
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  public
    constructor Create(const AValue: RawByteString); reintroduce;
    property Value: RawByteString read FValue write FValue;
  end;

  /// a PDF object, storing a textual value
  // - the value is specified as an UTF-8 encoded string
  // - this object is stored as '(escapedValue)'
  // - in case characters with ANSI code higher than 8 Bits, conversion is made
  // into Unicode before writing, and '<FEFFHexUnicodeEncodedValue>'
................................................................................
  TPdfRawText = class(TPdfText)
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  public
    /// simple creator, replacing every % in Fmt by the corresponding Args[]
    constructor CreateFmt(Fmt: PAnsiChar; const Args: array of Integer);
  end;

{$ifdef USE_PDFSECURITY}
  /// a PDF object, storing a textual value with not encryption
  // - the value is specified as a PDFString
  // - this object is stored as '(escapedValue)'
  TPdfClearText = class(TPdfText)
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  end;
{$else}
  TPdfClearText = TPdfText;
{$endif}

  /// a PDF object, storing a PDF name
  // - this object is stored as '/Value'
  TPdfName = class(TPdfText)
  protected
    procedure InternalWriteTo(var W: TPdfWrite); override;
  end;
................................................................................
  /// array used to store a TPdfImage hash
  // - uses 4 hash codes, created with 4 diverse algorithms, in order to avoid
  // false positives
  TPdfImageHash = array[0..3] of cardinal;

  /// the main class of the PDF engine, processing the whole PDF document
  TPdfDocument = class(TObject)
  protected
    FRoot: TPdfCatalog;
    FCurrentPages: TPdfDictionary;
    FOutputIntents: TPdfArray;
    FMetaData: TPdfStream;
    FCanvas: TPdfCanvas;

    FTrailer: TPdfTrailer;
    FXref: TPdfXref;
    FInfo: TPdfInfo;
    FFontList: TList;
    FObjectList: TList;
    FOutlineRoot: TPdfOutlineRoot;
    FXObjectList: TPdfArray;
................................................................................
    fUseFontFallBack: boolean;
    fFontFallBackIndex: integer;
    /// a list of Bookmark text keys, associated to a TPdfDest object
    fBookMarks: TRawUTF8List;
    fMissingBookmarks: TRawUTF8List;
    /// internal temporary variable - used by CreateOutline
    fLastOutline: TPdfOutlineEntry;
    fFileFormat: TPdfFileFormat;
    fPDFA1: boolean;
    fSaveToStreamWriter: TPdfWrite;
{$ifdef USE_PDFSECURITY}
    fEncryption: TPdfEncryption;
    fFileID: TMD5Digest;
    fEncryptionObject: TPdfDictionary;
    fCurrentObjectNumber: integer;
    fCurrentGenerationNumber: integer; 
{$endif}
    function GetInfo: TPdfInfo;     {$ifdef HASINLINE}inline;{$endif}
    function GetOutlineRoot: TPdfOutlineRoot; {$ifdef HASINLINE}inline;{$endif}
    procedure SetStandardFontsReplace(const Value: boolean); {$ifdef HASINLINE}inline;{$endif}
    function GetEmbeddedTTFIgnore: TRawUTF8List;
    procedure SetDefaultPaperSize(const Value: TPDFPaperSize);
    procedure SetDefaultPageHeight(const Value: cardinal);
    procedure SetDefaultPageWidth(const Value: cardinal);
    procedure SetPDFA1(const Value: boolean);
    function GetDefaultPageLandscape: boolean;
    procedure SetDefaultPageLandscape(const Value: boolean);
    procedure SetFontFallBackName(const Value: string);
    function GetFontFallBackName: string;
    
  protected
    /// can be useful in descendant objects in other units
    fTPdfPageClass: TPdfPageClass;
    procedure RaiseInvalidOperation;
    procedure CreateInfo;
    /// get the PostScript Name of a TrueType Font
    // - use the Naming Table ('name') of the TTF content if not 7 bit ascii
................................................................................
// is created from the specified numerical level (0=root)
procedure GDICommentOutline(MetaHandle: HDC; const aTitle: RawUTF8; aLevel: Integer);

/// append a EMR_GDICOMMENT message for creating a Link into a specified bookmark
procedure GDICommentLink(MetaHandle: HDC; const aBookmarkName: RawUTF8; const aRect: TRect);


{$ifdef USE_PDFSECURITY}
const
  /// allow all actions for a pdf encrypted file
  // - to be used as parameter for TPdfEncryption.New() class method
  PDF_PERMISSION_ALL: TPdfEncryptionPermissions = [epPrinting, epGeneralEditing,
    epContentCopy, epAuthoringComment, epPrintingHighResolution, epFillingForms,
    epContentExtraction, epDocumentAssembly];

  /// disable modification and annotation of a pdf encrypted file
  // - to be used as parameter for TPdfEncryption.New() class method
  PDF_PERMISSION_NOMODIF: TPdfEncryptionPermissions = [epPrinting,
    epContentCopy, epPrintingHighResolution, epFillingForms,
    epContentExtraction, epDocumentAssembly];

  /// disable printing for a pdf encrypted file
  // - to be used as parameter for TPdfEncryption.New() class method
  PDF_PERSMISSION_NOPRINT: TPdfEncryptionPermissions = [epGeneralEditing,
    epContentCopy, epAuthoringComment, epContentExtraction, epDocumentAssembly];

  /// disable content extraction or copy for a pdf encrypted file
  // - to be used as parameter for TPdfEncryption.New() class method
  PDF_PERMISSION_NOCOPY: TPdfEncryptionPermissions = [epPrinting,
    epAuthoringComment, epPrintingHighResolution, epFillingForms];

  /// disable printing and content extraction or copy for a pdf encrypted file
  // - to be used as parameter for TPdfEncryption.New() class method
  PDF_PERMISSION_NOCOPYNORPRINT: TPdfEncryptionPermissions = [];
{$endif USE_PDFSECURITY}


(*
    Windows Uniscribe APIs

    Uniscribe is a set of APIs that allow a high degree of control for fine
    typography and for processing complex scripts
    - see http://msdn.microsoft.com/en-us/library/dd374091(v=VS.85).aspx
................................................................................
constructor TPdfObject.Create;
begin
  FObjectNumber := -1;
end;

procedure TPdfObject.InternalWriteTo(var W: TPdfWrite);
begin
{$ifdef USE_PDFSECURITY}
  if FObjectNumber>0 then
    with W.fDoc do begin
      fCurrentObjectNumber := FObjectNumber;
      fCurrentGenerationNumber := FGenerationNumber;
    end;
{$endif}
end;

procedure TPdfObject.SetObjectNumber(Value: integer);
begin
  FObjectNumber := Value;
  if Value > 0 then
    FObjectType := otIndirectObject else
................................................................................

{ TPdfText }

procedure TPdfText.InternalWriteTo(var W: TPdfWrite);
begin
  // if the value has multibyte character, convert the value to hex unicode.
  // otherwise, escape characters.
  if SysLocale.FarEast and _HasMultiByteString(pointer(FValue)) then 
    W.Add('<FEFF').AddToUnicodeHex(FValue).Add('>') else
    W.Add('(').AddEscapeContent(FValue).Add(')');
end;

constructor TPdfText.Create(const AValue: RawByteString);
begin
  inherited Create;
  Value := AValue;
end;


{ TPdfTextUTF8 }
................................................................................

procedure TPdfTextUTF8.InternalWriteTo(var W: TPdfWrite);
var Len: Integer;
begin
  // if the value has multibyte character, convert the value to hex unicode.
  // otherwise, escape characters
  if IsWinAnsiU8Bit(Pointer(FValue)) then
    W.Add('(').AddEscapeContent(Utf8ToWinAnsi(FValue)).Add(')') else
    W.Add('<FEFF').AddUnicodeHex(
      Pointer(Utf8DecodeToRawUnicodeUI(FValue,@Len)),Len shr 1).Add('>');
end;


{ TPdfTextString }

................................................................................
end;

procedure TPdfRawText.InternalWriteTo(var W: TPdfWrite);
begin
  W.Add(FValue);
end;

{$ifdef USE_PDFSECURITY}

{ TPdfClearText }

procedure TPdfClearText.InternalWriteTo(var W: TPdfWrite);
begin
  W.Add('(').AddEscape(pointer(FValue),Length(FValue)).Add(')');
end;

{$endif}

{ TPdfName }

procedure TPdfName.InternalWriteTo(var W: TPdfWrite);
begin
  W.Add('/').AddEscapeName(pointer(FValue));
end;
................................................................................
    Result := 0 else
    Result := FArray.Count;
end;

procedure TPdfArray.InternalWriteTo(var W: TPdfWrite);
var i: integer;
begin
  inherited;
  W.Add('[');
  for i := 0 to FArray.Count-1 do begin
    TPdfObject(FArray[i]).WriteTo(W);
    W.Add(' ');
  end;
  W.Add(']');
end;
................................................................................
    Result := FArray.Count;
end;

procedure TPdfDictionary.InternalWriteTo(var W: TPdfWrite);
var i: integer;
    FElement: TPdfDictionaryElement;
begin
  inherited;
  W.Add('<<'#10);
  for i := 0 to FArray.Count-1 do begin
    FElement := FArray.List[i];
    if not FElement.IsInternal then begin
      FElement.FKey.WriteTo(W);
      W.Add(' ');
      FElement.FValue.WriteTo(W);
................................................................................

{ TPdfStream }

procedure TPdfStream.InternalWriteTo(var W: TPdfWrite);
var FLength: TPdfNumber;
    TmpStream: TMemoryStream;
    TmpSize: integer;
    Buf: pointer;
begin
  inherited;
  FLength := FAttributes.PdfNumberByName('Length');
//  assert(FFilter = TPdfArray(FAttributes.ValueByName('Filter')));
  FWriter.Save; // flush FWriter content
  TmpSize := FWriter.Position;
  if FFilter.FindName('FlateDecode')<>nil then
    if TmpSize<100 then // don't compress tiny blocks
      FFilter.RemoveName('FlateDecode') else begin
................................................................................
          Write(TMemoryStream(FWriter.fDestStream).Memory^,TmpSize);
          Free;
        end;
        TmpSize := TmpStream.Size;
        {$endif}
        FLength.Value := TmpSize;
        FAttributes.WriteTo(W);
        Buf := TmpStream.Memory;
        {$ifdef USE_PDFSECURITY}
        if W.fDoc.fEncryption<>nil then
          W.fDoc.fEncryption.EncodeBuffer(Buf^,Buf^,TmpSize);
        {$endif}
        W.Add(#10'stream'#10).Add(Buf,TmpSize).Add(#10'endstream');
      finally
        TmpStream.Free;
      end;
      FWriter.fDestStream.Size := 0; // release internal steram memory
      exit;
    end;
  FLength.Value := TmpSize;
  if FFilter.FArray.Count=0 then
    FAttributes.RemoveItem('Filter'); // if no filter needed finally
  FAttributes.WriteTo(W);
  Buf := TMemoryStream(FWriter.fDestStream).Memory;
{$ifdef USE_PDFSECURITY}
  if W.fDoc.fEncryption<>nil then
    W.fDoc.fEncryption.EncodeBuffer(Buf^,Buf^,TmpSize);
{$endif}
  W.Add(#10'stream'#10).Add(Buf,TmpSize).
    Add(#10'endstream');
  FWriter.fDestStream.Size := 0; // release internal steram memory
end;

constructor TPdfStream.Create(ADoc: TPdfDocument; DontAddToFXref: boolean=false);
var FXref: TPdfXRef;
begin
................................................................................
  end;
  FAttributes := TPdfDictionary.Create(FXref);
  FAttributes.AddItem('Length', TPdfNumber.Create(0));
  FFilter := TPdfArray.Create(FXref);
  if ADoc.CompressionMethod=cmFlateDecode then
    FFilter.AddItem(TPdfName.Create('FlateDecode'));
  FAttributes.AddItem('Filter', FFilter);
  FWriter := TPdfWrite.Create(ADoc,THeapMemoryStream.Create);
end;

destructor TPdfStream.Destroy;
begin
  FWriter.fDestStream.Free;
  FWriter.Free;
  FAttributes.Free;
................................................................................
end;


{ TPdfBinary }

procedure TPdfBinary.InternalWriteTo(var W: TPdfWrite);
begin
  inherited;
  W.Add(Stream.Memory,FStream.Size);
end;

constructor TPdfBinary.Create;
begin
  inherited;
  FStream := THeapMemoryStream.Create;
................................................................................
function TPdfWrite.Add(Value, DigitCount: Integer): TPdfWrite;
var t: array[0..15] of AnsiChar;
    i64: array[0..1] of Int64 absolute t;
begin
//  assert(DigitCount<high(t));
  if B+16>Bend then
    Save;
  i64[0] := $3030303030303030; // t[0..14]='0'
  i64[1] := $2030303030303030; // t[15]=' '
  if Value<0 then
    Value := 0;
  StrInt32(@t[15],Value);
  inc(DigitCount);
  Move(t[16-DigitCount],B^,DigitCount);
  inc(B,DigitCount);
................................................................................
var X: array[0..3] of Byte absolute Color;
begin
  if integer(Color)<0 then
    Color := GetSysColor(Color and $ff);
  result := AddWithSpace(X[0]/255).AddWithSpace(X[1]/255).AddWithSpace(X[2]/255);
end;

function TPdfWrite.AddEscapeContent(const Text: RawByteString): TPdfWrite;
{$ifdef USE_PDFSECURITY}
var tmp: PAnsiChar;
    L: integer;
{$endif}
begin
{$ifdef USE_PDFSECURITY}
  if (Text<>'') and (fDoc.fEncryption<>nil) then begin
    L := length(Text);
    GetMem(tmp,L);
    try
      fDoc.fEncryption.EncodeBuffer(pointer(Text)^,tmp^,L);
      result := AddEscape(tmp,L);
    finally
      Freemem(tmp);
    end;
  end else
{$endif}
  result := AddEscape(pointer(Text),length(Text));
end;

function TPdfWrite.AddEscape(Text: PAnsiChar; TextLen: integer): TPdfWrite;
var TextEnd: PAnsiChar;
begin


  TextEnd := Text+TextLen;
  while Text<TextEnd do begin
    if B>=Bend4 then
      Save;
    case Text^ of

     '(',')','\': PWord(B)^ := ord('\')+ord(Text^)shl 8;
     #0: begin
       PInteger(B)^ := ord('\')+ord('0')shl 8+ord('0')shl 16+ord('0')shl 24;
       inc(B,2);
     end;
     #8:  PWord(B)^ := ord('\')+ord('b')shl 8;
     #9:  PWord(B)^ := ord('\')+ord('t')shl 8;
     #10: PWord(B)^ := ord('\')+ord('n')shl 8;
     #12: PWord(B)^ := ord('\')+ord('f')shl 8;



     #13: PWord(B)^ := ord('\')+ord('r')shl 8;
     else begin
       B^ := Text^;
       Inc(B);
       Inc(Text);
       continue;
     end;
    end;
    Inc(B,2);
    Inc(Text);
  end;
  result := self;
end;

const // should be local for better code generation
  HexChars: array[0..15] of AnsiChar = '0123456789ABCDEF';

function TPdfWrite.AddEscapeName(Text: PAnsiChar): TPdfWrite;
................................................................................
    inc(Bin);
    Hex[0] := HexChars[v shr 4];   // MSB stored first (BigEndian)
    Hex[1] := HexChars[v and $F];
    inc(Hex,4);
  end;
end;
var L: Integer;
{$ifdef USE_PDFSECURITY}
    tmp: TWordDynArray;
{$endif}
begin
  if WideCharCount>0 then begin
{$ifdef USE_PDFSECURITY}
    if fDoc.fEncryption<>nil then begin
      SetLength(tmp,WideCharCount);
      fDoc.fEncryption.EncodeBuffer(PW^,pointer(tmp)^,WideCharCount*2);
      PW := pointer(tmp);
    end;
{$endif}
    repeat
      L := WideCharCount;
      if B+L*4>=BEnd then begin
        Save;
        if L>high(Tmp) shr 2 then
          L := high(Tmp) shr 2; // max WideCharCount allowed in Tmp[]
      end;
      BinToHex4(pointer(PW),B,L);
      inc(PtrInt(PW),L*2);
      inc(B,L*4);
      dec(WideCharCount,L);
    until WideCharCount=0;
  end;
  result := self;
end;

function TPdfWrite.AddToUnicodeHexText(const Text: PDFString;
  NextLine: boolean; Canvas: TPdfCanvas): TPdfWrite;
var PBuf: pointer;
    Len: integer;
................................................................................
    P := StrInt32(@t[14],Value);
    Move(P^,B^,@t[15]-P);
    inc(B,@t[15]-P);
  end;
  result := self;
end;

constructor TPdfWrite.Create(Destination: TPdfDocument; DestStream: TStream);
begin
  fDoc := Destination;
  fDestStream := DestStream;
  fDestStreamPosition := fDestStream.Seek(0,soFromCurrent);
  fCodePage := fDoc.CodePage;
  B := @Tmp;
  Bend := B+high(Tmp);
  Bend4 := Bend-4;
end;

function TPdfWrite.Position: Integer;
begin
................................................................................
    SelectObject(FDC,fSelectedDCFontOld);
  DeleteDC(FDC);
  FEmbeddedTTFIgnore.Free;
  fRawPages.Free;
  fBookMarks.Free;
  fMissingBookmarks.Free;
  inherited;
  {$ifdef USE_PDFSECURITY}
  fEncryption.Free;
  {$endif}
end;

function TPdfDocument.CreateEmbeddedFont(const FontName: RawUTF8): TPdfFont;
const
  // WidthArray[30]=Ascent, WidthArray[31]=Descent,
  // WidthArray[32..255]=Width(#32..#255)
  ARIAL_W_ARRAY: array[30..255] of SmallInt = ( 905, -212,
................................................................................
    i: integer;
    NeedFileID: boolean;
    FileID: array[0..3] of cardinal;
    {$ifndef USE_PDFSECURITY}
    P: PAnsiChar;
    {$endif}
const


  ICC: array[0..139] of cardinal = (
    805437440,1161970753,4098,1920233069,541214546,542792024,134270983,318769920,989868800,
    1886610273,1280331841,0,1701736302,0,0,0,0,3606446080,256,768802816,1161970753,
    0,0,0,0,0,0,0,0,0,0,0,167772160,1953656931,4227858432,838860800,1668506980,805371904,
    1795162112,1953526903,2617311232,335544320,1953524578,2952855552,335544320,1129469042,
    3288399872,234881024,1129469031,3556835328,234881024,1129469026,3825270784,234881024,
    1515804786,4093706240,335544320,1515804775,134348800,335544320,1515804770,469893120,
................................................................................
    16777216,13058,1987212643,0,16777216,13058,542792024,0,412876800,2773417984,4228120576,
    542792024,0,2368995328,748683264,2500788224,542792024,0,824573952,789577728,2629697536);
begin
  fLastOutline := nil;
  FreeDoc;
  FXref := TPdfXref.Create;
  FTrailer := TPdfTrailer.Create(FXref);

  FFontList := TList.Create;
  FXObjectList := TPdfArray.Create(FXref);
  FXObjectList.FSaveAtTheEnd := true;
  FObjectList := TList.Create;
  FRoot := TPdfCatalog.Create;
  CatalogDictionary := TPdfDictionary.Create(FXref);
  FXref.AddObject(CatalogDictionary);
................................................................................
    FRoot.Data.AddItem('Outlines', FOutlineRoot.Data);
  end;
  CreateInfo;
  FInfo.CreationDate := now;
  FCurrentPages := CreatePages(nil); // nil -> create root Pages XObject
  FRoot.SetPages(FCurrentPages);
  NeedFileID := false;
  {$ifdef USE_PDFSECURITY}
  if fEncryption<>nil then
    NeedFileID := true;
  {$endif}
  if PDFA1 then begin
    fFileFormat := pdf14;
    {$ifdef USE_PDFSECURITY}
    if fEncryption<>nil then
      raise EPdfInvalidOperation.Create('PDF/A-1 not allowed when encryption is enabled');
    {$endif}
    fUseFontFallBack := true;
    FOutputIntents := TPdfArray.Create(FXref);
    Dico := TPdfDictionary.Create(FXRef);
    Dico.AddItem('Type','OutputIntent');
    Dico.AddItem('S','GTS_PDFA1');
    Dico.AddItemText('OutputConditionIdentifier','sRGB');
    Dico.AddItemText('RegistryName','http://www.color.org');
................................................................................
  if NeedFileID then begin
    Randomize;
    for i := 0 to high(FileID) do
      FileID[i] := cardinal(Random(MaxInt));
    inc(FileID[0],GetTickCount);
    {$ifdef USE_PDFSECURITY}
    fFileID := MD5Buf(FileID[0],16);
    IDs := '<'+RawByteString(MD5DigestToString(fFileID))+'>';
    {$else}
    SetLength(IDs,34);
    P := pointer(IDs);
    P[0] := '<';
    SynCommons.BinToHex(PAnsiChar(@FileID[0]),P+1,16);
    P[33] := '>';
    {$endif}
    ID := TPdfArray.Create(FXref);
    ID.AddItem(TPdfRawText.Create(IDs));
    ID.AddItem(TPdfRawText.Create(IDs));
    FTrailer.Attributes.AddItem('ID',ID);
  end;
  {$ifdef USE_PDFSECURITY}
  if fEncryption<>nil then 
    fEncryption.AttachDocument(self);
  {$endif}
end;

function TPdfDocument.AddXObject(const AName: PDFString; AXObject: TPdfXObject): integer;
begin
  if GetXObject(AName)<>nil then
    raise EPdfInvalidValue.CreateFmt('AddXObject: dup name %s', [AName]);
  // check whether AImage is valid PdfImage or not.
................................................................................
    for i := FFontList.Count-1 downto 0 do
      TObject(FFontList.List[i]).Free;
    FreeAndNil(FFontList);
    for i := FObjectList.Count-1 downto 0 do
      TObject(FObjectList.List[i]).Free;
    FreeAndNil(FObjectList);
    FreeAndNil(FXref);

    FreeAndNil(FTrailer);
  end;
end;

procedure TPdfDocument.SaveToStream(AStream: TStream; ForceModDate: TDateTime=0);
begin
  if FCanvas.Page=nil then
................................................................................
    on E: Exception do // error on file creation (opened in reader?)
      result := false;
  end;
end;

procedure TPdfDocument.SaveToStreamDirectBegin(AStream: TStream;
  ForceModDate: TDateTime);
const PDFHEADER: array[TPdfFileFormat] of PDFString = (
    '%PDF-1.3'#10, '%PDF-1.4'#10'%'#228#229#230#240#10);
begin
  if fSaveToStreamWriter<>nil then
    raise EPdfInvalidOperation.Create('SaveToStreamDirectBegin');
  // write all objects to specified stream
  if ForceModDate=0 then
    FInfo.ModDate := Now else
    FInfo.ModDate := ForceModDate;
................................................................................
      '<pdf:Keywords>').Add(StringToUTF8(Info.Keywords)).Add('</pdf:Keywords>'+
      '<pdf:Producer>'+PDF_PRODUCER+'</pdf:Producer></rdf:Description>'+
      '<rdf:Description rdf:about="" xmlns:pdfaid="http://www.aiim.org/pdfa/ns/id/">'+
      '<pdfaid:part>1</pdfaid:part><pdfaid:conformance>A</pdfaid:conformance>'+
      '</rdf:Description></rdf:RDF></x:xmpmeta><?xpacket end="w"?>');
  end;
  // write beginning of the content
  fSaveToStreamWriter := TPdfWrite.Create(self,AStream);
  fSaveToStreamWriter.Add(PDFHEADER[fFileformat]);
end;

procedure TPdfDocument.SaveToStreamDirectPageFlush;
var i: integer;
begin
  if (self=nil) or (fSaveToStreamWriter=nil) or (FCanvas.FPage=nil) then
    raise EPdfInvalidOperation.Create('SaveToStreamDirectPageFlush');
................................................................................
    pusSubsetKeepList: PWordArray; usSubsetKeepListCount: word;
    lpfnAllocate, lpfnReAllocate, lpfnFree, reserved: pointer): cardinal; cdecl; 

procedure TPdfFontTrueType.PrepareForSaving;
var c: AnsiChar;
    i, n, L, ndx, count: integer;
    Descendants: TPdfArray;
    Descendant, CIDSystemInfo: TPdfDictionary;
    ToUnicode: TPdfStream;
    DS: TStream;
    WR: TPdfWrite;
    old: THandle;
    ttfSize: cardinal;
    ttf: PDFString;
    SubSetData: PAnsiChar;
    SubSetMem: cardinal;
    SubSetSize: cardinal;
    Used: TSortedWordArray;
begin
  DS := THeapMemoryStream.Create;
  WR := TPdfWrite.Create(fDoc,DS);
  try
    if Unicode then begin
      // 1. Unicode Font (see PDF 1.3 reference 5.9)
      // create descendant font
      Descendant := TPdfDictionary.Create(fDoc.FXref);
      Descendant.AddItem('Type','Font');
      Descendant.AddItem('Subtype','CIDFontType2');
      Descendant.AddItem('BaseFont',FName);
      CIDSystemInfo := TPdfDictionary.Create(FDoc.FXref);
      CIDSystemInfo.AddItem('Supplement',0);
      CIDSystemInfo.AddItemText('Ordering','Identity');
      CIDSystemInfo.AddItemText('Registry','Adobe');
      Descendant.AddItem('CIDSystemInfo',CIDSystemInfo);

      n := WinAnsiFont.fUsedWideChar.Count;
      if n>0 then begin
        fFirstChar := WinAnsiFont.fUsedWide[0].Glyph;
        fLastChar := WinAnsiFont.fUsedWide[n-1].Glyph;
      end;
      Descendant.AddItem('DW',WinAnsiFont.fDefaultWidth);
      if fDoc.PDFA1 or not WinAnsiFont.fFixedWidth then begin
................................................................................
constructor TPdfEncryption.Create(aLevel: TPdfEncryptionLevel;
  aPermissions: TPdfEncryptionPermissions;
  const aUserPassword, aOwnerPassword: string);
begin
  fLevel := aLevel;
  fPermissions := aPermissions;
  fUserPassword := aUserPassword;
  if aOwnerPassword='' then
    raise EPdfInvalidOperation.CreateFmt(
      '%s expect a non void owner password',[ClassName]);
  fOwnerPassword := aOwnerPassword;
end;

class function TPdfEncryption.New(aLevel: TPdfEncryptionLevel;
  const aUserPassword, aOwnerPassword: string;
  aPermissions: TPdfEncryptionPermissions): TPdfEncryption;
begin
................................................................................
  case aLevel of
  elRC4_40, elRC4_128:
    result := TPdfEncryptionRC4MD5.Create(aLevel,aPermissions,aUserPassword,aOwnerPassword);
  else
    result := nil;
  end;
end;

procedure TPdfEncryption.AttachDocument(aDoc: TPdfDocument);
begin
  fDoc := aDoc;
end;


{ TPdfEncryptionRC4MD5 }




const
  // see "Algorithm 3.2 Computing an encryption key" in the PDF reference doc
  PDF_PADDING: TPdfBuffer32 =
    ($28,$BF,$4E,$5E,$4E,$75,$8A,$41,$64,$00,$4E,$56,$FF,$FA,$01,$08,
     $2E,$2E,$00,$B6,$D0,$68,$3E,$80,$2F,$0C,$A9,$FE,$64,$53,$69,$7A);

procedure TPdfEncryptionRC4MD5.AttachDocument(aDoc: TPdfDocument);
procedure Pad(const source: string; var dest: TPdfBuffer32);
var L: integer;
    tmp: WinAnsiString;
begin
  tmp := StringToWinAnsi(source);
  L := Length(tmp);
  if L>SizeOf(dest) then
    L := SizeOf(dest) else
    Move(PDF_PADDING,dest[L],SizeOf(dest)-L);
  Move(pointer(tmp)^,dest,L);
end;
const HASHSIZE: array[elRC4_40..elRC4_128] of integer = (5,16);
      DICT: array[elRC4_40..elRC4_128] of record V,R,L: integer end =
        ((V:1;R:2;L:40),(V:2;R:3;L:128));
      FLAGPATTERN: array[elRC4_40..elRC4_128] of cardinal = ($FFFFFFC0,$FFFFF0C0);
var RC4: TRC4;
    MD5: TMD5;
    Digest, Digest2: TMD5Digest;
    i,j: integer;
    own, usr: TPdfBuffer32;
begin
  inherited;

  // compute corresponding flags
  fFlags := FLAGPATTERN[fLevel] or word(fPermissions);
  if fDoc.fFileFormat<pdf14 then
    fDoc.fFileFormat := pdf14;
  // calc fOwnerPass key (Algorithm 3.3 in PDF reference doc)
  Pad(fUserPassword,usr);
  Pad(fOwnerPassword,own);
  MD5.Full(@own,SizeOf(own),Digest);
  if fLevel=elRC4_128 then
    for i := 1 to 50 do
      MD5.Full(@Digest,sizeof(Digest),Digest);
  RC4.Init(Digest,HASHSIZE[fLevel]);
  RC4.Encrypt(usr,fOwnerPass,sizeof(fOwnerPass));
  if fLevel=elRC4_128 then
    for i := 1 to 19 do begin
      for j := 0 to high(Digest2) do
        Digest2[j] := Digest[j] xor i;
      RC4.Init(Digest2,sizeof(Digest2));
      RC4.Encrypt(fOwnerPass,fOwnerPass,sizeof(fOwnerPass));
    end;
  SetString(fOwnerPassEncoded,PAnsiChar(@fOwnerPass),sizeof(fOwnerPass));
  // calc main file key (Algorithm 3.2 in PDF reference doc)
  MD5.Init;
  MD5.Update(usr,SizeOf(usr));
  MD5.Update(fOwnerPass,sizeof(fOwnerPass));
  MD5.Update(fFlags,sizeof(fFlags));
  MD5.Update(aDoc.fFileID,sizeof(aDoc.fFileID));
  MD5.Final(Digest);
  if fLevel=elRC4_128 then
    for i := 1 to 50 do
      MD5.Full(@Digest,sizeof(Digest),Digest);
  SetLength(fInternalKey,HASHSIZE[fLevel]);
  move(Digest,fInternalKey[0],HASHSIZE[fLevel]);
  // calc fUserPassEncoded content
  SetString(fUserPassEncoded,PAnsiChar(@PDF_PADDING),SizeOf(PDF_PADDING));
  case fLevel of
  elRC4_40: begin   // Algorithm 3.4 in PDF reference doc
    RC4.Init(fInternalKey[0],HASHSIZE[fLevel]);
    RC4.Encrypt(PDF_PADDING,pointer(fUserPassEncoded)^,sizeof(PDF_PADDING));
  end;
  elRC4_128: begin  // Algorithm 3.5 in PDF reference doc
    MD5.Init;
    MD5.Update(PDF_PADDING,sizeof(PDF_PADDING));
    MD5.Update(aDoc.fFileID,sizeof(aDoc.fFileID));
    MD5.Final(Digest);
    for i := 0 to 19 do begin
      for j := 0 to high(Digest2) do
        Digest2[j] := fInternalKey[j] xor i;
      RC4.Init(Digest2,SizeOf(Digest2));
      RC4.Encrypt(Digest,Digest,SizeOf(Digest));
    end;
    Move(Digest,pointer(fUserPassEncoded)^,SizeOf(Digest));
  end;
  end;
  // add encryption dictionary object
  if aDoc.fEncryptionObject=nil then
    aDoc.fEncryptionObject := TPdfDictionary.Create(aDoc.FXref);
  with aDoc.fEncryptionObject, DICT[fLevel] do begin
    AddItem('Filter','Standard');
    AddItem('V',V);
    AddItem('R',R);
    AddItem('Length',L);
    AddItem('P',fFlags); // expected to be written as signed integer
    AddItem('O',TPdfClearText.Create(fOwnerPassEncoded));
    AddItem('U',TPdfClearText.Create(fUserPassEncoded));
  end;
  aDoc.FTrailer.Attributes.AddItem('Encrypt',aDoc.fEncryptionObject);
end;

procedure TPdfEncryptionRC4MD5.EncodeBuffer(const BufIn; var BufOut; Count: cardinal);

// see http://www.cs.cmu.edu/~dst/Adobe/Gallery/anon21jul01-pdf-encryption.txt
// see "Algorithm 3.1 Encryption of data" in PDF Reference document
var RC4: TRC4;
  procedure ComputeNewRC4Key;

  const KEYSIZE:  array[elRC4_40..elRC4_128] of integer = (10,16);
  var MD5: TMD5;
      Digest: TMD5Digest;
  begin
    MD5.Init;
    MD5.Update(fInternalKey[0],length(fInternalKey));
    MD5.Update(fDoc.fCurrentObjectNumber,3);
    MD5.Update(fDoc.fCurrentGenerationNumber,2);
    MD5.Final(Digest);
    RC4.Init(Digest,KEYSIZE[fLevel]);
    RC4.SaveKey(fLastRC4Key); // a lot of string encodings have the same context
    fLastObjectNumber := fDoc.fCurrentObjectNumber;
    fLastGenerationNumber := fDoc.fCurrentGenerationNumber;
  end;
begin
  if (fDoc.fCurrentObjectNumber<>fLastObjectNumber) or
     (fDoc.fCurrentGenerationNumber<>fLastGenerationNumber) then
    ComputeNewRC4Key else
    RC4.RestoreKey(fLastRC4Key);
  RC4.Encrypt(BufIn,BufOut,Count); // RC4 allows in-place encryption :)
end;

{$endif USE_PDFSECURITY}