-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathESCParser.h
191 lines (163 loc) · 5.65 KB
/
ESCParser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* This file is part of UKNCBTL.
UKNCBTL is free software: you can redistribute it and/or modify it under the terms
of the GNU Lesser General Public License as published by the Free Software Foundation,
either version 3 of the License, or (at your option) any later version.
UKNCBTL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along with
UKNCBTL. If not, see <http://www.gnu.org/licenses/>. */
#ifndef _ESCPARSER_H_
#define _ESCPARSER_H_
#include <iostream>
#include <vector>
#ifndef WIN32
#include <string.h>
#define sprintf_s snprintf
#define _stricmp strcasecmp
#endif
extern unsigned short RobotronFont[];
//////////////////////////////////////////////////////////////////////
// Output drivers
enum
{
OUTPUT_DRIVER_UNKNOWN = 0,
OUTPUT_DRIVER_SVG = 1,
OUTPUT_DRIVER_POSTSCRIPT = 2,
OUTPUT_DRIVER_PDF = 3,
};
// Base abstract class for output drivers
class OutputDriver
{
protected:
std::ostream& m_output;
public:
OutputDriver(std::ostream& output) : m_output(output) { }
virtual ~OutputDriver() { }
public:
// Write beginning of the document
virtual void WriteBeginning(int pagestotal) { } // Overwrite if needed
// Write ending of the document
virtual void WriteEnding() { } // Overwrite if needed
// Write beginning of the page
virtual void WritePageBeginning(int pageno) { } // Overwrite if needed
// Write ending of the page
virtual void WritePageEnding() { } // Overwrite if needed
// Write strike by one pin
virtual void WriteStrike(float x, float y, float r) = 0; // Always overwrite
};
// Stub driver, does nothing
class OutputDriverStub : public OutputDriver
{
public:
OutputDriverStub(std::ostream& output) : OutputDriver(output) { };
public:
virtual void WriteStrike(float x, float y, float r) { }
};
// SVG driver, for one-page output only
class OutputDriverSvg : public OutputDriver
{
public:
OutputDriverSvg(std::ostream& output) : OutputDriver(output) { };
public:
virtual void WriteBeginning(int pagestotal);
virtual void WriteEnding();
virtual void WriteStrike(float x, float y, float r);
};
// PostScript driver with multipage support
class OutputDriverPostScript : public OutputDriver
{
public:
OutputDriverPostScript(std::ostream& output) : OutputDriver(output) { };
public:
virtual void WriteBeginning(int pagestotal);
virtual void WriteEnding();
virtual void WritePageBeginning(int pageno);
virtual void WritePageEnding();
virtual void WriteStrike(float x, float y, float r);
};
struct PdfXrefItem
{
std::streamoff offset;
int size;
char flag;
public:
PdfXrefItem(std::streamoff anoffset, int asize, char aflag)
{
offset = anoffset;
size = asize;
flag = aflag;
}
};
// PDF driver with multipage support
class OutputDriverPdf : public OutputDriver
{
public:
OutputDriverPdf(std::ostream& output) : OutputDriver(output) { strikesize = 0.1f; };
public:
virtual void WriteBeginning(int pagestotal);
virtual void WriteEnding();
virtual void WritePageBeginning(int pageno);
virtual void WritePageEnding();
virtual void WriteStrike(float x, float y, float r);
private:
std::vector<PdfXrefItem> xref;
std::string pagebuf;
float strikesize;
};
//////////////////////////////////////////////////////////////////////
// ESC/P interpreter
class EscInterpreter
{
private: // Input and output
std::istream& m_input;
OutputDriver& m_output;
private: // Current state
// Units for all the int values are equal to 1/10 point = 1/720 inch
int m_x, m_y; // Current position
int m_marginleft, m_margintop;
int m_limitright;
int m_limitbottom;
int m_shiftx, m_shifty; // Shift for text printout
bool m_printmode; // false - DRAFT, true - LQ
bool m_endofpage;
bool m_fontsp; // Øðèôò âðàçðÿäêó
bool m_fontdo; // Äâîéíàÿ ïå÷àòü
bool m_fontfe; // Æèðíûé øðèôò
bool m_fontks; // Ñæàòûé øðèôò
bool m_fontel; // Øðèôò "ýëèòà"
bool m_fontun; // Ïîä÷åðêèâàíèå
bool m_superscript; // Âåðõíèé ðåãèñòð
bool m_subscript; // Íèæíèé ðåãèñòð
unsigned char m_charset; // Íîìåð íàáîðà ñèìâîëîâ
public:
// Constructor
EscInterpreter(std::istream& input, OutputDriver& output);
// Interpret next character or escape sequense
bool InterpretNext();
// Interpret escape sequence
bool InterpretEscape();
// is the end of input stream reached
bool IsEndOfFile() const { return m_input.eof(); }
protected:
// Retrieve a next byte from the input
unsigned char GetNextByte();
// Update m_shiftx according to current font settings
void UpdateShiftX();
// Increment m_y by shifty; proceed to the next page if needed
void ShiftY(int shifty);
// End the current page
void NextPage();
// Reset the printer settings
void PrinterReset();
// Print graphics
void printGR9(int dx, bool dblspeed = false);
// Print graphics
void printGR24(int dx);
// Print the symbol using current charset
void PrintCharacter(unsigned char ch);
// Draw strike made by one pin
void DrawStrike(float x, float y);
};
//////////////////////////////////////////////////////////////////////
#endif // _ESCPARSER_H_