tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/ccutil/unichar.h
Go to the documentation of this file.
00001 
00002 // File:        unichar.h
00003 // Description: Unicode character/ligature class.
00004 // Author:      Ray Smith
00005 // Created:     Wed Jun 28 17:05:01 PDT 2006
00006 //
00007 // (C) Copyright 2006, Google Inc.
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 //
00019 
00020 #ifndef TESSERACT_CCUTIL_UNICHAR_H__
00021 #define TESSERACT_CCUTIL_UNICHAR_H__
00022 
00023 #include <memory.h>
00024 #include <string.h>
00025 
00026 // Maximum number of characters that can be stored in a UNICHAR. Must be
00027 // at least 4. Must not exceed 31 without changing the coding of length.
00028 #define UNICHAR_LEN 30
00029 
00030 // A UNICHAR_ID is the unique id of a unichar.
00031 typedef int UNICHAR_ID;
00032 
00033 // A variable to indicate an invalid or uninitialized unichar id.
00034 static const int INVALID_UNICHAR_ID = -1;
00035 // A special unichar that corresponds to INVALID_UNICHAR_ID.
00036 static const char INVALID_UNICHAR[] = "__INVALID_UNICHAR__";
00037 
00038 enum StrongScriptDirection {
00039   DIR_NEUTRAL = 0,        // Text contains only neutral characters.
00040   DIR_LEFT_TO_RIGHT = 1,  // Text contains no Right-to-Left characters.
00041   DIR_RIGHT_TO_LEFT = 2,  // Text contains no Left-to-Right characters.
00042   DIR_MIX = 3,            // Text contains a mixture of left-to-right
00043                           // and right-to-left characters.
00044 };
00045 
00046 // The UNICHAR class holds a single classification result. This may be
00047 // a single Unicode character (stored as between 1 and 4 utf8 bytes) or
00048 // multple Unicode characters representing the NFKC expansion of a ligature
00049 // such as fi, ffl etc. These are also stored as utf8.
00050 class UNICHAR {
00051  public:
00052   UNICHAR() {
00053     memset(chars, 0, UNICHAR_LEN);
00054   }
00055 
00056   // Construct from a utf8 string. If len<0 then the string is null terminated.
00057   // If the string is too long to fit in the UNICHAR then it takes only what
00058   // will fit.
00059   UNICHAR(const char* utf8_str, int len);
00060 
00061   // Construct from a single UCS4 character.
00062   explicit UNICHAR(int unicode);
00063 
00064   // Default copy constructor and operator= are OK.
00065 
00066   // Get the first character as UCS-4.
00067   int first_uni() const;
00068 
00069   // Get the length of the UTF8 string.
00070   int utf8_len() const {
00071     int len = chars[UNICHAR_LEN - 1];
00072     return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
00073   }
00074 
00075   // Get a UTF8 string, but NOT NULL terminated.
00076   const char* utf8() const {
00077     return chars;
00078   }
00079 
00080   // Get a terminated UTF8 string: Must delete[] it after use.
00081   char* utf8_str() const;
00082 
00083   // Get the number of bytes in the first character of the given utf8 string.
00084   static int utf8_step(const char* utf8_str);
00085 
00086   // A class to simplify iterating over and accessing elements of a UTF8
00087   // string. Note that unlike the UNICHAR class, const_iterator does NOT COPY or
00088   // take ownership of the underlying byte array. It also does not permit
00089   // modification of the array (as the name suggests).
00090   //
00091   // Example:
00092   //   for (UNICHAR::const_iterator it = UNICHAR::begin(str, str_len);
00093   //        it != UNICHAR::end(str, len);
00094   //        ++it) {
00095   //     tprintf("UCS-4 symbol code = %d\n", *it);
00096   //     char buf[5];
00097   //     int char_len = it.get_utf8(buf); buf[char_len] = '\0';
00098   //     tprintf("Char = %s\n", buf);
00099   //   }
00100   class const_iterator {
00101     typedef const_iterator CI;
00102 
00103    public:
00104     // Step to the next UTF8 character.
00105     // If the current position is at an illegal UTF8 character, then print an
00106     // error message and step by one byte. If the current position is at a NULL
00107     // value, don't step past it.
00108     const_iterator& operator++();
00109 
00110     // Return the UCS-4 value at the current position.
00111     // If the current position is at an illegal UTF8 value, return a single
00112     // space character.
00113     int operator*() const;
00114 
00115     // Store the UTF-8 encoding of the current codepoint into buf, which must be
00116     // at least 4 bytes long. Return the number of bytes written.
00117     // If the current position is at an illegal UTF8 value, writes a single
00118     // space character and returns 1.
00119     // Note that this method does not null-terminate the buffer.
00120     int get_utf8(char* buf) const;
00121     // Returns the number of bytes of the current codepoint. Returns 1 if the
00122     // current position is at an illegal UTF8 value.
00123     int utf8_len() const;
00124 
00125     // Return the pointer into the string at the current position.
00126     const char* utf8_data() const { return it_; }
00127 
00128     // Iterator equality operators.
00129     friend bool operator==(const CI& lhs, const CI& rhs) {
00130       return lhs.it_ == rhs.it_;
00131     }
00132     friend bool operator!=(const CI& lhs, const CI& rhs) {
00133       return !(lhs == rhs);
00134     }
00135 
00136    private:
00137     friend class UNICHAR;
00138     explicit const_iterator(const char* it) : it_(it) {}
00139 
00140     const char* it_;  // Pointer into the string.
00141   };
00142 
00143   // Create a start/end iterator pointing to a string. Note that these methods
00144   // are static and do NOT create a copy or take ownership of the underlying
00145   // array.
00146   static const_iterator begin(const char* utf8_str, const int byte_length);
00147   static const_iterator end(const char* utf8_str, const int byte_length);
00148 
00149  private:
00150   // A UTF-8 representation of 1 or more Unicode characters.
00151   // The last element (chars[UNICHAR_LEN - 1]) is a length if
00152   // its value < UNICHAR_LEN, otherwise it is a genuine character.
00153   char chars[UNICHAR_LEN];
00154 };
00155 
00156 #endif  // TESSERACT_CCUTIL_UNICHAR_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines