47
static void ipeDebugImpl(const char *msg)
49
OutputDebugStringA(msg);
52
static void ipeDebugImpl(const char *)
54
// fprintf(stderr, "%s\n", msg);
58
static int topdforps(IpeDocument *doc, const char *src, const char *dst,
59
IpeDocument::TFormat fm, uint flags,
60
int fromPage = -1, int toPage = -1)
62
if (fm == IpeDocument::EEps && doc->totalViews() > 1) {
64
"The document contains %d views, cannot convert to EPS format.\n",
69
fprintf(stderr, "Document %s has %d pages (views)\n",
70
src, doc->totalViews());
72
int exitCode = IpeModel::RunLatex(doc);
75
if (fm == IpeDocument::EEps && doc->HasTrueTypeFonts()) {
36
static int topdforps(Document *doc, String src, String dst,
37
Document::TFormat fm, uint flags,
38
int fromPage = -1, int toPage = -1, int viewNo = -1)
40
if (fm == Document::EEps) {
41
if (viewNo < 0 && doc->countTotalViews() > 1) {
42
fprintf(stderr, "The document contains %d views, "
43
"cannot convert to EPS format.\n", doc->countTotalViews());
47
bool trans = doc->hasTransparency();
48
bool grad = doc->hasGradients();
50
if (!trans && !grad) {
51
int res = doc->runLatex();
56
bool ttf = doc->hasTrueTypeFonts();
58
if (trans || grad || ttf) {
59
fprintf(stderr, "The document uses the following features:\n");
61
fprintf(stderr, " - transparency\n");
63
fprintf(stderr, " - gradients\n");
65
fprintf(stderr, " - Truetype fonts\n");
77
"The document contains TrueType fonts.\n"
78
"Ipe cannot save these in Postscript format.\n"
67
"Ipe does not support these features for Postscript output.\n"
79
68
"You can save as PDF, and use pdftops to create a "
80
69
"Postscript file.\n");
81
} else if (!doc->Save(dst, "ipetoipe", fm, flags, fromPage, toPage))
82
fprintf(stderr, "Failed to save document!\n");
83
else if (flags & IpeDocument::EExport)
84
fprintf(stderr, "Warning: the exported file contains no Ipe markup!\n");
89
static void recenterPages(IpeDocument *doc)
91
for (int pg = 0; pg < doc->pages(); ++pg) {
92
IpePage *page = doc->page(pg);
94
// compute bounding box
95
IpeBBoxPainter bboxPainter(doc->StyleSheet());
96
for (IpePage::const_iterator it = page->begin(); it != page->end(); ++it)
97
it->Object()->Draw(bboxPainter);
98
IpeVector mid = 0.5 * (bboxPainter.BBox().Min() + bboxPainter.BBox().Max());
99
IpeRect p = doc->layout().paper();
100
IpeVector midPaper = 0.5 * (p.Min() + p.Max());
101
IpeMatrix m(midPaper - mid);
102
for (IpePage::iterator it = page->begin(); it != page->end(); ++it)
73
int res = doc->runLatex();
79
result = doc->exportView(dst.z(), fm, flags, fromPage, viewNo);
80
} else if (toPage >= 0) {
81
result = doc->exportPages(dst.z(), flags, fromPage, toPage);
83
result = doc->save(dst.z(), fm, flags);
86
fprintf(stderr, "Failed to save or export document!\n");
90
if (flags & Document::EExport)
91
fprintf(stderr, "Warning: the exported file contains no Ipe markup.\n"
92
"It cannot be read by Ipe - make sure you keep the original!\n");
107
96
static void usage()
110
"Usage: ipetoipe ( -xml | -eps | -pdf ) [ -export ] "
111
"[ -from <page> ] [ -to <page> ] "
112
"[ -noshading ] [ -lastview ] [ -runlatex ] [ -nocolor ] "
99
"Usage: ipetoipe ( -xml | -eps | -pdf ) <options> "
100
"infile [ outfile ]\n"
115
101
"Ipetoipe converts between the different Ipe file formats.\n"
116
" -export : output contains no Ipe markup.\n"
117
" -from : start exporting at this page.\n"
118
" -to : stop exporting at this page.\n"
119
" -lastview : only the last view of each Ipe page is saved.\n"
120
" -runlatex : run Latex even for XML output.\n"
121
" -nocolor : avoid any color commands in EPS output.\n"
102
" -export : output contains no Ipe markup.\n"
103
" -pages <n-m> : export only these pages (implies -export).\n"
104
" -view <p-v> : export only this view (implies -export).\n"
106
": export only last view of each page (implies -export).\n"
107
" -runlatex : run Latex even for XML output.\n"
108
" -nocolor : avoid any color commands in EPS output.\n"
109
" -nozip: : do not compress PDF streams.\n"
122
110
" -styleupdate : update style sheets.\n"
127
115
int main(int argc, char *argv[])
129
setlocale(LC_NUMERIC, "C");
131
// Check Ipelib version mismatch
132
if (IpelibVersion() != IPELIB_VERSION) {
134
"Ipetoipe has been compiled with header files for Ipelib %d\n"
135
"but is dynamically linked against libipe %d.\n"
136
"Check with 'ldd ipetoipe' which libipe is being loaded, and "
137
"replace it by the correct version or set LD_LIBRARY_PATH.\n",
138
IPELIB_VERSION, IpelibVersion());
142
// set ipeDebug handler
143
ipeDebugHandler = ipeDebugImpl;
145
// ensure at least three arguments (handles -help as well :-)
149
IpeDocument::TFormat frm = IpeDocument::EUnknown;
150
if (!qstrcmp(argv[1], "-xml"))
151
frm = IpeDocument::EXml;
152
else if (!qstrcmp(argv[1], "-eps"))
153
frm = IpeDocument::EEps;
154
else if (!qstrcmp(argv[1], "-pdf"))
155
frm = IpeDocument::EPdf;
157
if (frm == IpeDocument::EUnknown)
160
uint flags = IpeDocument::ESaveNormal;
117
ipe::Platform::initLib(ipe::IPELIB_VERSION);
119
// ensure at least two arguments (handles -help as well :-)
123
Document::TFormat frm = Document::EUnknown;
124
if (!strcmp(argv[1], "-xml"))
125
frm = Document::EXml;
126
else if (!strcmp(argv[1], "-eps"))
127
frm = Document::EEps;
128
else if (!strcmp(argv[1], "-pdf"))
129
frm = Document::EPdf;
131
if (frm == Document::EUnknown)
134
uint flags = Document::ESaveNormal;
161
135
bool runLatex = false;
162
136
bool styleUpdate = false;
163
bool recenter = false;
164
137
int fromPage = -1;
167
if (!qstrcmp(argv[i], "-export")) {
168
flags |= IpeDocument::EExport;
172
if (!qstrcmp(argv[i], "-from")) {
173
if (sscanf(argv[i+1], "%d", &fromPage) != 1)
179
if (!qstrcmp(argv[i], "-to")) {
180
if (sscanf(argv[i+1], "%d", &toPage) != 1)
186
if (!qstrcmp(argv[i], "-noshading")) {
187
flags |= IpeDocument::ENoShading;
191
if (!qstrcmp(argv[i], "-lastview")) {
192
flags |= IpeDocument::ELastView;
196
if (!qstrcmp(argv[i], "-runlatex")) {
201
if (!qstrcmp(argv[i], "-nocolor")) {
202
flags |= IpeDocument::ENoColor;
206
if (!qstrcmp(argv[i], "-styleupdate")) {
211
if (!qstrcmp(argv[i], "-recenter")) {
216
// remaining arguments must be two filenames
147
if (!strcmp(argv[i], "-export")) {
148
flags |= Document::EExport;
150
} else if (!strcmp(argv[i], "-view")) {
151
if (sscanf(argv[i+1], "%d-%d", &fromPage, &viewNo) != 2)
153
flags |= Document::EExport;
155
} else if (!strcmp(argv[i], "-pages")) {
156
if (sscanf(argv[i+1], "%d-%d", &fromPage, &toPage) != 2)
158
flags |= Document::EExport;
160
} else if (!strcmp(argv[i], "-lastview")) {
161
flags |= Document::ELastView;
162
flags |= Document::EExport;
164
} else if (!strcmp(argv[i], "-runlatex")) {
167
} else if (!strcmp(argv[i], "-nocolor")) {
168
flags |= Document::ENoColor;
170
} else if (!strcmp(argv[i], "-nozip")) {
171
flags |= Document::ENoZip;
173
} else if (!strcmp(argv[i], "-styleupdate")) {
177
// last one or two arguments must be filenames
220
if ((flags & IpeDocument::EExport) && frm == IpeDocument::EXml) {
192
if ((flags & Document::EExport) && frm == Document::EXml) {
221
193
fprintf(stderr, "-export only available with -pdf and -eps.\n");
225
if ((fromPage >= 0 || toPage >= 0) && !(flags & IpeDocument::EExport)) {
226
fprintf(stderr, "-from and -to only available with -export.\n");
231
IpeAutoPtr<IpeDocument> doc(IpeDocument::New(argv[i], reason));
234
fprintf(stderr, "Could not read Ipe file '%s' (reason %d)\n",
197
if (toPage >= 0 && frm != Document::EPdf) {
198
fprintf(stderr, "-pages only available with -pdf.\n");
202
if (toPage >= 0 && viewNo >= 0) {
203
fprintf(stderr, "cannot specify both -pages and -view.\n");
207
if (outfile.empty()) {
209
String ext = infile.right(4);
210
if (ext == ".ipe" || ext == ".pdf" || ext == ".xml" || ext == ".eps")
211
outfile = infile.left(infile.size() - 4);
224
if (outfile == infile) {
225
fprintf(stderr, "Cannot guess output filename.\n");
230
IpeAutoPtr<Document> doc(Document::loadWithErrorReport(infile.z()));
239
235
if (styleUpdate) {
240
fprintf(stderr, "%s",
241
IpeModel::UpdateStyleSheets(doc.Ptr(), QDir::current()).CString());
245
recenterPages(doc.Ptr());
236
String curdir = ipe::Platform::currentDirectory();
237
fprintf(stderr, "%s", doc.ptr()->cascade()->update(curdir).z());
240
fprintf(stderr, "Document %s has %d pages (%d views)\n",
241
infile.z(), doc->countPages(), doc->countTotalViews());
243
// check frompage, topage, viewno
245
if (fromPage <= 0 || fromPage > toPage || toPage > doc->countPages()) {
246
fprintf(stderr, "incorrect -pages specification.\n");
251
} else if (fromPage >= 0) {
252
if (fromPage <= 0 || fromPage > doc->countPages() ||
253
viewNo <= 0 || viewNo > doc->page(fromPage - 1)->countViews()) {
254
fprintf(stderr, "incorrect -view specification.\n");
262
sprintf(buf, "ipetoipe %d.%d.%d",
263
ipe::IPELIB_VERSION / 10000,
264
(ipe::IPELIB_VERSION / 100) % 100,
265
ipe::IPELIB_VERSION % 100);
266
Document::SProperties props = doc->properties();
267
props.iCreator = buf;
268
doc->setProperties(props);
248
case IpeDocument::EXml:
250
return topdforps(doc.Ptr(), argv[i], argv[i+1],
251
IpeDocument::EXml, flags);
252
doc->Save(argv[i+1], "ipetoipe", IpeDocument::EXml,
253
IpeDocument::ESaveNormal);
273
return topdforps(doc.ptr(), infile, outfile, Document::EXml, flags);
275
doc->save(outfile.z(), Document::EXml, Document::ESaveNormal);
257
case IpeDocument::EPdf:
258
return topdforps(doc.Ptr(), argv[i], argv[i+1], IpeDocument::EPdf,
259
flags, fromPage, toPage);
280
return topdforps(doc.ptr(), infile, outfile, Document::EPdf,
281
flags, fromPage, toPage, viewNo);
261
case IpeDocument::EEps:
262
return topdforps(doc.Ptr(), argv[i], argv[i+1], IpeDocument::EEps, flags);
284
return topdforps(doc.ptr(), infile, outfile, Document::EEps,
285
flags, fromPage, toPage, viewNo);