Tesseract  3.02
tesseract-ocr/ccstruct/werd.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:                        word.c
00003  * Description: Code for the WERD class.
00004  * Author:              Ray Smith
00005  * Created:             Tue Oct 08 14:32:12 BST 1991
00006  *
00007  * (C) Copyright 1991, Hewlett-Packard Ltd.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 #ifndef           WERD_H
00021 #define           WERD_H
00022 
00023 #include          "params.h"
00024 #include          "bits16.h"
00025 #include          "elst2.h"
00026 #include          "strngs.h"
00027 #include          "blckerr.h"
00028 #include          "stepblob.h"
00029 
00030 // Include automatically generated configuration file if running autoconf.
00031 #ifdef HAVE_CONFIG_H
00032 #include "config_auto.h"
00033 #endif
00034 
00035 enum WERD_FLAGS
00036 {
00037   W_SEGMENTED,                   //< correctly segmented
00038   W_ITALIC,                      //< italic text
00039   W_BOLD,                        //< bold text
00040   W_BOL,                         //< start of line
00041   W_EOL,                         //< end of line
00042   W_NORMALIZED,                  //< flags
00043   W_SCRIPT_HAS_XHEIGHT,          //< x-height concept makes sense.
00044   W_SCRIPT_IS_LATIN,             //< Special case latin for y. splitting.
00045   W_DONT_CHOP,                   //< fixed pitch chopped
00046   W_REP_CHAR,                    //< repeated character
00047   W_FUZZY_SP,                    //< fuzzy space
00048   W_FUZZY_NON,                   //< fuzzy nonspace
00049   W_INVERSE                      //< white on black
00050 };
00051 
00052 enum DISPLAY_FLAGS
00053 {
00054   /* Display flags bit number allocations */
00055   DF_BOX,                        //< Bounding box
00056   DF_TEXT,                       //< Correct ascii
00057   DF_POLYGONAL,                  //< Polyg approx
00058   DF_EDGE_STEP,                  //< Edge steps
00059   DF_BN_POLYGONAL,               //< BL normalisd polyapx
00060   DF_BLAMER                      //< Blamer information
00061 };
00062 
00063 class ROW;                       //forward decl
00064 
00065 class WERD : public ELIST2_LINK {
00066   public:
00067     WERD() {}
00068     // WERD constructed with:
00069     //   blob_list - blobs of the word (we take this list's contents)
00070     //   blanks - number of blanks before the word
00071     //   text - correct text (outlives WERD)
00072     WERD(C_BLOB_LIST *blob_list, uinT8 blanks, const char *text);
00073 
00074     // WERD constructed from:
00075     //   blob_list - blobs in the word
00076     //   clone - werd to clone flags, etc from.
00077     WERD(C_BLOB_LIST *blob_list, WERD *clone);
00078 
00079     // Construct a WERD from a single_blob and clone the flags from this.
00080     // W_BOL and W_EOL flags are set according to the given values.
00081     WERD* ConstructFromSingleBlob(bool bol, bool eol, C_BLOB* blob);
00082 
00083     ~WERD() {
00084     }
00085 
00086     // assignment
00087     WERD & operator= (const WERD &source);
00088 
00089     // This method returns a new werd constructed using the blobs in the input
00090     // all_blobs list, which correspond to the blobs in this werd object. The
00091     // blobs used to construct the new word are consumed and removed from the
00092     // input all_blobs list.
00093     // Returns NULL if the word couldn't be constructed.
00094     // Returns original blobs for which no matches were found in the output list
00095     // orphan_blobs (appends).
00096     WERD *ConstructWerdWithNewBlobs(C_BLOB_LIST *all_blobs,
00097                                     C_BLOB_LIST *orphan_blobs);
00098 
00099     // Accessors for reject / DUFF blobs in various formats
00100     C_BLOB_LIST *rej_cblob_list() {  // compact format
00101       return &rej_cblobs;
00102     }
00103 
00104     // Accessors for good blobs in various formats.
00105     C_BLOB_LIST *cblob_list() {  // get compact blobs
00106       return &cblobs;
00107     }
00108 
00109     uinT8 space() {  // access function
00110       return blanks;
00111     }
00112     void set_blanks(uinT8 new_blanks) {
00113       blanks = new_blanks;
00114     }
00115     int script_id() const {
00116       return script_id_;
00117     }
00118     void set_script_id(int id) {
00119       script_id_ = id;
00120     }
00121 
00122     TBOX bounding_box();  // compute bounding box
00123 
00124     const char *text() const { return correct.string(); }
00125     void set_text(const char *new_text) { correct = new_text; }
00126 
00127     BOOL8 flag(WERD_FLAGS mask) const { return flags.bit(mask); }
00128     void set_flag(WERD_FLAGS mask, BOOL8 value) { flags.set_bit(mask, value); }
00129 
00130     BOOL8 display_flag(uinT8 flag) const { return disp_flags.bit(flag); }
00131     void set_display_flag(uinT8 flag, BOOL8 value) {
00132       disp_flags.set_bit(flag, value);
00133     }
00134 
00135     WERD *shallow_copy();  // shallow copy word
00136 
00137     // reposition word by vector
00138     void move(const ICOORD vec);
00139 
00140     // join other's blobs onto this werd, emptying out other.
00141     void join_on(WERD* other);
00142 
00143     // copy other's blobs onto this word, leaving other intact.
00144     void copy_on(WERD* other);
00145 
00146     // tprintf word metadata (but not blob innards)
00147     void print();
00148 
00149     #ifndef GRAPHICS_DISABLED
00150     // plot word on window in a uniform colour
00151     void plot(ScrollView *window, ScrollView::Color colour);
00152     #endif  // GRAPHICS_DISABLED
00153 
00154     // Get the next color in the (looping) rainbow.
00155     static ScrollView::Color NextColor(ScrollView::Color colour);
00156 
00157     #ifndef GRAPHICS_DISABLED
00158     // plot word on window in a rainbow of colours
00159     void plot(ScrollView *window);
00160 
00161     // plot rejected blobs in a rainbow of colours
00162     void plot_rej_blobs(ScrollView *window);
00163     #endif  // GRAPHICS_DISABLED
00164 
00165  private:
00166     uinT8 blanks;                // no of blanks
00167     uinT8 dummy;                 // padding
00168     BITS16 flags;                // flags about word
00169     BITS16 disp_flags;           // display flags
00170     inT16 script_id_;            // From unicharset.
00171     STRING correct;              // correct text
00172     C_BLOB_LIST cblobs;          // compacted blobs
00173     C_BLOB_LIST rej_cblobs;      // DUFF blobs
00174 };
00175 
00176 ELIST2IZEH (WERD)
00177 #include          "ocrrow.h"     // placed here due to
00178 // compare words by increasing order of left edge, suitable for qsort(3)
00179 int word_comparator(const void *word1p, const void *word2p);
00180 #endif