]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/TextOutputDev.h
Synched with Xpdf 0.92
[evince.git] / pdf / xpdf / TextOutputDev.h
1 //========================================================================
2 //
3 // TextOutputDev.h
4 //
5 // Copyright 1997 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #ifndef TEXTOUTPUTDEV_H
10 #define TEXTOUTPUTDEV_H
11
12 #ifdef __GNUC__
13 #pragma interface
14 #endif
15
16 #include <stdio.h>
17 #include "gtypes.h"
18 #include "GfxFont.h"
19 #include "OutputDev.h"
20
21 class GfxState;
22 class GString;
23
24 //------------------------------------------------------------------------
25
26 enum TextOutputCharSet {
27   textOutASCII7,
28   textOutLatin1,
29   textOutLatin2,
30   textOutLatin5
31 };
32
33 //------------------------------------------------------------------------
34 // TextString
35 //------------------------------------------------------------------------
36
37 class TextString {
38 public:
39
40   // Constructor.
41   TextString(GfxState *state, GBool hexCodes1);
42
43   // Destructor.
44   ~TextString();
45
46   // Add a character to the string.
47   void addChar(GfxState *state, double x, double y,
48                double dx, double dy,
49                Guchar c, TextOutputCharSet charSet);
50
51   // Add a 16-bit character to the string.
52   void addChar16(GfxState *state, double x, double y,
53                  double dx, double dy,
54                  int c, GfxFontCharSet16 charSet);
55
56 private:
57
58   double xMin, xMax;            // bounding box x coordinates
59   double yMin, yMax;            // bounding box y coordinates
60   int col;                      // starting column
61   GString *text;                // the text
62   double *xRight;               // right-hand x coord of each char
63   TextString *yxNext;           // next string in y-major order
64   TextString *xyNext;           // next string in x-major order
65   GBool hexCodes;               // subsetted font with hex char codes
66
67   friend class TextPage;
68 };
69
70 //------------------------------------------------------------------------
71 // TextPage
72 //------------------------------------------------------------------------
73
74 class TextPage {
75 public:
76
77   // Constructor.
78   TextPage(TextOutputCharSet charSet, GBool rawOrder);
79
80   // Destructor.
81   ~TextPage();
82
83   // Begin a new string.
84   void beginString(GfxState *state, GString *s, GBool hex1);
85
86   // Add a character to the current string.
87   void addChar(GfxState *state, double x, double y,
88                double dx, double dy, Guchar c);
89
90   // Add a 16-bit character to the current string.
91   void addChar16(GfxState *state, double x, double y,
92                  double dx, double dy, int c,
93                  GfxFontCharSet16 charSet);
94
95   // End the current string, sorting it into the list of strings.
96   void endString();
97
98   // Coalesce strings that look like parts of the same line.
99   void coalesce();
100
101   // Find a string.  If <top> is true, starts looking at top of page;
102   // otherwise starts looking at <xMin>,<yMin>.  If <bottom> is true,
103   // stops looking at bottom of page; otherwise stops looking at
104   // <xMax>,<yMax>.  If found, sets the text bounding rectange and
105   // returns true; otherwise returns false.
106   GBool findText(char *s, GBool top, GBool bottom,
107                  double *xMin, double *yMin,
108                  double *xMax, double *yMax);
109
110   // Get the text which is inside the specified rectangle.
111   GString *getText(double xMin, double yMin,
112                    double xMax, double yMax);
113
114   // Dump contents of page to a file.
115   void dump(FILE *f);
116
117   // Clear the page.
118   void clear();
119
120 private:
121
122   TextOutputCharSet charSet;    // character set
123   GBool rawOrder;               // keep strings in content stream order
124
125   TextString *curStr;           // currently active string
126
127   TextString *yxStrings;        // strings in y-major order
128   TextString *xyStrings;        // strings in x-major order
129   TextString *yxCur1, *yxCur2;  // cursors for yxStrings list
130
131   int nest;                     // current nesting level (for Type 3 fonts)
132 };
133
134 //------------------------------------------------------------------------
135 // TextOutputDev
136 //------------------------------------------------------------------------
137
138 class TextOutputDev: public OutputDev {
139 public:
140
141   // Open a text output file.  If <fileName> is NULL, no file is
142   // written (this is useful, e.g., for searching text).  Text is
143   // converted to the character set specified by <charSet>.  This
144   // should be set to textOutASCII7 for Japanese (EUC-JP) text.  If
145   // <rawOrder> is true, the text is kept in content stream order.
146   TextOutputDev(char *fileName, TextOutputCharSet charSet,
147                 GBool rawOrder);
148
149   // Destructor.
150   virtual ~TextOutputDev();
151
152   // Check if file was successfully created.
153   virtual GBool isOk() { return ok; }
154
155   //---- get info about output device
156
157   // Does this device use upside-down coordinates?
158   // (Upside-down means (0,0) is the top left corner of the page.)
159   virtual GBool upsideDown() { return gTrue; }
160
161   // Does this device use drawChar() or drawString()?
162   virtual GBool useDrawChar() { return gTrue; }
163
164   //----- initialization and control
165
166   // Start a page.
167   virtual void startPage(int pageNum, GfxState *state);
168
169   // End a page.
170   virtual void endPage();
171
172   //----- update text state
173   virtual void updateFont(GfxState *state);
174
175   //----- text drawing
176   virtual void beginString(GfxState *state, GString *s);
177   virtual void endString(GfxState *state);
178   virtual void drawChar(GfxState *state, double x, double y,
179                         double dx, double dy, Guchar c);
180   virtual void drawChar16(GfxState *state, double x, double y,
181                           double dx, double dy, int c);
182
183   //----- special access
184
185   // Find a string.  If <top> is true, starts looking at top of page;
186   // otherwise starts looking at <xMin>,<yMin>.  If <bottom> is true,
187   // stops looking at bottom of page; otherwise stops looking at
188   // <xMax>,<yMax>.  If found, sets the text bounding rectange and
189   // returns true; otherwise returns false.
190   GBool findText(char *s, GBool top, GBool bottom,
191                  double *xMin, double *yMin,
192                  double *xMax, double *yMax);
193
194 private:
195
196   FILE *f;                      // text file
197   GBool needClose;              // need to close the file?
198   TextPage *text;               // text for the current page
199   GBool rawOrder;               // keep text in content stream order
200   GBool hexCodes;               // subsetted font with hex char codes
201   GBool ok;                     // set up ok?
202 };
203
204 #endif