source: trunk/libdjvu/DjVuText.h @ 426

Last change on this file since 426 was 280, checked in by rbri, 12 years ago

DJVU plugin: djvulibre updated to version 3.5.22

File size: 10.8 KB
Line 
1//C-  -*- C++ -*-
2//C- -------------------------------------------------------------------
3//C- DjVuLibre-3.5
4//C- Copyright (c) 2002  Leon Bottou and Yann Le Cun.
5//C- Copyright (c) 2001  AT&T
6//C-
7//C- This software is subject to, and may be distributed under, the
8//C- GNU General Public License, either Version 2 of the license,
9//C- or (at your option) any later version. The license should have
10//C- accompanied the software or you may obtain a copy of the license
11//C- from the Free Software Foundation at http://www.fsf.org .
12//C-
13//C- This program is distributed in the hope that it will be useful,
14//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
15//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16//C- GNU General Public License for more details.
17//C-
18//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library from
19//C- Lizardtech Software.  Lizardtech Software has authorized us to
20//C- replace the original DjVu(r) Reference Library notice by the following
21//C- text (see doc/lizard2002.djvu and doc/lizardtech2007.djvu):
22//C-
23//C-  ------------------------------------------------------------------
24//C- | DjVu (r) Reference Library (v. 3.5)
25//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
26//C- | The DjVu Reference Library is protected by U.S. Pat. No.
27//C- | 6,058,214 and patents pending.
28//C- |
29//C- | This software is subject to, and may be distributed under, the
30//C- | GNU General Public License, either Version 2 of the license,
31//C- | or (at your option) any later version. The license should have
32//C- | accompanied the software or you may obtain a copy of the license
33//C- | from the Free Software Foundation at http://www.fsf.org .
34//C- |
35//C- | The computer code originally released by LizardTech under this
36//C- | license and unmodified by other parties is deemed "the LIZARDTECH
37//C- | ORIGINAL CODE."  Subject to any third party intellectual property
38//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
39//C- | non-exclusive license to make, use, sell, or otherwise dispose of
40//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
41//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
42//C- | General Public License.   This grant only confers the right to
43//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
44//C- | the extent such infringement is reasonably necessary to enable
45//C- | recipient to make, have made, practice, sell, or otherwise dispose
46//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
47//C- | any greater extent that may be necessary to utilize further
48//C- | modifications or combinations.
49//C- |
50//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
51//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
52//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
53//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
54//C- +------------------------------------------------------------------
55//
56// $Id: DjVuText.h,v 1.12 2008/01/27 11:36:27 leonb Exp $
57// $Name: release_3_5_22 $
58
59#ifndef _DJVUTEXT_H
60#define _DJVUTEXT_H
61#ifdef HAVE_CONFIG_H
62#include "config.h"
63#endif
64#if NEED_GNUG_PRAGMAS
65# pragma interface
66#endif
67
68
69
70/** @name DjVuText.h
71
72    Files #"DjVuText.h"# and #"DjVuText.cpp"# implement the mechanism for
73    text in DjVuImages.
74
75    This file implements annotations understood by the DjVu plugins
76    and encoders.
77
78
79    using: contents of #TXT*# chunks.
80
81    Contents of the #FORM:TEXT# should be passed to \Ref{DjVuText::decode}()
82    for parsing, which initializes \Ref{DjVuText::TXT}
83    and fills them with decoded data.
84    @memo Implements support for DjVuImage hidden text.
85    @author Andrei Erofeev <eaf@geocities.com>
86    @version
87    #$Id: DjVuText.h,v 1.12 2008/01/27 11:36:27 leonb Exp $# */
88//@{
89
90
91#include "GMapAreas.h"
92
93#ifdef HAVE_NAMESPACES
94namespace DJVU {
95# ifdef NOT_DEFINED // Just to fool emacs c++ mode
96}
97#endif
98#endif
99
100
101class ByteStream;
102
103// -------- DJVUTXT --------
104
105/** Description of the text contained in a DjVu page.  This class contains the
106    textual data for the page.  It describes the text as a hierarchy of zones
107    corresponding to page, column, region, paragraph, lines, words, etc...
108    The piece of text associated with each zone is represented by an offset
109    and a length describing a segment of a global UTF8 encoded string.  */
110
111class DJVUAPI DjVuTXT : public GPEnabled
112{
113protected:
114  DjVuTXT(void) {}
115public:
116  /// Default creator
117  static GP<DjVuTXT> create(void) {return new DjVuTXT();}
118
119  /** These constants are used to tell what a zone describes.
120      This can be useful for a copy/paste application.
121      The deeper we go into the hierarchy, the higher the constant. */
122  enum ZoneType { PAGE=1, COLUMN=2, REGION=3, PARAGRAPH=4, 
123                  LINE=5, WORD=6, CHARACTER=7 };
124  /** Data structure representing document textual components.
125      The text structure is represented by a hierarchy of rectangular zones. */
126  class DJVUAPI Zone
127  {
128  public:
129    Zone();
130    /** Type of the zone. */
131    enum ZoneType ztype;
132    /** Rectangle spanned by the zone */
133    GRect rect;
134    /** Position of the zone text in string #textUTF8#. */
135    int text_start;
136    /** Length of the zone text in string #textUTF8#. */
137    int text_length;
138    /** List of children zone. */
139    GList<Zone> children;
140    /** Appends another subzone inside this zone.  The new zone is initialized
141        with an empty rectangle, empty text, and has the same type as this
142        zone. */
143    Zone *append_child();
144    /** Find the text_start and text_end indicated by the given box. */
145    void get_text_with_rect(const GRect &box, 
146                            int &string_start,int &string_end ) const;
147    /** Find the zones used by the specified string and append them to the list. */
148    void find_zones(GList<Zone *> &list, 
149                    const int string_start, const int string_end) const;
150    /** Finds the smallest rectangles and appends them to the list. */
151    void get_smallest(GList<GRect> &list) const;
152    /** Finds the smallest rectangles and appends them to the list after
153        padding the smallest unit to fit width or height for the parent rectangle
154        and adding the number of specified pixels. */
155    void get_smallest(GList<GRect> &list,const int padding) const;
156    /// Find out this Zone's parent.
157    const Zone *get_parent(void) const;
158  private:
159    friend class DjVuTXT;
160    const Zone *zone_parent;
161    void cleartext();
162    void normtext(const char *instr, GUTF8String &outstr);
163    unsigned int memuse() const;
164    static const int version;
165    void encode(const GP<ByteStream> &bs, 
166                const Zone * parent=0, const Zone * prev=0) const;
167    void decode(const GP<ByteStream> &bs, int maxtext,
168                const Zone * parent=0, const Zone * prev=0);
169  };
170  /** Textual data for this page. 
171      The content of this string is encoded using the UTF8 code.
172      This code corresponds to ASCII for the first 127 characters.
173      Columns, regions, paragraph and lines are delimited by the following
174      control character:
175      \begin{tabular}{lll}
176        {\bf Name} & {\bf Octal} & {\bf Ascii name} \\\hline\\
177        {\tt DjVuText::end_of_column}    & 013 & VT, Vertical Tab \\
178        {\tt DjVuText::end_of_region}    & 035 & GS, Group Separator \\
179        {\tt DjVuText::end_of_paragraph} & 037 & US, Unit Separator \\
180        {\tt DjVuText::end_of_line}      & 012 & LF: Line Feed
181      \end{tabular} */
182  GUTF8String textUTF8;
183  static const char end_of_column    ;      // VT: Vertical Tab
184  static const char end_of_region    ;      // GS: Group Separator
185  static const char end_of_paragraph ;      // US: Unit Separator
186  static const char end_of_line      ;      // LF: Line Feed
187  /** Main zone in the document.
188      This zone represent the page. */
189  Zone page_zone;
190  /** Tests whether there is a meaningful zone hierarchy. */
191  int has_valid_zones() const;
192  /** Normalize textual data.  Assuming that a zone hierarchy has been built
193      and represents the reading order.  This function reorganizes the string
194      #textUTF8# by gathering the highest level text available in the zone
195      hierarchy.  The text offsets and lengths are recomputed for all the
196      zones in the hierarchy. Separators are inserted where appropriate. */
197  void normalize_text();
198  /** Encode data for a TXT chunk. */
199  void encode(const GP<ByteStream> &bs) const;
200  /** Decode data from a TXT chunk. */
201  void decode(const GP<ByteStream> &bs);
202  /** Returns a copy of this object. */
203  GP<DjVuTXT> copy(void) const;
204  /// Write XML formated text.
205  void writeText(ByteStream &bs,const int height) const;
206  /// Get XML formatted text.
207  GUTF8String get_xmlText(const int height) const;
208  /** Find the text specified by the rectangle. */ 
209  GList<Zone*> find_text_in_rect(GRect target_rect, GUTF8String &text) const;
210  /** Find the text specified by the rectangle. */
211  GList<GRect> find_text_with_rect(const GRect &box, GUTF8String &text, const int padding=0) const;
212  /** Get all zones of zone type zone_type under node parent.
213      zone_list contains the return value. */
214  void get_zones(int zone_type, const Zone *parent, GList<Zone *> & zone_list) const;
215  /** Returns the number of bytes needed by this data structure. It's
216      used by caching routines to estimate the size of a \Ref{DjVuImage}. */
217  unsigned int get_memory_usage() const;
218};
219
220inline const DjVuTXT::Zone *
221DjVuTXT::Zone::get_parent(void) const
222{
223  return zone_parent;
224}
225
226
227class DJVUAPI DjVuText : public GPEnabled
228{
229protected:
230   DjVuText(void) {}
231public:
232   /// Default creator.
233   static GP<DjVuText> create(void) {return new DjVuText();}
234
235      /** Decodes a sequence of annotation chunks and merges contents of every
236          chunk with previously decoded information. This function
237          should be called right after applying \Ref{IFFByteStream::get_chunk}()
238          to data from #FORM:TEXT#. */
239   void decode(const GP<ByteStream> &bs);
240
241      /** Encodes all annotations back into a sequence of chunks to be put
242          inside a #FORM:TEXT#. */
243   void encode(const GP<ByteStream> &bs);
244
245      /// Returns a copy of this object
246   GP<DjVuText> copy(void) const;
247
248      /** Returns the number of bytes needed by this data structure. It's
249          used by caching routines to estimate the size of a \Ref{DjVuImage}. */
250   inline unsigned int get_memory_usage() const;
251
252   /// Write XML formated text.
253   void writeText(ByteStream &bs,const int height) const;
254
255   /// Get XML formatted text.
256   GUTF8String get_xmlText(const int height) const;
257
258   GP<DjVuTXT>  txt;
259private: // dummy stuff
260   static void decode(ByteStream *);
261   static void  encode(ByteStream *);
262};
263
264//@}
265
266inline unsigned int
267DjVuText::get_memory_usage() const
268{
269  return (txt)?(txt->get_memory_usage()):0;
270}
271
272
273// ----- THE END
274
275#ifdef HAVE_NAMESPACES
276}
277# ifndef NOT_USING_DJVU_NAMESPACE
278using namespace DJVU;
279# endif
280#endif
281#endif
282
283
Note: See TracBrowser for help on using the repository browser.