xref: /haiku/src/add-ons/print/drivers/pcl6/PCL6Writer.cpp (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2 ** PCL6Writer.cpp
3 ** Copyright 2005, Michael Pfeiffer, laplace@users.sourceforge.net. All rights reserved.
4 ** Distributed under the terms of the OpenBeOS License.
5 */
6 #include "PCL6Writer.h"
7 
8 #include <ByteOrder.h>
9 #include <String.h>
10 
11 #define BYTE_AT(lvalue, index) (((uint8*)(&lvalue))[index])
12 
13 PCL6Writer::PCL6Writer(PCL6WriterStream *stream, uint32 bufferSize)
14 	: 	fStream(stream),
15 		fBuffer(new uint8[bufferSize]),
16 		fSize(bufferSize),
17 		fIndex(0)
18 {
19 }
20 
21 PCL6Writer::~PCL6Writer() {
22 	delete fBuffer;
23 	fBuffer = NULL;
24 }
25 
26 // throws TransportException
27 void
28 PCL6Writer::Append(uint8 value) {
29 	if (fIndex == fSize) {
30 		Flush();
31 	}
32 	fBuffer[fIndex] = value;
33 	fIndex ++;
34 }
35 
36 void
37 PCL6Writer::Flush() {
38 	if (fIndex > 0) {
39 		fStream->write(fBuffer, fIndex);
40 		fIndex = 0;
41 	}
42 }
43 
44 void
45 PCL6Writer::Append(int16 value) {
46 	int16 v = B_HOST_TO_LENDIAN_INT16(value);
47 	Append(BYTE_AT(v, 0));
48 	Append(BYTE_AT(v, 1));
49 }
50 
51 void
52 PCL6Writer::Append(uint16 value) {
53 	int16 v = B_HOST_TO_LENDIAN_INT16(value);
54 	Append(BYTE_AT(v, 0));
55 	Append(BYTE_AT(v, 1));
56 }
57 
58 void
59 PCL6Writer::Append(int32 value) {
60 	int32 v = B_HOST_TO_LENDIAN_INT32(value);
61 	Append(BYTE_AT(v, 0));
62 	Append(BYTE_AT(v, 1));
63 	Append(BYTE_AT(v, 2));
64 	Append(BYTE_AT(v, 3));
65 }
66 
67 void
68 PCL6Writer::Append(uint32 value) {
69 	int32 v = B_HOST_TO_LENDIAN_INT32(value);
70 	Append(BYTE_AT(v, 0));
71 	Append(BYTE_AT(v, 1));
72 	Append(BYTE_AT(v, 2));
73 	Append(BYTE_AT(v, 3));
74 }
75 
76 void
77 PCL6Writer::Append(float value) {
78 	float v = B_HOST_TO_LENDIAN_FLOAT(value);
79 	Append(BYTE_AT(v, 0));
80 	Append(BYTE_AT(v, 1));
81 	Append(BYTE_AT(v, 2));
82 	Append(BYTE_AT(v, 3));
83 }
84 
85 void
86 PCL6Writer::Append(const uint8* data, uint32 size) {
87 	for (uint32 i = 0; i < size; i++) {
88 		Append(data[i]);
89 	}
90 }
91 
92 
93 void
94 PCL6Writer::AppendString(const char *string) {
95 	uint8 ch = *string;
96 	while (ch != 0) {
97 		Append(ch);
98 		string ++;
99 		ch = *string;
100 	}
101 }
102 
103 void
104 PCL6Writer::AppendOperator(Operator op) {
105 	Append((uint8)op);
106 }
107 
108 void
109 PCL6Writer::AppendAttribute(Attribute attr) {
110 	AppendDataTag(k8BitAttrId);
111 	Append((uint8)attr);
112 }
113 
114 void
115 PCL6Writer::AppendDataTag(DataTag tag) {
116 	Append((uint8)tag);
117 }
118 
119 void
120 PCL6Writer::AppendData(uint8 value) {
121 	AppendDataTag(kUByteData);
122 	Append(value);
123 }
124 
125 void
126 PCL6Writer::AppendData(int16 value) {
127 	AppendDataTag(kSInt16Data);
128 	Append(value);
129 }
130 
131 void
132 PCL6Writer::AppendData(uint16 value) {
133 	AppendDataTag(kUInt16Data);
134 	Append(value);
135 }
136 
137 void
138 PCL6Writer::AppendData(int32 value) {
139 	AppendDataTag(kSInt32Data);
140 	Append(value);
141 }
142 
143 void
144 PCL6Writer::AppendData(uint32 value) {
145 	AppendDataTag(kUInt32Data);
146 	Append(value);
147 }
148 
149 void
150 PCL6Writer::AppendData(float value) {
151 	AppendDataTag(kReal32Data);
152 	Append(value);
153 }
154 
155 void
156 PCL6Writer::EmbeddedDataPrefix(uint32 size) {
157 	if (size < 256) {
158 		AppendDataTag(kEmbeddedDataByte);
159 		Append((uint8)size);
160 	} else {
161 		AppendDataTag(kEmbeddedData);
162 		Append(size);
163 	}
164 }
165 
166 void
167 PCL6Writer::EmbeddedDataPrefix32(uint32 size) {
168 	AppendDataTag(kEmbeddedData);
169 	Append(size);
170 }
171 
172 void
173 PCL6Writer::AppendDataXY(uint8 x, uint8 y) {
174 	AppendDataTag(kUByteXY);
175 	Append(x); Append(y);
176 }
177 
178 void
179 PCL6Writer::AppendDataXY(int16 x, int16 y) {
180 	AppendDataTag(kSInt16XY);
181 	Append(x); Append(y);
182 }
183 
184 void
185 PCL6Writer::AppendDataXY(uint16 x, uint16 y) {
186 	AppendDataTag(kUInt16XY);
187 	Append(x); Append(y);
188 }
189 
190 void
191 PCL6Writer::AppendDataXY(int32 x, int32 y) {
192 	AppendDataTag(kSInt32XY);
193 	Append(x); Append(y);
194 }
195 
196 void
197 PCL6Writer::AppendDataXY(uint32 x, uint32 y) {
198 	AppendDataTag(kUInt32XY);
199 	Append(x); Append(y);
200 }
201 
202 void
203 PCL6Writer::AppendDataXY(float x, float y) {
204 	AppendDataTag(kReal32XY);
205 	Append(x); Append(y);
206 }
207 
208 void
209 PCL6Writer::PJLHeader(ProtocolClass protocolClass, int dpi, const char *comment)
210 {
211 	BString string;
212 	AppendString("\033%-12345X@PJL JOB\n"
213 				 "@PJL SET RESOLUTION=");
214 	string << dpi;
215 	AppendString(string.String());
216 	AppendString("\n"
217 	             "@PJL ENTER LANGUAGE=PCLXL\n"
218 	             ") HP-PCL XL;");
219 
220 	const char* pc = "";
221 	switch (protocolClass) {
222 		case kProtocolClass1_1:
223 			pc = "1;1;";
224 			break;
225 		case kProtocolClass2_0:
226 			pc = "2;0;";
227 			break;
228 		case kProtocolClass2_1:
229 			pc = "2;1;";
230 			break;
231 		case kProtocolClass3_0:
232 			pc = "3;0;";
233 			break;
234 	}
235 	AppendString(pc);
236 
237 	if (comment != NULL) {
238 		AppendString("Comment ");
239 		AppendString(comment);
240 	}
241 
242 	AppendString("\n");
243 }
244 
245 void
246 PCL6Writer::PJLFooter()
247 {
248 	AppendString("\033%-12345X@PJL EOJ\n"
249 	                 "\033%-12345X");
250 }
251 
252 
253 void
254 PCL6Writer::BeginSession(uint16 xres, uint16 yres, UnitOfMeasure unitOfMeasure, ErrorReporting errorReporting) {
255 	AppendDataXY(xres, yres);
256 	AppendAttribute(kUnitsPerMeasure);
257 
258 	AppendData((uint8)unitOfMeasure);
259 	AppendAttribute(kMeasure);
260 
261 	AppendData((uint8)errorReporting);
262 	AppendAttribute(kErrorReport);
263 
264 	AppendOperator(kBeginSession);
265 }
266 
267 void
268 PCL6Writer::EndSession() {
269 	AppendOperator(kEndSession);
270 }
271 
272 void
273 PCL6Writer::OpenDataSource() {
274 	AppendData((uint8)kDefaultDataSource);
275 	AppendAttribute(kSourceType);
276 
277 	AppendData((uint8)kBinaryLowByteFirst);
278 	AppendAttribute(kDataOrg);
279 
280 	AppendOperator(kOpenDataSource);
281 }
282 
283 void
284 PCL6Writer::CloseDataSource() {
285 	AppendOperator(kCloseDataSource);
286 }
287 
288 
289 void
290 PCL6Writer::BeginPage(Orientation orientation, MediaSize mediaSize, MediaSource mediaSource) {
291 	AppendData((uint8)orientation);
292 	AppendAttribute(kOrientation);
293 
294 	AppendData((uint8)mediaSize);
295 	AppendAttribute(kMediaSize);
296 
297 	AppendData((uint8)mediaSource);
298 	AppendAttribute(kMediaSource);
299 
300 	AppendOperator(kBeginPage);
301 }
302 
303 void
304 PCL6Writer::BeginPage(Orientation orientation, MediaSize mediaSize, MediaSource mediaSource, DuplexPageMode duplexPageMode, MediaSide mediaSide) {
305 	AppendData((uint8)orientation);
306 	AppendAttribute(kOrientation);
307 
308 	AppendData((uint8)mediaSize);
309 	AppendAttribute(kMediaSize);
310 
311 	AppendData((uint8)mediaSource);
312 	AppendAttribute(kMediaSource);
313 
314 	AppendData((uint8)duplexPageMode);
315 	AppendAttribute(kDuplexPageMode);
316 
317 	AppendData((uint8)mediaSide);
318 	AppendAttribute(kDuplexPageSide);
319 
320 	AppendOperator(kBeginPage);
321 }
322 
323 void
324 PCL6Writer::EndPage(uint16 copies) {
325 //	if (copies != 1) {
326 		AppendData(copies);
327 		AppendAttribute(kPageCopies);
328 //	}
329 
330 	AppendOperator(kEndPage);
331 }
332 
333 
334 void
335 PCL6Writer::SetPageOrigin(int16 x, int16 y) {
336 	AppendDataXY(x, y);
337 	AppendAttribute(kPageOrigin);
338 
339 	AppendOperator(kSetPageOrigin);
340 }
341 
342 void
343 PCL6Writer::SetColorSpace(ColorSpace colorSpace) {
344 	AppendData((uint8)colorSpace);
345 	AppendAttribute(kColorSpace);
346 
347 	AppendOperator(kSetColorSpace);
348 }
349 
350 void
351 PCL6Writer::SetPaintTxMode(Transparency transparency) {
352 	AppendData((uint8)transparency);
353 	AppendAttribute(kTxMode);
354 
355 	AppendOperator(kSetPaintTxMode);
356 }
357 
358 void
359 PCL6Writer::SetSourceTxMode(Transparency transparency) {
360 	AppendData((uint8)transparency);
361 	AppendAttribute(kTxMode);
362 
363 	AppendOperator(kSetSourceTxMode);
364 }
365 
366 void
367 PCL6Writer::SetROP(uint8 rop) {
368 	AppendData((uint8)rop);
369 	AppendAttribute(kROP3);
370 
371 	AppendOperator(kSetROP);
372 }
373 
374 void
375 PCL6Writer::SetCursor(int16 x, int16 y) {
376 	AppendDataXY(x, y);
377 	AppendAttribute(kPoint);
378 
379 	AppendOperator(kSetCursor);
380 }
381 
382 
383 void
384 PCL6Writer::BeginImage(ColorMapping colorMapping, ColorDepth colorDepth, uint16 sourceWidth, uint16 sourceHeight, uint16 destWidth, uint16 destHeight) {
385 	AppendData((uint8)colorMapping);
386 	AppendAttribute(kColorMapping);
387 
388 	AppendData((uint8)colorDepth);
389 	AppendAttribute(kColorDepth);
390 
391 	AppendData(sourceWidth);
392 	AppendAttribute(kSourceWidth);
393 
394 	AppendData(sourceHeight);
395 	AppendAttribute(kSourceHeight);
396 
397 	AppendDataXY(destWidth, destHeight);
398 	AppendAttribute(kDestinationSize);
399 
400 	AppendOperator(kBeginImage);
401 }
402 
403 void
404 PCL6Writer::ReadImage(Compression compression, uint16 startLine, uint16 blockHeight, uint8 padBytes) {
405 	AppendData(startLine);
406 	AppendAttribute(kStartLine);
407 
408 	AppendData(blockHeight);
409 	AppendAttribute(kBlockHeight);
410 
411 	AppendData((uint8)compression);
412 	AppendAttribute(kCompressMode);
413 
414 	if (padBytes < 1 || padBytes > 4) {
415 		// XXX throw exception
416 		return;
417 	}
418 	if (padBytes != 4) {
419 		AppendData((uint8)padBytes);
420 		AppendAttribute(kPadBytesMultiple);
421 	}
422 
423 	AppendOperator(kReadImage);
424 }
425 
426 void
427 PCL6Writer::EndImage() {
428 	AppendOperator(kEndImage);
429 }
430 
431 
432