Debian Codegraph @

src/lm/LabelManagerDriver.cpp

raw
1 // -*- C++ -*-
2 // $Id: LabelManagerDriver.cpp 16081 2011-09-16 07:55:54Z aleksandr $
3
4 // DYMO LabelWriter Drivers
5 // Copyright (C) 2008 Sanford L.P.
6
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
11
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21 #include "LabelManagerDriver.h"
22
23 #include <assert.h>
24 #include <stdio.h>
25 #include <algorithm>
26
27
28 namespace DymoPrinterDriver
29 {
30
31
32 const byte ESC = 0x1B;
33 const byte SYN = 0x16;
34
35 CLabelManagerDriver::CLabelManagerDriver(IPrintEnvironment& Environment):
36 Environment_(Environment),
37 CutOptions_(CLabelManagerDriver::coCut), Alignment_(alCenter), ContinuousPaper_(false), PrintChainMarksAtDocEnd_(false), AutoPaper_(false), TapeAlignmentOffset_(0), TapeColor_(tcBlackOnWhite),
38 DeviceName_(), SupportAutoCut_(true), TSDevice_(false), MaxPrintableWidth_(96),
39 NormalLeader_(75), MinLeader_(55), AlignedLeader_(43), MinPageLines_(133),
40 LastDotTab_(size_t(-1)), LastBytesPerLine_(size_t(-1)), EmptyLinesCount_(0), PageNo_(1),
41 RasterLines_(), ShiftedRasterLine_(12), TSBuffer_(0),
42 HLockFile_(0)
43 {
44 }
45
46 CLabelManagerDriver::~CLabelManagerDriver()
47 {
48 }
49
50 void
51 CLabelManagerDriver::StartDoc()
52 {
53 PageNo_ = 1;
54
55 SendCommand(buffer_t(GetMaxBytesPerLine(), 0)); // clean
56
57 SendTapeColor(TapeColor_);
58 }
59
60 void
61 CLabelManagerDriver::EndDoc()
62 {
63 if (PrintChainMarksAtDocEnd_)
64 SendChainMark();
65
66 SendSkipLines(MinLeader_); // advance to the cutter
67
68 if (SupportAutoCut_ && !PrintChainMarksAtDocEnd_)
69 SendCut();
70
71 if(IsTSDevice())
72 {
73 FlushCommandTS();
74 EndCommandTS();
75 }
76 }
77
78 void
79 CLabelManagerDriver::StartPage()
80 {
81 LastDotTab_ = size_t(-1);
82 LastBytesPerLine_ = size_t(-1);
83 PageLineCount_ = 0;
84 EmptyLinesCount_ = 0;
85
86 RasterLines_.clear();
87
88 size_t LeaderLength = NormalLeader_;
89 if (PageNo_ > 1)
90 {
91 if ((CutOptions_ == coCut) && SupportAutoCut_)
92 {
93 SendSkipLines(MinLeader_); // advance to the cutter
94 LeaderLength -= MinLeader_;
95 SendCut();
96 }
97 else
98 SendChainMark();
99 }
100 else // first page
101 LeaderLength -= MinLeader_; // already at cutter position
102
103 if (!ContinuousPaper_)
104 SendSkipLines(LeaderLength);
105 }
106
107 void
108 CLabelManagerDriver::EndPage()
109 {
110 // last empty lines will not be drawn in case of Auto paper
111 // so, adjust the page length to properly calculate min page length
112 if (AutoPaper_)
113 PageLineCount_ -= EmptyLinesCount_;
114
115 // process cached data
116 if (Alignment_ == alLeft)
117 {
118 size_t MinLabelLength = MinPageLines_ + (NormalLeader_ - AlignedLeader_);
119 if (PageLineCount_ < MinLabelLength)
120 SendSkipLines(MinLabelLength - PageLineCount_);
121 SendCachedRasterLines();
122 }
123
124 if (!ContinuousPaper_)
125 {
126 size_t TrailerLength = NormalLeader_;
127
128 if (Alignment_ != alCenter)
129 TrailerLength = AlignedLeader_;
130
131 // for left we already take short label length into account
132 if (Alignment_ != alLeft)
133 {
134 size_t MinLabelLength = MinPageLines_ + (NormalLeader_ - TrailerLength);
135 if (PageLineCount_ < MinLabelLength)
136 TrailerLength += MinLabelLength - PageLineCount_;
137 }
138
139 // draw empty lines at the and, so the label has a full length
140 if (!AutoPaper_)
141 TrailerLength += EmptyLinesCount_;
142 EmptyLinesCount_ = 0;
143
144 SendSkipLines(TrailerLength);
145 }
146
147 ++PageNo_;
148 }
149
150
151 void
152 CLabelManagerDriver::GetBlanks(
153 const buffer_t& Buf, size_t& LeaderBlanks, size_t& TrailerBlanks)
154 {
155 size_t i = 0;
156
157 LeaderBlanks = 0;
158 TrailerBlanks = 0;
159
160 size_t BufSize = Buf.size();
161
162 // count left spaces
163 for (i = 0; i < BufSize; ++i)
164 if (Buf[i] == 0)
165 ++LeaderBlanks;
166 else
167 break;
168
169 if (i == BufSize) return;
170
171 // count right spaces
172 for (i = BufSize - 1; i >= 0; --i)
173 if (Buf[i] == 0)
174 ++TrailerBlanks;
175 else
176 break;
177 } // GetBlanks()
178
179
180 void
181 CLabelManagerDriver::ProcessRasterLine(const buffer_t& lineBuffer)
182 {
183 ++PageLineCount_;
184
185 buffer_t b = lineBuffer;
186
187 if (b.size() > GetMaxBytesPerLine())
188 b = buffer_t(b.begin(), b.begin() + GetMaxBytesPerLine());
189
190 if (Alignment_ == alLeft)
191 RasterLines_.push_back(b); // save for future reversing
192 else
193 ProcessRasterLineInternal(b);
194 }
195
196 void
197 CLabelManagerDriver::ProcessRasterLineInternal(const buffer_t& LineBuffer)
198 {
199 ShiftData(LineBuffer, ShiftedRasterLine_, GetShiftValue(LineBuffer.size()));
200
201 size_t LeaderBlanks = 0;
202 size_t TrailerBlanks = 0;
203
204 // get blanks count
205 GetBlanks(ShiftedRasterLine_, LeaderBlanks, TrailerBlanks);
206
207 if (LeaderBlanks + TrailerBlanks == ShiftedRasterLine_.size())
208 {
209 // remember empty line
210 ++EmptyLinesCount_;
211 }
212 else // not empty line
213 {
214 // skip empty lines
215 if (EmptyLinesCount_)
216 SendSkipLines(EmptyLinesCount_);
217
218 EmptyLinesCount_ = 0;
219
220 // set dot tab
221 if (LastDotTab_ != LeaderBlanks)
222 {
223 SendDotTab(LeaderBlanks);
224 LastDotTab_ = LeaderBlanks;
225 }
226
227 size_t BytesPerLine = ShiftedRasterLine_.size() - LeaderBlanks - TrailerBlanks;
228 if (LastBytesPerLine_ != BytesPerLine)
229 {
230 LastBytesPerLine_ = BytesPerLine;
231 SendBytesPerLine(LastBytesPerLine_);
232 }
233
234 byte syn = SYN;
235 SendCommand(&syn, sizeof(syn));
236 SendCommand(&ShiftedRasterLine_[0] + LeaderBlanks, BytesPerLine);
237 }
238 }
239
240 static byte
241 ReverseByte(byte Value)
242 {
243 byte ReversedByte = 0;
244 size_t BitsCopied = 0;
245
246 while (Value)
247 {
248 ReversedByte <<= 1;
249 if (Value & 0x1)
250 ReversedByte |= 0x1;
251
252 Value >>= 1;
253 ++BitsCopied;
254 }
255
256 ReversedByte <<= 8 - BitsCopied;
257
258 return ReversedByte;
259 }
260
261 void
262 CLabelManagerDriver::SendCachedRasterLines()
263 {
264 for (std::vector<buffer_t>::reverse_iterator it = RasterLines_.rbegin(); it < RasterLines_.rend(); ++it)
265 {
266 buffer_t& b = *it;
267 for (size_t i = 0; i < b.size(); ++i)
268 b[i] = ReverseByte(b[i]);
269
270 std::reverse(b.begin(), b.end());
271
272 ProcessRasterLineInternal(b);
273 }
274 }
275
276 void
277 CLabelManagerDriver::SetMaxPrintableWidth(size_t Value)
278 {
279 MaxPrintableWidth_ = Value;
280 ShiftedRasterLine_.resize(GetMaxBytesPerLine());
281 }
282
283 void
284 CLabelManagerDriver::SetNormalLeader(size_t Value)
285 {
286 NormalLeader_ = Value;
287 }
288
289 void
290 CLabelManagerDriver::SetMinLeader(size_t Value)
291 {
292 MinLeader_ = Value;
293 }
294
295 void
296 CLabelManagerDriver::SetAlignedLeader(size_t Value)
297 {
298 AlignedLeader_ = Value;
299 }
300
301 void
302 CLabelManagerDriver::SetMinPageLines(size_t Value)
303 {
304 MinPageLines_ = Value;
305 }
306
307 void
308 CLabelManagerDriver::SetCutOptions(CLabelManagerDriver::cut_t Value)
309 {
310 CutOptions_ = Value;
311 }
312
313 void
314 CLabelManagerDriver::SetAlignment(CLabelManagerDriver::alignment_t Value)
315 {
316 Alignment_ = Value;
317 }
318
319 void
320 CLabelManagerDriver::SetContinuousPaper(bool Value)
321 {
322 ContinuousPaper_ = Value;
323 }
324
325 void
326 CLabelManagerDriver::SetPrintChainMarksAtDocEnd(bool Value)
327 {
328 PrintChainMarksAtDocEnd_ = Value;
329 }
330
331 void
332 CLabelManagerDriver::SetAutoPaper(bool Value)
333 {
334 AutoPaper_ = Value;
335 }
336
337 void
338 CLabelManagerDriver::SetTapeAlignmentOffset(int Value)
339 {
340 TapeAlignmentOffset_ = Value;
341 }
342
343 void
344 CLabelManagerDriver::SetTapeColor(tape_color_t Value)
345 {
346 TapeColor_ = Value;
347 }
348
349 void
350 CLabelManagerDriver::SetDeviceName(const std::string& Value)
351 {
352 DeviceName_ = Value;
353 }
354
355 void
356 CLabelManagerDriver::SetSupportAutoCut(bool Value)
357 {
358 SupportAutoCut_ = Value;
359 }
360
361 void
362 CLabelManagerDriver::SetTSDevice(bool Value)
363 {
364 TSDevice_ = Value;
365 }
366
367 size_t
368 CLabelManagerDriver::GetMaxBytesPerLine()
369 {
370 return MaxPrintableWidth_ / 8;
371 }
372
373 const std::string&
374 CLabelManagerDriver::GetDeviceName()
375 {
376 return DeviceName_;
377 }
378
379 bool
380 CLabelManagerDriver::IsSupportAutoCut()
381 {
382 return SupportAutoCut_;
383 }
384
385 bool
386 CLabelManagerDriver::IsTSDevice()
387 {
388 return TSDevice_;
389 }
390
391 CLabelManagerDriver::cut_t
392 CLabelManagerDriver::GetCutOptions()
393 {
394 return CutOptions_;
395 }
396
397 CLabelManagerDriver::alignment_t
398 CLabelManagerDriver::GetAlignment()
399 {
400 return Alignment_;
401 }
402
403 bool
404 CLabelManagerDriver::IsContinuousPaper()
405 {
406 return ContinuousPaper_;
407 }
408
409 bool
410 CLabelManagerDriver::IsPrintChainMarksAtDocEnd()
411 {
412 return PrintChainMarksAtDocEnd_;
413 }
414
415 bool
416 CLabelManagerDriver::IsAutoPaper()
417 {
418 return AutoPaper_;
419 }
420
421 CLabelManagerDriver::tape_color_t
422 CLabelManagerDriver::GetTapeColor()
423 {
424 return TapeColor_;
425 }
426
427 int
428 CLabelManagerDriver::GetTapeAlignmentOffset()
429 {
430 return TapeAlignmentOffset_;
431 }
432
433 size_t
434 CLabelManagerDriver::GetMaxPrintableWidth()
435 {
436 return MaxPrintableWidth_;
437 }
438
439 size_t
440 CLabelManagerDriver::GetNormalLeader()
441 {
442 return NormalLeader_;
443 }
444
445 size_t
446 CLabelManagerDriver::GetMinLeader()
447 {
448 return MinLeader_;
449 }
450
451 size_t
452 CLabelManagerDriver::GetAlignedLeader()
453 {
454 return AlignedLeader_;
455 }
456
457 size_t
458 CLabelManagerDriver::GetMinPageLines()
459 {
460 return MinPageLines_;
461 }
462
463 void
464 CLabelManagerDriver::SendCommand(const byte* Buf, size_t BufSize)
465 {
466 SendCommand(buffer_t(Buf, Buf + BufSize));
467 }
468
469 void
470 CLabelManagerDriver::SendCommand(const buffer_t& Buf)
471 {
472 if(IsTSDevice())
473 SendCommandTS(Buf);
474 else
475 Environment_.WriteData(Buf);
476 }
477
478 void
479 CLabelManagerDriver::SendCommandTS(const buffer_t& Buf)
480 {
481 TSBuffer_.insert(TSBuffer_.end(), Buf.begin(), Buf.end());
482
483 fprintf(stderr, "DEBUG: SendCommandTS() size %d\n", int(TSBuffer_.size()));
484
485 if(TSBuffer_.size() > 4096)
486 FlushCommandTS();
487 }
488
489 void
490 CLabelManagerDriver::FlushCommandTS()
491 {
492 if(TSBuffer_.size() > 0)
493 {
494 fprintf(stderr, "DEBUG: FlushCommandTS() size %d\n", int(TSBuffer_.size()));
495
496 byte buf[] = {ESC, 'Y', 1, 0, 0, 0, 0};
497 size_t size = TSBuffer_.size();
498
499 buf[3] = size >> 24;
500 buf[4] = (size >> 16) & 0xFF;
501 buf[5] = (size >> 8) & 0xFF;
502 buf[6] = size & 0xFF;
503
504 fprintf(stderr, "DEBUG: FlushCommandTS() size 0x%02X 0x%02X 0x%02X 0x%02X\n", buf[3], buf[4], buf[5], buf[6]);
505
506 buffer_t prefix = buffer_t(buf, buf + 7);
507
508 TSBuffer_.insert(TSBuffer_.begin(), prefix.begin(), prefix.end());
509
510 Environment_.WriteData(TSBuffer_);
511
512 TSBuffer_.clear();
513 }
514 }
515
516 void
517 CLabelManagerDriver::EndCommandTS()
518 {
519 byte buf[] = {ESC, 'Y', 0, 0, 0, 0, 0};
520
521 Environment_.WriteData(buffer_t(buf, buf + sizeof(buf)));
522 }
523
524 void
525 CLabelManagerDriver::SendDotTab(size_t Value)
526 {
527 byte buf[] = {ESC, 'B', 0};
528 buf[2] = Value;
529
530 SendCommand(buf, sizeof(buf));
531 }
532
533 void
534 CLabelManagerDriver::SendCut()
535 {
536 byte buf[] = {ESC, 'E'};
537
538 SendCommand(buf, sizeof(buf));
539 }
540
541 void
542 CLabelManagerDriver::SendChainMark()
543 {
544 byte buf[] = {ESC, 'B', 0, ESC, 'D', 0, SYN};
545 buf[5] = GetMaxBytesPerLine();
546 SendCommand(buf, sizeof(buf));
547
548 buffer_t buf2(GetMaxBytesPerLine(), 0x99);
549 SendCommand(&buf2[0], buf2.size());
550
551 LastDotTab_ = size_t(-1);
552 LastBytesPerLine_ = size_t(-1);
553 }
554
555 void
556 CLabelManagerDriver::SendBytesPerLine(size_t Value)
557 {
558 byte buf[] = {ESC, 'D', 0};
559 buf[2] = Value;
560
561 SendCommand(buf, sizeof(buf));
562 }
563
564 void
565 CLabelManagerDriver::SendSkipLines(size_t Value)
566 {
567 if (Value > 0)
568 {
569 SendBytesPerLine(0);
570
571 buffer_t buf(Value, SYN);
572 SendCommand(&buf[0], Value);
573
574 LastBytesPerLine_ = size_t(-1);
575 }
576 }
577
578 void
579 CLabelManagerDriver::SendTapeColor(tape_color_t Value)
580 {
581 byte buf[] = {ESC, 'C', 0};
582 buf[2] = int(Value);
583
584 SendCommand(buf, sizeof(buf));
585 }
586
587 void
588 CLabelManagerDriver::ShiftDataRight(const buffer_t& Buf, buffer_t& ShiftedBuf, size_t ShiftValue)
589 {
590 // shift bytes first
591 int ShiftedLen = ShiftedBuf.size() - ShiftValue / 8;
592 size_t ShiftedOffset = ShiftValue / 8;
593 ShiftValue = ShiftValue % 8;
594
595 if ((ShiftedLen <= 0) || (Buf.size() == 0)) return;
596
597 // shift bits
598 ShiftedBuf[ShiftedOffset] = Buf[0] >> ShiftValue; // first
599 size_t i = 0;
600 for (i = 1; ((i < Buf.size()) && (i < size_t(ShiftedLen))); ++i)
601 ShiftedBuf[ShiftedOffset + i] = (Buf[i - 1] << (8 - ShiftValue)) | (Buf[i] >> ShiftValue);
602 if (i < size_t(ShiftedLen))
603 ShiftedBuf[ShiftedOffset + Buf.size()] = (Buf[Buf.size() - 1] << (8 - ShiftValue));
604 }
605
606 void
607 CLabelManagerDriver::ShiftDataLeft(const buffer_t& Buf, buffer_t& ShiftedBuf, size_t ShiftValue)
608 {
609 // shift bytes first
610 int ShiftedLen = ShiftedBuf.size() - ShiftValue / 8;
611 ShiftValue = ShiftValue % 8;
612
613 if ((ShiftedLen <= 0) || (Buf.size() == 0)) return;
614
615 // shift bits
616 size_t i = 0;
617 for (i = 0; ((i < Buf.size() - 1) && (i < size_t(ShiftedLen))); ++i)
618 ShiftedBuf[i] = (Buf[i] << ShiftValue) | (Buf[i + 1] >> (8 - ShiftValue));
619 if (i < size_t(ShiftedLen))
620 ShiftedBuf[Buf.size() - 1] = (Buf[Buf.size() - 1] << ShiftValue); // last
621 }
622
623
624 void
625 CLabelManagerDriver::ShiftData(const buffer_t& Buf, buffer_t& ShiftedBuf, int ShiftValue)
626 {
627 // clear shift buffer first
628 for (size_t i = 0; i < ShiftedBuf.size(); ++i)
629 ShiftedBuf[i] = 0;
630
631 if (ShiftValue >= 0)
632 ShiftDataRight(Buf, ShiftedBuf, ShiftValue);
633 else
634 ShiftDataLeft(Buf, ShiftedBuf, -ShiftValue);
635 }
636
637 int
638 CLabelManagerDriver::GetShiftValue(size_t RasterLineSize)
639 {
640 return (MaxPrintableWidth_ - RasterLineSize * 8) / 2 + TapeAlignmentOffset_;
641 }
642
643 buffer_t
644 CLabelManagerDriver::GetRequestStatusCommand()
645 {
646 byte buf[] = {ESC, 'A'};
647
648 return buffer_t(buf, buf + sizeof(buf));
649 }
650
651 }; // namespace
652
653
654 /*
655 * End of "$Id: LabelManagerDriver.cpp 16081 2011-09-16 07:55:54Z aleksandr $".
656 */