~ubuntu-branches/ubuntu/quantal/cups-filters/quantal-proposed

« back to all changes in this revision

Viewing changes to filter/pdftopdf/qpdf_pdftopdf.cc

  • Committer: Package Import Robot
  • Author(s): Till Kamppeter
  • Date: 2012-08-20 14:53:42 UTC
  • mfrom: (2.1.2 experimental)
  • Revision ID: package-import@ubuntu.com-20120820145342-bddzpwqv0klmt84d
Tags: 1.0.22-1
* New upstream release
   - pdftopdf filter replaced by new QPDF-based filter from Tobias
     Hoffmann's Google Summer of Code project. The former Poppler-based
     pdftopdf duplicated a lot of Poppler's code. The old filter is
     still in the package as pdftopdf.old with source code in
     filter/pdftopdf.old. It will be removed in a later release.
   - bannertopdf: Page duplication routine fixed.
   - bannertopdf: Fixed invalid output of a direct stream object.
   - Added most recent contributors to AUTHORS and COPYING files.
* debian/control: Added build dependency on libqpdf-dev.
* debian/copyright: Updated for the addition of the new pdftopdf filter.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "qpdf_pdftopdf.h"
 
2
#include <assert.h>
 
3
#include <stdexcept>
 
4
 
 
5
PageRect getBoxAsRect(QPDFObjectHandle box) // {{{
 
6
{
 
7
  PageRect ret;
 
8
 
 
9
  ret.left=box.getArrayItem(0).getNumericValue();
 
10
  ret.bottom=box.getArrayItem(1).getNumericValue();
 
11
  ret.right=box.getArrayItem(2).getNumericValue();
 
12
  ret.top=box.getArrayItem(3).getNumericValue();
 
13
 
 
14
  ret.width=ret.right-ret.left;
 
15
  ret.height=ret.top-ret.bottom;
 
16
 
 
17
  return ret;
 
18
}
 
19
// }}}
 
20
 
 
21
Rotation getRotate(QPDFObjectHandle page) // {{{
 
22
{
 
23
  if (!page.hasKey("/Rotate")) {
 
24
    return ROT_0;
 
25
  }
 
26
  double rot=page.getKey("/Rotate").getNumericValue();
 
27
  if (rot==90.0) { // CW 
 
28
    return ROT_270; // CCW
 
29
  } else if (rot==180.0) {
 
30
    return ROT_180;
 
31
  } else if (rot==270.0) {
 
32
    return ROT_90;
 
33
  } else {
 
34
    assert(rot==0.0);
 
35
  }
 
36
  return ROT_0;
 
37
}
 
38
// }}}
 
39
 
 
40
double getUserUnit(QPDFObjectHandle page) // {{{
 
41
{
 
42
  if (!page.hasKey("/UserUnit")) {
 
43
    return 1.0;
 
44
  }
 
45
  return page.getKey("/UserUnit").getNumericValue();
 
46
}
 
47
// }}}
 
48
 
 
49
QPDFObjectHandle makeRotate(Rotation rot) // {{{
 
50
{
 
51
  switch (rot) {
 
52
  case ROT_0:
 
53
    return QPDFObjectHandle::newNull();
 
54
  case ROT_90: // CCW
 
55
    return QPDFObjectHandle::newInteger(270); // CW
 
56
  case ROT_180:
 
57
    return QPDFObjectHandle::newInteger(180);
 
58
  case ROT_270:
 
59
    return QPDFObjectHandle::newInteger(90);
 
60
  default:
 
61
    throw std::invalid_argument("Bad rotation");
 
62
  }
 
63
}
 
64
// }}}
 
65
 
 
66
#include "qpdf_tools.h"
 
67
 
 
68
QPDFObjectHandle getRectAsBox(const PageRect &rect) // {{{
 
69
{
 
70
  return makeBox(rect.left,rect.bottom,rect.right,rect.top);
 
71
}
 
72
// }}}
 
73
 
 
74
#include <qpdf/QUtil.hh>
 
75
 
 
76
Matrix::Matrix() // {{{
 
77
  : ctm({1,0,0,1,0,0})
 
78
{
 
79
}
 
80
// }}}
 
81
 
 
82
Matrix::Matrix(QPDFObjectHandle ar) // {{{
 
83
{
 
84
  if (ar.getArrayNItems()!=6) {
 
85
    throw std::runtime_error("Not a ctm matrix");
 
86
  }
 
87
  for (int iA=0;iA<6;iA++) {
 
88
    ctm[iA]=ar.getArrayItem(iA).getNumericValue();
 
89
  }
 
90
}
 
91
// }}}
 
92
 
 
93
Matrix &Matrix::rotate(Rotation rot) // {{{
 
94
{
 
95
  switch (rot) {
 
96
  case ROT_0:
 
97
    break;
 
98
  case ROT_90:
 
99
    std::swap(ctm[0],ctm[2]);
 
100
    std::swap(ctm[1],ctm[3]);
 
101
    ctm[2]=-ctm[2];
 
102
    ctm[3]=-ctm[3];
 
103
    break;
 
104
  case ROT_180:
 
105
    ctm[0]=-ctm[0];
 
106
    ctm[3]=-ctm[3];
 
107
    break;
 
108
  case ROT_270:
 
109
    std::swap(ctm[0],ctm[2]);
 
110
    std::swap(ctm[1],ctm[3]);
 
111
    ctm[0]=-ctm[0];
 
112
    ctm[1]=-ctm[1];
 
113
    break;
 
114
  default:
 
115
    assert(0);
 
116
  }
 
117
  return *this;
 
118
}
 
119
// }}}
 
120
 
 
121
// TODO: test
 
122
Matrix &Matrix::rotate_move(Rotation rot,double width,double height) // {{{
 
123
{
 
124
  rotate(rot);
 
125
  switch (rot) {
 
126
  case ROT_0:
 
127
    break;
 
128
  case ROT_90:
 
129
    translate(width,0);
 
130
    break;
 
131
  case ROT_180:
 
132
    translate(width,height);
 
133
    break;
 
134
  case ROT_270:
 
135
    translate(0,height);
 
136
    break;
 
137
  }
 
138
  return *this;
 
139
}
 
140
// }}}
 
141
 
 
142
Matrix &Matrix::rotate(double rad) // {{{
 
143
{
 
144
  Matrix tmp;
 
145
 
 
146
  tmp.ctm[0]=cos(rad);
 
147
  tmp.ctm[1]=sin(rad);
 
148
  tmp.ctm[2]=-sin(rad);
 
149
  tmp.ctm[3]=cos(rad);
 
150
 
 
151
  return (*this*=tmp);
 
152
}
 
153
// }}}
 
154
 
 
155
Matrix &Matrix::translate(double tx,double ty) // {{{
 
156
{
 
157
  ctm[4]+=ctm[0]*tx+ctm[2]*ty;
 
158
  ctm[5]+=ctm[1]*tx+ctm[3]*ty;
 
159
  return *this;
 
160
}
 
161
// }}}
 
162
 
 
163
Matrix &Matrix::scale(double sx,double sy) // {{{
 
164
{
 
165
  ctm[0]*=sx;
 
166
  ctm[1]*=sx;
 
167
  ctm[2]*=sy;
 
168
  ctm[3]*=sy;
 
169
  return *this;
 
170
}
 
171
// }}}
 
172
 
 
173
Matrix &Matrix::operator*=(const Matrix &rhs) // {{{
 
174
{
 
175
  double tmp[6];
 
176
  std::copy(ctm,ctm+6,tmp);
 
177
 
 
178
  ctm[0] = tmp[0]*rhs.ctm[0] + tmp[2]*rhs.ctm[1];
 
179
  ctm[1] = tmp[1]*rhs.ctm[0] + tmp[3]*rhs.ctm[1];
 
180
 
 
181
  ctm[2] = tmp[0]*rhs.ctm[2] + tmp[2]*rhs.ctm[3];
 
182
  ctm[3] = tmp[1]*rhs.ctm[2] + tmp[3]*rhs.ctm[3];
 
183
 
 
184
  ctm[4] = tmp[0]*rhs.ctm[4] + tmp[2]*rhs.ctm[5] + tmp[4];
 
185
  ctm[5] = tmp[1]*rhs.ctm[4] + tmp[3]*rhs.ctm[5] + tmp[5];
 
186
 
 
187
  return *this;
 
188
}
 
189
// }}}
 
190
 
 
191
QPDFObjectHandle Matrix::get() const // {{{
 
192
{
 
193
  QPDFObjectHandle ret=QPDFObjectHandle::newArray();
 
194
  ret.appendItem(QPDFObjectHandle::newReal(ctm[0]));
 
195
  ret.appendItem(QPDFObjectHandle::newReal(ctm[1]));
 
196
  ret.appendItem(QPDFObjectHandle::newReal(ctm[2]));
 
197
  ret.appendItem(QPDFObjectHandle::newReal(ctm[3]));
 
198
  ret.appendItem(QPDFObjectHandle::newReal(ctm[4]));
 
199
  ret.appendItem(QPDFObjectHandle::newReal(ctm[5]));
 
200
  return ret;
 
201
}
 
202
// }}}
 
203
 
 
204
std::string Matrix::get_string() const // {{{
 
205
{
 
206
  std::string ret;
 
207
  ret.append(QUtil::double_to_string(ctm[0]));
 
208
  ret.append(" ");
 
209
  ret.append(QUtil::double_to_string(ctm[1]));
 
210
  ret.append(" ");
 
211
  ret.append(QUtil::double_to_string(ctm[2]));
 
212
  ret.append(" ");
 
213
  ret.append(QUtil::double_to_string(ctm[3]));
 
214
  ret.append(" ");
 
215
  ret.append(QUtil::double_to_string(ctm[4]));
 
216
  ret.append(" ");
 
217
  ret.append(QUtil::double_to_string(ctm[5]));
 
218
  return ret;
 
219
}
 
220
// }}}
 
221