~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/fpvectorial/pdfvectorialreader.pas

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
pdfvectorialreader.pas
 
3
 
 
4
Reads the vectorial information form a PDF file
 
5
 
 
6
PDF file format specification obtained from:
 
7
 
 
8
ADOBE SYSTEMS INCORPORATED. PDF Reference: Adobe®
 
9
Portable Document Format. San Jose, 2006. (Sixth edition).
 
10
 
 
11
AUTHORS: Felipe Monteiro de Carvalho
 
12
         Pedro Sol Pegorini L de Lima
 
13
}
 
14
unit pdfvectorialreader;
 
15
 
 
16
{$ifdef fpc}
 
17
  {$mode delphi}
 
18
{$endif}
 
19
 
 
20
interface
 
21
 
 
22
uses
 
23
  Classes, SysUtils,
 
24
  pdfvrlexico, pdfvrsintatico, pdfvrsemantico, avisozlib,
 
25
  fpvectorial;
 
26
 
 
27
type
 
28
 
 
29
  { TvPDFVectorialReader }
 
30
 
 
31
  TvPDFVectorialReader = class(TvCustomVectorialReader)
 
32
  private
 
33
    procedure WriteStringToStream(AStream: TStream; AString: string);
 
34
  public
 
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;
 
42
  end;
 
43
 
 
44
implementation
 
45
 
 
46
{ TvPDFVectorialReader }
 
47
 
 
48
procedure TvPDFVectorialReader.WriteStringToStream(AStream: TStream;
 
49
  AString: string);
 
50
begin
 
51
  AStream.WriteBuffer(AString[1], Length(AString));
 
52
end;
 
53
 
 
54
function TvPDFVectorialReader.getFirstPage(AInput: TStream; AOutput: TStream): PageHeader;
 
55
var
 
56
  mytoken: Token;
 
57
  myAnLexicoPage: AnLexico;
 
58
  myAnLexicoContents: AnLexico;
 
59
  myAnSintaticoPage: AnSintaticoPage;
 
60
  myAnSintaticoContents: AnSintaticoPageContents;
 
61
  AInput2: TStream;
 
62
begin
 
63
  {$ifdef FPVECTORIALDEBUG}
 
64
  WriteLn(':> TvPDFVectorialReader.getFirstPage');
 
65
  {$endif}
 
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);
 
71
 
 
72
  myAnLexicoPage := AnLexico.Create;
 
73
  myAnLexicoPage.Doc := AInput;
 
74
  myAnLexicoPage.bytesRemaining:= myAnLexicoPage.Doc.Size;
 
75
  myAnSintaticoPage := AnSintaticoPage.Create;
 
76
 
 
77
  // find first page
 
78
  while ((myAnSintaticoPage.pageFound <> true) and
 
79
                   (myAnLexicoPage.bytesRemaining > 0)) do
 
80
  begin
 
81
    mytoken := myAnLexicoPage.getToken();
 
82
    myAnSintaticoPage.automata(mytoken);
 
83
  end;
 
84
 
 
85
  if (myAnSintaticoPage.pageFound = false) then
 
86
  begin
 
87
    raise Exception.Create('ERROR: Arquivo corrompido.');
 
88
    Halt(1);
 
89
  end;
 
90
 
 
91
  AInput.Seek(0, soFromBeginning);
 
92
  myAnLexicoContents := AnLexico.Create;
 
93
  myAnLexicoContents.Doc := AInput;
 
94
  myAnLexicoContents.bytesRemaining:= myAnLexicoContents.Doc.Size;
 
95
  myAnSintaticoContents := AnSintaticoPageContents.Create;
 
96
 
 
97
  // gathering information of the first page
 
98
  myAnSintaticoContents.obj1:=myAnSintaticoPage.obj1;
 
99
  myAnSintaticoContents.obj2:=myAnSintaticoPage.obj2;
 
100
 
 
101
  //find first page contents
 
102
  while ((myAnSintaticoContents.contentsFound <> true) and
 
103
                   (myAnLexicoContents.bytesRemaining > 0)) do
 
104
  begin
 
105
    mytoken := myAnLexicoContents.getToken();
 
106
    myAnSintaticoContents.automata(mytoken, AInput2);
 
107
  end;
 
108
 
 
109
  if (myAnSintaticoContents.contentsFound = false) then
 
110
  begin
 
111
    raise Exception.Create('ERROR: Arquivo corrompido.');
 
112
    Halt(1);
 
113
  end;
 
114
 
 
115
  // gathering information of the first page
 
116
  myAnLexicoContents.bytesRemaining:=myAnSintaticoContents.h.page_length;
 
117
 
 
118
  // write file with content just from the first page
 
119
  while (myAnLexicoContents.bytesRemaining > 0) do
 
120
  begin
 
121
    mytoken := myAnLexicoContents.getPageToken();
 
122
    WriteStringToStream(AOutput, mytoken.token_string);
 
123
  end;
 
124
 
 
125
  Result:=myAnSintaticoContents.h;
 
126
 
 
127
  {$ifdef FPVECTORIALDEBUG}
 
128
  WriteLn(':< TvPDFVectorialReader.getFirstPage');
 
129
  {$endif}
 
130
 
 
131
//  AInput2.Free;
 
132
end;
 
133
 
 
134
procedure TvPDFVectorialReader.unzipPage(AInput: TStream; AOutput: TStream);
 
135
var
 
136
  compr, uncompr: Pbyte;
 
137
  comprLen, uncomprLen: LongInt;
 
138
  myDecode: decode;
 
139
  BufStr: string;
 
140
begin
 
141
  {$ifdef FPVECTORIALDEBUG}
 
142
  WriteLn(':> TvPDFVectorialReader.unzipPage');
 
143
  {$endif}
 
144
  
 
145
  myDecode := Decode.Create;
 
146
 
 
147
  comprLen := 10000 * SizeOf(Integer); // don't overflow
 
148
  uncomprLen := comprLen;
 
149
  GetMem(compr, comprLen);
 
150
  GetMem(uncompr, uncomprLen);
 
151
 
 
152
  if (compr = NIL) or (uncompr = NIL) then
 
153
     myDecode.EXIT_ERR('Out of memory');
 
154
 
 
155
  (* compr and uncompr are cleared to avoid reading uninitialized
 
156
   * data and to ensure that uncompr compresses well.
 
157
   *)
 
158
 
 
159
  FillChar(compr^, comprLen, 0);
 
160
  FillChar(uncompr^, uncomprLen, 0);
 
161
 
 
162
  AInput.Read(compr^, comprLen);
 
163
 
 
164
  BufStr := string(myDecode.test_inflate(compr, comprLen, uncompr, uncomprLen));
 
165
 
 
166
  WriteStringToStream(AOutput, BufStr);
 
167
 
 
168
  FreeMem(compr, comprLen);
 
169
  FreeMem(uncompr, uncomprLen);
 
170
 
 
171
  {$ifdef FPVECTORIALDEBUG}
 
172
  WriteLn(':< TvPDFVectorialReader.unzipPage');
 
173
  {$endif}
 
174
end;
 
175
 
 
176
procedure TvPDFVectorialReader.translatePage(AInput: TStream;
 
177
 AData: TvVectorialDocument; APageHeader: PageHeader);
 
178
var
 
179
  myAnLexico: AnLexico;
 
180
  myAnSintaticoCommand: AnSintaticoCommand;
 
181
  myAnSemantico: AnSemantico;
 
182
  mytoken: Token;
 
183
  c: Command;
 
184
begin
 
185
  {$ifdef FPVECTORIALDEBUG}
 
186
  WriteLn(':> TvPDFVectorialReader.translatePage');
 
187
  {$endif}
 
188
 
 
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;
 
195
 
 
196
  // initialize machine
 
197
  myAnSemantico.startMachine();
 
198
 
 
199
  while (myAnLexico.bytesRemaining > 0) do
 
200
  begin
 
201
    mytoken := myAnLexico.getToken();
 
202
    c:=myAnSintaticoCommand.automata(mytoken);
 
203
    if (myAnSintaticoCommand.Codigo = true) then
 
204
      myAnSemantico.generate(c, AData);
 
205
  end;
 
206
 
 
207
  // end machine
 
208
  myAnSemantico.endMachine();
 
209
end;
 
210
 
 
211
procedure TvPDFVectorialReader.ReadFromStream(AStream: TStream;
 
212
  AData: TvVectorialDocument);
 
213
var
 
214
  APageHeader: PageHeader;
 
215
  APageStream, AUnzipStream: TStream;
 
216
begin
 
217
  {$ifdef FPVECTORIALDEBUG}
 
218
  WriteLn(':> TvPDFVectorialReader.ReadFromStream');
 
219
  {$endif}
 
220
 
 
221
  APageStream := TMemoryStream.Create;
 
222
  AUnzipStream := TMemoryStream.Create;
 
223
 
 
224
  // get first page
 
225
  APageHeader := getFirstPage(AStream, APageStream);
 
226
 
 
227
  // unzip page
 
228
  if (APageHeader.flate_decode = true) then
 
229
  begin
 
230
    APageStream.Seek(0, soFromBeginning);
 
231
    unzipPage(APageStream, AUnzipStream);
 
232
 
 
233
    // translate page to doc data
 
234
    AUnzipStream.Seek(0, soFromBeginning);
 
235
    translatePage(AUnzipStream, AData, APageHeader);
 
236
  end
 
237
  else
 
238
  begin
 
239
    // translate page to doc data
 
240
    APageStream.Seek(0, soFromBeginning);
 
241
    translatePage(APageStream, AData, APageHeader);
 
242
  end;
 
243
 
 
244
  APageStream.Free;
 
245
  AUnzipStream.Free;
 
246
 
 
247
  //ShowMessage('Sucesso!');
 
248
  {$ifdef FPVECTORIALDEBUG}
 
249
  WriteLn(':< TvPDFVectorialReader.ReadFromStream');
 
250
  WriteLn('Sucesso!');
 
251
  {$endif}
 
252
end;
 
253
 
 
254
{*******************************************************************
 
255
*  Initialization section
 
256
*
 
257
*  Registers this reader / writer on fpVectorial
 
258
*
 
259
*******************************************************************}
 
260
initialization
 
261
 
 
262
  RegisterVectorialReader(TvPDFVectorialReader, vfPDF);
 
263
 
 
264
end.
 
265