1 //========================================================================
5 // Copyright 1997-2002 Glyph & Cog, LLC
7 //========================================================================
9 #ifndef TEXTOUTPUTDEV_H
10 #define TEXTOUTPUTDEV_H
19 #include "OutputDev.h"
24 //------------------------------------------------------------------------
26 typedef void (*TextOutputFunc)(void *stream, char *text, int len);
28 //------------------------------------------------------------------------
30 //------------------------------------------------------------------------
36 TextString(GfxState *state, double fontSize);
41 // Add a character to the string.
42 void addChar(GfxState *state, double x, double y,
43 double dx, double dy, Unicode u);
47 double xMin, xMax; // bounding box x coordinates
48 double yMin, yMax; // bounding box y coordinates
49 int col; // starting column
50 Unicode *text; // the text
51 double *xRight; // right-hand x coord of each char
52 int len; // length of text and xRight
53 int size; // size of text and xRight arrays
54 TextString *yxNext; // next string in y-major order
55 TextString *xyNext; // next string in x-major order
57 friend class TextPage;
60 //------------------------------------------------------------------------
62 //------------------------------------------------------------------------
68 TextPage(GBool rawOrderA);
73 // Update the current font.
74 void updateFont(GfxState *state);
76 // Begin a new string.
77 void beginString(GfxState *state);
79 // Add a character to the current string.
80 void addChar(GfxState *state, double x, double y,
81 double dx, double dy, Unicode *u, int uLen);
83 // End the current string, sorting it into the list of strings.
86 // Coalesce strings that look like parts of the same line.
89 // Find a string. If <top> is true, starts looking at top of page;
90 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
91 // stops looking at bottom of page; otherwise stops looking at
92 // <xMax>,<yMax>. If found, sets the text bounding rectange and
93 // returns true; otherwise returns false.
94 GBool findText(Unicode *s, int len,
95 GBool top, GBool bottom,
96 double *xMin, double *yMin,
97 double *xMax, double *yMax);
99 // Get the text which is inside the specified rectangle.
100 GString *getText(double xMin, double yMin,
101 double xMax, double yMax);
103 // Dump contents of page to a file.
104 void dump(void *outputStream, TextOutputFunc outputFunc);
111 GBool rawOrder; // keep strings in content stream order
113 TextString *curStr; // currently active string
114 double fontSize; // current font size
116 TextString *yxStrings; // strings in y-major order
117 TextString *xyStrings; // strings in x-major order
118 TextString *yxCur1, *yxCur2; // cursors for yxStrings list
120 int nest; // current nesting level (for Type 3 fonts)
123 //------------------------------------------------------------------------
125 //------------------------------------------------------------------------
127 class TextOutputDev: public OutputDev {
130 // Open a text output file. If <fileName> is NULL, no file is
131 // written (this is useful, e.g., for searching text). If
132 // <rawOrder> is true, the text is kept in content stream order.
133 TextOutputDev(char *fileName, GBool rawOrderA, GBool append);
135 // Create a TextOutputDev which will write to a generic stream. If
136 // <rawOrder> is true, the text is kept in content stream order.
137 TextOutputDev(TextOutputFunc func, void *stream, GBool rawOrderA);
140 virtual ~TextOutputDev();
142 // Check if file was successfully created.
143 virtual GBool isOk() { return ok; }
145 //---- get info about output device
147 // Does this device use upside-down coordinates?
148 // (Upside-down means (0,0) is the top left corner of the page.)
149 virtual GBool upsideDown() { return gTrue; }
151 // Does this device use drawChar() or drawString()?
152 virtual GBool useDrawChar() { return gTrue; }
154 // Does this device use beginType3Char/endType3Char? Otherwise,
155 // text in Type 3 fonts will be drawn with drawChar/drawString.
156 virtual GBool interpretType3Chars() { return gFalse; }
158 // Does this device need non-text content?
159 virtual GBool needNonText() { return gFalse; }
161 //----- initialization and control
164 virtual void startPage(int pageNum, GfxState *state);
167 virtual void endPage();
169 //----- update text state
170 virtual void updateFont(GfxState *state);
173 virtual void beginString(GfxState *state, GString *s);
174 virtual void endString(GfxState *state);
175 virtual void drawChar(GfxState *state, double x, double y,
176 double dx, double dy,
177 double originX, double originY,
178 CharCode c, Unicode *u, int uLen);
180 //----- special access
182 // Find a string. If <top> is true, starts looking at top of page;
183 // otherwise starts looking at <xMin>,<yMin>. If <bottom> is true,
184 // stops looking at bottom of page; otherwise stops looking at
185 // <xMax>,<yMax>. If found, sets the text bounding rectange and
186 // returns true; otherwise returns false.
187 GBool findText(Unicode *s, int len,
188 GBool top, GBool bottom,
189 double *xMin, double *yMin,
190 double *xMax, double *yMax);
194 TextOutputFunc outputFunc; // output function
195 void *outputStream; // output stream
196 GBool needClose; // need to close the output file?
197 // (only if outputStream is a FILE*)
198 TextPage *text; // text for the current page
199 GBool rawOrder; // keep text in content stream order
200 GBool ok; // set up ok?