~ubuntu-branches/ubuntu/precise/ipe/precise

« back to all changes in this revision

Viewing changes to src/ipetoipe/ipetoipe.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2009-12-11 21:22:35 UTC
  • mfrom: (4.1.6 sid)
  • Revision ID: james.westby@ubuntu.com-20091211212235-5iio4nzpra64snab
Tags: 7.0.10-1
* New upstream.  Closes: #551192.
  - New build-depends: libcairo2-dev, liblua5.1-0-dev, gsfonts
  - patches/config.diff: Remove.  Upstream build system replaced.
  - Runtime lib package changed to libipe7.0.10 from libipe1c2a
  - Devel package renamed to libipe-dev (from libipe1-dev)
  - Package ipe depends on lua5.1 due to ipe-update-master.

* rules: Re-write to use dh.

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
/*
5
5
 
6
6
    This file is part of the extensible drawing editor Ipe.
7
 
    Copyright (C) 1993-2007  Otfried Cheong
 
7
    Copyright (C) 1993-2009  Otfried Cheong
8
8
 
9
9
    Ipe is free software; you can redistribute it and/or modify it
10
10
    under the terms of the GNU General Public License as published by
11
 
    the Free Software Foundation; either version 2 of the License, or
 
11
    the Free Software Foundation; either version 3 of the License, or
12
12
    (at your option) any later version.
13
13
 
14
14
    As a special exception, you have permission to link Ipe with the
28
28
 
29
29
*/
30
30
 
31
 
#include "ipeq.h"
32
 
#include "ipemodel.h"
33
 
#include "ipeprefs.h"
34
 
#include "ipeutils.h"
35
 
 
36
 
#include <QDir>
37
 
 
38
 
#include <cstdio>
39
 
#include <cstdlib>
40
 
#include <clocale>
41
 
 
42
 
#ifdef WIN32
43
 
#include <windows.h>
44
 
#endif
45
 
 
46
 
#ifdef WIN32
47
 
static void ipeDebugImpl(const char *msg)
48
 
{
49
 
  OutputDebugStringA(msg);
50
 
}
51
 
#else
52
 
static void ipeDebugImpl(const char *)
53
 
{
54
 
  // fprintf(stderr, "%s\n", msg);
55
 
}
56
 
#endif
57
 
 
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)
61
 
{
62
 
  if (fm == IpeDocument::EEps && doc->totalViews() > 1) {
63
 
    fprintf(stderr,
64
 
            "The document contains %d views, cannot convert to EPS format.\n",
65
 
            doc->totalViews());
66
 
    return 1;
67
 
  }
68
 
 
69
 
  fprintf(stderr, "Document %s has %d pages (views)\n",
70
 
          src, doc->totalViews());
71
 
 
72
 
  int exitCode = IpeModel::RunLatex(doc);
73
 
 
74
 
  if (!exitCode) {
75
 
    if (fm == IpeDocument::EEps && doc->HasTrueTypeFonts()) {
 
31
#include "ipedoc.h"
 
32
 
 
33
using ipe::Document;
 
34
using ipe::String;
 
35
 
 
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)
 
39
{
 
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());
 
44
      return 1;
 
45
    }
 
46
 
 
47
    bool trans = doc->hasTransparency();
 
48
    bool grad = doc->hasGradients();
 
49
 
 
50
    if (!trans && !grad) {
 
51
      int res = doc->runLatex();
 
52
      if (res)
 
53
        return res;
 
54
    }
 
55
 
 
56
    bool ttf = doc->hasTrueTypeFonts();
 
57
 
 
58
    if (trans || grad || ttf) {
 
59
      fprintf(stderr, "The document uses the following features:\n");
 
60
      if (trans)
 
61
        fprintf(stderr, " - transparency\n");
 
62
      if (grad)
 
63
        fprintf(stderr, " - gradients\n");
 
64
      if (ttf)
 
65
        fprintf(stderr, " - Truetype fonts\n");
76
66
      fprintf(stderr,
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");
85
 
  }
86
 
  return exitCode;
87
 
}
88
 
 
89
 
static void recenterPages(IpeDocument *doc)
90
 
{
91
 
  for (int pg = 0; pg < doc->pages(); ++pg) {
92
 
    IpePage *page = doc->page(pg);
93
 
 
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)
103
 
      it->Transform(m);
104
 
  }
 
70
      return 1;
 
71
    }
 
72
  } else {
 
73
    int res = doc->runLatex();
 
74
    if (res) return res;
 
75
  }
 
76
 
 
77
  bool result = false;
 
78
  if (viewNo >= 0) {
 
79
    result = doc->exportView(dst.z(), fm, flags, fromPage, viewNo);
 
80
  } else if (toPage >= 0) {
 
81
    result = doc->exportPages(dst.z(), flags, fromPage, toPage);
 
82
  } else {
 
83
    result = doc->save(dst.z(), fm, flags);
 
84
  }
 
85
  if (!result) {
 
86
    fprintf(stderr, "Failed to save or export document!\n");
 
87
    return 1;
 
88
  }
 
89
 
 
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");
 
93
  return 0;
105
94
}
106
95
 
107
96
static void usage()
108
97
{
109
98
  fprintf(stderr,
110
 
          "Usage: ipetoipe ( -xml | -eps | -pdf ) [ -export ] "
111
 
          "[ -from <page> ] [ -to <page> ] "
112
 
          "[ -noshading ] [ -lastview ] [ -runlatex ] [ -nocolor ] "
113
 
          "[ -styleupdate ] "
114
 
          "infile outfile\n"
 
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"
 
105
          " -lastview    "
 
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"
123
111
          );
124
112
  exit(1);
126
114
 
127
115
int main(int argc, char *argv[])
128
116
{
129
 
  setlocale(LC_NUMERIC, "C");
130
 
 
131
 
  // Check Ipelib version mismatch
132
 
  if (IpelibVersion() != IPELIB_VERSION) {
133
 
    fprintf(stderr,
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());
139
 
    exit(99);
140
 
  }
141
 
 
142
 
  // set ipeDebug handler
143
 
  ipeDebugHandler = ipeDebugImpl;
144
 
 
145
 
  // ensure at least three arguments (handles -help as well :-)
146
 
  if (argc < 4)
147
 
    usage();
148
 
 
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;
156
 
 
157
 
  if (frm == IpeDocument::EUnknown)
158
 
    usage();
159
 
 
160
 
  uint flags = IpeDocument::ESaveNormal;
 
117
  ipe::Platform::initLib(ipe::IPELIB_VERSION);
 
118
 
 
119
  // ensure at least two arguments (handles -help as well :-)
 
120
  if (argc < 3)
 
121
    usage();
 
122
 
 
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;
 
130
 
 
131
  if (frm == Document::EUnknown)
 
132
    usage();
 
133
 
 
134
  uint flags = Document::ESaveNormal;
161
135
  bool runLatex = false;
162
136
  bool styleUpdate = false;
163
 
  bool recenter = false;
164
137
  int fromPage = -1;
165
138
  int toPage = -1;
 
139
  int viewNo = -1;
166
140
  int i = 2;
167
 
  if (!qstrcmp(argv[i], "-export")) {
168
 
    flags |= IpeDocument::EExport;
169
 
    ++i;
170
 
  }
171
 
 
172
 
  if (!qstrcmp(argv[i], "-from")) {
173
 
    if (sscanf(argv[i+1], "%d", &fromPage) != 1)
174
 
      usage();
175
 
    --fromPage;
176
 
    i += 2;
177
 
  }
178
 
 
179
 
  if (!qstrcmp(argv[i], "-to")) {
180
 
    if (sscanf(argv[i+1], "%d", &toPage) != 1)
181
 
      usage();
182
 
    --toPage;
183
 
    i += 2;
184
 
  }
185
 
 
186
 
  if (!qstrcmp(argv[i], "-noshading")) {
187
 
    flags |= IpeDocument::ENoShading;
188
 
    ++i;
189
 
  }
190
 
 
191
 
  if (!qstrcmp(argv[i], "-lastview")) {
192
 
    flags |= IpeDocument::ELastView;
193
 
    ++i;
194
 
  }
195
 
 
196
 
  if (!qstrcmp(argv[i], "-runlatex")) {
197
 
    runLatex = true;
198
 
    ++i;
199
 
  }
200
 
 
201
 
  if (!qstrcmp(argv[i], "-nocolor")) {
202
 
    flags |= IpeDocument::ENoColor;
203
 
    ++i;
204
 
  }
205
 
 
206
 
  if (!qstrcmp(argv[i], "-styleupdate")) {
207
 
    styleUpdate = true;
208
 
    ++i;
209
 
  }
210
 
 
211
 
  if (!qstrcmp(argv[i], "-recenter")) {
212
 
    recenter = true;
213
 
    ++i;
214
 
  }
215
 
 
216
 
  // remaining arguments must be two filenames
217
 
  if (argc != i + 2)
 
141
 
 
142
  String infile;
 
143
  String outfile;
 
144
 
 
145
  while (i < argc) {
 
146
 
 
147
    if (!strcmp(argv[i], "-export")) {
 
148
      flags |= Document::EExport;
 
149
      ++i;
 
150
    } else if (!strcmp(argv[i], "-view")) {
 
151
      if (sscanf(argv[i+1], "%d-%d", &fromPage, &viewNo) != 2)
 
152
        usage();
 
153
      flags |= Document::EExport;
 
154
      i += 2;
 
155
    } else if (!strcmp(argv[i], "-pages")) {
 
156
      if (sscanf(argv[i+1], "%d-%d", &fromPage, &toPage) != 2)
 
157
        usage();
 
158
      flags |= Document::EExport;
 
159
      i += 2;
 
160
    } else if (!strcmp(argv[i], "-lastview")) {
 
161
      flags |= Document::ELastView;
 
162
      flags |= Document::EExport;
 
163
      ++i;
 
164
    } else if (!strcmp(argv[i], "-runlatex")) {
 
165
      runLatex = true;
 
166
      ++i;
 
167
    } else if (!strcmp(argv[i], "-nocolor")) {
 
168
      flags |= Document::ENoColor;
 
169
      ++i;
 
170
    } else if (!strcmp(argv[i], "-nozip")) {
 
171
      flags |= Document::ENoZip;
 
172
      ++i;
 
173
    } else if (!strcmp(argv[i], "-styleupdate")) {
 
174
      styleUpdate = true;
 
175
      ++i;
 
176
    } else {
 
177
      // last one or two arguments must be filenames
 
178
      infile = argv[i];
 
179
      ++i;
 
180
      if (i < argc) {
 
181
        outfile = argv[i];
 
182
        ++i;
 
183
      }
 
184
      if (i != argc)
 
185
        usage();
 
186
    }
 
187
  }
 
188
 
 
189
  if (infile.empty())
218
190
    usage();
219
191
 
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");
222
194
    exit(1);
223
195
  }
224
196
 
225
 
  if ((fromPage >= 0 || toPage >= 0) && !(flags & IpeDocument::EExport)) {
226
 
    fprintf(stderr, "-from and -to only available with -export.\n");
227
 
    exit(1);
228
 
  }
229
 
 
230
 
  int reason;
231
 
  IpeAutoPtr<IpeDocument> doc(IpeDocument::New(argv[i], reason));
232
 
 
233
 
  if (!doc.Ptr()) {
234
 
    fprintf(stderr, "Could not read Ipe file '%s' (reason %d)\n",
235
 
            argv[i], reason);
 
197
  if (toPage >= 0 && frm != Document::EPdf) {
 
198
    fprintf(stderr, "-pages only available with -pdf.\n");
 
199
    exit(1);
 
200
  }
 
201
 
 
202
  if (toPage >= 0 && viewNo >= 0) {
 
203
    fprintf(stderr, "cannot specify both -pages and -view.\n");
 
204
    exit(1);
 
205
  }
 
206
 
 
207
  if (outfile.empty()) {
 
208
    outfile = infile;
 
209
    String ext = infile.right(4);
 
210
    if (ext == ".ipe" || ext == ".pdf" || ext == ".xml" || ext == ".eps")
 
211
      outfile = infile.left(infile.size() - 4);
 
212
    switch (frm) {
 
213
    case Document::EXml:
 
214
      outfile += ".ipe";
 
215
      break;
 
216
    case Document::EPdf:
 
217
      outfile += ".pdf";
 
218
      break;
 
219
    case Document::EEps:
 
220
      outfile += ".eps";
 
221
    default:
 
222
      break;
 
223
    }
 
224
    if (outfile == infile) {
 
225
      fprintf(stderr, "Cannot guess output filename.\n");
 
226
      exit(1);
 
227
    }
 
228
  }
 
229
 
 
230
  IpeAutoPtr<Document> doc(Document::loadWithErrorReport(infile.z()));
 
231
 
 
232
  if (!doc.ptr())
236
233
    return 1;
237
 
  }
238
234
 
239
235
  if (styleUpdate) {
240
 
    fprintf(stderr, "%s",
241
 
            IpeModel::UpdateStyleSheets(doc.Ptr(), QDir::current()).CString());
242
 
  }
243
 
 
244
 
  if (recenter)
245
 
    recenterPages(doc.Ptr());
 
236
    String curdir = ipe::Platform::currentDirectory();
 
237
    fprintf(stderr, "%s", doc.ptr()->cascade()->update(curdir).z());
 
238
  }
 
239
 
 
240
  fprintf(stderr, "Document %s has %d pages (%d views)\n",
 
241
          infile.z(), doc->countPages(), doc->countTotalViews());
 
242
 
 
243
  // check frompage, topage, viewno
 
244
  if (toPage >= 0) {
 
245
    if (fromPage <= 0 || fromPage > toPage || toPage > doc->countPages()) {
 
246
      fprintf(stderr, "incorrect -pages specification.\n");
 
247
      exit(1);
 
248
    }
 
249
    --fromPage;
 
250
    --toPage;
 
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");
 
255
      exit(1);
 
256
    }
 
257
    --fromPage;
 
258
    --viewNo;
 
259
  }
 
260
 
 
261
  char buf[64];
 
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);
246
269
 
247
270
  switch (frm) {
248
 
  case IpeDocument::EXml:
 
271
  case Document::EXml:
249
272
    if (runLatex)
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);
 
274
    else
 
275
      doc->save(outfile.z(), Document::EXml, Document::ESaveNormal);
254
276
  default:
255
277
    return 0;
256
278
 
257
 
  case IpeDocument::EPdf:
258
 
    return topdforps(doc.Ptr(), argv[i], argv[i+1], IpeDocument::EPdf,
259
 
                     flags, fromPage, toPage);
 
279
  case Document::EPdf:
 
280
    return topdforps(doc.ptr(), infile, outfile, Document::EPdf,
 
281
                     flags, fromPage, toPage, viewNo);
260
282
 
261
 
  case IpeDocument::EEps:
262
 
    return topdforps(doc.Ptr(), argv[i], argv[i+1], IpeDocument::EEps, flags);
 
283
  case Document::EEps:
 
284
    return topdforps(doc.ptr(), infile, outfile, Document::EEps,
 
285
                     flags, fromPage, toPage, viewNo);
263
286
  }
264
287
}
265
288