tesseract 3.04.01

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