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

« back to all changes in this revision

Viewing changes to src/ipemodel/ipepdfloader.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2004-06-08 00:44:02 UTC
  • Revision ID: james.westby@ubuntu.com-20040608004402-72yu51xlh7vt6p9m
Tags: upstream-6.0pre16
ImportĀ upstreamĀ versionĀ 6.0pre16

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// --------------------------------------------------------------------
 
2
// Loading IML stream (and objects) from PDF file
 
3
// --------------------------------------------------------------------
 
4
/*
 
5
 
 
6
    This file is part of the extensible drawing editor Ipe.
 
7
    Copyright (C) 1993-2004  Otfried Cheong
 
8
 
 
9
    Ipe is free software; you can redistribute it and/or modify it
 
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
 
12
    (at your option) any later version.
 
13
 
 
14
    As a special exception, you have permission to link Ipe with the
 
15
    CGAL library and distribute executables, as long as you follow the
 
16
    requirements of the Gnu General Public License in regard to all of
 
17
    the software in the executable aside from CGAL.
 
18
 
 
19
    Ipe is distributed in the hope that it will be useful, but WITHOUT
 
20
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
21
    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 
22
    License for more details.
 
23
 
 
24
    You should have received a copy of the GNU General Public License
 
25
    along with Ipe; if not, you can find it at
 
26
    "http://www.gnu.org/copyleft/gpl.html", or write to the Free
 
27
    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
28
 
 
29
*/
 
30
 
 
31
#include "ipeiml.h"
 
32
#include "ipepdfloader_p.h"
 
33
 
 
34
#include <qfile.h>
 
35
 
 
36
// --------------------------------------------------------------------
 
37
 
 
38
IpePdfParser::IpePdfParser(IpePdfLoader &loader, IpeRepository *rep)
 
39
  : IpeImlParser(loader, rep)
 
40
{
 
41
  iXRef = loader.iDoc->getXRef();
 
42
}
 
43
 
 
44
IpeString IpePdfParser::PdfStream(int objNum)
 
45
{
 
46
  PdfObject streamObj;
 
47
  iXRef->fetch(objNum, 0, &streamObj);
 
48
  if (streamObj->isStream()) {
 
49
    Stream *str = streamObj->getStream()->getBaseStream();
 
50
    str->reset();
 
51
    IpeString data;
 
52
    int ch;
 
53
    while ((ch = str->getChar()) != EOF)
 
54
      data += ch;
 
55
    return data;
 
56
  } else
 
57
    return IpeString();
 
58
}
 
59
 
 
60
// --------------------------------------------------------------------
 
61
 
 
62
/*! \class IpePdfLoader
 
63
  \brief Loader for PDF files written by Ipe.
 
64
 
 
65
  Create an IpePdfLoader given a file name.
 
66
  Check that the file has been opened okay using IsOk().
 
67
  Check that it is a file written by Ipe using IsIpeFile().
 
68
 
 
69
 */
 
70
 
 
71
//! Create a loader object.
 
72
IpePdfLoader::IpePdfLoader(QString fname)
 
73
{
 
74
  iDoc = 0;
 
75
  iIpeStream = -1;
 
76
 
 
77
  InitXpdfLib();
 
78
 
 
79
  // iDoc takes ownership of GString
 
80
  iDoc = new PDFDoc(new GString(fname.utf8()));
 
81
  if (!iDoc->isOk())
 
82
    return;
 
83
 
 
84
  // try opening object # 1
 
85
  iDoc->getXRef()->fetch(1, 0, &iStreamObj);
 
86
  if (iStreamObj->isStream() && iStreamObj->streamIs("Ipe")) {
 
87
    iIpeStream = 1;
 
88
    iStreamObj->streamReset();
 
89
    return;
 
90
  } else
 
91
    iStreamObj->free();
 
92
 
 
93
  // otherwise, use /Ipe key in /Catalog (first preview versions of Ipe 6.0)
 
94
  PdfObject catObj;
 
95
  iDoc->getXRef()->getCatalog(&catObj);
 
96
  if (catObj->isNull() || !catObj->isDict("Catalog")) {
 
97
    return;
 
98
  }
 
99
  PdfObject ipeRefObj;
 
100
  catObj->dictLookupNF("Ipe", &ipeRefObj);
 
101
  if (ipeRefObj->isNull() || !ipeRefObj->isRef() ||
 
102
      ipeRefObj->getRef().gen != 0)
 
103
    // not an Ipe file
 
104
    return;
 
105
  iIpeStream = ipeRefObj->getRef().num;
 
106
  iDoc->getXRef()->fetch(iIpeStream, 0, &iStreamObj);
 
107
  iStreamObj->streamReset();
 
108
}
 
109
 
 
110
//! Destructor.
 
111
IpePdfLoader::~IpePdfLoader()
 
112
{
 
113
  delete iDoc;
 
114
}
 
115
 
 
116
//! Has PDF file been opened okay?
 
117
bool IpePdfLoader::IsOk() const
 
118
{
 
119
  return (iDoc && iDoc->isOk() && iIpeStream >= 0);
 
120
}
 
121
 
 
122
//! Read one character from Ipe stream in PDF document.
 
123
int IpePdfLoader::GetChar()
 
124
{
 
125
  return iStreamObj->streamGetChar();
 
126
}
 
127
 
 
128
// --------------------------------------------------------------------