]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/TextOutputDev.h
f681ecfa8d314135b32c7a27b70e5cc38a9440a0
[evince.git] / pdf / xpdf / 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
23 class GfxState;
24 class GString;
25 class TextBlock;
26 class TextLine;
27
28 #undef TEXTOUT_DO_SYMBOLS
29
30 //------------------------------------------------------------------------
31
32 typedef void (*TextOutputFunc)(void *stream, char *text, int len);
33
34
35 //------------------------------------------------------------------------
36 // TextString
37 //------------------------------------------------------------------------
38
39 class TextString {
40 public:
41
42   // Constructor.
43   TextString(GfxState *state, double x0, double y0,
44              double fontSize);
45
46
47   // Destructor.
48   ~TextString();
49
50   // Add a character to the string.
51   void addChar(GfxState *state, double x, double y,
52                double dx, double dy, Unicode u);
53
54 private:
55
56   double xMin, xMax;            // bounding box x coordinates
57   double yMin, yMax;            // bounding box y coordinates
58   union {
59     GBool marked;               // temporary flag used by coalesce()
60     GBool spaceAfter;           // insert a space after this string?
61   };
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
66   TextString *next;
67
68   friend class TextPage;
69   friend class TextBlock;
70 };
71
72 //------------------------------------------------------------------------
73 // TextPage
74 //------------------------------------------------------------------------
75
76 class TextPage {
77 public:
78
79   // Constructor.
80   TextPage(GBool rawOrderA);
81
82   // Destructor.
83   ~TextPage();
84
85   // Update the current font.
86   void updateFont(GfxState *state);
87
88
89   // Begin a new string.
90   void beginString(GfxState *state, double x0, double y0);
91
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);
95
96   // End the current string, sorting it into the list of strings.
97   void endString();
98
99   // Add a string, sorting it into the list of strings.
100   void addString(TextString *str);
101
102
103   // Coalesce strings that look like parts of the same line.
104   void coalesce();
105
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);
115
116   // Get the text which is inside the specified rectangle.
117   GString *getText(double xMin, double yMin,
118                    double xMax, double yMax);
119
120   // Dump contents of page to a file.
121   void dump(void *outputStream, TextOutputFunc outputFunc);
122
123   // Clear the page.
124   void clear();
125
126 private:
127
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);
132
133   GBool rawOrder;               // keep strings in content stream order
134
135   TextString *curStr;           // currently active string
136   double fontSize;              // current font size
137
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
142
143   int nest;                     // current nesting level (for Type 3 fonts)
144
145   int nTinyChars;               // number of "tiny" chars seen so far
146
147 };
148
149 //------------------------------------------------------------------------
150 // TextOutputDev
151 //------------------------------------------------------------------------
152
153 class TextOutputDev: public OutputDev {
154 public:
155
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);
160
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);
164
165   // Destructor.
166   virtual ~TextOutputDev();
167
168   // Check if file was successfully created.
169   virtual GBool isOk() { return ok; }
170
171   //---- get info about output device
172
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; }
176
177   // Does this device use drawChar() or drawString()?
178   virtual GBool useDrawChar() { return gTrue; }
179
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; }
183
184   // Does this device need non-text content?
185   virtual GBool needNonText() { return gFalse; }
186
187   //----- initialization and control
188
189   // Start a page.
190   virtual void startPage(int pageNum, GfxState *state);
191
192   // End a page.
193   virtual void endPage();
194
195   //----- update text state
196   virtual void updateFont(GfxState *state);
197
198   //----- text drawing
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);
205
206   //----- path painting
207
208   //----- special access
209
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);
219
220   // Get the text which is inside the specified rectangle.
221   GString *getText(double xMin, double yMin,
222                    double xMax, double yMax);
223
224 private:
225
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?
233
234 };
235
236 #endif