Debian Codegraph @

src/lib/MWAWPrinter.cxx

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw
4 * Version: MPL 2.0 / LGPLv2+
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 2.0 (the "License"); you may not use this file except in compliance with
8 * the License or as specified alternatively below. You may obtain a copy of
9 * the License at http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * Major Contributor(s):
17 * Copyright (C) 2002 William Lachance ([email protected])
18 * Copyright (C) 2002,2004 Marc Maurer ([email protected])
19 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
20 * Copyright (C) 2006, 2007 Andrew Ziem
21 * Copyright (C) 2011, 2012 Alonso Laurent ([email protected])
22 *
23 *
24 * All Rights Reserved.
25 *
26 * For minor contributions see the git repository.
27 *
28 * Alternatively, the contents of this file may be used under the terms of
29 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
30 * in which case the provisions of the LGPLv2+ are applicable
31 * instead of those above.
32 */
33
34 #include <iostream>
35
36 #include "libmwaw_internal.hxx"
37 #include "MWAWInputStream.hxx"
38
39 #include "MWAWPrinter.hxx"
40
41 namespace libmwaw
42 {
43 //------------------------------------------------------------
44 //
45 // The printer information
46 // see http://www.mactech.com/articles/mactech/Vol.01/01.09/AllAboutPrinting/index.html
47 //
48 //------------------------------------------------------------
49
50
51 //
52 // PrinterRect : a rectangle
53 //
54 bool PrinterRect::read(MWAWInputStreamPtr input, MWAWVec2i const &res)
55 {
56 for (auto &st : m_pos) {
57 auto y = int(input->readLong(2));
58 y = int(float(y)*72.f/float(res.y()));
59 auto x = int(input->readLong(2));
60 x = int(float(x)*72.f/float(res.x()));
61 st.set(x,y);
62 }
63
64 if (input->isEnd()) return false;
65
66 if (m_pos[0].x() > m_pos[1].x() || m_pos[0].y() > m_pos[1].y())
67 return false;
68 return true;
69 }
70
71 //! Internal: structure used to keep a rectangle with its resolution
72 struct PrinterRectResolution {
73 PrinterRectResolution()
74 : m_rect()
75 , m_resolution()
76 , m_iDev(-1)
77 {
78 }
79 //! page dimension
80 PrinterRect page() const
81 {
82 return m_rect;
83 }
84 //! resolution
85 MWAWVec2i const &resolution() const
86 {
87 return m_resolution;
88 }
89
90 //! operator <<
91 friend std::ostream &operator<< (std::ostream &o, PrinterRectResolution const &r)
92 {
93 o << r.m_rect << ":" << r.m_resolution;
94 return o;
95 }
96 //! reads the data from file
97 bool read(MWAWInputStreamPtr input)
98 {
99 m_iDev = int(input->readLong(2));
100 auto y = int(input->readLong(2));
101 auto x = int(input->readLong(2));
102 if (x <= 0 || y <= 0) return false;
103 m_resolution.set(x,y);
104 return m_rect.read(input, m_resolution);
105 }
106
107 protected:
108 //! returns the main rectangle
109 PrinterRect m_rect;
110 //! returns the resolution
111 MWAWVec2i m_resolution;
112 //! a field which is reserved
113 int m_iDev;
114 };
115
116 //! Internal: structure used to keep the printer style information
117 struct PrinterStyle {
118 /** operator<<
119
120 \note print nothing*/
121 friend std::ostream &operator<< (std::ostream &o, PrinterStyle const &)
122 {
123 return o;
124 }
125 //! reads data from file
126 bool read(MWAWInputStreamPtr input)
127 {
128 m_wDev = static_cast<int>(input->readLong(2));
129 m_pageWidth = static_cast<int>(input->readLong(2));
130 m_pageHeight = static_cast<int>(input->readLong(2));
131 if (m_pageWidth < 0 || m_pageHeight < 0) return false;
132 m_port = static_cast<int>(input->readULong(1));
133 m_feed = static_cast<int>(input->readLong(1));
134 if (input->isEnd()) return false;
135 return true;
136 }
137
138 protected:
139 int m_wDev /** used internally */;
140 int m_feed /** paper type: cut, fanfold, mechcut or other */;
141 int m_pageHeight /** paper height */, m_pageWidth /** paper width*/, m_port /** printer or modem port */;
142 };
143
144 //! Internal: structure used to keep a printer job
145 struct PrinterJob {
146 //! operator<<
147 friend std::ostream &operator<< (std::ostream &o, PrinterJob const &r)
148 {
149 o << "fP=" << r.m_firstPage << ", lP=" << r.m_lastPage << ", copies=" << r.m_copies;
150 if (r.m_fileVol || r.m_fileVers) o << ", fVol=" << r.m_fileVol << ", fVers=" << r.m_fileVers;
151 return o;
152 }
153 //! read data from file
154 bool read(MWAWInputStreamPtr input)
155 {
156 m_firstPage = static_cast<int>(input->readLong(2));
157 m_lastPage = static_cast<int>(input->readLong(2));
158 m_copies = static_cast<int>(input->readLong(2));
159 m_jobDocLoop = static_cast<int>(input->readULong(1));
160 m_fromUser = static_cast<int>(input->readLong(1));
161 // skip pIdleProc
162 if (input->seek(4, librevenge::RVNG_SEEK_CUR) != 0 || input->isEnd()) return false;
163 // skip pFileName
164 if (input->seek(4, librevenge::RVNG_SEEK_CUR) != 0 || input->isEnd()) return false;
165 m_fileVol = static_cast<int>(input->readLong(2));
166 m_fileVers = static_cast<int>(input->readLong(1));
167 return true;
168 }
169
170 protected:
171 int m_firstPage /** first page*/, m_lastPage/** last page*/, m_copies /** number of copies */;
172 int m_jobDocLoop/** printing method: draft or defered */;
173 int m_fileVol /** volume reference number*/, m_fileVers /** version of spool file */;
174 //! reserved
175 int m_fromUser;
176 };
177
178 //
179 // PrinterInfo storage class
180 //
181 struct PrinterInfoData {
182 PrinterInfoData()
183 : m_info()
184 , m_paper()
185 , m_feed()
186 , m_info2()
187 , m_job()
188 , m_version(-1)
189 {
190 }
191
192 //! printer information
193 PrinterRectResolution m_info;
194 //! paper
195 PrinterRect m_paper;
196 //! printer style
197 PrinterStyle m_feed;
198 //! printer information
199 PrinterRectResolution m_info2;
200 //! jobs
201 PrinterJob m_job;
202 //! reserved
203 int m_version;
204 };
205
206 //
207 // PrinterInfo : ie. apple TPrint
208 //
209 PrinterInfo::PrinterInfo()
210 : m_data(new PrinterInfoData)
211 {
212 }
213
214 PrinterInfo::~PrinterInfo()
215 {
216 }
217
218 PrinterRect PrinterInfo::page() const
219 {
220 return m_data->m_info.page();
221 }
222 PrinterRect PrinterInfo::paper() const
223 {
224 return m_data->m_paper;
225 }
226
227 //! operator<< for a PrinterInfo
228 std::ostream &operator<< (std::ostream &o, PrinterInfo const &r)
229 {
230 o << "page = " << r.m_data->m_info << ", paper = " << r.m_data->m_paper
231 << ", infoPt: " << r.m_data->m_info2 << ", jobs: [" << r.m_data->m_job << "]";
232 return o;
233 }
234
235 bool PrinterInfo::read(MWAWInputStreamPtr input)
236 {
237 m_data->m_version = static_cast<int>(input->readLong(2));
238 if (!m_data->m_info.read(input)) return false;
239 if (!m_data->m_paper.read(input, m_data->m_info.resolution())) return false;
240 if (!m_data->m_feed.read(input)) return false;
241 long pos = input->tell();
242 if (!m_data->m_info2.read(input)) {
243 // can be left unfilled, so as we do not use the result, ...
244 input->seek(pos+14, librevenge::RVNG_SEEK_SET);
245 if (input->tell() != pos+14) return false;
246 }
247 // skip unknown structure prXInfo
248 if (input->seek(16, librevenge::RVNG_SEEK_CUR) != 0 || input->isEnd()) return false;
249
250 if (!m_data->m_job.read(input)) return false;
251 input->readLong(1);
252
253 // skip printX 19 short + 2 align
254 pos = input->tell();
255 if (input->seek(19*2,librevenge::RVNG_SEEK_CUR) != 0 || input->tell()!=pos+19*2) return false;
256 return true;
257 }
258 }
259 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: