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