Tesseract  3.02
tesseract-ocr/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 // The UNICHAR class holds a single classification result. This may be
00039 // a single Unicode character (stored as between 1 and 4 utf8 bytes) or
00040 // multple Unicode characters representing the NFKC expansion of a ligature
00041 // such as fi, ffl etc. These are also stored as utf8.
00042 class UNICHAR {
00043  public:
00044   UNICHAR() {
00045     memset(chars, 0, UNICHAR_LEN);
00046   }
00047 
00048   // Construct from a utf8 string. If len<0 then the string is null terminated.
00049   // If the string is too long to fit in the UNICHAR then it takes only what
00050   // will fit.
00051   UNICHAR(const char* utf8_str, int len);
00052 
00053   // Construct from a single UCS4 character.
00054   explicit UNICHAR(int unicode);
00055 
00056   // Default copy constructor and operator= are OK.
00057 
00058   // Get the first character as UCS-4.
00059   int first_uni() const;
00060 
00061   // Get the length of the UTF8 string.
00062   int utf8_len() const {
00063     int len = chars[UNICHAR_LEN - 1];
00064     return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
00065   }
00066 
00067   // Get a UTF8 string, but NOT NULL terminated.
00068   const char* utf8() const {
00069     return chars;
00070   }
00071 
00072   // Get a terminated UTF8 string: Must delete[] it after use.
00073   char* utf8_str() const;
00074 
00075   // Get the number of bytes in the first character of the given utf8 string.
00076   static int utf8_step(const char* utf8_str);
00077 
00078  private:
00079   // A UTF-8 representation of 1 or more Unicode characters.
00080   // The last element (chars[UNICHAR_LEN - 1]) is a length if
00081   // its value < UNICHAR_LEN, otherwise it is a genuine character.
00082   char chars[UNICHAR_LEN];
00083 };
00084 
00085 #endif  // TESSERACT_CCUTIL_UNICHAR_H__