tesseract  4.00.00dev
unichar.h
Go to the documentation of this file.
1 // File: unichar.h
3 // Description: Unicode character/ligature class.
4 // Author: Ray Smith
5 // Created: Wed Jun 28 17:05:01 PDT 2006
6 //
7 // (C) Copyright 2006, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
19 
20 #ifndef TESSERACT_CCUTIL_UNICHAR_H_
21 #define TESSERACT_CCUTIL_UNICHAR_H_
22 
23 #include <memory.h>
24 #include <string.h>
25 
26 template <typename T> class GenericVector;
27 
28 // Maximum number of characters that can be stored in a UNICHAR. Must be
29 // at least 4. Must not exceed 31 without changing the coding of length.
30 #define UNICHAR_LEN 30
31 
32 // A UNICHAR_ID is the unique id of a unichar.
33 typedef int UNICHAR_ID;
34 
35 // A variable to indicate an invalid or uninitialized unichar id.
36 static const int INVALID_UNICHAR_ID = -1;
37 // A special unichar that corresponds to INVALID_UNICHAR_ID.
38 static const char INVALID_UNICHAR[] = "__INVALID_UNICHAR__";
39 
41  DIR_NEUTRAL = 0, // Text contains only neutral characters.
42  DIR_LEFT_TO_RIGHT = 1, // Text contains no Right-to-Left characters.
43  DIR_RIGHT_TO_LEFT = 2, // Text contains no Left-to-Right characters.
44  DIR_MIX = 3, // Text contains a mixture of left-to-right
45  // and right-to-left characters.
46 };
47 
48 // The UNICHAR class holds a single classification result. This may be
49 // a single Unicode character (stored as between 1 and 4 utf8 bytes) or
50 // multiple Unicode characters representing the NFKC expansion of a ligature
51 // such as fi, ffl etc. These are also stored as utf8.
52 class UNICHAR {
53  public:
54  UNICHAR() {
55  memset(chars, 0, UNICHAR_LEN);
56  }
57 
58  // Construct from a utf8 string. If len<0 then the string is null terminated.
59  // If the string is too long to fit in the UNICHAR then it takes only what
60  // will fit.
61  UNICHAR(const char* utf8_str, int len);
62 
63  // Construct from a single UCS4 character.
64  explicit UNICHAR(int unicode);
65 
66  // Default copy constructor and operator= are OK.
67 
68  // Get the first character as UCS-4.
69  int first_uni() const;
70 
71  // Get the length of the UTF8 string.
72  int utf8_len() const {
73  int len = chars[UNICHAR_LEN - 1];
74  return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
75  }
76 
77  // Get a UTF8 string, but NOT NULL terminated.
78  const char* utf8() const {
79  return chars;
80  }
81 
82  // Get a terminated UTF8 string: Must delete[] it after use.
83  char* utf8_str() const;
84 
85  // Get the number of bytes in the first character of the given utf8 string.
86  static int utf8_step(const char* utf8_str);
87 
88  // A class to simplify iterating over and accessing elements of a UTF8
89  // string. Note that unlike the UNICHAR class, const_iterator does NOT COPY or
90  // take ownership of the underlying byte array. It also does not permit
91  // modification of the array (as the name suggests).
92  //
93  // Example:
94  // for (UNICHAR::const_iterator it = UNICHAR::begin(str, str_len);
95  // it != UNICHAR::end(str, len);
96  // ++it) {
97  // tprintf("UCS-4 symbol code = %d\n", *it);
98  // char buf[5];
99  // int char_len = it.get_utf8(buf); buf[char_len] = '\0';
100  // tprintf("Char = %s\n", buf);
101  // }
103  typedef const_iterator CI;
104 
105  public:
106  // Step to the next UTF8 character.
107  // If the current position is at an illegal UTF8 character, then print an
108  // error message and step by one byte. If the current position is at a NULL
109  // value, don't step past it.
111 
112  // Return the UCS-4 value at the current position.
113  // If the current position is at an illegal UTF8 value, return a single
114  // space character.
115  int operator*() const;
116 
117  // Store the UTF-8 encoding of the current codepoint into buf, which must be
118  // at least 4 bytes long. Return the number of bytes written.
119  // If the current position is at an illegal UTF8 value, writes a single
120  // space character and returns 1.
121  // Note that this method does not null-terminate the buffer.
122  int get_utf8(char* buf) const;
123  // Returns the number of bytes of the current codepoint. Returns 1 if the
124  // current position is at an illegal UTF8 value.
125  int utf8_len() const;
126  // Returns true if the UTF-8 encoding at the current position is legal.
127  bool is_legal() const;
128 
129  // Return the pointer into the string at the current position.
130  const char* utf8_data() const { return it_; }
131 
132  // Iterator equality operators.
133  friend bool operator==(const CI& lhs, const CI& rhs) {
134  return lhs.it_ == rhs.it_;
135  }
136  friend bool operator!=(const CI& lhs, const CI& rhs) {
137  return !(lhs == rhs);
138  }
139 
140  private:
141  friend class UNICHAR;
142  explicit const_iterator(const char* it) : it_(it) {}
143 
144  const char* it_; // Pointer into the string.
145  };
146 
147  // Create a start/end iterator pointing to a string. Note that these methods
148  // are static and do NOT create a copy or take ownership of the underlying
149  // array.
150  static const_iterator begin(const char* utf8_str, const int byte_length);
151  static const_iterator end(const char* utf8_str, const int byte_length);
152 
153  // Converts a utf-8 string to a vector of unicodes.
154  // Returns false if the input contains invalid UTF-8, and replaces
155  // the rest of the string with a single space.
156  static bool UTF8ToUnicode(const char* utf8_str, GenericVector<int>* unicodes);
157 
158  private:
159  // A UTF-8 representation of 1 or more Unicode characters.
160  // The last element (chars[UNICHAR_LEN - 1]) is a length if
161  // its value < UNICHAR_LEN, otherwise it is a genuine character.
162  char chars[UNICHAR_LEN];
163 };
164 
165 #endif // TESSERACT_CCUTIL_UNICHAR_H_
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:200
int get_utf8(char *buf) const
Definition: unichar.cpp:174
int UNICHAR_ID
Definition: unichar.h:33
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:204
UNICHAR()
Definition: unichar.h:54
int utf8_len() const
Definition: unichar.h:72
int utf8_len() const
Definition: unichar.cpp:186
bool is_legal() const
Definition: unichar.cpp:196
friend bool operator!=(const CI &lhs, const CI &rhs)
Definition: unichar.h:136
const char * utf8_data() const
Definition: unichar.h:130
#define UNICHAR_LEN
Definition: unichar.h:30
const_iterator & operator++()
Definition: unichar.cpp:149
char * utf8_str() const
Definition: unichar.cpp:125
int operator*() const
Definition: unichar.cpp:163
const char * utf8() const
Definition: unichar.h:78
int first_uni() const
Definition: unichar.cpp:97
friend bool operator==(const CI &lhs, const CI &rhs)
Definition: unichar.h:133
StrongScriptDirection
Definition: unichar.h:40
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:134
static bool UTF8ToUnicode(const char *utf8_str, GenericVector< int > *unicodes)
Definition: unichar.cpp:211
voidpf void * buf
Definition: ioapi.h:39