Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
unichar.h
Go to the documentation of this file.
1
2 // 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 // Maximum number of characters that can be stored in a UNICHAR. Must be
27 // at least 4. Must not exceed 31 without changing the coding of length.
28 #define UNICHAR_LEN 30
29
30 // A UNICHAR_ID is the unique id of a unichar.
31 typedef int UNICHAR_ID;
32
33 // A variable to indicate an invalid or uninitialized unichar id.
34 static const int INVALID_UNICHAR_ID = -1;
35 // A special unichar that corresponds to INVALID_UNICHAR_ID.
36 static const char INVALID_UNICHAR[] = "__INVALID_UNICHAR__";
37
38 enum StrongScriptDirection {
39 DIR_NEUTRAL = 0, // Text contains only neutral characters.
40 DIR_LEFT_TO_RIGHT = 1, // Text contains no Right-to-Left characters.
41 DIR_RIGHT_TO_LEFT = 2, // Text contains no Left-to-Right characters.
42 DIR_MIX = 3, // Text contains a mixture of left-to-right
43 // and right-to-left characters.
44 };
45
46 // The UNICHAR class holds a single classification result. This may be
47 // a single Unicode character (stored as between 1 and 4 utf8 bytes) or
48 // multple Unicode characters representing the NFKC expansion of a ligature
49 // such as fi, ffl etc. These are also stored as utf8.
50 class UNICHAR {
51 public:
52 UNICHAR() {
53 memset(chars, 0, UNICHAR_LEN);
54 }
55
56 // Construct from a utf8 string. If len<0 then the string is null terminated.
57 // If the string is too long to fit in the UNICHAR then it takes only what
58 // will fit.
59 UNICHAR(const char* utf8_str, int len);
60
61 // Construct from a single UCS4 character.
62 explicit UNICHAR(int unicode);
63
64 // Default copy constructor and operator= are OK.
65
66 // Get the first character as UCS-4.
67 int first_uni() const;
68
69 // Get the length of the UTF8 string.
70 int utf8_len() const {
71 int len = chars[UNICHAR_LEN - 1];
72 return len >=0 && len < UNICHAR_LEN ? len : UNICHAR_LEN;
73 }
74
75 // Get a UTF8 string, but NOT NULL terminated.
76 const char* utf8() const {
77 return chars;
78 }
79
80 // Get a terminated UTF8 string: Must delete[] it after use.
81 char* utf8_str() const;
82
83 // Get the number of bytes in the first character of the given utf8 string.
84 static int utf8_step(const char* utf8_str);
85
86 private:
87 // A UTF-8 representation of 1 or more Unicode characters.
88 // The last element (chars[UNICHAR_LEN - 1]) is a length if
89 // its value < UNICHAR_LEN, otherwise it is a genuine character.
90 char chars[UNICHAR_LEN];
91 };
92
93 #endif // TESSERACT_CCUTIL_UNICHAR_H__