4
Reads the vectorial information form a PDF file
6
PDF file format specification obtained from:
8
ADOBE SYSTEMS INCORPORATED. PDF Reference: Adobe®
9
Portable Document Format. San Jose, 2006. (Sixth edition).
11
AUTHORS: Felipe Monteiro de Carvalho
12
Pedro Sol Pegorini L de Lima
14
unit pdfvectorialreader;
24
pdfvrlexico, pdfvrsintatico, pdfvrsemantico, avisozlib,
29
{ TvPDFVectorialReader }
31
TvPDFVectorialReader = class(TvCustomVectorialReader)
33
procedure WriteStringToStream(AStream: TStream; AString: string);
35
{ public to allow uncompressing PDFs independently }
36
function getFirstPage(AInput: TStream; AOutput: TStream):PageHeader;
37
procedure unzipPage(AInput: TStream; AOutput: TStream);
38
procedure translatePage(AInput: TStream; AData: TvVectorialDocument;
39
APageHeader: PageHeader);
40
{ General reading methods }
41
procedure ReadFromStream(AStream: TStream; AData: TvVectorialDocument); override;
46
{ TvPDFVectorialReader }
48
procedure TvPDFVectorialReader.WriteStringToStream(AStream: TStream;
51
AStream.WriteBuffer(AString[1], Length(AString));
54
function TvPDFVectorialReader.getFirstPage(AInput: TStream; AOutput: TStream): PageHeader;
57
myAnLexicoPage: AnLexico;
58
myAnLexicoContents: AnLexico;
59
myAnSintaticoPage: AnSintaticoPage;
60
myAnSintaticoContents: AnSintaticoPageContents;
63
{$ifdef FPVECTORIALDEBUG}
64
WriteLn(':> TvPDFVectorialReader.getFirstPage');
66
AInput2 := TMemoryStream.Create;
67
AInput2.Size := AInput.Size;
68
AInput2.CopyFrom(AInput, AInput.Size);
69
AInput.Seek(0, soFromBeginning);
70
AInput2.Seek(0, soFromBeginning);
72
myAnLexicoPage := AnLexico.Create;
73
myAnLexicoPage.Doc := AInput;
74
myAnLexicoPage.bytesRemaining:= myAnLexicoPage.Doc.Size;
75
myAnSintaticoPage := AnSintaticoPage.Create;
78
while ((myAnSintaticoPage.pageFound <> true) and
79
(myAnLexicoPage.bytesRemaining > 0)) do
81
mytoken := myAnLexicoPage.getToken();
82
myAnSintaticoPage.automata(mytoken);
85
if (myAnSintaticoPage.pageFound = false) then
87
raise Exception.Create('ERROR: Arquivo corrompido.');
91
AInput.Seek(0, soFromBeginning);
92
myAnLexicoContents := AnLexico.Create;
93
myAnLexicoContents.Doc := AInput;
94
myAnLexicoContents.bytesRemaining:= myAnLexicoContents.Doc.Size;
95
myAnSintaticoContents := AnSintaticoPageContents.Create;
97
// gathering information of the first page
98
myAnSintaticoContents.obj1:=myAnSintaticoPage.obj1;
99
myAnSintaticoContents.obj2:=myAnSintaticoPage.obj2;
101
//find first page contents
102
while ((myAnSintaticoContents.contentsFound <> true) and
103
(myAnLexicoContents.bytesRemaining > 0)) do
105
mytoken := myAnLexicoContents.getToken();
106
myAnSintaticoContents.automata(mytoken, AInput2);
109
if (myAnSintaticoContents.contentsFound = false) then
111
raise Exception.Create('ERROR: Arquivo corrompido.');
115
// gathering information of the first page
116
myAnLexicoContents.bytesRemaining:=myAnSintaticoContents.h.page_length;
118
// write file with content just from the first page
119
while (myAnLexicoContents.bytesRemaining > 0) do
121
mytoken := myAnLexicoContents.getPageToken();
122
WriteStringToStream(AOutput, mytoken.token_string);
125
Result:=myAnSintaticoContents.h;
127
{$ifdef FPVECTORIALDEBUG}
128
WriteLn(':< TvPDFVectorialReader.getFirstPage');
134
procedure TvPDFVectorialReader.unzipPage(AInput: TStream; AOutput: TStream);
136
compr, uncompr: Pbyte;
137
comprLen, uncomprLen: LongInt;
141
{$ifdef FPVECTORIALDEBUG}
142
WriteLn(':> TvPDFVectorialReader.unzipPage');
145
myDecode := Decode.Create;
147
comprLen := 10000 * SizeOf(Integer); // don't overflow
148
uncomprLen := comprLen;
149
GetMem(compr, comprLen);
150
GetMem(uncompr, uncomprLen);
152
if (compr = NIL) or (uncompr = NIL) then
153
myDecode.EXIT_ERR('Out of memory');
155
(* compr and uncompr are cleared to avoid reading uninitialized
156
* data and to ensure that uncompr compresses well.
159
FillChar(compr^, comprLen, 0);
160
FillChar(uncompr^, uncomprLen, 0);
162
AInput.Read(compr^, comprLen);
164
BufStr := string(myDecode.test_inflate(compr, comprLen, uncompr, uncomprLen));
166
WriteStringToStream(AOutput, BufStr);
168
FreeMem(compr, comprLen);
169
FreeMem(uncompr, uncomprLen);
171
{$ifdef FPVECTORIALDEBUG}
172
WriteLn(':< TvPDFVectorialReader.unzipPage');
176
procedure TvPDFVectorialReader.translatePage(AInput: TStream;
177
AData: TvVectorialDocument; APageHeader: PageHeader);
179
myAnLexico: AnLexico;
180
myAnSintaticoCommand: AnSintaticoCommand;
181
myAnSemantico: AnSemantico;
185
{$ifdef FPVECTORIALDEBUG}
186
WriteLn(':> TvPDFVectorialReader.translatePage');
189
// initialize data main
190
myAnLexico := AnLexico.Create;
191
myAnLexico.Doc := AInput;
192
myAnLexico.bytesRemaining:= myAnLexico.Doc.Size;
193
myAnSintaticoCommand := AnSintaticoCommand.Create;
194
myAnSemantico := AnSemantico.Create;
196
// initialize machine
197
myAnSemantico.startMachine();
199
while (myAnLexico.bytesRemaining > 0) do
201
mytoken := myAnLexico.getToken();
202
c:=myAnSintaticoCommand.automata(mytoken);
203
if (myAnSintaticoCommand.Codigo = true) then
204
myAnSemantico.generate(c, AData);
208
myAnSemantico.endMachine();
211
procedure TvPDFVectorialReader.ReadFromStream(AStream: TStream;
212
AData: TvVectorialDocument);
214
APageHeader: PageHeader;
215
APageStream, AUnzipStream: TStream;
217
{$ifdef FPVECTORIALDEBUG}
218
WriteLn(':> TvPDFVectorialReader.ReadFromStream');
221
APageStream := TMemoryStream.Create;
222
AUnzipStream := TMemoryStream.Create;
225
APageHeader := getFirstPage(AStream, APageStream);
228
if (APageHeader.flate_decode = true) then
230
APageStream.Seek(0, soFromBeginning);
231
unzipPage(APageStream, AUnzipStream);
233
// translate page to doc data
234
AUnzipStream.Seek(0, soFromBeginning);
235
translatePage(AUnzipStream, AData, APageHeader);
239
// translate page to doc data
240
APageStream.Seek(0, soFromBeginning);
241
translatePage(APageStream, AData, APageHeader);
247
//ShowMessage('Sucesso!');
248
{$ifdef FPVECTORIALDEBUG}
249
WriteLn(':< TvPDFVectorialReader.ReadFromStream');
254
{*******************************************************************
255
* Initialization section
257
* Registers this reader / writer on fpVectorial
259
*******************************************************************}
262
RegisterVectorialReader(TvPDFVectorialReader, vfPDF);