Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

TextOutputDev.h

Go to the documentation of this file.
00001 //========================================================================
00002 //
00003 // TextOutputDev.h
00004 //
00005 // Copyright 1997-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #ifndef TEXTOUTPUTDEV_H
00010 #define TEXTOUTPUTDEV_H
00011 
00012 #ifdef __GNUC__
00013 #pragma interface
00014 #endif
00015 
00016 #include <stdio.h>
00017 #include "gtypes.h"
00018 #include "GfxFont.h"
00019 #include "OutputDev.h"
00020 
00021 class GfxState;
00022 class GString;
00023 
00024 //------------------------------------------------------------------------
00025 
00026 typedef void (*TextOutputFunc)(void *stream, char *text, int len);
00027 
00028 //------------------------------------------------------------------------
00029 // TextString
00030 //------------------------------------------------------------------------
00031 
00032 class TextString {
00033 public:
00034 
00035   // Constructor.
00036   TextString(GfxState *state, fouble fontSize);
00037 
00038   // Destructor.
00039   ~TextString();
00040 
00041   // Add a character to the string.
00042   void addChar(GfxState *state, fouble x, fouble y,
00043                fouble dx, fouble dy, Unicode u);
00044 
00045 private:
00046 
00047   fouble xMin, xMax;            // bounding box x coordinates
00048   fouble yMin, yMax;            // bounding box y coordinates
00049   int col;                      // starting column
00050   Unicode *text;                // the text
00051   fouble *xRight;               // right-hand x coord of each char
00052   int len;                      // length of text and xRight
00053   int size;                     // size of text and xRight arrays
00054   TextString *yxNext;           // next string in y-major order
00055   TextString *xyNext;           // next string in x-major order
00056 
00057   friend class TextPage;
00058 };
00059 
00060 //------------------------------------------------------------------------
00061 // TextPage
00062 //------------------------------------------------------------------------
00063 
00064 class TextPage {
00065 public:
00066 
00067   // Constructor.
00068   TextPage(GBool rawOrderA);
00069 
00070   // Destructor.
00071   ~TextPage();
00072 
00073   // Update the current font.
00074   void updateFont(GfxState *state);
00075 
00076   // Begin a new string.
00077   void beginString(GfxState *state);
00078 
00079   // Add a character to the current string.
00080   void addChar(GfxState *state, fouble x, fouble y,
00081                fouble dx, fouble dy, Unicode *u, int uLen);
00082 
00083   // End the current string, sorting it into the list of strings.
00084   void endString();
00085 
00086   // Coalesce strings that look like parts of the same line.
00087   void coalesce();
00088 
00089   // Find a string.  If <top> is true, starts looking at top of page;
00090   // otherwise starts looking at <xMin>,<yMin>.  If <bottom> is true,
00091   // stops looking at bottom of page; otherwise stops looking at
00092   // <xMax>,<yMax>.  If found, sets the text bounding rectange and
00093   // returns true; otherwise returns false.
00094   GBool findText(Unicode *s, int len,
00095                  GBool top, GBool bottom,
00096                  fouble *xMin, fouble *yMin,
00097                  fouble *xMax, fouble *yMax);
00098 
00099   // Get the text which is inside the specified rectangle.
00100   GString *getText(fouble xMin, fouble yMin,
00101                    fouble xMax, fouble yMax);
00102 
00103   // Dump contents of page to a file.
00104   void dump(void *outputStream, TextOutputFunc outputFunc);
00105 
00106   // Clear the page.
00107   void clear();
00108 
00109 private:
00110 
00111   GBool rawOrder;               // keep strings in content stream order
00112 
00113   TextString *curStr;           // currently active string
00114   fouble fontSize;              // current font size
00115 
00116   TextString *yxStrings;        // strings in y-major order
00117   TextString *xyStrings;        // strings in x-major order
00118   TextString *yxCur1, *yxCur2;  // cursors for yxStrings list
00119 
00120   int nest;                     // current nesting level (for Type 3 fonts)
00121 };
00122 
00123 //------------------------------------------------------------------------
00124 // TextOutputDev
00125 //------------------------------------------------------------------------
00126 
00127 class TextOutputDev: public OutputDev {
00128 public:
00129 
00130   // Open a text output file.  If <fileName> is NULL, no file is
00131   // written (this is useful, e.g., for searching text).  If
00132   // <rawOrder> is true, the text is kept in content stream order.
00133   TextOutputDev(char *fileName, GBool rawOrderA, GBool append);
00134 
00135   // Create a TextOutputDev which will write to a generic stream.  If
00136   // <rawOrder> is true, the text is kept in content stream order.
00137   TextOutputDev(TextOutputFunc func, void *stream, GBool rawOrderA);
00138 
00139   // Destructor.
00140   virtual ~TextOutputDev();
00141 
00142   // Check if file was successfully created.
00143   virtual GBool isOk() { return ok; }
00144 
00145   //---- get info about output device
00146 
00147   // Does this device use upside-down coordinates?
00148   // (Upside-down means (0,0) is the top left corner of the page.)
00149   virtual GBool upsideDown() { return gTrue; }
00150 
00151   // Does this device use drawChar() or drawString()?
00152   virtual GBool useDrawChar() { return gTrue; }
00153 
00154   // Does this device use beginType3Char/endType3Char?  Otherwise,
00155   // text in Type 3 fonts will be drawn with drawChar/drawString.
00156   virtual GBool interpretType3Chars() { return gFalse; }
00157 
00158   // Does this device need non-text content?
00159   virtual GBool needNonText() { return gFalse; }
00160 
00161   //----- initialization and control
00162 
00163   // Start a page.
00164   virtual void startPage(int pageNum, GfxState *state);
00165 
00166   // End a page.
00167   virtual void endPage();
00168 
00169   //----- update text state
00170   virtual void updateFont(GfxState *state);
00171 
00172   //----- text drawing
00173   virtual void beginString(GfxState *state, GString *s);
00174   virtual void endString(GfxState *state);
00175   virtual void drawChar(GfxState *state, fouble x, fouble y,
00176                         fouble dx, fouble dy,
00177                         fouble originX, fouble originY,
00178                         CharCode c, Unicode *u, int uLen);
00179 
00180   //----- special access
00181 
00182   // Find a string.  If <top> is true, starts looking at top of page;
00183   // otherwise starts looking at <xMin>,<yMin>.  If <bottom> is true,
00184   // stops looking at bottom of page; otherwise stops looking at
00185   // <xMax>,<yMax>.  If found, sets the text bounding rectange and
00186   // returns true; otherwise returns false.
00187   GBool findText(Unicode *s, int len,
00188                  GBool top, GBool bottom,
00189                  fouble *xMin, fouble *yMin,
00190                  fouble *xMax, fouble *yMax);
00191 
00192 private:
00193 
00194   TextOutputFunc outputFunc;    // output function
00195   void *outputStream;           // output stream
00196   GBool needClose;              // need to close the output file?
00197                                 //   (only if outputStream is a FILE*)
00198   TextPage *text;               // text for the current page
00199   GBool rawOrder;               // keep text in content stream order
00200   GBool ok;                     // set up ok?
00201 };
00202 
00203 #endif

Generated on Sat Nov 5 16:18:17 2005 for OPIE by  doxygen 1.4.2