tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
unichar.h
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 <cstring>
25 #include <string>
26 #include <vector>
27 #include "platform.h"
28 
29 // Maximum number of characters that can be stored in a UNICHAR. Must be
30 // at least 4. Must not exceed 31 without changing the coding of length.
31 #define UNICHAR_LEN 30
32 
33 // TODO(rays) Move these to the tesseract namespace.
34 // A UNICHAR_ID is the unique id of a unichar.
35 using UNICHAR_ID = int;
36 
37 // A variable to indicate an invalid or uninitialized unichar id.
38 static const int INVALID_UNICHAR_ID = -1;
39 // A special unichar that corresponds to INVALID_UNICHAR_ID.
40 static const char INVALID_UNICHAR[] = "__INVALID_UNICHAR__";
41 
42 enum StrongScriptDirection {
43  DIR_NEUTRAL = 0, // Text contains only neutral characters.
44  DIR_LEFT_TO_RIGHT = 1, // Text contains no Right-to-Left characters.
45  DIR_RIGHT_TO_LEFT = 2, // Text contains no Left-to-Right characters.
46  DIR_MIX = 3, // Text contains a mixture of left-to-right
47  // and right-to-left characters.
48 };
49 
50 namespace tesseract {
51 
52 using char32 = signed int;
53 
54 // The UNICHAR class holds a single classification result. This may be
55 // a single Unicode character (stored as between 1 and 4 utf8 bytes) or
56 // multiple Unicode characters representing the NFKC expansion of a ligature
57 // such as fi, ffl etc. These are also stored as utf8.
58 class UNICHAR {
59  public:
60  UNICHAR() {
61  memset(chars, 0, UNICHAR_LEN);
62  }
63 
64  // Construct from a utf8 string. If len<0 then the string is null terminated.
65  // If the string is too long to fit in the UNICHAR then it takes only what
66  // will fit.
67  UNICHAR(const char* utf8_str, int len);
68 
69  // Construct from a single UCS4 character.
70  explicit UNICHAR(int unicode);
71 
72  // Default copy constructor and operator= are OK.
73 
74  // Get the first character as UCS-4.
75  int first_uni() const;
76 
77  // Get the length of the UTF8 string.
78  int utf8_len() const {
79  int len = chars[UNICHAR_LEN - 1];
80  return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
81  }
82 
83  // Get a UTF8 string, but NOT nullptr terminated.
84  const char* utf8() const {
85  return chars;
86  }
87 
88  // Get a terminated UTF8 string: Must delete[] it after use.
89  char* utf8_str() const;
90 
91  // Get the number of bytes in the first character of the given utf8 string.
92  static int utf8_step(const char* utf8_str);
93 
94  // A class to simplify iterating over and accessing elements of a UTF8
95  // string. Note that unlike the UNICHAR class, const_iterator does NOT COPY or
96  // take ownership of the underlying byte array. It also does not permit
97  // modification of the array (as the name suggests).
98  //
99  // Example:
100  // for (UNICHAR::const_iterator it = UNICHAR::begin(str, str_len);
101  // it != UNICHAR::end(str, len);
102  // ++it) {
103  // tprintf("UCS-4 symbol code = %d\n", *it);
104  // char buf[5];
105  // int char_len = it.get_utf8(buf); buf[char_len] = '\0';
106  // tprintf("Char = %s\n", buf);
107  // }
109  using CI = const_iterator ;
110 
111  public:
112  // Step to the next UTF8 character.
113  // If the current position is at an illegal UTF8 character, then print an
114  // error message and step by one byte. If the current position is at a nullptr
115  // value, don't step past it.
117 
118  // Return the UCS-4 value at the current position.
119  // If the current position is at an illegal UTF8 value, return a single
120  // space character.
121  int operator*() const;
122 
123  // Store the UTF-8 encoding of the current codepoint into buf, which must be
124  // at least 4 bytes long. Return the number of bytes written.
125  // If the current position is at an illegal UTF8 value, writes a single
126  // space character and returns 1.
127  // Note that this method does not null-terminate the buffer.
128  int get_utf8(char* buf) const;
129  // Returns the number of bytes of the current codepoint. Returns 1 if the
130  // current position is at an illegal UTF8 value.
131  int utf8_len() const;
132  // Returns true if the UTF-8 encoding at the current position is legal.
133  bool is_legal() const;
134 
135  // Return the pointer into the string at the current position.
136  const char* utf8_data() const { return it_; }
137 
138  // Iterator equality operators.
139  friend bool operator==(const CI& lhs, const CI& rhs) {
140  return lhs.it_ == rhs.it_;
141  }
142  friend bool operator!=(const CI& lhs, const CI& rhs) {
143  return !(lhs == rhs);
144  }
145 
146  private:
147  friend class UNICHAR;
148  explicit const_iterator(const char* it) : it_(it) {}
149 
150  const char* it_; // Pointer into the string.
151  };
152 
153  // Create a start/end iterator pointing to a string. Note that these methods
154  // are static and do NOT create a copy or take ownership of the underlying
155  // array.
156  static const_iterator begin(const char* utf8_str, const int byte_length);
157  static const_iterator end(const char* utf8_str, const int byte_length);
158 
159  // Converts a utf-8 string to a vector of unicodes.
160  // Returns an empty vector if the input contains invalid UTF-8.
161  static std::vector<char32> UTF8ToUTF32(const char* utf8_str);
162  // Converts a vector of unicodes to a utf8 string.
163  // Returns an empty string if the input contains an invalid unicode.
164  static std::string UTF32ToUTF8(const std::vector<char32>& str32);
165 
166  private:
167  // A UTF-8 representation of 1 or more Unicode characters.
168  // The last element (chars[UNICHAR_LEN - 1]) is a length if
169  // its value < UNICHAR_LEN, otherwise it is a genuine character.
170  char chars[UNICHAR_LEN];
171 };
172 
173 } // namespace tesseract
174 
175 #endif // TESSERACT_CCUTIL_UNICHAR_H_
const_iterator(const char *it)
Definition: unichar.h:148
const_iterator & operator++()
Definition: unichar.cpp:151
Definition: unichar.h:58
signed int char32
Definition: unichar.h:52
bool is_legal() const
Definition: unichar.cpp:198
Definition: unichar.h:108
static int utf8_step(const char *utf8_str)
Definition: unichar.cpp:136
static const_iterator begin(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:202
int operator*() const
Definition: unichar.cpp:165
const char * utf8_data() const
Definition: unichar.h:136
char * utf8_str() const
Definition: unichar.cpp:127
int utf8_len() const
Definition: unichar.cpp:188
Definition: baseapi.cpp:94
const char * utf8() const
Definition: unichar.h:84
static std::vector< char32 > UTF8ToUTF32(const char *utf8_str)
Definition: unichar.cpp:213
int get_utf8(char *buf) const
Definition: unichar.cpp:176
const char * it_
Definition: unichar.h:150
friend bool operator==(const CI &lhs, const CI &rhs)
Definition: unichar.h:139
friend bool operator!=(const CI &lhs, const CI &rhs)
Definition: unichar.h:142
static const_iterator end(const char *utf8_str, const int byte_length)
Definition: unichar.cpp:206
int utf8_len() const
Definition: unichar.h:78
static std::string UTF32ToUTF8(const std::vector< char32 > &str32)
Definition: unichar.cpp:230
UNICHAR()
Definition: unichar.h:60
char chars[30]
Definition: unichar.h:170
int first_uni() const
Definition: unichar.cpp:99