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

« back to all changes in this revision

Viewing changes to src/include/ipepdfparser.h

  • 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:
5
5
/*
6
6
 
7
7
    This file is part of the extensible drawing editor Ipe.
8
 
    Copyright (C) 1993-2007  Otfried Cheong
 
8
    Copyright (C) 1993-2009  Otfried Cheong
9
9
 
10
10
    Ipe is free software; you can redistribute it and/or modify it
11
11
    under the terms of the GNU General Public License as published by
12
 
    the Free Software Foundation; either version 2 of the License, or
 
12
    the Free Software Foundation; either version 3 of the License, or
13
13
    (at your option) any later version.
14
14
 
15
15
    As a special exception, you have permission to link Ipe with the
36
36
 
37
37
// --------------------------------------------------------------------
38
38
 
39
 
class IpePdfNull;
40
 
class IpePdfBool;
41
 
class IpePdfNumber;
42
 
class IpePdfString;
43
 
class IpePdfName;
44
 
class IpePdfRef;
45
 
class IpePdfArray;
46
 
class IpePdfDict;
47
 
 
48
 
class IpePdfFile;
49
 
 
50
 
class IPE_EXPORT IpePdfObj {
51
 
public:
52
 
  virtual ~IpePdfObj() = 0;
53
 
  virtual const IpePdfNull *Null() const;
54
 
  virtual const IpePdfBool *Bool() const;
55
 
  virtual const IpePdfNumber *Number() const;
56
 
  virtual const IpePdfString *String() const;
57
 
  virtual const IpePdfName *Name() const;
58
 
  virtual const IpePdfRef *Ref() const;
59
 
  virtual const IpePdfArray *Array() const;
60
 
  virtual const IpePdfDict *Dict() const;
61
 
  virtual void Write(IpeStream &stream) const = 0;
62
 
  IpeString Repr() const;
63
 
};
64
 
 
65
 
class IPE_EXPORT IpePdfNull : public IpePdfObj {
66
 
public:
67
 
  explicit IpePdfNull() { /* nothing */ }
68
 
  virtual const IpePdfNull *Null() const;
69
 
  virtual void Write(IpeStream &stream) const;
70
 
};
71
 
 
72
 
class IPE_EXPORT IpePdfBool : public IpePdfObj {
73
 
public:
74
 
  explicit IpePdfBool(bool val) : iValue(val) { /* nothing */ }
75
 
  virtual const IpePdfBool *Bool() const;
76
 
  virtual void Write(IpeStream &stream) const;
77
 
  inline bool Value() const { return iValue; }
78
 
private:
79
 
  bool iValue;
80
 
};
81
 
 
82
 
class IPE_EXPORT IpePdfNumber : public IpePdfObj {
83
 
public:
84
 
  explicit IpePdfNumber(double val) : iValue(val) { /* nothing */ }
85
 
  virtual const IpePdfNumber *Number() const;
86
 
  virtual void Write(IpeStream &stream) const;
87
 
  inline double Value() const { return iValue; }
88
 
private:
89
 
  double iValue;
90
 
};
91
 
 
92
 
class IPE_EXPORT IpePdfString : public IpePdfObj {
93
 
public:
94
 
  explicit IpePdfString(const IpeString &val) : iValue(val) { /* nothing */ }
95
 
  virtual const IpePdfString *String() const;
96
 
  virtual void Write(IpeStream &stream) const;
97
 
  inline IpeString Value() const { return iValue; }
98
 
private:
99
 
  IpeString iValue;
100
 
};
101
 
 
102
 
class IPE_EXPORT IpePdfName : public IpePdfObj {
103
 
public:
104
 
  explicit IpePdfName(const IpeString &val) : iValue(val) { /* nothing */ }
105
 
  virtual const IpePdfName *Name() const;
106
 
  virtual void Write(IpeStream &stream) const;
107
 
  inline IpeString Value() const { return iValue; }
108
 
private:
109
 
  IpeString iValue;
110
 
};
111
 
 
112
 
class IPE_EXPORT IpePdfRef : public IpePdfObj {
113
 
public:
114
 
  explicit IpePdfRef(int val) : iValue(val) { /* nothing */ }
115
 
  virtual const IpePdfRef *Ref() const;
116
 
  virtual void Write(IpeStream &stream) const;
117
 
  inline int Value() const { return iValue; }
118
 
private:
119
 
  int iValue;
120
 
};
121
 
 
122
 
class IPE_EXPORT IpePdfArray : public IpePdfObj {
123
 
public:
124
 
  explicit IpePdfArray() { /* nothing */ }
125
 
  ~IpePdfArray();
126
 
  virtual const IpePdfArray *Array() const;
127
 
  virtual void Write(IpeStream &stream) const;
128
 
  void Append(const IpePdfObj *);
129
 
  inline int Count() const { return iObjects.size(); }
130
 
  const IpePdfObj *Obj(int index, const IpePdfFile *file) const;
131
 
private:
132
 
  std::vector<const IpePdfObj *> iObjects;
133
 
};
134
 
 
135
 
class IPE_EXPORT IpePdfDict : public IpePdfObj {
136
 
public:
137
 
  explicit IpePdfDict() { /* nothing */ }
138
 
  ~IpePdfDict();
139
 
  virtual const IpePdfDict *Dict() const;
140
 
  virtual void Write(IpeStream &stream) const;
141
 
  void SetStream(const IpeBuffer &stream);
142
 
  void Add(IpeString key, const IpePdfObj *obj);
143
 
  const IpePdfObj *Get(IpeString key, const IpePdfFile *file) const;
144
 
  inline int Count() const { return iItems.size(); }
145
 
  inline IpeString Key(int index) const { return iItems[index].iKey; }
146
 
  inline IpeBuffer Stream() const { return iStream; }
147
 
  bool Deflated() const;
148
 
  // IpeBuffer Inflate() const;
149
 
private:
150
 
  struct Item {
151
 
    IpeString iKey;
152
 
    const IpePdfObj *iVal;
153
 
  };
154
 
  std::vector<Item> iItems;
155
 
  IpeBuffer iStream;
156
 
};
157
 
 
158
 
// --------------------------------------------------------------------
159
 
 
160
 
//! A PDF lexical token.
161
 
struct IpePdfToken {
162
 
  //! The various types of tokens.
163
 
  enum TToken { EErr, EOp, EName, ENumber, EString, ETrue, EFalse, ENull,
164
 
                EArrayBg, EArrayEnd, EDictBg, EDictEnd };
165
 
  //! The type of this token.
166
 
  TToken iType;
167
 
  //! The string representing this token.
168
 
  IpeString iString;
169
 
};
170
 
 
171
 
class IPE_EXPORT IpePdfParser {
172
 
public:
173
 
  IpePdfParser(IpeDataSource &source);
174
 
 
175
 
  inline void GetChar() { iCh = iSource.GetChar(); ++iPos; }
176
 
  inline bool Eos() const { return (iCh == EOF); }
177
 
  inline IpePdfToken Token() const { return iTok; }
178
 
 
179
 
  void GetToken();
180
 
  IpePdfObj *GetObject();
181
 
  IpePdfObj *GetObjectDef();
182
 
  IpePdfDict *GetTrailer();
183
 
  void SkipXRef();
184
 
 
185
 
 private:
186
 
  void SkipWhiteSpace();
187
 
  IpePdfArray *MakeArray();
188
 
  IpePdfDict *MakeDict();
189
 
 
190
 
private:
191
 
  IpeDataSource &iSource;
192
 
  int iPos;
193
 
  int iCh;
194
 
  IpePdfToken iTok;
195
 
};
196
 
 
197
 
class IPE_EXPORT IpePdfFile {
198
 
public:
199
 
  IpePdfFile();
200
 
  ~IpePdfFile();
201
 
  bool Parse(IpeDataSource &source);
202
 
  const IpePdfObj *Object(int num) const;
203
 
  const IpePdfDict *Catalog() const;
204
 
  const IpePdfDict *Page() const;
205
 
private:
206
 
  std::map<int, const IpePdfObj *> iObjects;
207
 
  const IpePdfDict *iTrailer;
208
 
};
 
39
namespace ipe {
 
40
 
 
41
  class PdfNull;
 
42
  class PdfBool;
 
43
  class PdfNumber;
 
44
  class PdfString;
 
45
  class PdfName;
 
46
  class PdfRef;
 
47
  class PdfArray;
 
48
  class PdfDict;
 
49
 
 
50
  class PdfFile;
 
51
 
 
52
  class PdfObj {
 
53
  public:
 
54
    virtual ~PdfObj() = 0;
 
55
    virtual const PdfNull *null() const;
 
56
    virtual const PdfBool *boolean() const;
 
57
    virtual const PdfNumber *number() const;
 
58
    virtual const PdfString *string() const;
 
59
    virtual const PdfName *name() const;
 
60
    virtual const PdfRef *ref() const;
 
61
    virtual const PdfArray *array() const;
 
62
    virtual const PdfDict *dict() const;
 
63
    virtual void write(Stream &stream) const = 0;
 
64
    String repr() const;
 
65
  };
 
66
 
 
67
  class PdfNull : public PdfObj {
 
68
  public:
 
69
    explicit PdfNull() { /* nothing */ }
 
70
    virtual const PdfNull *null() const;
 
71
    virtual void write(Stream &stream) const;
 
72
  };
 
73
 
 
74
  class PdfBool : public PdfObj {
 
75
  public:
 
76
    explicit PdfBool(bool val) : iValue(val) { /* nothing */ }
 
77
    virtual const PdfBool *boolean() const;
 
78
    virtual void write(Stream &stream) const;
 
79
    inline bool value() const { return iValue; }
 
80
  private:
 
81
    bool iValue;
 
82
  };
 
83
 
 
84
  class PdfNumber : public PdfObj {
 
85
  public:
 
86
    explicit PdfNumber(double val) : iValue(val) { /* nothing */ }
 
87
    virtual const PdfNumber *number() const;
 
88
    virtual void write(Stream &stream) const;
 
89
    inline double value() const { return iValue; }
 
90
  private:
 
91
    double iValue;
 
92
  };
 
93
 
 
94
  class PdfString : public PdfObj {
 
95
  public:
 
96
    explicit PdfString(const String &val) : iValue(val) { /* nothing */ }
 
97
    virtual const PdfString *string() const;
 
98
    virtual void write(Stream &stream) const;
 
99
    inline String value() const { return iValue; }
 
100
  private:
 
101
    String iValue;
 
102
  };
 
103
 
 
104
  class PdfName : public PdfObj {
 
105
  public:
 
106
    explicit PdfName(const String &val) : iValue(val) { /* nothing */ }
 
107
    virtual const PdfName *name() const;
 
108
    virtual void write(Stream &stream) const;
 
109
    inline String value() const { return iValue; }
 
110
  private:
 
111
    String iValue;
 
112
  };
 
113
 
 
114
  class PdfRef : public PdfObj {
 
115
  public:
 
116
  explicit PdfRef(int val) : iValue(val) { /* nothing */ }
 
117
    virtual const PdfRef *ref() const;
 
118
    virtual void write(Stream &stream) const;
 
119
    inline int value() const { return iValue; }
 
120
  private:
 
121
    int iValue;
 
122
  };
 
123
 
 
124
  class PdfArray : public PdfObj {
 
125
  public:
 
126
    explicit PdfArray() { /* nothing */ }
 
127
    ~PdfArray();
 
128
    virtual const PdfArray *array() const;
 
129
    virtual void write(Stream &stream) const;
 
130
    void append(const PdfObj *);
 
131
    inline int count() const { return iObjects.size(); }
 
132
    const PdfObj *obj(int index, const PdfFile *file) const;
 
133
  private:
 
134
    std::vector<const PdfObj *> iObjects;
 
135
  };
 
136
 
 
137
  class PdfDict : public PdfObj {
 
138
  public:
 
139
    explicit PdfDict() { /* nothing */ }
 
140
    ~PdfDict();
 
141
    virtual const PdfDict *dict() const;
 
142
    virtual void write(Stream &stream) const;
 
143
    void setStream(const Buffer &stream);
 
144
    void add(String key, const PdfObj *obj);
 
145
    const PdfObj *get(String key, const PdfFile *file) const;
 
146
    inline int count() const { return iItems.size(); }
 
147
    inline String key(int index) const { return iItems[index].iKey; }
 
148
    inline Buffer stream() const { return iStream; }
 
149
    bool deflated() const;
 
150
    // Buffer Inflate() const;
 
151
  private:
 
152
    struct Item {
 
153
      String iKey;
 
154
      const PdfObj *iVal;
 
155
    };
 
156
    std::vector<Item> iItems;
 
157
    Buffer iStream;
 
158
  };
 
159
 
 
160
  // --------------------------------------------------------------------
 
161
 
 
162
  //! A PDF lexical token.
 
163
  struct PdfToken {
 
164
    //! The various types of tokens.
 
165
    enum TToken { EErr, EOp, EName, ENumber, EString, ETrue, EFalse, ENull,
 
166
                  EArrayBg, EArrayEnd, EDictBg, EDictEnd };
 
167
    //! The type of this token.
 
168
    TToken iType;
 
169
    //! The string representing this token.
 
170
    String iString;
 
171
  };
 
172
 
 
173
  class PdfParser {
 
174
  public:
 
175
    PdfParser(DataSource &source);
 
176
 
 
177
    inline void getChar() { iCh = iSource.getChar(); ++iPos; }
 
178
    inline bool eos() const { return (iCh == EOF); }
 
179
    inline PdfToken token() const { return iTok; }
 
180
 
 
181
    void getToken();
 
182
    PdfObj *getObject();
 
183
    PdfObj *getObjectDef();
 
184
    PdfDict *getTrailer();
 
185
    void skipXRef();
 
186
 
 
187
  private:
 
188
    void skipWhiteSpace();
 
189
    PdfArray *makeArray();
 
190
    PdfDict *makeDict();
 
191
 
 
192
  private:
 
193
    DataSource &iSource;
 
194
    int iPos;
 
195
    int iCh;
 
196
    PdfToken iTok;
 
197
  };
 
198
 
 
199
  class PdfFile {
 
200
  public:
 
201
    PdfFile();
 
202
    ~PdfFile();
 
203
    bool parse(DataSource &source);
 
204
    const PdfObj *object(int num) const;
 
205
    const PdfDict *catalog() const;
 
206
    const PdfDict *page() const;
 
207
  private:
 
208
    std::map<int, const PdfObj *> iObjects;
 
209
    const PdfDict *iTrailer;
 
210
  };
 
211
 
 
212
} // namespace
209
213
 
210
214
// --------------------------------------------------------------------
211
215
#endif