-
Notifications
You must be signed in to change notification settings - Fork 1
/
pdodump.lpr
263 lines (231 loc) · 6.76 KB
/
pdodump.lpr
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
program pdodump;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}
cthreads, cmem,
{$ENDIF}
sysutils, classes, crc,
gzip_format, dc2_simple_api, utils, hpdf, progress_report,
pdo_format, pdo_common, pdo_writer, pdo2obj, pdo2opf, opf2vector;
const
DefaultDpi = 150;
ImageDumpPath = 'r:\';
type
TCliParams = record
detailedTime: boolean;
dumpStructure: boolean;
dumpPng: boolean;
multifile: boolean;
end;
var
g_params: TCliParams;
function optSet(const c: char): boolean;
begin
result := (Paramcount > 1) and (Pos(c, ParamStr(2)) <> 0)
end;
{ ExportPatterns
Export 2d patterns from the pdo data as either SVG or PDF files.
}
procedure ExportPatterns(const pdo: TPdoParser; const name: string;
const exportFormat: TVectorExportFormat);
var
log: T2dTransformPartProgressLog;
transform: PdoToOpf2dTransform;
pdo_struct: TPdoStructure;
opt: TVectorExportOptions;
vec_export: TOpf2dVectorExport;
t: QWord;
procedure TimedProc(const procName: string);
begin
t := GetMsecs;
writeln(procName);
end;
procedure EndTimer();
begin
if not g_params.detailedTime then exit;
t := GetMsecs - t;
writeln(Format(' took %.2fs', [t / 1000]));
end;
begin
if exportFormat = TvfSvg then begin
writeln('exporting to svg file: ', name);
end else begin
writeln('exporting to pdf file: ', name);
if LoadHaru then
writeln('using libHaru ', HPDF_GetVersion)
else begin
writeln('Couldn''t load libHaru!');
exit;
end;
end;
TimedProc('transforming parts to 2D');
pdo_struct := pdo.GetStructure;
log := T2dTransformPartProgressLog.Create;
transform := PdoToOpf2dTransform.Create(pdo_struct, log);
transform.PartsTo2D;
EndTimer;
TimedProc('rasterization preparation');
transform.RasterizationBegin;
EndTimer;
TimedProc('rasterizing and compressing');
transform.RasterizeToPngStream(DefaultDpi);
transform.RasterizationEnd;
EndTimer;
if g_params.dumpPng then
transform.DumpPng(ImageDumpPath);
TimedProc('output writing');
DefaultVectorExportOptions(opt);
opt.tabs := pdo_struct.settings.show_flaps = 1;
opt.faces := exportFormat = TvfSvg;
opt.multi_file := g_params.multifile;
//opt.debug_edges := true;
vec_export := TOpf2dVectorExport.Create(pdo_struct, transform.GetParts, transform.GetPageSetup);
vec_export.Prepare(opt);
vec_export.ExportToFile(name, exportFormat);
vec_export.Free;
EndTimer;
log.Free;
transform.Free;
end;
{ Gzcompress
Compress SVG with Gzip, aka SVGZ.
}
procedure Gzcompress(const svg_name, svgz_name: string);
var
svg, svgz: TMemoryStream;
svgz_file: file;
checksum: longword;
encoder: TLzEncoder;
begin
writeln('compress to svgz file: ', svgz_name);
AssignFile(svgz_file, svgz_name);
Rewrite(svgz_file, 1);
gzf_write_header(svgz_file, svg_name);
svg := TMemoryStream.Create;
svg.LoadFromFile(svg_name);
svgz := TMemoryStream.Create;
encoder := TLzEncoder.Create(1);
encoder.EncodeBytesToStream(svg.Memory, svg.Size, svgz);
encoder.Free;
svg.Free;
checksum := crc32(0, nil, 0);
checksum := crc32(checksum, svgz.Memory, svgz.Size);
BlockWrite(svgz_file, svgz.Memory^, svgz.Size);
gzf_write_end(svgz_file, svgz.Size, checksum);
CloseFile(svgz_file);
svgz.Free;
end;
{ ExportMesh
Export the 3d mesh from the pdo data.
}
procedure ExportMesh(const name: string; const pdo: TPdoParser);
var
opt: TObjExportOptions;
begin
writeln('exporting obj: ', name);
opt.normalize := true;
opt.flip_v_coordinate := false;
WriteObj(pdo.GetStructure, name, opt);
end;
{ RewritePdo
Write a copy of the unmodified input file using the PDO writing functions
}
procedure RewritePdo(const name: string; const pdo: TPdoParser);
begin
writeln('rewriting: ', name);
WritePdo(pdo.GetStructure, name);
end;
{ Main
}
var
infile: string;
pdo: TPdoParser;
fname: string;
t_parsing, t_processing, t_recompress: QWord;
begin
if Paramcount < 1 then begin
writeln('no input file specified!');
writeln('usage: pdodump file [params] [output name]');
writeln('export parameters:');
writeln(' d - dump pdo structure');
writeln(' e - resave pdo file');
writeln(' o - output obj');
writeln(' f - output pdf');
writeln(' s - output svg');
writeln('optional:');
writeln(' m - export each page separately');
writeln(' p - dump rasterized textures to png');
writeln(' t - print elapsed time for 2D export subparts');
writeln(' w - recompress textures to save size when resaving pdo');
writeln(' z - compress the svg output to svgz');
writeln(' x - dump pdo textures to pnm');
halt;
end;
infile := ParamStr(1);
writeln('file: ', infile);
if not FileExists(infile) then begin
writeln('file doesn''t exist!');
halt;
end;
g_params.dumpStructure := false;
if Paramcount > 1 then
g_params.dumpStructure := optSet('d');
t_parsing := GetMsecs;
pdo := TPdoParser.Create(g_params.dumpStructure);
try
pdo.OpenFile(infile);
except
on e: Exception do begin
writeln(e.Message);
writeln('could not open file, ending');
halt;
end;
end;
pdo.Load;
t_parsing := GetMsecs - t_parsing;
writeln('parsing time: ', (t_parsing / 1000):5:2);
//process data
t_processing := GetMsecs;
if Paramcount > 1 then begin
fname := ExtractFilePath(infile);
if fname <> '' then
fname += DirectorySeparator;
if Paramcount > 2 then
fname += ParamStr(3)
else
fname += ExtractFileName(ParamStr(1));
g_params.dumpPng := optSet('p');
g_params.multifile := optSet('m');
g_params.detailedTime := optSet('t');
if optSet('o') then
ExportMesh(fname + '.obj', pdo);
if optSet('s') then begin
ExportPatterns(pdo, fname, TvfSvg);
if optSet('z') then begin
Gzcompress(fname + '.svg', fname + '.svgz');
DeleteFile(fname + '.svg');
end;
end;
if optSet('f') then
ExportPatterns(pdo, fname, TvfPdf);
if optSet('e') then begin
if optSet('w') then begin
t_recompress := GetMsecs;
pdo.GetStructure.tex_storage.Recompress;
t_recompress := GetMsecs - t_recompress;
writeln('texture recompress time: ', (t_recompress / 1000):5:2);
end;
RewritePdo(fname + '_new.pdo', pdo);
end;
if optSet('x') then begin
if DirectoryExists(ImageDumpPath) then
pdo.GetStructure.tex_storage.DumpTextures(ImageDumpPath)
else
writeln('target path does not exist: ', ImageDumpPath);
end;
end;
t_processing := GetMsecs - t_processing;
writeln('processing time: ', (t_processing / 1000):5:2);
pdo.Free;
writeln('done');
end.