1 //========================================================================
5 // Copyright 1997-2002 Glyph & Cog, LLC
7 //========================================================================
9 #ifndef TEXTOUTPUTDEV_H
10 #define TEXTOUTPUTDEV_H
14 #ifdef USE_GCC_PRAGMAS
21 #include "OutputDev.h"
28 #undef TEXTOUT_DO_SYMBOLS
30 //------------------------------------------------------------------------
32 typedef void (*TextOutputFunc)(void *stream, char *text, int len);
35 //------------------------------------------------------------------------
37 //------------------------------------------------------------------------
43 TextString(GfxState *state, double x0, double y0,
50 // Add a character to the string.
51 void addChar(GfxState *state, double x, double y,
52 double dx, double dy, Unicode u);
56 double xMin, xMax; // bounding box x coordinates
57 double yMin, yMax; // bounding box y coordinates
59 GBool marked; // temporary flag used by coalesce()
60 GBool spaceAfter; // insert a space after this string?
62 Unicode *text; // the text
63 double *xRight; // right-hand x coord of each char
64 int len; // length of text and xRight
65 int size; // size of text and xRight arrays
68 friend class TextPage;
69 friend class TextBlock;
72 //------------------------------------------------------------------------
74 //------------------------------------------------------------------------
80 TextPage(GBool rawOrderA);
85 // Update the current font.
86 void updateFont(GfxState *state);
89 // Begin a new string.
90 void beginString(GfxState *state, double x0, double y0);
92 // Add a character to the current string.
93 void addChar(GfxState *state, double x, double y,
94 double dx, double dy, Unicode *u, int uLen);
96 // End the current string, sorting it into the list of strings.
99 // Add a string, sorting it into the list of strings.
100 void addString(TextString *str);
103 // Coalesce strings that look like parts of the same line.
106 // Find a string. If <top> is true, starts looking at top of page;
107 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
108 // stops looking at bottom of page; otherwise stops looking at
109 // <xMax>,<yMax>. If found, sets the text bounding rectange and
110 // returns true; otherwise returns false.
111 GBool findText(Unicode *s, int len,
112 GBool top, GBool bottom,
113 double *xMin, double *yMin,
114 double *xMax, double *yMax);
116 // Get the text which is inside the specified rectangle.
117 GString *getText(double xMin, double yMin,
118 double xMax, double yMax);
120 // Dump contents of page to a file.
121 void dump(void *outputStream, TextOutputFunc outputFunc);
128 GBool xyBefore(TextString *str1, TextString *str2);
129 GBool xyBefore(TextBlock *blk1, TextBlock *blk2);
130 GBool yxBefore(TextBlock *blk1, TextBlock *blk2);
131 double coalesceFit(TextString *str1, TextString *str2);
133 GBool rawOrder; // keep strings in content stream order
135 TextString *curStr; // currently active string
136 double fontSize; // current font size
138 TextString *xyStrings; // strings in x-major order (before
139 // they're sorted into lines)
140 TextString *xyCur1, *xyCur2; // cursors for xyStrings list
141 TextLine *lines; // list of lines
143 int nest; // current nesting level (for Type 3 fonts)
145 int nTinyChars; // number of "tiny" chars seen so far
149 //------------------------------------------------------------------------
151 //------------------------------------------------------------------------
153 class TextOutputDev: public OutputDev {
156 // Open a text output file. If <fileName> is NULL, no file is
157 // written (this is useful, e.g., for searching text). If
158 // <rawOrder> is true, the text is kept in content stream order.
159 TextOutputDev(char *fileName, GBool rawOrderA, GBool append);
161 // Create a TextOutputDev which will write to a generic stream. If
162 // <rawOrder> is true, the text is kept in content stream order.
163 TextOutputDev(TextOutputFunc func, void *stream, GBool rawOrderA);
166 virtual ~TextOutputDev();
168 // Check if file was successfully created.
169 virtual GBool isOk() { return ok; }
171 //---- get info about output device
173 // Does this device use upside-down coordinates?
174 // (Upside-down means (0,0) is the top left corner of the page.)
175 virtual GBool upsideDown() { return gTrue; }
177 // Does this device use drawChar() or drawString()?
178 virtual GBool useDrawChar() { return gTrue; }
180 // Does this device use beginType3Char/endType3Char? Otherwise,
181 // text in Type 3 fonts will be drawn with drawChar/drawString.
182 virtual GBool interpretType3Chars() { return gFalse; }
184 // Does this device need non-text content?
185 virtual GBool needNonText() { return gFalse; }
187 //----- initialization and control
190 virtual void startPage(int pageNum, GfxState *state);
193 virtual void endPage();
195 //----- update text state
196 virtual void updateFont(GfxState *state);
199 virtual void beginString(GfxState *state, GString *s);
200 virtual void endString(GfxState *state);
201 virtual void drawChar(GfxState *state, double x, double y,
202 double dx, double dy,
203 double originX, double originY,
204 CharCode c, Unicode *u, int uLen);
206 //----- path painting
208 //----- special access
210 // Find a string. If <top> is true, starts looking at top of page;
211 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
212 // stops looking at bottom of page; otherwise stops looking at
213 // <xMax>,<yMax>. If found, sets the text bounding rectange and
214 // returns true; otherwise returns false.
215 GBool findText(Unicode *s, int len,
216 GBool top, GBool bottom,
217 double *xMin, double *yMin,
218 double *xMax, double *yMax);
220 // Get the text which is inside the specified rectangle.
221 GString *getText(double xMin, double yMin,
222 double xMax, double yMax);
226 TextOutputFunc outputFunc; // output function
227 void *outputStream; // output stream
228 GBool needClose; // need to close the output file?
229 // (only if outputStream is a FILE*)
230 TextPage *text; // text for the current page
231 GBool rawOrder; // keep text in content stream order
232 GBool ok; // set up ok?