filters

TextOutputDev.h
1//========================================================================
2//
3// TextOutputDev.h
4//
5// Copyright 1997-2002 Glyph & Cog, LLC
6//
7//========================================================================
8
9#ifndef TEXTOUTPUTDEV_H
10#define TEXTOUTPUTDEV_H
11
12#include <aconf.h>
13
14#ifdef USE_GCC_PRAGMAS
15#pragma interface
16#endif
17
18#include <stdio.h>
19#include "gtypes.h"
20#include "GfxFont.h"
21#include "OutputDev.h"
22
23class GfxState;
24class GString;
25class TextBlock;
26class TextLine;
27
28#undef TEXTOUT_DO_SYMBOLS
29
30//------------------------------------------------------------------------
31
32typedef void (*TextOutputFunc)(void *stream, char *text, int len);
33
34
35//------------------------------------------------------------------------
36// TextString
37//------------------------------------------------------------------------
38namespace PDFImport {
39 class String;
40 class Page;
41}
42
43class TextString {
44public:
45
46 // Constructor.
47 TextString(GfxState *state, double x0, double y0,
48 double fontSize);
49
50
51 // Destructor.
52 virtual ~TextString();
53
54 // Add a character to the string.
55 virtual void addChar(GfxState *state, double x, double y,
56 double dx, double dy, Unicode u);
57
58protected:
59 double xMin, xMax; // bounding box x coordinates
60 double yMin, yMax; // bounding box y coordinates
61 union {
62 GBool marked; // temporary flag used by coalesce()
63 GBool spaceAfter; // insert a space after this string?
64 };
65 Unicode *text; // the text
66 double *xRight; // right-hand x coord of each char
67 int len; // length of text and xRight
68 int size; // size of text and xRight arrays
69 TextString *next;
70
71 friend class TextPage;
72 friend class TextBlock;
73 friend class PDFImport::String;
74 friend class PDFImport::Page;
75};
76
77
78//------------------------------------------------------------------------
79// TextBlock
80//------------------------------------------------------------------------
81
82class TextBlock {
83public:
84
85 TextBlock();
86 ~TextBlock();
87
88 double xMin, xMax;
89 double yMin, yMax;
90 TextString *strings; // list of strings in the block
91 TextBlock *next; // next block in line
92 TextBlock *xyNext; // next block on xyBlocks list
93 Unicode *text; // Unicode text of the block, including
94 // spaces between strings
95 double *xRight; // right-hand x coord of each char
96 int len; // total number of Unicode characters
97 int convertedLen; // total number of converted characters
98 int *col; // starting column number for each
99 // Unicode character
100};
101
102//------------------------------------------------------------------------
103// TextLine
104//------------------------------------------------------------------------
105
106class TextLine {
107public:
108
109 TextLine();
110 ~TextLine();
111
112 TextBlock *blocks;
113 TextLine *next;
114 double yMin, yMax;
115};
116
117//------------------------------------------------------------------------
118// TextPage
119//------------------------------------------------------------------------
120
121class TextPage {
122public:
123
124 // Constructor.
125 TextPage(GBool rawOrderA);
126
127 // Destructor.
128 virtual ~TextPage();
129
130 // Update the current font.
131 void updateFont(GfxState *state);
132
133
134 // Begin a new string.
135 virtual void beginString(GfxState *state, double x0, double y0);
136
137 // Add a character to the current string.
138 void addChar(GfxState *state, double x, double y,
139 double dx, double dy, Unicode *u, int uLen);
140
141 // End the current string, sorting it into the list of strings.
142 virtual void endString();
143
144 // Add a string, sorting it into the list of strings.
145 virtual void addString(TextString *str);
146
147
148 // Coalesce strings that look like parts of the same line.
149 void coalesce();
150
151 // Find a string. If <top> is true, starts looking at top of page;
152 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
153 // stops looking at bottom of page; otherwise stops looking at
154 // <xMax>,<yMax>. If found, sets the text bounding rectange and
155 // returns true; otherwise returns false.
156 GBool findText(Unicode *s, int len,
157 GBool top, GBool bottom,
158 double *xMin, double *yMin,
159 double *xMax, double *yMax);
160
161 // Get the text which is inside the specified rectangle.
162 GString *getText(double xMin, double yMin,
163 double xMax, double yMax);
164
165 // Dump contents of page to a file.
166 void dump(void *outputStream, TextOutputFunc outputFunc);
167
168 // Clear the page.
169 virtual void clear();
170
171private:
172
173 GBool xyBefore(TextString *str1, TextString *str2);
174 GBool xyBefore(TextBlock *blk1, TextBlock *blk2);
175 GBool yxBefore(TextBlock *blk1, TextBlock *blk2);
176 double coalesceFit(TextString *str1, TextString *str2);
177
178 GBool rawOrder; // keep strings in content stream order
179
180 TextString *curStr; // currently active string
181 double fontSize; // current font size
182
183 TextString *xyStrings; // strings in x-major order (before
184 // they're sorted into lines)
185 TextString *xyCur1, *xyCur2; // cursors for xyStrings list
186 TextLine *lines; // list of lines
187
188 int nest; // current nesting level (for Type 3 fonts)
189
190 int nTinyChars; // number of "tiny" chars seen so far
191
192 friend class PDFImport::Page;
193};
194
195//------------------------------------------------------------------------
196// TextOutputDev
197//------------------------------------------------------------------------
198
199class TextOutputDev: public OutputDev {
200public:
201
202 // Open a text output file. If <fileName> is NULL, no file is
203 // written (this is useful, e.g., for searching text). If
204 // <rawOrder> is true, the text is kept in content stream order.
205 TextOutputDev(char *fileName, GBool rawOrderA, GBool append);
206
207 // Create a TextOutputDev which will write to a generic stream. If
208 // <rawOrder> is true, the text is kept in content stream order.
209 TextOutputDev(TextOutputFunc func, void *stream, GBool rawOrderA);
210
211 // Destructor.
212 virtual ~TextOutputDev();
213
214 // Check if file was successfully created.
215 virtual GBool isOk() { return ok; }
216
217 //---- get info about output device
218
219 // Does this device use upside-down coordinates?
220 // (Upside-down means (0,0) is the top left corner of the page.)
221 virtual GBool upsideDown() { return gTrue; }
222
223 // Does this device use drawChar() or drawString()?
224 virtual GBool useDrawChar() { return gTrue; }
225
226 // Does this device use beginType3Char/endType3Char? Otherwise,
227 // text in Type 3 fonts will be drawn with drawChar/drawString.
228 virtual GBool interpretType3Chars() { return gFalse; }
229
230 // Does this device need non-text content?
231 virtual GBool needNonText() { return gFalse; }
232
233 //----- initialization and control
234
235 // Start a page.
236 virtual void startPage(int pageNum, GfxState *state);
237
238 // End a page.
239 virtual void endPage();
240
241 //----- update text state
242 virtual void updateFont(GfxState *state);
243
244 //----- text drawing
245 virtual void beginString(GfxState *state, GString *s);
246 virtual void endString(GfxState *state);
247 virtual void drawChar(GfxState *state, double x, double y,
248 double dx, double dy,
249 double originX, double originY,
250 CharCode c, Unicode *u, int uLen);
251
252 //----- path painting
253
254 //----- special access
255
256 // Find a string. If <top> is true, starts looking at top of page;
257 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
258 // stops looking at bottom of page; otherwise stops looking at
259 // <xMax>,<yMax>. If found, sets the text bounding rectange and
260 // returns true; otherwise returns false.
261 GBool findText(Unicode *s, int len,
262 GBool top, GBool bottom,
263 double *xMin, double *yMin,
264 double *xMax, double *yMax);
265
266 // Get the text which is inside the specified rectangle.
267 GString *getText(double xMin, double yMin,
268 double xMax, double yMax);
269
270private:
271
272 TextOutputFunc outputFunc; // output function
273 void *outputStream; // output stream
274 GBool needClose; // need to close the output file?
275 // (only if outputStream is a FILE*)
276 TextPage *text; // text for the current page
277 GBool rawOrder; // keep text in content stream order
278 GBool ok; // set up ok?
279
280};
281
282#endif