Update Tesseract to version 4.
This commit is contained in:
56 files changed
+10765
-3462
No files matched your search
@@ -17,18 +17,22 @@
|
||||
//
|
||||
///////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef TESSERACT_CCUTIL_UNICHAR_H__
|
||||
#define TESSERACT_CCUTIL_UNICHAR_H__
|
||||
#ifndef TESSERACT_CCUTIL_UNICHAR_H_
|
||||
#define TESSERACT_CCUTIL_UNICHAR_H_
|
||||
|
||||
#include <memory.h>
|
||||
#include <string.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include "platform.h"
|
||||
|
||||
// Maximum number of characters that can be stored in a UNICHAR. Must be
|
||||
// at least 4. Must not exceed 31 without changing the coding of length.
|
||||
#define UNICHAR_LEN 30
|
||||
|
||||
// TODO(rays) Move these to the tesseract namespace.
|
||||
// A UNICHAR_ID is the unique id of a unichar.
|
||||
typedef int UNICHAR_ID;
|
||||
using UNICHAR_ID = int;
|
||||
|
||||
// A variable to indicate an invalid or uninitialized unichar id.
|
||||
static const int INVALID_UNICHAR_ID = -1;
|
||||
@@ -43,9 +47,13 @@ enum StrongScriptDirection {
|
||||
// and right-to-left characters.
|
||||
};
|
||||
|
||||
namespace tesseract {
|
||||
|
||||
using char32 = signed int;
|
||||
|
||||
// The UNICHAR class holds a single classification result. This may be
|
||||
// a single Unicode character (stored as between 1 and 4 utf8 bytes) or
|
||||
// multple Unicode characters representing the NFKC expansion of a ligature
|
||||
// multiple Unicode characters representing the NFKC expansion of a ligature
|
||||
// such as fi, ffl etc. These are also stored as utf8.
|
||||
class UNICHAR {
|
||||
public:
|
||||
@@ -72,7 +80,7 @@ class UNICHAR {
|
||||
return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
|
||||
}
|
||||
|
||||
// Get a UTF8 string, but NOT NULL terminated.
|
||||
// Get a UTF8 string, but NOT nullptr terminated.
|
||||
const char* utf8() const {
|
||||
return chars;
|
||||
}
|
||||
@@ -83,6 +91,78 @@ class UNICHAR {
|
||||
// Get the number of bytes in the first character of the given utf8 string.
|
||||
static int utf8_step(const char* utf8_str);
|
||||
|
||||
// A class to simplify iterating over and accessing elements of a UTF8
|
||||
// string. Note that unlike the UNICHAR class, const_iterator does NOT COPY or
|
||||
// take ownership of the underlying byte array. It also does not permit
|
||||
// modification of the array (as the name suggests).
|
||||
//
|
||||
// Example:
|
||||
// for (UNICHAR::const_iterator it = UNICHAR::begin(str, str_len);
|
||||
// it != UNICHAR::end(str, len);
|
||||
// ++it) {
|
||||
// tprintf("UCS-4 symbol code = %d\n", *it);
|
||||
// char buf[5];
|
||||
// int char_len = it.get_utf8(buf); buf[char_len] = '\0';
|
||||
// tprintf("Char = %s\n", buf);
|
||||
// }
|
||||
class const_iterator {
|
||||
using CI = const_iterator ;
|
||||
|
||||
public:
|
||||
// Step to the next UTF8 character.
|
||||
// If the current position is at an illegal UTF8 character, then print an
|
||||
// error message and step by one byte. If the current position is at a nullptr
|
||||
// value, don't step past it.
|
||||
const_iterator& operator++();
|
||||
|
||||
// Return the UCS-4 value at the current position.
|
||||
// If the current position is at an illegal UTF8 value, return a single
|
||||
// space character.
|
||||
int operator*() const;
|
||||
|
||||
// Store the UTF-8 encoding of the current codepoint into buf, which must be
|
||||
// at least 4 bytes long. Return the number of bytes written.
|
||||
// If the current position is at an illegal UTF8 value, writes a single
|
||||
// space character and returns 1.
|
||||
// Note that this method does not null-terminate the buffer.
|
||||
int get_utf8(char* buf) const;
|
||||
// Returns the number of bytes of the current codepoint. Returns 1 if the
|
||||
// current position is at an illegal UTF8 value.
|
||||
int utf8_len() const;
|
||||
// Returns true if the UTF-8 encoding at the current position is legal.
|
||||
bool is_legal() const;
|
||||
|
||||
// Return the pointer into the string at the current position.
|
||||
const char* utf8_data() const { return it_; }
|
||||
|
||||
// Iterator equality operators.
|
||||
friend bool operator==(const CI& lhs, const CI& rhs) {
|
||||
return lhs.it_ == rhs.it_;
|
||||
}
|
||||
friend bool operator!=(const CI& lhs, const CI& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class UNICHAR;
|
||||
explicit const_iterator(const char* it) : it_(it) {}
|
||||
|
||||
const char* it_; // Pointer into the string.
|
||||
};
|
||||
|
||||
// Create a start/end iterator pointing to a string. Note that these methods
|
||||
// are static and do NOT create a copy or take ownership of the underlying
|
||||
// array.
|
||||
static const_iterator begin(const char* utf8_str, const int byte_length);
|
||||
static const_iterator end(const char* utf8_str, const int byte_length);
|
||||
|
||||
// Converts a utf-8 string to a vector of unicodes.
|
||||
// Returns an empty vector if the input contains invalid UTF-8.
|
||||
static std::vector<char32> UTF8ToUTF32(const char* utf8_str);
|
||||
// Converts a vector of unicodes to a utf8 string.
|
||||
// Returns an empty string if the input contains an invalid unicode.
|
||||
static std::string UTF32ToUTF8(const std::vector<char32>& str32);
|
||||
|
||||
private:
|
||||
// A UTF-8 representation of 1 or more Unicode characters.
|
||||
// The last element (chars[UNICHAR_LEN - 1]) is a length if
|
||||
@@ -90,4 +170,6 @@ class UNICHAR {
|
||||
char chars[UNICHAR_LEN];
|
||||
};
|
||||
|
||||
#endif // TESSERACT_CCUTIL_UNICHAR_H__
|
||||
} // namespace tesseract
|
||||
|
||||
#endif // TESSERACT_CCUTIL_UNICHAR_H_
|
||||
Reference in new issue
Block a user