File: pdfvectorialreader.pas

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (265 lines) | stat: -rw-r--r-- 6,994 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
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
258
259
260
261
262
263
264
265
{
pdfvectorialreader.pas

Reads the vectorial information form a PDF file

PDF file format specification obtained from:

ADOBE SYSTEMS INCORPORATED. PDF Reference: AdobeĀ®
Portable Document Format. San Jose, 2006. (Sixth edition).

AUTHORS: Felipe Monteiro de Carvalho
         Pedro Sol Pegorini L de Lima
}
unit pdfvectorialreader;

{$ifdef fpc}
  {$mode delphi}
{$endif}

interface

uses
  Classes, SysUtils,
  pdfvrlexico, pdfvrsintatico, pdfvrsemantico, avisozlib,
  fpvectorial;

type

  { TvPDFVectorialReader }

  TvPDFVectorialReader = class(TvCustomVectorialReader)
  private
    procedure WriteStringToStream(AStream: TStream; AString: string);
  public
    { public to allow uncompressing PDFs independently }
    function getFirstPage(AInput: TStream; AOutput: TStream):PageHeader;
    procedure unzipPage(AInput: TStream; AOutput: TStream);
    procedure translatePage(AInput: TStream; AData: TvVectorialDocument;
              APageHeader: PageHeader);
    { General reading methods }
    procedure ReadFromStream(AStream: TStream; AData: TvVectorialDocument); override;
  end;

implementation

{ TvPDFVectorialReader }

procedure TvPDFVectorialReader.WriteStringToStream(AStream: TStream;
  AString: string);
begin
  AStream.WriteBuffer(AString[1], Length(AString));
end;

function TvPDFVectorialReader.getFirstPage(AInput: TStream; AOutput: TStream): PageHeader;
var
  mytoken: Token;
  myAnLexicoPage: AnLexico;
  myAnLexicoContents: AnLexico;
  myAnSintaticoPage: AnSintaticoPage;
  myAnSintaticoContents: AnSintaticoPageContents;
  AInput2: TStream;
begin
  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':> TvPDFVectorialReader.getFirstPage');
  {$endif}
  AInput2 := TMemoryStream.Create;
  AInput2.Size := AInput.Size;
  AInput2.CopyFrom(AInput, AInput.Size);
  AInput.Seek(0, soFromBeginning);
  AInput2.Seek(0, soFromBeginning);

  myAnLexicoPage := AnLexico.Create;
  myAnLexicoPage.Doc := AInput;
  myAnLexicoPage.bytesRemaining:= myAnLexicoPage.Doc.Size;
  myAnSintaticoPage := AnSintaticoPage.Create;

  // find first page
  while ((myAnSintaticoPage.pageFound <> true) and
                   (myAnLexicoPage.bytesRemaining > 0)) do
  begin
    mytoken := myAnLexicoPage.getToken();
    myAnSintaticoPage.automata(mytoken);
  end;

  if (myAnSintaticoPage.pageFound = false) then
  begin
    raise Exception.Create('ERROR: Arquivo corrompido.');
    Halt(1);
  end;

  AInput.Seek(0, soFromBeginning);
  myAnLexicoContents := AnLexico.Create;
  myAnLexicoContents.Doc := AInput;
  myAnLexicoContents.bytesRemaining:= myAnLexicoContents.Doc.Size;
  myAnSintaticoContents := AnSintaticoPageContents.Create;

  // gathering information of the first page
  myAnSintaticoContents.obj1:=myAnSintaticoPage.obj1;
  myAnSintaticoContents.obj2:=myAnSintaticoPage.obj2;

  //find first page contents
  while ((myAnSintaticoContents.contentsFound <> true) and
                   (myAnLexicoContents.bytesRemaining > 0)) do
  begin
    mytoken := myAnLexicoContents.getToken();
    myAnSintaticoContents.automata(mytoken, AInput2);
  end;

  if (myAnSintaticoContents.contentsFound = false) then
  begin
    raise Exception.Create('ERROR: Arquivo corrompido.');
    Halt(1);
  end;

  // gathering information of the first page
  myAnLexicoContents.bytesRemaining:=myAnSintaticoContents.h.page_length;

  // write file with content just from the first page
  while (myAnLexicoContents.bytesRemaining > 0) do
  begin
    mytoken := myAnLexicoContents.getPageToken();
    WriteStringToStream(AOutput, mytoken.token_string);
  end;

  Result:=myAnSintaticoContents.h;

  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':< TvPDFVectorialReader.getFirstPage');
  {$endif}

//  AInput2.Free;
end;

procedure TvPDFVectorialReader.unzipPage(AInput: TStream; AOutput: TStream);
var
  compr, uncompr: Pbyte;
  comprLen, uncomprLen: LongInt;
  myDecode: decode;
  BufStr: string;
begin
  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':> TvPDFVectorialReader.unzipPage');
  {$endif}
  
  myDecode := Decode.Create;

  comprLen := 10000 * SizeOf(Integer); // don't overflow
  uncomprLen := comprLen;
  GetMem(compr, comprLen);
  GetMem(uncompr, uncomprLen);

  if (compr = NIL) or (uncompr = NIL) then
     myDecode.EXIT_ERR('Out of memory');

  (* compr and uncompr are cleared to avoid reading uninitialized
   * data and to ensure that uncompr compresses well.
   *)

  FillChar(compr^, comprLen, 0);
  FillChar(uncompr^, uncomprLen, 0);

  AInput.Read(compr^, comprLen);

  BufStr := string(myDecode.test_inflate(compr, comprLen, uncompr, uncomprLen));

  WriteStringToStream(AOutput, BufStr);

  FreeMem(compr, comprLen);
  FreeMem(uncompr, uncomprLen);

  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':< TvPDFVectorialReader.unzipPage');
  {$endif}
end;

procedure TvPDFVectorialReader.translatePage(AInput: TStream;
 AData: TvVectorialDocument; APageHeader: PageHeader);
var
  myAnLexico: AnLexico;
  myAnSintaticoCommand: AnSintaticoCommand;
  myAnSemantico: AnSemantico;
  mytoken: Token;
  c: Command;
begin
  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':> TvPDFVectorialReader.translatePage');
  {$endif}

  // initialize data main
  myAnLexico := AnLexico.Create;
  myAnLexico.Doc := AInput;
  myAnLexico.bytesRemaining:= myAnLexico.Doc.Size;
  myAnSintaticoCommand := AnSintaticoCommand.Create;
  myAnSemantico := AnSemantico.Create;

  // initialize machine
  myAnSemantico.startMachine();

  while (myAnLexico.bytesRemaining > 0) do
  begin
    mytoken := myAnLexico.getToken();
    c:=myAnSintaticoCommand.automata(mytoken);
    if (myAnSintaticoCommand.Codigo = true) then
      myAnSemantico.generate(c, AData);
  end;

  // end machine
  myAnSemantico.endMachine();
end;

procedure TvPDFVectorialReader.ReadFromStream(AStream: TStream;
  AData: TvVectorialDocument);
var
  APageHeader: PageHeader;
  APageStream, AUnzipStream: TStream;
begin
  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':> TvPDFVectorialReader.ReadFromStream');
  {$endif}

  APageStream := TMemoryStream.Create;
  AUnzipStream := TMemoryStream.Create;

  // get first page
  APageHeader := getFirstPage(AStream, APageStream);

  // unzip page
  if (APageHeader.flate_decode = true) then
  begin
    APageStream.Seek(0, soFromBeginning);
    unzipPage(APageStream, AUnzipStream);

    // translate page to doc data
    AUnzipStream.Seek(0, soFromBeginning);
    translatePage(AUnzipStream, AData, APageHeader);
  end
  else
  begin
    // translate page to doc data
    APageStream.Seek(0, soFromBeginning);
    translatePage(APageStream, AData, APageHeader);
  end;

  APageStream.Free;
  AUnzipStream.Free;

  //ShowMessage('Sucesso!');
  {$ifdef FPVECTORIALDEBUG}
  WriteLn(':< TvPDFVectorialReader.ReadFromStream');
  WriteLn('Sucesso!');
  {$endif}
end;

{*******************************************************************
*  Initialization section
*
*  Registers this reader / writer on fpVectorial
*
*******************************************************************}
initialization

  RegisterVectorialReader(TvPDFVectorialReader, vfPDF);

end.